From: Cédric Villemain Date: Thu, 4 Jun 2009 11:29:19 +0000 (+0200) Subject: Fonction quasi ok X-Git-Tag: 0.1~3 X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=9f60095dba35185013ee3b93d55545f594281b38;p=pgfincore.git Fonction quasi ok manque un peu de debug sur free ... --- diff --git a/README.pgfincore b/README.pgfincore new file mode 100644 index 0000000..e69de29 diff --git a/pgfincore.c b/pgfincore.c index 833f58b..a27f99d 100644 --- a/pgfincore.c +++ b/pgfincore.c @@ -25,7 +25,9 @@ */ -#include "postgres.h" +#include "postgres.h" /* general Postgres declarations */ + + #include "mb/pg_wchar.h" #include "utils/elog.h" @@ -52,116 +54,120 @@ PG_MODULE_MAGIC; #include #include -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); } diff --git a/pgfincore.sql b/pgfincore.sql deleted file mode 100644 index 2041547..0000000 --- a/pgfincore.sql +++ /dev/null @@ -1,11 +0,0 @@ -SET search_path = public; - -CREATE OR REPLACE FUNCTION pgfincore(text) -RETURNS text -AS '$libdir/pgfincore','pgfincore' -LANGUAGE C STABLE STRICT; -CREATE OR REPLACE FUNCTION pgfincore(oid) -RETURNS text -AS '$libdir/pgfincore','pgfincore' -LANGUAGE C STABLE STRICT; - diff --git a/pgfincore.sql.in b/pgfincore.sql.in index b5d4809..750d8e7 100644 --- a/pgfincore.sql.in +++ b/pgfincore.sql.in @@ -4,8 +4,8 @@ CREATE OR REPLACE FUNCTION pgfincore(text) RETURNS text AS 'MODULE_PATHNAME','pgfincore' LANGUAGE C STABLE STRICT; -CREATE OR REPLACE FUNCTION pgfincore(oid) -RETURNS text -AS 'MODULE_PATHNAME','pgfincore' -LANGUAGE C STABLE STRICT; +-- CREATE OR REPLACE FUNCTION pgfincore(oid) +-- RETURNS text +-- AS '$libdir/pgfincore','pgfincore' +-- LANGUAGE C STABLE STRICT;