⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 proc.c

📁 postgresql8.3.4源码,开源数据库
💻 C
📖 第 1 页 / 共 3 页
字号:
/*------------------------------------------------------------------------- * * proc.c *	  routines to manage per-process shared memory data structure * * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION *	  $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.199 2008/01/26 19:55:08 tgl Exp $ * *------------------------------------------------------------------------- *//* * Interface (a): *		ProcSleep(), ProcWakeup(), *		ProcQueueAlloc() -- create a shm queue for sleeping processes *		ProcQueueInit() -- create a queue without allocing memory * * Waiting for a lock causes the backend to be put to sleep.  Whoever releases * the lock wakes the process up again (and gives it an error code so it knows * whether it was awoken on an error condition). * * Interface (b): * * ProcReleaseLocks -- frees the locks associated with current transaction * * ProcKill -- destroys the shared memory state (and locks) * associated with the process. */#include "postgres.h"#include <signal.h>#include <unistd.h>#include <sys/time.h>#include "access/transam.h"#include "access/xact.h"#include "miscadmin.h"#include "postmaster/autovacuum.h"#include "storage/ipc.h"#include "storage/lmgr.h"#include "storage/proc.h"#include "storage/procarray.h"#include "storage/spin.h"/* GUC variables */int			DeadlockTimeout = 1000;int			StatementTimeout = 0;bool		log_lock_waits = false;/* Pointer to this process's PGPROC struct, if any */PGPROC	   *MyProc = NULL;/* * This spinlock protects the freelist of recycled PGPROC structures. * We cannot use an LWLock because the LWLock manager depends on already * having a PGPROC and a wait semaphore!  But these structures are touched * relatively infrequently (only at backend startup or shutdown) and not for * very long, so a spinlock is okay. */NON_EXEC_STATIC slock_t *ProcStructLock = NULL;/* Pointers to shared-memory structures */NON_EXEC_STATIC PROC_HDR *ProcGlobal = NULL;NON_EXEC_STATIC PGPROC *AuxiliaryProcs = NULL;/* If we are waiting for a lock, this points to the associated LOCALLOCK */static LOCALLOCK *lockAwaited = NULL;/* Mark these volatile because they can be changed by signal handler */static volatile bool statement_timeout_active = false;static volatile bool deadlock_timeout_active = false;static volatile DeadLockState deadlock_state = DS_NOT_YET_CHECKED;volatile bool cancel_from_timeout = false;/* timeout_start_time is set when log_lock_waits is true */static TimestampTz timeout_start_time;/* statement_fin_time is valid only if statement_timeout_active is true */static TimestampTz statement_fin_time;static void RemoveProcFromArray(int code, Datum arg);static void ProcKill(int code, Datum arg);static void AuxiliaryProcKill(int code, Datum arg);static bool CheckStatementTimeout(void);/* * Report shared-memory space needed by InitProcGlobal. */SizeProcGlobalShmemSize(void){	Size		size = 0;	/* ProcGlobal */	size = add_size(size, sizeof(PROC_HDR));	/* AuxiliaryProcs */	size = add_size(size, mul_size(NUM_AUXILIARY_PROCS, sizeof(PGPROC)));	/* MyProcs, including autovacuum */	size = add_size(size, mul_size(MaxBackends, sizeof(PGPROC)));	/* ProcStructLock */	size = add_size(size, sizeof(slock_t));	return size;}/* * Report number of semaphores needed by InitProcGlobal. */intProcGlobalSemas(void){	/*	 * We need a sema per backend (including autovacuum), plus one for each	 * auxiliary process.	 */	return MaxBackends + NUM_AUXILIARY_PROCS;}/* * InitProcGlobal - *	  Initialize the global process table during postmaster or standalone *	  backend startup. * *	  We also create all the per-process semaphores we will need to support *	  the requested number of backends.  We used to allocate semaphores *	  only when backends were actually started up, but that is bad because *	  it lets Postgres fail under load --- a lot of Unix systems are *	  (mis)configured with small limits on the number of semaphores, and *	  running out when trying to start another backend is a common failure. *	  So, now we grab enough semaphores to support the desired max number *	  of backends immediately at initialization --- if the sysadmin has set *	  MaxConnections or autovacuum_max_workers higher than his kernel will *	  support, he'll find out sooner rather than later. * *	  Another reason for creating semaphores here is that the semaphore *	  implementation typically requires us to create semaphores in the *	  postmaster, not in backends. * * Note: this is NOT called by individual backends under a postmaster, * not even in the EXEC_BACKEND case.  The ProcGlobal and AuxiliaryProcs * pointers must be propagated specially for EXEC_BACKEND operation. */voidInitProcGlobal(void){	PGPROC	   *procs;	int			i;	bool		found;	/* Create the ProcGlobal shared structure */	ProcGlobal = (PROC_HDR *)		ShmemInitStruct("Proc Header", sizeof(PROC_HDR), &found);	Assert(!found);	/*	 * Create the PGPROC structures for auxiliary (bgwriter) processes, too.	 * These do not get linked into the freeProcs list.	 */	AuxiliaryProcs = (PGPROC *)		ShmemInitStruct("AuxiliaryProcs", NUM_AUXILIARY_PROCS * sizeof(PGPROC),						&found);	Assert(!found);	/*	 * Initialize the data structures.	 */	ProcGlobal->freeProcs = INVALID_OFFSET;	ProcGlobal->autovacFreeProcs = INVALID_OFFSET;	ProcGlobal->spins_per_delay = DEFAULT_SPINS_PER_DELAY;	/*	 * Pre-create the PGPROC structures and create a semaphore for each.	 */	procs = (PGPROC *) ShmemAlloc((MaxConnections) * sizeof(PGPROC));	if (!procs)		ereport(FATAL,				(errcode(ERRCODE_OUT_OF_MEMORY),				 errmsg("out of shared memory")));	MemSet(procs, 0, MaxConnections * sizeof(PGPROC));	for (i = 0; i < MaxConnections; i++)	{		PGSemaphoreCreate(&(procs[i].sem));		procs[i].links.next = ProcGlobal->freeProcs;		ProcGlobal->freeProcs = MAKE_OFFSET(&procs[i]);	}	procs = (PGPROC *) ShmemAlloc((autovacuum_max_workers) * sizeof(PGPROC));	if (!procs)		ereport(FATAL,				(errcode(ERRCODE_OUT_OF_MEMORY),				 errmsg("out of shared memory")));	MemSet(procs, 0, autovacuum_max_workers * sizeof(PGPROC));	for (i = 0; i < autovacuum_max_workers; i++)	{		PGSemaphoreCreate(&(procs[i].sem));		procs[i].links.next = ProcGlobal->autovacFreeProcs;		ProcGlobal->autovacFreeProcs = MAKE_OFFSET(&procs[i]);	}	MemSet(AuxiliaryProcs, 0, NUM_AUXILIARY_PROCS * sizeof(PGPROC));	for (i = 0; i < NUM_AUXILIARY_PROCS; i++)	{		AuxiliaryProcs[i].pid = 0;		/* marks auxiliary proc as not in use */		PGSemaphoreCreate(&(AuxiliaryProcs[i].sem));	}	/* Create ProcStructLock spinlock, too */	ProcStructLock = (slock_t *) ShmemAlloc(sizeof(slock_t));	SpinLockInit(ProcStructLock);}/* * InitProcess -- initialize a per-process data structure for this backend */voidInitProcess(void){	/* use volatile pointer to prevent code rearrangement */	volatile PROC_HDR *procglobal = ProcGlobal;	SHMEM_OFFSET myOffset;	int			i;	/*	 * ProcGlobal should be set up already (if we are a backend, we inherit	 * this by fork() or EXEC_BACKEND mechanism from the postmaster).	 */	if (procglobal == NULL)		elog(PANIC, "proc header uninitialized");	if (MyProc != NULL)		elog(ERROR, "you already exist");	/*	 * Try to get a proc struct from the free list.  If this fails, we must be	 * out of PGPROC structures (not to mention semaphores).	 *	 * While we are holding the ProcStructLock, also copy the current shared	 * estimate of spins_per_delay to local storage.	 */	SpinLockAcquire(ProcStructLock);	set_spins_per_delay(procglobal->spins_per_delay);	if (IsAutoVacuumWorkerProcess())		myOffset = procglobal->autovacFreeProcs;	else		myOffset = procglobal->freeProcs;	if (myOffset != INVALID_OFFSET)	{		MyProc = (PGPROC *) MAKE_PTR(myOffset);		if (IsAutoVacuumWorkerProcess())			procglobal->autovacFreeProcs = MyProc->links.next;		else			procglobal->freeProcs = MyProc->links.next;		SpinLockRelease(ProcStructLock);	}	else	{		/*		 * If we reach here, all the PGPROCs are in use.  This is one of the		 * possible places to detect "too many backends", so give the standard		 * error message.  XXX do we need to give a different failure message		 * in the autovacuum case?		 */		SpinLockRelease(ProcStructLock);		ereport(FATAL,				(errcode(ERRCODE_TOO_MANY_CONNECTIONS),				 errmsg("sorry, too many clients already")));	}	/*	 * Initialize all fields of MyProc, except for the semaphore which was	 * prepared for us by InitProcGlobal.	 */	SHMQueueElemInit(&(MyProc->links));	MyProc->waitStatus = STATUS_OK;	MyProc->lxid = InvalidLocalTransactionId;	MyProc->xid = InvalidTransactionId;	MyProc->xmin = InvalidTransactionId;	MyProc->pid = MyProcPid;	/* backendId, databaseId and roleId will be filled in later */	MyProc->backendId = InvalidBackendId;	MyProc->databaseId = InvalidOid;	MyProc->roleId = InvalidOid;	MyProc->inCommit = false;	MyProc->vacuumFlags = 0;	if (IsAutoVacuumWorkerProcess())		MyProc->vacuumFlags |= PROC_IS_AUTOVACUUM;	MyProc->lwWaiting = false;	MyProc->lwExclusive = false;	MyProc->lwWaitLink = NULL;	MyProc->waitLock = NULL;	MyProc->waitProcLock = NULL;	for (i = 0; i < NUM_LOCK_PARTITIONS; i++)		SHMQueueInit(&(MyProc->myProcLocks[i]));	/*	 * We might be reusing a semaphore that belonged to a failed process. So	 * be careful and reinitialize its value here.	(This is not strictly	 * necessary anymore, but seems like a good idea for cleanliness.)	 */	PGSemaphoreReset(&MyProc->sem);	/*	 * Arrange to clean up at backend exit.	 */	on_shmem_exit(ProcKill, 0);	/*	 * Now that we have a PGPROC, we could try to acquire locks, so initialize	 * the deadlock checker.	 */	InitDeadLockChecking();}/* * InitProcessPhase2 -- make MyProc visible in the shared ProcArray. * * This is separate from InitProcess because we can't acquire LWLocks until * we've created a PGPROC, but in the EXEC_BACKEND case there is a good deal * of stuff to be done before this step that will require LWLock access. */voidInitProcessPhase2(void){	Assert(MyProc != NULL);	/*	 * We should now know what database we're in, so advertise that.  (We need	 * not do any locking here, since no other backend can yet see our	 * PGPROC.)	 */	Assert(OidIsValid(MyDatabaseId));	MyProc->databaseId = MyDatabaseId;	/*	 * Add our PGPROC to the PGPROC array in shared memory.	 */	ProcArrayAdd(MyProc);	/*	 * Arrange to clean that up at backend exit.	 */	on_shmem_exit(RemoveProcFromArray, 0);}/* * InitAuxiliaryProcess -- create a per-auxiliary-process data structure * * This is called by bgwriter and similar processes so that they will have a * MyProc value that's real enough to let them wait for LWLocks.  The PGPROC * and sema that are assigned are one of the extra ones created during * InitProcGlobal. * * Auxiliary processes are presently not expected to wait for real (lockmgr) * locks, so we need not set up the deadlock checker.  They are never added * to the ProcArray or the sinval messaging mechanism, either.	They also * don't get a VXID assigned, since this is only useful when we actually * hold lockmgr locks. */voidInitAuxiliaryProcess(void){	PGPROC	   *auxproc;	int			proctype;	int			i;	/*	 * ProcGlobal should be set up already (if we are a backend, we inherit	 * this by fork() or EXEC_BACKEND mechanism from the postmaster).	 */	if (ProcGlobal == NULL || AuxiliaryProcs == NULL)		elog(PANIC, "proc header uninitialized");	if (MyProc != NULL)		elog(ERROR, "you already exist");	/*	 * We use the ProcStructLock to protect assignment and releasing of	 * AuxiliaryProcs entries.	 *	 * While we are holding the ProcStructLock, also copy the current shared	 * estimate of spins_per_delay to local storage.	 */	SpinLockAcquire(ProcStructLock);	set_spins_per_delay(ProcGlobal->spins_per_delay);	/*	 * Find a free auxproc ... *big* trouble if there isn't one ...	 */	for (proctype = 0; proctype < NUM_AUXILIARY_PROCS; proctype++)	{		auxproc = &AuxiliaryProcs[proctype];		if (auxproc->pid == 0)			break;	}	if (proctype >= NUM_AUXILIARY_PROCS)	{		SpinLockRelease(ProcStructLock);		elog(FATAL, "all AuxiliaryProcs are in use");	}	/* Mark auxiliary proc as in use by me */	/* use volatile pointer to prevent code rearrangement */	((volatile PGPROC *) auxproc)->pid = MyProcPid;	MyProc = auxproc;	SpinLockRelease(ProcStructLock);	/*	 * Initialize all fields of MyProc, except for the semaphore which was	 * prepared for us by InitProcGlobal.	 */	SHMQueueElemInit(&(MyProc->links));	MyProc->waitStatus = STATUS_OK;	MyProc->lxid = InvalidLocalTransactionId;	MyProc->xid = InvalidTransactionId;	MyProc->xmin = InvalidTransactionId;	MyProc->backendId = InvalidBackendId;	MyProc->databaseId = InvalidOid;	MyProc->roleId = InvalidOid;	MyProc->inCommit = false;	/* we don't set the "is autovacuum" flag in the launcher */	MyProc->vacuumFlags = 0;	MyProc->lwWaiting = false;	MyProc->lwExclusive = false;	MyProc->lwWaitLink = NULL;	MyProc->waitLock = NULL;	MyProc->waitProcLock = NULL;	for (i = 0; i < NUM_LOCK_PARTITIONS; i++)		SHMQueueInit(&(MyProc->myProcLocks[i]));	/*	 * We might be reusing a semaphore that belonged to a failed process. So	 * be careful and reinitialize its value here.	(This is not strictly	 * necessary anymore, but seems like a good idea for cleanliness.)	 */	PGSemaphoreReset(&MyProc->sem);	/*	 * Arrange to clean up at process exit.	 */	on_shmem_exit(AuxiliaryProcKill, Int32GetDatum(proctype));}/* * Check whether there are at least N free PGPROC objects. * * Note: this is designed on the assumption that N will generally be small. */boolHaveNFreeProcs(int n){	SHMEM_OFFSET offset;	PGPROC	   *proc;	/* use volatile pointer to prevent code rearrangement */	volatile PROC_HDR *procglobal = ProcGlobal;	SpinLockAcquire(ProcStructLock);	offset = procglobal->freeProcs;	while (n > 0 && offset != INVALID_OFFSET)	{		proc = (PGPROC *) MAKE_PTR(offset);		offset = proc->links.next;		n--;	}	SpinLockRelease(ProcStructLock);	return (n <= 0);}/* * Cancel any pending wait for lock, when aborting a transaction. * * (Normally, this would only happen if we accept a cancel/die * interrupt while waiting; but an ereport(ERROR) while waiting is * within the realm of possibility, too.) */voidLockWaitCancel(void){	LWLockId	partitionLock;	/* Nothing to do if we weren't waiting for a lock */	if (lockAwaited == NULL)		return;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -