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.44 1998/12/18 19:45:37 momjian 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.44 1998/12/18 19:45:37 momjian Exp $
55 #include <sys/types.h>
57 #if defined(solaris_sparc)
63 #include "miscadmin.h"
64 #include "libpq/pqsignal.h"
66 #include "access/xact.h"
67 #include "utils/hsearch.h"
69 #include "storage/ipc.h"
70 /* In Ultrix, sem.h must be included after ipc.h */
72 #include "storage/buf.h"
73 #include "storage/lock.h"
74 #include "storage/lmgr.h"
75 #include "storage/shmem.h"
76 #include "storage/spin.h"
77 #include "storage/proc.h"
78 #include "utils/trace.h"
80 static void HandleDeadLock(void);
81 static PROC *ProcWakeup(PROC *proc, int errType);
83 #define DeadlockCheckTimer pg_options[OPT_DEADLOCKTIMEOUT]
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);
110 * initializes the global process table. We put it here so that
111 * the postmaster can do this initialization. (ProcFreeAllSem needs
112 * to read this table on exiting the postmaster. If we have the first
113 * backend do this, starting up and killing the postmaster without
114 * starting any backends will be a problem.)
117 InitProcGlobal(IPCKey key)
121 /* attach to the free list */
122 ProcGlobal = (PROC_HDR *)
123 ShmemInitStruct("Proc Header", (unsigned) sizeof(PROC_HDR), &found);
125 /* --------------------
126 * We're the first - initialize.
127 * --------------------
133 ProcGlobal->freeProcs = INVALID_OFFSET;
134 ProcGlobal->currKey = IPCGetProcessSemaphoreInitKey(key);
135 for (i = 0; i < MAX_PROC_SEMS / PROC_NSEMS_PER_SET; i++)
136 ProcGlobal->freeSemMap[i] = 0;
140 /* ------------------------
141 * InitProc -- create a per-process data structure for this process
142 * used by the lock manager on semaphore queues.
143 * ------------------------
146 InitProcess(IPCKey key)
150 unsigned long location,
153 /* ------------------
154 * Routine called if deadlock timer goes off. See ProcSleep()
157 SpinAcquire(ProcStructLock);
159 /* attach to the free list */
160 ProcGlobal = (PROC_HDR *)
161 ShmemInitStruct("Proc Header", (unsigned) sizeof(PROC_HDR), &found);
164 /* this should not happen. InitProcGlobal() is called before this. */
165 elog(ERROR, "InitProcess: Proc Header uninitialized");
170 SpinRelease(ProcStructLock);
171 elog(ERROR, "ProcInit: you already exist");
175 /* try to get a proc from the free list first */
177 myOffset = ProcGlobal->freeProcs;
179 if (myOffset != INVALID_OFFSET)
181 MyProc = (PROC *) MAKE_PTR(myOffset);
182 ProcGlobal->freeProcs = MyProc->links.next;
188 * have to allocate one. We can't use the normal shmem index
189 * table mechanism because the proc structure is stored by PID
190 * instead of by a global name (need to look it up by PID when we
191 * cleanup dead processes).
194 MyProc = (PROC *) ShmemAlloc((unsigned) sizeof(PROC));
197 SpinRelease(ProcStructLock);
198 elog(FATAL, "cannot create new proc: out of memory");
201 /* this cannot be initialized until after the buffer pool */
202 SHMQueueInit(&(MyProc->lockQueue));
206 * zero out the spin lock counts and set the sLocks field for
207 * ProcStructLock to 1 as we have acquired this spinlock above but
208 * didn't record it since we didn't have MyProc until now.
210 MemSet(MyProc->sLocks, 0, sizeof(MyProc->sLocks));
211 MyProc->sLocks[ProcStructLock] = 1;
214 if (IsUnderPostmaster)
221 ProcGetNewSemKeyAndNum(&semKey, &semNum);
223 semId = IpcSemaphoreCreate(semKey,
226 IpcSemaphoreDefaultStartValue,
231 * we might be reusing a semaphore that belongs to a dead backend.
232 * So be careful and reinitialize its value here.
234 semun.val = IpcSemaphoreDefaultStartValue;
235 semctl(semId, semNum, SETVAL, semun);
237 IpcSemaphoreLock(semId, semNum, IpcExclusiveLock);
238 MyProc->sem.semId = semId;
239 MyProc->sem.semNum = semNum;
240 MyProc->sem.semKey = semKey;
243 MyProc->sem.semId = -1;
245 /* ----------------------
247 * ----------------------
249 SpinRelease(ProcStructLock);
251 MyProc->pid = MyProcPid;
252 MyProc->xid = InvalidTransactionId;
253 #ifdef LowLevelLocking
254 MyProc->xmin = InvalidTransactionId;
258 * Start keeping spin lock stats from here on. Any botch before
259 * this initialization is forever botched
262 MemSet(MyProc->sLocks, 0, MAX_SPINS * sizeof(*MyProc->sLocks));
264 /* -------------------------
265 * Install ourselves in the shmem index table. The name to
266 * use is determined by the OS-assigned process id. That
267 * allows the cleanup process to find us after any untimely
269 * -------------------------
271 location = MAKE_OFFSET(MyProc);
272 if ((!ShmemPIDLookup(MyProcPid, &location)) || (location != MAKE_OFFSET(MyProc)))
273 elog(FATAL, "InitProc: ShmemPID table broken");
275 MyProc->errType = NO_ERROR;
276 SHMQueueElemInit(&(MyProc->links));
278 on_shmem_exit(ProcKill, (caddr_t) MyProcPid);
280 ProcInitialized = TRUE;
284 * ProcReleaseLocks() -- release all locks associated with this process
292 LockReleaseAll(1, &MyProc->lockQueue);
297 * used by the postmaster to clean up the global tables. This also frees
298 * up the semaphore used for the lmgr of the process. (We have to do
299 * this is the postmaster instead of doing a IpcSemaphoreKill on exiting
300 * the process because the semaphore set is shared among backends and
301 * we don't want to remove other's semaphores on exit.)
306 SHMEM_OFFSET location;
309 location = INVALID_OFFSET;
311 location = ShmemPIDDestroy(pid);
312 if (location == INVALID_OFFSET)
314 proc = (PROC *) MAKE_PTR(location);
316 SpinAcquire(ProcStructLock);
318 ProcFreeSem(proc->sem.semKey, proc->sem.semNum);
320 proc->links.next = ProcGlobal->freeProcs;
321 ProcGlobal->freeProcs = MAKE_OFFSET(proc);
323 SpinRelease(ProcStructLock);
329 * ProcKill() -- Destroy the per-proc data structure for
330 * this process. Release any of its held spin locks.
333 ProcKill(int exitStatus, int pid)
336 SHMEM_OFFSET location;
338 /* --------------------
339 * If this is a FATAL exit the postmaster will have to kill all the
340 * existing backends and reinitialize shared memory. So all we don't
341 * need to do anything here.
342 * --------------------
347 ShmemPIDLookup(MyProcPid, &location);
348 if (location == INVALID_OFFSET)
351 proc = (PROC *) MAKE_PTR(location);
353 Assert(proc == MyProc || pid != MyProcPid);
358 * Assume one lock table.
361 ProcReleaseSpins(proc);
362 LockReleaseAll(DEFAULT_LOCKMETHOD, &proc->lockQueue);
367 * Assume we have a second lock table.
369 LockReleaseAll(USER_LOCKMETHOD, &proc->lockQueue);
373 * get off the wait queue
377 if (proc->links.next != INVALID_OFFSET)
379 Assert(proc->waitLock->waitProcs.size > 0);
380 SHMQueueDelete(&(proc->links));
381 --proc->waitLock->waitProcs.size;
383 SHMQueueElemInit(&(proc->links));
390 * ProcQueue package: routines for putting processes to sleep
395 * ProcQueueAlloc -- alloc/attach to a shared memory process queue
397 * Returns: a pointer to the queue or NULL
398 * Side Effects: Initializes the queue if we allocated one
402 ProcQueueAlloc(char *name)
405 PROC_QUEUE *queue = (PROC_QUEUE *)
406 ShmemInitStruct(name, (unsigned) sizeof(PROC_QUEUE), &found);
411 ProcQueueInit(queue);
418 * ProcQueueInit -- initialize a shared memory process queue
421 ProcQueueInit(PROC_QUEUE *queue)
423 SHMQueueInit(&(queue->links));
430 * ProcSleep -- put a process to sleep
432 * P() on the semaphore should put us to sleep. The process
433 * semaphore is cleared by default, so the first time we try
434 * to acquire it, we sleep.
436 * ASSUME: that no one will fiddle with the queue until after
437 * we release the spin lock.
439 * NOTES: The process queue is now a priority queue for locking.
442 ProcSleep(PROC_QUEUE *waitQueue,/* lock->waitProcs */
444 int token, /* lockmode */
447 TransactionId xid) /* needed by user locks, see below */
450 bool deadlock_checked = false;
452 struct timeval timeval;
455 * If the first entries in the waitQueue have a greater priority than
456 * we have, we must be a reader, and they must be a writers, and we
457 * must be here because the current holder is a writer or a reader but
458 * we don't share shared locks if a writer is waiting. We put
459 * ourselves after the writers. This way, we have a FIFO, but keep
460 * the readers together to give them decent priority, and no one
461 * starves. Because we group all readers together, a non-empty queue
462 * only has a few possible configurations:
464 * [readers] [writers] [readers][writers] [writers][readers]
465 * [writers][readers][writers]
467 * In a full queue, we would have a reader holding a lock, then a writer
468 * gets the lock, then a bunch of readers, made up of readers who
469 * could not share the first readlock because a writer was waiting,
470 * and new readers arriving while the writer had the lock.
473 proc = (PROC *) MAKE_PTR(waitQueue->links.prev);
475 /* If we are a reader, and they are writers, skip past them */
476 for (i = 0; i < waitQueue->size && proc->prio > prio; i++)
477 proc = (PROC *) MAKE_PTR(proc->links.prev);
479 /* The rest of the queue is FIFO, with readers first, writers last */
480 for (; i < waitQueue->size && proc->prio <= prio; i++)
481 proc = (PROC *) MAKE_PTR(proc->links.prev);
484 MyProc->token = token;
485 MyProc->waitLock = lock;
488 /* -------------------
489 * Currently, we only need this for the ProcWakeup routines.
490 * This must be 0 for user lock, so we can't just use the value
491 * from GetCurrentTransactionId().
492 * -------------------
494 TransactionIdStore(xid, &MyProc->xid);
496 #ifndef LowLevelLocking
497 /* -------------------
498 * currently, we only need this for the ProcWakeup routines
499 * -------------------
501 TransactionIdStore((TransactionId) GetCurrentTransactionId(), &MyProc->xid);
505 /* -------------------
506 * assume that these two operations are atomic (because
508 * -------------------
510 SHMQueueInsertTL(&(proc->links), &(MyProc->links));
513 SpinRelease(spinlock);
516 * We set this so we can wake up periodically and check for a deadlock.
517 * If a deadlock is detected, the handler releases the processes
518 * semaphore and aborts the current transaction.
520 * Need to zero out struct to set the interval and the micro seconds fields
524 MemSet(&timeval, 0, sizeof(struct timeval));
526 (DeadlockCheckTimer ? DeadlockCheckTimer : DEADLOCK_CHECK_TIMER);
532 MyProc->errType = NO_ERROR; /* reset flag after deadlock check */
534 if ((expire = select(0, NULL, NULL, NULL,
535 (deadlock_checked == false) ? &timeval : NULL)) == -1)
536 elog(FATAL, "ProcSleep: Unable to set timer for process wakeup");
538 if (expire == 0 /* timeout reached */ && deadlock_checked == false)
541 deadlock_checked = true;
545 * if someone wakes us between SpinRelease and IpcSemaphoreLock,
546 * IpcSemaphoreLock will not block. The wakeup is "saved" by
547 * the semaphore implementation.
550 IpcSemaphoreLock(MyProc->sem.semId, MyProc->sem.semNum,
552 } while (MyProc->errType == STATUS_NOT_FOUND); /* sleep after deadlock
556 * We were assumed to be in a critical section when we went
560 SpinAcquire(spinlock);
562 #ifdef LOCK_MGR_DEBUG
563 /* Just to get meaningful debug messages from DumpLocks() */
564 MyProc->waitLock = (LOCK *) NULL;
567 return MyProc->errType;
572 * ProcWakeup -- wake up a process by releasing its private semaphore.
574 * remove the process from the wait queue and set its links invalid.
575 * RETURN: the next process in the wait queue.
578 ProcWakeup(PROC *proc, int errType)
582 /* assume that spinlock has been acquired */
584 if (proc->links.prev == INVALID_OFFSET ||
585 proc->links.next == INVALID_OFFSET)
586 return (PROC *) NULL;
588 retProc = (PROC *) MAKE_PTR(proc->links.prev);
590 /* you have to update waitLock->waitProcs.size yourself */
591 SHMQueueDelete(&(proc->links));
592 SHMQueueElemInit(&(proc->links));
594 proc->errType = errType;
596 IpcSemaphoreUnlock(proc->sem.semId, proc->sem.semNum, IpcExclusiveLock);
602 * ProcLockWakeup -- routine for waking up processes when a lock is
606 ProcLockWakeup(PROC_QUEUE *queue, LOCKMETHOD lockmethod, LOCK *lock)
611 int last_locktype = -1;
612 int queue_size = queue->size;
614 Assert(queue->size >= 0);
617 return STATUS_NOT_FOUND;
619 proc = (PROC *) MAKE_PTR(queue->links.prev);
621 while ((queue_size--) && (proc))
625 * This proc will conflict as the previous one did, don't even
628 if (proc->token == last_locktype)
632 * This proc conflicts with locks held by others, ignored.
634 if (LockResolveConflicts(lockmethod,
638 (XIDLookupEnt *) NULL) != STATUS_OK)
640 last_locktype = proc->token;
645 * there was a waiting process, grant it the lock before waking it
646 * up. This will prevent another process from seizing the lock
647 * between the time we release the lock master (spinlock) and the
648 * time that the awoken process begins executing again.
650 GrantLock(lock, proc->token);
653 * ProcWakeup removes proc from the lock waiting process queue and
654 * returns the next proc in chain.
659 proc = ProcWakeup(proc, NO_ERROR);
662 Assert(queue->size >= 0);
668 /* Something is still blocking us. May have deadlocked. */
669 trace_flag = (lock->tag.lockmethod == USER_LOCKMETHOD) ? \
670 TRACE_USERLOCKS : TRACE_LOCKS;
672 "ProcLockWakeup: lock(%x) can't wake up any process",
674 #ifdef DEADLOCK_DEBUG
675 if (pg_options[trace_flag] >= 2)
678 return STATUS_NOT_FOUND;
683 ProcAddLock(SHM_QUEUE *elem)
685 SHMQueueInsertTL(&MyProc->lockQueue, elem);
688 /* --------------------
689 * We only get to this routine if we got SIGALRM after DEADLOCK_CHECK_TIMER
690 * while waiting for a lock to be released by some other process. If we have
691 * a real deadlock, we must also indicate that I'm no longer waiting
692 * on a lock so that other processes don't try to wake me up and screw
694 * --------------------
703 /* ---------------------
704 * Check to see if we've been awoken by anyone in the interim.
706 * If we have we can return and resume our transaction -- happy day.
707 * Before we are awoken the process releasing the lock grants it to
708 * us so we know that we don't have to wait anymore.
710 * Damn these names are LONG! -mer
711 * ---------------------
713 if (IpcSemaphoreGetCount(MyProc->sem.semId, MyProc->sem.semNum) ==
714 IpcSemaphoreDefaultStartValue)
721 * you would think this would be unnecessary, but...
723 * this also means we've been removed already. in some ports (e.g.,
724 * sparc and aix) the semop(2) implementation is such that we can
725 * actually end up in this handler after someone has removed us from
726 * the queue and bopped the semaphore *but the test above fails to
727 * detect the semaphore update* (presumably something weird having to
728 * do with the order in which the semaphore wakeup signal and SIGALRM
731 if (MyProc->links.prev == INVALID_OFFSET ||
732 MyProc->links.next == INVALID_OFFSET)
738 #ifdef DEADLOCK_DEBUG
742 if (!DeadLockCheck(&(MyProc->lockQueue), MyProc->waitLock, true))
745 MyProc->errType = STATUS_NOT_FOUND;
749 mywaitlock = MyProc->waitLock;
751 /* ------------------------
752 * Get this process off the lock's wait queue
753 * ------------------------
755 Assert(mywaitlock->waitProcs.size > 0);
756 --mywaitlock->waitProcs.size;
757 SHMQueueDelete(&(MyProc->links));
758 SHMQueueElemInit(&(MyProc->links));
760 /* ------------------
761 * Unlock my semaphore so that the count is right for next time.
762 * I was awoken by a signal, not by someone unlocking my semaphore.
765 IpcSemaphoreUnlock(MyProc->sem.semId, MyProc->sem.semNum,
769 * Set MyProc->errType to STATUS_ERROR so that we abort after
770 * returning from this handler.
773 MyProc->errType = STATUS_ERROR;
776 * if this doesn't follow the IpcSemaphoreUnlock then we get lock
777 * table corruption ("LockReplace: xid table corrupted") due to race
778 * conditions. i don't claim to understand this...
782 elog(NOTICE, "Deadlock detected -- See the lock(l) manual page for a possible cause.");
787 ProcReleaseSpins(PROC *proc)
796 for (i = 0; i < (int) MAX_SPINS; i++)
800 Assert(proc->sLocks[i] == 1);
806 /*****************************************************************************
808 *****************************************************************************/
811 * ProcGetNewSemKeyAndNum -
812 * scan the free semaphore bitmap and allocate a single semaphore from
813 * a semaphore set. (If the semaphore set doesn't exist yet,
814 * IpcSemaphoreCreate will create it. Otherwise, we use the existing
818 ProcGetNewSemKeyAndNum(IPCKey *key, int *semNum)
821 int32 *freeSemMap = ProcGlobal->freeSemMap;
822 unsigned int fullmask;
825 * we hold ProcStructLock when entering this routine. We scan through
826 * the bitmap to look for a free semaphore.
828 fullmask = ~0 >> (32 - PROC_NSEMS_PER_SET);
829 for (i = 0; i < MAX_PROC_SEMS / PROC_NSEMS_PER_SET; i++)
834 if (freeSemMap[i] == fullmask)
835 continue; /* none free for this set */
837 for (j = 0; j < PROC_NSEMS_PER_SET; j++)
839 if ((freeSemMap[i] & mask) == 0)
843 * a free semaphore found. Mark it as allocated.
845 freeSemMap[i] |= mask;
847 *key = ProcGlobal->currKey + i;
855 /* if we reach here, all the semaphores are in use. */
856 elog(ERROR, "InitProc: cannot allocate a free semaphore");
861 * free up our semaphore in the semaphore set. If we're the last one
862 * in the set, also remove the semaphore set.
865 ProcFreeSem(IpcSemaphoreKey semKey, int semNum)
869 int32 *freeSemMap = ProcGlobal->freeSemMap;
871 i = semKey - ProcGlobal->currKey;
872 mask = ~(1 << semNum);
873 freeSemMap[i] &= mask;
875 if (freeSemMap[i] == 0)
876 IpcSemaphoreKill(semKey);
880 * ProcFreeAllSemaphores -
881 * on exiting the postmaster, we free up all the semaphores allocated
882 * to the lmgrs of the backends.
885 ProcFreeAllSemaphores()
888 int32 *freeSemMap = ProcGlobal->freeSemMap;
890 for (i = 0; i < MAX_PROC_SEMS / PROC_NSEMS_PER_SET; i++)
892 if (freeSemMap[i] != 0)
893 IpcSemaphoreKill(ProcGlobal->currKey + i);