shmem.c 15.9 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * shmem.c--
4
 *	  create shared memory and initialize shared memory data structures.
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/storage/ipc/shmem.c,v 1.24 1998/06/27 14:06:41 momjian Exp $
11 12 13 14 15 16 17 18 19 20
 *
 *-------------------------------------------------------------------------
 */
/*
 * POSTGRES processes share one or more regions of shared memory.
 * The shared memory is created by a postmaster and is "attached to"
 * by each of the backends.  The routines in this file are used for
 * allocating and binding to shared memory data structures.
 *
 * NOTES:
21 22 23 24 25 26 27 28 29 30
 *		(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).
31
 *
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 *		(b) During initialization, each module looks for its
 *	shared data structures in a hash table called the "Binding Table".
 *	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.
 *		The binding table has two purposes: first, it gives us
 *	a simple model of how the world looks when a backend process
 *	initializes.  If something is present in the binding table,
 *	it is initialized.	If it is not, it is uninitialized.	Second,
 *	the binding table allows us to allocate shared memory on demand
 *	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.
47
 *
48 49 50 51 52 53 54
 *		(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.
55
 *
56 57
 *		See InitSem() in sem.c for an example of how to use the
 *	binding table.
58 59 60 61
 *
 */
#include <stdio.h>
#include <string.h>
62

63 64 65 66
#include "postgres.h"
#include "storage/ipc.h"
#include "storage/shmem.h"
#include "storage/spin.h"
67
#include "storage/proc.h"
B
Bruce Momjian 已提交
68
#include "utils/dynahash.h"
69 70 71 72
#include "utils/hsearch.h"

/* shared memory global variables */

73
unsigned long ShmemBase = 0;	/* start and end address of shared memory */
74 75
static unsigned long ShmemEnd = 0;
static unsigned long ShmemSize = 0;		/* current size (and default) */
76

77
SPINLOCK	ShmemLock;			/* lock for shared memory allocation */
78

79
SPINLOCK	BindingLock;		/* lock for binding table access */
80

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

89
static HTAB *BindingTable = NULL;
90 91

/* ---------------------
92
 * ShmemBindingTableReset() - Resets the binding table to NULL....
93 94 95 96 97
 * useful when the postmaster destroys existing shared memory
 * and creates all new segments after a backend crash.
 * ----------------------
 */
void
98
ShmemBindingTableReset(void)
99
{
100
	BindingTable = (HTAB *) NULL;
101 102 103
}

/*
104
 *	CreateSharedRegion() --
105
 *
106 107 108 109
 *	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.
110 111 112 113 114 115 116 117 118
 *
 * 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)
{
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	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;
135 136 137
}

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

148 149 150 151 152 153
	HASHCTL		info;
	int			hash_flags;
	BindingEnt *result,
				item;
	bool		found;
	IpcMemoryId shmid;
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

	/* 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");
		return (FALSE);
	}

	/* 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;
	/* next is a shmem pointer to the binding table */
182
	ShmemBindingTableOffset = ShmemFreeStart + 1;
183 184

	currFreeSpace +=
185
		sizeof(ShmemFreeStart) + sizeof(ShmemBindingTableOffset);
186 187 188 189

	/*
	 * bootstrap initialize spin locks so we can start to use the
	 * allocator and binding table.
190
	 */
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
	if (!InitSpinLocks(ShmemBootstrap, IPCKeyGetSpinLockSemaphoreKey(key)))
		return (FALSE);

	/*
	 * We have just allocated additional space for two spinlocks. Now
	 * setup the global free space count
	 */
	if (ShmemBootstrap)
		*ShmemFreeStart = currFreeSpace;

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

	/* create OR attach to the shared memory binding table */
	info.keysize = BTABLE_KEYSIZE;
	info.datasize = BTABLE_DATASIZE;
	hash_flags = (HASH_ELEM);

	/* This will acquire the binding table lock, but not release it. */
	BindingTable = ShmemInitHash("BindingTable",
								 BTABLE_SIZE, BTABLE_SIZE,
								 &info, hash_flags);

	if (!BindingTable)
	{
		elog(FATAL, "InitShmem: couldn't initialize Binding Table");
		return (FALSE);
	}

	/*
	 * Now, check the binding table for an entry to the binding table.	If
222 223
	 * there is an entry there, someone else created the table. Otherwise,
	 * we did and we have to initialize it.
224
	 */
B
Bruce Momjian 已提交
225
	MemSet(item.key, 0, BTABLE_KEYSIZE);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	strncpy(item.key, "BindingTable", BTABLE_KEYSIZE);

	result = (BindingEnt *)
		hash_search(BindingTable, (char *) &item, HASH_ENTER, &found);


	if (!result)
	{
		elog(FATAL, "InitShmem: corrupted binding table");
		return (FALSE);
	}

	if (!found)
	{

		/*
		 * bootstrapping shmem: we have to initialize the binding table
		 * now.
		 */

		Assert(ShmemBootstrap);
		result->location = MAKE_OFFSET(BindingTable->hctl);
248
		*ShmemBindingTableOffset = result->location;
249 250 251 252 253 254 255 256 257 258 259 260 261
		result->size = BTABLE_SIZE;

		ShmemBootstrap = FALSE;

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

	Assert(result->location == MAKE_OFFSET(BindingTable->hctl));

	return (TRUE);
262 263 264 265
}

/*
 * ShmemAlloc -- allocate word-aligned byte string from
266
 *		shared memory
267 268 269
 *
 * Assumes ShmemLock and ShmemFreeStart are initialized.
 * Returns: real pointer to memory or NULL if we are out
270 271
 *		of space.  Has to return a real pointer in order
 *		to be compatable with malloc().
272
 */
273
long *
274 275
ShmemAlloc(unsigned long size)
{
276 277
	unsigned long tmpFree;
	long	   *newSpace;
278 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 ");
	return (newSpace);
307 308 309
}

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

/*
321 322
 * ShmemInitHash -- Create/Attach to and initialize
 *		shared memory hash table.
323 324 325 326 327 328 329 330
 *
 * 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.
 */
331
HTAB *
332 333 334
ShmemInitHash(char *name,		/* table string name for binding */
			  long init_size,	/* initial size */
			  long max_size,	/* max size of the table */
335
			  HASHCTL *infoP,	/* info about key and bucket size */
336
			  int hash_flags)	/* info about infoP */
337
{
338 339
	bool		found;
	long	   *location;
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

	/*
	 * shared memory hash tables have a fixed max size so that the control
	 * structures don't try to grow.  The segbase is for calculating
	 * pointer values.	The shared memory allocator must be specified.
	 */
	infoP->segbase = (long *) ShmemBase;
	infoP->alloc = ShmemAlloc;
	infoP->max_size = max_size;
	hash_flags |= HASH_SHARED_MEM;

	/* look it up in the binding table */
	location =
		ShmemInitStruct(name, my_log2(max_size) + sizeof(HHDR), &found);

	/*
	 * binding table is corrupted.	Let someone else give the error
	 * message since they have more information
	 */
	if (location == NULL)
		return (0);

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

	/* these structures were allocated or bound in ShmemInitStruct */
	/* control information and parameters */
	infoP->hctl = (long *) location;
	/* directory for hash lookup */
	infoP->dir = (long *) (location + sizeof(HHDR));

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

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

	Assert(BindingTable);
B
Bruce Momjian 已提交
396
	MemSet(item.key, 0, BTABLE_KEYSIZE);
397 398 399 400 401 402 403 404 405 406
	sprintf(item.key, "PID %d", pid);

	SpinAcquire(BindingLock);
	result = (BindingEnt *)
		hash_search(BindingTable, (char *) &item, HASH_ENTER, &found);

	if (!result)
	{

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

	}

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

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

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

	Assert(BindingTable);

B
Bruce Momjian 已提交
440
	MemSet(item.key, 0, BTABLE_KEYSIZE);
441 442 443 444 445 446 447 448 449 450 451 452 453
	sprintf(item.key, "PID %d", pid);

	SpinAcquire(BindingLock);
	result = (BindingEnt *)
		hash_search(BindingTable, (char *) &item, HASH_REMOVE, &found);

	if (found)
		location = result->location;
	SpinRelease(BindingLock);

	if (!result)
	{

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

	}

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

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

	strncpy(item.key, name, BTABLE_KEYSIZE);
	item.location = BAD_LOCATION;

	SpinAcquire(BindingLock);

	if (!BindingTable)
	{
B
Bruce Momjian 已提交
493
#ifdef USE_ASSERT_CHECKING
494
		char	   *strname = "BindingTable";
495
#endif
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
		/*
		 * If the binding table doesnt exist, we fake it.
		 *
		 * If we are creating the first binding table, then let shmemalloc()
		 * allocate the space for a new HTAB.  Otherwise, find the old one
		 * and return that.  Notice that the BindingLock is held until the
		 * binding table has been completely initialized.
		 */
		Assert(!strcmp(name, strname));
		if (ShmemBootstrap)
		{
			/* in POSTMASTER/Single process */

			*foundPtr = FALSE;
			return ((long *) ShmemAlloc(size));
		}
		else
		{
514
			Assert(ShmemBindingTableOffset);
515 516

			*foundPtr = TRUE;
517
			return ((long *) MAKE_PTR(*ShmemBindingTableOffset));
518 519 520
		}


521
	}
522 523
	else
	{
524
		/* look it up in the binding table */
525 526 527 528 529 530 531 532
		result = (BindingEnt *)
			hash_search(BindingTable, (char *) &item, HASH_ENTER, foundPtr);
	}

	if (!result)
	{
		SpinRelease(BindingLock);

533
		elog(ERROR, "ShmemInitStruct: Binding Table corrupted");
534 535
		return (NULL);

536
	}
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
	else if (*foundPtr)
	{
		/*
		 * Structure is in the binding table so someone else has allocated
		 * 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)
		{
			SpinRelease(BindingLock);

			elog(NOTICE, "ShmemInitStruct: BindingTable entry size is wrong");
			/* let caller print its message too */
			return (NULL);
		}
		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 */
			Assert(BindingTable);
			hash_search(BindingTable, (char *) &item, HASH_REMOVE, foundPtr);
			SpinRelease(BindingLock);
			*foundPtr = FALSE;

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

	SpinRelease(BindingLock);
	return (structPtr);
577 578 579
}


580 581 582
/*
 * TransactionIdIsInProgress -- is given transaction running by some backend
 *
583
 * Strange place for this func, but we have to lookup process data structures
584 585 586
 * for all running backends. - vadim 11/26/96
 */
bool
587
TransactionIdIsInProgress(TransactionId xid)
588
{
589 590
	BindingEnt *result;
	PROC	   *proc;
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

	Assert(BindingTable);

	SpinAcquire(BindingLock);

	hash_seq((HTAB *) NULL);
	while ((result = (BindingEnt *) hash_seq(BindingTable)) != NULL)
	{
		if (result == (BindingEnt *) TRUE)
		{
			SpinRelease(BindingLock);
			return (false);
		}
		if (result->location == INVALID_OFFSET ||
			strncmp(result->key, "PID ", 4) != 0)
			continue;
		proc = (PROC *) MAKE_PTR(result->location);
		if (proc->xid == xid)
		{
			SpinRelease(BindingLock);
			return (true);
		}
613
	}
614

615
	SpinRelease(BindingLock);
616
	elog(ERROR, "TransactionIdIsInProgress: BindingTable corrupted");
617 618
	return (false);
}