Skip to content

Instantly share code, notes, and snippets.

@kmikmy
Last active January 14, 2016 15:28
Show Gist options
  • Save kmikmy/87c0353508c39141317d to your computer and use it in GitHub Desktop.
Save kmikmy/87c0353508c39141317d to your computer and use it in GitHub Desktop.

共有メモリ

参考資料 http://ikubo.x0.com/PostgreSQL/pdf/IK08_ipc_041117.pdf

上記の資料から引用

PostgreSQLでは、バックエンドプロセス間でデータを共有 するために、共有メモリを使用している。 
共有メモリは、postmaster 起動時にまとめて1つの領域として確保される。
そして、この1つの共有メモリの領域をセグメントに切り分けて、様々な目的に使用する。
この共有メモリの管理を行うのが shmem.c  である。

共有メモリの確保・初期化

CreateSharedMemoryAndSemaphores(bool makePrivate, int port)で、各モジュールに必要な共有メモリのサイズを計算(add_size())して、領域を確保する。

XLOGモジュールに必要な共有メモリのサイズは

size = add_size(size, XLOGShmemSize());

として計算される。

XLOGShmemsize()

/*
 * Initialization of shared memory for XLOG
 */
Size
XLOGShmemSize(void)
{
    Size        size;

    /*
     * If the value of wal_buffers is -1, use the preferred auto-tune value.
     * This isn't an amazingly clean place to do this, but we must wait till
     * NBuffers has received its final value, and must do it before using the
     * value of XLOGbuffers to do anything important.
     */
    if (XLOGbuffers == -1)
    {
        char        buf[32];

        snprintf(buf, sizeof(buf), "%d", XLOGChooseNumBuffers());
        SetConfigOption("wal_buffers", buf, PGC_POSTMASTER, PGC_S_OVERRIDE);
    }
    Assert(XLOGbuffers > 0);

    /* XLogCtl */
    size = sizeof(XLogCtlData);

    /* WAL insertion locks, plus alignment */
    size = add_size(size, mul_size(sizeof(WALInsertLockPadded), NUM_XLOGINSERT_LOCKS + 1));
    /* xlblocks array */
    size = add_size(size, mul_size(sizeof(XLogRecPtr), XLOGbuffers));
    /* extra alignment padding for XLOG I/O buffers */
    size = add_size(size, XLOG_BLCKSZ);
    /* and the buffers themselves */
    size = add_size(size, mul_size(XLOG_BLCKSZ, XLOGbuffers));

    /*
     * Note: we don't count ControlFileData, it comes out of the "slop factor"
     * added by CreateSharedMemoryAndSemaphores.  This lets us use this
     * routine again below to compute the actual allocation size.
     */

    return size;
}

Drawing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment