shmem.c 15.2 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * shmem.c
4
 *	  create shared memory and initialize shared memory data structures.
5
 *
B
Add:  
Bruce Momjian 已提交
6 7
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
11
 *	  $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.49 2000/02/26 05:25:55 tgl Exp $
12 13 14 15 16
 *
 *-------------------------------------------------------------------------
 */
/*
 * POSTGRES processes share one or more regions of shared memory.
17 18
 * The shared memory is created by a postmaster and is inherited
 * by each backends via fork().  The routines in this file are used for
19 20 21
 * allocating and binding to shared memory data structures.
 *
 * NOTES:
22 23 24 25 26 27 28 29 30 31
 *		(a) There are three kinds of shared memory data structures
 *	available to POSTGRES: fixed-size structures, queues and hash
 *	tables.  Fixed-size structures contain things like global variables
 *	for a module and should never be allocated after the process
 *	initialization phase.  Hash tables have a fixed maximum size, but
 *	their actual size can vary dynamically.  When entries are added
 *	to the table, more space is allocated.	Queues link data structures
 *	that have been allocated either as fixed size structures or as hash
 *	buckets.  Each shared data structure has a string name to identify
 *	it (assigned in the module that declares it).
32
 *
33
 *		(b) During initialization, each module looks for its
34
 *	shared data structures in a hash table called the "Shmem Index".
35 36 37 38
 *	If the data structure is not present, the caller can allocate
 *	a new one and initialize it.  If the data structure is present,
 *	the caller "attaches" to the structure by initializing a pointer
 *	in the local address space.
39
 *		The shmem index has two purposes: first, it gives us
40
 *	a simple model of how the world looks when a backend process
41
 *	initializes.  If something is present in the shmem index,
42
 *	it is initialized.	If it is not, it is uninitialized.	Second,
43
 *	the shmem index allows us to allocate shared memory on demand
44 45 46 47
 *	instead of trying to preallocate structures and hard-wire the
 *	sizes and locations in header files.  If you are using a lot
 *	of shared memory in a lot of different places (and changing
 *	things during development), this is important.
48
 *
49 50 51 52 53 54 55
 *		(c) memory allocation model: shared memory can never be
 *	freed, once allocated.	 Each hash table has its own free list,
 *	so hash buckets can be reused when an item is deleted.	However,
 *	if one hash table grows very large and then shrinks, its space
 *	cannot be redistributed to other tables.  We could build a simple
 *	hash bucket garbage collector if need be.  Right now, it seems
 *	unnecessary.
56
 *
57
 *		See InitSem() in sem.c for an example of how to use the
58
 *	shmem index.
59 60
 *
 */
61

62
#include "postgres.h"
63
#include "storage/proc.h"
V
Vadim B. Mikheev 已提交
64
#include "utils/tqual.h"
65 66 67

/* shared memory global variables */

68
unsigned long ShmemBase = 0;	/* start and end address of shared memory */
69 70
static unsigned long ShmemEnd = 0;
static unsigned long ShmemSize = 0;		/* current size (and default) */
71

72
extern VariableCache ShmemVariableCache;		/* varsup.c */
73

74
SPINLOCK	ShmemLock;			/* lock for shared memory allocation */
75

76
SPINLOCK	ShmemIndexLock;		/* lock for shmem index access */
77

78 79 80
static unsigned long *ShmemFreeStart = NULL;	/* pointer to the OFFSET
												 * of first free shared
												 * memory */
81 82 83
static unsigned long *ShmemIndexOffset = NULL;	/* start of the shmem
												 * index table (for
												 * bootstrap) */
84
static int	ShmemBootstrap = FALSE;		/* flag becomes true when shared
85
										 * mem is created by POSTMASTER */
86

87
static HTAB *ShmemIndex = NULL;
88 89

/* ---------------------
90
 * ShmemIndexReset() - Resets the shmem index to NULL....
91 92 93 94 95
 * useful when the postmaster destroys existing shared memory
 * and creates all new segments after a backend crash.
 * ----------------------
 */
void
96
ShmemIndexReset(void)
97
{
98
	ShmemIndex = (HTAB *) NULL;
99 100 101
}

/*
B
Bruce Momjian 已提交
102
 *	CreateSharedRegion()
103
 *
104 105 106 107
 *	This routine is called once by the postmaster to
 *	initialize the shared buffer pool.	Assume there is
 *	only one postmaster so no synchronization is necessary
 *	until after this routine completes successfully.
108 109 110 111 112 113 114 115 116
 *
 * key is a unique identifier for the shmem region.
 * size is the size of the region.
 */
static IpcMemoryId ShmemId;

void
ShmemCreate(unsigned int key, unsigned int size)
{
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	if (size)
		ShmemSize = size;
	/* create shared mem region */
	if ((ShmemId = IpcMemoryCreate(key, ShmemSize, IPCProtection))
		== IpcMemCreationFailed)
	{
		elog(FATAL, "ShmemCreate: cannot create region");
		exit(1);
	}

	/*
	 * ShmemBootstrap is true if shared memory has been created, but not
	 * yet initialized.  Only the postmaster/creator-of-all-things should
	 * have this flag set.
	 */
	ShmemBootstrap = TRUE;
133 134 135
}

/*
136 137
 *	InitShmem() -- map region into process address space
 *		and initialize shared data structures.
138 139 140 141 142
 *
 */
int
InitShmem(unsigned int key, unsigned int size)
{
143 144
	Pointer		sharedRegion;
	unsigned long currFreeSpace;
145

146 147
	HASHCTL		info;
	int			hash_flags;
148
	ShmemIndexEnt *result,
149 150 151
				item;
	bool		found;
	IpcMemoryId shmid;
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	/* if zero key, use default memory size */
	if (size)
		ShmemSize = size;

	/* default key is 0 */

	/* attach to shared memory region (SysV or BSD OS specific) */
	if (ShmemBootstrap && key == PrivateIPCKey)
		/* if we are running backend alone */
		shmid = ShmemId;
	else
		shmid = IpcMemoryIdGet(IPCKeyGetBufferMemoryKey(key), ShmemSize);
	sharedRegion = IpcMemoryAttach(shmid);
	if (sharedRegion == NULL)
	{
		elog(FATAL, "AttachSharedRegion: couldn't attach to shmem\n");
169
		return FALSE;
170 171 172 173 174 175 176 177 178
	}

	/* get pointers to the dimensions of shared memory */
	ShmemBase = (unsigned long) sharedRegion;
	ShmemEnd = (unsigned long) sharedRegion + ShmemSize;
	currFreeSpace = 0;

	/* First long in shared memory is the count of available space */
	ShmemFreeStart = (unsigned long *) ShmemBase;
179 180
	/* next is a shmem pointer to the shmem index */
	ShmemIndexOffset = ShmemFreeStart + 1;
181 182
	/* next is ShmemVariableCache */
	ShmemVariableCache = (VariableCache) (ShmemIndexOffset + 1);
183

184
	currFreeSpace += sizeof(ShmemFreeStart) + sizeof(ShmemIndexOffset) +
185
		LONGALIGN(sizeof(VariableCacheData));
186 187 188

	/*
	 * bootstrap initialize spin locks so we can start to use the
189
	 * allocator and shmem index.
190
	 */
191
	InitSpinLocks();
192 193 194 195 196 197

	/*
	 * We have just allocated additional space for two spinlocks. Now
	 * setup the global free space count
	 */
	if (ShmemBootstrap)
198
	{
199
		*ShmemFreeStart = currFreeSpace;
200
		memset(ShmemVariableCache, 0, sizeof(*ShmemVariableCache));
201
	}
202 203 204 205

	/* if ShmemFreeStart is NULL, then the allocator won't work */
	Assert(*ShmemFreeStart);

206 207 208
	/* create OR attach to the shared memory shmem index */
	info.keysize = SHMEM_INDEX_KEYSIZE;
	info.datasize = SHMEM_INDEX_DATASIZE;
209
	hash_flags = HASH_ELEM;
210

211 212
	/* This will acquire the shmem index lock, but not release it. */
	ShmemIndex = ShmemInitHash("ShmemIndex",
213 214
							   SHMEM_INDEX_SIZE, SHMEM_INDEX_SIZE,
							   &info, hash_flags);
215

216
	if (!ShmemIndex)
217
	{
218
		elog(FATAL, "InitShmem: couldn't initialize Shmem Index");
219
		return FALSE;
220 221 222
	}

	/*
223
	 * Now, check the shmem index for an entry to the shmem index.	If
224 225
	 * there is an entry there, someone else created the table. Otherwise,
	 * we did and we have to initialize it.
226
	 */
227 228
	MemSet(item.key, 0, SHMEM_INDEX_KEYSIZE);
	strncpy(item.key, "ShmemIndex", SHMEM_INDEX_KEYSIZE);
229

230 231
	result = (ShmemIndexEnt *)
		hash_search(ShmemIndex, (char *) &item, HASH_ENTER, &found);
232 233 234 235


	if (!result)
	{
236
		elog(FATAL, "InitShmem: corrupted shmem index");
237
		return FALSE;
238 239 240 241 242 243
	}

	if (!found)
	{

		/*
244
		 * bootstrapping shmem: we have to initialize the shmem index now.
245 246 247
		 */

		Assert(ShmemBootstrap);
248 249 250
		result->location = MAKE_OFFSET(ShmemIndex->hctl);
		*ShmemIndexOffset = result->location;
		result->size = SHMEM_INDEX_SIZE;
251 252 253 254 255 256 257

		ShmemBootstrap = FALSE;

	}
	else
		Assert(!ShmemBootstrap);
	/* now release the lock acquired in ShmemHashInit */
258
	SpinRelease(ShmemIndexLock);
259

260
	Assert(result->location == MAKE_OFFSET(ShmemIndex->hctl));
261

262
	return TRUE;
263 264 265 266
}

/*
 * ShmemAlloc -- allocate word-aligned byte string from
267
 *		shared memory
268 269 270
 *
 * Assumes ShmemLock and ShmemFreeStart are initialized.
 * Returns: real pointer to memory or NULL if we are out
271 272
 *		of space.  Has to return a real pointer in order
 *		to be compatable with malloc().
273
 */
274
long *
275 276
ShmemAlloc(unsigned long size)
{
277 278
	unsigned long tmpFree;
	long	   *newSpace;
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

	/*
	 * ensure space is word aligned.
	 *
	 * Word-alignment is not good enough. We have to be more conservative:
	 * doubles need 8-byte alignment. (We probably only need this on RISC
	 * platforms but this is not a big waste of space.) - ay 12/94
	 */
	if (size % sizeof(double))
		size += sizeof(double) - (size % sizeof(double));

	Assert(*ShmemFreeStart);

	SpinAcquire(ShmemLock);

	tmpFree = *ShmemFreeStart + size;
	if (tmpFree <= ShmemSize)
	{
		newSpace = (long *) MAKE_PTR(*ShmemFreeStart);
		*ShmemFreeStart += size;
	}
	else
		newSpace = NULL;

	SpinRelease(ShmemLock);

	if (!newSpace)
		elog(NOTICE, "ShmemAlloc: out of memory ");
307
	return newSpace;
308 309 310
}

/*
311 312
 * ShmemIsValid -- test if an offset refers to valid shared memory
 *
313 314 315 316 317
 * Returns TRUE if the pointer is valid.
 */
int
ShmemIsValid(unsigned long addr)
{
318
	return (addr < ShmemEnd) && (addr >= ShmemBase);
319 320 321
}

/*
322 323
 * ShmemInitHash -- Create/Attach to and initialize
 *		shared memory hash table.
324 325 326 327 328 329 330 331
 *
 * Notes:
 *
 * assume caller is doing some kind of synchronization
 * so that two people dont try to create/initialize the
 * table at once.  Use SpinAlloc() to create a spinlock
 * for the structure before creating the structure itself.
 */
332
HTAB *
333
ShmemInitHash(char *name,		/* table string name for shmem index */
334
			  long init_size,	/* initial table size */
335
			  long max_size,	/* max size of the table */
336
			  HASHCTL *infoP,	/* info about key and bucket size */
337
			  int hash_flags)	/* info about infoP */
338
{
339 340
	bool		found;
	long	   *location;
341 342

	/*
343 344 345 346 347
	 * Hash tables allocated in shared memory have a fixed directory;
	 * it can't grow or other backends wouldn't be able to find it.
	 * So, make sure we make it big enough to start with.
	 *
	 * The segbase is for calculating pointer values. The shared memory
B
Bruce Momjian 已提交
348
	 * allocator must be specified too.
349
	 */
350
	infoP->dsize = infoP->max_dsize = hash_select_dirsize(max_size);
351 352
	infoP->segbase = (long *) ShmemBase;
	infoP->alloc = ShmemAlloc;
353
	hash_flags |= HASH_SHARED_MEM | HASH_DIRSIZE;
354

355
	/* look it up in the shmem index */
356
	location = ShmemInitStruct(name,
357
						 sizeof(HHDR) + infoP->dsize * sizeof(SEG_OFFSET),
358
							   &found);
359 360

	/*
361
	 * shmem index is corrupted.	Let someone else give the error
362 363 364
	 * message since they have more information
	 */
	if (location == NULL)
365
		return 0;
366 367 368 369 370 371 372 373

	/*
	 * it already exists, attach to it rather than allocate and initialize
	 * new space
	 */
	if (found)
		hash_flags |= HASH_ATTACH;

374
	/* Now provide the header and directory pointers */
375
	infoP->hctl = (long *) location;
B
Bruce Momjian 已提交
376
	infoP->dir = (long *) (((char *) location) + sizeof(HHDR));
377

378
	return hash_create(init_size, infoP, hash_flags);
379 380 381 382 383 384
}

/*
 * ShmemPIDLookup -- lookup process data structure using process id
 *
 * Returns: TRUE if no error.  locationPtr is initialized if PID is
385
 *		found in the shmem index.
386 387
 *
 * NOTES:
388 389
 *		only information about success or failure is the value of
 *		locationPtr.
390 391
 */
bool
392
ShmemPIDLookup(int pid, SHMEM_OFFSET *locationPtr)
393
{
394
	ShmemIndexEnt *result,
395 396
				item;
	bool		found;
397

398 399
	Assert(ShmemIndex);
	MemSet(item.key, 0, SHMEM_INDEX_KEYSIZE);
400 401
	sprintf(item.key, "PID %d", pid);

402 403 404
	SpinAcquire(ShmemIndexLock);
	result = (ShmemIndexEnt *)
		hash_search(ShmemIndex, (char *) &item, HASH_ENTER, &found);
405 406 407 408

	if (!result)
	{

409 410
		SpinRelease(ShmemIndexLock);
		elog(ERROR, "ShmemInitPID: ShmemIndex corrupted");
411
		return FALSE;
412 413 414 415 416 417 418 419

	}

	if (found)
		*locationPtr = result->location;
	else
		result->location = *locationPtr;

420
	SpinRelease(ShmemIndexLock);
421
	return TRUE;
422 423 424
}

/*
425
 * ShmemPIDDestroy -- destroy shmem index entry for process
426
 *		using process id
427 428
 *
 * Returns: offset of the process struct in shared memory or
429
 *		INVALID_OFFSET if not found.
430
 *
431
 * Side Effect: removes the entry from the shmem index
432 433 434 435
 */
SHMEM_OFFSET
ShmemPIDDestroy(int pid)
{
436
	ShmemIndexEnt *result,
437 438 439
				item;
	bool		found;
	SHMEM_OFFSET location = 0;
440

441
	Assert(ShmemIndex);
442

443
	MemSet(item.key, 0, SHMEM_INDEX_KEYSIZE);
444 445
	sprintf(item.key, "PID %d", pid);

446 447 448
	SpinAcquire(ShmemIndexLock);
	result = (ShmemIndexEnt *)
		hash_search(ShmemIndex, (char *) &item, HASH_REMOVE, &found);
449 450 451

	if (found)
		location = result->location;
452
	SpinRelease(ShmemIndexLock);
453 454 455 456

	if (!result)
	{

457
		elog(ERROR, "ShmemPIDDestroy: PID table corrupted");
458
		return INVALID_OFFSET;
459 460 461 462

	}

	if (found)
463
		return location;
464
	else
465
		return INVALID_OFFSET;
466 467 468 469
}

/*
 * ShmemInitStruct -- Create/attach to a structure in shared
470
 *		memory.
471
 *
472 473 474 475 476
 *	This is called during initialization to find or allocate
 *		a data structure in shared memory.	If no other processes
 *		have created the structure, this routine allocates space
 *		for it.  If it exists already, a pointer to the existing
 *		table is returned.
477
 *
478
 *	Returns: real pointer to the object.  FoundPtr is TRUE if
479
 *		the object is already in the shmem index (hence, already
480
 *		initialized).
481
 */
482
long *
483
ShmemInitStruct(char *name, unsigned long size, bool *foundPtr)
484
{
485
	ShmemIndexEnt *result,
486 487
				item;
	long	   *structPtr;
488

489
	strncpy(item.key, name, SHMEM_INDEX_KEYSIZE);
490 491
	item.location = BAD_LOCATION;

492
	SpinAcquire(ShmemIndexLock);
B
Bruce Momjian 已提交
493

494
	if (!ShmemIndex)
495
	{
B
Bruce Momjian 已提交
496
#ifdef USE_ASSERT_CHECKING
497
		char	   *strname = "ShmemIndex";
498

499
#endif
500

501
		/*
502
		 * If the shmem index doesnt exist, we fake it.
503
		 *
504
		 * If we are creating the first shmem index, then let shmemalloc()
505
		 * allocate the space for a new HTAB.  Otherwise, find the old one
506 507
		 * and return that.  Notice that the ShmemIndexLock is held until
		 * the shmem index has been completely initialized.
508 509 510 511 512 513 514
		 */
		Assert(!strcmp(name, strname));
		if (ShmemBootstrap)
		{
			/* in POSTMASTER/Single process */

			*foundPtr = FALSE;
515
			return (long *) ShmemAlloc(size);
516 517 518
		}
		else
		{
519
			Assert(ShmemIndexOffset);
520 521

			*foundPtr = TRUE;
522
			return (long *) MAKE_PTR(*ShmemIndexOffset);
523 524 525
		}


526
	}
527 528
	else
	{
529 530 531
		/* look it up in the shmem index */
		result = (ShmemIndexEnt *)
			hash_search(ShmemIndex, (char *) &item, HASH_ENTER, foundPtr);
532 533 534 535
	}

	if (!result)
	{
536
		SpinRelease(ShmemIndexLock);
537

538
		elog(ERROR, "ShmemInitStruct: Shmem Index corrupted");
539
		return NULL;
540

541
	}
542 543
	else if (*foundPtr)
	{
544

545
		/*
546
		 * Structure is in the shmem index so someone else has allocated
547 548 549 550 551
		 * it already.	The size better be the same as the size we are
		 * trying to initialize to or there is a name conflict (or worse).
		 */
		if (result->size != size)
		{
552
			SpinRelease(ShmemIndexLock);
553

554
			elog(NOTICE, "ShmemInitStruct: ShmemIndex entry size is wrong");
555
			/* let caller print its message too */
556
			return NULL;
557 558 559 560 561 562 563 564 565 566
		}
		structPtr = (long *) MAKE_PTR(result->location);
	}
	else
	{
		/* It isn't in the table yet. allocate and initialize it */
		structPtr = ShmemAlloc((long) size);
		if (!structPtr)
		{
			/* out of memory */
567 568 569
			Assert(ShmemIndex);
			hash_search(ShmemIndex, (char *) &item, HASH_REMOVE, foundPtr);
			SpinRelease(ShmemIndexLock);
570 571 572 573
			*foundPtr = FALSE;

			elog(NOTICE, "ShmemInitStruct: cannot allocate '%s'",
				 name);
574
			return NULL;
575 576 577 578 579 580
		}
		result->size = size;
		result->location = MAKE_OFFSET(structPtr);
	}
	Assert(ShmemIsValid((unsigned long) structPtr));

581
	SpinRelease(ShmemIndexLock);
582
	return structPtr;
583
}