mirror of
https://github.com/postgres/postgres.git
synced 2025-11-24 00:23:06 +03:00
Allow memory contexts to have both fixed and variable ident strings.
Originally, we treated memory context names as potentially variable in all cases, and therefore always copied them into the context header. Commit9fa6f00b1rethought this a little bit and invented a distinction between fixed and variable names, skipping the copy step for the former. But we can make things both simpler and more useful by instead allowing there to be two parts to a context's identification, a fixed "name" and an optional, variable "ident". The name supplied in the context create call is now required to be a compile-time-constant string in all cases, as it is never copied but just pointed to. The "ident" string, if wanted, is supplied later. This is needed because typically we want the ident to be stored inside the context so that it's cleaned up automatically on context deletion; that means it has to be copied into the context before we can set the pointer. The cost of this approach is basically just an additional pointer field in struct MemoryContextData, which isn't much overhead, and is bought back entirely in the AllocSet case by not needing a headerSize field anymore, since we no longer have to cope with variable header length. In addition, we can simplify the internal interfaces for memory context creation still further, saving a few cycles there. And it's no longer true that a custom identifier disqualifies a context from participating in aset.c's freelist scheme, so possibly there's some win on that end. All the places that were using non-compile-time-constant context names are adjusted to put the variable info into the "ident" instead. This allows more effective identification of those contexts in many cases; for example, subsidary contexts of relcache entries are now identified by both type (e.g. "index info") and relname, where before you got only one or the other. Contexts associated with PL function cache entries are now identified more fully and uniformly, too. I also arranged for plancache contexts to use the query source string as their identifier. This is basically free for CachedPlanSources, as they contained a copy of that string already. We pay an extra pstrdup to do it for CachedPlans. That could perhaps be avoided, but it would make things more fragile (since the CachedPlanSource is sometimes destroyed first). I suspect future improvements in error reporting will require CachedPlans to have a copy of that string anyway, so it's not clear that it's worth moving mountains to avoid it now. This also changes the APIs for context statistics routines so that the context-specific routines no longer assume that output goes straight to stderr, nor do they know all details of the output format. This is useful immediately to reduce code duplication, and it also allows for external code to do something with stats output that's different from printing to stderr. The reason for pushing this now rather than waiting for v12 is that it rethinks some of the API changes made by commit9fa6f00b1. Seems better for extension authors to endure just one round of API changes not two. Discussion: https://postgr.es/m/CAB=Je-FdtmFZ9y9REHD7VsSrnCkiBhsA4mdsLKSPauwXtQBeNA@mail.gmail.com
This commit is contained in:
@@ -131,7 +131,8 @@ static void SlabReset(MemoryContext context);
|
||||
static void SlabDelete(MemoryContext context);
|
||||
static Size SlabGetChunkSpace(MemoryContext context, void *pointer);
|
||||
static bool SlabIsEmpty(MemoryContext context);
|
||||
static void SlabStats(MemoryContext context, int level, bool print,
|
||||
static void SlabStats(MemoryContext context,
|
||||
MemoryStatsPrintFunc printfunc, void *passthru,
|
||||
MemoryContextCounters *totals);
|
||||
#ifdef MEMORY_CONTEXT_CHECKING
|
||||
static void SlabCheck(MemoryContext context);
|
||||
@@ -176,27 +177,22 @@ static const MemoryContextMethods SlabMethods = {
|
||||
* Create a new Slab context.
|
||||
*
|
||||
* parent: parent context, or NULL if top-level context
|
||||
* name: name of context (for debugging only, need not be unique)
|
||||
* flags: bitmask of MEMCONTEXT_XXX option flags
|
||||
* name: name of context (must be statically allocated)
|
||||
* blockSize: allocation block size
|
||||
* chunkSize: allocation chunk size
|
||||
*
|
||||
* Notes: if flags & MEMCONTEXT_COPY_NAME, the name string will be copied into
|
||||
* context-lifespan storage; otherwise, it had better be statically allocated.
|
||||
* The chunkSize may not exceed:
|
||||
* MAXALIGN_DOWN(SIZE_MAX) - MAXALIGN(sizeof(SlabBlock)) - SLAB_CHUNKHDRSZ
|
||||
*/
|
||||
MemoryContext
|
||||
SlabContextCreate(MemoryContext parent,
|
||||
const char *name,
|
||||
int flags,
|
||||
Size blockSize,
|
||||
Size chunkSize)
|
||||
{
|
||||
int chunksPerBlock;
|
||||
Size fullChunkSize;
|
||||
Size freelistSize;
|
||||
Size nameOffset;
|
||||
Size headerSize;
|
||||
SlabContext *slab;
|
||||
int i;
|
||||
@@ -231,12 +227,8 @@ SlabContextCreate(MemoryContext parent,
|
||||
* this with the first regular block; not worth the extra complication.
|
||||
*/
|
||||
|
||||
/* Size of the memory context header, including name storage if needed */
|
||||
nameOffset = offsetof(SlabContext, freelist) + freelistSize;
|
||||
if (flags & MEMCONTEXT_COPY_NAME)
|
||||
headerSize = nameOffset + strlen(name) + 1;
|
||||
else
|
||||
headerSize = nameOffset;
|
||||
/* Size of the memory context header */
|
||||
headerSize = offsetof(SlabContext, freelist) + freelistSize;
|
||||
|
||||
slab = (SlabContext *) malloc(headerSize);
|
||||
if (slab == NULL)
|
||||
@@ -270,12 +262,9 @@ SlabContextCreate(MemoryContext parent,
|
||||
/* Finally, do the type-independent part of context creation */
|
||||
MemoryContextCreate((MemoryContext) slab,
|
||||
T_SlabContext,
|
||||
headerSize,
|
||||
nameOffset,
|
||||
&SlabMethods,
|
||||
parent,
|
||||
name,
|
||||
flags);
|
||||
name);
|
||||
|
||||
return (MemoryContext) slab;
|
||||
}
|
||||
@@ -634,12 +623,13 @@ SlabIsEmpty(MemoryContext context)
|
||||
* SlabStats
|
||||
* Compute stats about memory consumption of a Slab context.
|
||||
*
|
||||
* level: recursion level (0 at top level); used for print indentation.
|
||||
* print: true to print stats to stderr.
|
||||
* totals: if not NULL, add stats about this Slab into *totals.
|
||||
* printfunc: if not NULL, pass a human-readable stats string to this.
|
||||
* passthru: pass this pointer through to printfunc.
|
||||
* totals: if not NULL, add stats about this context into *totals.
|
||||
*/
|
||||
static void
|
||||
SlabStats(MemoryContext context, int level, bool print,
|
||||
SlabStats(MemoryContext context,
|
||||
MemoryStatsPrintFunc printfunc, void *passthru,
|
||||
MemoryContextCounters *totals)
|
||||
{
|
||||
SlabContext *slab = castNode(SlabContext, context);
|
||||
@@ -667,14 +657,15 @@ SlabStats(MemoryContext context, int level, bool print,
|
||||
}
|
||||
}
|
||||
|
||||
if (print)
|
||||
if (printfunc)
|
||||
{
|
||||
for (i = 0; i < level; i++)
|
||||
fprintf(stderr, " ");
|
||||
fprintf(stderr,
|
||||
"Slab: %s: %zu total in %zd blocks; %zu free (%zd chunks); %zu used\n",
|
||||
slab->header.name, totalspace, nblocks, freespace, freechunks,
|
||||
totalspace - freespace);
|
||||
char stats_string[200];
|
||||
|
||||
snprintf(stats_string, sizeof(stats_string),
|
||||
"%zu total in %zd blocks; %zu free (%zd chunks); %zu used",
|
||||
totalspace, nblocks, freespace, freechunks,
|
||||
totalspace - freespace);
|
||||
printfunc(context, passthru, stats_string);
|
||||
}
|
||||
|
||||
if (totals)
|
||||
|
||||
Reference in New Issue
Block a user