Fonction quasi ok
authorCédric Villemain <cedric.villemain.debian@gmail.com>
Thu, 4 Jun 2009 11:29:19 +0000 (13:29 +0200)
committerCédric Villemain <cedric.villemain.debian@gmail.com>
Thu, 4 Jun 2009 11:29:19 +0000 (13:29 +0200)
manque un peu de debug sur free ...

README.pgfincore [new file with mode: 0644]
pgfincore.c
pgfincore.sql [deleted file]
pgfincore.sql.in

diff --git a/README.pgfincore b/README.pgfincore
new file mode 100644 (file)
index 0000000..e69de29
index 833f58bf54b677a493056ec4fe04f916096965b2..a27f99d1255a28b363d15f7c5e6ba53c61b04d19 100644 (file)
@@ -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 <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);
 }
 
diff --git a/pgfincore.sql b/pgfincore.sql
deleted file mode 100644 (file)
index 2041547..0000000
+++ /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;
-
index b5d4809669cff2c87bee08791bf28d977994832d..750d8e750732636b10d4659d4a057949e735e026 100644 (file)
@@ -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;