566021fe89698972843cae408dc2c5c0e0ce5ffe
[kopensolaris-gnu/glibc.git] / db2 / lock / lock_deadlock.c
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 1996, 1997
5  *      Sleepycat Software.  All rights reserved.
6  */
7
8 #include "config.h"
9
10 #ifndef lint
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.25 (Sleepycat) 11/1/97";
15 #endif
16
17 #ifndef NO_SYSTEM_INCLUDES
18 #include <sys/types.h>
19
20 #include <errno.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #endif
24
25 #include "db_int.h"
26 #include "shqueue.h"
27 #include "db_shash.h"
28 #include "lock.h"
29 #include "common_ext.h"
30
31 #define ISSET_MAP(M, N) (M[(N) / 32] & (1 << (N) % 32))
32
33 #define CLEAR_MAP(M, N) {                                               \
34         u_int32_t __i;                                                  \
35         for (__i = 0; __i < (N); __i++)                                 \
36                 M[__i] = 0;                                             \
37 }
38
39 #define SET_MAP(M, B)   (M[(B) / 32] |= (1 << ((B) % 32)))
40 #define CLR_MAP(M, B)   (M[(B) / 32] &= ~(1 << ((B) % 32)))
41
42 #define OR_MAP(D, S, N) {                                               \
43         u_int32_t __i;                                                  \
44         for (__i = 0; __i < (N); __i++)                                 \
45                 D[__i] |= S[__i];                                       \
46 }
47 #define BAD_KILLID      0xffffffff
48
49 typedef struct {
50         int             valid;
51         u_int32_t       id;
52         DB_LOCK         last_lock;
53         db_pgno_t       pgno;
54 } locker_info;
55
56 static int  __dd_abort __P((DB_ENV *, locker_info *));
57 static int  __dd_build
58         __P((DB_ENV *, u_int32_t **, u_int32_t *, locker_info **));
59 static u_int32_t
60            *__dd_find __P((u_int32_t *, locker_info *, u_int32_t));
61
62 #ifdef DEBUG
63 static void __dd_debug __P((DB_ENV *, locker_info *, u_int32_t *, u_int32_t));
64 #endif
65
66 int
67 lock_detect(lt, flags, atype)
68         DB_LOCKTAB *lt;
69         int flags;
70         u_int32_t atype;
71 {
72         DB_ENV *dbenv;
73         locker_info *idmap;
74         u_int32_t *bitmap, *deadlock, i, killid, nentries, nlockers;
75         int do_pass, ret;
76
77         /* Validate arguments. */
78         if ((ret =
79             __db_fchk(lt->dbenv, "lock_detect", flags, DB_LOCK_CONFLICT)) != 0)
80                 return (ret);
81
82         /* Check if a detector run is necessary. */
83         dbenv = lt->dbenv;
84         if (LF_ISSET(DB_LOCK_CONFLICT)) {
85                 /* Make a pass every time a lock waits. */
86                 LOCK_LOCKREGION(lt);
87                 do_pass = dbenv->lk_info->region->need_dd != 0;
88                 UNLOCK_LOCKREGION(lt);
89
90                 if (!do_pass)
91                         return (0);
92         }
93
94         /* Build the waits-for bitmap. */
95         if ((ret = __dd_build(dbenv, &bitmap, &nlockers, &idmap)) != 0)
96                 return (ret);
97
98         if (nlockers == 0)
99                 return (0);
100 #ifdef DEBUG
101         if (dbenv->db_verbose != 0)
102                 __dd_debug(dbenv, idmap, bitmap, nlockers);
103 #endif
104         /* Find a deadlock. */
105         deadlock = __dd_find(bitmap, idmap, nlockers);
106         nentries = ALIGN(nlockers, 32) / 32;
107         killid = BAD_KILLID;
108         if (deadlock != NULL) {
109                 /* Kill someone. */
110                 switch (atype) {
111                 case DB_LOCK_OLDEST:
112                         /*
113                          * Find the first bit set in the current
114                          * array and then look for a lower tid in
115                          * the array.
116                          */
117                         for (i = 0; i < nlockers; i++)
118                                 if (ISSET_MAP(deadlock, i))
119                                         killid = i;
120
121                         if (killid == BAD_KILLID) {
122                                 __db_err(dbenv,
123                                     "warning: could not find locker to abort");
124                                 break;
125                         }
126
127                         /*
128                          * The oldest transaction has the lowest
129                          * transaction id.
130                          */
131                         for (i = killid + 1; i < nlockers; i++)
132                                 if (ISSET_MAP(deadlock, i) &&
133                                     idmap[i].id < idmap[killid].id)
134                                         killid = i;
135                         break;
136                 case DB_LOCK_DEFAULT:
137                 case DB_LOCK_RANDOM:
138                         /*
139                          * We are trying to calculate the id of the
140                          * locker whose entry is indicated by deadlock.
141                          */
142                         killid = (deadlock - bitmap) / nentries;
143                         break;
144                 case DB_LOCK_YOUNGEST:
145                         /*
146                          * Find the first bit set in the current
147                          * array and then look for a lower tid in
148                          * the array.
149                          */
150                         for (i = 0; i < nlockers; i++)
151                                 if (ISSET_MAP(deadlock, i))
152                                         killid = i;
153
154                         if (killid == BAD_KILLID) {
155                                 __db_err(dbenv,
156                                     "warning: could not find locker to abort");
157                                 break;
158                         }
159                         /*
160                          * The youngest transaction has the highest
161                          * transaction id.
162                          */
163                         for (i = killid + 1; i < nlockers; i++)
164                                 if (ISSET_MAP(deadlock, i) &&
165                                     idmap[i].id > idmap[killid].id)
166                                         killid = i;
167                         break;
168                 default:
169                         killid = BAD_KILLID;
170                         ret = EINVAL;
171                 }
172
173                 /* Kill the locker with lockid idmap[killid]. */
174                 if (dbenv->db_verbose != 0 && killid != BAD_KILLID)
175                         __db_err(dbenv, "Aborting locker %lx",
176                             (u_long)idmap[killid].id);
177
178                 if (killid != BAD_KILLID &&
179                     (ret = __dd_abort(dbenv, &idmap[killid])) != 0)
180                         __db_err(dbenv,
181                             "warning: unable to abort locker %lx",
182                             (u_long)idmap[killid].id);
183         }
184         __db_free(bitmap);
185         __db_free(idmap);
186
187         return (ret);
188 }
189
190 /*
191  * ========================================================================
192  * Utilities
193  */
194 static int
195 __dd_build(dbenv, bmp, nlockers, idmap)
196         DB_ENV *dbenv;
197         u_int32_t **bmp, *nlockers;
198         locker_info **idmap;
199 {
200         struct __db_lock *lp;
201         DB_LOCKTAB *lt;
202         DB_LOCKOBJ *op, *lo, *lockerp;
203         u_int8_t *pptr;
204         locker_info *id_array;
205         u_int32_t *bitmap, count, *entryp, i, id, nentries, *tmpmap;
206         int is_first, ret;
207
208         lt = dbenv->lk_info;
209
210         /*
211          * We'll check how many lockers there are, add a few more in for
212          * good measure and then allocate all the structures.  Then we'll
213          * verify that we have enough room when we go back in and get the
214          * mutex the second time.
215          */
216         LOCK_LOCKREGION(lt);
217 retry:  count = lt->region->nlockers;
218         lt->region->need_dd = 0;
219         UNLOCK_LOCKREGION(lt);
220
221         if (count == 0) {
222                 *nlockers = 0;
223                 return (0);
224         }
225
226         if (dbenv->db_verbose)
227                 __db_err(dbenv, "%lu lockers", (u_long)count);
228
229         count += 10;
230         nentries = ALIGN(count, 32) / 32;
231         /*
232          * Allocate enough space for a count by count bitmap matrix.
233          *
234          * XXX
235          * We can probably save the malloc's between iterations just
236          * reallocing if necessary because count grew by too much.
237          */
238         if ((bitmap = (u_int32_t *)__db_calloc((size_t)count,
239             sizeof(u_int32_t) * nentries)) == NULL) {
240                 __db_err(dbenv, "%s", strerror(ENOMEM));
241                 return (ENOMEM);
242         }
243
244         if ((tmpmap =
245             (u_int32_t *)__db_calloc(sizeof(u_int32_t), nentries)) == NULL) {
246                 __db_err(dbenv, "%s", strerror(ENOMEM));
247                 __db_free(bitmap);
248                 return (ENOMEM);
249         }
250
251         if ((id_array = (locker_info *)__db_calloc((size_t)count,
252             sizeof(locker_info))) == NULL) {
253                 __db_err(dbenv, "%s", strerror(ENOMEM));
254                 __db_free(bitmap);
255                 __db_free(tmpmap);
256                 return (ENOMEM);
257         }
258
259         /*
260          * Now go back in and actually fill in the matrix.
261          */
262         LOCK_LOCKREGION(lt);
263         if (lt->region->nlockers > count) {
264                 __db_free(bitmap);
265                 __db_free(tmpmap);
266                 __db_free(id_array);
267                 goto retry;
268         }
269
270         /*
271          * First we go through and assign each locker a deadlock detector id.
272          * Note that we fill in the idmap in the next loop since that's the
273          * only place where we conveniently have both the deadlock id and the
274          * actual locker.
275          */
276         for (id = 0, i = 0; i < lt->region->table_size; i++)
277                 for (op = SH_TAILQ_FIRST(&lt->hashtab[i], __db_lockobj);
278                     op != NULL; op = SH_TAILQ_NEXT(op, links, __db_lockobj))
279                         if (op->type == DB_LOCK_LOCKER)
280                                 op->dd_id = id++;
281         /*
282          * We go through the hash table and find each object.  For each object,
283          * we traverse the waiters list and add an entry in the waitsfor matrix
284          * for each waiter/holder combination.
285          */
286         for (i = 0; i < lt->region->table_size; i++) {
287                 for (op = SH_TAILQ_FIRST(&lt->hashtab[i], __db_lockobj);
288                     op != NULL; op = SH_TAILQ_NEXT(op, links, __db_lockobj)) {
289                         if (op->type != DB_LOCK_OBJTYPE)
290                                 continue;
291                         CLEAR_MAP(tmpmap, nentries);
292
293                         /*
294                          * First we go through and create a bit map that
295                          * represents all the holders of this object.
296                          */
297                         for (lp = SH_TAILQ_FIRST(&op->holders, __db_lock);
298                             lp != NULL;
299                             lp = SH_TAILQ_NEXT(lp, links, __db_lock)) {
300                                 if (__lock_getobj(lt, lp->holder,
301                                     NULL, DB_LOCK_LOCKER, &lockerp) != 0) {
302                                         __db_err(dbenv,
303                                             "warning unable to find object");
304                                         continue;
305                                 }
306                                 id_array[lockerp->dd_id].id = lp->holder;
307                                 id_array[lockerp->dd_id].valid = 1;
308
309                                 /*
310                                  * If the holder has already been aborted, then
311                                  * we should ignore it for now.
312                                  */
313                                 if (lp->status == DB_LSTAT_HELD)
314                                         SET_MAP(tmpmap, lockerp->dd_id);
315                         }
316
317                         /*
318                          * Next, for each waiter, we set its row in the matrix
319                          * equal to the map of holders we set up above.
320                          */
321                         for (is_first = 1,
322                             lp = SH_TAILQ_FIRST(&op->waiters, __db_lock);
323                             lp != NULL;
324                             is_first = 0,
325                             lp = SH_TAILQ_NEXT(lp, links, __db_lock)) {
326                                 if ((ret = __lock_getobj(lt, lp->holder,
327                                     NULL, DB_LOCK_LOCKER, &lockerp)) != 0) {
328                                         __db_err(dbenv,
329                                             "warning unable to find object");
330                                         continue;
331                                 }
332                                 id_array[lockerp->dd_id].id = lp->holder;
333                                 id_array[lockerp->dd_id].valid = 1;
334
335                                 /*
336                                  * If the transaction is pending abortion, then
337                                  * ignore it on this iteration.
338                                  */
339                                 if (lp->status != DB_LSTAT_WAITING)
340                                         continue;
341
342                                 entryp = bitmap + (nentries * lockerp->dd_id);
343                                 OR_MAP(entryp, tmpmap, nentries);
344                                 /*
345                                  * If this is the first waiter on the queue,
346                                  * then we remove the waitsfor relationship
347                                  * with oneself.  However, if it's anywhere
348                                  * else on the queue, then we have to keep
349                                  * it and we have an automatic deadlock.
350                                  */
351                                 if (is_first)
352                                         CLR_MAP(entryp, lockerp->dd_id);
353                         }
354                 }
355         }
356
357         /* Now for each locker; record its last lock. */
358         for (id = 0; id < count; id++) {
359                 if (!id_array[id].valid)
360                         continue;
361                 if ((ret = __lock_getobj(lt,
362                     id_array[id].id, NULL, DB_LOCK_LOCKER, &lockerp)) != 0) {
363                         __db_err(dbenv,
364                             "No locks for locker %lu", (u_long)id_array[id].id);
365                         continue;
366                 }
367                 lp = SH_LIST_FIRST(&lockerp->heldby, __db_lock);
368                 if (lp != NULL) {
369                         id_array[id].last_lock = LOCK_TO_OFFSET(lt, lp);
370                         lo = (DB_LOCKOBJ *)((u_int8_t *)lp + lp->obj);
371                         pptr = SH_DBT_PTR(&lo->lockobj);
372                         if (lo->lockobj.size >= sizeof(db_pgno_t))
373                                 memcpy(&id_array[id].pgno, pptr,
374                                     sizeof(db_pgno_t));
375                         else
376                                 id_array[id].pgno = 0;
377                 }
378         }
379
380         /* Pass complete, reset the deadlock detector bit. */
381         lt->region->need_dd = 0;
382         UNLOCK_LOCKREGION(lt);
383
384         /*
385          * Now we can release everything except the bitmap matrix that we
386          * created.
387          */
388         *nlockers = id;
389         *idmap = id_array;
390         *bmp = bitmap;
391         __db_free(tmpmap);
392         return (0);
393 }
394
395 static u_int32_t *
396 __dd_find(bmp, idmap, nlockers)
397         u_int32_t *bmp, nlockers;
398         locker_info *idmap;
399 {
400         u_int32_t i, j, nentries, *mymap, *tmpmap;
401
402         /*
403          * For each locker, OR in the bits from the lockers on which that
404          * locker is waiting.
405          */
406         nentries = ALIGN(nlockers, 32) / 32;
407         for (mymap = bmp, i = 0; i < nlockers; i++, mymap += nentries) {
408                 if (!idmap[i].valid)
409                         continue;
410                 for (j = 0; j < nlockers; j++) {
411                         if (ISSET_MAP(mymap, j)) {
412                                 /* Find the map for this bit. */
413                                 tmpmap = bmp + (nentries * j);
414                                 OR_MAP(mymap, tmpmap, nentries);
415                                 if (ISSET_MAP(mymap, i))
416                                         return (mymap);
417                         }
418                 }
419         }
420         return (NULL);
421 }
422
423 static int
424 __dd_abort(dbenv, info)
425         DB_ENV *dbenv;
426         locker_info *info;
427 {
428         struct __db_lock *lockp;
429         DB_LOCKTAB *lt;
430         DB_LOCKOBJ *lockerp, *sh_obj;
431         int ret;
432
433         lt = dbenv->lk_info;
434         LOCK_LOCKREGION(lt);
435
436         /* Find the locker's last lock. */
437         if ((ret =
438             __lock_getobj(lt, info->id, NULL, DB_LOCK_LOCKER, &lockerp)) != 0)
439                 goto out;
440
441         lockp = SH_LIST_FIRST(&lockerp->heldby, __db_lock);
442         if (LOCK_TO_OFFSET(lt, lockp) != info->last_lock ||
443             lockp == NULL || lockp->status != DB_LSTAT_WAITING)
444                 goto out;
445
446         /* Abort lock, take it off list, and wake up this lock. */
447         lockp->status = DB_LSTAT_ABORTED;
448         lt->region->ndeadlocks++;
449         SH_LIST_REMOVE(lockp, locker_links, __db_lock);
450         sh_obj = (DB_LOCKOBJ *)((u_int8_t *)lockp + lockp->obj);
451         SH_TAILQ_REMOVE(&sh_obj->waiters, lockp, links, __db_lock);
452         (void)__db_mutex_unlock(&lockp->mutex, lt->fd);
453
454         ret = 0;
455
456 out:    UNLOCK_LOCKREGION(lt);
457         return (ret);
458 }
459
460 #ifdef DEBUG
461 static void
462 __dd_debug(dbenv, idmap, bitmap, nlockers)
463         DB_ENV *dbenv;
464         locker_info *idmap;
465         u_int32_t *bitmap, nlockers;
466 {
467         u_int32_t i, j, *mymap, nentries;
468         char *msgbuf;
469
470         __db_err(dbenv, "Waitsfor array");
471         __db_err(dbenv, "waiter\twaiting on");
472         /*
473          * Allocate space to print 10 bytes per item waited on.
474          */
475         if ((msgbuf = (char *)__db_malloc((nlockers + 1) * 10 + 64)) == NULL) {
476                 __db_err(dbenv, "%s", strerror(ENOMEM));
477                 return;
478         }
479
480         nentries = ALIGN(nlockers, 32) / 32;
481         for (mymap = bitmap, i = 0; i < nlockers; i++, mymap += nentries) {
482                 if (!idmap[i].valid)
483                         continue;
484                 sprintf(msgbuf,                                 /* Waiter. */
485                     "%lx/%lu:\t", (u_long)idmap[i].id, (u_long)idmap[i].pgno);
486                 for (j = 0; j < nlockers; j++)
487                         if (ISSET_MAP(mymap, j))
488                                 sprintf(msgbuf, "%s %lx", msgbuf,
489                                     (u_long)idmap[j].id);
490                 (void)sprintf(msgbuf,
491                     "%s %lu", msgbuf, (u_long)idmap[i].last_lock);
492                 __db_err(dbenv, msgbuf);
493         }
494
495         __db_free(msgbuf);
496 }
497 #endif