Merge branch 'urcu/ht' into urcu/ht-benchmarks
[urcu.git] / tests / test_glib_hash.c
1 /*
2 * test_glib_hash.c
3 *
4 * Userspace RCU library - test program
5 *
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <pthread.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <assert.h>
33 #include <sched.h>
34 #include <errno.h>
35 #include <glib.h>
36
37 #ifdef __linux__
38 #include <syscall.h>
39 #endif
40
41 #define DEFAULT_HASH_SIZE 32
42 #define DEFAULT_RAND_POOL 1000000
43
44 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
45 #define CACHE_LINE_SIZE 4096
46
47 /* hardcoded number of CPUs */
48 #define NR_CPUS 16384
49
50 #if defined(_syscall0)
51 _syscall0(pid_t, gettid)
52 #elif defined(__NR_gettid)
53 static inline pid_t gettid(void)
54 {
55 return syscall(__NR_gettid);
56 }
57 #else
58 #warning "use pid as tid"
59 static inline pid_t gettid(void)
60 {
61 return getpid();
62 }
63 #endif
64
65 #ifndef DYNAMIC_LINK_TEST
66 #define _LGPL_SOURCE
67 #else
68 #define debug_yield_read()
69 #endif
70 #include <urcu.h>
71 #include <urcu-call-rcu.h>
72
73 struct wr_count {
74 unsigned long update_ops;
75 unsigned long add;
76 unsigned long add_exist;
77 unsigned long remove;
78 };
79
80 static unsigned int __thread rand_lookup;
81 static unsigned long __thread nr_add;
82 static unsigned long __thread nr_addexist;
83 static unsigned long __thread nr_del;
84 static unsigned long __thread nr_delnoent;
85 static unsigned long __thread lookup_fail;
86 static unsigned long __thread lookup_ok;
87
88 static GHashTable *test_ht;
89
90 struct test_data {
91 int a;
92 int b;
93 };
94
95 static volatile int test_go, test_stop;
96
97 static unsigned long wdelay;
98
99 static unsigned long duration;
100
101 /* read-side C.S. duration, in loops */
102 static unsigned long rduration;
103
104 static unsigned long init_hash_size = DEFAULT_HASH_SIZE;
105 static unsigned long init_populate;
106 static unsigned long rand_pool = DEFAULT_RAND_POOL;
107 static int add_only, add_unique;
108
109 static inline void loop_sleep(unsigned long l)
110 {
111 while(l-- != 0)
112 caa_cpu_relax();
113 }
114
115 static int verbose_mode;
116
117 #define printf_verbose(fmt, args...) \
118 do { \
119 if (verbose_mode) \
120 printf(fmt, ## args); \
121 } while (0)
122
123 static unsigned int cpu_affinities[NR_CPUS];
124 static unsigned int next_aff = 0;
125 static int use_affinity = 0;
126
127 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
128
129 static void set_affinity(void)
130 {
131 cpu_set_t mask;
132 int cpu;
133 int ret;
134
135 if (!use_affinity)
136 return;
137
138 ret = pthread_mutex_lock(&affinity_mutex);
139 if (ret) {
140 perror("Error in pthread mutex lock");
141 exit(-1);
142 }
143 cpu = cpu_affinities[next_aff++];
144 ret = pthread_mutex_unlock(&affinity_mutex);
145 if (ret) {
146 perror("Error in pthread mutex unlock");
147 exit(-1);
148 }
149 CPU_ZERO(&mask);
150 CPU_SET(cpu, &mask);
151 sched_setaffinity(0, sizeof(mask), &mask);
152 }
153
154 /*
155 * returns 0 if test should end.
156 */
157 static int test_duration_write(void)
158 {
159 return !test_stop;
160 }
161
162 static int test_duration_read(void)
163 {
164 return !test_stop;
165 }
166
167 static unsigned long long __thread nr_writes;
168 static unsigned long long __thread nr_reads;
169
170 static unsigned int nr_readers;
171 static unsigned int nr_writers;
172
173 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
174
175 void rcu_copy_mutex_lock(void)
176 {
177 int ret;
178 ret = pthread_mutex_lock(&rcu_copy_mutex);
179 if (ret) {
180 perror("Error in pthread mutex lock");
181 exit(-1);
182 }
183 }
184
185 void rcu_copy_mutex_unlock(void)
186 {
187 int ret;
188
189 ret = pthread_mutex_unlock(&rcu_copy_mutex);
190 if (ret) {
191 perror("Error in pthread mutex unlock");
192 exit(-1);
193 }
194 }
195
196 /*
197 * Hash function
198 * Source: http://burtleburtle.net/bob/c/lookup3.c
199 * Originally Public Domain
200 */
201
202 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
203
204 #define mix(a, b, c) \
205 do { \
206 a -= c; a ^= rot(c, 4); c += b; \
207 b -= a; b ^= rot(a, 6); a += c; \
208 c -= b; c ^= rot(b, 8); b += a; \
209 a -= c; a ^= rot(c, 16); c += b; \
210 b -= a; b ^= rot(a, 19); a += c; \
211 c -= b; c ^= rot(b, 4); b += a; \
212 } while (0)
213
214 #define final(a, b, c) \
215 { \
216 c ^= b; c -= rot(b, 14); \
217 a ^= c; a -= rot(c, 11); \
218 b ^= a; b -= rot(a, 25); \
219 c ^= b; c -= rot(b, 16); \
220 a ^= c; a -= rot(c, 4);\
221 b ^= a; b -= rot(a, 14); \
222 c ^= b; c -= rot(b, 24); \
223 }
224
225 static __attribute__((unused))
226 uint32_t hash_u32(
227 const uint32_t *k, /* the key, an array of uint32_t values */
228 size_t length, /* the length of the key, in uint32_ts */
229 uint32_t initval) /* the previous hash, or an arbitrary value */
230 {
231 uint32_t a, b, c;
232
233 /* Set up the internal state */
234 a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval;
235
236 /*----------------------------------------- handle most of the key */
237 while (length > 3) {
238 a += k[0];
239 b += k[1];
240 c += k[2];
241 mix(a, b, c);
242 length -= 3;
243 k += 3;
244 }
245
246 /*----------------------------------- handle the last 3 uint32_t's */
247 switch (length) { /* all the case statements fall through */
248 case 3: c += k[2];
249 case 2: b += k[1];
250 case 1: a += k[0];
251 final(a, b, c);
252 case 0: /* case 0: nothing left to add */
253 break;
254 }
255 /*---------------------------------------------- report the result */
256 return c;
257 }
258
259 static
260 void hashword2(
261 const uint32_t *k, /* the key, an array of uint32_t values */
262 size_t length, /* the length of the key, in uint32_ts */
263 uint32_t *pc, /* IN: seed OUT: primary hash value */
264 uint32_t *pb) /* IN: more seed OUT: secondary hash value */
265 {
266 uint32_t a, b, c;
267
268 /* Set up the internal state */
269 a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc;
270 c += *pb;
271
272 /*----------------------------------------- handle most of the key */
273 while (length > 3) {
274 a += k[0];
275 b += k[1];
276 c += k[2];
277 mix(a, b, c);
278 length -= 3;
279 k += 3;
280 }
281
282 /*----------------------------------- handle the last 3 uint32_t's */
283 switch (length) { /* all the case statements fall through */
284 case 3: c += k[2];
285 case 2: b += k[1];
286 case 1: a += k[0];
287 final(a, b, c);
288 case 0: /* case 0: nothing left to add */
289 break;
290 }
291 /*---------------------------------------------- report the result */
292 *pc = c;
293 *pb = b;
294 }
295
296 #if (CAA_BITS_PER_LONG == 32)
297 static
298 unsigned long test_hash(const void *_key, size_t length, unsigned long seed)
299 {
300 unsigned long key = (unsigned long) _key;
301 unsigned long v;
302
303 assert(length == sizeof(unsigned long));
304 return hash_u32(&v, 1, seed);
305 }
306 #else
307 static
308 unsigned long test_hash(const void *_key, size_t length, unsigned long seed)
309 {
310 union {
311 uint64_t v64;
312 uint32_t v32[2];
313 } v;
314 union {
315 uint64_t v64;
316 uint32_t v32[2];
317 } key;
318
319 assert(length == sizeof(unsigned long));
320 v.v64 = (uint64_t) seed;
321 key.v64 = (uint64_t) _key;
322 hashword2(key.v32, 2, &v.v32[0], &v.v32[1]);
323 return v.v64;
324 }
325 #endif
326
327 static
328 unsigned long test_compare(const void *key1, size_t key1_len,
329 const void *key2, size_t key2_len)
330 {
331 if (unlikely(key1_len != key2_len))
332 return -1;
333 assert(key1_len == sizeof(unsigned long));
334 if (key1 == key2)
335 return 0;
336 else
337 return 1;
338 }
339
340 static
341 guint test_hash_fct(const void *key)
342 {
343 return test_hash(key, sizeof(void *), 0x42);
344 }
345
346 static
347 gboolean test_compare_fct(const void *a, const void *b)
348 {
349 return !test_compare(a, sizeof(void *), b, sizeof(void *));
350 }
351
352 void *thr_reader(void *_count)
353 {
354 unsigned long long *count = _count;
355 void *node;
356
357 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
358 "reader", pthread_self(), (unsigned long)gettid());
359
360 set_affinity();
361
362 rcu_register_thread();
363
364 while (!test_go)
365 {
366 }
367 cmm_smp_mb();
368
369 for (;;) {
370 rcu_copy_mutex_lock();
371 node = g_hash_table_lookup(test_ht,
372 (void *)(unsigned long)(rand_r(&rand_lookup) % rand_pool));
373 if (node == NULL)
374 lookup_fail++;
375 else
376 lookup_ok++;
377 debug_yield_read();
378 if (unlikely(rduration))
379 loop_sleep(rduration);
380 rcu_copy_mutex_unlock();
381 nr_reads++;
382 if (unlikely(!test_duration_read()))
383 break;
384 }
385
386 rcu_unregister_thread();
387
388 *count = nr_reads;
389 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
390 "reader", pthread_self(), (unsigned long)gettid());
391 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
392 pthread_self(), lookup_fail, lookup_ok);
393 return ((void*)1);
394
395 }
396
397 void *thr_writer(void *_count)
398 {
399 void *node, *ret_node, *key;
400 struct wr_count *count = _count;
401 int ret;
402
403 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
404 "writer", pthread_self(), (unsigned long)gettid());
405
406 set_affinity();
407
408 rcu_register_thread();
409
410 while (!test_go)
411 {
412 }
413 cmm_smp_mb();
414
415 for (;;) {
416 if (add_only || rand_r(&rand_lookup) & 1) {
417 void *lookup_node;
418
419 node = malloc(1);
420
421 rcu_copy_mutex_lock();
422 /* Glib hash table only supports replacement. */
423 if (!add_unique) {
424 add_unique = 1; /* force add_unique */
425 printf("glib hash tables only supports "
426 "replacing values (and keys in addition) when the key to insert is already "
427 "present. Make sure you compare with the \"add_unique\" (-u) RCU hash table "
428 "behavior, which is the closest match.\n");
429 }
430 /* Uniquify behavior: lookup and add if not found */
431 key = (void *)(unsigned long)(rand_r(&rand_lookup) % rand_pool);
432 lookup_node = g_hash_table_lookup(test_ht, key);
433 if (lookup_node == NULL) {
434 g_hash_table_insert(test_ht, key, node);
435 rcu_copy_mutex_unlock();
436 nr_add++;
437 } else {
438 rcu_copy_mutex_unlock();
439 free(node);
440 nr_addexist++;
441 }
442 } else {
443 /* May delete */
444 rcu_copy_mutex_lock();
445 ret = g_hash_table_remove(test_ht,
446 (void *)(unsigned long)(rand_r(&rand_lookup) % rand_pool));
447 rcu_copy_mutex_unlock();
448 if (ret) {
449 nr_del++;
450 } else
451 nr_delnoent++;
452 }
453 #if 0
454 //if (nr_writes % 100000 == 0) {
455 if (nr_writes % 1000 == 0) {
456 rcu_read_lock();
457 if (rand_r(&rand_lookup) & 1) {
458 ht_resize(test_ht, 1);
459 } else {
460 ht_resize(test_ht, -1);
461 }
462 rcu_read_unlock();
463 }
464 #endif //0
465 nr_writes++;
466 if (unlikely(!test_duration_write()))
467 break;
468 if (unlikely(wdelay))
469 loop_sleep(wdelay);
470 }
471
472 rcu_unregister_thread();
473
474 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
475 "writer", pthread_self(), (unsigned long)gettid());
476 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
477 "nr_delnoent %lu\n", pthread_self(), nr_add,
478 nr_addexist, nr_del, nr_delnoent);
479 count->update_ops = nr_writes;
480 count->add = nr_add;
481 count->add_exist = nr_addexist;
482 count->remove = nr_del;
483 return ((void*)2);
484 }
485
486 static void populate_hash(void)
487 {
488 struct cds_lfht_node *node, *lookup_node;
489 void *key;
490
491 if (!init_populate)
492 return;
493
494 while (nr_add < init_populate) {
495 node = malloc(1);
496
497 /* Glib hash table only supports replacement. */
498 if (!add_unique) {
499 add_unique = 1; /* force add_unique */
500 printf("glib hash tables only supports "
501 "replacing values (and keys in addition) when the key to insert is already "
502 "present. Make sure you compare with the \"add_unique\" (-u) RCU hash table "
503 "behavior, which is the closest match.\n");
504 }
505 /* Uniquify behavior: lookup and add if not found */
506 key = (void *)(unsigned long)(rand_r(&rand_lookup) % rand_pool);
507 lookup_node = g_hash_table_lookup(test_ht, key);
508 if (lookup_node == NULL) {
509 g_hash_table_insert(test_ht, key, node);
510 nr_add++;
511 } else {
512 free(node);
513 nr_addexist++;
514 }
515 nr_writes++;
516 }
517 }
518
519 static
520 void count_nodes(gpointer key, gpointer value, gpointer user_data)
521 {
522 unsigned long *count = user_data;
523 (*count)++;
524 }
525
526 void show_usage(int argc, char **argv)
527 {
528 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
529 #ifdef DEBUG_YIELD
530 printf(" [-r] [-w] (yield reader and/or writer)");
531 #endif
532 printf(" [-d delay] (writer period (us))");
533 printf(" [-c duration] (reader C.S. duration (in loops))");
534 printf(" [-v] (verbose output)");
535 printf(" [-a cpu#] [-a cpu#]... (affinity)");
536 printf(" [-p size] (random key value pool size)");
537 printf(" [-h size] (initial hash table size)");
538 printf(" [-u] Uniquify add.");
539 printf(" [-i] Add only (no removal).");
540 printf(" [-k nr_nodes] Number of nodes to insert initially.");
541 printf("\n");
542 }
543
544 int main(int argc, char **argv)
545 {
546 int err;
547 pthread_t *tid_reader, *tid_writer;
548 void *tret;
549 unsigned long long *count_reader;
550 struct wr_count *count_writer;
551 unsigned long long tot_reads = 0, tot_writes = 0,
552 tot_add = 0, tot_add_exist = 0, tot_remove = 0;
553 unsigned long count;
554 int i, a, ret;
555
556 if (argc < 4) {
557 show_usage(argc, argv);
558 return -1;
559 }
560
561 err = sscanf(argv[1], "%u", &nr_readers);
562 if (err != 1) {
563 show_usage(argc, argv);
564 return -1;
565 }
566
567 err = sscanf(argv[2], "%u", &nr_writers);
568 if (err != 1) {
569 show_usage(argc, argv);
570 return -1;
571 }
572
573 err = sscanf(argv[3], "%lu", &duration);
574 if (err != 1) {
575 show_usage(argc, argv);
576 return -1;
577 }
578
579 for (i = 4; i < argc; i++) {
580 if (argv[i][0] != '-')
581 continue;
582 switch (argv[i][1]) {
583 #ifdef DEBUG_YIELD
584 case 'r':
585 yield_active |= YIELD_READ;
586 break;
587 case 'w':
588 yield_active |= YIELD_WRITE;
589 break;
590 #endif
591 case 'a':
592 if (argc < i + 2) {
593 show_usage(argc, argv);
594 return -1;
595 }
596 a = atoi(argv[++i]);
597 cpu_affinities[next_aff++] = a;
598 use_affinity = 1;
599 printf_verbose("Adding CPU %d affinity\n", a);
600 break;
601 case 'c':
602 if (argc < i + 2) {
603 show_usage(argc, argv);
604 return -1;
605 }
606 rduration = atol(argv[++i]);
607 break;
608 case 'd':
609 if (argc < i + 2) {
610 show_usage(argc, argv);
611 return -1;
612 }
613 wdelay = atol(argv[++i]);
614 break;
615 case 'v':
616 verbose_mode = 1;
617 break;
618 case 'p':
619 if (argc < i + 2) {
620 show_usage(argc, argv);
621 return -1;
622 }
623 rand_pool = atol(argv[++i]);
624 break;
625 case 'h':
626 if (argc < i + 2) {
627 show_usage(argc, argv);
628 return -1;
629 }
630 init_hash_size = atol(argv[++i]);
631 break;
632 case 'u':
633 add_unique = 1;
634 break;
635 case 'i':
636 add_only = 1;
637 break;
638 case 'k':
639 init_populate = atol(argv[++i]);
640 break;
641 }
642 }
643
644 /* Check if hash size is power of 2 */
645 if (init_hash_size && init_hash_size & (init_hash_size - 1)) {
646 printf("Error: Hash table size %lu is not a power of 2.\n",
647 init_hash_size);
648 return -1;
649 }
650
651 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
652 duration, nr_readers, nr_writers);
653 printf_verbose("Writer delay : %lu loops.\n", wdelay);
654 printf_verbose("Reader duration : %lu loops.\n", rduration);
655 printf_verbose("Random pool size : %lu.\n", rand_pool);
656 printf_verbose("Mode:%s%s.\n",
657 add_only ? " add only" : " add/remove",
658 add_unique ? " uniquify" : "");
659 printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size);
660 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
661 "main", pthread_self(), (unsigned long)gettid());
662
663 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
664 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
665 count_reader = malloc(sizeof(*count_reader) * nr_readers);
666 count_writer = malloc(sizeof(*count_writer) * nr_writers);
667 test_ht = g_hash_table_new_full(test_hash_fct, test_compare_fct,
668 NULL, free);
669 populate_hash();
670 err = create_all_cpu_call_rcu_data(0);
671 assert(!err);
672
673 next_aff = 0;
674
675 for (i = 0; i < nr_readers; i++) {
676 err = pthread_create(&tid_reader[i], NULL, thr_reader,
677 &count_reader[i]);
678 if (err != 0)
679 exit(1);
680 }
681 for (i = 0; i < nr_writers; i++) {
682 err = pthread_create(&tid_writer[i], NULL, thr_writer,
683 &count_writer[i]);
684 if (err != 0)
685 exit(1);
686 }
687
688 cmm_smp_mb();
689
690 test_go = 1;
691
692 sleep(duration);
693
694 test_stop = 1;
695
696 for (i = 0; i < nr_readers; i++) {
697 err = pthread_join(tid_reader[i], &tret);
698 if (err != 0)
699 exit(1);
700 tot_reads += count_reader[i];
701 }
702 for (i = 0; i < nr_writers; i++) {
703 err = pthread_join(tid_writer[i], &tret);
704 if (err != 0)
705 exit(1);
706 tot_writes += count_writer[i].update_ops;
707 tot_add += count_writer[i].add;
708 tot_add_exist += count_writer[i].add_exist;
709 tot_remove += count_writer[i].remove;
710 }
711 printf("Counting nodes... ");
712 fflush(stdout);
713 count = 0;
714 g_hash_table_foreach(test_ht, count_nodes, &count);
715 printf("done.\n");
716 if (count)
717 printf("WARNING: nodes left in the hash table upon destroy: "
718 "%lu nodes.\n", count);
719 g_hash_table_destroy(test_ht);
720
721 if (ret)
722 printf_verbose("final delete aborted\n");
723 else
724 printf_verbose("final delete success\n");
725 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
726 tot_writes);
727 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
728 "nr_writers %3u "
729 "wdelay %6lu rand_pool %12llu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
730 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
731 argv[0], duration, nr_readers, rduration,
732 nr_writers, wdelay, rand_pool, tot_reads, tot_writes,
733 tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove,
734 (long long) tot_add + init_populate - tot_remove - count);
735 free(tid_reader);
736 free(tid_writer);
737 free(count_reader);
738 free(count_writer);
739 return 0;
740 }
This page took 0.096469 seconds and 5 git commands to generate.