Debian Codegraph @

regressions/ck_hs/benchmark/parallel_bytestring.c

raw
1 /*
2 * Copyright 2012 Samy Al Bahra.
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 copyrights
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyrights
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 #include "../../common.h"
27 #include <ck_hs.h>
28 #include "../../../src/ck_ht_hash.h"
29 #include <assert.h>
30 #include <ck_epoch.h>
31 #include <ck_malloc.h>
32 #include <ck_pr.h>
33 #include <ck_spinlock.h>
34
35 #include <errno.h>
36 #include <inttypes.h>
37 #include <pthread.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <time.h>
43 #include <unistd.h>
44
45 static ck_hs_t hs CK_CC_CACHELINE;
46 static char **keys;
47 static size_t keys_length = 0;
48 static size_t keys_capacity = 128;
49 static ck_epoch_t epoch_hs;
50 static ck_epoch_record_t epoch_wr;
51 static int n_threads;
52 static bool next_stage;
53
54 enum state {
55 HS_STATE_STOP = 0,
56 HS_STATE_GET,
57 HS_STATE_STRICT_REPLACEMENT,
58 HS_STATE_DELETION,
59 HS_STATE_REPLACEMENT,
60 HS_STATE_COUNT
61 };
62
63 static ck_spinlock_t mtx = CK_SPINLOCK_INITIALIZER;
64 static struct affinity affinerator = AFFINITY_INITIALIZER;
65 static uint64_t accumulator[HS_STATE_COUNT];
66 static int barrier[HS_STATE_COUNT];
67 static int state;
68
69 struct hs_epoch {
70 ck_epoch_entry_t epoch_entry;
71 };
72
73 COMMON_ALARM_DECLARE_GLOBAL(hs_alarm, alarm_event, next_stage)
74
75 static void
76 alarm_handler(int s)
77 {
78
79 (void)s;
80 next_stage = true;
81 return;
82 }
83
84 static unsigned long
85 hs_hash(const void *object, unsigned long seed)
86 {
87 const char *c = object;
88 unsigned long h;
89
90 h = (unsigned long)MurmurHash64A(c, strlen(c), seed);
91 return h;
92 }
93
94 static bool
95 hs_compare(const void *previous, const void *compare)
96 {
97
98 return strcmp(previous, compare) == 0;
99 }
100
101 static void
102 hs_destroy(ck_epoch_entry_t *e)
103 {
104
105 free(e);
106 return;
107 }
108
109 static void *
110 hs_malloc(size_t r)
111 {
112 ck_epoch_entry_t *b;
113
114 b = malloc(sizeof(*b) + r);
115 return b + 1;
116 }
117
118 static void
119 hs_free(void *p, size_t b, bool r)
120 {
121 struct hs_epoch *e = p;
122
123 (void)b;
124
125 if (r == true) {
126 /* Destruction requires safe memory reclamation. */
127 ck_epoch_call(&epoch_wr, &(--e)->epoch_entry, hs_destroy);
128 } else {
129 free(--e);
130 }
131
132 return;
133 }
134
135 static struct ck_malloc my_allocator = {
136 .malloc = hs_malloc,
137 .free = hs_free
138 };
139
140 static void
141 set_init(void)
142 {
143 unsigned int mode = CK_HS_MODE_OBJECT | CK_HS_MODE_SPMC;
144
145 #ifdef HS_DELETE
146 mode |= CK_HS_MODE_DELETE;
147 #endif
148
149 ck_epoch_init(&epoch_hs);
150 ck_epoch_register(&epoch_hs, &epoch_wr, NULL);
151 common_srand48((long int)time(NULL));
152 if (ck_hs_init(&hs, mode, hs_hash, hs_compare, &my_allocator, 65536, common_lrand48()) == false) {
153 perror("ck_hs_init");
154 exit(EXIT_FAILURE);
155 }
156
157 return;
158 }
159
160 static bool
161 set_remove(const char *value)
162 {
163 unsigned long h;
164
165 h = CK_HS_HASH(&hs, hs_hash, value);
166 return (bool)ck_hs_remove(&hs, h, value);
167 }
168
169 static bool
170 set_replace(const char *value)
171 {
172 unsigned long h;
173 void *previous;
174
175 h = CK_HS_HASH(&hs, hs_hash, value);
176 return ck_hs_set(&hs, h, value, &previous);
177 }
178
179 static bool
180 set_swap(const char *value)
181 {
182 unsigned long h;
183 void *previous;
184
185 h = CK_HS_HASH(&hs, hs_hash, value);
186 return ck_hs_fas(&hs, h, value, &previous);
187 }
188
189 static void *
190 set_get(const char *value)
191 {
192 unsigned long h;
193 void *v;
194
195 h = CK_HS_HASH(&hs, hs_hash, value);
196 v = ck_hs_get(&hs, h, value);
197 return v;
198 }
199
200 static bool
201 set_insert(const char *value)
202 {
203 unsigned long h;
204
205 h = CK_HS_HASH(&hs, hs_hash, value);
206 return ck_hs_put(&hs, h, value);
207 }
208
209 static size_t
210 set_count(void)
211 {
212
213 return ck_hs_count(&hs);
214 }
215
216 static bool
217 set_reset(void)
218 {
219
220 return ck_hs_reset(&hs);
221 }
222
223 static void *
224 reader(void *unused)
225 {
226 size_t i;
227 ck_epoch_record_t epoch_record;
228 int state_previous = HS_STATE_STOP;
229 int n_state = 0;
230 uint64_t s, j, a;
231
232 (void)unused;
233 if (aff_iterate(&affinerator) != 0)
234 perror("WARNING: Failed to affine thread");
235
236 s = j = a = 0;
237 ck_epoch_register(&epoch_hs, &epoch_record, NULL);
238 for (;;) {
239 j++;
240 ck_epoch_begin(&epoch_record, NULL);
241 s = rdtsc();
242 for (i = 0; i < keys_length; i++) {
243 char *r;
244
245 r = set_get(keys[i]);
246 if (r == NULL) {
247 if (n_state == HS_STATE_STRICT_REPLACEMENT) {
248 ck_error("ERROR: Did not find during replacement: %s\n", keys[i]);
249 }
250
251 continue;
252 }
253
254 if (strcmp(r, keys[i]) == 0)
255 continue;
256
257 ck_error("ERROR: Found invalid value: [%s] but expected [%s]\n", (char *)r, keys[i]);
258 }
259 a += rdtsc() - s;
260 ck_epoch_end(&epoch_record, NULL);
261
262 n_state = ck_pr_load_int(&state);
263 if (n_state != state_previous) {
264 ck_spinlock_lock(&mtx);
265 accumulator[state_previous] += a / (j * keys_length);
266 ck_spinlock_unlock(&mtx);
267
268 ck_pr_inc_int(&barrier[state_previous]);
269 while (ck_pr_load_int(&barrier[state_previous]) != n_threads + 1)
270 ck_pr_stall();
271
272 state_previous = n_state;
273 s = j = a = 0;
274 }
275 }
276
277 return NULL;
278 }
279
280 static uint64_t
281 acc(size_t i)
282 {
283 uint64_t r;
284
285 ck_spinlock_lock(&mtx);
286 r = accumulator[i];
287 ck_spinlock_unlock(&mtx);
288
289 return r;
290 }
291
292 int
293 main(int argc, char *argv[])
294 {
295 FILE *fp;
296 char buffer[512];
297 size_t i, j, r;
298 unsigned int d = 0;
299 uint64_t s, e, a, repeated;
300 char **t;
301 pthread_t *readers;
302 double p_r, p_d;
303
304 COMMON_ALARM_DECLARE_LOCAL(hs_alarm, alarm_event)
305
306 r = 20;
307 s = 8;
308 p_d = 0.5;
309 p_r = 0.5;
310 n_threads = CORES - 1;
311
312 if (argc < 2) {
313 ck_error("Usage: parallel <dictionary> [<interval length> <initial size> <readers>\n"
314 " <probability of replacement> <probability of deletion> <epoch threshold>]\n");
315 }
316
317 if (argc >= 3)
318 r = atoi(argv[2]);
319
320 if (argc >= 4)
321 s = (uint64_t)atoi(argv[3]);
322
323 if (argc >= 5) {
324 n_threads = atoi(argv[4]);
325 if (n_threads < 1) {
326 ck_error("ERROR: Number of readers must be >= 1.\n");
327 }
328 }
329
330 if (argc >= 6) {
331 p_r = atof(argv[5]) / 100.00;
332 if (p_r < 0) {
333 ck_error("ERROR: Probability of replacement must be >= 0 and <= 100.\n");
334 }
335 }
336
337 if (argc >= 7) {
338 p_d = atof(argv[6]) / 100.00;
339 if (p_d < 0) {
340 ck_error("ERROR: Probability of deletion must be >= 0 and <= 100.\n");
341 }
342 }
343
344 COMMON_ALARM_INIT(hs_alarm, alarm_event, r)
345
346 affinerator.delta = 1;
347 readers = malloc(sizeof(pthread_t) * n_threads);
348 assert(readers != NULL);
349
350 keys = malloc(sizeof(char *) * keys_capacity);
351 assert(keys != NULL);
352
353 fp = fopen(argv[1], "r");
354 assert(fp != NULL);
355
356 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
357 buffer[strlen(buffer) - 1] = '\0';
358 keys[keys_length++] = strdup(buffer);
359 assert(keys[keys_length - 1] != NULL);
360
361 if (keys_length == keys_capacity) {
362 t = realloc(keys, sizeof(char *) * (keys_capacity *= 2));
363 assert(t != NULL);
364 keys = t;
365 }
366 }
367
368 t = realloc(keys, sizeof(char *) * keys_length);
369 assert(t != NULL);
370 keys = t;
371
372 set_init();
373
374 for (i = 0; i < (size_t)n_threads; i++) {
375 if (pthread_create(&readers[i], NULL, reader, NULL) != 0) {
376 ck_error("ERROR: Failed to create thread %zu.\n", i);
377 }
378 }
379
380 for (i = 0; i < keys_length; i++)
381 d += set_insert(keys[i]) == false;
382
383 fprintf(stderr, " [S] %d readers, 1 writer.\n", n_threads);
384 fprintf(stderr, " [S] %zu entries stored and %u duplicates.\n\n",
385 set_count(), d);
386
387 fprintf(stderr, " ,- BASIC TEST\n");
388 fprintf(stderr, " | Executing SMR test...");
389 a = 0;
390 for (j = 0; j < r; j++) {
391 if (set_reset() == false) {
392 ck_error("ERROR: Failed to reset hash table.\n");
393 }
394
395 s = rdtsc();
396 for (i = 0; i < keys_length; i++)
397 d += set_insert(keys[i]) == false;
398 e = rdtsc();
399 a += e - s;
400 }
401 fprintf(stderr, "done (%" PRIu64 " ticks)\n", a / (r * keys_length));
402
403 fprintf(stderr, " | Executing replacement test...");
404 a = 0;
405 for (j = 0; j < r; j++) {
406 s = rdtsc();
407 for (i = 0; i < keys_length; i++)
408 set_replace(keys[i]);
409 e = rdtsc();
410 a += e - s;
411 }
412 fprintf(stderr, "done (%" PRIu64 " ticks)\n", a / (r * keys_length));
413
414 fprintf(stderr, " | Executing get test...");
415 a = 0;
416 for (j = 0; j < r; j++) {
417 s = rdtsc();
418 for (i = 0; i < keys_length; i++) {
419 if (set_get(keys[i]) == NULL) {
420 ck_error("ERROR: Unexpected NULL value.\n");
421 }
422 }
423 e = rdtsc();
424 a += e - s;
425 }
426 fprintf(stderr, "done (%" PRIu64 " ticks)\n", a / (r * keys_length));
427
428 a = 0;
429 fprintf(stderr, " | Executing removal test...");
430 for (j = 0; j < r; j++) {
431 s = rdtsc();
432 for (i = 0; i < keys_length; i++)
433 set_remove(keys[i]);
434 e = rdtsc();
435 a += e - s;
436
437 for (i = 0; i < keys_length; i++)
438 set_insert(keys[i]);
439 }
440 fprintf(stderr, "done (%" PRIu64 " ticks)\n", a / (r * keys_length));
441
442 fprintf(stderr, " | Executing negative look-up test...");
443 a = 0;
444 for (j = 0; j < r; j++) {
445 s = rdtsc();
446 for (i = 0; i < keys_length; i++) {
447 set_get("\x50\x03\x04\x05\x06\x10");
448 }
449 e = rdtsc();
450 a += e - s;
451 }
452 fprintf(stderr, "done (%" PRIu64 " ticks)\n", a / (r * keys_length));
453
454 ck_epoch_record_t epoch_temporary = epoch_wr;
455 ck_epoch_synchronize(&epoch_wr);
456
457 fprintf(stderr, " '- Summary: %u pending, %u peak, %u reclamations -> "
458 "%u pending, %u peak, %u reclamations\n\n",
459 epoch_temporary.n_pending, epoch_temporary.n_peak, epoch_temporary.n_dispatch,
460 epoch_wr.n_pending, epoch_wr.n_peak, epoch_wr.n_dispatch);
461
462 fprintf(stderr, " ,- READER CONCURRENCY\n");
463 fprintf(stderr, " | Executing reader test...");
464
465 ck_pr_store_int(&state, HS_STATE_GET);
466 while (ck_pr_load_int(&barrier[HS_STATE_STOP]) != n_threads)
467 ck_pr_stall();
468 ck_pr_inc_int(&barrier[HS_STATE_STOP]);
469 common_sleep(r);
470 ck_pr_store_int(&state, HS_STATE_STRICT_REPLACEMENT);
471 while (ck_pr_load_int(&barrier[HS_STATE_GET]) != n_threads)
472 ck_pr_stall();
473
474 fprintf(stderr, "done (reader = %" PRIu64 " ticks)\n",
475 acc(HS_STATE_GET) / n_threads);
476
477 fprintf(stderr, " | Executing strict replacement test...");
478
479 a = repeated = 0;
480 common_alarm(alarm_handler, &alarm_event, r);
481
482 ck_pr_inc_int(&barrier[HS_STATE_GET]);
483 for (;;) {
484 repeated++;
485 s = rdtsc();
486 for (i = 0; i < keys_length; i++) {
487 if (i & 1) {
488 set_replace(keys[i]);
489 } else {
490 set_swap(keys[i]);
491 }
492 }
493 e = rdtsc();
494 a += e - s;
495
496 if (next_stage == true) {
497 next_stage = false;
498 break;
499 }
500 }
501
502 ck_pr_store_int(&state, HS_STATE_DELETION);
503 while (ck_pr_load_int(&barrier[HS_STATE_STRICT_REPLACEMENT]) != n_threads)
504 ck_pr_stall();
505 set_reset();
506 ck_epoch_synchronize(&epoch_wr);
507 fprintf(stderr, "done (writer = %" PRIu64 " ticks, reader = %" PRIu64 " ticks)\n",
508 a / (repeated * keys_length), acc(HS_STATE_STRICT_REPLACEMENT) / n_threads);
509
510 common_alarm(alarm_handler, &alarm_event, r);
511
512 fprintf(stderr, " | Executing deletion test (%.2f)...", p_d * 100);
513 a = repeated = 0;
514 ck_pr_inc_int(&barrier[HS_STATE_STRICT_REPLACEMENT]);
515 for (;;) {
516 double delete;
517
518 repeated++;
519 s = rdtsc();
520 for (i = 0; i < keys_length; i++) {
521 set_insert(keys[i]);
522 if (p_d != 0.0) {
523 delete = common_drand48();
524 if (delete <= p_d)
525 set_remove(keys[i]);
526 }
527 }
528 e = rdtsc();
529 a += e - s;
530
531 if (next_stage == true) {
532 next_stage = false;
533 break;
534 }
535 }
536 ck_pr_store_int(&state, HS_STATE_REPLACEMENT);
537 while (ck_pr_load_int(&barrier[HS_STATE_DELETION]) != n_threads)
538 ck_pr_stall();
539
540 set_reset();
541 ck_epoch_synchronize(&epoch_wr);
542 fprintf(stderr, "done (writer = %" PRIu64 " ticks, reader = %" PRIu64 " ticks)\n",
543 a / (repeated * keys_length), acc(HS_STATE_DELETION) / n_threads);
544
545 common_alarm(alarm_handler, &alarm_event, r);
546
547 fprintf(stderr, " | Executing replacement test (%.2f)...", p_r * 100);
548 a = repeated = 0;
549 ck_pr_inc_int(&barrier[HS_STATE_DELETION]);
550 for (;;) {
551 double delete, replace;
552
553 repeated++;
554 s = rdtsc();
555 for (i = 0; i < keys_length; i++) {
556 set_insert(keys[i]);
557 if (p_d != 0.0) {
558 delete = common_drand48();
559 if (delete <= p_d)
560 set_remove(keys[i]);
561 } else {
562 delete = 0.0;
563 }
564
565 if (p_r != 0.0) {
566 replace = common_drand48();
567 if (replace <= p_r) {
568 if ((i & 1) || (delete <= p_d)) {
569 set_replace(keys[i]);
570 } else {
571 set_swap(keys[i]);
572 }
573 }
574 }
575 }
576 e = rdtsc();
577 a += e - s;
578
579 if (next_stage == true) {
580 next_stage = false;
581 break;
582 }
583 }
584 ck_pr_store_int(&state, HS_STATE_STOP);
585 while (ck_pr_load_int(&barrier[HS_STATE_REPLACEMENT]) != n_threads)
586 ck_pr_stall();
587 set_reset();
588 ck_epoch_synchronize(&epoch_wr);
589 fprintf(stderr, "done (writer = %" PRIu64 " ticks, reader = %" PRIu64 " ticks)\n",
590 a / (repeated * keys_length), acc(HS_STATE_REPLACEMENT) / n_threads);
591
592 ck_pr_inc_int(&barrier[HS_STATE_REPLACEMENT]);
593 epoch_temporary = epoch_wr;
594 ck_epoch_synchronize(&epoch_wr);
595
596 fprintf(stderr, " '- Summary: %u pending, %u peak, %u reclamations -> "
597 "%u pending, %u peak, %u reclamations\n\n",
598 epoch_temporary.n_pending, epoch_temporary.n_peak, epoch_temporary.n_dispatch,
599 epoch_wr.n_pending, epoch_wr.n_peak, epoch_wr.n_dispatch);
600 return 0;
601 }
602