diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c index 08aff33..19f3756 100644 --- a/src/backend/utils/mmgr/aset.c +++ b/src/backend/utils/mmgr/aset.c @@ -778,6 +778,8 @@ AllocSetAlloc(MemoryContext context, Size size) /* Disallow external access to private part of chunk header. */ VALGRIND_MAKE_MEM_NOACCESS(chunk, ALLOCCHUNK_PRIVATE_LEN); + context->usedspace += chunk_size; + return AllocChunkGetPointer(chunk); } @@ -817,6 +819,8 @@ AllocSetAlloc(MemoryContext context, Size size) /* Disallow external access to private part of chunk header. */ VALGRIND_MAKE_MEM_NOACCESS(chunk, ALLOCCHUNK_PRIVATE_LEN); + context->usedspace += chunk->size; + return AllocChunkGetPointer(chunk); } @@ -976,6 +980,8 @@ AllocSetAlloc(MemoryContext context, Size size) /* Disallow external access to private part of chunk header. */ VALGRIND_MAKE_MEM_NOACCESS(chunk, ALLOCCHUNK_PRIVATE_LEN); + context->usedspace += chunk_size; + return AllocChunkGetPointer(chunk); } @@ -1022,6 +1028,7 @@ AllocSetFree(MemoryContext context, void *pointer) elog(ERROR, "could not find block containing chunk %p", chunk); /* OK, remove block from aset's list and free it */ + context->usedspace -= chunk->size; if (block->prev) block->prev->next = block->next; else @@ -1039,6 +1046,7 @@ AllocSetFree(MemoryContext context, void *pointer) int fidx = AllocSetFreeIndex(chunk->size); chunk->aset = (void *) set->freelist[fidx]; + context->usedspace -= chunk->size; #ifdef CLOBBER_FREED_MEMORY wipe_mem(pointer, chunk->size); @@ -1159,6 +1167,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size) /* Do the realloc */ chksize = MAXALIGN(size); blksize = chksize + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ; + context->usedspace -= oldsize; block = (AllocBlock) realloc(block, blksize); if (block == NULL) { @@ -1178,6 +1187,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size) if (block->next) block->next->prev = block; chunk->size = chksize; + context->usedspace += chksize; #ifdef MEMORY_CONTEXT_CHECKING #ifdef RANDOMIZE_ALLOCATED_MEMORY diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c index 43c58c3..cb4dfff 100644 --- a/src/backend/utils/mmgr/mcxt.c +++ b/src/backend/utils/mmgr/mcxt.c @@ -172,6 +172,7 @@ MemoryContextResetOnly(MemoryContext context) */ context->methods->reset(context); + context->usedspace = 0; context->isReset = true; VALGRIND_DESTROY_MEMPOOL(context); VALGRIND_CREATE_MEMPOOL(context, 0, false); @@ -740,6 +741,7 @@ MemoryContextCreate(MemoryContext node, node->name = name; node->ident = NULL; node->reset_cbs = NULL; + node->consumption = 0; /* OK to link node into context tree */ if (parent) diff --git a/src/include/nodes/memnodes.h b/src/include/nodes/memnodes.h index dbae98d..34babde 100644 --- a/src/include/nodes/memnodes.h +++ b/src/include/nodes/memnodes.h @@ -87,6 +87,7 @@ typedef struct MemoryContextData const char *name; /* context name (just for debugging) */ const char *ident; /* context ID if any (just for debugging) */ MemoryContextCallback *reset_cbs; /* list of reset/delete callbacks */ + Size usedspace; /* accumulates consumed memory size */ } MemoryContextData; /* utils/palloc.h contains typedef struct MemoryContextData *MemoryContext */ @@ -105,3 +106,6 @@ typedef struct MemoryContextData IsA((context), GenerationContext))) #endif /* MEMNODES_H */ + +/* Interface routines for memory usedspace-based accounting */ +#define MemoryContextGetUsedspace(c) ((c)->usedspace)