Add debug code to aid in memory-leak tracking: if SHOW_MEMORY_STATS is
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 21 May 2000 02:23:30 +0000 (02:23 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 21 May 2000 02:23:30 +0000 (02:23 +0000)
defined then statistics about memory usage of all the global memory
contexts are printed after each commit.

src/backend/tcop/postgres.c
src/backend/utils/mmgr/aset.c
src/backend/utils/mmgr/mcxt.c
src/include/utils/mcxt.h
src/include/utils/memutils.h

index b6c6935613058913d9f52d31b3fd459dcb95266a..683f057f21266894bba16fc4da457085ba061eb1 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.154 2000/04/30 21:29:23 tgl Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.155 2000/05/21 02:23:30 tgl Exp $
  *
  * NOTES
  *       this is the "main" module of the postgres backend and
@@ -1452,7 +1452,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
        if (!IsUnderPostmaster)
        {
                puts("\nPOSTGRES backend interactive interface ");
-               puts("$Revision: 1.154 $ $Date: 2000/04/30 21:29:23 $\n");
+               puts("$Revision: 1.155 $ $Date: 2000/05/21 02:23:30 $\n");
        }
 
        /*
@@ -1631,6 +1631,11 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
                                TPRINTF(TRACE_VERBOSE, "CommitTransactionCommand");
                        PS_SET_STATUS("commit");
                        CommitTransactionCommand();
+#ifdef SHOW_MEMORY_STATS
+                       /* print global-context stats at each commit for leak tracking */
+                       if (ShowStats)
+                               GlobalMemoryStats();
+#endif
                }
                else
                {
index 4bc96c5a2aa61e667761c11b99a3c258e84cb558..574b98697d2062194f1c3629d85a595fbf767dc5 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/utils/mmgr/aset.c,v 1.26 2000/04/12 17:16:09 momjian Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/utils/mmgr/aset.c,v 1.27 2000/05/21 02:23:29 tgl Exp $
  *
  * NOTE:
  *     This is a new (Feb. 05, 1999) implementation of the allocation set
@@ -541,3 +541,41 @@ AllocSetDump(AllocSet set)
 {
        elog(DEBUG, "Currently unable to dump AllocSet");
 }
+
+/*
+ * AllocSetStats
+ *             Displays stats about memory consumption of an allocset.
+ */
+void
+AllocSetStats(AllocSet set, const char *ident)
+{
+       long            nblocks = 0;
+       long            nchunks = 0;
+       long            totalspace = 0;
+       long            freespace = 0;
+       AllocBlock      block;
+       AllocChunk      chunk;
+       int                     fidx;
+
+       AssertArg(AllocSetIsValid(set));
+
+       for (block = set->blocks; block != NULL; block = block->next)
+       {
+               nblocks++;
+               totalspace += block->endptr - ((char *) block);
+               freespace += block->endptr - block->freeptr;
+       }
+       for (fidx = 0; fidx < ALLOCSET_NUM_FREELISTS; fidx++)
+       {
+               for (chunk = set->freelist[fidx]; chunk != NULL;
+                        chunk = (AllocChunk) chunk->aset)
+               {
+                       nchunks++;
+                       freespace += chunk->size + ALLOC_CHUNKHDRSZ;
+               }
+       }
+       fprintf(stderr,
+                       "%s: %ld total in %ld blocks; %ld free (%ld chunks); %ld used\n",
+                       ident, totalspace, nblocks, freespace, nchunks,
+                       totalspace - freespace);
+}
index 29af5ce8e2a0e92c523b9104d155cc24c21eb92a..5a3be6700e679b4ba7a37edbe5b8e114f845a8cd 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.20 2000/01/26 05:57:30 momjian Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.21 2000/05/21 02:23:29 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -489,7 +489,7 @@ GlobalMemoryDump(GlobalMemory this)
        if (PointerIsValid(context))
                printf("\tsucessor=%s\n", GlobalMemoryGetName(context));
 
-       AllocSetDump(&this->setData);           /* XXX is this right interface */
+       AllocSetDump(&this->setData);
 }
 
 /*
@@ -511,9 +511,26 @@ DumpGlobalMemories()
        {
                GlobalMemoryDump(context);
 
-               context = (GlobalMemory) OrderedElemGetSuccessor(
-                                                                                                        &context->elemData);
+               context = (GlobalMemory) OrderedElemGetSuccessor(&context->elemData);
        }
 }
 
 #endif
+
+/*
+ * GlobalMemoryStats
+ *             Displays stats about memory consumption of all global contexts.
+ */
+void
+GlobalMemoryStats(void)
+{
+       GlobalMemory context;
+
+       context = (GlobalMemory) OrderedSetGetHead(&ActiveGlobalMemorySetData);
+
+       while (PointerIsValid(context))
+       {
+               AllocSetStats(&context->setData, GlobalMemoryGetName(context));
+               context = (GlobalMemory) OrderedElemGetSuccessor(&context->elemData);
+       }
+}
index 2f7ae1cf5d300f234e7fdb8f064c4110fb9bf488..7b867d86649ed80360d75a4aa95578cacda90766 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: mcxt.h,v 1.16 2000/01/26 05:58:38 momjian Exp $
+ * $Id: mcxt.h,v 1.17 2000/05/21 02:23:28 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -54,6 +54,7 @@ extern void MemoryContextFree(MemoryContext context, Pointer pointer);
 extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
 extern GlobalMemory CreateGlobalMemory(char *name);
 extern void GlobalMemoryDestroy(GlobalMemory context);
+extern void GlobalMemoryStats(void);
 
 
 #endif  /* MCXT_H */
index a42bce402cdfe83e517533550c9b104c390d2edc..3e6ad2e53da223ab0772ee598bac8f57b2ae9f39 100644 (file)
@@ -13,7 +13,7 @@
  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: memutils.h,v 1.34 2000/04/12 17:16:55 momjian Exp $
+ * $Id: memutils.h,v 1.35 2000/05/21 02:23:28 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -224,6 +224,7 @@ extern AllocPointer AllocSetRealloc(AllocSet set, AllocPointer pointer,
                                Size size);
 
 extern void AllocSetDump(AllocSet set);
+extern void AllocSetStats(AllocSet set, const char *ident);
 
 
 #endif  /* MEMUTILS_H */