Add db 2.3.4 sources with minor changes.
[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.20 (Sleepycat) 8/21/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 } locker_info;
54
55 static int  __dd_abort __P((DB_ENV *, locker_info *));
56 static int  __dd_build __P((DB_ENV *, u_int32_t **, int *, locker_info **));
57 #ifdef DEBUG
58 static void __dd_debug __P((DB_ENV *, locker_info *, u_int32_t *, int));
59 #endif
60 static u_int32_t
61            *__dd_find __P((u_int32_t *, locker_info *, u_int32_t));
62
63 int
64 lock_detect(lt, flags, atype)
65         DB_LOCKTAB *lt;
66         int flags;
67         u_int32_t atype;
68 {
69         DB_ENV *dbenv;
70         locker_info *idmap;
71         u_int32_t *bitmap, *deadlock, killid;
72         int do_pass, i, nlockers, nentries, ret;
73
74         /* Validate arguments. */
75         if ((ret =
76             __db_fchk(lt->dbenv, "lock_detect", flags, DB_LOCK_CONFLICT)) != 0)
77                 return (ret);
78
79         /* Check if a detector run is necessary. */
80         do_pass = 1;
81         dbenv = lt->dbenv;
82         if (LF_ISSET(DB_LOCK_CONFLICT)) {
83                 /* Make a pass every time a lock waits. */
84                 LOCK_LOCKREGION(lt);
85                 do_pass = dbenv->lk_info->region->need_dd != 0;
86                 UNLOCK_LOCKREGION(lt);
87         }
88
89         if (!do_pass)
90                 return (0);
91
92         /* Build the waits-for bitmap. */
93         if ((ret = __dd_build(dbenv, &bitmap, &nlockers, &idmap)) != 0)
94                 return (ret);
95
96         if (nlockers == 0)
97                 return (0);
98 #ifdef DEBUG
99         if (dbenv->db_verbose != 0)
100                 __dd_debug(dbenv, idmap, bitmap, nlockers);
101 #endif
102         /* Find a deadlock. */
103         deadlock = __dd_find(bitmap, idmap, nlockers);
104         nentries = ALIGN(nlockers, 32) / 32;
105         killid = BAD_KILLID;
106         if (deadlock != NULL) {
107                 /* Kill someone. */
108                 switch (atype) {
109                 case DB_LOCK_OLDEST:
110                         /*
111                          * Find the first bit set in the current
112                          * array and then look for a lower tid in
113                          * the array.
114                          */
115                         for (i = 0; i < nlockers; i++)
116                                 if (ISSET_MAP(deadlock, i))
117                                         killid = i;
118
119                         if (killid == BAD_KILLID) {
120                                 __db_err(dbenv,
121                                     "warning: could not find %s",
122                                     "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                          * We know that this is less than nlockers, so
141                          * the cast below is valid.
142                          */
143                         killid =
144                             (u_int32_t)((deadlock - bitmap) / nentries);
145                         break;
146                 case DB_LOCK_YOUNGEST:
147                         /*
148                          * Find the first bit set in the current
149                          * array and then look for a lower tid in
150                          * the array.
151                          */
152                         for (i = 0; i < nlockers; i++)
153                                 if (ISSET_MAP(deadlock, i))
154                                         killid = i;
155
156                         if (killid == BAD_KILLID) {
157                                 __db_err(dbenv,
158                                     "warning: could not find %s",
159                                     "locker to abort");
160                                 break;
161                         }
162                         /*
163                          * The youngest transaction has the highest
164                          * transaction id.
165                          */
166                         for (i = killid + 1; i < nlockers; i++)
167                                 if (ISSET_MAP(deadlock, i) &&
168                                     idmap[i].id > idmap[killid].id)
169                                         killid = i;
170                         break;
171                 default:
172                         killid = BAD_KILLID;
173                         ret = EINVAL;
174                 }
175
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);
180
181                 if (killid != BAD_KILLID &&
182                     (ret = __dd_abort(dbenv, &idmap[killid])) != 0)
183                         __db_err(dbenv,
184                             "warning: unable to abort locker %lx",
185                             (u_long)idmap[killid].id);
186         }
187         free(bitmap);
188         free(idmap);
189
190         return (ret);
191 }
192
193 /*
194  * ========================================================================
195  * Utilities
196  */
197 static int
198 __dd_build(dbenv, bmp, nlockers, idmap)
199         DB_ENV *dbenv;
200         u_int32_t **bmp;
201         int *nlockers;
202         locker_info **idmap;
203 {
204         DB_LOCKTAB *lt;
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;
209         int is_first, ret;
210
211         lt = dbenv->lk_info;
212
213         /*
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.
218          */
219         LOCK_LOCKREGION(lt);
220 retry:  count = lt->region->nlockers;
221         lt->region->need_dd = 0;
222         UNLOCK_LOCKREGION(lt);
223
224         if (count == 0) {
225                 *nlockers = 0;
226                 return (0);
227         }
228
229         if (dbenv->db_verbose)
230                 __db_err(dbenv, "%lu lockers", (u_long)count);
231
232         count += 10;
233         nentries = ALIGN(count, 32) / 32;
234         /*
235          * Allocate enough space for a count by count bitmap matrix.
236          *
237          * XXX
238          * We can probably save the malloc's between iterations just
239          * reallocing if necessary because count grew by too much.
240          */
241         if ((bitmap = (u_int32_t *)calloc((size_t)count,
242             sizeof(u_int32_t) * nentries)) == NULL) {
243                 __db_err(dbenv, "%s", strerror(ENOMEM));
244                 return (ENOMEM);
245         }
246
247         if ((tmpmap =
248             (u_int32_t *)calloc(sizeof(u_int32_t), nentries)) == NULL) {
249                 __db_err(dbenv, "%s", strerror(ENOMEM));
250                 free(bitmap);
251                 return (ENOMEM);
252         }
253
254         if ((id_array = (locker_info *)calloc((size_t)count,
255             sizeof(locker_info))) == NULL) {
256                 __db_err(dbenv, "%s", strerror(ENOMEM));
257                 free(bitmap);
258                 free(tmpmap);
259                 return (ENOMEM);
260         }
261
262         /*
263          * Now go back in and actually fill in the matrix.
264          */
265         LOCK_LOCKREGION(lt);
266         if (lt->region->nlockers > count) {
267                 free(bitmap);
268                 free(tmpmap);
269                 free(id_array);
270                 goto retry;
271         }
272
273         /*
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
277          * actual locker.
278          */
279         for (id = 0, i = 0; i < lt->region->table_size; i++)
280                 for (op = SH_TAILQ_FIRST(&lt->hashtab[i], __db_lockobj);
281                     op != NULL; op = SH_TAILQ_NEXT(op, links, __db_lockobj))
282                         if (op->type == DB_LOCK_LOCKER)
283                                 op->dd_id = id++;
284         /*
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.
288          */
289         for (i = 0; i < lt->region->table_size; i++) {
290                 for (op = SH_TAILQ_FIRST(&lt->hashtab[i], __db_lockobj);
291                     op != NULL; op = SH_TAILQ_NEXT(op, links, __db_lockobj)) {
292                         if (op->type != DB_LOCK_OBJTYPE)
293                                 continue;
294                         CLEAR_MAP(tmpmap, nentries);
295
296                         /*
297                          * First we go through and create a bit map that
298                          * represents all the holders of this object.
299                          */
300                         for (lp = SH_TAILQ_FIRST(&op->holders, __db_lock);
301                             lp != NULL;
302                             lp = SH_TAILQ_NEXT(lp, links, __db_lock)) {
303                                 if ((errno = __lock_getobj(lt, lp->holder,
304                                     NULL, DB_LOCK_LOCKER, &lockerp)) != 0) {
305                                         __db_err(dbenv,
306                                             "warning unable to find object");
307                                         continue;
308                                 }
309                                 id_array[lockerp->dd_id].id = lp->holder;
310                                 id_array[lockerp->dd_id].valid = 1;
311
312                                 /*
313                                  * If the holder has already been aborted, then
314                                  * we should ignore it for now.
315                                  */
316                                 if (lp->status == DB_LSTAT_HELD)
317                                         SET_MAP(tmpmap, lockerp->dd_id);
318                         }
319
320                         /*
321                          * Next, for each waiter, we set its row in the matrix
322                          * equal to the map of holders we set up above.
323                          */
324                         for (is_first = 1,
325                             lp = SH_TAILQ_FIRST(&op->waiters, __db_lock);
326                             lp != NULL;
327                             is_first = 0,
328                             lp = SH_TAILQ_NEXT(lp, links, __db_lock)) {
329                                 if ((ret = __lock_getobj(lt,
330                                     lp->holder, NULL, DB_LOCK_LOCKER, &lockerp))
331                                     != 0) {
332                                         __db_err(dbenv,
333                                             "warning unable to find object");
334                                         continue;
335                                 }
336                                 id_array[lockerp->dd_id].id = lp->holder;
337                                 id_array[lockerp->dd_id].valid = 1;
338
339                                 /*
340                                  * If the transaction is pending abortion, then
341                                  * ignore it on this iteration.
342                                  */
343                                 if (lp->status != DB_LSTAT_WAITING)
344                                         continue;
345
346                                 entryp = bitmap + (nentries * lockerp->dd_id);
347                                 OR_MAP(entryp, tmpmap, nentries);
348                                 /*
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.
354                                  */
355                                 if (is_first)
356                                         CLR_MAP(entryp, lockerp->dd_id);
357                         }
358                 }
359         }
360
361         /* Now for each locker; record its last lock. */
362         for (id = 0; id < count; id++) {
363                 if (!id_array[id].valid)
364                         continue;
365                 if ((ret = __lock_getobj(lt,
366                     id_array[id].id, NULL, DB_LOCK_LOCKER, &lockerp)) != 0) {
367                         __db_err(dbenv,
368                             "No locks for locker %lu", (u_long)id_array[id].id);
369                         continue;
370                 }
371                 lp = SH_LIST_FIRST(&lockerp->heldby, __db_lock);
372                 if (lp != NULL)
373                         id_array[id].last_lock = LOCK_TO_OFFSET(lt, lp);
374         }
375
376         /* Pass complete, reset the deadlock detector bit. */
377         lt->region->need_dd = 0;
378         UNLOCK_LOCKREGION(lt);
379
380         /*
381          * Now we can release everything except the bitmap matrix that we
382          * created.
383          */
384         *nlockers = id;
385         *idmap = id_array;
386         *bmp = bitmap;
387         free(tmpmap);
388         return (0);
389 }
390
391 static u_int32_t *
392 __dd_find(bmp, idmap, nlockers)
393         u_int32_t *bmp;
394         locker_info *idmap;
395         u_int32_t nlockers;
396 {
397         u_int32_t i, j, nentries, *mymap, *tmpmap;
398
399         /*
400          * For each locker, or in the bits from the lockers
401          * on which that locker is waiting.
402          */
403         nentries = ALIGN(nlockers, 32) / 32;
404         for (mymap = bmp, i = 0; i < nlockers; i++, mymap += nentries) {
405                 if (!idmap[i].valid)
406                         continue;
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))
413                                         return (mymap);
414                         }
415                 }
416         }
417         return (NULL);
418 }
419
420 static int
421 __dd_abort(dbenv, info)
422         DB_ENV *dbenv;
423         locker_info *info;
424 {
425         DB_LOCKTAB *lt;
426         DB_LOCKOBJ *lockerp, *sh_obj;
427         struct __db_lock *lockp;
428         int ret;
429
430         lt = dbenv->lk_info;
431         LOCK_LOCKREGION(lt);
432
433         /* Find the locker's last lock. */
434         if ((ret =
435             __lock_getobj(lt, info->id, NULL, DB_LOCK_LOCKER, &lockerp)) != 0)
436                 goto out;
437
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)
441                 goto out;
442
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);
450
451         ret = 0;
452
453 out:    UNLOCK_LOCKREGION(lt);
454         return (ret);
455 }
456
457 #ifdef DEBUG
458 static void
459 __dd_debug(dbenv, idmap, bitmap, nlockers)
460         DB_ENV *dbenv;
461         locker_info *idmap;
462         u_int32_t *bitmap;
463         int nlockers;
464 {
465         u_int32_t *mymap;
466         int i, j, nentries;
467         char *msgbuf;
468
469         __db_err(dbenv, "Waitsfor array");
470         __db_err(dbenv, "waiter\twaiting on");
471         /*
472          * Alloc space to print 10 bytes per item waited on.
473          */
474         if ((msgbuf = (char *)malloc((nlockers + 1) * 10 + 64)) == NULL) {
475                 errno = ENOMEM;
476                 __db_err(dbenv, "%s", strerror(errno));
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, "%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);
492         }
493
494         free(msgbuf);
495 }
496 #endif