| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <fcntl.h> |
| 15 |
#include <sched.h> /* IWYU pragma: keep */ |
| 16 |
#include <setjmp.h> |
| 17 |
#include <stdarg.h> |
| 18 |
#include <stddef.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <unistd.h> |
| 21 |
|
| 22 |
#define UNIT_TESTING |
| 23 |
#include <cmocka.h> |
| 24 |
|
| 25 |
#include <isc/atomic.h> |
| 26 |
#include <isc/barrier.h> |
| 27 |
#include <isc/file.h> |
| 28 |
#include <isc/mem.h> |
| 29 |
#include <isc/os.h> |
| 30 |
#include <isc/pause.h> |
| 31 |
#include <isc/random.h> |
| 32 |
#include <isc/result.h> |
| 33 |
#include <isc/rwlock.h> |
| 34 |
#include <isc/stdio.h> |
| 35 |
#include <isc/thread.h> |
| 36 |
#include <isc/time.h> |
| 37 |
#include <isc/util.h> |
| 38 |
|
| 39 |
#include <tests/isc.h> |
| 40 |
|
| 41 |
static unsigned int loops = 100; |
| 42 |
static unsigned int delay_loop = 1; |
| 43 |
|
| 44 |
static isc_rwlock_t rwlock; |
| 45 |
static pthread_rwlock_t prwlock; |
| 46 |
static isc_barrier_t barrier1; |
| 47 |
static isc_barrier_t barrier2; |
| 48 |
|
| 49 |
#define ITERS 20 |
| 50 |
|
| 51 |
#define DC 200 |
| 52 |
#define CNT_MIN 800 |
| 53 |
#define CNT_MAX 1600 |
| 54 |
|
| 55 |
static size_t shared_counter = 0; |
| 56 |
static size_t expected_counter = SIZE_MAX; |
| 57 |
static uint8_t boundary = 0; |
| 58 |
static uint8_t *rnd; |
| 59 |
|
| 60 |
static int |
| 61 |
setup_env(void **unused __attribute__((__unused__))) { |
| 62 |
char *env = getenv("ISC_BENCHMARK_LOOPS"); |
| 63 |
if (env != NULL) { |
| 64 |
loops = atoi(env); |
| 65 |
} |
| 66 |
assert_int_not_equal(loops, 0); |
| 67 |
|
| 68 |
env = getenv("ISC_BENCHMARK_DELAY"); |
| 69 |
if (env != NULL) { |
| 70 |
delay_loop = atoi(env); |
| 71 |
} |
| 72 |
assert_int_not_equal(delay_loop, 0); |
| 73 |
|
| 74 |
rnd = isc_mem_cget(mctx, loops, sizeof(rnd[0])); |
| 75 |
for (size_t i = 0; i < loops; i++) { |
| 76 |
rnd[i] = (uint8_t)isc_random_uniform(100); |
| 77 |
} |
| 78 |
|
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
static int |
| 83 |
teardown_env(void **state __attribute__((__unused__))) { |
| 84 |
isc_mem_cput(mctx, rnd, loops, sizeof(rnd[0])); |
| 85 |
|
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
static int |
| 90 |
rwlock_setup(void **state __attribute__((__unused__))) { |
| 91 |
isc_rwlock_init(&rwlock); |
| 92 |
|
| 93 |
isc_barrier_init(&barrier1, 2); |
| 94 |
isc_barrier_init(&barrier2, 2); |
| 95 |
if (pthread_rwlock_init(&prwlock, NULL) == -1) { |
| 96 |
return errno; |
| 97 |
} |
| 98 |
|
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
static int |
| 103 |
rwlock_teardown(void **state __attribute__((__unused__))) { |
| 104 |
if (pthread_rwlock_destroy(&prwlock) == -1) { |
| 105 |
return errno; |
| 106 |
} |
| 107 |
isc_barrier_destroy(&barrier2); |
| 108 |
isc_barrier_destroy(&barrier1); |
| 109 |
|
| 110 |
isc_rwlock_destroy(&rwlock); |
| 111 |
|
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
* Simple single-threaded read lock/unlock test |
| 117 |
*/ |
| 118 |
ISC_RUN_TEST_IMPL(isc_rwlock_rdlock) { |
| 119 |
isc_rwlock_lock(&rwlock, isc_rwlocktype_read); |
| 120 |
isc_pause_n(delay_loop); |
| 121 |
isc_rwlock_unlock(&rwlock, isc_rwlocktype_read); |
| 122 |
} |
| 123 |
|
| 124 |
/* |
| 125 |
* Simple single-threaded write lock/unlock test |
| 126 |
*/ |
| 127 |
ISC_RUN_TEST_IMPL(isc_rwlock_wrlock) { |
| 128 |
isc_rwlock_lock(&rwlock, isc_rwlocktype_write); |
| 129 |
isc_pause_n(delay_loop); |
| 130 |
isc_rwlock_unlock(&rwlock, isc_rwlocktype_write); |
| 131 |
} |
| 132 |
|
| 133 |
/* |
| 134 |
* Simple single-threaded lock/tryupgrade/unlock test |
| 135 |
*/ |
| 136 |
ISC_RUN_TEST_IMPL(isc_rwlock_tryupgrade) { |
| 137 |
isc_result_t result; |
| 138 |
isc_rwlock_lock(&rwlock, isc_rwlocktype_read); |
| 139 |
result = isc_rwlock_tryupgrade(&rwlock); |
| 140 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 141 |
isc_rwlock_unlock(&rwlock, isc_rwlocktype_write); |
| 142 |
} |
| 143 |
|
| 144 |
static void * |
| 145 |
trylock_thread1(void *arg __attribute__((__unused__))) { |
| 146 |
isc_rwlock_lock(&rwlock, isc_rwlocktype_write); |
| 147 |
|
| 148 |
isc_barrier_wait(&barrier1); |
| 149 |
isc_barrier_wait(&barrier2); |
| 150 |
|
| 151 |
isc_rwlock_unlock(&rwlock, isc_rwlocktype_write); |
| 152 |
|
| 153 |
isc_rwlock_lock(&rwlock, isc_rwlocktype_read); |
| 154 |
|
| 155 |
isc_barrier_wait(&barrier1); |
| 156 |
isc_barrier_wait(&barrier2); |
| 157 |
|
| 158 |
isc_rwlock_unlock(&rwlock, isc_rwlocktype_read); |
| 159 |
|
| 160 |
return NULL; |
| 161 |
} |
| 162 |
|
| 163 |
static void * |
| 164 |
trylock_thread2(void *arg __attribute__((__unused__))) { |
| 165 |
isc_result_t result; |
| 166 |
|
| 167 |
isc_barrier_wait(&barrier1); |
| 168 |
|
| 169 |
result = isc_rwlock_trylock(&rwlock, isc_rwlocktype_read); |
| 170 |
assert_int_equal(result, ISC_R_LOCKBUSY); |
| 171 |
|
| 172 |
isc_barrier_wait(&barrier2); |
| 173 |
isc_barrier_wait(&barrier1); |
| 174 |
|
| 175 |
result = isc_rwlock_trylock(&rwlock, isc_rwlocktype_read); |
| 176 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 177 |
|
| 178 |
isc_barrier_wait(&barrier2); |
| 179 |
|
| 180 |
isc_rwlock_unlock(&rwlock, isc_rwlocktype_read); |
| 181 |
|
| 182 |
return NULL; |
| 183 |
} |
| 184 |
|
| 185 |
ISC_RUN_TEST_IMPL(isc_rwlock_trylock) { |
| 186 |
isc_thread_t thread1; |
| 187 |
isc_thread_t thread2; |
| 188 |
|
| 189 |
isc_thread_create(trylock_thread1, NULL, &thread1); |
| 190 |
isc_thread_create(trylock_thread2, NULL, &thread2); |
| 191 |
|
| 192 |
isc_thread_join(thread2, NULL); |
| 193 |
isc_thread_join(thread1, NULL); |
| 194 |
} |
| 195 |
|
| 196 |
static void * |
| 197 |
pthread_rwlock_thread(void *arg __attribute__((__unused__))) { |
| 198 |
size_t cont = *(size_t *)arg; |
| 199 |
|
| 200 |
for (size_t i = 0; i < loops; i++) { |
| 201 |
if (rnd[i] < boundary) { |
| 202 |
pthread_rwlock_wrlock(&prwlock); |
| 203 |
size_t v = shared_counter; |
| 204 |
isc_pause_n(delay_loop); |
| 205 |
shared_counter = v + 1; |
| 206 |
pthread_rwlock_unlock(&prwlock); |
| 207 |
} else { |
| 208 |
pthread_rwlock_rdlock(&prwlock); |
| 209 |
isc_pause_n(delay_loop); |
| 210 |
pthread_rwlock_unlock(&prwlock); |
| 211 |
} |
| 212 |
isc_pause_n(cont); |
| 213 |
} |
| 214 |
|
| 215 |
return NULL; |
| 216 |
} |
| 217 |
|
| 218 |
static void * |
| 219 |
isc_rwlock_thread(void *arg __attribute__((__unused__))) { |
| 220 |
size_t cont = *(size_t *)arg; |
| 221 |
|
| 222 |
for (size_t i = 0; i < loops; i++) { |
| 223 |
if (rnd[i] < boundary) { |
| 224 |
isc_rwlock_lock(&rwlock, isc_rwlocktype_write); |
| 225 |
size_t v = shared_counter; |
| 226 |
isc_pause_n(delay_loop); |
| 227 |
shared_counter = v + 1; |
| 228 |
isc_rwlock_unlock(&rwlock, isc_rwlocktype_write); |
| 229 |
} else { |
| 230 |
isc_rwlock_lock(&rwlock, isc_rwlocktype_read); |
| 231 |
isc_pause_n(delay_loop); |
| 232 |
isc_rwlock_unlock(&rwlock, isc_rwlocktype_read); |
| 233 |
} |
| 234 |
isc_pause_n(cont); |
| 235 |
} |
| 236 |
|
| 237 |
return NULL; |
| 238 |
} |
| 239 |
|
| 240 |
static void |
| 241 |
isc__rwlock_benchmark(isc_thread_t *threads, unsigned int nthreads, |
| 242 |
uint8_t pct) { |
| 243 |
isc_time_t ts1, ts2; |
| 244 |
double t; |
| 245 |
int dc; |
| 246 |
size_t cont; |
| 247 |
|
| 248 |
expected_counter = ITERS * nthreads * loops * |
| 249 |
((CNT_MAX - CNT_MIN) / DC + 1); |
| 250 |
|
| 251 |
boundary = pct; |
| 252 |
|
| 253 |
/* PTHREAD RWLOCK */ |
| 254 |
|
| 255 |
ts1 = isc_time_now_hires(); |
| 256 |
|
| 257 |
shared_counter = 0; |
| 258 |
dc = DC; |
| 259 |
for (size_t l = 0; l < ITERS; l++) { |
| 260 |
for (cont = (dc > 0) ? CNT_MIN : CNT_MAX; |
| 261 |
cont <= CNT_MAX && cont >= CNT_MIN; cont += dc) |
| 262 |
{ |
| 263 |
for (size_t i = 0; i < nthreads; i++) { |
| 264 |
isc_thread_create(pthread_rwlock_thread, &cont, |
| 265 |
&threads[i]); |
| 266 |
} |
| 267 |
for (size_t i = 0; i < nthreads; i++) { |
| 268 |
isc_thread_join(threads[i], NULL); |
| 269 |
} |
| 270 |
} |
| 271 |
dc = -dc; |
| 272 |
} |
| 273 |
|
| 274 |
ts2 = isc_time_now_hires(); |
| 275 |
|
| 276 |
t = isc_time_microdiff(&ts2, &ts1); |
| 277 |
|
| 278 |
printf("[ TIME ] isc_rwlock_benchmark: %zu pthread_rwlock loops in " |
| 279 |
"%u threads, %2.3f%% writes, %2.3f seconds, %2.3f " |
| 280 |
"calls/second\n", |
| 281 |
expected_counter, nthreads, |
| 282 |
(double)shared_counter * 100 / expected_counter, t / 1000000.0, |
| 283 |
expected_counter / (t / 1000000.0)); |
| 284 |
|
| 285 |
/* ISC RWLOCK */ |
| 286 |
|
| 287 |
ts1 = isc_time_now_hires(); |
| 288 |
|
| 289 |
dc = DC; |
| 290 |
shared_counter = 0; |
| 291 |
for (size_t l = 0; l < ITERS; l++) { |
| 292 |
for (cont = (dc > 0) ? CNT_MIN : CNT_MAX; |
| 293 |
cont <= CNT_MAX && cont >= CNT_MIN; cont += dc) |
| 294 |
{ |
| 295 |
for (size_t i = 0; i < nthreads; i++) { |
| 296 |
isc_thread_create(isc_rwlock_thread, &cont, |
| 297 |
&threads[i]); |
| 298 |
} |
| 299 |
for (size_t i = 0; i < nthreads; i++) { |
| 300 |
isc_thread_join(threads[i], NULL); |
| 301 |
} |
| 302 |
} |
| 303 |
dc = -dc; |
| 304 |
} |
| 305 |
|
| 306 |
ts2 = isc_time_now_hires(); |
| 307 |
|
| 308 |
t = isc_time_microdiff(&ts2, &ts1); |
| 309 |
|
| 310 |
printf("[ TIME ] isc_rwlock_benchmark: %zu isc_rwlock loops in " |
| 311 |
"%u threads, %2.3f%% writes, %2.3f seconds, %2.3f " |
| 312 |
"calls/second\n", |
| 313 |
expected_counter, nthreads, |
| 314 |
(double)shared_counter * 100 / expected_counter, t / 1000000.0, |
| 315 |
expected_counter / (t / 1000000.0)); |
| 316 |
} |
| 317 |
|
| 318 |
ISC_RUN_TEST_IMPL(isc_rwlock_benchmark) { |
| 319 |
isc_thread_t *threads = isc_mem_cget(mctx, workers, sizeof(*threads)); |
| 320 |
|
| 321 |
memset(threads, 0, sizeof(*threads) * workers); |
| 322 |
|
| 323 |
for (unsigned int nthreads = workers; nthreads > 0; nthreads /= 2) { |
| 324 |
isc__rwlock_benchmark(threads, nthreads, 0); |
| 325 |
isc__rwlock_benchmark(threads, nthreads, 1); |
| 326 |
isc__rwlock_benchmark(threads, nthreads, 10); |
| 327 |
isc__rwlock_benchmark(threads, nthreads, 50); |
| 328 |
isc__rwlock_benchmark(threads, nthreads, 90); |
| 329 |
isc__rwlock_benchmark(threads, nthreads, 99); |
| 330 |
isc__rwlock_benchmark(threads, nthreads, 100); |
| 331 |
} |
| 332 |
|
| 333 |
isc_mem_cput(mctx, threads, workers, sizeof(*threads)); |
| 334 |
} |
| 335 |
|
| 336 |
ISC_TEST_LIST_START |
| 337 |
|
| 338 |
ISC_TEST_ENTRY_CUSTOM(isc_rwlock_rdlock, rwlock_setup, rwlock_teardown) |
| 339 |
ISC_TEST_ENTRY_CUSTOM(isc_rwlock_wrlock, rwlock_setup, rwlock_teardown) |
| 340 |
#if !defined(__SANITIZE_THREAD__) |
| 341 |
ISC_TEST_ENTRY_CUSTOM(isc_rwlock_tryupgrade, rwlock_setup, rwlock_teardown) |
| 342 |
ISC_TEST_ENTRY_CUSTOM(isc_rwlock_trylock, rwlock_setup, rwlock_teardown) |
| 343 |
ISC_TEST_ENTRY_CUSTOM(isc_rwlock_benchmark, rwlock_setup, rwlock_teardown) |
| 344 |
#endif /* __SANITIZE_THREAD__ */ |
| 345 |
|
| 346 |
ISC_TEST_LIST_END |
| 347 |
|
| 348 |
ISC_TEST_MAIN_CUSTOM(setup_env, teardown_env) |