shmem.c 15.1 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
B
Add:  
Bruce Momjian 已提交
11
 *	  $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.48 2000/01/26 05:56:58 momjian 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 335
			  long init_size,	/* initial table size */
			  long max_size,	/* max size of the table (NOT USED) */
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

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

353
	/* look it up in the shmem index */
354
	location = ShmemInitStruct(name,
B
Bruce Momjian 已提交
355
						 sizeof(HHDR) + DEF_DIRSIZE * sizeof(SEG_OFFSET),
356
							   &found);
357 358

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

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

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

376
	return hash_create(init_size, infoP, hash_flags);
377 378 379 380 381 382
}

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

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

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

	if (!result)
	{

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

	}

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

418
	SpinRelease(ShmemIndexLock);
419
	return TRUE;
420 421 422
}

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

439
	Assert(ShmemIndex);
440

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

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

	if (found)
		location = result->location;
450
	SpinRelease(ShmemIndexLock);
451 452 453 454

	if (!result)
	{

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

	}

	if (found)
461
		return location;
462
	else
463
		return INVALID_OFFSET;
464 465 466 467
}

/*
 * ShmemInitStruct -- Create/attach to a structure in shared
468
 *		memory.
469
 *
470 471 472 473 474
 *	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.
475
 *
476
 *	Returns: real pointer to the object.  FoundPtr is TRUE if
477
 *		the object is already in the shmem index (hence, already
478
 *		initialized).
479
 */
480
long *
481
ShmemInitStruct(char *name, unsigned long size, bool *foundPtr)
482
{
483
	ShmemIndexEnt *result,
484 485
				item;
	long	   *structPtr;
486

487
	strncpy(item.key, name, SHMEM_INDEX_KEYSIZE);
488 489
	item.location = BAD_LOCATION;

490
	SpinAcquire(ShmemIndexLock);
B
Bruce Momjian 已提交
491

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

497
#endif
498

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

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

			*foundPtr = TRUE;
520
			return (long *) MAKE_PTR(*ShmemIndexOffset);
521 522 523
		}


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

	if (!result)
	{
534
		SpinRelease(ShmemIndexLock);
535

536
		elog(ERROR, "ShmemInitStruct: Shmem Index corrupted");
537
		return NULL;
538

539
	}
540 541
	else if (*foundPtr)
	{
542

543
		/*
544
		 * Structure is in the shmem index so someone else has allocated
545 546 547 548 549
		 * 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)
		{
550
			SpinRelease(ShmemIndexLock);
551

552
			elog(NOTICE, "ShmemInitStruct: ShmemIndex entry size is wrong");
553
			/* let caller print its message too */
554
			return NULL;
555 556 557 558 559 560 561 562 563 564
		}
		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 */
565 566 567
			Assert(ShmemIndex);
			hash_search(ShmemIndex, (char *) &item, HASH_REMOVE, foundPtr);
			SpinRelease(ShmemIndexLock);
568 569 570 571
			*foundPtr = FALSE;

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

579
	SpinRelease(ShmemIndexLock);
580
	return structPtr;
581
}