From ade1f6bf389d834cd4428f302a5cc4deaf66be9e Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Wed, 13 Feb 2019 13:36:38 +0900
Subject: [PATCH 2/5] Memory consumption report reature of memorycontext

This adds a feature that count memory consumption (in other words,
internally allocated size for a chunk) and read it from others.  This
allows other features to know "(almost) real" consumption of memory.
---
 src/backend/utils/mmgr/aset.c | 13 +++++++++++++
 src/backend/utils/mmgr/mcxt.c |  1 +
 src/include/nodes/memnodes.h  |  4 ++++
 3 files changed, 18 insertions(+)

diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c
index 08aff333a4..3c5798734c 100644
--- a/src/backend/utils/mmgr/aset.c
+++ b/src/backend/utils/mmgr/aset.c
@@ -614,6 +614,9 @@ AllocSetReset(MemoryContext context)
 
 	/* Reset block size allocation sequence, too */
 	set->nextBlockSize = set->initBlockSize;
+
+	/* Reset consumption account */
+	set->header.consumption = 0;
 }
 
 /*
@@ -778,6 +781,8 @@ AllocSetAlloc(MemoryContext context, Size size)
 		/* Disallow external access to private part of chunk header. */
 		VALGRIND_MAKE_MEM_NOACCESS(chunk, ALLOCCHUNK_PRIVATE_LEN);
 
+		context->consumption += chunk_size;
+
 		return AllocChunkGetPointer(chunk);
 	}
 
@@ -817,6 +822,8 @@ AllocSetAlloc(MemoryContext context, Size size)
 		/* Disallow external access to private part of chunk header. */
 		VALGRIND_MAKE_MEM_NOACCESS(chunk, ALLOCCHUNK_PRIVATE_LEN);
 
+		context->consumption += chunk->size;
+
 		return AllocChunkGetPointer(chunk);
 	}
 
@@ -976,6 +983,8 @@ AllocSetAlloc(MemoryContext context, Size size)
 	/* Disallow external access to private part of chunk header. */
 	VALGRIND_MAKE_MEM_NOACCESS(chunk, ALLOCCHUNK_PRIVATE_LEN);
 
+	context->consumption += chunk_size;
+
 	return AllocChunkGetPointer(chunk);
 }
 
@@ -1022,6 +1031,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->consumption -= chunk->size;
 		if (block->prev)
 			block->prev->next = block->next;
 		else
@@ -1039,6 +1049,7 @@ AllocSetFree(MemoryContext context, void *pointer)
 		int			fidx = AllocSetFreeIndex(chunk->size);
 
 		chunk->aset = (void *) set->freelist[fidx];
+		context->consumption -= chunk->size;
 
 #ifdef CLOBBER_FREED_MEMORY
 		wipe_mem(pointer, chunk->size);
@@ -1159,6 +1170,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
 		/* Do the realloc */
 		chksize = MAXALIGN(size);
 		blksize = chksize + ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ;
+		context->consumption -= oldsize;
 		block = (AllocBlock) realloc(block, blksize);
 		if (block == NULL)
 		{
@@ -1178,6 +1190,7 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
 		if (block->next)
 			block->next->prev = block;
 		chunk->size = chksize;
+		context->consumption += 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 43c58c351b..395fca9e5d 100644
--- a/src/backend/utils/mmgr/mcxt.c
+++ b/src/backend/utils/mmgr/mcxt.c
@@ -740,6 +740,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 dbae98d3d9..cb0f23bac7 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 */
+	uint64		consumption;	/* 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 consumption-based accounting */
+#define MemoryContextGetConsumption(c)  ((c)->consumption)
-- 
2.16.3

