add pgmincore_snapshot --> write mincore to file
authorCédric Villemain <cedric.villemain.debian@gmail.com>
Tue, 22 Dec 2009 00:26:32 +0000 (01:26 +0100)
committerCédric Villemain <cedric.villemain.debian@gmail.com>
Tue, 22 Dec 2009 00:26:32 +0000 (01:26 +0100)
ChangeLog
pgfincore.c
pgfincore.sql.in

index 44095b2780691cc0f2af305be0e52b7a51e3bd4b..7cc8f3a949f1be29cb5232d620ea6067f94928ed 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,4 @@
-10/12/2009 Cédric Villemain <cedric@villemain.org>
+22/12/2009 Cédric Villemain <cedric@villemain.org>
 
   * 0.4.0 - fix copyright
           - fix test is not temp table
@@ -10,6 +10,7 @@
                  - rewrite main SRF
                  - improve output (more informations)
                  - add pgsysconf()
+                 - add pgmincore_snapshot to write mincore state in a file
 
 26/10/2009 Cédric Villemain <cedric.villemain@dalibo.com>
 
index fb61836784bcf6c659a1711f341fb7f8503a153e..a1a868a5c2f4280c9c90b28ca2f24dadf80c242a 100644 (file)
@@ -41,7 +41,7 @@ typedef struct
 
 Datum pgsysconf(PG_FUNCTION_ARGS);
 Datum pgfincore(PG_FUNCTION_ARGS);
-static Datum pgmincore_file(char *filename, FunctionCallInfo fcinfo);
+static Datum pgmincore_file(char *filename, int writeStat, FunctionCallInfo fcinfo);
 static Datum pgfadvise_file(char *filename, int action, FunctionCallInfo fcinfo);
 
 /*
@@ -159,14 +159,17 @@ pgfincore(PG_FUNCTION_ARGS)
   */
   switch (fctx->action)
   {
-       case 1 : /* MINCORE */
-         result = pgmincore_file(pathname, fcinfo);
+       case 10 : /* MINCORE */
+         result = pgmincore_file(pathname, 0, fcinfo);
        break;
-       case 2 : /* FADVISE_WILLNEED */
-       case 3 : /* FADVISE_DONTNEED */
-       case 4 : /* POSIX_FADV_NORMAL */
-       case 5 : /* POSIX_FADV_SEQUENTIAL */
-       case 6 : /* POSIX_FADV_RANDOM */
+       case 11 : /* MINCORE with snapshot in a file */
+         result = pgmincore_file(pathname, 1, fcinfo);
+       break;
+       case 20 : /* FADVISE_WILLNEED */
+       case 30 : /* FADVISE_DONTNEED */
+       case 40 : /* POSIX_FADV_NORMAL */
+       case 50 : /* POSIX_FADV_SEQUENTIAL */
+       case 60 : /* POSIX_FADV_RANDOM */
          /* pgfadvise_file handle several flags, thanks to the same action value */
          result = pgfadvise_file(pathname, fctx->action, fcinfo);
        break;
@@ -196,7 +199,7 @@ pgfincore(PG_FUNCTION_ARGS)
  * pgmincore_file handle the mmaping, mincore process (and access file, etc.)
  */
 static Datum
-pgmincore_file(char *filename, FunctionCallInfo fcinfo) {
+pgmincore_file(char *filename, int writeStat, FunctionCallInfo fcinfo) {
   HeapTuple    tuple;
   TupleDesc tupdesc;
   Datum                values[5];
@@ -230,7 +233,7 @@ pgmincore_file(char *filename, FunctionCallInfo fcinfo) {
 
 /* Do the main work */
   fd = open(filename, O_RDONLY);
-  if (fd == -1) {
+         if (fd == -1) {
     goto error;
   }
   if (fstat(fd, &st) == -1) {
@@ -269,8 +272,30 @@ pgmincore_file(char *filename, FunctionCallInfo fcinfo) {
          goto error;
        }
 
+       /*
+       * the data from mincore is fwrite to a file contigous to the relation file
+       * in the PGDATA, suffix : _mincore
+       * FIXME use some postgres internal for that ?
+       */
+       if (writeStat) {
+           FILE       *f;
+               int64       count = 0;
+
+               f = fopen(strcat(filename,"_mincore") , "wb");
+               // TODO check the size is correct.
+               count = fwrite(vec, 1, ((st.st_size+pageSize-1)/pageSize)/8 , f);
+
+               elog(DEBUG1, "writeStat count : %ld",count);
+
+        if (count != ((st.st_size+pageSize-1)/pageSize)/8)
+            ereport(ERROR,
+                    (errcode_for_file_access(),
+                     errmsg("could not write file \"%s\"_mincore: %m", filename)));
+       fclose(f);
+    }
+
        /* handle the results */
-       for (pageIndex = 0; pageIndex <= st.st_size/pageSize; pageIndex++) {
+         for (pageIndex = 0; pageIndex <= st.st_size/pageSize; pageIndex++) {
          // block in memory
          if (vec[pageIndex] & 1) {
                block_mem++;
@@ -358,23 +383,23 @@ pgfadvise_file(char *filename, int action, FunctionCallInfo fcinfo)
 
   switch (action)
   {
-       case 2 : /* FADVISE_WILLNEED */
+       case 20 : /* FADVISE_WILLNEED */
          elog(DEBUG1, "pgfadv_willneed: setting flag");
          posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED);
        break;
-       case 3 : /* FADVISE_DONTNEED */
+       case 30 : /* FADVISE_DONTNEED */
          elog(DEBUG1, "pgfadv_dontneed: setting flag");
          posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
        break;
-       case 4 : /* POSIX_FADV_NORMAL */
+       case 40 : /* POSIX_FADV_NORMAL */
          elog(DEBUG1, "pgfadv_normal: setting flag");
          posix_fadvise(fd, 0, 0, POSIX_FADV_NORMAL);
        break;
-       case 5 : /* POSIX_FADV_SEQUENTIAL */
+       case 50 : /* POSIX_FADV_SEQUENTIAL */
          elog(DEBUG1, "pgfadv_sequential: setting flag");
          posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
        break;
-       case 6 : /* POSIX_FADV_RANDOM */
+       case 60 : /* POSIX_FADV_RANDOM */
          elog(DEBUG1, "pgfadv_random: setting flag");
          posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM);
        break;
index fd60df21f1f413f857b7e23412a0e5e69c6d6fb7..c5a7f65502eb06e2441d26ed4fd157f29d67e911 100644 (file)
@@ -24,7 +24,18 @@ pgmincore(IN regclass,
                  OUT block_mem bigint,
                  OUT group_mem bigint)
 RETURNS setof record
-AS 'SELECT pgfincore($1, ''main'', 1)'
+AS 'SELECT pgfincore($1, ''main'', 10)'
+LANGUAGE SQL;
+
+CREATE OR REPLACE FUNCTION
+pgmincore_snapshot(IN regclass,
+                 OUT relpath text,
+                 OUT block_size bigint,
+                 OUT block_disk bigint,
+                 OUT block_mem bigint,
+                 OUT group_mem bigint)
+RETURNS setof record
+AS 'SELECT pgfincore($1, ''main'', 11)'
 LANGUAGE SQL;
 
 CREATE OR REPLACE FUNCTION
@@ -35,7 +46,7 @@ pgfadv_willneed(IN regclass,
                                OUT block_cache bigint,
                                OUT block_free bigint)
 RETURNS setof record
-AS 'SELECT pgfincore($1, ''main'', 2)'
+AS 'SELECT pgfincore($1, ''main'', 20)'
 LANGUAGE SQL;
 
 CREATE OR REPLACE FUNCTION
@@ -46,7 +57,7 @@ pgfadv_dontneed(IN regclass,
                                OUT block_cache bigint,
                                OUT block_free bigint)
 RETURNS setof record
-AS 'SELECT pgfincore($1, ''main'', 3)'
+AS 'SELECT pgfincore($1, ''main'', 30)'
 LANGUAGE SQL;
 
 CREATE OR REPLACE FUNCTION
@@ -57,7 +68,7 @@ pgfadv_normal(IN regclass,
                          OUT block_cache bigint,
                          OUT block_free bigint)
 RETURNS setof record
-AS 'SELECT pgfincore($1, ''main'', 4)'
+AS 'SELECT pgfincore($1, ''main'', 40)'
 LANGUAGE SQL;
 
 CREATE OR REPLACE FUNCTION
@@ -68,7 +79,7 @@ pgfadv_sequential(IN regclass,
                                  OUT block_cache bigint,
                                  OUT block_free bigint)
 RETURNS setof record
-AS 'SELECT pgfincore($1, ''main'', 5)'
+AS 'SELECT pgfincore($1, ''main'', 50)'
 LANGUAGE SQL;
 
 CREATE OR REPLACE FUNCTION
@@ -79,5 +90,5 @@ pgfadv_random(IN regclass,
                          OUT block_cache bigint,
                          OUT block_free bigint)
 RETURNS setof record
-AS 'SELECT pgfincore($1, ''main'', 6)'
+AS 'SELECT pgfincore($1, ''main'', 60)'
 LANGUAGE SQL;