mirror of
https://github.com/postgres/postgres.git
synced 2025-11-24 00:23:06 +03:00
Add transparent block-level memory accounting
Adds accounting of memory allocated in a memory context. Compared to various ad hoc solutions, the main advantage is that the accounting is transparent and does not require direct control over allocations (this matters for use cases where the allocations happen in user code, like for example aggregate states allocated in a transition functions). To reduce overhead, the accounting happens at the block level (not for individual chunks) and only the context immediately owning the block is updated. When inquiring about amount of memory allocated in a context, we have to recursively walk all children contexts. This "lazy" accounting works well for cases with relatively small number of contexts in the relevant subtree and/or with infrequent inquiries. Author: Jeff Davis Reivewed-by: Tomas Vondra, Melanie Plageman, Soumyadeep Chakraborty Discussion: https://www.postgresql.org/message-id/flat/027a129b8525601c6a680d27ce3a7172dab61aab.camel@j-davis.com
This commit is contained in:
@@ -462,6 +462,30 @@ MemoryContextIsEmpty(MemoryContext context)
|
||||
return context->methods->is_empty(context);
|
||||
}
|
||||
|
||||
/*
|
||||
* Find the memory allocated to blocks for this memory context. If recurse is
|
||||
* true, also include children.
|
||||
*/
|
||||
int64
|
||||
MemoryContextMemAllocated(MemoryContext context, bool recurse)
|
||||
{
|
||||
int64 total = context->mem_allocated;
|
||||
|
||||
AssertArg(MemoryContextIsValid(context));
|
||||
|
||||
if (recurse)
|
||||
{
|
||||
MemoryContext child = context->firstchild;
|
||||
|
||||
for (child = context->firstchild;
|
||||
child != NULL;
|
||||
child = child->nextchild)
|
||||
total += MemoryContextMemAllocated(child, true);
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
/*
|
||||
* MemoryContextStats
|
||||
* Print statistics about the named context and all its descendants.
|
||||
@@ -736,6 +760,7 @@ MemoryContextCreate(MemoryContext node,
|
||||
node->methods = methods;
|
||||
node->parent = parent;
|
||||
node->firstchild = NULL;
|
||||
node->mem_allocated = 0;
|
||||
node->prevchild = NULL;
|
||||
node->name = name;
|
||||
node->ident = NULL;
|
||||
|
||||
Reference in New Issue
Block a user