Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit aa16179

Browse files
committed
Add debug code to aid in memory-leak tracking: if SHOW_MEMORY_STATS is
defined then statistics about memory usage of all the global memory contexts are printed after each commit.
1 parent 25a7a7f commit aa16179

File tree

5 files changed

+71
-9
lines changed

5 files changed

+71
-9
lines changed

src/backend/tcop/postgres.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.154 2000/04/30 21:29:23 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.155 2000/05/21 02:23:30 tgl Exp $
1212
*
1313
* NOTES
1414
* 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[])
14521452
if (!IsUnderPostmaster)
14531453
{
14541454
puts("\nPOSTGRES backend interactive interface ");
1455-
puts("$Revision: 1.154 $ $Date: 2000/04/30 21:29:23 $\n");
1455+
puts("$Revision: 1.155 $ $Date: 2000/05/21 02:23:30 $\n");
14561456
}
14571457

14581458
/*
@@ -1631,6 +1631,11 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
16311631
TPRINTF(TRACE_VERBOSE, "CommitTransactionCommand");
16321632
PS_SET_STATUS("commit");
16331633
CommitTransactionCommand();
1634+
#ifdef SHOW_MEMORY_STATS
1635+
/* print global-context stats at each commit for leak tracking */
1636+
if (ShowStats)
1637+
GlobalMemoryStats();
1638+
#endif
16341639
}
16351640
else
16361641
{

src/backend/utils/mmgr/aset.c

+39-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/aset.c,v 1.26 2000/04/12 17:16:09 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/aset.c,v 1.27 2000/05/21 02:23:29 tgl Exp $
1212
*
1313
* NOTE:
1414
* This is a new (Feb. 05, 1999) implementation of the allocation set
@@ -541,3 +541,41 @@ AllocSetDump(AllocSet set)
541541
{
542542
elog(DEBUG, "Currently unable to dump AllocSet");
543543
}
544+
545+
/*
546+
* AllocSetStats
547+
* Displays stats about memory consumption of an allocset.
548+
*/
549+
void
550+
AllocSetStats(AllocSet set, const char *ident)
551+
{
552+
long nblocks = 0;
553+
long nchunks = 0;
554+
long totalspace = 0;
555+
long freespace = 0;
556+
AllocBlock block;
557+
AllocChunk chunk;
558+
int fidx;
559+
560+
AssertArg(AllocSetIsValid(set));
561+
562+
for (block = set->blocks; block != NULL; block = block->next)
563+
{
564+
nblocks++;
565+
totalspace += block->endptr - ((char *) block);
566+
freespace += block->endptr - block->freeptr;
567+
}
568+
for (fidx = 0; fidx < ALLOCSET_NUM_FREELISTS; fidx++)
569+
{
570+
for (chunk = set->freelist[fidx]; chunk != NULL;
571+
chunk = (AllocChunk) chunk->aset)
572+
{
573+
nchunks++;
574+
freespace += chunk->size + ALLOC_CHUNKHDRSZ;
575+
}
576+
}
577+
fprintf(stderr,
578+
"%s: %ld total in %ld blocks; %ld free (%ld chunks); %ld used\n",
579+
ident, totalspace, nblocks, freespace, nchunks,
580+
totalspace - freespace);
581+
}

src/backend/utils/mmgr/mcxt.c

+21-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.20 2000/01/26 05:57:30 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.21 2000/05/21 02:23:29 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -489,7 +489,7 @@ GlobalMemoryDump(GlobalMemory this)
489489
if (PointerIsValid(context))
490490
printf("\tsucessor=%s\n", GlobalMemoryGetName(context));
491491

492-
AllocSetDump(&this->setData); /* XXX is this right interface */
492+
AllocSetDump(&this->setData);
493493
}
494494

495495
/*
@@ -511,9 +511,26 @@ DumpGlobalMemories()
511511
{
512512
GlobalMemoryDump(context);
513513

514-
context = (GlobalMemory) OrderedElemGetSuccessor(
515-
&context->elemData);
514+
context = (GlobalMemory) OrderedElemGetSuccessor(&context->elemData);
516515
}
517516
}
518517

519518
#endif
519+
520+
/*
521+
* GlobalMemoryStats
522+
* Displays stats about memory consumption of all global contexts.
523+
*/
524+
void
525+
GlobalMemoryStats(void)
526+
{
527+
GlobalMemory context;
528+
529+
context = (GlobalMemory) OrderedSetGetHead(&ActiveGlobalMemorySetData);
530+
531+
while (PointerIsValid(context))
532+
{
533+
AllocSetStats(&context->setData, GlobalMemoryGetName(context));
534+
context = (GlobalMemory) OrderedElemGetSuccessor(&context->elemData);
535+
}
536+
}

src/include/utils/mcxt.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: mcxt.h,v 1.16 2000/01/26 05:58:38 momjian Exp $
10+
* $Id: mcxt.h,v 1.17 2000/05/21 02:23:28 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -54,6 +54,7 @@ extern void MemoryContextFree(MemoryContext context, Pointer pointer);
5454
extern MemoryContext MemoryContextSwitchTo(MemoryContext context);
5555
extern GlobalMemory CreateGlobalMemory(char *name);
5656
extern void GlobalMemoryDestroy(GlobalMemory context);
57+
extern void GlobalMemoryStats(void);
5758

5859

5960
#endif /* MCXT_H */

src/include/utils/memutils.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
1414
* Portions Copyright (c) 1994, Regents of the University of California
1515
*
16-
* $Id: memutils.h,v 1.34 2000/04/12 17:16:55 momjian Exp $
16+
* $Id: memutils.h,v 1.35 2000/05/21 02:23:28 tgl Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -224,6 +224,7 @@ extern AllocPointer AllocSetRealloc(AllocSet set, AllocPointer pointer,
224224
Size size);
225225

226226
extern void AllocSetDump(AllocSet set);
227+
extern void AllocSetStats(AllocSet set, const char *ident);
227228

228229

229230
#endif /* MEMUTILS_H */

0 commit comments

Comments
 (0)