1996-08-28 03:59:28 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* memnodes.h
|
1997-09-07 07:04:48 +02:00
|
|
|
* POSTGRES memory context node definitions.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*
|
2017-01-03 19:48:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/include/nodes/memnodes.h
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
#ifndef MEMNODES_H
|
1996-08-28 03:59:28 +02:00
|
|
|
#define MEMNODES_H
|
|
|
|
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "nodes/nodes.h"
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2015-08-25 19:09:48 +02:00
|
|
|
/*
|
|
|
|
* MemoryContextCounters
|
|
|
|
* Summarization state for MemoryContextStats collection.
|
|
|
|
*
|
|
|
|
* The set of counters in this struct is biased towards AllocSet; if we ever
|
|
|
|
* add any context types that are based on fundamentally different approaches,
|
|
|
|
* we might need more or different counters here. A possible API spec then
|
|
|
|
* would be to print only nonzero counters, but for now we just summarize in
|
|
|
|
* the format historically used by AllocSet.
|
|
|
|
*/
|
|
|
|
typedef struct MemoryContextCounters
|
|
|
|
{
|
|
|
|
Size nblocks; /* Total number of malloc blocks */
|
|
|
|
Size freechunks; /* Total number of free chunks */
|
|
|
|
Size totalspace; /* Total bytes requested from malloc */
|
|
|
|
Size freespace; /* The unused portion of totalspace */
|
|
|
|
} MemoryContextCounters;
|
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* MemoryContext
|
1997-09-07 07:04:48 +02:00
|
|
|
* A logical context in which memory allocations occur.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
2000-06-28 05:33:33 +02:00
|
|
|
* MemoryContext itself is an abstract type that can have multiple
|
|
|
|
* implementations, though for now we have only AllocSetContext.
|
|
|
|
* The function pointers in MemoryContextMethods define one specific
|
|
|
|
* implementation of MemoryContext --- they are a virtual function table
|
|
|
|
* in C++ terms.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
2000-06-28 05:33:33 +02:00
|
|
|
* Node types that are actual implementations of memory contexts must
|
|
|
|
* begin with the same fields as MemoryContext.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
2000-06-28 05:33:33 +02:00
|
|
|
* Note: for largely historical reasons, typedef MemoryContext is a pointer
|
|
|
|
* to the context struct rather than the struct type itself.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
|
|
|
|
2000-06-28 05:33:33 +02:00
|
|
|
typedef struct MemoryContextMethods
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-06-28 05:33:33 +02:00
|
|
|
void *(*alloc) (MemoryContext context, Size size);
|
|
|
|
/* call this free_p in case someone #define's free() */
|
|
|
|
void (*free_p) (MemoryContext context, void *pointer);
|
|
|
|
void *(*realloc) (MemoryContext context, void *pointer, Size size);
|
|
|
|
void (*init) (MemoryContext context);
|
|
|
|
void (*reset) (MemoryContext context);
|
2009-07-16 08:33:46 +02:00
|
|
|
void (*delete_context) (MemoryContext context);
|
2002-08-12 02:36:12 +02:00
|
|
|
Size (*get_chunk_space) (MemoryContext context, void *pointer);
|
2004-09-16 22:17:49 +02:00
|
|
|
bool (*is_empty) (MemoryContext context);
|
2015-08-25 19:09:48 +02:00
|
|
|
void (*stats) (MemoryContext context, int level, bool print,
|
2017-06-21 20:39:04 +02:00
|
|
|
MemoryContextCounters *totals);
|
2001-03-22 05:01:46 +01:00
|
|
|
#ifdef MEMORY_CONTEXT_CHECKING
|
Here is the patch with memory leak checker. This checker allow detect
in-chunk leaks, overwrite-next-chunk leaks and overwrite block-freeptr leaks.
A in-chunk leak --- if something overwrite space after wanted (via palloc()
size, but it is still inside chunk. For example
x = palloc(12); /* create 16b chunk */
memset(x, '#', 13);
this leak is in the current source total invisible, because chunk is 16b and
leak is in the "align space".
For this feature I add data_size to StandardChunk, and all memory which go
from AllocSetAlloc() is marked as 0x7F.
The MemoryContextCheck() is compiled '#ifdef USE_ASSERT_CHECKING'.
I add this checking to 'tcop/postgres.c' and is active after each backend
query, but it is probably not sufficient, because some MemoryContext exist
only during memory processing --- will good if someone who known where
it is needful (Tom:-) add it for others contexts;
A problem in the current source is that we have still some malloc()
allocation that is not needful and this allocation is total invisible for
all context routines. For example Dllist in backend (pretty dirty it is in
catcache where values in Dllist are palloc-ed, but list is malloc-ed).
--- and BTW. this Dllist design stand in the way for query cache :-)
Tom, if you agree I start replace some mallocs.
BTW. --- Tom, have you idea for across transaction presistent allocation for
SQL functions? (like regex - now it is via malloc)
I almost forget. I add one if() to AllocSetAlloc(), for 'size' that are
greater than ALLOC_BIGCHUNK_LIMIT is not needful check AllocSetFreeIndex(),
because 'fidx' is always 'ALLOCSET_NUM_FREELISTS - 1'. It a little brisk up
allocation for very large chunks. Right?
Karel
2000-07-11 16:30:37 +02:00
|
|
|
void (*check) (MemoryContext context);
|
|
|
|
#endif
|
2000-06-28 05:33:33 +02:00
|
|
|
} MemoryContextMethods;
|
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1999-02-06 17:50:34 +01:00
|
|
|
typedef struct MemoryContextData
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
NodeTag type; /* identifies exact kind of context */
|
2015-02-27 23:16:40 +01:00
|
|
|
/* these two fields are placed here to minimize alignment wastage: */
|
|
|
|
bool isReset; /* T = no space alloced since last reset */
|
|
|
|
bool allowInCritSection; /* allow palloc in critical section */
|
2001-03-22 05:01:46 +01:00
|
|
|
MemoryContextMethods *methods; /* virtual function table */
|
|
|
|
MemoryContext parent; /* NULL if no parent (toplevel context) */
|
|
|
|
MemoryContext firstchild; /* head of linked list of children */
|
2015-12-09 00:32:49 +01:00
|
|
|
MemoryContext prevchild; /* previous child of same parent */
|
2001-03-22 05:01:46 +01:00
|
|
|
MemoryContext nextchild; /* next child of same parent */
|
|
|
|
char *name; /* context name (just for debugging) */
|
2015-02-27 23:16:40 +01:00
|
|
|
MemoryContextCallback *reset_cbs; /* list of reset/delete callbacks */
|
1999-05-26 00:43:53 +02:00
|
|
|
} MemoryContextData;
|
1999-03-08 00:03:32 +01:00
|
|
|
|
2000-06-28 05:33:33 +02:00
|
|
|
/* utils/palloc.h contains typedef struct MemoryContextData *MemoryContext */
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1999-03-08 00:03:32 +01:00
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
/*
|
1999-05-25 18:15:34 +02:00
|
|
|
* MemoryContextIsValid
|
1997-09-07 07:04:48 +02:00
|
|
|
* True iff memory context is valid.
|
2000-06-28 05:33:33 +02:00
|
|
|
*
|
|
|
|
* Add new context types to the set accepted by this macro.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
|
|
|
#define MemoryContextIsValid(context) \
|
2000-06-28 05:33:33 +02:00
|
|
|
((context) != NULL && \
|
Add "Slab" MemoryContext implementation for efficient equal-sized allocations.
The default general purpose aset.c style memory context is not a great
choice for allocations that are all going to be evenly sized,
especially when those objects aren't small, and have varying
lifetimes. There tends to be a lot of fragmentation, larger
allocations always directly go to libc rather than have their cost
amortized over several pallocs.
These problems lead to the introduction of ad-hoc slab allocators in
reorderbuffer.c. But it turns out that the simplistic implementation
leads to problems when a lot of objects are allocated and freed, as
aset.c is still the underlying implementation. Especially freeing can
easily run into O(n^2) behavior in aset.c.
While the O(n^2) behavior in aset.c can, and probably will, be
addressed, custom allocators for this behavior are more efficient
both in space and time.
This allocator is for evenly sized allocations, and supports both
cheap allocations and freeing, without fragmenting significantly. It
does so by allocating evenly sized blocks via malloc(), and carves
them into chunks that can be used for allocations. In order to
release blocks to the OS as early as possible, chunks are allocated
from the fullest block that still has free objects, increasing the
likelihood of a block being entirely unused.
A subsequent commit uses this in reorderbuffer.c, but a further
allocator is needed to resolve the performance problems triggering
this work.
There likely are further potentialy uses of this allocator besides
reorderbuffer.c.
There's potential further optimizations of the new slab.c, in
particular the array of freelists could be replaced by a more
intelligent structure - but for now this looks more than good enough.
Author: Tomas Vondra, editorialized by Andres Freund
Reviewed-By: Andres Freund, Petr Jelinek, Robert Haas, Jim Nasby
Discussion: https://postgr.es/m/d15dff83-0b37-28ed-0809-95a5cc7292ad@2ndquadrant.com
2017-02-27 12:41:44 +01:00
|
|
|
(IsA((context), AllocSetContext) || IsA((context), SlabContext)))
|
2000-06-28 05:33:33 +02:00
|
|
|
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* MEMNODES_H */
|