ipci.c 2.6 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * ipci.c
4
 *	  POSTGRES inter-process communication initialization code.
5
 *
6
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
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.41 2001/06/27 23:31:39 tgl Exp $
12 13 14
 *
 *-------------------------------------------------------------------------
 */
M
Marc G. Fournier 已提交
15
#include "postgres.h"
16

17 18
#include <sys/types.h>

B
Bruce Momjian 已提交
19
#include "miscadmin.h"
20
#include "access/xlog.h"
B
Bruce Momjian 已提交
21
#include "storage/bufmgr.h"
22
#include "storage/freespace.h"
23
#include "storage/lmgr.h"
24
#include "storage/proc.h"
B
Bruce Momjian 已提交
25
#include "storage/sinval.h"
26
#include "storage/spin.h"
27 28 29


/*
B
Bruce Momjian 已提交
30
 * CreateSharedMemoryAndSemaphores
31
 *		Creates and initializes shared memory and semaphores.
32 33 34 35 36
 *
 * This is called by the postmaster or by a standalone backend.
 * It is NEVER called by a backend forked from the postmaster;
 * for such a backend, the shared memory is already ready-to-go.
 *
37
 * If "makePrivate" is true then we only need private memory, not shared
B
Bruce Momjian 已提交
38
 * memory.	This is true for a standalone backend, false for a postmaster.
39 40
 */
void
41
CreateSharedMemoryAndSemaphores(bool makePrivate, int maxBackends)
42
{
43
	int			size;
44
	PGShmemHeader *seghdr;
45

46
	/*
47
	 * Size of the Postgres shared-memory block is estimated via
B
Bruce Momjian 已提交
48 49
	 * moderately-accurate estimates for the big hogs, plus 100K for the
	 * stuff that's too small to bother with estimating.
50
	 */
51 52 53 54 55 56
	size = BufferShmemSize();
	size += LockShmemSize(maxBackends);
	size += XLOGShmemSize();
	size += SLockShmemSize();
	size += SInvalShmemSize(maxBackends);
	size += FreeSpaceShmemSize();
57
#ifdef STABLE_MEMORY_STORAGE
58
	size += MMShmemSize();
59
#endif
60
	size += 100000;
61 62
	/* might as well round it off to a multiple of a typical page size */
	size += 8192 - (size % 8192);
63 64

	if (DebugLvl > 1)
65
		fprintf(stderr, "invoking IpcMemoryCreate(size=%d)\n", size);
66

67 68
	/*
	 * Create the shmem segment
69
	 */
70
	seghdr = IpcMemoryCreate(size, makePrivate, IPCProtection);
71

72 73
	/*
	 * First initialize spinlocks --- needed by InitShmemAllocation()
74
	 */
75
	CreateSpinlocks(seghdr);
76

77 78
	/*
	 * Set up shmem.c hashtable
79
	 */
80
	InitShmemAllocation(seghdr);
81

82 83
	/*
	 * Set up xlog and buffers
84
	 */
85 86
	XLOGShmemInit();
	InitBufferPool();
87

88 89
	/*
	 * Set up lock manager
90 91
	 */
	InitLocks();
92
	if (InitLockTable(maxBackends) == INVALID_TABLEID)
93 94 95 96 97 98
		elog(FATAL, "Couldn't create the lock table");

	/*
	 * Set up process table
	 */
	InitProcGlobal(maxBackends);
99

100 101 102 103
	/*
	 * Set up shared-inval messaging
	 */
	CreateSharedInvalidationState(maxBackends);
104 105 106 107 108

	/*
	 * Set up free-space map
	 */
	InitFreeSpaceMap();
109
}