Debian Codegraph @

regressions/ck_rhs/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_rhs.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_rhs_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_RHS_MODE_OBJECT | CK_RHS_MODE_SPMC;
144
145
146 ck_epoch_init(&epoch_hs);
147 ck_epoch_register(&epoch_hs, &epoch_wr, NULL);
148 common_srand48((long int)time(NULL));
149 if (ck_rhs_init(&hs, mode, hs_hash, hs_compare, &my_allocator, 65536, common_lrand48()) == false) {
150 perror("ck_rhs_init");
151 exit(EXIT_FAILURE);
152 }
153
154 return;
155 }
156
157 static bool
158 set_remove(const char *value)
159 {
160 unsigned long h;
161
162 h = CK_RHS_HASH(&hs, hs_hash, value);
163 return (bool)ck_rhs_remove(&hs, h, value);
164 }
165
166 static bool
167 set_replace(const char *value)
168 {
169 unsigned long h;
170 void *previous;
171
172 h = CK_RHS_HASH(&hs, hs_hash, value);
173 return ck_rhs_set(&hs, h, value, &previous);
174 }
175
176 static bool
177 set_swap(const char *value)
178 {
179 unsigned long h;
180 void *previous;
181
182 h = CK_RHS_HASH(&hs, hs_hash, value);
183 return ck_rhs_fas(&hs, h, value, &previous);
184 }
185
186 static void *
187 set_get(const char *value)
188 {
189 unsigned long h;
190 void *v;
191
192 h = CK_RHS_HASH(&hs, hs_hash, value);
193 v = ck_rhs_get(&hs, h, value);
194 return v;
195 }
196
197 static bool
198 set_insert(const char *value)
199 {
200 unsigned long h;
201
202 h = CK_RHS_HASH(&hs, hs_hash, value);
203 return ck_rhs_put(&hs, h, value);
204 }
205
206 static size_t
207 set_count(void)
208 {
209
210 return ck_rhs_count(&hs);
211 }
212
213 static bool
214 set_reset(void)
215 {
216
217 return ck_rhs_reset(&hs);
218 }
219
220 static void *
221 reader(void *unused)
222 {
223 size_t i;
224 ck_epoch_record_t epoch_record;
225 int state_previous = HS_STATE_STOP;
226 int n_state = 0;
227 uint64_t s, j, a;
228
229 (void)unused;
230 if (aff_iterate(&affinerator) != 0)
231 perror("WARNING: Failed to affine thread");
232
233 s = j = a = 0;
234 ck_epoch_register(&epoch_hs, &epoch_record, NULL);
235 for (;;) {
236 j++;
237 ck_epoch_begin(&epoch_record, NULL);
238 s = rdtsc();
239 for (i = 0; i < keys_length; i++) {
240 char *r;
241
242 r = set_get(keys[i]);
243 if (r == NULL) {
244 if (n_state == HS_STATE_STRICT_REPLACEMENT) {
245 ck_error("ERROR: Did not find during replacement: %s\n", keys[i]);
246 }
247
248 continue;
249 }
250
251 if (strcmp(r, keys[i]) == 0)
252 continue;
253
254 ck_error("ERROR: Found invalid value: [%s] but expected [%s]\n", (char *)r, keys[i]);
255 }
256 a += rdtsc() - s;
257 ck_epoch_end(&epoch_record, NULL);
258
259 n_state = ck_pr_load_int(&state);
260 if (n_state != state_previous) {
261 ck_spinlock_lock(&mtx);
262 accumulator[state_previous] += a / (j * keys_length);
263 ck_spinlock_unlock(&mtx);
264
265 ck_pr_inc_int(&barrier[state_previous]);
266 while (ck_pr_load_int(&barrier[state_previous]) != n_threads + 1)
267 ck_pr_stall();
268
269 state_previous = n_state;
270 s = j = a = 0;
271 }
272 }
273
274 return NULL;
275 }
276
277 static uint64_t
278 acc(size_t i)
279 {
280 uint64_t r;
281
282 ck_spinlock_lock(&mtx);
283 r = accumulator[i];
284 ck_spinlock_unlock(&mtx);
285
286 return r;
287 }
288
289 int
290 main(int argc, char *argv[])
291 {
292 FILE *fp;
293 char buffer[512];
294 size_t i, j, r;
295 unsigned int d = 0;
296 uint64_t s, e, a, repeated;
297 char **t;
298 pthread_t *readers;
299 double p_r, p_d;
300
301 COMMON_ALARM_DECLARE_LOCAL(hs_alarm, alarm_event)
302
303 r = 20;
304 s = 8;
305 p_d = 0.5;
306 p_r = 0.5;
307 n_threads = CORES - 1;
308
309 if (argc < 2) {
310 ck_error("Usage: parallel <dictionary> [<interval length> <initial size> <readers>\n"
311 " <probability of replacement> <probability of deletion> <epoch threshold>]\n");
312 }
313
314 if (argc >= 3)
315 r = atoi(argv[2]);
316
317 if (argc >= 4)
318 s = (uint64_t)atoi(argv[3]);
319
320 if (argc >= 5) {
321 n_threads = atoi(argv[4]);
322 if (n_threads < 1) {
323 ck_error("ERROR: Number of readers must be >= 1.\n");
324 }
325 }
326
327 if (argc >= 6) {
328 p_r = atof(argv[5]) / 100.00;
329 if (p_r < 0) {
330 ck_error("ERROR: Probability of replacement must be >= 0 and <= 100.\n");
331 }
332 }
333
334 if (argc >= 7) {
335 p_d = atof(argv[6]) / 100.00;
336 if (p_d < 0) {
337 ck_error("ERROR: Probability of deletion must be >= 0 and <= 100.\n");
338 }
339 }
340
341 COMMON_ALARM_INIT(hs_alarm, alarm_event, r)
342
343 affinerator.delta = 1;
344 readers = malloc(sizeof(pthread_t) * n_threads);
345 assert(readers != NULL);
346
347 keys = malloc(sizeof(char *) * keys_capacity);
348 assert(keys != NULL);
349
350 fp = fopen(argv[1], "r");
351 assert(fp != NULL);
352
353 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
354 buffer[strlen(buffer) - 1] = '\0';
355 keys[keys_length++] = strdup(buffer);
356 assert(keys[keys_length - 1] != NULL);
357
358 if (keys_length == keys_capacity) {
359 t = realloc(keys, sizeof(char *) * (keys_capacity *= 2));
360 assert(t != NULL);
361 keys = t;
362 }
363 }
364
365 t = realloc(keys, sizeof(char *) * keys_length);
366 assert(t != NULL);
367 keys = t;
368
369 set_init();
370
371 for (i = 0; i < (size_t)n_threads; i++) {
372 if (pthread_create(&readers[i], NULL, reader, NULL) != 0) {
373 ck_error("ERROR: Failed to create thread %zu.\n", i);
374 }
375 }
376
377 for (i = 0; i < keys_length; i++)
378 d += set_insert(keys[i]) == false;
379
380 fprintf(stderr, " [S] %d readers, 1 writer.\n", n_threads);
381 fprintf(stderr, " [S] %zu entries stored and %u duplicates.\n\n",
382 set_count(), d);
383
384 fprintf(stderr, " ,- BASIC TEST\n");
385 fprintf(stderr, " | Executing SMR test...");
386 a = 0;
387 for (j = 0; j < r; j++) {
388 if (set_reset() == false) {
389 ck_error("ERROR: Failed to reset hash table.\n");
390 }
391
392 s = rdtsc();
393 for (i = 0; i < keys_length; i++)
394 d += set_insert(keys[i]) == false;
395 e = rdtsc();
396 a += e - s;
397 }
398 fprintf(stderr, "done (%" PRIu64 " ticks)\n", a / (r * keys_length));
399
400 fprintf(stderr, " | Executing replacement test...");
401 a = 0;
402 for (j = 0; j < r; j++) {
403 s = rdtsc();
404 for (i = 0; i < keys_length; i++)
405 set_replace(keys[i]);
406 e = rdtsc();
407 a += e - s;
408 }
409 fprintf(stderr, "done (%" PRIu64 " ticks)\n", a / (r * keys_length));
410
411 fprintf(stderr, " | Executing get test...");
412 a = 0;
413 for (j = 0; j < r; j++) {
414 s = rdtsc();
415 for (i = 0; i < keys_length; i++) {
416 if (set_get(keys[i]) == NULL) {
417 ck_error("ERROR: Unexpected NULL value.\n");
418 }
419 }
420 e = rdtsc();
421 a += e - s;
422 }
423 fprintf(stderr, "done (%" PRIu64 " ticks)\n", a / (r * keys_length));
424
425 a = 0;
426 fprintf(stderr, " | Executing removal test...");
427 for (j = 0; j < r; j++) {
428 s = rdtsc();
429 for (i = 0; i < keys_length; i++)
430 set_remove(keys[i]);
431 e = rdtsc();
432 a += e - s;
433
434 for (i = 0; i < keys_length; i++)
435 set_insert(keys[i]);
436 }
437 fprintf(stderr, "done (%" PRIu64 " ticks)\n", a / (r * keys_length));
438
439 fprintf(stderr, " | Executing negative look-up test...");
440 a = 0;
441 for (j = 0; j < r; j++) {
442 s = rdtsc();
443 for (i = 0; i < keys_length; i++) {
444 set_get("\x50\x03\x04\x05\x06\x10");
445 }
446 e = rdtsc();
447 a += e - s;
448 }
449 fprintf(stderr, "done (%" PRIu64 " ticks)\n", a / (r * keys_length));
450
451 ck_epoch_record_t epoch_temporary = epoch_wr;
452 ck_epoch_synchronize(&epoch_wr);
453
454 fprintf(stderr, " '- Summary: %u pending, %u peak, %u reclamations -> "
455 "%u pending, %u peak, %u reclamations\n\n",
456 epoch_temporary.n_pending, epoch_temporary.n_peak, epoch_temporary.n_dispatch,
457 epoch_wr.n_pending, epoch_wr.n_peak, epoch_wr.n_dispatch);
458
459 fprintf(stderr, " ,- READER CONCURRENCY\n");
460 fprintf(stderr, " | Executing reader test...");
461
462 ck_pr_store_int(&state, HS_STATE_GET);
463 while (ck_pr_load_int(&barrier[HS_STATE_STOP]) != n_threads)
464 ck_pr_stall();
465 ck_pr_inc_int(&barrier[HS_STATE_STOP]);
466 common_sleep(r);
467 ck_pr_store_int(&state, HS_STATE_STRICT_REPLACEMENT);
468 while (ck_pr_load_int(&barrier[HS_STATE_GET]) != n_threads)
469 ck_pr_stall();
470
471 fprintf(stderr, "done (reader = %" PRIu64 " ticks)\n",
472 acc(HS_STATE_GET) / n_threads);
473
474 fprintf(stderr, " | Executing strict replacement test...");
475
476 a = repeated = 0;
477 common_alarm(alarm_handler, &alarm_event, r);
478
479 ck_pr_inc_int(&barrier[HS_STATE_GET]);
480 for (;;) {
481 repeated++;
482 s = rdtsc();
483 for (i = 0; i < keys_length; i++) {
484 if (i & 1) {
485 set_replace(keys[i]);
486 } else {
487 set_swap(keys[i]);
488 }
489 }
490 e = rdtsc();
491 a += e - s;
492
493 if (next_stage == true) {
494 next_stage = false;
495 break;
496 }
497 }
498
499 ck_pr_store_int(&state, HS_STATE_DELETION);
500 while (ck_pr_load_int(&barrier[HS_STATE_STRICT_REPLACEMENT]) != n_threads)
501 ck_pr_stall();
502 set_reset();
503 ck_epoch_synchronize(&epoch_wr);
504 fprintf(stderr, "done (writer = %" PRIu64 " ticks, reader = %" PRIu64 " ticks)\n",
505 a / (repeated * keys_length), acc(HS_STATE_STRICT_REPLACEMENT) / n_threads);
506
507 common_alarm(alarm_handler, &alarm_event, r);
508
509 fprintf(stderr, " | Executing deletion test (%.2f)...", p_d * 100);
510 a = repeated = 0;
511 ck_pr_inc_int(&barrier[HS_STATE_STRICT_REPLACEMENT]);
512 for (;;) {
513 double delete;
514
515 repeated++;
516 s = rdtsc();
517 for (i = 0; i < keys_length; i++) {
518 set_insert(keys[i]);
519 if (p_d != 0.0) {
520 delete = common_drand48();
521 if (delete <= p_d)
522 set_remove(keys[i]);
523 }
524 }
525 e = rdtsc();
526 a += e - s;
527
528 if (next_stage == true) {
529 next_stage = false;
530 break;
531 }
532 }
533 ck_pr_store_int(&state, HS_STATE_REPLACEMENT);
534 while (ck_pr_load_int(&barrier[HS_STATE_DELETION]) != n_threads)
535 ck_pr_stall();
536
537 set_reset();
538 ck_epoch_synchronize(&epoch_wr);
539 fprintf(stderr, "done (writer = %" PRIu64 " ticks, reader = %" PRIu64 " ticks)\n",
540 a / (repeated * keys_length), acc(HS_STATE_DELETION) / n_threads);
541
542 common_alarm(alarm_handler, &alarm_event, r);
543
544 fprintf(stderr, " | Executing replacement test (%.2f)...", p_r * 100);
545 a = repeated = 0;
546 ck_pr_inc_int(&barrier[HS_STATE_DELETION]);
547 for (;;) {
548 double delete, replace;
549
550 repeated++;
551 s = rdtsc();
552 for (i = 0; i < keys_length; i++) {
553 set_insert(keys[i]);
554 if (p_d != 0.0) {
555 delete = common_drand48();
556 if (delete <= p_d)
557 set_remove(keys[i]);
558 } else {
559 delete = 0.0;
560 }
561
562 if (p_r != 0.0) {
563 replace = common_drand48();
564 if (replace <= p_r) {
565 if ((i & 1) || (delete <= p_d)) {
566 set_replace(keys[i]);
567 } else {
568 set_swap(keys[i]);
569 }
570 }
571 }
572 }
573 e = rdtsc();
574 a += e - s;
575
576 if (next_stage == true) {
577 next_stage = false;
578 break;
579 }
580 }
581 ck_pr_store_int(&state, HS_STATE_STOP);
582 while (ck_pr_load_int(&barrier[HS_STATE_REPLACEMENT]) != n_threads)
583 ck_pr_stall();
584 set_reset();
585 ck_epoch_synchronize(&epoch_wr);
586 fprintf(stderr, "done (writer = %" PRIu64 " ticks, reader = %" PRIu64 " ticks)\n",
587 a / (repeated * keys_length), acc(HS_STATE_REPLACEMENT) / n_threads);
588
589 ck_pr_inc_int(&barrier[HS_STATE_REPLACEMENT]);
590 epoch_temporary = epoch_wr;
591 ck_epoch_synchronize(&epoch_wr);
592
593 fprintf(stderr, " '- Summary: %u pending, %u peak, %u reclamations -> "
594 "%u pending, %u peak, %u reclamations\n\n",
595 epoch_temporary.n_pending, epoch_temporary.n_peak, epoch_temporary.n_dispatch,
596 epoch_wr.n_pending, epoch_wr.n_peak, epoch_wr.n_dispatch);
597 return 0;
598 }
599