Debian Codegraph @

regressions/ck_rhs/benchmark/serial.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
27 #include <ck_rhs.h>
28
29 #include <assert.h>
30 #include <ck_malloc.h>
31 #include <errno.h>
32 #include <inttypes.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <time.h>
37
38 #include "../../common.h"
39 #include "../../../src/ck_ht_hash.h"
40
41 static ck_rhs_t hs;
42 static char **keys;
43 static size_t keys_length = 0;
44 static size_t keys_capacity = 128;
45 static unsigned long global_seed;
46
47 static void *
48 hs_malloc(size_t r)
49 {
50
51 return malloc(r);
52 }
53
54 static void
55 hs_free(void *p, size_t b, bool r)
56 {
57
58 (void)b;
59 (void)r;
60
61 free(p);
62
63 return;
64 }
65
66 static struct ck_malloc my_allocator = {
67 .malloc = hs_malloc,
68 .free = hs_free
69 };
70
71 static unsigned long
72 hs_hash(const void *object, unsigned long seed)
73 {
74 const char *c = object;
75 unsigned long h;
76
77 h = (unsigned long)MurmurHash64A(c, strlen(c), seed);
78 return h;
79 }
80
81 static bool
82 hs_compare(const void *previous, const void *compare)
83 {
84
85 return strcmp(previous, compare) == 0;
86 }
87
88 static void
89 set_destroy(void)
90 {
91
92 ck_rhs_destroy(&hs);
93 return;
94 }
95
96 static void
97 set_init(unsigned int size, unsigned int mode)
98 {
99
100 if (ck_rhs_init(&hs, CK_RHS_MODE_OBJECT | CK_RHS_MODE_SPMC | mode, hs_hash, hs_compare,
101 &my_allocator, size, global_seed) == false) {
102 perror("ck_rhs_init");
103 exit(EXIT_FAILURE);
104 }
105
106 return;
107 }
108
109 static bool
110 set_remove(const char *value)
111 {
112 unsigned long h;
113
114 h = CK_RHS_HASH(&hs, hs_hash, value);
115 return ck_rhs_remove(&hs, h, value) != NULL;
116 }
117
118 static bool
119 set_swap(const char *value)
120 {
121 unsigned long h;
122 void *previous;
123
124 h = CK_RHS_HASH(&hs, hs_hash, value);
125 return ck_rhs_fas(&hs, h, value, &previous);
126 }
127
128 static bool
129 set_replace(const char *value)
130 {
131 unsigned long h;
132 void *previous;
133
134 h = CK_RHS_HASH(&hs, hs_hash, value);
135 ck_rhs_set(&hs, h, value, &previous);
136 return previous != NULL;
137 }
138
139 static void *
140 set_get(const char *value)
141 {
142 unsigned long h;
143 void *v;
144
145 h = CK_RHS_HASH(&hs, hs_hash, value);
146 v = ck_rhs_get(&hs, h, value);
147 return v;
148 }
149
150 static bool
151 set_insert(const char *value)
152 {
153 unsigned long h;
154
155 h = CK_RHS_HASH(&hs, hs_hash, value);
156 return ck_rhs_put(&hs, h, value);
157 }
158
159 static bool
160 set_insert_unique(const char *value)
161 {
162 unsigned long h;
163
164 h = CK_RHS_HASH(&hs, hs_hash, value);
165 return ck_rhs_put_unique(&hs, h, value);
166 }
167
168 static size_t
169 set_count(void)
170 {
171
172 return ck_rhs_count(&hs);
173 }
174
175 static bool
176 set_reset(void)
177 {
178
179 return ck_rhs_reset(&hs);
180 }
181
182 static void
183 set_gc(void)
184 {
185
186 ck_rhs_gc(&hs);
187 return;
188 }
189
190 static void
191 set_rebuild(void)
192 {
193
194 ck_rhs_rebuild(&hs);
195 return;
196 }
197
198 static void
199 keys_shuffle(char **k)
200 {
201 size_t i, j;
202 char *t;
203
204 for (i = keys_length; i > 1; i--) {
205 j = rand() % (i - 1);
206
207 if (j != i - 1) {
208 t = k[i - 1];
209 k[i - 1] = k[j];
210 k[j] = t;
211 }
212 }
213
214 return;
215 }
216
217 static void
218 run_test(const char *file, size_t r, unsigned int size, unsigned int mode)
219 {
220 FILE *fp;
221 char buffer[512];
222 size_t i, j;
223 unsigned int d = 0;
224 uint64_t s, e, a, ri, si, ai, sr, rg, sg, ag, sd, ng, ss, sts, su, sgc, sb;
225 struct ck_rhs_stat st;
226 char **t;
227
228 keys = malloc(sizeof(char *) * keys_capacity);
229 assert(keys != NULL);
230
231 fp = fopen(file, "r");
232 assert(fp != NULL);
233
234 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
235 buffer[strlen(buffer) - 1] = '\0';
236 keys[keys_length++] = strdup(buffer);
237 assert(keys[keys_length - 1] != NULL);
238
239 if (keys_length == keys_capacity) {
240 t = realloc(keys, sizeof(char *) * (keys_capacity *= 2));
241 assert(t != NULL);
242 keys = t;
243 }
244 }
245
246 t = realloc(keys, sizeof(char *) * keys_length);
247 assert(t != NULL);
248 keys = t;
249
250 set_init(size, mode);
251 for (i = 0; i < keys_length; i++)
252 d += set_insert(keys[i]) == false;
253 ck_rhs_stat(&hs, &st);
254
255 fprintf(stderr, "# %zu entries stored, %u duplicates, %u probe.\n",
256 set_count(), d, st.probe_maximum);
257
258 a = 0;
259 for (j = 0; j < r; j++) {
260 if (set_reset() == false) {
261 ck_error("ERROR: Failed to reset hash table.\n");
262 }
263
264 s = rdtsc();
265 for (i = keys_length; i > 0; i--)
266 d += set_insert(keys[i - 1]) == false;
267 e = rdtsc();
268 a += e - s;
269 }
270 ri = a / (r * keys_length);
271
272 a = 0;
273 for (j = 0; j < r; j++) {
274 if (set_reset() == false) {
275 ck_error("ERROR: Failed to reset hash table.\n");
276 }
277
278 s = rdtsc();
279 for (i = 0; i < keys_length; i++)
280 d += set_insert(keys[i]) == false;
281 e = rdtsc();
282 a += e - s;
283 }
284 si = a / (r * keys_length);
285
286 a = 0;
287 for (j = 0; j < r; j++) {
288 keys_shuffle(keys);
289
290 if (set_reset() == false) {
291 ck_error("ERROR: Failed to reset hash table.\n");
292 }
293
294 s = rdtsc();
295 for (i = 0; i < keys_length; i++)
296 d += set_insert(keys[i]) == false;
297 e = rdtsc();
298 a += e - s;
299 }
300 ai = a / (r * keys_length);
301
302 a = 0;
303 for (j = 0; j < r; j++) {
304 s = rdtsc();
305 for (i = 0; i < keys_length; i++)
306 set_swap(keys[i]);
307 e = rdtsc();
308 a += e - s;
309 }
310 ss = a / (r * keys_length);
311
312 a = 0;
313 for (j = 0; j < r; j++) {
314 s = rdtsc();
315 for (i = 0; i < keys_length; i++)
316 set_replace(keys[i]);
317 e = rdtsc();
318 a += e - s;
319 }
320 sr = a / (r * keys_length);
321
322 set_reset();
323 for (i = 0; i < keys_length; i++)
324 set_insert(keys[i]);
325
326 a = 0;
327 for (j = 0; j < r; j++) {
328 s = rdtsc();
329 for (i = keys_length; i > 0; i--) {
330 if (set_get(keys[i - 1]) == NULL) {
331 ck_error("ERROR: Unexpected NULL value.\n");
332 }
333 }
334 e = rdtsc();
335 a += e - s;
336 }
337 rg = a / (r * keys_length);
338
339 a = 0;
340 for (j = 0; j < r; j++) {
341 s = rdtsc();
342 for (i = 0; i < keys_length; i++) {
343 if (set_get(keys[i]) == NULL) {
344 ck_error("ERROR: Unexpected NULL value.\n");
345 }
346 }
347 e = rdtsc();
348 a += e - s;
349 }
350 sg = a / (r * keys_length);
351
352 a = 0;
353 for (j = 0; j < r; j++) {
354 keys_shuffle(keys);
355
356 s = rdtsc();
357 for (i = 0; i < keys_length; i++) {
358 if (set_get(keys[i]) == NULL) {
359 ck_error("ERROR: Unexpected NULL value.\n");
360 }
361 }
362 e = rdtsc();
363 a += e - s;
364 }
365 ag = a / (r * keys_length);
366
367 a = 0;
368 for (j = 0; j < r; j++) {
369 s = rdtsc();
370 for (i = 0; i < keys_length; i++)
371 set_remove(keys[i]);
372 e = rdtsc();
373 a += e - s;
374
375 for (i = 0; i < keys_length; i++)
376 set_insert(keys[i]);
377 }
378 sd = a / (r * keys_length);
379
380 a = 0;
381 for (j = 0; j < r; j++) {
382 s = rdtsc();
383 for (i = 0; i < keys_length; i++) {
384 set_get("\x50\x03\x04\x05\x06\x10");
385 }
386 e = rdtsc();
387 a += e - s;
388 }
389 ng = a / (r * keys_length);
390
391 set_reset();
392 for (i = 0; i < keys_length; i++)
393 set_insert(keys[i]);
394 for (i = 0; i < keys_length; i++)
395 set_remove(keys[i]);
396
397 a = 0;
398 for (j = 0; j < r; j++) {
399 s = rdtsc();
400 for (i = 0; i < keys_length; i++)
401 set_insert(keys[i]);
402 e = rdtsc();
403 a += e - s;
404
405 for (i = 0; i < keys_length; i++)
406 set_remove(keys[i]);
407 }
408 sts = a / (r * keys_length);
409
410 set_reset();
411
412 /* Prune duplicates. */
413 for (i = 0; i < keys_length; i++) {
414 if (set_insert(keys[i]) == true)
415 continue;
416
417 free(keys[i]);
418 keys[i] = keys[--keys_length];
419 }
420
421 for (i = 0; i < keys_length; i++)
422 set_remove(keys[i]);
423
424 a = 0;
425 for (j = 0; j < r; j++) {
426 s = rdtsc();
427 for (i = 0; i < keys_length; i++)
428 set_insert_unique(keys[i]);
429 e = rdtsc();
430 a += e - s;
431
432 for (i = 0; i < keys_length; i++)
433 set_remove(keys[i]);
434 }
435 su = a / (r * keys_length);
436
437 for (i = 0; i < keys_length; i++)
438 set_insert_unique(keys[i]);
439
440 for (i = 0; i < keys_length / 2; i++)
441 set_remove(keys[i]);
442
443 a = 0;
444 for (j = 0; j < r; j++) {
445 s = rdtsc();
446 set_gc();
447 e = rdtsc();
448 a += e - s;
449 }
450 sgc = a / r;
451
452 a = 0;
453 for (j = 0; j < r; j++) {
454 s = rdtsc();
455 set_rebuild();
456 e = rdtsc();
457 a += e - s;
458 }
459 sb = a / r;
460
461 printf("%zu "
462 "%" PRIu64 " "
463 "%" PRIu64 " "
464 "%" PRIu64 " "
465 "%" PRIu64 " "
466 "%" PRIu64 " "
467 "%" PRIu64 " "
468 "%" PRIu64 " "
469 "%" PRIu64 " "
470 "%" PRIu64 " "
471 "%" PRIu64 " "
472 "%" PRIu64 " "
473 "%" PRIu64 " "
474 "%" PRIu64 " "
475 "%" PRIu64 "\n",
476 keys_length, ri, si, ai, ss, sr, rg, sg, ag, sd, ng, sts, su, sgc, sb);
477
478 fclose(fp);
479
480 for (i = 0; i < keys_length; i++) {
481 free(keys[i]);
482 }
483
484 free(keys);
485 keys_length = 0;
486 set_destroy();
487 return;
488 }
489
490 int
491 main(int argc, char *argv[])
492 {
493 unsigned int r, size;
494
495 common_srand48((long int)time(NULL));
496 if (argc < 2) {
497 ck_error("Usage: ck_rhs <dictionary> [<repetitions> <initial size>]\n");
498 }
499
500 r = 16;
501 if (argc >= 3)
502 r = atoi(argv[2]);
503
504 size = 8;
505 if (argc >= 4)
506 size = atoi(argv[3]);
507
508 global_seed = common_lrand48();
509 run_test(argv[1], r, size, 0);
510 run_test(argv[1], r, size, CK_RHS_MODE_READ_MOSTLY);
511 fprintf(stderr, "# reverse_insertion serial_insertion random_insertion serial_swap "
512 "serial_replace reverse_get serial_get random_get serial_remove negative_get tombstone "
513 "set_unique gc rebuild\n\n");
514
515 return 0;
516 }
517