]> granicus.if.org Git - postgresql/blob - src/backend/storage/ipc/ipci.c
Add code to allow profiling of backends on Linux: save and restore the
[postgresql] / src / backend / storage / ipc / ipci.c
1 /*-------------------------------------------------------------------------
2  *
3  * ipci.c
4  *        POSTGRES inter-process communication initialization code.
5  *
6  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/storage/ipc/ipci.c,v 1.45 2001/11/04 19:55:31 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include <sys/types.h>
18
19 #include "miscadmin.h"
20 #include "access/clog.h"
21 #include "access/xlog.h"
22 #include "storage/bufmgr.h"
23 #include "storage/freespace.h"
24 #include "storage/lmgr.h"
25 #include "storage/lwlock.h"
26 #include "storage/pmsignal.h"
27 #include "storage/proc.h"
28 #include "storage/sinval.h"
29 #include "storage/spin.h"
30
31
32 /*
33  * CreateSharedMemoryAndSemaphores
34  *              Creates and initializes shared memory and semaphores.
35  *
36  * This is called by the postmaster or by a standalone backend.
37  * It is NEVER called by a backend forked from the postmaster;
38  * for such a backend, the shared memory is already ready-to-go.
39  *
40  * If "makePrivate" is true then we only need private memory, not shared
41  * memory.      This is true for a standalone backend, false for a postmaster.
42  */
43 void
44 CreateSharedMemoryAndSemaphores(bool makePrivate, int maxBackends)
45 {
46         int                     size;
47         PGShmemHeader *seghdr;
48
49         /*
50          * Size of the Postgres shared-memory block is estimated via
51          * moderately-accurate estimates for the big hogs, plus 100K for the
52          * stuff that's too small to bother with estimating.
53          */
54         size = BufferShmemSize();
55         size += LockShmemSize(maxBackends);
56         size += XLOGShmemSize();
57         size += CLOGShmemSize();
58         size += LWLockShmemSize();
59         size += SInvalShmemSize(maxBackends);
60         size += FreeSpaceShmemSize();
61 #ifdef STABLE_MEMORY_STORAGE
62         size += MMShmemSize();
63 #endif
64         size += 100000;
65         /* might as well round it off to a multiple of a typical page size */
66         size += 8192 - (size % 8192);
67
68         if (DebugLvl > 1)
69                 fprintf(stderr, "invoking IpcMemoryCreate(size=%d)\n", size);
70
71         /*
72          * Create the shmem segment
73          */
74         seghdr = IpcMemoryCreate(size, makePrivate, IPCProtection);
75
76         /*
77          * First initialize spinlocks --- needed by InitShmemAllocation()
78          */
79         CreateSpinlocks();
80
81         /*
82          * Set up shared memory allocation mechanism
83          */
84         InitShmemAllocation(seghdr);
85
86         /*
87          * Now initialize LWLocks, which do shared memory allocation and are
88          * needed for InitShmemIndex.
89          */
90         CreateLWLocks();
91
92         /*
93          * Set up shmem.c index hashtable
94          */
95         InitShmemIndex();
96
97         /*
98          * Set up xlog, clog, and buffers
99          */
100         XLOGShmemInit();
101         CLOGShmemInit();
102         InitBufferPool();
103
104         /*
105          * Set up lock manager
106          */
107         InitLocks();
108         if (InitLockTable(maxBackends) == INVALID_TABLEID)
109                 elog(FATAL, "Couldn't create the lock table");
110
111         /*
112          * Set up process table
113          */
114         InitProcGlobal(maxBackends);
115
116         /*
117          * Set up shared-inval messaging
118          */
119         CreateSharedInvalidationState(maxBackends);
120
121         /*
122          * Set up free-space map
123          */
124         InitFreeSpaceMap();
125
126         /*
127          * Set up child-to-postmaster signaling mechanism
128          */
129         PMSignalInit();
130 }