1 /*-------------------------------------------------------------------------
4 * routines to manage per-process shared memory data structure
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.1.1.1 1996/07/09 06:21:57 scrappy Exp $
12 *-------------------------------------------------------------------------
15 * Each postgres backend gets one of these. We'll use it to
16 * clean up after the process should the process suddenly die.
20 * ProcSleep(), ProcWakeup(), ProcWakeupNext(),
21 * ProcQueueAlloc() -- create a shm queue for sleeping processes
22 * ProcQueueInit() -- create a queue without allocing memory
24 * Locking and waiting for buffers can cause the backend to be
25 * put to sleep. Whoever releases the lock, etc. wakes the
26 * process up again (and gives it an error code so it knows
27 * whether it was awoken on an error condition).
31 * ProcReleaseLocks -- frees the locks associated with this process,
32 * ProcKill -- destroys the shared memory state (and locks)
33 * associated with the process.
35 * 5/15/91 -- removed the buffer pool based lock chain in favor
36 * of a shared memory lock chain. The write-protection is
37 * more expensive if the lock chain is in the buffer pool.
38 * The only reason I kept the lock chain in the buffer pool
39 * in the first place was to allow the lock table to grow larger
40 * than available shared memory and that isn't going to work
41 * without a lot of unimplemented support anyway.
43 * 4/7/95 -- instead of allocating a set of 1 semaphore per process, we
44 * allocate a semaphore from a set of PROC_NSEMS_PER_SET semaphores
45 * shared among backends (we keep a few sets of semaphores around).
46 * This is so that we can support more backends. (system-wide semaphore
47 * sets run out pretty fast.) -ay 4/95
49 * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.1.1.1 1996/07/09 06:21:57 scrappy Exp $
56 #include <sys/types.h>
57 #include "libpq/pqsignal.h" /* substitute for <signal.h> */
59 #if defined(PORTNAME_bsdi)
60 /* hacka, hacka, hacka (XXX) */
62 int val; /* value for SETVAL */
63 struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
64 ushort *array; /* array for GETALL & SETALL */
68 #include "access/xact.h"
69 #include "utils/hsearch.h"
70 #include "utils/elog.h"
72 #include "storage/buf.h"
73 #include "storage/lock.h"
74 #include "storage/shmem.h"
75 #include "storage/spin.h"
76 #include "storage/proc.h"
79 * timeout (in seconds) for resolving possible deadlock
81 #ifndef DEADLOCK_TIMEOUT
82 #define DEADLOCK_TIMEOUT 60
85 /* --------------------
86 * Spin lock for manipulating the shared process data structure:
87 * ProcGlobal.... Adding an extra spin lock seemed like the smallest
88 * hack to get around reading and updating this structure in shared
89 * memory. -mer 17 July 1991
90 * --------------------
92 SPINLOCK ProcStructLock;
95 * For cleanup routines. Don't cleanup if the initialization
98 static bool ProcInitialized = FALSE;
100 static PROC_HDR *ProcGlobal = NULL;
104 static void ProcKill(int exitStatus, int pid);
105 static void ProcGetNewSemKeyAndNum(IPCKey *key, int *semNum);
106 static void ProcFreeSem(IpcSemaphoreKey semKey, int semNum);
107 #if defined(PORTNAME_linux)
108 extern int HandleDeadLock(int);
110 extern int HandleDeadLock(void);
114 * initializes the global process table. We put it here so that
115 * the postmaster can do this initialization. (ProcFreeAllSem needs
116 * to read this table on exiting the postmaster. If we have the first
117 * backend do this, starting up and killing the postmaster without
118 * starting any backends will be a problem.)
121 InitProcGlobal(IPCKey key)
125 /* attach to the free list */
126 ProcGlobal = (PROC_HDR *)
127 ShmemInitStruct("Proc Header",(unsigned)sizeof(PROC_HDR),&found);
129 /* --------------------
130 * We're the first - initialize.
131 * --------------------
137 ProcGlobal->numProcs = 0;
138 ProcGlobal->freeProcs = INVALID_OFFSET;
139 ProcGlobal->currKey = IPCGetProcessSemaphoreInitKey(key);
140 for (i=0; i < MAX_PROC_SEMS/PROC_NSEMS_PER_SET; i++)
141 ProcGlobal->freeSemMap[i] = 0;
145 /* ------------------------
146 * InitProc -- create a per-process data structure for this process
147 * used by the lock manager on semaphore queues.
148 * ------------------------
151 InitProcess(IPCKey key)
156 unsigned long location, myOffset;
158 /* ------------------
159 * Routine called if deadlock timer goes off. See ProcSleep()
163 signal(SIGALRM, HandleDeadLock);
164 #endif /* WIN32 we'll have to figure out how to handle this later */
166 SpinAcquire(ProcStructLock);
168 /* attach to the free list */
169 ProcGlobal = (PROC_HDR *)
170 ShmemInitStruct("Proc Header",(unsigned)sizeof(PROC_HDR),&found);
172 /* this should not happen. InitProcGlobal() is called before this. */
173 elog(WARN, "InitProcess: Proc Header uninitialized");
178 SpinRelease(ProcStructLock);
179 elog(WARN,"ProcInit: you already exist");
183 /* try to get a proc from the free list first */
185 myOffset = ProcGlobal->freeProcs;
187 if (myOffset != INVALID_OFFSET)
189 MyProc = (PROC *) MAKE_PTR(myOffset);
190 ProcGlobal->freeProcs = MyProc->links.next;
194 /* have to allocate one. We can't use the normal binding
195 * table mechanism because the proc structure is stored
196 * by PID instead of by a global name (need to look it
197 * up by PID when we cleanup dead processes).
200 MyProc = (PROC *) ShmemAlloc((unsigned)sizeof(PROC));
203 SpinRelease(ProcStructLock);
204 elog (FATAL,"cannot create new proc: out of memory");
207 /* this cannot be initialized until after the buffer pool */
208 SHMQueueInit(&(MyProc->lockQueue));
209 MyProc->procId = ProcGlobal->numProcs;
210 ProcGlobal->numProcs++;
214 * zero out the spin lock counts and set the sLocks field for
215 * ProcStructLock to 1 as we have acquired this spinlock above but
216 * didn't record it since we didn't have MyProc until now.
218 memset(MyProc->sLocks, 0, sizeof(MyProc->sLocks));
219 MyProc->sLocks[ProcStructLock] = 1;
222 if (IsUnderPostmaster) {
228 ProcGetNewSemKeyAndNum(&semKey, &semNum);
230 semId = IpcSemaphoreCreate(semKey,
233 IpcSemaphoreDefaultStartValue,
237 * we might be reusing a semaphore that belongs to a dead
238 * backend. So be careful and reinitialize its value here.
240 semun.val = IpcSemaphoreDefaultStartValue;
241 semctl(semId, semNum, SETVAL, semun);
243 IpcSemaphoreLock(semId, semNum, IpcExclusiveLock);
244 MyProc->sem.semId = semId;
245 MyProc->sem.semNum = semNum;
246 MyProc->sem.semKey = semKey;
248 MyProc->sem.semId = -1;
251 /* ----------------------
253 * ----------------------
255 SpinRelease(ProcStructLock);
263 * Start keeping spin lock stats from here on. Any botch before
264 * this initialization is forever botched
267 memset(MyProc->sLocks, 0, MAX_SPINS*sizeof(*MyProc->sLocks));
269 /* -------------------------
270 * Install ourselves in the binding table. The name to
271 * use is determined by the OS-assigned process id. That
272 * allows the cleanup process to find us after any untimely
274 * -------------------------
277 location = MAKE_OFFSET(MyProc);
278 if ((! ShmemPIDLookup(pid,&location)) || (location != MAKE_OFFSET(MyProc)))
280 elog(FATAL,"InitProc: ShmemPID table broken");
283 MyProc->errType = NO_ERROR;
284 SHMQueueElemInit(&(MyProc->links));
286 on_exitpg(ProcKill, (caddr_t)pid);
288 ProcInitialized = TRUE;
292 * ProcReleaseLocks() -- release all locks associated with this process
300 LockReleaseAll(1,&MyProc->lockQueue);
305 * used by the postmaster to clean up the global tables. This also frees
306 * up the semaphore used for the lmgr of the process. (We have to do
307 * this is the postmaster instead of doing a IpcSemaphoreKill on exiting
308 * the process because the semaphore set is shared among backends and
309 * we don't want to remove other's semaphores on exit.)
314 SHMEM_OFFSET location;
317 location = INVALID_OFFSET;
319 location = ShmemPIDDestroy(pid);
320 if (location == INVALID_OFFSET)
322 proc = (PROC *) MAKE_PTR(location);
324 SpinAcquire(ProcStructLock);
326 ProcFreeSem(proc->sem.semKey, proc->sem.semNum);
328 proc->links.next = ProcGlobal->freeProcs;
329 ProcGlobal->freeProcs = MAKE_OFFSET(proc);
331 SpinRelease(ProcStructLock);
337 * ProcKill() -- Destroy the per-proc data structure for
338 * this process. Release any of its held spin locks.
341 ProcKill(int exitStatus, int pid)
344 SHMEM_OFFSET location;
346 /* --------------------
347 * If this is a FATAL exit the postmaster will have to kill all the
348 * existing backends and reinitialize shared memory. So all we don't
349 * need to do anything here.
350 * --------------------
360 ShmemPIDLookup(pid,&location);
361 if (location == INVALID_OFFSET)
364 proc = (PROC *) MAKE_PTR(location);
366 if (proc != MyProc) {
367 Assert( pid != getpid() );
372 * Assume one lock table.
375 ProcReleaseSpins(proc);
376 LockReleaseAll(1,&proc->lockQueue);
379 * get off the wait queue
383 if (proc->links.next != INVALID_OFFSET) {
384 Assert(proc->waitLock->waitProcs.size > 0);
385 SHMQueueDelete(&(proc->links));
386 --proc->waitLock->waitProcs.size;
388 SHMQueueElemInit(&(proc->links));
395 * ProcQueue package: routines for putting processes to sleep
400 * ProcQueueAlloc -- alloc/attach to a shared memory process queue
402 * Returns: a pointer to the queue or NULL
403 * Side Effects: Initializes the queue if we allocated one
406 ProcQueueAlloc(char *name)
409 PROC_QUEUE *queue = (PROC_QUEUE *)
410 ShmemInitStruct(name,(unsigned)sizeof(PROC_QUEUE),&found);
418 ProcQueueInit(queue);
424 * ProcQueueInit -- initialize a shared memory process queue
427 ProcQueueInit(PROC_QUEUE *queue)
429 SHMQueueInit(&(queue->links));
436 * ProcSleep -- put a process to sleep
438 * P() on the semaphore should put us to sleep. The process
439 * semaphore is cleared by default, so the first time we try
440 * to acquire it, we sleep.
442 * ASSUME: that no one will fiddle with the queue until after
443 * we release the spin lock.
445 * NOTES: The process queue is now a priority queue for locking.
448 ProcSleep(PROC_QUEUE *queue,
456 #ifndef WIN32 /* figure this out later */
457 struct itimerval timeval, dummy;
460 proc = (PROC *) MAKE_PTR(queue->links.prev);
461 for (i=0;i<queue->size;i++)
463 if (proc->prio < prio)
464 proc = (PROC *) MAKE_PTR(proc->links.prev);
469 MyProc->token = token;
470 MyProc->waitLock = lock;
472 /* -------------------
473 * currently, we only need this for the ProcWakeup routines
474 * -------------------
476 TransactionIdStore((TransactionId) GetCurrentTransactionId(), &MyProc->xid);
478 /* -------------------
479 * assume that these two operations are atomic (because
481 * -------------------
483 SHMQueueInsertTL(&(proc->links),&(MyProc->links));
486 SpinRelease(spinlock);
489 * Postgres does not have any deadlock detection code and for this
490 * reason we must set a timer to wake up the process in the event of
491 * a deadlock. For now the timer is set for 1 minute and we assume that
492 * any process which sleeps for this amount of time is deadlocked and will
493 * receive a SIGALRM signal. The handler should release the processes
494 * semaphore and abort the current transaction.
496 * Need to zero out struct to set the interval and the micro seconds fields
501 memset(&timeval, 0, sizeof(struct itimerval));
502 timeval.it_value.tv_sec = DEADLOCK_TIMEOUT;
504 if (setitimer(ITIMER_REAL, &timeval, &dummy))
505 elog(FATAL, "ProcSleep: Unable to set timer for process wakeup");
509 * if someone wakes us between SpinRelease and IpcSemaphoreLock,
510 * IpcSemaphoreLock will not block. The wakeup is "saved" by
511 * the semaphore implementation.
514 IpcSemaphoreLock(MyProc->sem.semId, MyProc->sem.semNum, IpcExclusiveLock);
517 * We were awoken before a timeout - now disable the timer
521 timeval.it_value.tv_sec = 0;
524 if (setitimer(ITIMER_REAL, &timeval, &dummy))
525 elog(FATAL, "ProcSleep: Unable to diable timer for process wakeup");
529 * We were assumed to be in a critical section when we went
533 SpinAcquire(spinlock);
535 return(MyProc->errType);
540 * ProcWakeup -- wake up a process by releasing its private semaphore.
542 * remove the process from the wait queue and set its links invalid.
543 * RETURN: the next process in the wait queue.
546 ProcWakeup(PROC *proc, int errType)
549 /* assume that spinlock has been acquired */
551 if (proc->links.prev == INVALID_OFFSET ||
552 proc->links.next == INVALID_OFFSET)
553 return((PROC *) NULL);
555 retProc = (PROC *) MAKE_PTR(proc->links.prev);
557 /* you have to update waitLock->waitProcs.size yourself */
558 SHMQueueDelete(&(proc->links));
559 SHMQueueElemInit(&(proc->links));
561 proc->errType = errType;
563 IpcSemaphoreUnlock(proc->sem.semId, proc->sem.semNum, IpcExclusiveLock);
575 return( MyProc->procId );
579 * ProcLockWakeup -- routine for waking up processes when a lock is
583 ProcLockWakeup(PROC_QUEUE *queue, char *ltable, char *lock)
589 return(STATUS_NOT_FOUND);
591 proc = (PROC *) MAKE_PTR(queue->links.prev);
593 while ((LockResolveConflicts ((LOCKTAB *) ltable,
596 proc->xid) == STATUS_OK))
598 /* there was a waiting process, grant it the lock before waking it
599 * up. This will prevent another process from seizing the lock
600 * between the time we release the lock master (spinlock) and
601 * the time that the awoken process begins executing again.
603 GrantLock((LOCK *) lock, proc->token);
607 * ProcWakeup removes proc from the lock waiting process queue and
608 * returns the next proc in chain. If a writer just dropped
609 * its lock and there are several waiting readers, wake them all up.
611 proc = ProcWakeup(proc, NO_ERROR);
614 if (!proc || queue->size == 0)
621 /* Something is still blocking us. May have deadlocked. */
622 return(STATUS_NOT_FOUND);
626 ProcAddLock(SHM_QUEUE *elem)
628 SHMQueueInsertTL(&MyProc->lockQueue,elem);
631 /* --------------------
632 * We only get to this routine if we got SIGALRM after DEADLOCK_TIMEOUT
633 * while waiting for a lock to be released by some other process. After
634 * the one minute deadline we assume we have a deadlock and must abort
635 * this transaction. We must also indicate that I'm no longer waiting
636 * on a lock so that other processes don't try to wake me up and screw
638 * --------------------
641 #if defined(PORTNAME_linux)
642 HandleDeadLock(int i)
652 /* ---------------------
653 * Check to see if we've been awoken by anyone in the interim.
655 * If we have we can return and resume our transaction -- happy day.
656 * Before we are awoken the process releasing the lock grants it to
657 * us so we know that we don't have to wait anymore.
659 * Damn these names are LONG! -mer
660 * ---------------------
662 if (IpcSemaphoreGetCount(MyProc->sem.semId, MyProc->sem.semNum) ==
663 IpcSemaphoreDefaultStartValue) {
669 * you would think this would be unnecessary, but...
671 * this also means we've been removed already. in some ports
672 * (e.g., sparc and aix) the semop(2) implementation is such that
673 * we can actually end up in this handler after someone has removed
674 * us from the queue and bopped the semaphore *but the test above
675 * fails to detect the semaphore update* (presumably something weird
676 * having to do with the order in which the semaphore wakeup signal
677 * and SIGALRM get handled).
679 if (MyProc->links.prev == INVALID_OFFSET ||
680 MyProc->links.next == INVALID_OFFSET) {
685 lock = MyProc->waitLock;
686 size = lock->waitProcs.size; /* so we can look at this in the core */
688 /* ------------------------
689 * Get this process off the lock's wait queue
690 * ------------------------
692 Assert(lock->waitProcs.size > 0);
693 --lock->waitProcs.size;
694 SHMQueueDelete(&(MyProc->links));
695 SHMQueueElemInit(&(MyProc->links));
697 /* ------------------
698 * Unlock my semaphore so that the count is right for next time.
699 * I was awoken by a signal, not by someone unlocking my semaphore.
702 IpcSemaphoreUnlock(MyProc->sem.semId, MyProc->sem.semNum, IpcExclusiveLock);
705 * Set MyProc->errType to STATUS_ERROR so that we abort after
706 * returning from this handler.
709 MyProc->errType = STATUS_ERROR;
712 * if this doesn't follow the IpcSemaphoreUnlock then we get lock
713 * table corruption ("LockReplace: xid table corrupted") due to
714 * race conditions. i don't claim to understand this...
718 elog(NOTICE, "Timeout -- possible deadlock");
723 ProcReleaseSpins(PROC *proc)
732 for (i=0; i < (int)MAX_SPINS; i++)
736 Assert(proc->sLocks[i] == 1);
742 /*****************************************************************************
744 *****************************************************************************/
747 * ProcGetNewSemKeyAndNum -
748 * scan the free semaphore bitmap and allocate a single semaphore from
749 * a semaphore set. (If the semaphore set doesn't exist yet,
750 * IpcSemaphoreCreate will create it. Otherwise, we use the existing
754 ProcGetNewSemKeyAndNum(IPCKey *key, int *semNum)
757 int32 *freeSemMap = ProcGlobal->freeSemMap;
758 unsigned int fullmask;
761 * we hold ProcStructLock when entering this routine. We scan through
762 * the bitmap to look for a free semaphore.
764 fullmask = ~0 >> (32 - PROC_NSEMS_PER_SET);
765 for(i=0; i < MAX_PROC_SEMS/PROC_NSEMS_PER_SET; i++) {
769 if (freeSemMap[i] == fullmask)
770 continue; /* none free for this set */
772 for(j = 0; j < PROC_NSEMS_PER_SET; j++) {
773 if ((freeSemMap[i] & mask) == 0) {
775 * a free semaphore found. Mark it as allocated.
777 freeSemMap[i] |= mask;
779 *key = ProcGlobal->currKey + i;
787 /* if we reach here, all the semaphores are in use. */
788 elog(WARN, "InitProc: cannot allocate a free semaphore");
793 * free up our semaphore in the semaphore set. If we're the last one
794 * in the set, also remove the semaphore set.
797 ProcFreeSem(IpcSemaphoreKey semKey, int semNum)
801 int32 *freeSemMap = ProcGlobal->freeSemMap;
803 i = semKey - ProcGlobal->currKey;
804 mask = ~(1 << semNum);
805 freeSemMap[i] &= mask;
807 if (freeSemMap[i]==0)
808 IpcSemaphoreKill(semKey);
812 * ProcFreeAllSemaphores -
813 * on exiting the postmaster, we free up all the semaphores allocated
814 * to the lmgrs of the backends.
817 ProcFreeAllSemaphores()
820 int32 *freeSemMap = ProcGlobal->freeSemMap;
822 for(i=0; i < MAX_PROC_SEMS/PROC_NSEMS_PER_SET; i++) {
823 if (freeSemMap[i]!=0)
824 IpcSemaphoreKill(ProcGlobal->currKey + i);