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