*/
-#include "postgres.h"
+#include "postgres.h" /* general Postgres declarations */
+
+
#include "mb/pg_wchar.h"
#include "utils/elog.h"
#include <unistd.h>
#include <sys/mman.h>
-Datum
-pgfincore(PG_FUNCTION_ARGS);
+Datum pgfincore(PG_FUNCTION_ARGS); /* Prototype */
PG_FUNCTION_INFO_V1(pgfincore);
/* fincore -
*/
-Datum
-pgfincore(PG_FUNCTION_ARGS) {
- char *filename = (char *)PG_GETARG_CSTRING(0);
- int filename_size = VARSIZE(filename) - VARHDRSZ;
+Datum pgfincore(PG_FUNCTION_ARGS) {
+ VarChar *str_in = (VarChar *)PG_GETARG_VARCHAR_P(0);
+ int str_in_size = VARSIZE(str_in) - VARHDRSZ;
+ char *filename;
VarChar *return_pgfincore;
- char *buffer_in,*output;
+ int n=0;
+ char *output;
- int fd;
- struct stat st;
- void *pa = (char *)0;
- char *vec = (char *)0;
- register size_t n = 0;
- size_t pageSize = getpagesize();
- register size_t pageIndex;
- fd = open(filename, 0);
- if (0 > fd) {
- elog(ERROR, "Can not open object file : %s", filename);
- return 0;
- }
-
- if (0 != fstat(fd, &st)) {
- elog(ERROR, "Can not stat object file : %s", filename);
- close(fd);
- return 0;
- }
-
- pa = mmap((void *)0, st.st_size, PROT_NONE, MAP_SHARED, fd, 0);
- if (MAP_FAILED == pa) {
- elog(ERROR, "Can not mmap object file : %s", filename);
- close(fd);
- return 0;
- }
-
- /* vec = calloc(1, 1+st.st_size/pageSize); */
- vec = calloc(1, (st.st_size+pageSize-1)/pageSize);
- if ((void *)0 == vec) {
- elog(ERROR, "Can not calloc object file : %s", filename);
- close(fd);
- return 0;
- }
-
- if (0 != mincore(pa, st.st_size, vec)) {
- /* perror("mincore"); */
- elog(ERROR, "mincore(%p, %lu, %p): %s\n",
- pa, (unsigned long)st.st_size, vec, strerror(errno));
- free(vec);
- close(fd);
- return 0;
- }
-
- /* handle the results */
- for (pageIndex = 0; pageIndex <= st.st_size/pageSize; pageIndex++) {
- if (vec[pageIndex]&1) {
- elog (NOTICE, "r: %lu", (unsigned long)pageIndex); /* TODO fix that /!\ (on veut concaténer tous les résultats pour le moment)*/
- }
- }
-
- free(vec);
- vec = (char *)0;
-
- munmap(pa, st.st_size);
- close(fd);
+ // for open file
+ int fd;
+ // for stat file
+ struct stat st;
+ // for mmap file
+ void *pa = (char *)0;
+ // for calloc file
+ char *vec = (char *)0;
+
+ size_t pageSize = getpagesize();
+
+
+ register size_t pageIndex;
- /* convert string to VarChar for result */
- return_pgfincore = DatumGetVarCharP(
- DirectFunctionCall2(
- varcharin,
- CStringGetDatum(output),
- Int32GetDatum(strlen(output) + VARHDRSZ)
- )
- );
-#ifdef PGFINCORE_DEBUG
- elog(NOTICE, "pgfincore : %s", return_pgfincore);
-#endif /*PGFINCORE_DEBUG*/
- PG_RETURN_VARCHAR_P(return_pgfincore);
-}
-/* END */
- void *h ;
pg_verifymbstr(VARDATA(str_in), str_in_size, false);
- buffer_in = DatumGetCString(
+ filename = DatumGetCString(
DirectFunctionCall1(textout,
PointerGetDatum(str_in)
)
);
PG_FREE_IF_COPY(str_in, 0);
- h = textcat_Init( "/usr/share/postgresql/8.2/contrib/textcat_conf.txt" );
- if (!h) {
- elog(ERROR, "can not open config file : /usr/share/postgresql/8.2/contrib/textcat_conf.txt or one of the fingerprints.");
- PG_RETURN_VARCHAR_P("unable to open one file");
+
+
+
+
+
+
+
+ fd = open(filename, 0);
+ if (0 > fd) {
+ elog(ERROR, "Can not open object file : %s", filename);
+ return 0;
+ }
+
+ if (0 != fstat(fd, &st)) {
+ elog(ERROR, "Can not stat object file : %s", filename);
+ close(fd);
+ return 0;
+ }
+
+ pa = mmap((void *)0, st.st_size, PROT_NONE, MAP_SHARED, fd, 0);
+ if (MAP_FAILED == pa) {
+ elog(ERROR, "Can not mmap object file : %s", filename);
+ close(fd);
+ return 0;
+ }
+
+ /* vec = calloc(1, 1+st.st_size/pageSize); */
+ vec = calloc(1, (st.st_size+pageSize-1)/pageSize);
+ if ((void *)0 == vec) {
+ elog(ERROR, "Can not calloc object file : %s", filename);
+ close(fd);
+ return 0;
+ }
+
+ if (0 != mincore(pa, st.st_size, vec)) {
+ /* perror("mincore"); */
+ elog(ERROR, "mincore(%p, %lu, %p): %s\n",
+ pa, (unsigned long)st.st_size, vec, strerror(errno));
+ free(vec);
+ close(fd);
+ return 0;
+ }
+
+ /* handle the results */
+ for (pageIndex = 0; pageIndex <= st.st_size/pageSize; pageIndex++) {
+ if (vec[pageIndex]&1) {
+ n++;
+// elog (NOTICE, "r: %lu / %lu", (unsigned long)pageIndex, (unsigned long)(st.st_size/pageSize)); /* TODO fix that /!\ (on veut concaténer tous les résultats pour le moment)*/
+ }
}
+ free(vec);
+ vec = (char *)0;
+ munmap(pa, st.st_size);
+ close(fd);
+
+ elog(ERROR, "pgfincore : %lu of %lu block in linux cache",(unsigned long)n, (unsigned long)(st.st_size/pageSize));
+ /* convert string to VarChar for result */
+ return_pgfincore = DatumGetVarCharP(
+ DirectFunctionCall2(
+ varcharin,
+ CStringGetDatum(output),
+ Int32GetDatum(strlen(output) + VARHDRSZ)
+ )
+ );
+
+ PG_RETURN_VARCHAR_P(return_pgfincore);
}