2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
11 static const char copyright[] =
12 "@(#) Copyright (c) 1997\n\
13 Sleepycat Software Inc. All rights reserved.\n";
14 static const char sccsid[] = "@(#)lock_deadlock.c 10.20 (Sleepycat) 8/21/97";
17 #ifndef NO_SYSTEM_INCLUDES
18 #include <sys/types.h>
29 #include "common_ext.h"
31 #define ISSET_MAP(M, N) (M[(N) / 32] & (1 << (N) % 32))
33 #define CLEAR_MAP(M, N) { \
35 for (__i = 0; __i < (N); __i++) \
39 #define SET_MAP(M, B) (M[(B) / 32] |= (1 << ((B) % 32)))
40 #define CLR_MAP(M, B) (M[(B) / 32] &= ~(1 << ((B) % 32)))
42 #define OR_MAP(D, S, N) { \
44 for (__i = 0; __i < (N); __i++) \
47 #define BAD_KILLID 0xffffffff
55 static int __dd_abort __P((DB_ENV *, locker_info *));
56 static int __dd_build __P((DB_ENV *, u_int32_t **, int *, locker_info **));
58 static void __dd_debug __P((DB_ENV *, locker_info *, u_int32_t *, int));
61 *__dd_find __P((u_int32_t *, locker_info *, u_int32_t));
64 lock_detect(lt, flags, atype)
71 u_int32_t *bitmap, *deadlock, killid;
72 int do_pass, i, nlockers, nentries, ret;
74 /* Validate arguments. */
76 __db_fchk(lt->dbenv, "lock_detect", flags, DB_LOCK_CONFLICT)) != 0)
79 /* Check if a detector run is necessary. */
82 if (LF_ISSET(DB_LOCK_CONFLICT)) {
83 /* Make a pass every time a lock waits. */
85 do_pass = dbenv->lk_info->region->need_dd != 0;
86 UNLOCK_LOCKREGION(lt);
92 /* Build the waits-for bitmap. */
93 if ((ret = __dd_build(dbenv, &bitmap, &nlockers, &idmap)) != 0)
99 if (dbenv->db_verbose != 0)
100 __dd_debug(dbenv, idmap, bitmap, nlockers);
102 /* Find a deadlock. */
103 deadlock = __dd_find(bitmap, idmap, nlockers);
104 nentries = ALIGN(nlockers, 32) / 32;
106 if (deadlock != NULL) {
111 * Find the first bit set in the current
112 * array and then look for a lower tid in
115 for (i = 0; i < nlockers; i++)
116 if (ISSET_MAP(deadlock, i))
119 if (killid == BAD_KILLID) {
121 "warning: could not find %s",
127 * The oldest transaction has the lowest
130 for (i = killid + 1; i < nlockers; i++)
131 if (ISSET_MAP(deadlock, i) &&
132 idmap[i].id < idmap[killid].id)
135 case DB_LOCK_DEFAULT:
138 * We are trying to calculate the id of the
139 * locker whose entry is indicated by deadlock.
140 * We know that this is less than nlockers, so
141 * the cast below is valid.
144 (u_int32_t)((deadlock - bitmap) / nentries);
146 case DB_LOCK_YOUNGEST:
148 * Find the first bit set in the current
149 * array and then look for a lower tid in
152 for (i = 0; i < nlockers; i++)
153 if (ISSET_MAP(deadlock, i))
156 if (killid == BAD_KILLID) {
158 "warning: could not find %s",
163 * The youngest transaction has the highest
166 for (i = killid + 1; i < nlockers; i++)
167 if (ISSET_MAP(deadlock, i) &&
168 idmap[i].id > idmap[killid].id)
176 /* Kill the locker with lockid idmap[killid]. */
177 if (dbenv->db_verbose != 0 && killid != BAD_KILLID)
178 __db_err(dbenv, "Aborting locker %lx",
179 (u_long)idmap[killid].id);
181 if (killid != BAD_KILLID &&
182 (ret = __dd_abort(dbenv, &idmap[killid])) != 0)
184 "warning: unable to abort locker %lx",
185 (u_long)idmap[killid].id);
194 * ========================================================================
198 __dd_build(dbenv, bmp, nlockers, idmap)
205 DB_LOCKOBJ *op, *lockerp;
206 struct __db_lock *lp;
207 u_int32_t *bitmap, count, *entryp, i, id, nentries, *tmpmap;
208 locker_info *id_array;
214 * We'll check how many lockers there are, add a few more in for
215 * good measure and then allocate all the structures. Then we'll
216 * verify that we have enough room when we go back in and get the
217 * mutex the second time.
220 retry: count = lt->region->nlockers;
221 lt->region->need_dd = 0;
222 UNLOCK_LOCKREGION(lt);
229 if (dbenv->db_verbose)
230 __db_err(dbenv, "%lu lockers", (u_long)count);
233 nentries = ALIGN(count, 32) / 32;
235 * Allocate enough space for a count by count bitmap matrix.
238 * We can probably save the malloc's between iterations just
239 * reallocing if necessary because count grew by too much.
241 if ((bitmap = (u_int32_t *)calloc((size_t)count,
242 sizeof(u_int32_t) * nentries)) == NULL) {
243 __db_err(dbenv, "%s", strerror(ENOMEM));
248 (u_int32_t *)calloc(sizeof(u_int32_t), nentries)) == NULL) {
249 __db_err(dbenv, "%s", strerror(ENOMEM));
254 if ((id_array = (locker_info *)calloc((size_t)count,
255 sizeof(locker_info))) == NULL) {
256 __db_err(dbenv, "%s", strerror(ENOMEM));
263 * Now go back in and actually fill in the matrix.
266 if (lt->region->nlockers > count) {
274 * First we go through and assign each locker a deadlock detector id.
275 * Note that we fill in the idmap in the next loop since that's the
276 * only place where we conveniently have both the deadlock id and the
279 for (id = 0, i = 0; i < lt->region->table_size; i++)
280 for (op = SH_TAILQ_FIRST(<->hashtab[i], __db_lockobj);
281 op != NULL; op = SH_TAILQ_NEXT(op, links, __db_lockobj))
282 if (op->type == DB_LOCK_LOCKER)
285 * We go through the hash table and find each object. For each object,
286 * we traverse the waiters list and add an entry in the waitsfor matrix
287 * for each waiter/holder combination.
289 for (i = 0; i < lt->region->table_size; i++) {
290 for (op = SH_TAILQ_FIRST(<->hashtab[i], __db_lockobj);
291 op != NULL; op = SH_TAILQ_NEXT(op, links, __db_lockobj)) {
292 if (op->type != DB_LOCK_OBJTYPE)
294 CLEAR_MAP(tmpmap, nentries);
297 * First we go through and create a bit map that
298 * represents all the holders of this object.
300 for (lp = SH_TAILQ_FIRST(&op->holders, __db_lock);
302 lp = SH_TAILQ_NEXT(lp, links, __db_lock)) {
303 if ((__set_errno(__lock_getobj(lt, lp->holder,
304 NULL, DB_LOCK_LOCKER, &lockerp))) != 0) {
306 "warning unable to find object");
309 id_array[lockerp->dd_id].id = lp->holder;
310 id_array[lockerp->dd_id].valid = 1;
313 * If the holder has already been aborted, then
314 * we should ignore it for now.
316 if (lp->status == DB_LSTAT_HELD)
317 SET_MAP(tmpmap, lockerp->dd_id);
321 * Next, for each waiter, we set its row in the matrix
322 * equal to the map of holders we set up above.
325 lp = SH_TAILQ_FIRST(&op->waiters, __db_lock);
328 lp = SH_TAILQ_NEXT(lp, links, __db_lock)) {
329 if ((ret = __lock_getobj(lt,
330 lp->holder, NULL, DB_LOCK_LOCKER, &lockerp))
333 "warning unable to find object");
336 id_array[lockerp->dd_id].id = lp->holder;
337 id_array[lockerp->dd_id].valid = 1;
340 * If the transaction is pending abortion, then
341 * ignore it on this iteration.
343 if (lp->status != DB_LSTAT_WAITING)
346 entryp = bitmap + (nentries * lockerp->dd_id);
347 OR_MAP(entryp, tmpmap, nentries);
349 * If this is the first waiter on the queue,
350 * then we remove the waitsfor relationship
351 * with oneself. However, if it's anywhere
352 * else on the queue, then we have to keep
353 * it and we have an automatic deadlock.
356 CLR_MAP(entryp, lockerp->dd_id);
361 /* Now for each locker; record its last lock. */
362 for (id = 0; id < count; id++) {
363 if (!id_array[id].valid)
365 if ((ret = __lock_getobj(lt,
366 id_array[id].id, NULL, DB_LOCK_LOCKER, &lockerp)) != 0) {
368 "No locks for locker %lu", (u_long)id_array[id].id);
371 lp = SH_LIST_FIRST(&lockerp->heldby, __db_lock);
373 id_array[id].last_lock = LOCK_TO_OFFSET(lt, lp);
376 /* Pass complete, reset the deadlock detector bit. */
377 lt->region->need_dd = 0;
378 UNLOCK_LOCKREGION(lt);
381 * Now we can release everything except the bitmap matrix that we
392 __dd_find(bmp, idmap, nlockers)
397 u_int32_t i, j, nentries, *mymap, *tmpmap;
400 * For each locker, or in the bits from the lockers
401 * on which that locker is waiting.
403 nentries = ALIGN(nlockers, 32) / 32;
404 for (mymap = bmp, i = 0; i < nlockers; i++, mymap += nentries) {
407 for (j = 0; j < nlockers; j++) {
408 if (ISSET_MAP(mymap, j)) {
409 /* Find the map for this bit. */
410 tmpmap = bmp + (nentries * j);
411 OR_MAP(mymap, tmpmap, nentries);
412 if (ISSET_MAP(mymap, i))
421 __dd_abort(dbenv, info)
426 DB_LOCKOBJ *lockerp, *sh_obj;
427 struct __db_lock *lockp;
433 /* Find the locker's last lock. */
435 __lock_getobj(lt, info->id, NULL, DB_LOCK_LOCKER, &lockerp)) != 0)
438 lockp = SH_LIST_FIRST(&lockerp->heldby, __db_lock);
439 if (LOCK_TO_OFFSET(lt, lockp) != info->last_lock ||
440 lockp == NULL || lockp->status != DB_LSTAT_WAITING)
443 /* Abort lock, take it off list, and wake up this lock. */
444 lockp->status = DB_LSTAT_ABORTED;
445 lt->region->ndeadlocks++;
446 SH_LIST_REMOVE(lockp, locker_links, __db_lock);
447 sh_obj = (DB_LOCKOBJ *)((u_int8_t *)lockp + lockp->obj);
448 SH_TAILQ_REMOVE(&sh_obj->waiters, lockp, links, __db_lock);
449 (void)__db_mutex_unlock(&lockp->mutex, lt->fd);
453 out: UNLOCK_LOCKREGION(lt);
459 __dd_debug(dbenv, idmap, bitmap, nlockers)
469 __db_err(dbenv, "Waitsfor array");
470 __db_err(dbenv, "waiter\twaiting on");
472 * Alloc space to print 10 bytes per item waited on.
474 if ((msgbuf = (char *)malloc((nlockers + 1) * 10 + 64)) == NULL) {
476 __db_err(dbenv, "%s", strerror(errno));
480 nentries = ALIGN(nlockers, 32) / 32;
481 for (mymap = bitmap, i = 0; i < nlockers; i++, mymap += nentries) {
484 sprintf(msgbuf, "%lx\t\t", (u_long)idmap[i].id);/* Waiter. */
485 for (j = 0; j < nlockers; j++)
486 if (ISSET_MAP(mymap, j))
487 sprintf(msgbuf, "%s %lx", msgbuf,
488 (u_long)idmap[j].id);
489 (void)sprintf(msgbuf,
490 "%s %lu", msgbuf, (u_long)idmap[i].last_lock);
491 __db_err(dbenv, msgbuf);