~ubuntu-branches/ubuntu/maverick/evolution-data-server/maverick-proposed

« back to all changes in this revision

Viewing changes to libdb/lock/lock_deadlock.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-05-17 17:02:06 UTC
  • mfrom: (1.1.79 upstream) (1.6.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20100517170206-4ufr52vwrhh26yh0
Tags: 2.30.1-1ubuntu1
* Merge from debian experimental. Remaining change:
  (LP: #42199, #229669, #173703, #360344, #508494)
  + debian/control:
    - add Vcs-Bzr tag
    - don't use libgnome
    - Use Breaks instead of Conflicts against evolution 2.25 and earlier.
  + debian/evolution-data-server.install,
    debian/patches/45_libcamel_providers_version.patch:
    - use the upstream versioning, not a Debian-specific one 
  + debian/libedata-book1.2-dev.install, debian/libebackend-1.2-dev.install,
    debian/libcamel1.2-dev.install, debian/libedataserverui1.2-dev.install:
    - install html documentation
  + debian/rules:
    - don't build documentation it's shipped with the tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*-
2
 
 * See the file LICENSE for redistribution information.
3
 
 *
4
 
 * Copyright (c) 1996-2002
5
 
 *      Sleepycat Software.  All rights reserved.
6
 
 */
7
 
 
8
 
#include "db_config.h"
9
 
 
10
 
#ifndef lint
11
 
static const char revid[] = "$Id$";
12
 
#endif /* not lint */
13
 
 
14
 
#ifndef NO_SYSTEM_INCLUDES
15
 
#include <sys/types.h>
16
 
 
17
 
#include <string.h>
18
 
#endif
19
 
 
20
 
#include "db_int.h"
21
 
#include "dbinc/db_shash.h"
22
 
#include "dbinc/lock.h"
23
 
#include "dbinc/txn.h"
24
 
#include "dbinc/rep.h"
25
 
 
26
 
#define ISSET_MAP(M, N) ((M)[(N) / 32] & (1 << (N) % 32))
27
 
 
28
 
#define CLEAR_MAP(M, N) {                                               \
29
 
        u_int32_t __i;                                                  \
30
 
        for (__i = 0; __i < (N); __i++)                                 \
31
 
                (M)[__i] = 0;                                           \
32
 
}
33
 
 
34
 
#define SET_MAP(M, B)   ((M)[(B) / 32] |= (1 << ((B) % 32)))
35
 
#define CLR_MAP(M, B)   ((M)[(B) / 32] &= ~(1 << ((B) % 32)))
36
 
 
37
 
#define OR_MAP(D, S, N) {                                               \
38
 
        u_int32_t __i;                                                  \
39
 
        for (__i = 0; __i < (N); __i++)                                 \
40
 
                D[__i] |= S[__i];                                       \
41
 
}
42
 
#define BAD_KILLID      0xffffffff
43
 
 
44
 
typedef struct {
45
 
        int             valid;
46
 
        int             self_wait;
47
 
        u_int32_t       count;
48
 
        u_int32_t       id;
49
 
        u_int32_t       last_lock;
50
 
        u_int32_t       last_locker_id;
51
 
        db_pgno_t       pgno;
52
 
} locker_info;
53
 
 
54
 
static int  __dd_abort __P((DB_ENV *, locker_info *));
55
 
static int  __dd_build __P((DB_ENV *,
56
 
            u_int32_t, u_int32_t **, u_int32_t *, u_int32_t *, locker_info **));
57
 
static int  __dd_find __P((DB_ENV *,
58
 
            u_int32_t *, locker_info *, u_int32_t, u_int32_t, u_int32_t ***));
59
 
static int  __dd_isolder __P((u_int32_t, u_int32_t, u_int32_t, u_int32_t));
60
 
static int __dd_verify __P((locker_info *, u_int32_t *, u_int32_t *,
61
 
            u_int32_t *, u_int32_t, u_int32_t, u_int32_t));
62
 
 
63
 
#ifdef DIAGNOSTIC
64
 
static void __dd_debug
65
 
            __P((DB_ENV *, locker_info *, u_int32_t *, u_int32_t, u_int32_t));
66
 
#endif
67
 
 
68
 
/*
69
 
 * lock_detect --
70
 
 *
71
 
 * PUBLIC: int __lock_detect __P((DB_ENV *, u_int32_t, u_int32_t, int *));
72
 
 */
73
 
int
74
 
__lock_detect(dbenv, flags, atype, abortp)
75
 
        DB_ENV *dbenv;
76
 
        u_int32_t flags, atype;
77
 
        int *abortp;
78
 
{
79
 
        DB_LOCKREGION *region;
80
 
        DB_LOCKTAB *lt;
81
 
        DB_TXNMGR *tmgr;
82
 
        locker_info *idmap;
83
 
        u_int32_t *bitmap, *copymap, **deadp, **free_me, *tmpmap;
84
 
        u_int32_t i, keeper, killid, limit, nalloc, nlockers;
85
 
        u_int32_t lock_max, txn_max;
86
 
        int ret;
87
 
 
88
 
        PANIC_CHECK(dbenv);
89
 
        ENV_REQUIRES_CONFIG(dbenv,
90
 
            dbenv->lk_handle, "DB_ENV->lock_detect", DB_INIT_LOCK);
91
 
 
92
 
        /* Validate arguments. */
93
 
        if ((ret = __db_fchk(dbenv, "DB_ENV->lock_detect", flags, 0)) != 0)
94
 
                return (ret);
95
 
        switch (atype) {
96
 
        case DB_LOCK_DEFAULT:
97
 
        case DB_LOCK_EXPIRE:
98
 
        case DB_LOCK_MAXLOCKS:
99
 
        case DB_LOCK_MINLOCKS:
100
 
        case DB_LOCK_MINWRITE:
101
 
        case DB_LOCK_OLDEST:
102
 
        case DB_LOCK_RANDOM:
103
 
        case DB_LOCK_YOUNGEST:
104
 
                break;
105
 
        default:
106
 
                __db_err(dbenv,
107
 
            "DB_ENV->lock_detect: unknown deadlock detection mode specified");
108
 
                return (EINVAL);
109
 
        }
110
 
 
111
 
        /*
112
 
         * If this environment is a replication client, then we must use the
113
 
         * MINWRITE detection discipline.
114
 
         */
115
 
        if (__rep_is_client(dbenv))
116
 
                atype = DB_LOCK_MINWRITE;
117
 
 
118
 
        free_me = NULL;
119
 
 
120
 
        lt = dbenv->lk_handle;
121
 
        if (abortp != NULL)
122
 
                *abortp = 0;
123
 
 
124
 
        /* Check if a detector run is necessary. */
125
 
        LOCKREGION(dbenv, lt);
126
 
 
127
 
        /* Make a pass only if auto-detect would run. */
128
 
        region = lt->reginfo.primary;
129
 
 
130
 
        if (region->need_dd == 0) {
131
 
                UNLOCKREGION(dbenv, lt);
132
 
                return (0);
133
 
        }
134
 
 
135
 
        /* Reset need_dd, so we know we've run the detector. */
136
 
        region->need_dd = 0;
137
 
 
138
 
        /* Build the waits-for bitmap. */
139
 
        ret = __dd_build(dbenv, atype, &bitmap, &nlockers, &nalloc, &idmap);
140
 
        lock_max = region->stat.st_cur_maxid;
141
 
        UNLOCKREGION(dbenv, lt);
142
 
 
143
 
        /*
144
 
         * We need the cur_maxid from the txn region as well.  In order
145
 
         * to avoid tricky synchronization between the lock and txn
146
 
         * regions, we simply unlock the lock region and then lock the
147
 
         * txn region.  This introduces a small window during which the
148
 
         * transaction system could then wrap.  We're willing to return
149
 
         * the wrong answer for "oldest" or "youngest" in those rare
150
 
         * circumstances.
151
 
         */
152
 
        tmgr = dbenv->tx_handle;
153
 
        if (tmgr != NULL) {
154
 
                R_LOCK(dbenv, &tmgr->reginfo);
155
 
                txn_max = ((DB_TXNREGION *)tmgr->reginfo.primary)->cur_maxid;
156
 
                R_UNLOCK(dbenv, &tmgr->reginfo);
157
 
        } else
158
 
                txn_max = TXN_MAXIMUM;
159
 
        if (ret != 0 || atype == DB_LOCK_EXPIRE)
160
 
                return (ret);
161
 
 
162
 
        if (nlockers == 0)
163
 
                return (0);
164
 
#ifdef DIAGNOSTIC
165
 
        if (FLD_ISSET(dbenv->verbose, DB_VERB_WAITSFOR))
166
 
                __dd_debug(dbenv, idmap, bitmap, nlockers, nalloc);
167
 
#endif
168
 
        /* Now duplicate the bitmaps so we can verify deadlock participants. */
169
 
        if ((ret = __os_calloc(dbenv, (size_t)nlockers,
170
 
            sizeof(u_int32_t) * nalloc, &copymap)) != 0)
171
 
                goto err;
172
 
        memcpy(copymap, bitmap, nlockers * sizeof(u_int32_t) * nalloc);
173
 
 
174
 
        if ((ret = __os_calloc(dbenv, sizeof(u_int32_t), nalloc, &tmpmap)) != 0)
175
 
                goto err1;
176
 
 
177
 
        /* Find a deadlock. */
178
 
        if ((ret =
179
 
            __dd_find(dbenv, bitmap, idmap, nlockers, nalloc, &deadp)) != 0)
180
 
                return (ret);
181
 
 
182
 
        killid = BAD_KILLID;
183
 
        free_me = deadp;
184
 
        for (; *deadp != NULL; deadp++) {
185
 
                if (abortp != NULL)
186
 
                        ++*abortp;
187
 
                killid = (u_int32_t)((*deadp - bitmap) / nalloc);
188
 
                limit = killid;
189
 
                keeper = BAD_KILLID;
190
 
 
191
 
                if (atype == DB_LOCK_DEFAULT || atype == DB_LOCK_RANDOM)
192
 
                        goto dokill;
193
 
                /*
194
 
                 * It's conceivable that under XA, the locker could
195
 
                 * have gone away.
196
 
                 */
197
 
                if (killid == BAD_KILLID)
198
 
                        break;
199
 
 
200
 
                /*
201
 
                 * Start with the id that we know is deadlocked
202
 
                 * and then examine all other set bits and see
203
 
                 * if any are a better candidate for abortion
204
 
                 * and that they are genuinely part of the
205
 
                 * deadlock.  The definition of "best":
206
 
                 * OLDEST: smallest id
207
 
                 * YOUNGEST: largest id
208
 
                 * MAXLOCKS: maximum count
209
 
                 * MINLOCKS: minimum count
210
 
                 * MINWRITE: minimum count
211
 
                 */
212
 
 
213
 
                for (i = (killid + 1) % nlockers;
214
 
                    i != limit;
215
 
                    i = (i + 1) % nlockers) {
216
 
                        if (!ISSET_MAP(*deadp, i))
217
 
                                continue;
218
 
                        switch (atype) {
219
 
                        case DB_LOCK_OLDEST:
220
 
                                if (__dd_isolder(idmap[killid].id,
221
 
                                    idmap[i].id, lock_max, txn_max))
222
 
                                        continue;
223
 
                                keeper = i;
224
 
                                break;
225
 
                        case DB_LOCK_YOUNGEST:
226
 
                                if (__dd_isolder(idmap[i].id,
227
 
                                    idmap[killid].id, lock_max, txn_max))
228
 
                                        continue;
229
 
                                keeper = i;
230
 
                                break;
231
 
                        case DB_LOCK_MAXLOCKS:
232
 
                                if (idmap[i].count < idmap[killid].count)
233
 
                                        continue;
234
 
                                keeper = i;
235
 
                                break;
236
 
                        case DB_LOCK_MINLOCKS:
237
 
                        case DB_LOCK_MINWRITE:
238
 
                                if (idmap[i].count > idmap[killid].count)
239
 
                                        continue;
240
 
                                keeper = i;
241
 
                                break;
242
 
                        default:
243
 
                                killid = BAD_KILLID;
244
 
                                ret = EINVAL;
245
 
                                goto dokill;
246
 
                        }
247
 
                        if (__dd_verify(idmap, *deadp,
248
 
                            tmpmap, copymap, nlockers, nalloc, i))
249
 
                                killid = i;
250
 
                }
251
 
 
252
 
dokill:         if (killid == BAD_KILLID)
253
 
                        continue;
254
 
 
255
 
                /*
256
 
                 * There are cases in which our general algorithm will
257
 
                 * fail.  Returning 1 from verify indicates that the
258
 
                 * particular locker is not only involved in a deadlock,
259
 
                 * but that killing him will allow others to make forward
260
 
                 * progress.  Unfortunately, there are cases where we need
261
 
                 * to abort someone, but killing them will not necessarily
262
 
                 * ensure forward progress (imagine N readers all trying to
263
 
                 * acquire a write lock).  In such a scenario, we'll have
264
 
                 * gotten all the way through the loop, we will have found
265
 
                 * someone to keep (keeper will be valid), but killid will
266
 
                 * still be the initial deadlocker.  In this case, if the
267
 
                 * initial killid satisfies __dd_verify, kill it, else abort
268
 
                 * keeper and indicate that we need to run deadlock detection
269
 
                 * again.
270
 
                 */
271
 
 
272
 
                if (keeper != BAD_KILLID && killid == limit &&
273
 
                    __dd_verify(idmap, *deadp,
274
 
                    tmpmap, copymap, nlockers, nalloc, killid) == 0) {
275
 
                        LOCKREGION(dbenv, lt);
276
 
                        region->need_dd = 1;
277
 
                        UNLOCKREGION(dbenv, lt);
278
 
                        killid = keeper;
279
 
                }
280
 
 
281
 
                /* Kill the locker with lockid idmap[killid]. */
282
 
                if ((ret = __dd_abort(dbenv, &idmap[killid])) != 0) {
283
 
                        /*
284
 
                         * It's possible that the lock was already aborted;
285
 
                         * this isn't necessarily a problem, so do not treat
286
 
                         * it as an error.
287
 
                         */
288
 
                        if (ret == DB_ALREADY_ABORTED)
289
 
                                ret = 0;
290
 
                        else
291
 
                                __db_err(dbenv,
292
 
                                    "warning: unable to abort locker %lx",
293
 
                                    (u_long)idmap[killid].id);
294
 
                } else if (FLD_ISSET(dbenv->verbose, DB_VERB_DEADLOCK))
295
 
                        __db_err(dbenv,
296
 
                            "Aborting locker %lx", (u_long)idmap[killid].id);
297
 
        }
298
 
        __os_free(dbenv, tmpmap);
299
 
err1:   __os_free(dbenv, copymap);
300
 
 
301
 
err:    if (free_me != NULL)
302
 
                __os_free(dbenv, free_me);
303
 
        __os_free(dbenv, bitmap);
304
 
        __os_free(dbenv, idmap);
305
 
 
306
 
        return (ret);
307
 
}
308
 
 
309
 
/*
310
 
 * ========================================================================
311
 
 * Utilities
312
 
 */
313
 
 
314
 
# define DD_INVALID_ID  ((u_int32_t) -1)
315
 
 
316
 
static int
317
 
__dd_build(dbenv, atype, bmp, nlockers, allocp, idmap)
318
 
        DB_ENV *dbenv;
319
 
        u_int32_t atype, **bmp, *nlockers, *allocp;
320
 
        locker_info **idmap;
321
 
{
322
 
        struct __db_lock *lp;
323
 
        DB_LOCKER *lip, *lockerp, *child;
324
 
        DB_LOCKOBJ *op, *lo;
325
 
        DB_LOCKREGION *region;
326
 
        DB_LOCKTAB *lt;
327
 
        locker_info *id_array;
328
 
        db_timeval_t now;
329
 
        u_int32_t *bitmap, count, dd, *entryp, id, ndx, nentries, *tmpmap;
330
 
        u_int8_t *pptr;
331
 
        int expire_only, is_first, need_timeout, ret;
332
 
 
333
 
        lt = dbenv->lk_handle;
334
 
        region = lt->reginfo.primary;
335
 
        LOCK_SET_TIME_INVALID(&now);
336
 
        need_timeout = 0;
337
 
        expire_only = atype == DB_LOCK_EXPIRE;
338
 
 
339
 
        /*
340
 
         * While we always check for expired timeouts, if we are called
341
 
         * with DB_LOCK_EXPIRE, then we are only checking for timeouts
342
 
         * (i.e., not doing deadlock detection at all).  If we aren't
343
 
         * doing real deadlock detection, then we can skip a significant,
344
 
         * amount of the processing.  In particular we do not build
345
 
         * the conflict array and our caller needs to expect this.
346
 
         */
347
 
        if (expire_only) {
348
 
                count = 0;
349
 
                nentries = 0;
350
 
                goto obj_loop;
351
 
        }
352
 
 
353
 
        /*
354
 
         * We'll check how many lockers there are, add a few more in for
355
 
         * good measure and then allocate all the structures.  Then we'll
356
 
         * verify that we have enough room when we go back in and get the
357
 
         * mutex the second time.
358
 
         */
359
 
retry:  count = region->stat.st_nlockers;
360
 
 
361
 
        if (count == 0) {
362
 
                *nlockers = 0;
363
 
                return (0);
364
 
        }
365
 
 
366
 
        if (FLD_ISSET(dbenv->verbose, DB_VERB_DEADLOCK))
367
 
                __db_err(dbenv, "%lu lockers", (u_long)count);
368
 
 
369
 
        count += 20;
370
 
        nentries = ALIGN(count, 32) / 32;
371
 
 
372
 
        /*
373
 
         * Allocate enough space for a count by count bitmap matrix.
374
 
         *
375
 
         * XXX
376
 
         * We can probably save the malloc's between iterations just
377
 
         * reallocing if necessary because count grew by too much.
378
 
         */
379
 
        if ((ret = __os_calloc(dbenv, (size_t)count,
380
 
            sizeof(u_int32_t) * nentries, &bitmap)) != 0)
381
 
                return (ret);
382
 
 
383
 
        if ((ret = __os_calloc(dbenv,
384
 
            sizeof(u_int32_t), nentries, &tmpmap)) != 0) {
385
 
                __os_free(dbenv, bitmap);
386
 
                return (ret);
387
 
        }
388
 
 
389
 
        if ((ret = __os_calloc(dbenv,
390
 
            (size_t)count, sizeof(locker_info), &id_array)) != 0) {
391
 
                __os_free(dbenv, bitmap);
392
 
                __os_free(dbenv, tmpmap);
393
 
                return (ret);
394
 
        }
395
 
 
396
 
        /*
397
 
         * Now go back in and actually fill in the matrix.
398
 
         */
399
 
        if (region->stat.st_nlockers > count) {
400
 
                __os_free(dbenv, bitmap);
401
 
                __os_free(dbenv, tmpmap);
402
 
                __os_free(dbenv, id_array);
403
 
                goto retry;
404
 
        }
405
 
 
406
 
        /*
407
 
         * First we go through and assign each locker a deadlock detector id.
408
 
         */
409
 
        for (id = 0, lip = SH_TAILQ_FIRST(&region->lockers, __db_locker);
410
 
            lip != NULL;
411
 
            lip = SH_TAILQ_NEXT(lip, ulinks, __db_locker)) {
412
 
                if (F_ISSET(lip, DB_LOCKER_INABORT))
413
 
                        continue;
414
 
                if (lip->master_locker == INVALID_ROFF) {
415
 
                        lip->dd_id = id++;
416
 
                        id_array[lip->dd_id].id = lip->id;
417
 
                        if (atype == DB_LOCK_MINLOCKS ||
418
 
                            atype == DB_LOCK_MAXLOCKS)
419
 
                                id_array[lip->dd_id].count = lip->nlocks;
420
 
                        if (atype == DB_LOCK_MINWRITE)
421
 
                                id_array[lip->dd_id].count = lip->nwrites;
422
 
                } else
423
 
                        lip->dd_id = DD_INVALID_ID;
424
 
 
425
 
        }
426
 
 
427
 
        /*
428
 
         * We only need consider objects that have waiters, so we use
429
 
         * the list of objects with waiters (dd_objs) instead of traversing
430
 
         * the entire hash table.  For each object, we traverse the waiters
431
 
         * list and add an entry in the waitsfor matrix for each waiter/holder
432
 
         * combination.
433
 
         */
434
 
obj_loop:
435
 
        for (op = SH_TAILQ_FIRST(&region->dd_objs, __db_lockobj);
436
 
            op != NULL; op = SH_TAILQ_NEXT(op, dd_links, __db_lockobj)) {
437
 
                if (expire_only)
438
 
                        goto look_waiters;
439
 
                CLEAR_MAP(tmpmap, nentries);
440
 
 
441
 
                /*
442
 
                 * First we go through and create a bit map that
443
 
                 * represents all the holders of this object.
444
 
                 */
445
 
                for (lp = SH_TAILQ_FIRST(&op->holders, __db_lock);
446
 
                    lp != NULL;
447
 
                    lp = SH_TAILQ_NEXT(lp, links, __db_lock)) {
448
 
                        LOCKER_LOCK(lt, region, lp->holder, ndx);
449
 
                        if ((ret = __lock_getlocker(lt,
450
 
                            lp->holder, ndx, 0, &lockerp)) != 0)
451
 
                                continue;
452
 
                        if (F_ISSET(lockerp, DB_LOCKER_INABORT))
453
 
                                continue;
454
 
 
455
 
                        if (lockerp->dd_id == DD_INVALID_ID) {
456
 
                                dd = ((DB_LOCKER *)R_ADDR(&lt->reginfo,
457
 
                                    lockerp->master_locker))->dd_id;
458
 
                                lockerp->dd_id = dd;
459
 
                                if (atype == DB_LOCK_MINLOCKS ||
460
 
                                    atype == DB_LOCK_MAXLOCKS)
461
 
                                        id_array[dd].count += lockerp->nlocks;
462
 
                                if (atype == DB_LOCK_MINWRITE)
463
 
                                        id_array[dd].count += lockerp->nwrites;
464
 
 
465
 
                        } else
466
 
                                dd = lockerp->dd_id;
467
 
                        id_array[dd].valid = 1;
468
 
 
469
 
                        /*
470
 
                         * If the holder has already been aborted, then
471
 
                         * we should ignore it for now.
472
 
                         */
473
 
                        if (lp->status == DB_LSTAT_HELD)
474
 
                                SET_MAP(tmpmap, dd);
475
 
                }
476
 
 
477
 
                /*
478
 
                 * Next, for each waiter, we set its row in the matrix
479
 
                 * equal to the map of holders we set up above.
480
 
                 */
481
 
look_waiters:
482
 
                for (is_first = 1,
483
 
                    lp = SH_TAILQ_FIRST(&op->waiters, __db_lock);
484
 
                    lp != NULL;
485
 
                    is_first = 0,
486
 
                    lp = SH_TAILQ_NEXT(lp, links, __db_lock)) {
487
 
                        LOCKER_LOCK(lt, region, lp->holder, ndx);
488
 
                        if ((ret = __lock_getlocker(lt,
489
 
                            lp->holder, ndx, 0, &lockerp)) != 0)
490
 
                                continue;
491
 
                        if (lp->status == DB_LSTAT_WAITING) {
492
 
                                if (__lock_expired(dbenv,
493
 
                                    &now, &lockerp->lk_expire)) {
494
 
                                        lp->status = DB_LSTAT_EXPIRED;
495
 
                                        MUTEX_UNLOCK(dbenv, &lp->mutex);
496
 
                                        continue;
497
 
                                }
498
 
                                need_timeout =
499
 
                                    LOCK_TIME_ISVALID(&lockerp->lk_expire);
500
 
                        }
501
 
 
502
 
                        if (expire_only)
503
 
                                continue;
504
 
 
505
 
                        if (lockerp->dd_id == DD_INVALID_ID) {
506
 
                                dd = ((DB_LOCKER *)R_ADDR(&lt->reginfo,
507
 
                                    lockerp->master_locker))->dd_id;
508
 
                                lockerp->dd_id = dd;
509
 
                                if (atype == DB_LOCK_MINLOCKS ||
510
 
                                    atype == DB_LOCK_MAXLOCKS)
511
 
                                        id_array[dd].count += lockerp->nlocks;
512
 
                                if (atype == DB_LOCK_MINWRITE)
513
 
                                        id_array[dd].count += lockerp->nwrites;
514
 
                        } else
515
 
                                dd = lockerp->dd_id;
516
 
                        id_array[dd].valid = 1;
517
 
 
518
 
                        /*
519
 
                         * If the transaction is pending abortion, then
520
 
                         * ignore it on this iteration.
521
 
                         */
522
 
                        if (lp->status != DB_LSTAT_WAITING)
523
 
                                continue;
524
 
 
525
 
                        entryp = bitmap + (nentries * dd);
526
 
                        OR_MAP(entryp, tmpmap, nentries);
527
 
                        /*
528
 
                         * If this is the first waiter on the queue,
529
 
                         * then we remove the waitsfor relationship
530
 
                         * with oneself.  However, if it's anywhere
531
 
                         * else on the queue, then we have to keep
532
 
                         * it and we have an automatic deadlock.
533
 
                         */
534
 
                        if (is_first) {
535
 
                                if (ISSET_MAP(entryp, dd))
536
 
                                        id_array[dd].self_wait = 1;
537
 
                                CLR_MAP(entryp, dd);
538
 
                        }
539
 
                }
540
 
        }
541
 
 
542
 
        if (expire_only) {
543
 
                region->need_dd = need_timeout;
544
 
                return (0);
545
 
        }
546
 
 
547
 
        /* Now for each locker; record its last lock. */
548
 
        for (id = 0; id < count; id++) {
549
 
                if (!id_array[id].valid)
550
 
                        continue;
551
 
                LOCKER_LOCK(lt, region, id_array[id].id, ndx);
552
 
                if ((ret = __lock_getlocker(lt,
553
 
                    id_array[id].id, ndx, 0, &lockerp)) != 0) {
554
 
                        __db_err(dbenv,
555
 
                            "No locks for locker %lu", (u_long)id_array[id].id);
556
 
                        continue;
557
 
                }
558
 
 
559
 
                /*
560
 
                 * If this is a master transaction, try to
561
 
                 * find one of its children's locks first,
562
 
                 * as they are probably more recent.
563
 
                 */
564
 
                child = SH_LIST_FIRST(&lockerp->child_locker, __db_locker);
565
 
                if (child != NULL) {
566
 
                        do {
567
 
                                lp = SH_LIST_FIRST(&child->heldby, __db_lock);
568
 
                                if (lp != NULL &&
569
 
                                    lp->status == DB_LSTAT_WAITING) {
570
 
                                        id_array[id].last_locker_id = child->id;
571
 
                                        goto get_lock;
572
 
                                }
573
 
                                child = SH_LIST_NEXT(
574
 
                                    child, child_link, __db_locker);
575
 
                        } while (child != NULL);
576
 
                }
577
 
                lp = SH_LIST_FIRST(&lockerp->heldby, __db_lock);
578
 
                if (lp != NULL) {
579
 
                        id_array[id].last_locker_id = lockerp->id;
580
 
        get_lock:       id_array[id].last_lock = R_OFFSET(&lt->reginfo, lp);
581
 
                        lo = (DB_LOCKOBJ *)((u_int8_t *)lp + lp->obj);
582
 
                        pptr = SH_DBT_PTR(&lo->lockobj);
583
 
                        if (lo->lockobj.size >= sizeof(db_pgno_t))
584
 
                                memcpy(&id_array[id].pgno,
585
 
                                    pptr, sizeof(db_pgno_t));
586
 
                        else
587
 
                                id_array[id].pgno = 0;
588
 
                }
589
 
        }
590
 
 
591
 
        /*
592
 
         * Pass complete, reset the deadlock detector bit,
593
 
         * unless we have pending timeouts.
594
 
         */
595
 
        region->need_dd = need_timeout;
596
 
 
597
 
        /*
598
 
         * Now we can release everything except the bitmap matrix that we
599
 
         * created.
600
 
         */
601
 
        *nlockers = id;
602
 
        *idmap = id_array;
603
 
        *bmp = bitmap;
604
 
        *allocp = nentries;
605
 
        __os_free(dbenv, tmpmap);
606
 
        return (0);
607
 
}
608
 
 
609
 
static int
610
 
__dd_find(dbenv, bmp, idmap, nlockers, nalloc, deadp)
611
 
        DB_ENV *dbenv;
612
 
        u_int32_t *bmp, nlockers, nalloc;
613
 
        locker_info *idmap;
614
 
        u_int32_t ***deadp;
615
 
{
616
 
        u_int32_t i, j, k, *mymap, *tmpmap;
617
 
        u_int32_t **retp;
618
 
        int ndead, ndeadalloc, ret;
619
 
 
620
 
#undef  INITIAL_DEAD_ALLOC
621
 
#define INITIAL_DEAD_ALLOC      8
622
 
 
623
 
        ndeadalloc = INITIAL_DEAD_ALLOC;
624
 
        ndead = 0;
625
 
        if ((ret = __os_malloc(dbenv,
626
 
            ndeadalloc * sizeof(u_int32_t *), &retp)) != 0)
627
 
                return (ret);
628
 
 
629
 
        /*
630
 
         * For each locker, OR in the bits from the lockers on which that
631
 
         * locker is waiting.
632
 
         */
633
 
        for (mymap = bmp, i = 0; i < nlockers; i++, mymap += nalloc) {
634
 
                if (!idmap[i].valid)
635
 
                        continue;
636
 
                for (j = 0; j < nlockers; j++) {
637
 
                        if (!ISSET_MAP(mymap, j))
638
 
                                continue;
639
 
 
640
 
                        /* Find the map for this bit. */
641
 
                        tmpmap = bmp + (nalloc * j);
642
 
                        OR_MAP(mymap, tmpmap, nalloc);
643
 
                        if (!ISSET_MAP(mymap, i))
644
 
                                continue;
645
 
 
646
 
                        /* Make sure we leave room for NULL. */
647
 
                        if (ndead + 2 >= ndeadalloc) {
648
 
                                ndeadalloc <<= 1;
649
 
                                /*
650
 
                                 * If the alloc fails, then simply return the
651
 
                                 * deadlocks that we already have.
652
 
                                 */
653
 
                                if (__os_realloc(dbenv,
654
 
                                    ndeadalloc * sizeof(u_int32_t),
655
 
                                    &retp) != 0) {
656
 
                                        retp[ndead] = NULL;
657
 
                                        *deadp = retp;
658
 
                                        return (0);
659
 
                                }
660
 
                        }
661
 
                        retp[ndead++] = mymap;
662
 
 
663
 
                        /* Mark all participants in this deadlock invalid. */
664
 
                        for (k = 0; k < nlockers; k++)
665
 
                                if (ISSET_MAP(mymap, k))
666
 
                                        idmap[k].valid = 0;
667
 
                        break;
668
 
                }
669
 
        }
670
 
        retp[ndead] = NULL;
671
 
        *deadp = retp;
672
 
        return (0);
673
 
}
674
 
 
675
 
static int
676
 
__dd_abort(dbenv, info)
677
 
        DB_ENV *dbenv;
678
 
        locker_info *info;
679
 
{
680
 
        struct __db_lock *lockp;
681
 
        DB_LOCKER *lockerp;
682
 
        DB_LOCKOBJ *sh_obj;
683
 
        DB_LOCKREGION *region;
684
 
        DB_LOCKTAB *lt;
685
 
        u_int32_t ndx;
686
 
        int ret;
687
 
 
688
 
        lt = dbenv->lk_handle;
689
 
        region = lt->reginfo.primary;
690
 
 
691
 
        LOCKREGION(dbenv, lt);
692
 
 
693
 
        /* Find the locker's last lock. */
694
 
        LOCKER_LOCK(lt, region, info->last_locker_id, ndx);
695
 
        if ((ret = __lock_getlocker(lt,
696
 
            info->last_locker_id, ndx, 0, &lockerp)) != 0 || lockerp == NULL) {
697
 
                if (ret == 0)
698
 
                        ret = DB_ALREADY_ABORTED;
699
 
                goto out;
700
 
        }
701
 
 
702
 
        /* It's possible that this locker was already aborted. */
703
 
        if ((lockp = SH_LIST_FIRST(&lockerp->heldby, __db_lock)) == NULL) {
704
 
                ret = DB_ALREADY_ABORTED;
705
 
                goto out;
706
 
        }
707
 
        if (R_OFFSET(&lt->reginfo, lockp) != info->last_lock ||
708
 
            lockp->status != DB_LSTAT_WAITING) {
709
 
                ret = DB_ALREADY_ABORTED;
710
 
                goto out;
711
 
        }
712
 
 
713
 
        sh_obj = (DB_LOCKOBJ *)((u_int8_t *)lockp + lockp->obj);
714
 
        SH_LIST_REMOVE(lockp, locker_links, __db_lock);
715
 
 
716
 
        /* Abort lock, take it off list, and wake up this lock. */
717
 
        SHOBJECT_LOCK(lt, region, sh_obj, ndx);
718
 
        lockp->status = DB_LSTAT_ABORTED;
719
 
        SH_TAILQ_REMOVE(&sh_obj->waiters, lockp, links, __db_lock);
720
 
 
721
 
        /*
722
 
         * Either the waiters list is now empty, in which case we remove
723
 
         * it from dd_objs, or it is not empty, in which case we need to
724
 
         * do promotion.
725
 
         */
726
 
        if (SH_TAILQ_FIRST(&sh_obj->waiters, __db_lock) == NULL)
727
 
                SH_TAILQ_REMOVE(&region->dd_objs,
728
 
                    sh_obj, dd_links, __db_lockobj);
729
 
        else
730
 
                ret = __lock_promote(lt, sh_obj, 0);
731
 
        MUTEX_UNLOCK(dbenv, &lockp->mutex);
732
 
 
733
 
        region->stat.st_ndeadlocks++;
734
 
        UNLOCKREGION(dbenv, lt);
735
 
 
736
 
        return (0);
737
 
 
738
 
out:    UNLOCKREGION(dbenv, lt);
739
 
        return (ret);
740
 
}
741
 
 
742
 
#ifdef DIAGNOSTIC
743
 
static void
744
 
__dd_debug(dbenv, idmap, bitmap, nlockers, nalloc)
745
 
        DB_ENV *dbenv;
746
 
        locker_info *idmap;
747
 
        u_int32_t *bitmap, nlockers, nalloc;
748
 
{
749
 
        u_int32_t i, j, *mymap;
750
 
        char *msgbuf;
751
 
 
752
 
        __db_err(dbenv, "Waitsfor array\nWaiter:\tWaiting on:");
753
 
 
754
 
        /* Allocate space to print 10 bytes per item waited on. */
755
 
#undef  MSGBUF_LEN
756
 
#define MSGBUF_LEN ((nlockers + 1) * 10 + 64)
757
 
        if (__os_malloc(dbenv, MSGBUF_LEN, &msgbuf) != 0)
758
 
                return;
759
 
 
760
 
        for (mymap = bitmap, i = 0; i < nlockers; i++, mymap += nalloc) {
761
 
                if (!idmap[i].valid)
762
 
                        continue;
763
 
                sprintf(msgbuf,                                 /* Waiter. */
764
 
                    "%lx/%lu:\t", (u_long)idmap[i].id, (u_long)idmap[i].pgno);
765
 
                for (j = 0; j < nlockers; j++)
766
 
                        if (ISSET_MAP(mymap, j))
767
 
                                sprintf(msgbuf, "%s %lx", msgbuf,
768
 
                                    (u_long)idmap[j].id);
769
 
                (void)sprintf(msgbuf,
770
 
                    "%s %lu", msgbuf, (u_long)idmap[i].last_lock);
771
 
                __db_err(dbenv, msgbuf);
772
 
        }
773
 
 
774
 
        __os_free(dbenv, msgbuf);
775
 
}
776
 
#endif
777
 
 
778
 
/*
779
 
 * Given a bitmap that contains a deadlock, verify that the bit
780
 
 * specified in the which parameter indicates a transaction that
781
 
 * is actually deadlocked.  Return 1 if really deadlocked, 0 otherwise.
782
 
 * deadmap is the array that identified the deadlock.
783
 
 * tmpmap is a copy of the initial bitmaps from the dd_build phase
784
 
 * origmap is a temporary bit map into which we can OR things
785
 
 * nlockers is the number of actual lockers under consideration
786
 
 * nalloc is the number of words allocated for the bitmap
787
 
 * which is the locker in question
788
 
 */
789
 
static int
790
 
__dd_verify(idmap, deadmap, tmpmap, origmap, nlockers, nalloc, which)
791
 
        locker_info *idmap;
792
 
        u_int32_t *deadmap, *tmpmap, *origmap;
793
 
        u_int32_t nlockers, nalloc, which;
794
 
{
795
 
        u_int32_t *tmap;
796
 
        u_int32_t j;
797
 
        int count;
798
 
 
799
 
        memset(tmpmap, 0, sizeof(u_int32_t) * nalloc);
800
 
 
801
 
        /*
802
 
         * In order for "which" to be actively involved in
803
 
         * the deadlock, removing him from the evaluation
804
 
         * must remove the deadlock.  So, we OR together everyone
805
 
         * except which; if all the participants still have their
806
 
         * bits set, then the deadlock persists and which does
807
 
         * not participate.  If the deadlock does not persist
808
 
         * then "which" does participate.
809
 
         */
810
 
        count = 0;
811
 
        for (j = 0; j < nlockers; j++) {
812
 
                if (!ISSET_MAP(deadmap, j) || j == which)
813
 
                        continue;
814
 
 
815
 
                /* Find the map for this bit. */
816
 
                tmap = origmap + (nalloc * j);
817
 
 
818
 
                /*
819
 
                 * We special case the first waiter who is also a holder, so
820
 
                 * we don't automatically call that a deadlock.  However, if
821
 
                 * it really is a deadlock, we need the bit set now so that
822
 
                 * we treat the first waiter like other waiters.
823
 
                 */
824
 
                if (idmap[j].self_wait)
825
 
                        SET_MAP(tmap, j);
826
 
                OR_MAP(tmpmap, tmap, nalloc);
827
 
                count++;
828
 
        }
829
 
 
830
 
        if (count == 1)
831
 
                return (1);
832
 
 
833
 
        /*
834
 
         * Now check the resulting map and see whether
835
 
         * all participants still have their bit set.
836
 
         */
837
 
        for (j = 0; j < nlockers; j++) {
838
 
                if (!ISSET_MAP(deadmap, j) || j == which)
839
 
                        continue;
840
 
                if (!ISSET_MAP(tmpmap, j))
841
 
                        return (1);
842
 
        }
843
 
        return (0);
844
 
}
845
 
 
846
 
/*
847
 
 * __dd_isolder --
848
 
 *
849
 
 * Figure out the relative age of two lockers.  We make all lockers
850
 
 * older than all transactions, because that's how it's worked
851
 
 * historically (because lockers are lower ids).
852
 
 */
853
 
static int
854
 
__dd_isolder(a, b, lock_max, txn_max)
855
 
        u_int32_t       a, b;
856
 
        u_int32_t       lock_max, txn_max;
857
 
{
858
 
        u_int32_t max;
859
 
 
860
 
        /* Check for comparing lock-id and txnid. */
861
 
        if (a <= DB_LOCK_MAXID && b > DB_LOCK_MAXID)
862
 
                return (1);
863
 
        if (b <= DB_LOCK_MAXID && a > DB_LOCK_MAXID)
864
 
                return (0);
865
 
 
866
 
        /* In the same space; figure out which one. */
867
 
        max = txn_max;
868
 
        if (a <= DB_LOCK_MAXID)
869
 
                max = lock_max;
870
 
 
871
 
        /*
872
 
         * We can't get a 100% correct ordering, because we don't know
873
 
         * where the current interval started and if there were older
874
 
         * lockers outside the interval.  We do the best we can.
875
 
         */
876
 
 
877
 
        /*
878
 
         * Check for a wrapped case with ids above max.
879
 
         */
880
 
        if (a > max && b < max)
881
 
                return (1);
882
 
        if (b > max && a < max)
883
 
                return (0);
884
 
 
885
 
        return (a < b);
886
 
}