PostgreSQL/解析

Memory Context

Memory Contextの構造と操作

Memory Contextを使用することでメモリ管理が容易になり、メモリリークなどのバグを減らせる。
基本的なメモリコンテキストの操作イメージは以下の図の通りである。

memory-context-overview.png

Memory Contextに関連する構造体

MemoryContextData

typedef struct MemoryContextData
{
	NodeTag		type;			/* identifies exact kind of context */
	/* 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 */
	const MemoryContextMethods *methods;	/* virtual function table */
	MemoryContext parent;		/* NULL if no parent (toplevel context) */
	MemoryContext firstchild;	/* head of linked list of children */
	MemoryContext prevchild;	/* previous child of same parent */
	MemoryContext nextchild;	/* next child of same parent */
	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 */
} MemoryContextData;

参考struct MemoryContextData - https://git.postgresql.org/gitweb/?p=postgresql.git;a=shortlog;h=refs/tags/REL_12_1

MemoryContextMethods

メモリコンテキストの具象実装である。

typedef struct MemoryContextMethods
{
	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		(*reset) (MemoryContext context);
	void		(*delete_context) (MemoryContext context);
	Size		(*get_chunk_space) (MemoryContext context, void *pointer);
	bool		(*is_empty) (MemoryContext context);
	void		(*stats) (MemoryContext context,
						  MemoryStatsPrintFunc printfunc, void *passthru,
						  MemoryContextCounters *totals);
#ifdef MEMORY_CONTEXT_CHECKING
	void		(*check) (MemoryContext context);
#endif
} MemoryContextMethods;

参考struct MemoryContextMethods - https://git.postgresql.org/gitweb/?p=postgresql.git;a=shortlog;h=refs/tags/REL_12_1

例えば、デフォルトで使われるAllocSetは以下の関数で構成される。

static const MemoryContextMethods AllocSetMethods = {
	AllocSetAlloc,
	AllocSetFree,
	AllocSetRealloc,
	AllocSetReset,
	AllocSetDelete,
	AllocSetGetChunkSpace,
	AllocSetIsEmpty,
	AllocSetStats
#ifdef MEMORY_CONTEXT_CHECKING
	,AllocSetCheck
#endif
};

参考MemoryContextMethods AllocSetMethods - https://git.postgresql.org/gitweb/?p=postgresql.git;a=shortlog;h=refs/tags/REL_12_1

AllocSetContext

最も標準的なMemoryContextの実装はAllocSetである。
以下はAllocSetの構造外観を図示したものである。
(詳細は、aset.cを参照して欲しい)

allocset-overview.png

参考 src/backend/utils/mmgr/aset.c - https://git.postgresql.org/gitweb/?p=postgresql.git;a=shortlog;h=refs/tags/REL_12_1

MemoryContextの情報

MemoryContextStats関数を呼ぶことで、メモリコンテキストの内部情報を知ることができる。

AllocSetStats

AllocSetの場合は、MemoryContextStats関数の呼び出しでAllocSetStats関数が呼ばれる。
出力される情報は以下の通りである。

typedef struct AllocSetContext
{
	MemoryContextData header;	/* Standard memory-context fields */
	/* Info about storage allocated in this context: */
	AllocBlock	blocks;			/* head of list of blocks in this set */
	AllocChunk	freelist[ALLOCSET_NUM_FREELISTS];	/* free chunk lists */
	/* Allocation parameters for this context: */
	Size		initBlockSize;	/* initial block size */
	Size		maxBlockSize;	/* maximum block size */
	Size		nextBlockSize;	/* next block size to allocate */
	Size		allocChunkLimit;	/* effective chunk size limit */
	AllocBlock	keeper;			/* keep this block over resets */
	/* freelist this context could be put in, or -1 if not a candidate: */
	int			freeListIndex;	/* index in context_freelists[], or -1 */
} AllocSetContext;
項目変数説明
totaltotalspaceブロックサイズの合計(AllocSetContextのサイズも含む)(bytes)
blocksnblocksブロック数(個)
freefreespaceブロック内の空き領域の合計(フリーリスト、チャンクヘッダサイズ含む)(bytes)
chunksfreechunksフリーリスト内のチャンク数(個)
usedtotalspace - freespace使用領域(bytes)

使用領域は、上記の計算からわかるように、ブロックやチャンクのヘッダサイズも含んだ値である。
アライメントやfreelistのchunk割り当てなどがあるため、実際にpallocなどのアロケーション関数に指定したメモリサイズの合計となるとは限らない。
参考までにPG12.1でのサイズを以下に掲載する。

(lldb) print sizeof(AllocChunkData)
(unsigned long) $0 = 24
(lldb) print sizeof(AllocBlockData)
(unsigned long) $1 = 40
(lldb) print sizeof(AllocSetContext)
(unsigned long) $2 = 216

参考リンク


トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
目次
ダブルクリックで閉じるTOP | 閉じる
GO TO TOP