2
* Standalone mutex tester for Berkeley DB mutexes.
18
#if defined(HAVE_MUTEX_PTHREADS) || defined(BUILD_PTHREADS_ANYWAY)
29
void *run_thread_wake();
30
void tm_mutex_destroy();
32
void tm_mutex_stats();
35
#define MUTEX_WAKEME 0x80 /* Wake-me flag. */
37
DB_ENV dbenv; /* Fake out DB. */
38
size_t len; /* Backing file size. */
39
int align; /* Mutex alignment in file. */
40
int quit; /* End-of-test flag. */
41
char *file = "mutex.file"; /* Backing file. */
43
int maxlocks = 20; /* -l: Backing locks. */
44
int nlocks = 10000; /* -n: Locks per processes. */
45
int nprocs = 20; /* -p: Processes. */
46
int child; /* -s: Slave. */
47
int nthreads = 1; /* -t: Threads. */
48
int verbose; /* -v: Verbosity. */
58
int ch, eval, i, status;
62
while ((ch = getopt(argc, argv, "l:n:p:st:v")) != EOF)
65
maxlocks = atoi(optarg);
68
nlocks = atoi(optarg);
71
nprocs = atoi(optarg);
77
nthreads = atoi(optarg);
78
#if !defined(HAVE_MUTEX_PTHREADS) && !defined(BUILD_PTHREADS_ANYWAY)
81
"tm: pthreads not available or not compiled for this platform.\n");
82
return (EXIT_FAILURE);
92
"usage: tm [-v] [-l maxlocks] [-n locks] [-p procs] [-t threads]\n");
93
return (EXIT_FAILURE);
100
* DB_MUTEX[1] per-thread mutex array lock
101
* DB_MUTEX[nthreads] per-thread mutex array
102
* DB_MUTEX[maxlocks] per-lock mutex array
103
* u_long[maxlocks][2] per-lock ID array
105
align = ALIGN(sizeof(DB_MUTEX) * 2, MUTEX_ALIGN);
107
align * (1 + nthreads + maxlocks) + sizeof(u_long) * maxlocks * 2;
109
"mutex alignment %d, structure alignment %d, backing file %lu bytes\n",
110
MUTEX_ALIGN, align, (u_long)len);
114
return (EXIT_SUCCESS);
121
"%d proc, %d threads/proc, %d lock requests from %d locks:\n",
122
nprocs, nthreads, nlocks, maxlocks);
123
for (i = 0; i < nprocs; ++i)
127
return (EXIT_FAILURE);
136
while ((pid = wait(&status)) != (pid_t)-1) {
138
"%lu: exited %d\n", (u_long)pid, WEXITSTATUS(status));
139
if (WEXITSTATUS(status) != 0)
146
printf("tm: exit status: %s\n",
147
eval == EXIT_SUCCESS ? "success" : "failed!");
155
char *argv[10], **ap, b_l[10], b_n[10], b_t[10];
159
sprintf(b_l, "-l%d", maxlocks);
161
sprintf(b_n, "-n%d", nlocks);
164
sprintf(b_t, "-t%d", nthreads);
170
execvp(tmpath, argv);
172
fprintf(stderr, "%s: %s\n", tmpath, strerror(errno));
179
#if defined(HAVE_MUTEX_PTHREADS) || defined(BUILD_PTHREADS_ANYWAY)
180
pthread_t *kidsp, wakep;
184
__os_sleep(&dbenv, 3, 0); /* Let everyone catch up. */
186
srand((u_int)time(NULL) / getpid()); /* Initialize random numbers. */
188
if (nthreads == 1) /* Simple case. */
189
exit((int)run_thread((void *)0));
191
#if defined(HAVE_MUTEX_PTHREADS) || defined(BUILD_PTHREADS_ANYWAY)
193
* Spawn off threads. We have nthreads all locking and going to
194
* sleep, and one other thread cycling through and waking them up.
197
(pthread_t *)calloc(sizeof(pthread_t), nthreads)) == NULL) {
198
fprintf(stderr, "tm: %s\n", strerror(errno));
201
for (i = 0; i < nthreads; i++)
202
if ((errno = pthread_create(
203
&kidsp[i], NULL, run_thread, (void *)i)) != 0) {
204
fprintf(stderr, "tm: failed spawning thread %d: %s\n",
209
if ((errno = pthread_create(
210
&wakep, NULL, run_thread_wake, (void *)0)) != 0) {
211
fprintf(stderr, "tm: failed spawning wakeup thread: %s\n",
216
/* Wait for the threads to exit. */
218
for (i = 0; i < nthreads; i++) {
219
pthread_join(kidsp[i], &retp);
222
"tm: thread %d exited with error\n", i);
223
status = EXIT_FAILURE;
228
/* Signal wakeup thread to stop. */
230
pthread_join(wakep, &retp);
232
fprintf(stderr, "tm: wakeup thread exited with error\n");
233
status = EXIT_FAILURE;
244
DB_MUTEX *gm_addr, *lm_addr, *tm_addr, *mp;
245
u_long gid1, gid2, *id_addr;
246
int fd, i, lock, id, nl, remap;
248
/* Set local and global per-thread ID. */
250
gid1 = (u_long)getpid();
251
#if defined(HAVE_MUTEX_PTHREADS) || defined(BUILD_PTHREADS_ANYWAY)
252
gid2 = (u_long)pthread_self();
256
printf("\tPID: %lu; TID: %lx; ID: %d\n", gid1, gid2, id);
259
for (gm_addr = NULL, remap = 0;;) {
260
/* Map in the file as necessary. */
261
if (gm_addr == NULL) {
262
map_file(&gm_addr, &tm_addr, &lm_addr, &id_addr, &fd);
263
remap = (rand() % 100) + 35;
266
/* Select and acquire a data lock. */
267
lock = rand() % maxlocks;
268
mp = (DB_MUTEX *)((u_int8_t *)lm_addr + lock * align);
270
printf("%lu/%lx: %03d\n", gid1, gid2, lock);
272
if (__db_mutex_lock(&dbenv, mp)) {
274
"%lu/%lx: never got lock\n", gid1, gid2);
275
return ((void *)EXIT_FAILURE);
277
if (id_addr[lock * 2] != 0) {
279
"RACE! (%lu/%lx granted lock %d held by %lu/%lx)\n",
281
lock, id_addr[lock * 2], id_addr[lock * 2 + 1]);
282
return ((void *)EXIT_FAILURE);
284
id_addr[lock * 2] = gid1;
285
id_addr[lock * 2 + 1] = gid2;
288
* Pretend to do some work, periodically checking to see if
289
* we still hold the mutex.
291
for (i = 0; i < 3; ++i) {
292
__os_sleep(&dbenv, 0, rand() % 3);
293
if (id_addr[lock * 2] != gid1 ||
294
id_addr[lock * 2 + 1] != gid2) {
296
"RACE! (%lu/%lx stole lock %d from %lu/%lx)\n",
298
id_addr[lock * 2 + 1], lock, gid1, gid2);
299
return ((void *)EXIT_FAILURE);
303
#if defined(HAVE_MUTEX_PTHREADS) || defined(BUILD_PTHREADS_ANYWAY)
305
* Test self-blocking and unlocking by other threads/processes:
307
* acquire the global lock
308
* set our wakeup flag
309
* release the global lock
310
* acquire our per-thread lock
312
* The wakeup thread will wake us up.
314
if (__db_mutex_lock(&dbenv, gm_addr)) {
315
fprintf(stderr, "%lu/%lx: global lock\n", gid1, gid2);
316
return ((void *)EXIT_FAILURE);
318
mp = (DB_MUTEX *)((u_int8_t *)tm_addr + id * align);
319
F_SET(mp, MUTEX_WAKEME);
320
if (__db_mutex_unlock(&dbenv, gm_addr)) {
322
"%lu/%lx: per-thread wakeup failed\n", gid1, gid2);
323
return ((void *)EXIT_FAILURE);
325
if (__db_mutex_lock(&dbenv, mp)) {
327
"%lu/%lx: per-thread lock\n", gid1, gid2);
328
return ((void *)EXIT_FAILURE);
331
if (F_ISSET(mp, MUTEX_WAKEME)) {
332
fprintf(stderr, "%lu/%lx: %03d wakeup flag still set\n",
334
return ((void *)EXIT_FAILURE);
338
/* Release the data lock. */
339
id_addr[lock * 2] = id_addr[lock * 2 + 1] = 0;
340
mp = (DB_MUTEX *)((u_int8_t *)lm_addr + lock * align);
341
if (__db_mutex_unlock(&dbenv, mp)) {
342
fprintf(stderr, "%lu/%lx: wakeup failed\n", gid1, gid2);
343
return ((void *)EXIT_FAILURE);
347
fprintf(stderr, "%lu/%lx: %d\n", gid1, gid2, nl);
349
if (nl == 0 || --remap == 0) {
350
unmap_file((void *)gm_addr, fd);
356
__os_sleep(&dbenv, rand() % 3, 0);
363
#if defined(HAVE_MUTEX_PTHREADS) || defined(BUILD_PTHREADS_ANYWAY)
366
* Thread to wake up other threads that are sleeping.
372
DB_MUTEX *gm_addr, *tm_addr, *mp;
376
map_file(&gm_addr, &tm_addr, NULL, NULL, &fd);
378
/* Loop, waking up sleepers and periodically sleeping ourselves. */
382
/* Acquire the global lock. */
383
retry: if (__db_mutex_lock(&dbenv, gm_addr)) {
384
fprintf(stderr, "wt: global lock failed\n");
385
return ((void *)EXIT_FAILURE);
388
next: mp = (DB_MUTEX *)((u_int8_t *)tm_addr + id * align);
389
if (F_ISSET(mp, MUTEX_WAKEME)) {
390
F_CLR(mp, MUTEX_WAKEME);
391
if (__db_mutex_unlock(&dbenv, mp)) {
392
fprintf(stderr, "wt: wakeup failed\n");
393
return ((void *)EXIT_FAILURE);
397
if (++id < nthreads && id % 3 != 0)
400
if (__db_mutex_unlock(&dbenv, gm_addr)) {
401
fprintf(stderr, "wt: global unlock failed\n");
402
return ((void *)EXIT_FAILURE);
405
__os_sleep(&dbenv, 0, 500);
416
* Initialize the backing file.
424
/* Initialize the backing file. */
425
printf("Create the backing file...\n");
427
(void)shm_unlink(file);
428
if ((fd = shm_open(file, O_CREAT | O_RDWR | O_TRUNC,
431
if ((fd = open(file, O_CREAT | O_RDWR | O_TRUNC,
434
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) == -1) {
435
(void)fprintf(stderr, "%s: open: %s\n", file, strerror(errno));
438
if (lseek(fd, (off_t)len, SEEK_SET) != len || write(fd, &fd, 1) != 1) {
439
(void)fprintf(stderr,
440
"%s: seek/write: %s\n", file, strerror(errno));
448
* Initialize the mutexes.
453
DB_MUTEX *gm_addr, *lm_addr, *mp, *tm_addr;
456
map_file(&gm_addr, &tm_addr, &lm_addr, NULL, &fd);
458
printf("Initialize the global mutex...\n");
459
if (__db_mutex_init_int(&dbenv, gm_addr, 0, 0)) {
461
"__db_mutex_init (global): %s\n", strerror(errno));
465
printf("Initialize the per-thread mutexes...\n");
466
for (i = 1, mp = tm_addr;
467
i <= nthreads; ++i, mp = (DB_MUTEX *)((u_int8_t *)mp + align)) {
468
if (__db_mutex_init_int(&dbenv, mp, 0, MUTEX_SELF_BLOCK)) {
469
fprintf(stderr, "__db_mutex_init (per-thread %d): %s\n",
473
if (__db_mutex_lock(&dbenv, mp)) {
475
"__db_mutex_init (per-thread %d) lock: %s\n",
481
printf("Initialize the per-lock mutexes...\n");
482
for (i = 1, mp = lm_addr;
483
i <= maxlocks; ++i, mp = (DB_MUTEX *)((u_int8_t *)mp + align))
484
if (__db_mutex_init_int(&dbenv, mp, 0, 0)) {
485
fprintf(stderr, "__db_mutex_init (per-lock: %d): %s\n",
490
unmap_file((void *)gm_addr, fd);
494
* tm_mutex_destroy --
495
* Destroy the mutexes.
500
DB_MUTEX *gm_addr, *lm_addr, *mp, *tm_addr;
503
map_file(&gm_addr, &tm_addr, &lm_addr, NULL, &fd);
505
printf("Destroy the global mutex...\n");
506
if (__db_mutex_destroy(gm_addr)) {
508
"__db_mutex_destroy (global): %s\n", strerror(errno));
512
printf("Destroy the per-thread mutexes...\n");
513
for (i = 1, mp = tm_addr;
514
i <= nthreads; ++i, mp = (DB_MUTEX *)((u_int8_t *)mp + align)) {
515
if (__db_mutex_destroy(mp)) {
517
"__db_mutex_destroy (per-thread %d): %s\n",
523
printf("Destroy the per-lock mutexes...\n");
524
for (i = 1, mp = lm_addr;
525
i <= maxlocks; ++i, mp = (DB_MUTEX *)((u_int8_t *)mp + align))
526
if (__db_mutex_destroy(mp)) {
528
"__db_mutex_destroy (per-lock: %d): %s\n",
533
unmap_file((void *)gm_addr, fd);
535
(void)shm_unlink(file);
541
* Display mutex statistics.
546
DB_MUTEX *gm_addr, *lm_addr, *mp;
549
map_file(&gm_addr, NULL, &lm_addr, NULL, &fd);
551
printf("Per-lock mutex statistics...\n");
552
for (i = 1, mp = lm_addr;
553
i <= maxlocks; ++i, mp = (DB_MUTEX *)((u_int8_t *)mp + align))
554
printf("mutex %2d: wait: %lu; no wait %lu\n", i,
555
(u_long)mp->mutex_set_wait, (u_long)mp->mutex_set_nowait);
557
unmap_file((void *)gm_addr, fd);
562
* Map in the backing file.
565
map_file(gm_addrp, tm_addrp, lm_addrp, id_addrp, fdp)
566
DB_MUTEX **gm_addrp, **tm_addrp, **lm_addrp;
574
#define MAP_FAILED (void *)-1
580
if ((fd = shm_open(file, O_RDWR, 0)) == -1) {
582
if ((fd = open(file, O_RDWR, 0)) == -1) {
584
fprintf(stderr, "%s: open %s\n", file, strerror(errno));
588
maddr = mmap(NULL, len,
589
PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, (off_t)0);
590
if (maddr == MAP_FAILED) {
591
fprintf(stderr, "%s: mmap: %s\n", file, strerror(errno));
595
if (gm_addrp != NULL)
596
*gm_addrp = (DB_MUTEX *)maddr;
597
maddr = (u_int8_t *)maddr + align;
598
if (tm_addrp != NULL)
599
*tm_addrp = (DB_MUTEX *)maddr;
600
maddr = (u_int8_t *)maddr + align * nthreads;
601
if (lm_addrp != NULL)
602
*lm_addrp = (DB_MUTEX *)maddr;
603
maddr = (u_int8_t *)maddr + align * maxlocks;
604
if (id_addrp != NULL)
605
*id_addrp = (u_long *)maddr;
612
* Discard backing file map.
615
unmap_file(maddr, fd)
619
if (munmap(maddr, len) != 0) {
620
fprintf(stderr, "munmap: %s\n", strerror(errno));
623
if (close(fd) != 0) {
624
fprintf(stderr, "close: %s\n", strerror(errno));