~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/backend/storage/ipc/ipci.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * ipci.c
 
4
 *        POSTGRES inter-process communication initialization code.
 
5
 *
 
6
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
 
7
 * Portions Copyright (c) 1994, Regents of the University of California
 
8
 *
 
9
 *
 
10
 * IDENTIFICATION
 
11
 *        $PostgreSQL: pgsql/src/backend/storage/ipc/ipci.c,v 1.74 2004-12-31 22:00:56 pgsql Exp $
 
12
 *
 
13
 *-------------------------------------------------------------------------
 
14
 */
 
15
#include "postgres.h"
 
16
 
 
17
#include "access/clog.h"
 
18
#include "access/subtrans.h"
 
19
#include "access/xlog.h"
 
20
#include "miscadmin.h"
 
21
#include "postmaster/bgwriter.h"
 
22
#include "postmaster/postmaster.h"
 
23
#include "storage/bufmgr.h"
 
24
#include "storage/freespace.h"
 
25
#include "storage/ipc.h"
 
26
#include "storage/lmgr.h"
 
27
#include "storage/lwlock.h"
 
28
#include "storage/pg_sema.h"
 
29
#include "storage/pg_shmem.h"
 
30
#include "storage/pmsignal.h"
 
31
#include "storage/proc.h"
 
32
#include "storage/sinval.h"
 
33
#include "storage/spin.h"
 
34
 
 
35
 
 
36
/*
 
37
 * CreateSharedMemoryAndSemaphores
 
38
 *              Creates and initializes shared memory and semaphores.
 
39
 *
 
40
 * This is called by the postmaster or by a standalone backend.
 
41
 * It is also called by a backend forked from the postmaster in the
 
42
 * EXEC_BACKEND case.  In the latter case, the shared memory segment
 
43
 * already exists and has been physically attached to, but we have to
 
44
 * initialize pointers in local memory that reference the shared structures,
 
45
 * because we didn't inherit the correct pointer values from the postmaster
 
46
 * as we do in the fork() scenario.  The easiest way to do that is to run
 
47
 * through the same code as before.  (Note that the called routines mostly
 
48
 * check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case.
 
49
 * This is a bit code-wasteful and could be cleaned up.)
 
50
 *
 
51
 * If "makePrivate" is true then we only need private memory, not shared
 
52
 * memory.      This is true for a standalone backend, false for a postmaster.
 
53
 */
 
54
void
 
55
CreateSharedMemoryAndSemaphores(bool makePrivate,
 
56
                                                                int maxBackends,
 
57
                                                                int port)
 
58
{
 
59
        PGShmemHeader *seghdr = NULL;
 
60
 
 
61
        if (!IsUnderPostmaster)
 
62
        {
 
63
                int                     size;
 
64
                int                     numSemas;
 
65
 
 
66
                /*
 
67
                 * Size of the Postgres shared-memory block is estimated via
 
68
                 * moderately-accurate estimates for the big hogs, plus 100K for
 
69
                 * the stuff that's too small to bother with estimating.
 
70
                 */
 
71
                size = hash_estimate_size(SHMEM_INDEX_SIZE, sizeof(ShmemIndexEnt));
 
72
                size += BufferShmemSize();
 
73
                size += LockShmemSize(maxBackends);
 
74
                size += ProcGlobalShmemSize(maxBackends);
 
75
                size += XLOGShmemSize();
 
76
                size += CLOGShmemSize();
 
77
                size += SUBTRANSShmemSize();
 
78
                size += LWLockShmemSize();
 
79
                size += SInvalShmemSize(maxBackends);
 
80
                size += FreeSpaceShmemSize();
 
81
                size += BgWriterShmemSize();
 
82
#ifdef EXEC_BACKEND
 
83
                size += ShmemBackendArraySize();
 
84
#endif
 
85
                size += 100000;
 
86
                /* might as well round it off to a multiple of a typical page size */
 
87
                size += 8192 - (size % 8192);
 
88
 
 
89
                elog(DEBUG3, "invoking IpcMemoryCreate(size=%d)", size);
 
90
 
 
91
                /*
 
92
                 * Create the shmem segment
 
93
                 */
 
94
                seghdr = PGSharedMemoryCreate(size, makePrivate, port);
 
95
 
 
96
                /*
 
97
                 * Create semaphores
 
98
                 */
 
99
                numSemas = ProcGlobalSemas(maxBackends);
 
100
                numSemas += SpinlockSemas();
 
101
                PGReserveSemaphores(numSemas, port);
 
102
        }
 
103
        else
 
104
        {
 
105
                /*
 
106
                 * We are reattaching to an existing shared memory segment.
 
107
                 * This should only be reached in the EXEC_BACKEND case, and
 
108
                 * even then only with makePrivate == false.
 
109
                 */
 
110
#ifdef EXEC_BACKEND
 
111
                Assert(!makePrivate);
 
112
                Assert(UsedShmemSegAddr != NULL);
 
113
                seghdr = UsedShmemSegAddr;
 
114
#else
 
115
                elog(PANIC, "should be attached to shared memory already");
 
116
#endif
 
117
        }
 
118
 
 
119
 
 
120
        /*
 
121
         * Set up shared memory allocation mechanism
 
122
         */
 
123
        InitShmemAllocation(seghdr, !IsUnderPostmaster);
 
124
 
 
125
        /*
 
126
         * Now initialize LWLocks, which do shared memory allocation and are
 
127
         * needed for InitShmemIndex.
 
128
         */
 
129
        if (!IsUnderPostmaster)
 
130
                CreateLWLocks();
 
131
 
 
132
        /*
 
133
         * Set up shmem.c index hashtable
 
134
         */
 
135
        InitShmemIndex();
 
136
 
 
137
        /*
 
138
         * Set up xlog, clog, and buffers
 
139
         */
 
140
        XLOGShmemInit();
 
141
        CLOGShmemInit();
 
142
        SUBTRANSShmemInit();
 
143
        InitBufferPool();
 
144
 
 
145
        /*
 
146
         * Set up lock manager
 
147
         */
 
148
        InitLocks();
 
149
        InitLockTable(maxBackends);
 
150
 
 
151
        /*
 
152
         * Set up process table
 
153
         */
 
154
        InitProcGlobal(maxBackends);
 
155
 
 
156
        /*
 
157
         * Set up shared-inval messaging
 
158
         */
 
159
        CreateSharedInvalidationState(maxBackends);
 
160
 
 
161
        /*
 
162
         * Set up free-space map
 
163
         */
 
164
        InitFreeSpaceMap();
 
165
 
 
166
        /*
 
167
         * Set up interprocess signaling mechanisms
 
168
         */
 
169
        PMSignalInit();
 
170
        BgWriterShmemInit();
 
171
 
 
172
#ifdef EXEC_BACKEND
 
173
 
 
174
        /*
 
175
         * Alloc the win32 shared backend array
 
176
         */
 
177
        if (!IsUnderPostmaster)
 
178
                ShmemBackendArrayAllocation();
 
179
#endif
 
180
}