Debian Codegraph @

tests/bench/load-names.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 <assert.h>
15 #include <stdlib.h>
16
17 #include <isc/barrier.h>
18 #include <isc/file.h>
19 #include <isc/hashmap.h>
20 #include <isc/ht.h>
21 #include <isc/list.h>
22 #include <isc/rwlock.h>
23 #include <isc/thread.h>
24 #include <isc/urcu.h>
25 #include <isc/util.h>
26
27 #include <dns/fixedname.h>
28 #include <dns/qp.h>
29 #include <dns/rbt.h>
30 #include <dns/types.h>
31
32 #include "qp_p.h"
33
34 #include <tests/dns.h>
35 #include <tests/qp.h>
36
37 struct item_s {
38 const char *text;
39 dns_fixedname_t fixed;
40 struct cds_lfht_node ht_node;
41 } item[1024 * 1024];
42
43 isc_barrier_t barrier;
44 isc_rwlock_t rwl;
45
46 struct thread_s {
47 isc_thread_t thread;
48 struct fun *fun;
49 void *map;
50 size_t start;
51 size_t end;
52 uint64_t d0;
53 uint64_t d1;
54 } threads[1024];
55
56 static void
57 item_check(void *ctx, void *pval, uint32_t ival) {
58 UNUSED(ctx);
59 assert(pval == &item[ival]);
60 }
61
62 static size_t
63 item_makekey(dns_qpkey_t key, void *ctx, void *pval, uint32_t ival) {
64 UNUSED(ctx);
65 assert(pval == &item[ival]);
66 return dns_qpkey_fromname(key, &item[ival].fixed.name);
67 }
68
69 static void
70 testname(void *ctx, char *buf, size_t size) {
71 REQUIRE(ctx == NULL);
72 strlcpy(buf, "test", size);
73 }
74
75 const dns_qpmethods_t qpmethods = {
76 item_check,
77 item_check,
78 item_makekey,
79 testname,
80 };
81
82 #define CHECK(count, result) \
83 do { \
84 if (result != ISC_R_SUCCESS) { \
85 dns_name_t *name = &item[count].fixed.name; \
86 char buf[DNS_NAME_MAXTEXT] = { 0 }; \
87 dns_name_format(name, buf, sizeof(buf)); \
88 fprintf(stderr, "%s: %s\n", buf, \
89 isc_result_totext(result)); \
90 exit(EXIT_FAILURE); \
91 } \
92 } while (0)
93
94 struct fun {
95 const char *name;
96 void *(*new)(isc_mem_t *mem);
97 isc_threadfunc_t thread;
98 };
99
100 /*
101 * cds_lfht
102 */
103
104 static void *
105 new_lfht(isc_mem_t *mem ISC_ATTR_UNUSED) {
106 struct cds_lfht *lfht = cds_lfht_new(
107 1, 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
108 return lfht;
109 }
110
111 static int
112 lfht_match(struct cds_lfht_node *ht_node, const void *_key) {
113 const struct item_s *i = caa_container_of(ht_node, struct item_s,
114 ht_node);
115 const dns_name_t *key = _key;
116
117 return dns_name_equal(key, &i->fixed.name);
118 }
119
120 static isc_result_t
121 add_lfht(void *lfht, size_t count) {
122 unsigned long hash = dns_name_hash(&item[count].fixed.name);
123
124 struct cds_lfht_node *ht_node = cds_lfht_add_unique(
125 lfht, hash, lfht_match, &item[count].fixed.name,
126 &item[count].ht_node);
127
128 if (ht_node != &item[count].ht_node) {
129 return ISC_R_EXISTS;
130 }
131
132 return ISC_R_SUCCESS;
133 }
134
135 static isc_result_t
136 get_lfht(void *lfht, size_t count, void **pval) {
137 unsigned long hash = dns_name_hash(&item[count].fixed.name);
138
139 struct cds_lfht_iter iter;
140 cds_lfht_lookup(lfht, hash, lfht_match, &item[count].fixed.name, &iter);
141
142 struct cds_lfht_node *ht_node = cds_lfht_iter_get_node(&iter);
143 if (ht_node == NULL) {
144 return ISC_R_NOTFOUND;
145 }
146
147 *pval = caa_container_of(ht_node, struct item_s, ht_node);
148 return ISC_R_SUCCESS;
149 }
150
151 static void *
152 thread_lfht(void *arg0) {
153 struct thread_s *arg = arg0;
154
155 isc_barrier_wait(&barrier);
156
157 isc_time_t t0 = isc_time_now_hires();
158 for (size_t n = arg->start; n < arg->end; n++) {
159 isc_result_t result = add_lfht(arg->map, n);
160 CHECK(n, result);
161 }
162
163 isc_time_t t1 = isc_time_now_hires();
164 for (size_t n = arg->start; n < arg->end; n++) {
165 void *pval = NULL;
166 isc_result_t result = get_lfht(arg->map, n, &pval);
167 CHECK(n, result);
168 assert(pval == &item[n]);
169 }
170
171 isc_time_t t2 = isc_time_now_hires();
172
173 arg->d0 = isc_time_microdiff(&t1, &t0);
174 arg->d1 = isc_time_microdiff(&t2, &t1);
175
176 return NULL;
177 }
178
179 /*
180 * hashmap
181 */
182
183 static void *
184 new_hashmap(isc_mem_t *mem) {
185 isc_hashmap_t *hashmap = NULL;
186 isc_hashmap_create(mem, 1, &hashmap);
187
188 return hashmap;
189 }
190
191 static bool
192 name_match(void *node, const void *key) {
193 const struct item_s *i = node;
194 return dns_name_equal(&i->fixed.name, key);
195 }
196
197 static isc_result_t
198 add_hashmap(void *hashmap, size_t count) {
199 isc_result_t result = isc_hashmap_add(
200 hashmap, dns_name_hash(&item[count].fixed.name), name_match,
201 &item[count].fixed.name, &item[count], NULL);
202 return result;
203 }
204
205 static isc_result_t
206 get_hashmap(void *hashmap, size_t count, void **pval) {
207 isc_result_t result = isc_hashmap_find(
208 hashmap, dns_name_hash(&item[count].fixed.name), name_match,
209 &item[count].fixed.name, pval);
210 return result;
211 }
212
213 static void *
214 thread_hashmap(void *arg0) {
215 struct thread_s *arg = arg0;
216
217 isc_barrier_wait(&barrier);
218
219 isc_time_t t0 = isc_time_now_hires();
220 WRLOCK(&rwl);
221 for (size_t n = arg->start; n < arg->end; n++) {
222 isc_result_t result = add_hashmap(arg->map, n);
223 CHECK(n, result);
224 }
225 WRUNLOCK(&rwl);
226
227 isc_time_t t1 = isc_time_now_hires();
228 RDLOCK(&rwl);
229 for (size_t n = arg->start; n < arg->end; n++) {
230 void *pval = NULL;
231 isc_result_t result = get_hashmap(arg->map, n, &pval);
232 CHECK(n, result);
233 assert(pval == &item[n]);
234 }
235 RDUNLOCK(&rwl);
236 isc_time_t t2 = isc_time_now_hires();
237
238 arg->d0 = isc_time_microdiff(&t1, &t0);
239 arg->d1 = isc_time_microdiff(&t2, &t1);
240
241 return NULL;
242 }
243
244 /*
245 * ht
246 */
247
248 static void *
249 new_ht(isc_mem_t *mem) {
250 isc_ht_t *ht = NULL;
251 isc_ht_init(&ht, mem, 1, 0);
252 return ht;
253 }
254
255 static isc_result_t
256 add_ht(void *ht, size_t count) {
257 isc_result_t result = isc_ht_add(ht, item[count].fixed.name.ndata,
258 item[count].fixed.name.length,
259 &item[count]);
260 return result;
261 }
262
263 static isc_result_t
264 get_ht(void *ht, size_t count, void **pval) {
265 isc_result_t result = isc_ht_find(ht, item[count].fixed.name.ndata,
266 item[count].fixed.name.length, pval);
267 return result;
268 }
269
270 static void *
271 thread_ht(void *arg0) {
272 struct thread_s *arg = arg0;
273
274 isc_barrier_wait(&barrier);
275
276 isc_time_t t0 = isc_time_now_hires();
277 WRLOCK(&rwl);
278 for (size_t n = arg->start; n < arg->end; n++) {
279 isc_result_t result = add_ht(arg->map, n);
280 CHECK(n, result);
281 }
282 WRUNLOCK(&rwl);
283
284 isc_time_t t1 = isc_time_now_hires();
285 RDLOCK(&rwl);
286 for (size_t n = arg->start; n < arg->end; n++) {
287 void *pval = NULL;
288 isc_result_t result = get_ht(arg->map, n, &pval);
289 CHECK(n, result);
290 assert(pval == &item[n]);
291 }
292 RDUNLOCK(&rwl);
293 isc_time_t t2 = isc_time_now_hires();
294
295 arg->d0 = isc_time_microdiff(&t1, &t0);
296 arg->d1 = isc_time_microdiff(&t2, &t1);
297
298 return NULL;
299 }
300
301 /*
302 * rbt
303 */
304
305 static void *
306 new_rbt(isc_mem_t *mem) {
307 dns_rbt_t *rbt = NULL;
308 (void)dns_rbt_create(mem, NULL, NULL, &rbt);
309 return rbt;
310 }
311
312 static isc_result_t
313 add_rbt(void *rbt, size_t count) {
314 isc_result_t result;
315 dns_rbtnode_t *node = NULL;
316
317 result = dns_rbt_addnode(rbt, &item[count].fixed.name, &node);
318 if (result == ISC_R_SUCCESS ||
319 (result == ISC_R_EXISTS && node->data == NULL))
320 {
321 node->data = &item[count];
322 result = ISC_R_SUCCESS;
323 }
324
325 return result;
326 }
327
328 static isc_result_t
329 get_rbt(void *rbt, size_t count, void **pval) {
330 isc_result_t result;
331 dns_rbtnode_t *node = NULL;
332
333 result = dns_rbt_findnode(rbt, &item[count].fixed.name, NULL, &node,
334 NULL, 0, NULL, NULL);
335 if (result == ISC_R_SUCCESS) {
336 *pval = node->data;
337 }
338 return result;
339 }
340
341 static void *
342 thread_rbt(void *arg0) {
343 struct thread_s *arg = arg0;
344
345 isc_barrier_wait(&barrier);
346
347 isc_time_t t0 = isc_time_now_hires();
348 WRLOCK(&rwl);
349 for (size_t n = arg->start; n < arg->end; n++) {
350 isc_result_t result = add_rbt(arg->map, n);
351 CHECK(n, result);
352 }
353 WRUNLOCK(&rwl);
354
355 isc_time_t t1 = isc_time_now_hires();
356 RDLOCK(&rwl);
357 for (size_t n = arg->start; n < arg->end; n++) {
358 void *pval = NULL;
359 isc_result_t result = get_rbt(arg->map, n, &pval);
360 CHECK(n, result);
361 assert(pval == &item[n]);
362 }
363 RDUNLOCK(&rwl);
364
365 isc_time_t t2 = isc_time_now_hires();
366
367 arg->d0 = isc_time_microdiff(&t1, &t0);
368 arg->d1 = isc_time_microdiff(&t2, &t1);
369
370 return NULL;
371 }
372
373 /*
374 * qp
375 */
376
377 static void *
378 new_qp(isc_mem_t *mem) {
379 dns_qpmulti_t *qpmulti = NULL;
380 dns_qpmulti_create(mem, &qpmethods, NULL, &qpmulti);
381 return qpmulti;
382 }
383
384 static isc_result_t
385 add_qp(void *qp, size_t count) {
386 isc_result_t result = dns_qp_insert(qp, &item[count], count);
387 return result;
388 }
389
390 static void
391 sqz_qp(void *qp) {
392 dns_qp_compact(qp, DNS_QPGC_MAYBE);
393 }
394
395 static isc_result_t
396 get_qp(void *qp, size_t count, void **pval) {
397 return dns_qp_getname(qp, &item[count].fixed.name, pval, NULL);
398 }
399
400 static void *
401 _thread_qp(void *arg0, bool sqz, bool brr) {
402 struct thread_s *arg = arg0;
403
404 isc_barrier_wait(&barrier);
405
406 dns_qp_t *qp = NULL;
407 dns_qpmulti_write(arg->map, &qp);
408
409 isc_time_t t0 = isc_time_now_hires();
410 for (size_t n = arg->start; n < arg->end; n++) {
411 isc_result_t result = add_qp(qp, n);
412 CHECK(n, result);
413 }
414 if (sqz) {
415 sqz_qp(qp);
416 }
417 dns_qpmulti_commit(arg->map, &qp);
418 if (brr) {
419 rcu_barrier();
420 }
421
422 isc_time_t t1 = isc_time_now_hires();
423
424 dns_qpread_t qpr;
425 dns_qpmulti_query(arg->map, &qpr);
426
427 for (size_t n = arg->start; n < arg->end; n++) {
428 void *pval = NULL;
429 isc_result_t result = get_qp(&qpr, n, &pval);
430 CHECK(n, result);
431 assert(pval == &item[n]);
432 }
433
434 dns_qpread_destroy(arg->map, &qpr);
435
436 isc_time_t t2 = isc_time_now_hires();
437
438 arg->d0 = isc_time_microdiff(&t1, &t0);
439 arg->d1 = isc_time_microdiff(&t2, &t1);
440
441 return NULL;
442 }
443
444 static void *
445 thread_qp(void *arg0) {
446 return _thread_qp(arg0, true, false);
447 }
448
449 static void *
450 thread_qp_nosqz(void *arg0) {
451 return _thread_qp(arg0, false, false);
452 }
453
454 static void *
455 thread_qp_brr(void *arg0) {
456 return _thread_qp(arg0, true, true);
457 }
458
459 /*
460 * fun table
461 */
462 static struct fun fun_list[] = {
463 { "lfht", new_lfht, thread_lfht },
464 { "ht", new_ht, thread_ht },
465 { "hashmap", new_hashmap, thread_hashmap },
466 { "rbt", new_rbt, thread_rbt },
467 { "qp", new_qp, thread_qp },
468 { "qp+nosqz", new_qp, thread_qp_nosqz },
469 { "qp+barrier", new_qp, thread_qp_brr },
470 { NULL, NULL, NULL },
471 };
472
473 #define FILE_CHECK(check, msg) \
474 do { \
475 if (!(check)) { \
476 fprintf(stderr, "%s:%zu: %s\n", filename, lines, msg); \
477 exit(EXIT_FAILURE); \
478 } \
479 } while (0)
480
481 int
482 main(int argc, char *argv[]) {
483 isc_result_t result;
484 const char *filename = NULL;
485 char *filetext = NULL;
486 off_t fileoff;
487 FILE *fp = NULL;
488 size_t filesize, lines = 0, wirebytes = 0, labels = 0;
489 char *pos = NULL, *file_end = NULL;
490
491 isc_rwlock_init(&rwl);
492
493 isc_mem_create(&mctx);
494
495 if (argc != 2) {
496 fprintf(stderr,
497 "usage: load-names <filename.csv> <nthreads>\n");
498 exit(EXIT_FAILURE);
499 }
500
501 filename = argv[1];
502 result = isc_file_getsize(filename, &fileoff);
503 if (result != ISC_R_SUCCESS) {
504 fprintf(stderr, "stat(%s): %s\n", filename,
505 isc_result_totext(result));
506 exit(EXIT_FAILURE);
507 }
508 filesize = (size_t)fileoff;
509
510 filetext = isc_mem_get(mctx, filesize + 1);
511 fp = fopen(filename, "r");
512 if (fp == NULL || fread(filetext, 1, filesize, fp) < filesize) {
513 fprintf(stderr, "read(%s): %s\n", filename, strerror(errno));
514 exit(EXIT_FAILURE);
515 }
516 fclose(fp);
517 filetext[filesize] = '\0';
518
519 pos = filetext;
520 file_end = pos + filesize;
521 while (pos < file_end) {
522 char *domain = NULL, *newline = NULL;
523 size_t len;
524
525 FILE_CHECK(lines < ARRAY_SIZE(item), "too many lines");
526 pos += strspn(pos, "0123456789");
527
528 FILE_CHECK(*pos++ == ',', "missing comma");
529
530 domain = pos;
531 pos += strcspn(pos, "\r\n");
532 FILE_CHECK(*pos != '\0', "missing newline");
533 newline = pos;
534 pos += strspn(pos, "\r\n");
535 len = newline - domain;
536
537 item[lines].text = domain;
538 domain[len] = '\0';
539
540 dns_name_t *name = dns_fixedname_initname(&item[lines].fixed);
541 isc_buffer_t buffer;
542 isc_buffer_init(&buffer, domain, len);
543 isc_buffer_add(&buffer, len);
544 result = dns_name_fromtext(name, &buffer, dns_rootname, 0,
545 NULL);
546 FILE_CHECK(result == ISC_R_SUCCESS, isc_result_totext(result));
547
548 wirebytes += name->length;
549 labels += name->labels;
550 lines++;
551 }
552
553 printf("names %g MB labels %g MB\n\n", (double)wirebytes / 1048576.0,
554 (double)labels / 1048576.0);
555
556 printf("%10s | %10s | %10s | %10s | %10s | %10s | %10s |\n",
557 "algorithm", "threads", "load", "query", "dirty MB", "total",
558 "final MB");
559
560 for (size_t nthreads = 128; nthreads > 0; nthreads /= 2) {
561 printf("---------- | ---------- | ---------- | ---------- | "
562 "---------- | ---------- | ---------- |\n");
563
564 for (struct fun *fun = fun_list; fun->name != NULL; fun++) {
565 isc_mem_t *mem = NULL;
566 void *map = NULL;
567
568 isc_mem_create(&mem);
569 map = fun->new(mem);
570
571 size_t nitems = lines / (nthreads + 1);
572
573 isc_barrier_init(&barrier, nthreads);
574
575 isc_time_t t0 = isc_time_now_hires();
576 size_t m0 = isc_mem_inuse(mem);
577
578 for (size_t i = 0; i < nthreads; i++) {
579 threads[i] = (struct thread_s){
580 .fun = fun,
581 .map = map,
582 .start = nitems * i,
583 .end = nitems * i + nitems,
584 };
585 isc_thread_create(fun->thread, &threads[i],
586 &threads[i].thread);
587 }
588
589 uint64_t d0 = 0;
590 uint64_t d1 = 0;
591
592 for (size_t i = 0; i < nthreads; i++) {
593 isc_thread_join(threads[i].thread, NULL);
594 d0 += threads[i].d0;
595 d1 += threads[i].d1;
596 }
597
598 size_t m1 = isc_mem_inuse(mem);
599
600 rcu_barrier();
601
602 isc_time_t t1 = isc_time_now_hires();
603 uint64_t d3 = isc_time_microdiff(&t1, &t0);
604 size_t m2 = isc_mem_inuse(mem);
605
606 printf("%10s | %10zu | %10.4f | %10.4f | %10.4f | "
607 "%10.4f | %10.4f |\n",
608 fun->name, nthreads,
609 (double)(d0 / nthreads) / (1000.0 * 1000.0),
610 (double)(d1 / nthreads) / (1000.0 * 1000.0),
611 (double)(m1 - m0) / (1024.0 * 1024.0),
612 (double)d3 / (1000.0 * 1000.0),
613 (double)(m2 - m0) / (1024.0 * 1024.0)
614
615 );
616 }
617 }
618
619 printf("---------- | ---------- | ---------- | ---------- | "
620 "---------- | ---------- | ---------- |\n");
621 }