| 1 |
/* |
| 2 |
* Copyright 2018 Paul Khuong. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 15 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 18 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 20 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 21 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 22 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 23 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 24 |
* SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <assert.h> |
| 28 |
#include <ck_cc.h> |
| 29 |
#include <ck_ec.h> |
| 30 |
#include <errno.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <inttypes.h> |
| 34 |
|
| 35 |
#include "../../common.h" |
| 36 |
|
| 37 |
#ifndef STEPS |
| 38 |
#define STEPS (65536 * 64) |
| 39 |
#endif |
| 40 |
|
| 41 |
static int gettime(const struct ck_ec_ops *, struct timespec *out); |
| 42 |
static void wake32(const struct ck_ec_ops *, const uint32_t *); |
| 43 |
static void wait32(const struct ck_ec_wait_state *, |
| 44 |
const uint32_t *, uint32_t, const struct timespec *); |
| 45 |
static void wake64(const struct ck_ec_ops *, const uint64_t *); |
| 46 |
static void wait64(const struct ck_ec_wait_state *, |
| 47 |
const uint64_t *, uint64_t, const struct timespec *); |
| 48 |
|
| 49 |
static const struct ck_ec_ops test_ops = { |
| 50 |
.gettime = gettime, |
| 51 |
.wait32 = wait32, |
| 52 |
.wait64 = wait64, |
| 53 |
.wake32 = wake32, |
| 54 |
.wake64 = wake64 |
| 55 |
}; |
| 56 |
|
| 57 |
#ifndef __linux__ |
| 58 |
static int gettime(const struct ck_ec_ops *ops, struct timespec *out) |
| 59 |
{ |
| 60 |
(void)out; |
| 61 |
|
| 62 |
assert(ops == &test_ops); |
| 63 |
return -1; |
| 64 |
} |
| 65 |
|
| 66 |
static void wait32(const struct ck_ec_wait_state *state, |
| 67 |
const uint32_t *address, uint32_t expected, |
| 68 |
const struct timespec *deadline) |
| 69 |
{ |
| 70 |
(void)address; |
| 71 |
(void)expected; |
| 72 |
(void)deadline; |
| 73 |
|
| 74 |
assert(state->ops == &test_ops); |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
static void wait64(const struct ck_ec_wait_state *state, |
| 79 |
const uint64_t *address, uint64_t expected, |
| 80 |
const struct timespec *deadline) |
| 81 |
{ |
| 82 |
(void)address; |
| 83 |
(void)expected; |
| 84 |
(void)deadline; |
| 85 |
|
| 86 |
assert(state->ops == &test_ops); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
static void wake32(const struct ck_ec_ops *ops, const uint32_t *address) |
| 91 |
{ |
| 92 |
(void)address; |
| 93 |
|
| 94 |
assert(ops == &test_ops); |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
static void wake64(const struct ck_ec_ops *ops, const uint64_t *address) |
| 99 |
{ |
| 100 |
(void)address; |
| 101 |
|
| 102 |
assert(ops == &test_ops); |
| 103 |
return; |
| 104 |
} |
| 105 |
#else |
| 106 |
#include <linux/futex.h> |
| 107 |
#include <sys/syscall.h> |
| 108 |
#include <time.h> |
| 109 |
#include <unistd.h> |
| 110 |
|
| 111 |
static int gettime(const struct ck_ec_ops *ops, struct timespec *out) |
| 112 |
{ |
| 113 |
assert(ops == &test_ops); |
| 114 |
return clock_gettime(CLOCK_MONOTONIC, out); |
| 115 |
} |
| 116 |
|
| 117 |
static void wait32(const struct ck_ec_wait_state *state, |
| 118 |
const uint32_t *address, uint32_t expected, |
| 119 |
const struct timespec *deadline) |
| 120 |
{ |
| 121 |
assert(state->ops == &test_ops); |
| 122 |
syscall(SYS_futex, address, |
| 123 |
FUTEX_WAIT_BITSET, expected, deadline, |
| 124 |
NULL, FUTEX_BITSET_MATCH_ANY, 0); |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
static void wait64(const struct ck_ec_wait_state *state, |
| 129 |
const uint64_t *address, uint64_t expected, |
| 130 |
const struct timespec *deadline) |
| 131 |
{ |
| 132 |
const void *low_half; |
| 133 |
|
| 134 |
assert(state->ops == &test_ops); |
| 135 |
|
| 136 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 137 |
low_half = address; |
| 138 |
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 139 |
low_half = (uintptr_t)address + sizeof(uint32_t); |
| 140 |
#else |
| 141 |
# error "__BYTE_ORDER__ must be defined." |
| 142 |
#endif |
| 143 |
|
| 144 |
syscall(SYS_futex, low_half, |
| 145 |
FUTEX_WAIT_BITSET, (uint32_t)expected, deadline, |
| 146 |
NULL, FUTEX_BITSET_MATCH_ANY, 0); |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
static void wake32(const struct ck_ec_ops *ops, const uint32_t *address) |
| 151 |
{ |
| 152 |
assert(ops == &test_ops); |
| 153 |
syscall(SYS_futex, address, |
| 154 |
FUTEX_WAKE, INT_MAX, |
| 155 |
/* ignored arguments */NULL, NULL, 0); |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
static void wake64(const struct ck_ec_ops *ops, const uint64_t *address) |
| 160 |
{ |
| 161 |
const void *low_half; |
| 162 |
|
| 163 |
assert(ops == &test_ops); |
| 164 |
|
| 165 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 166 |
low_half = address; |
| 167 |
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 168 |
low_half = (uintptr_t)address + sizeof(uint32_t); |
| 169 |
#else |
| 170 |
# error "__BYTE_ORDER__ must be defined." |
| 171 |
#endif |
| 172 |
|
| 173 |
syscall(SYS_futex, low_half, |
| 174 |
FUTEX_WAKE, INT_MAX, |
| 175 |
/* ignored arguments */NULL, NULL, 0); |
| 176 |
return; |
| 177 |
} |
| 178 |
#endif /* __linux__ */ |
| 179 |
|
| 180 |
static const struct ck_ec_mode sp = { |
| 181 |
.ops = &test_ops, |
| 182 |
.single_producer = true |
| 183 |
}; |
| 184 |
|
| 185 |
static const struct ck_ec_mode mp = { |
| 186 |
.ops = &test_ops, |
| 187 |
.single_producer = false |
| 188 |
}; |
| 189 |
|
| 190 |
static CK_CC_FORCE_INLINE void bench32(const struct ck_ec_mode mode) |
| 191 |
{ |
| 192 |
ck_ec32_t ec CK_CC_CACHELINE = CK_EC_INITIALIZER; |
| 193 |
uint64_t a; |
| 194 |
uint64_t baseline = 1000 * 1000; |
| 195 |
uint32_t value; |
| 196 |
|
| 197 |
for (size_t i = 0; i < STEPS; i++) { |
| 198 |
uint64_t s = rdtsc(); |
| 199 |
uint64_t elapsed = rdtsc() - s; |
| 200 |
|
| 201 |
if (elapsed < baseline) { |
| 202 |
baseline = elapsed; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/* Read value. */ |
| 207 |
a = 0; |
| 208 |
value = 0; |
| 209 |
for (size_t i = 0; i < STEPS / 4; i++) { |
| 210 |
uint64_t s = rdtsc(); |
| 211 |
|
| 212 |
value ^= ck_ec32_value(&ec); |
| 213 |
value ^= ck_ec32_value(&ec); |
| 214 |
value ^= ck_ec32_value(&ec); |
| 215 |
value ^= ck_ec32_value(&ec); |
| 216 |
|
| 217 |
__asm__ volatile("" :: "r"(value)); |
| 218 |
a += rdtsc() - s - baseline; |
| 219 |
} |
| 220 |
|
| 221 |
printf("%s ec32_value: %" PRIu64 "\n", |
| 222 |
(mode.single_producer ? "SP" : "MP"), a / STEPS); |
| 223 |
|
| 224 |
/* Wait (fast path). */ |
| 225 |
a = 0; |
| 226 |
for (size_t i = 0; i < STEPS / 4; i++) { |
| 227 |
uint64_t s = rdtsc(); |
| 228 |
|
| 229 |
ck_ec32_wait(&ec, &mode, 1, NULL); |
| 230 |
ck_ec32_wait(&ec, &mode, 1, NULL); |
| 231 |
ck_ec32_wait(&ec, &mode, 1, NULL); |
| 232 |
ck_ec32_wait(&ec, &mode, 1, NULL); |
| 233 |
|
| 234 |
a += rdtsc() - s - baseline; |
| 235 |
} |
| 236 |
|
| 237 |
printf("%s ec32_wait fast: %" PRIu64 "\n", |
| 238 |
(mode.single_producer ? "SP" : "MP"), a / STEPS); |
| 239 |
|
| 240 |
/* trywait. */ |
| 241 |
a = 0; |
| 242 |
for (size_t i = 0; i < STEPS / 4; i++) { |
| 243 |
struct timespec past = { .tv_sec = 0 }; |
| 244 |
uint64_t s = rdtsc(); |
| 245 |
|
| 246 |
ck_ec32_wait(&ec, &mode, 0, &past); |
| 247 |
ck_ec32_wait(&ec, &mode, 0, &past); |
| 248 |
ck_ec32_wait(&ec, &mode, 0, &past); |
| 249 |
ck_ec32_wait(&ec, &mode, 0, &past); |
| 250 |
|
| 251 |
a += rdtsc() - s - baseline; |
| 252 |
} |
| 253 |
|
| 254 |
printf("%s ec32_wait timeout: %" PRIu64 "\n", |
| 255 |
(mode.single_producer ? "SP" : "MP"), a / STEPS); |
| 256 |
|
| 257 |
/* Inc (no waiter). */ |
| 258 |
assert(!ck_ec32_has_waiters(&ec)); |
| 259 |
a = 0; |
| 260 |
for (size_t i = 0; i < STEPS / 4; i++) { |
| 261 |
uint64_t s = rdtsc(); |
| 262 |
|
| 263 |
ck_ec32_inc(&ec, &mode); |
| 264 |
ck_ec32_inc(&ec, &mode); |
| 265 |
ck_ec32_inc(&ec, &mode); |
| 266 |
ck_ec32_inc(&ec, &mode); |
| 267 |
|
| 268 |
a += rdtsc() - s - baseline; |
| 269 |
} |
| 270 |
|
| 271 |
printf("%s ec32_inc: %" PRIu64 "\n", |
| 272 |
(mode.single_producer ? "SP" : "MP"), a / STEPS); |
| 273 |
|
| 274 |
/* Inc (with waiter). */ |
| 275 |
assert(!ck_ec32_has_waiters(&ec)); |
| 276 |
a = 0; |
| 277 |
for (size_t i = 0; i < STEPS; i++) { |
| 278 |
struct timespec past = { .tv_sec = 1 }; |
| 279 |
uint64_t s; |
| 280 |
|
| 281 |
ck_ec32_wait(&ec, &mode, ck_ec32_value(&ec), &past); |
| 282 |
assert(ck_ec32_has_waiters(&ec)); |
| 283 |
|
| 284 |
s = rdtsc(); |
| 285 |
ck_ec32_inc(&ec, &mode); |
| 286 |
a += rdtsc() - s - baseline; |
| 287 |
} |
| 288 |
|
| 289 |
printf("%s ec32_inc slow: %" PRIu64 "\n", |
| 290 |
(mode.single_producer ? "SP" : "MP"), a / STEPS); |
| 291 |
|
| 292 |
/* Add (no waiter). */ |
| 293 |
assert(!ck_ec32_has_waiters(&ec)); |
| 294 |
a = 0; |
| 295 |
for (size_t i = 0; i < STEPS / 4; i++) { |
| 296 |
uint64_t s = rdtsc(); |
| 297 |
|
| 298 |
ck_ec32_add(&ec, &mode, i + 1); |
| 299 |
ck_ec32_add(&ec, &mode, i + 2); |
| 300 |
ck_ec32_add(&ec, &mode, i + 3); |
| 301 |
ck_ec32_add(&ec, &mode, i + 4); |
| 302 |
|
| 303 |
a += rdtsc() - s - baseline; |
| 304 |
} |
| 305 |
|
| 306 |
printf("%s ec32_add: %" PRIu64 "\n", |
| 307 |
(mode.single_producer ? "SP" : "MP"), a / STEPS); |
| 308 |
|
| 309 |
assert(!ck_ec32_has_waiters(&ec)); |
| 310 |
a = 0; |
| 311 |
for (size_t i = 0; i < STEPS; i++) { |
| 312 |
struct timespec past = { .tv_sec = 1 }; |
| 313 |
uint64_t s; |
| 314 |
|
| 315 |
ck_ec32_wait(&ec, &mode, ck_ec32_value(&ec), &past); |
| 316 |
assert(ck_ec32_has_waiters(&ec)); |
| 317 |
|
| 318 |
s = rdtsc(); |
| 319 |
ck_ec32_add(&ec, &mode, i + 1); |
| 320 |
a += rdtsc() - s - baseline; |
| 321 |
} |
| 322 |
|
| 323 |
printf("%s ec32_add slow: %" PRIu64 "\n", |
| 324 |
(mode.single_producer ? "SP" : "MP"), a / STEPS); |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
#ifdef CK_F_EC64 |
| 329 |
static CK_CC_FORCE_INLINE void bench64(const struct ck_ec_mode mode) |
| 330 |
{ |
| 331 |
ck_ec64_t ec CK_CC_CACHELINE = CK_EC_INITIALIZER; |
| 332 |
uint64_t a; |
| 333 |
uint64_t baseline = 1000 * 1000; |
| 334 |
uint64_t value; |
| 335 |
|
| 336 |
for (size_t i = 0; i < STEPS; i++) { |
| 337 |
uint64_t s = rdtsc(); |
| 338 |
uint64_t elapsed = rdtsc() - s; |
| 339 |
|
| 340 |
if (elapsed < baseline) { |
| 341 |
baseline = elapsed; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
/* Read value. */ |
| 346 |
a = 0; |
| 347 |
value = 0; |
| 348 |
for (size_t i = 0; i < STEPS / 4; i++) { |
| 349 |
uint64_t s = rdtsc(); |
| 350 |
|
| 351 |
value ^= ck_ec64_value(&ec); |
| 352 |
value ^= ck_ec64_value(&ec); |
| 353 |
value ^= ck_ec64_value(&ec); |
| 354 |
value ^= ck_ec64_value(&ec); |
| 355 |
|
| 356 |
__asm__ volatile("" :: "r"(value)); |
| 357 |
a += rdtsc() - s - baseline; |
| 358 |
} |
| 359 |
|
| 360 |
printf("%s ec64_value: %" PRIu64 "\n", |
| 361 |
(mode.single_producer ? "SP" : "MP"), a / STEPS); |
| 362 |
|
| 363 |
/* Wait (fast path). */ |
| 364 |
a = 0; |
| 365 |
for (size_t i = 0; i < STEPS / 4; i++) { |
| 366 |
uint64_t s = rdtsc(); |
| 367 |
|
| 368 |
ck_ec64_wait(&ec, &mode, 1, NULL); |
| 369 |
ck_ec64_wait(&ec, &mode, 1, NULL); |
| 370 |
ck_ec64_wait(&ec, &mode, 1, NULL); |
| 371 |
ck_ec64_wait(&ec, &mode, 1, NULL); |
| 372 |
|
| 373 |
a += rdtsc() - s - baseline; |
| 374 |
} |
| 375 |
|
| 376 |
printf("%s ec64_wait fast: %" PRIu64 "\n", |
| 377 |
(mode.single_producer ? "SP" : "MP"), a / STEPS); |
| 378 |
|
| 379 |
/* trywait. */ |
| 380 |
a = 0; |
| 381 |
for (size_t i = 0; i < STEPS / 4; i++) { |
| 382 |
struct timespec past = { .tv_sec = 0 }; |
| 383 |
uint64_t s = rdtsc(); |
| 384 |
|
| 385 |
ck_ec64_wait(&ec, &mode, 0, &past); |
| 386 |
ck_ec64_wait(&ec, &mode, 0, &past); |
| 387 |
ck_ec64_wait(&ec, &mode, 0, &past); |
| 388 |
ck_ec64_wait(&ec, &mode, 0, &past); |
| 389 |
|
| 390 |
a += rdtsc() - s - baseline; |
| 391 |
} |
| 392 |
|
| 393 |
printf("%s ec64_wait timeout: %" PRIu64 "\n", |
| 394 |
(mode.single_producer ? "SP" : "MP"), a / STEPS); |
| 395 |
|
| 396 |
/* Inc (no waiter). */ |
| 397 |
assert(!ck_ec64_has_waiters(&ec)); |
| 398 |
a = 0; |
| 399 |
for (size_t i = 0; i < STEPS / 4; i++) { |
| 400 |
uint64_t s = rdtsc(); |
| 401 |
|
| 402 |
ck_ec64_inc(&ec, &mode); |
| 403 |
ck_ec64_inc(&ec, &mode); |
| 404 |
ck_ec64_inc(&ec, &mode); |
| 405 |
ck_ec64_inc(&ec, &mode); |
| 406 |
|
| 407 |
a += rdtsc() - s - baseline; |
| 408 |
} |
| 409 |
|
| 410 |
printf("%s ec64_inc: %" PRIu64 "\n", |
| 411 |
(mode.single_producer ? "SP" : "MP"), a / STEPS); |
| 412 |
|
| 413 |
/* Inc (with waiter). */ |
| 414 |
assert(!ck_ec64_has_waiters(&ec)); |
| 415 |
a = 0; |
| 416 |
for (size_t i = 0; i < STEPS; i++) { |
| 417 |
struct timespec past = { .tv_sec = 1 }; |
| 418 |
uint64_t s; |
| 419 |
|
| 420 |
ck_ec64_wait(&ec, &mode, ck_ec64_value(&ec), &past); |
| 421 |
assert(ck_ec64_has_waiters(&ec)); |
| 422 |
|
| 423 |
s = rdtsc(); |
| 424 |
ck_ec64_inc(&ec, &mode); |
| 425 |
a += rdtsc() - s - baseline; |
| 426 |
} |
| 427 |
|
| 428 |
printf("%s ec64_inc slow: %" PRIu64 "\n", |
| 429 |
(mode.single_producer ? "SP" : "MP"), a / STEPS); |
| 430 |
|
| 431 |
/* Add (no waiter). */ |
| 432 |
assert(!ck_ec64_has_waiters(&ec)); |
| 433 |
a = 0; |
| 434 |
for (size_t i = 0; i < STEPS / 4; i++) { |
| 435 |
uint64_t s = rdtsc(); |
| 436 |
|
| 437 |
ck_ec64_add(&ec, &mode, i + 1); |
| 438 |
ck_ec64_add(&ec, &mode, i + 2); |
| 439 |
ck_ec64_add(&ec, &mode, i + 3); |
| 440 |
ck_ec64_add(&ec, &mode, i + 4); |
| 441 |
|
| 442 |
a += rdtsc() - s - baseline; |
| 443 |
} |
| 444 |
|
| 445 |
printf("%s ec64_add: %" PRIu64 "\n", |
| 446 |
(mode.single_producer ? "SP" : "MP"), a / STEPS); |
| 447 |
|
| 448 |
assert(!ck_ec64_has_waiters(&ec)); |
| 449 |
a = 0; |
| 450 |
for (size_t i = 0; i < STEPS; i++) { |
| 451 |
struct timespec past = { .tv_sec = 1 }; |
| 452 |
uint64_t s; |
| 453 |
|
| 454 |
ck_ec64_wait(&ec, &mode, ck_ec64_value(&ec), &past); |
| 455 |
assert(ck_ec64_has_waiters(&ec)); |
| 456 |
|
| 457 |
s = rdtsc(); |
| 458 |
ck_ec64_add(&ec, &mode, i + 1); |
| 459 |
a += rdtsc() - s - baseline; |
| 460 |
} |
| 461 |
|
| 462 |
printf("%s ec64_add slow: %" PRIu64 "\n", |
| 463 |
(mode.single_producer ? "SP" : "MP"), a / STEPS); |
| 464 |
return; |
| 465 |
} |
| 466 |
#endif /* CK_F_EC64 */ |
| 467 |
|
| 468 |
int |
| 469 |
main(void) |
| 470 |
{ |
| 471 |
printf("SP ec32\n"); |
| 472 |
bench32(sp); |
| 473 |
printf("\nMP ec32\n"); |
| 474 |
bench32(mp); |
| 475 |
|
| 476 |
#ifdef CK_F_EC64 |
| 477 |
printf("\nSP ec64\n"); |
| 478 |
bench64(sp); |
| 479 |
printf("\nMP ec64\n"); |
| 480 |
bench64(mp); |
| 481 |
#endif /* CK_F_EC64 */ |
| 482 |
|
| 483 |
return 0; |
| 484 |
} |