| 1 |
/*- |
| 2 |
* See the file LICENSE for redistribution information. |
| 3 |
* |
| 4 |
* Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
* $Id$ |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "bench.h" |
| 10 |
|
| 11 |
#ifdef _POSIX_THREADS |
| 12 |
typedef struct { |
| 13 |
pthread_t id; |
| 14 |
DB_ENV *dbenv; |
| 15 |
int iterations; |
| 16 |
db_mutex_t mutex; |
| 17 |
int contentions; |
| 18 |
} threadinfo_t; |
| 19 |
|
| 20 |
static void *b_latch_latch_threadmain __P((void *)); |
| 21 |
#endif |
| 22 |
|
| 23 |
static int time_latches __P((DB_ENV *, db_mutex_t, int)); |
| 24 |
|
| 25 |
#define LATCH_THREADS_MAX 100 |
| 26 |
|
| 27 |
/* Return the environment needed for __mutex_lock(), depending on release. |
| 28 |
*/ |
| 29 |
#if DB_VERSION_MAJOR <4 || DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 7 |
| 30 |
#define ENV_ARG(dbenv) (dbenv) |
| 31 |
#else |
| 32 |
#define ENV_ARG(dbenv) ((dbenv)->env) |
| 33 |
#endif |
| 34 |
|
| 35 |
/* |
| 36 |
* In the mulithreaded latch test each thread locks and updates this variable. |
| 37 |
* It detects contention when the value of this counter changes during the |
| 38 |
* mutex lock call. |
| 39 |
*/ |
| 40 |
static int CurrentCounter = 0; |
| 41 |
static int b_latch_latch_usage __P((void)); |
| 42 |
|
| 43 |
static int |
| 44 |
b_latch_latch_usage() |
| 45 |
{ |
| 46 |
(void)fprintf(stderr, "usage: b_latch [-c number of %s", |
| 47 |
"lock+unlock pairs] [-n number of threads]\n"); |
| 48 |
return (EXIT_FAILURE); |
| 49 |
} |
| 50 |
|
| 51 |
/* |
| 52 |
* time_latches -- |
| 53 |
* Repeat acquire and release of an exclusive latch, counting the |
| 54 |
* number of times that 'someone else' got it just as we tried to. |
| 55 |
*/ |
| 56 |
static int time_latches(dbenv, mutex, iterations) |
| 57 |
DB_ENV *dbenv; |
| 58 |
db_mutex_t mutex; |
| 59 |
int iterations; |
| 60 |
{ |
| 61 |
int contended, i, previous; |
| 62 |
|
| 63 |
contended = 0; |
| 64 |
for (i = 0; i < iterations; ++i) { |
| 65 |
previous = CurrentCounter; |
| 66 |
DB_BENCH_ASSERT(__mutex_lock(ENV_ARG(dbenv), mutex) == 0); |
| 67 |
if (previous != CurrentCounter) |
| 68 |
contended++; |
| 69 |
CurrentCounter++; |
| 70 |
DB_BENCH_ASSERT(__mutex_unlock(ENV_ARG(dbenv), mutex) == 0); |
| 71 |
} |
| 72 |
return (contended); |
| 73 |
} |
| 74 |
|
| 75 |
#ifdef _POSIX_THREADS |
| 76 |
/* |
| 77 |
* latch_threadmain -- |
| 78 |
* Entry point for multithreaded latching test. |
| 79 |
* |
| 80 |
* Currently only supported for POSIX threads. |
| 81 |
*/ |
| 82 |
static void * |
| 83 |
b_latch_latch_threadmain(arg) |
| 84 |
void *arg; |
| 85 |
{ |
| 86 |
threadinfo_t *info = arg; |
| 87 |
|
| 88 |
info->contentions = time_latches(info->dbenv, |
| 89 |
info->mutex, info->iterations); |
| 90 |
|
| 91 |
return ((void *) 0); |
| 92 |
} |
| 93 |
#endif |
| 94 |
|
| 95 |
/* |
| 96 |
* b_latch -- |
| 97 |
* Measure the speed of latching and mutex operations. |
| 98 |
* |
| 99 |
* |
| 100 |
*/ |
| 101 |
int |
| 102 |
b_latch(argc, argv) |
| 103 |
int argc; |
| 104 |
char *argv[]; |
| 105 |
{ |
| 106 |
extern char *optarg; |
| 107 |
extern int optind, __db_getopt_reset; |
| 108 |
DB_ENV *dbenv; |
| 109 |
int ch, count, nthreads; |
| 110 |
#ifdef _POSIX_THREADS |
| 111 |
threadinfo_t threads[LATCH_THREADS_MAX]; |
| 112 |
int i, ret; |
| 113 |
void *status; |
| 114 |
#endif |
| 115 |
db_mutex_t mutex; |
| 116 |
int contended; |
| 117 |
|
| 118 |
contended = 0; |
| 119 |
count = 1000000; |
| 120 |
nthreads = 0; /* Default to running the test without extra threads */ |
| 121 |
__db_getopt_reset = 1; |
| 122 |
while ((ch = getopt(argc, argv, "c:n:")) != EOF) |
| 123 |
switch (ch) { |
| 124 |
case 'c': |
| 125 |
count = atoi(optarg); |
| 126 |
break; |
| 127 |
case 'n': |
| 128 |
nthreads = atoi(optarg); |
| 129 |
break; |
| 130 |
case '?': |
| 131 |
default: |
| 132 |
return (b_latch_latch_usage()); |
| 133 |
} |
| 134 |
argc -= optind; |
| 135 |
argv += optind; |
| 136 |
if (argc != 0 || count < 1 || nthreads < 0 || |
| 137 |
nthreads > LATCH_THREADS_MAX) |
| 138 |
return (b_latch_latch_usage()); |
| 139 |
#ifndef _POSIX_THREADS |
| 140 |
if (nthreads > 1) { |
| 141 |
(void)fprintf(stderr, |
| 142 |
"Sorry, support for -n %d: threads not yet available\n", |
| 143 |
nthreads); |
| 144 |
exit(EXIT_FAILURE); |
| 145 |
} |
| 146 |
#endif |
| 147 |
|
| 148 |
/* Create the environment. */ |
| 149 |
DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0); |
| 150 |
dbenv->set_errfile(dbenv, stderr); |
| 151 |
#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 1 |
| 152 |
DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, |
| 153 |
NULL, DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | |
| 154 |
DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE, 0666) == 0); |
| 155 |
#else |
| 156 |
DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, |
| 157 |
DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | |
| 158 |
DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE | DB_THREAD, 0666) == 0); |
| 159 |
#endif |
| 160 |
DB_BENCH_ASSERT(dbenv->mutex_alloc(dbenv, DB_MUTEX_SELF_BLOCK, |
| 161 |
&mutex) == 0); |
| 162 |
#ifdef _POSIX_THREADS |
| 163 |
for (i = 0; i < nthreads; i++) { |
| 164 |
threads[i].dbenv = dbenv; |
| 165 |
threads[i].mutex = mutex; |
| 166 |
threads[i].iterations = |
| 167 |
nthreads <= 1 ? count : count / nthreads; |
| 168 |
} |
| 169 |
#endif |
| 170 |
|
| 171 |
/* Start and acquire and release a mutex count times. If there's |
| 172 |
* posix support and a non-zero number of threads start them. |
| 173 |
*/ |
| 174 |
TIMER_START; |
| 175 |
#ifdef _POSIX_THREADS |
| 176 |
if (nthreads > 0) { |
| 177 |
for (i = 0; i < nthreads; i++) |
| 178 |
DB_BENCH_ASSERT(pthread_create(&threads[i].id, |
| 179 |
NULL, b_latch_latch_threadmain, &threads[i]) == 0); |
| 180 |
for (i = 0; i < nthreads; i++) { |
| 181 |
ret = pthread_join(threads[i].id, &status); |
| 182 |
DB_BENCH_ASSERT(ret == 0); |
| 183 |
contended += threads[i].contentions; |
| 184 |
} |
| 185 |
|
| 186 |
} else |
| 187 |
#endif |
| 188 |
contended = time_latches(dbenv, mutex, count); |
| 189 |
TIMER_STOP; |
| 190 |
|
| 191 |
printf("# %d mutex lock-unlock pairs of %d thread%s\n", count, |
| 192 |
nthreads, nthreads == 1 ? "" : "s"); |
| 193 |
TIMER_DISPLAY(count); |
| 194 |
|
| 195 |
DB_BENCH_ASSERT(dbenv->mutex_free(dbenv, mutex) == 0); |
| 196 |
DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0); |
| 197 |
COMPQUIET(contended, 0); |
| 198 |
|
| 199 |
return (0); |
| 200 |
} |