Debian Codegraph @

tests/bench/qpmulti.c

raw
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 <math.h>
15 #include <stdbool.h>
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <unistd.h>
19
20 #include <isc/async.h>
21 #include <isc/barrier.h>
22 #include <isc/list.h>
23 #include <isc/log.h>
24 #include <isc/loop.h>
25 #include <isc/magic.h>
26 #include <isc/mem.h>
27 #include <isc/mutex.h>
28 #include <isc/os.h>
29 #include <isc/random.h>
30 #include <isc/refcount.h>
31 #include <isc/rwlock.h>
32 #include <isc/thread.h>
33 #include <isc/tid.h>
34 #include <isc/time.h>
35 #include <isc/timer.h>
36 #include <isc/urcu.h>
37 #include <isc/util.h>
38 #include <isc/uv.h>
39
40 #include <dns/log.h>
41 #include <dns/qp.h>
42 #include <dns/types.h>
43
44 #include "loop_p.h"
45 #include "qp_p.h"
46
47 #include <tests/qp.h>
48
49 #define ITEM_COUNT ((size_t)1000000)
50 #define RUNTIME (0.25 * NS_PER_SEC)
51 #define MAX_OPS_PER_LOOP (1 << 10)
52
53 #define VERBOSE 0
54 #define ZIPF 0
55
56 #if VERBOSE
57 #define TRACE(fmt, ...) \
58 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_QP, \
59 ISC_LOG_DEBUG(7), "%s:%d:%s():t%d: " fmt, __FILE__, \
60 __LINE__, __func__, isc_tid(), ##__VA_ARGS__)
61 #else
62 #define TRACE(...)
63 #endif
64
65 #if ZIPF
66 /*
67 * Zipf rejection sampling derived from code by Jason Crease
68 * https://jasoncrease.medium.com/rejection-sampling-the-zipf-distribution-6b359792cffa
69 */
70 static uint32_t
71 rand_zipf(uint32_t max, double skew) {
72 double s = skew;
73 double t = (pow(max, 1 - s) - s) / (1 - s);
74 for (;;) {
75 double p = t * (double)isc_random32() / UINT32_MAX;
76 double invB = p <= 1 ? p : pow(p * (1 - s) + s, 1 / (1 - s));
77 uint32_t sample = (uint32_t)(invB + 1);
78 double ratio = sample <= 1 ? pow(sample, -s)
79 : pow(sample, -s) / pow(invB, -s);
80 if (ratio > (double)isc_random32() / UINT32_MAX) {
81 return sample - 1;
82 }
83 }
84 }
85 #endif
86
87 static struct {
88 size_t len;
89 bool present;
90 dns_qpkey_t key;
91 } *item;
92
93 static void
94 item_refcount(void *ctx, void *pval, uint32_t ival) {
95 UNUSED(ctx);
96 UNUSED(pval);
97 UNUSED(ival);
98 }
99
100 static size_t
101 item_makekey(dns_qpkey_t key, void *ctx, void *pval, uint32_t ival) {
102 UNUSED(ctx);
103 UNUSED(pval);
104 memmove(key, item[ival].key, item[ival].len);
105 return item[ival].len;
106 }
107
108 static void
109 benchname(void *ctx, char *buf, size_t size) {
110 UNUSED(ctx);
111 strlcpy(buf, "bench", size);
112 }
113
114 const dns_qpmethods_t item_methods = {
115 item_refcount,
116 item_refcount,
117 item_makekey,
118 benchname,
119 };
120
121 static uint8_t
122 random_byte(void) {
123 return isc_random_uniform(SHIFT_OFFSET - SHIFT_NOBYTE) + SHIFT_NOBYTE;
124 }
125
126 static void
127 init_items(isc_mem_t *mctx) {
128 dns_qp_t *qp = NULL;
129 uint64_t start;
130
131 start = isc_time_monotonic();
132 item = isc_mem_callocate(mctx, ITEM_COUNT, sizeof(*item));
133
134 /* ensure there are no duplicate names */
135 dns_qp_create(mctx, &item_methods, NULL, &qp);
136 for (size_t i = 0; i < ITEM_COUNT; i++) {
137 do {
138 size_t len = isc_random_uniform(16) + 4;
139 item[i].len = len;
140 for (size_t off = 0; off < len; off++) {
141 item[i].key[off] = random_byte();
142 }
143 item[i].key[len] = SHIFT_NOBYTE;
144 } while (dns_qp_getkey(qp, item[i].key, item[i].len, NULL,
145 NULL) == ISC_R_SUCCESS);
146 INSIST(dns_qp_insert(qp, &item[i], i) == ISC_R_SUCCESS);
147 }
148 dns_qp_destroy(&qp);
149
150 double time = (double)(isc_time_monotonic() - start) / NS_PER_SEC;
151 printf("%f sec to create %zu items, %f/sec %zu bytes\n", time,
152 ITEM_COUNT, ITEM_COUNT / time, ITEM_COUNT * sizeof(*item));
153 }
154
155 static void
156 init_logging(isc_mem_t *mctx) {
157 isc_result_t result;
158 isc_logdestination_t destination;
159 isc_logconfig_t *logconfig = NULL;
160 isc_log_t *lctx = NULL;
161
162 isc_log_create(mctx, &lctx, &logconfig);
163 isc_log_setcontext(lctx);
164 dns_log_init(lctx);
165 dns_log_setcontext(lctx);
166
167 destination.file.stream = stderr;
168 destination.file.name = NULL;
169 destination.file.versions = ISC_LOG_ROLLNEVER;
170 destination.file.maximum_size = 0;
171 isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC,
172 ISC_LOG_DYNAMIC, &destination,
173 ISC_LOG_PRINTPREFIX | ISC_LOG_PRINTTIME |
174 ISC_LOG_ISO8601);
175 #if VERBOSE
176 isc_log_setdebuglevel(lctx, 7);
177 #endif
178
179 result = isc_log_usechannel(logconfig, "stderr",
180 ISC_LOGCATEGORY_DEFAULT, NULL);
181 INSIST(result == ISC_R_SUCCESS);
182 }
183
184 static void
185 collect(void *);
186
187 struct thread_args {
188 struct bench_state *bctx; /* (in) */
189 isc_barrier_t *barrier; /* (in) */
190 isc_loopmgr_t *loopmgr; /* (in) */
191 isc_job_t job; /* (in) */
192 isc_job_cb cb; /* (in) */
193 dns_qpmulti_t *multi; /* (in) */
194 double zipf_skew; /* (in) */
195 uint32_t max_item; /* (in) */
196 uint32_t ops_per_tx; /* (in) */
197 uint32_t tx_per_loop; /* (in) */
198 uint32_t absent; /* items not found or inserted (out) */
199 uint32_t present; /* items found or deleted (out) */
200 uint32_t compactions; /* (out) */
201 uint64_t transactions; /* (out) */
202 isc_nanosecs_t worked; /* (out) */
203 isc_nanosecs_t start; /* (out) */
204 isc_nanosecs_t stop; /* (out) */
205 };
206
207 static void
208 first_loop(void *varg) {
209 struct thread_args *args = varg;
210 isc_loop_t *loop = isc_loop();
211
212 isc_job_run(loop, &args->job, args->cb, args);
213
214 isc_barrier_wait(args->barrier);
215 args->start = isc_time_monotonic();
216 }
217
218 static void
219 next_loop(struct thread_args *args, isc_nanosecs_t start) {
220 isc_nanosecs_t stop = isc_time_monotonic();
221
222 args->worked += stop - start;
223 args->stop = stop;
224 if (args->stop - args->start < RUNTIME) {
225 isc_job_run(isc_loop(), &args->job, args->cb, args);
226 return;
227 }
228 isc_async_run(isc_loop_main(args->loopmgr), collect, args);
229 }
230
231 #if ZIPF
232 static void
233 read_zipf(void *varg) {
234 struct thread_args *args = varg;
235 isc_nanosecs_t start;
236
237 /* outside time because it is v slow */
238 uint32_t r[args->tx_per_loop][args->ops_per_tx];
239 for (uint32_t tx = 0; tx < args->tx_per_loop; tx++) {
240 for (uint32_t op = 0; op < args->ops_per_tx; op++) {
241 r[tx][op] = rand_zipf(args->max_item, args->zipf_skew);
242 }
243 }
244
245 start = isc_time_monotonic();
246 for (uint32_t tx = 0; tx < args->tx_per_loop; tx++) {
247 args->transactions++;
248 dns_qpread_t qp;
249 dns_qpmulti_query(args->multi, &qp);
250 for (uint32_t op = 0; op < args->ops_per_tx; op++) {
251 uint32_t i = r[tx][op];
252 isc_result_t result = dns_qp_getkey(
253 &qp, item[i].key, item[i].len, NULL, NULL);
254 if (result == ISC_R_SUCCESS) {
255 args->present++;
256 } else {
257 args->absent++;
258 }
259 }
260 dns_qpread_destroy(args->multi, &qp);
261 }
262 next_loop(args, start);
263 }
264 #else
265 #define read_zipf read_transactions
266 #endif
267
268 static void
269 read_transactions(void *varg) {
270 struct thread_args *args = varg;
271 isc_nanosecs_t start = isc_time_monotonic();
272
273 for (uint32_t tx = 0; tx < args->tx_per_loop; tx++) {
274 args->transactions++;
275 dns_qpread_t qp;
276 dns_qpmulti_query(args->multi, &qp);
277 for (uint32_t op = 0; op < args->ops_per_tx; op++) {
278 uint32_t i = isc_random_uniform(args->max_item);
279 isc_result_t result = dns_qp_getkey(
280 &qp, item[i].key, item[i].len, NULL, NULL);
281 if (result == ISC_R_SUCCESS) {
282 args->present++;
283 } else {
284 args->absent++;
285 }
286 }
287 dns_qpread_destroy(args->multi, &qp);
288 }
289 next_loop(args, start);
290 }
291
292 static void
293 mutate_transactions(void *varg) {
294 struct thread_args *args = varg;
295 isc_nanosecs_t start = isc_time_monotonic();
296
297 for (uint32_t tx = 0; tx < args->tx_per_loop; tx++) {
298 dns_qp_t *qp = NULL;
299 dns_qpmulti_write(args->multi, &qp);
300 for (uint32_t op = 0; op < args->ops_per_tx; op++) {
301 uint32_t i = isc_random_uniform(args->max_item);
302 if (item[i].present) {
303 isc_result_t result = dns_qp_deletekey(
304 qp, item[i].key, item[i].len, NULL,
305 NULL);
306 INSIST(result == ISC_R_SUCCESS);
307 item[i].present = false;
308 args->present++;
309 } else {
310 isc_result_t result =
311 dns_qp_insert(qp, &item[i], i);
312 INSIST(result == ISC_R_SUCCESS);
313 item[i].present = true;
314 args->absent++;
315 }
316 }
317 /*
318 * We would normally use DNS_QPGC_MAYBE, but here we do the
319 * fragmented check ourself so we can count compactions
320 */
321 if (dns_qp_memusage(qp).fragmented) {
322 dns_qp_compact(qp, DNS_QPGC_NOW);
323 args->compactions++;
324 }
325 dns_qpmulti_commit(args->multi, &qp);
326 args->transactions++;
327 }
328 next_loop(args, start);
329 }
330
331 enum benchmode {
332 init,
333 vary_max_items_rw,
334 vary_max_items_ro,
335 vary_mut_read,
336 vary_read_only,
337 vary_mut_ops_per_tx,
338 vary_mut_tx_per_loop,
339 vary_read_ops_per_tx_rw,
340 vary_read_ops_per_tx_ro,
341 vary_read_tx_per_loop_rw,
342 vary_read_tx_per_loop_ro,
343 vary_zipf_skew,
344 };
345
346 struct bench_state {
347 isc_mem_t *mctx;
348 isc_barrier_t barrier;
349 isc_loopmgr_t *loopmgr;
350 dns_qpmulti_t *multi;
351 enum benchmode mode;
352 size_t bytes;
353 size_t qp_bytes;
354 size_t qp_items;
355 isc_nanosecs_t load_time;
356 uint32_t nloops;
357 uint32_t waiting;
358 uint32_t max_item;
359 uint32_t mutate;
360 uint32_t mut_ops_per_tx;
361 uint32_t mut_tx_per_loop;
362 uint32_t readers;
363 uint32_t read_ops_per_tx;
364 uint32_t read_tx_per_loop;
365 double zipf_skew;
366 struct thread_args thread[];
367 };
368
369 static void
370 load_multi(struct bench_state *bctx) {
371 dns_qp_t *qp = NULL;
372 size_t count = 0;
373 uint64_t start;
374
375 dns_qpmulti_create(bctx->mctx, &item_methods, NULL, &bctx->multi);
376
377 /* initial contents of the trie */
378 start = isc_time_monotonic();
379 dns_qpmulti_update(bctx->multi, &qp);
380 for (size_t i = 0; i < bctx->max_item; i++) {
381 if (isc_random_uniform(2) == 0) {
382 item[i].present = false;
383 continue;
384 }
385 INSIST(dns_qp_insert(qp, &item[i], i) == ISC_R_SUCCESS);
386 item[i].present = true;
387 count++;
388 }
389 dns_qp_compact(qp, DNS_QPGC_ALL);
390 dns_qpmulti_commit(bctx->multi, &qp);
391
392 bctx->load_time = isc_time_monotonic() - start;
393 bctx->qp_bytes = dns_qpmulti_memusage(bctx->multi).bytes;
394 bctx->qp_items = count;
395 }
396
397 static void
398 tsv_header(void) {
399 printf("runtime\t");
400 printf("elapsed\t");
401 printf(" load s\t");
402 printf(" B/item\t");
403 printf(" items\t");
404
405 printf(" mut\t");
406 printf("tx/loop\t");
407 printf(" ops/tx\t");
408 printf(" gc\t");
409 printf(" txns\t");
410 printf(" ops\t");
411 printf(" work s\t");
412 printf("txns/us\t");
413 printf(" ops/us\t");
414
415 printf(" read\t");
416 printf("tx/loop\t");
417 printf(" ops/tx\t");
418 printf(" Ktxns\t");
419 printf(" Kops\t");
420 printf(" work s\t");
421 printf("txns/us\t");
422 printf(" ops/us\t");
423 printf(" raw\t");
424 printf(" loop\n");
425 }
426
427 /*
428 * This function sets up the parameters for each benchmark run and
429 * dispatches the work to the event loops. Each run is part of a
430 * series, where most of the parameters are fixed and one parameter is
431 * varied. The layout here is somewhat eccentric, in order to keep
432 * each series together.
433 *
434 * A series starts with an `init` block, which sets up the constant
435 * parameters and the variable parameter for the first run. Following
436 * the `init` block is a `case` label which adjusts the variable
437 * parameter for each subsequent run in the series, and checks when
438 * the series is finished. At the end of the series, we `goto` the
439 * `init` label for the next series.
440 */
441 static void
442 dispatch(struct bench_state *bctx) {
443 switch (bctx->mode) {
444 case init:
445 goto init_max_items_rw;
446
447 fini:;
448 isc_loopmgr_t *loopmgr = bctx->loopmgr;
449 dns_qpmulti_destroy(&bctx->multi);
450 isc_mem_putanddetach(&bctx->mctx, bctx, bctx->bytes);
451 isc_loopmgr_shutdown(loopmgr);
452 return;
453
454 init_max_items_rw:
455 bctx->mode = vary_max_items_rw;
456 printf("\n");
457 printf("vary size of trie\n");
458 tsv_header();
459 bctx->mutate = 1;
460 bctx->readers = bctx->nloops - 1;
461 bctx->mut_ops_per_tx = 4;
462 bctx->mut_tx_per_loop = 4;
463 bctx->read_ops_per_tx = 32;
464 bctx->read_tx_per_loop = 32;
465 bctx->max_item = 10;
466 load_multi(bctx);
467 break;
468
469 case vary_max_items_rw:
470 if (bctx->max_item == ITEM_COUNT) {
471 goto init_max_items_ro;
472 } else {
473 dns_qpmulti_destroy(&bctx->multi);
474 bctx->max_item *= 10;
475 load_multi(bctx);
476 }
477 break;
478
479 init_max_items_ro:
480 bctx->mode = vary_max_items_ro;
481 printf("\n");
482 printf("vary size of trie (readonly)\n");
483 tsv_header();
484 bctx->mutate = 0;
485 bctx->readers = bctx->nloops;
486 bctx->mut_ops_per_tx = 4;
487 bctx->mut_tx_per_loop = 4;
488 bctx->read_ops_per_tx = 32;
489 bctx->read_tx_per_loop = 32;
490 dns_qpmulti_destroy(&bctx->multi);
491 bctx->max_item = 10;
492 load_multi(bctx);
493 break;
494 case vary_max_items_ro:
495 if (bctx->max_item == ITEM_COUNT) {
496 goto init_zipf_skew;
497 } else {
498 dns_qpmulti_destroy(&bctx->multi);
499 bctx->max_item *= 10;
500 load_multi(bctx);
501 }
502 break;
503
504 init_zipf_skew:
505 bctx->mode = vary_zipf_skew;
506 printf("\n");
507 printf("vary zipf skew (readonly) "
508 " [ cache friendliness? ]\n");
509 tsv_header();
510 bctx->mutate = 0;
511 bctx->readers = 0;
512 bctx->mut_ops_per_tx = 4;
513 bctx->mut_tx_per_loop = 4;
514 bctx->read_ops_per_tx = 32;
515 bctx->read_tx_per_loop = 32;
516 bctx->zipf_skew = 0.01;
517 /* dumb hack */
518 bctx->load_time = bctx->zipf_skew * NS_PER_SEC;
519 break;
520 case vary_zipf_skew:
521 bctx->zipf_skew += 0.1;
522 bctx->load_time = bctx->zipf_skew * NS_PER_SEC;
523 if (bctx->zipf_skew >= 1.0) {
524 bctx->zipf_skew = 0.0;
525 bctx->load_time = 0;
526 goto init_mut_read;
527 }
528 break;
529
530 init_mut_read:
531 bctx->mode = vary_mut_read;
532 printf("\n");
533 printf("vary mutate / read threads "
534 "[ read perf per thread should be flat ]\n");
535 tsv_header();
536 bctx->mutate = bctx->nloops - 1;
537 bctx->readers = 1;
538 bctx->mut_ops_per_tx = 4;
539 bctx->mut_tx_per_loop = 4;
540 bctx->read_ops_per_tx = 32;
541 bctx->read_tx_per_loop = 32;
542 break;
543 case vary_mut_read:
544 if (bctx->mutate == 1) {
545 goto init_read_only;
546 } else {
547 bctx->mutate--;
548 bctx->readers++;
549 }
550 break;
551
552 init_read_only:
553 bctx->mode = vary_read_only;
554 printf("\n");
555 printf("vary read threads "
556 "[ read perf per thread should be flat ]\n");
557 tsv_header();
558 bctx->mutate = 0;
559 bctx->readers = 1;
560 bctx->mut_ops_per_tx = 4;
561 bctx->mut_tx_per_loop = 4;
562 bctx->read_ops_per_tx = 32;
563 bctx->read_tx_per_loop = 32;
564 break;
565 case vary_read_only:
566 if (bctx->readers == bctx->nloops) {
567 goto init_mut_ops_per_tx;
568 } else {
569 bctx->readers++;
570 }
571 break;
572
573 init_mut_ops_per_tx:
574 bctx->mode = vary_mut_ops_per_tx;
575 printf("\n");
576 printf("vary mutate operations per transaction "
577 "[ mutate activity affects read perf? ]\n");
578 tsv_header();
579 bctx->mutate = 1;
580 bctx->readers = bctx->nloops - 1;
581 bctx->mut_ops_per_tx = 1;
582 bctx->mut_tx_per_loop = 1;
583 bctx->read_ops_per_tx = 32;
584 bctx->read_tx_per_loop = 32;
585 break;
586 case vary_mut_ops_per_tx:
587 if (bctx->mut_ops_per_tx * bctx->mut_tx_per_loop ==
588 MAX_OPS_PER_LOOP)
589 {
590 goto init_mut_tx_per_loop;
591 } else {
592 bctx->mut_ops_per_tx *= 2;
593 }
594 break;
595
596 init_mut_tx_per_loop:
597 bctx->mode = vary_mut_tx_per_loop;
598 printf("\n");
599 printf("vary mutate transactions per loop "
600 "[ mutate activity affects read perf? ]\n");
601 tsv_header();
602 bctx->mutate = 1;
603 bctx->readers = bctx->nloops - 1;
604 bctx->mut_ops_per_tx = 4;
605 bctx->mut_tx_per_loop = 1;
606 bctx->read_ops_per_tx = 32;
607 bctx->read_tx_per_loop = 32;
608 break;
609 case vary_mut_tx_per_loop:
610 if (bctx->mut_ops_per_tx * bctx->mut_tx_per_loop ==
611 MAX_OPS_PER_LOOP)
612 {
613 goto init_read_tx_per_loop_rw;
614 } else {
615 bctx->mut_tx_per_loop *= 2;
616 }
617 break;
618
619 init_read_tx_per_loop_rw:
620 bctx->mode = vary_read_tx_per_loop_rw;
621 printf("\n");
622 printf("vary read transactions per loop "
623 "[ loop overhead? ]\n");
624 tsv_header();
625 bctx->mutate = 1;
626 bctx->readers = bctx->nloops - 1;
627 bctx->mut_ops_per_tx = 4;
628 bctx->mut_tx_per_loop = 4;
629 bctx->read_ops_per_tx = 4;
630 bctx->read_tx_per_loop = 1;
631 break;
632 case vary_read_tx_per_loop_rw:
633 if (bctx->read_ops_per_tx * bctx->read_tx_per_loop ==
634 MAX_OPS_PER_LOOP)
635 {
636 goto init_read_tx_per_loop_ro;
637 } else {
638 bctx->read_tx_per_loop *= 2;
639 }
640 break;
641
642 init_read_tx_per_loop_ro:
643 bctx->mode = vary_read_tx_per_loop_ro;
644 printf("\n");
645 printf("vary read transactions per loop (readonly) "
646 "[ loop overhead? ]\n");
647 tsv_header();
648 bctx->mutate = 0;
649 bctx->readers = bctx->nloops;
650 bctx->mut_ops_per_tx = 4;
651 bctx->mut_tx_per_loop = 4;
652 bctx->read_ops_per_tx = 4;
653 bctx->read_tx_per_loop = 1;
654 break;
655 case vary_read_tx_per_loop_ro:
656 if (bctx->read_ops_per_tx * bctx->read_tx_per_loop ==
657 MAX_OPS_PER_LOOP)
658 {
659 goto init_read_ops_per_tx_rw;
660 } else {
661 bctx->read_tx_per_loop *= 2;
662 }
663 break;
664
665 init_read_ops_per_tx_rw:
666 bctx->mode = vary_read_ops_per_tx_rw;
667 printf("\n");
668 printf("vary read operations per transaction "
669 " [ transaction overhead should be small ]\n");
670 tsv_header();
671 bctx->mutate = 1;
672 bctx->readers = bctx->nloops - 1;
673 bctx->mut_ops_per_tx = 4;
674 bctx->mut_tx_per_loop = 4;
675 bctx->read_ops_per_tx = 1;
676 bctx->read_tx_per_loop = MAX_OPS_PER_LOOP;
677 break;
678 case vary_read_ops_per_tx_rw:
679 if (bctx->read_ops_per_tx == MAX_OPS_PER_LOOP) {
680 goto init_read_ops_per_tx_ro;
681 } else {
682 bctx->read_ops_per_tx *= 2;
683 bctx->read_tx_per_loop /= 2;
684 }
685 break;
686
687 init_read_ops_per_tx_ro:
688 bctx->mode = vary_read_ops_per_tx_ro;
689 printf("\n");
690 printf("vary read operations per transaction (readonly) "
691 " [ transaction overhead should be small ]\n");
692 tsv_header();
693 bctx->mutate = 0;
694 bctx->readers = bctx->nloops;
695 bctx->mut_ops_per_tx = 0;
696 bctx->mut_tx_per_loop = 0;
697 bctx->read_ops_per_tx = 1;
698 bctx->read_tx_per_loop = MAX_OPS_PER_LOOP;
699 break;
700 case vary_read_ops_per_tx_ro:
701 if (bctx->read_ops_per_tx == MAX_OPS_PER_LOOP) {
702 goto fini;
703 } else {
704 bctx->read_ops_per_tx *= 2;
705 bctx->read_tx_per_loop /= 2;
706 }
707 break;
708 }
709
710 /* dispatch a benchmark run */
711
712 bool zipf = bctx->mutate == 0 && bctx->readers == 0;
713 bctx->waiting = zipf ? bctx->nloops : bctx->readers + bctx->mutate;
714 isc_barrier_init(&bctx->barrier, bctx->waiting);
715 for (uint32_t t = 0; t < bctx->waiting; t++) {
716 bool mut = t < bctx->mutate;
717 bctx->thread[t] = (struct thread_args){
718 .bctx = bctx,
719 .barrier = &bctx->barrier,
720 .loopmgr = bctx->loopmgr,
721 .multi = bctx->multi,
722 .max_item = bctx->max_item,
723 .zipf_skew = bctx->zipf_skew,
724 .cb = zipf ? read_zipf
725 : mut ? mutate_transactions
726 : read_transactions,
727 .job = ISC_JOB_INITIALIZER,
728 .ops_per_tx = mut ? bctx->mut_ops_per_tx
729 : bctx->read_ops_per_tx,
730 .tx_per_loop = mut ? bctx->mut_tx_per_loop
731 : bctx->read_tx_per_loop,
732 };
733 isc_async_run(isc_loop_get(bctx->loopmgr, t), first_loop,
734 &bctx->thread[t]);
735 }
736 }
737
738 static void
739 collect(void *varg) {
740 struct thread_args *args = varg;
741 struct bench_state *bctx = args->bctx;
742 struct thread_args *thread = bctx->thread;
743 struct {
744 uint64_t worked, txns, ops, compactions;
745 } stats[2] = {};
746 double load_time = bctx->load_time;
747 double elapsed = 0, mut_work, readers, read_work, elapsed_ms;
748 uint32_t nloops;
749 bool zipf;
750
751 TRACE("collect");
752
753 bctx->waiting--;
754 if (bctx->waiting > 0) {
755 return;
756 }
757 isc_barrier_destroy(&bctx->barrier);
758
759 load_time = load_time > 0 ? load_time / (double)NS_PER_SEC : NAN;
760
761 zipf = bctx->mutate == 0 && bctx->readers == 0;
762 nloops = zipf ? bctx->nloops : bctx->readers + bctx->mutate;
763 for (uint32_t t = 0; t < nloops; t++) {
764 struct thread_args *tp = &thread[t];
765 elapsed = ISC_MAX(elapsed, (tp->stop - tp->start));
766 bool mut = t < bctx->mutate;
767
768 stats[mut].worked += tp->worked;
769 stats[mut].txns += tp->transactions;
770 stats[mut].ops += tp->transactions * tp->ops_per_tx;
771 stats[mut].compactions += tp->compactions;
772 }
773
774 printf("%7.3f\t", RUNTIME / (double)NS_PER_SEC);
775 printf("%7.3f\t", elapsed / (double)NS_PER_SEC);
776 printf("%7.3f\t", load_time);
777 printf("%7.2f\t", (double)bctx->qp_bytes / bctx->qp_items);
778 printf("%7u\t", bctx->max_item);
779
780 mut_work = stats[1].worked / (double)US_PER_MS;
781 printf("%7u\t", bctx->mutate);
782 printf("%7u\t", bctx->mut_tx_per_loop);
783 printf("%7u\t", bctx->mut_ops_per_tx);
784 printf("%7llu\t", (unsigned long long)stats[1].compactions);
785 printf("%7llu\t", (unsigned long long)stats[1].txns);
786 printf("%7llu\t", (unsigned long long)stats[1].ops);
787 printf("%7.2f\t", stats[1].worked / (double)NS_PER_SEC);
788 printf("%7.2f\t", stats[1].txns / mut_work);
789 printf("%7.2f\t", stats[1].ops / mut_work);
790
791 readers = zipf ? bctx->nloops - bctx->mutate : bctx->readers;
792 read_work = stats[0].worked / (double)US_PER_MS;
793 elapsed_ms = elapsed / (double)US_PER_MS;
794 printf("%7u\t", bctx->readers);
795 printf("%7u\t", bctx->read_tx_per_loop);
796 printf("%7u\t", bctx->read_ops_per_tx);
797 printf("%7llu\t", (unsigned long long)stats[0].txns / 1000);
798 printf("%7llu\t", (unsigned long long)stats[0].ops / 1000);
799 printf("%7.2f\t", stats[0].worked / (double)NS_PER_SEC);
800 printf("%7.2f\t", stats[0].txns / read_work);
801 printf("%7.2f\t", stats[0].ops / read_work);
802 printf("%7.2f\t", stats[0].ops * readers / read_work);
803 printf("%7.2f\n", stats[0].ops / elapsed_ms);
804
805 dispatch(bctx);
806 }
807
808 static void
809 startup(void *arg) {
810 isc_loopmgr_t *loopmgr = arg;
811 isc_loop_t *loop = isc_loop();
812 isc_mem_t *mctx = isc_loop_getmctx(loop);
813 uint32_t nloops = isc_loopmgr_nloops(loopmgr);
814 size_t bytes = sizeof(struct bench_state) +
815 sizeof(struct thread_args) * nloops;
816 struct bench_state *bctx = isc_mem_cget(mctx, 1, bytes);
817
818 *bctx = (struct bench_state){
819 .loopmgr = loopmgr,
820 .bytes = bytes,
821 .nloops = nloops,
822 };
823 isc_mem_attach(mctx, &bctx->mctx);
824
825 dispatch(bctx);
826 }
827
828 struct ticker {
829 isc_loopmgr_t *loopmgr;
830 isc_mem_t *mctx;
831 isc_timer_t *timer;
832 };
833
834 static void
835 tick(void *varg) {
836 /* just make the loop cycle */
837 UNUSED(varg);
838 }
839
840 static void
841 start_ticker(void *varg) {
842 struct ticker *ticker = varg;
843 isc_loop_t *loop = isc_loop();
844
845 isc_timer_create(loop, tick, NULL, &ticker->timer);
846 isc_timer_start(ticker->timer, isc_timertype_ticker,
847 &(isc_interval_t){
848 .seconds = 0,
849 .nanoseconds = 1 * NS_PER_MS,
850 });
851 }
852
853 static void
854 stop_ticker(void *varg) {
855 struct ticker *ticker = varg;
856
857 isc_timer_stop(ticker->timer);
858 isc_timer_destroy(&ticker->timer);
859 isc_mem_putanddetach(&ticker->mctx, ticker, sizeof(*ticker));
860 }
861
862 static void
863 setup_tickers(isc_mem_t *mctx, isc_loopmgr_t *loopmgr) {
864 uint32_t nloops = isc_loopmgr_nloops(loopmgr);
865 for (uint32_t i = 0; i < nloops; i++) {
866 isc_loop_t *loop = isc_loop_get(loopmgr, i);
867 struct ticker *ticker = isc_mem_get(mctx, sizeof(*ticker));
868 *ticker = (struct ticker){
869 .loopmgr = loopmgr,
870 };
871 isc_mem_attach(mctx, &ticker->mctx);
872 isc_loop_setup(loop, start_ticker, ticker);
873 isc_loop_teardown(loop, stop_ticker, ticker);
874 }
875 }
876
877 int
878 main(void) {
879 isc_loopmgr_t *loopmgr = NULL;
880 isc_mem_t *mctx = NULL;
881
882 setlinebuf(stdout);
883
884 uint32_t nloops;
885 const char *env_workers = getenv("ISC_TASK_WORKERS");
886
887 if (env_workers != NULL) {
888 nloops = atoi(env_workers);
889 } else {
890 nloops = isc_os_ncpus();
891 }
892 INSIST(nloops > 1);
893
894 isc_mem_create(&mctx);
895 isc_mem_setdestroycheck(mctx, true);
896 init_logging(mctx);
897 init_items(mctx);
898
899 isc_loopmgr_create(mctx, nloops, &loopmgr);
900 setup_tickers(mctx, loopmgr);
901 isc_loop_setup(isc_loop_main(loopmgr), startup, loopmgr);
902 isc_loopmgr_run(loopmgr);
903 isc_loopmgr_destroy(&loopmgr);
904
905 isc_log_destroy(&dns_lctx);
906 isc_mem_free(mctx, item);
907 isc_mem_checkdestroyed(stdout);
908 isc_mem_destroy(&mctx);
909
910 return 0;
911 }