3043557aba96fe8644f567091fece942ac15bc20
[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
179 ret = pthread_mutex_lock(&rcu_copy_mutex);
180 if (ret) {
181 perror("Error in pthread mutex lock");
182 exit(-1);
183 }
184 }
185
186 void rcu_copy_mutex_unlock(void)
187 {
188 int ret;
189
190 ret = pthread_mutex_unlock(&rcu_copy_mutex);
191 if (ret) {
192 perror("Error in pthread mutex unlock");
193 exit(-1);
194 }
195 }
196
197 /*
198 * Hash function
199 * Source: http://burtleburtle.net/bob/c/lookup3.c
200 * Originally Public Domain
201 */
202
203 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
204
205 #define mix(a, b, c) \
206 do { \
207 a -= c; a ^= rot(c, 4); c += b; \
208 b -= a; b ^= rot(a, 6); a += c; \
209 c -= b; c ^= rot(b, 8); b += a; \
210 a -= c; a ^= rot(c, 16); c += b; \
211 b -= a; b ^= rot(a, 19); a += c; \
212 c -= b; c ^= rot(b, 4); b += a; \
213 } while (0)
214
215 #define final(a, b, c) \
216 { \
217 c ^= b; c -= rot(b, 14); \
218 a ^= c; a -= rot(c, 11); \
219 b ^= a; b -= rot(a, 25); \
220 c ^= b; c -= rot(b, 16); \
221 a ^= c; a -= rot(c, 4);\
222 b ^= a; b -= rot(a, 14); \
223 c ^= b; c -= rot(b, 24); \
224 }
225
226 static __attribute__((unused))
227 uint32_t hash_u32(
228 const uint32_t *k, /* the key, an array of uint32_t values */
229 size_t length, /* the length of the key, in uint32_ts */
230 uint32_t initval) /* the previous hash, or an arbitrary value */
231 {
232 uint32_t a, b, c;
233
234 /* Set up the internal state */
235 a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval;
236
237 /*----------------------------------------- handle most of the key */
238 while (length > 3) {
239 a += k[0];
240 b += k[1];
241 c += k[2];
242 mix(a, b, c);
243 length -= 3;
244 k += 3;
245 }
246
247 /*----------------------------------- handle the last 3 uint32_t's */
248 switch (length) { /* all the case statements fall through */
249 case 3: c += k[2];
250 case 2: b += k[1];
251 case 1: a += k[0];
252 final(a, b, c);
253 case 0: /* case 0: nothing left to add */
254 break;
255 }
256 /*---------------------------------------------- report the result */
257 return c;
258 }
259
260 static
261 void hashword2(
262 const uint32_t *k, /* the key, an array of uint32_t values */
263 size_t length, /* the length of the key, in uint32_ts */
264 uint32_t *pc, /* IN: seed OUT: primary hash value */
265 uint32_t *pb) /* IN: more seed OUT: secondary hash value */
266 {
267 uint32_t a, b, c;
268
269 /* Set up the internal state */
270 a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc;
271 c += *pb;
272
273 /*----------------------------------------- handle most of the key */
274 while (length > 3) {
275 a += k[0];
276 b += k[1];
277 c += k[2];
278 mix(a, b, c);
279 length -= 3;
280 k += 3;
281 }
282
283 /*----------------------------------- handle the last 3 uint32_t's */
284 switch (length) { /* all the case statements fall through */
285 case 3: c += k[2];
286 case 2: b += k[1];
287 case 1: a += k[0];
288 final(a, b, c);
289 case 0: /* case 0: nothing left to add */
290 break;
291 }
292 /*---------------------------------------------- report the result */
293 *pc = c;
294 *pb = b;
295 }
296
297 #if (CAA_BITS_PER_LONG == 32)
298 static
299 unsigned long test_hash(const void *_key, size_t length, unsigned long seed)
300 {
301 unsigned long key = (unsigned long) _key;
302 unsigned long v;
303
304 assert(length == sizeof(unsigned long));
305 return hash_u32(&v, 1, seed);
306 }
307 #else
308 static
309 unsigned long test_hash(const void *_key, size_t length, unsigned long seed)
310 {
311 union {
312 uint64_t v64;
313 uint32_t v32[2];
314 } v;
315 union {
316 uint64_t v64;
317 uint32_t v32[2];
318 } key;
319
320 assert(length == sizeof(unsigned long));
321 v.v64 = (uint64_t) seed;
322 key.v64 = (uint64_t) _key;
323 hashword2(key.v32, 2, &v.v32[0], &v.v32[1]);
324 return v.v64;
325 }
326 #endif
327
328 static
329 unsigned long test_compare(const void *key1, size_t key1_len,
330 const void *key2, size_t key2_len)
331 {
332 if (unlikely(key1_len != key2_len))
333 return -1;
334 assert(key1_len == sizeof(unsigned long));
335 if (key1 == key2)
336 return 0;
337 else
338 return 1;
339 }
340
341 static
342 guint test_hash_fct(const void *key)
343 {
344 return test_hash(key, sizeof(void *), 0x42);
345 }
346
347 static
348 gboolean test_compare_fct(const void *a, const void *b)
349 {
350 return !test_compare(a, sizeof(void *), b, sizeof(void *));
351 }
352
353 void *thr_reader(void *_count)
354 {
355 unsigned long long *count = _count;
356 void *node;
357
358 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
359 "reader", pthread_self(), (unsigned long)gettid());
360
361 set_affinity();
362
363 rcu_register_thread();
364
365 while (!test_go)
366 {
367 }
368 cmm_smp_mb();
369
370 for (;;) {
371 rcu_copy_mutex_lock();
372 node = g_hash_table_lookup(test_ht,
373 (void *)(unsigned long)(rand_r(&rand_lookup) % rand_pool));
374 if (node == NULL)
375 lookup_fail++;
376 else
377 lookup_ok++;
378 debug_yield_read();
379 if (unlikely(rduration))
380 loop_sleep(rduration);
381 rcu_copy_mutex_unlock();
382 nr_reads++;
383 if (unlikely(!test_duration_read()))
384 break;
385 }
386
387 rcu_unregister_thread();
388
389 *count = nr_reads;
390 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
391 "reader", pthread_self(), (unsigned long)gettid());
392 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
393 pthread_self(), lookup_fail, lookup_ok);
394 return ((void*)1);
395
396 }
397
398 void *thr_writer(void *_count)
399 {
400 void *node, *ret_node, *key;
401 struct wr_count *count = _count;
402 int ret;
403
404 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
405 "writer", pthread_self(), (unsigned long)gettid());
406
407 set_affinity();
408
409 rcu_register_thread();
410
411 while (!test_go)
412 {
413 }
414 cmm_smp_mb();
415
416 for (;;) {
417 if (add_only || rand_r(&rand_lookup) & 1) {
418 void *lookup_node;
419
420 node = malloc(1);
421
422 rcu_copy_mutex_lock();
423 /* Glib hash table only supports replacement. */
424 if (!add_unique) {
425 add_unique = 1; /* force add_unique */
426 printf("glib hash tables only supports "
427 "replacing values (and keys in addition) when the key to insert is already "
428 "present. Make sure you compare with the \"add_unique\" (-u) RCU hash table "
429 "behavior, which is the closest match.\n");
430 }
431 /* Uniquify behavior: lookup and add if not found */
432 key = (void *)(unsigned long)(rand_r(&rand_lookup) % rand_pool);
433 lookup_node = g_hash_table_lookup(test_ht, key);
434 if (lookup_node == NULL) {
435 g_hash_table_insert(test_ht, key, node);
436 rcu_copy_mutex_unlock();
437 nr_add++;
438 } else {
439 rcu_copy_mutex_unlock();
440 free(node);
441 nr_addexist++;
442 }
443 } else {
444 /* May delete */
445 rcu_copy_mutex_lock();
446 ret = g_hash_table_remove(test_ht,
447 (void *)(unsigned long)(rand_r(&rand_lookup) % rand_pool));
448 rcu_copy_mutex_unlock();
449 if (ret) {
450 nr_del++;
451 } else
452 nr_delnoent++;
453 }
454 #if 0
455 //if (nr_writes % 100000 == 0) {
456 if (nr_writes % 1000 == 0) {
457 rcu_read_lock();
458 if (rand_r(&rand_lookup) & 1) {
459 ht_resize(test_ht, 1);
460 } else {
461 ht_resize(test_ht, -1);
462 }
463 rcu_read_unlock();
464 }
465 #endif //0
466 nr_writes++;
467 if (unlikely(!test_duration_write()))
468 break;
469 if (unlikely(wdelay))
470 loop_sleep(wdelay);
471 }
472
473 rcu_unregister_thread();
474
475 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
476 "writer", pthread_self(), (unsigned long)gettid());
477 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
478 "nr_delnoent %lu\n", pthread_self(), nr_add,
479 nr_addexist, nr_del, nr_delnoent);
480 count->update_ops = nr_writes;
481 count->add = nr_add;
482 count->add_exist = nr_addexist;
483 count->remove = nr_del;
484 return ((void*)2);
485 }
486
487 static int populate_hash(void)
488 {
489 struct cds_lfht_node *node, *lookup_node;
490 void *key;
491
492 if (!init_populate)
493 return 0;
494
495 if (add_unique && init_populate * 10 > rand_pool) {
496 printf("WARNING: required to populate %lu nodes (-k), but random"
497 "pool is quite small (%lu values) and we are in add_unique (-u) mode. Try with a "
498 "larger random pool (-p option).\n", init_populate, rand_pool);
499 return -1;
500 }
501
502 while (nr_add < init_populate) {
503 node = malloc(1);
504
505 /* Glib hash table only supports replacement. */
506 if (!add_unique) {
507 add_unique = 1; /* force add_unique */
508 printf("glib hash tables only supports "
509 "replacing values (and keys in addition) when the key to insert is already "
510 "present. Make sure you compare with the \"add_unique\" (-u) RCU hash table "
511 "behavior, which is the closest match.\n");
512 }
513 /* Uniquify behavior: lookup and add if not found */
514 key = (void *)(unsigned long)(rand_r(&rand_lookup) % rand_pool);
515 lookup_node = g_hash_table_lookup(test_ht, key);
516 if (lookup_node == NULL) {
517 g_hash_table_insert(test_ht, key, node);
518 nr_add++;
519 } else {
520 free(node);
521 nr_addexist++;
522 }
523 nr_writes++;
524 }
525 return 0;
526 }
527
528 static
529 void count_nodes(gpointer key, gpointer value, gpointer user_data)
530 {
531 unsigned long *count = user_data;
532 (*count)++;
533 }
534
535 void show_usage(int argc, char **argv)
536 {
537 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
538 #ifdef DEBUG_YIELD
539 printf(" [-r] [-w] (yield reader and/or writer)");
540 #endif
541 printf(" [-d delay] (writer period (us))");
542 printf(" [-c duration] (reader C.S. duration (in loops))");
543 printf(" [-v] (verbose output)");
544 printf(" [-a cpu#] [-a cpu#]... (affinity)");
545 printf(" [-p size] (random key value pool size)");
546 printf(" [-h size] (initial hash table size)");
547 printf(" [-u] Uniquify add.");
548 printf(" [-i] Add only (no removal).");
549 printf(" [-k nr_nodes] Number of nodes to insert initially.");
550 printf("\n");
551 }
552
553 int main(int argc, char **argv)
554 {
555 int err;
556 pthread_t *tid_reader, *tid_writer;
557 void *tret;
558 unsigned long long *count_reader;
559 struct wr_count *count_writer;
560 unsigned long long tot_reads = 0, tot_writes = 0,
561 tot_add = 0, tot_add_exist = 0, tot_remove = 0;
562 unsigned long count;
563 int i, a, ret;
564
565 if (argc < 4) {
566 show_usage(argc, argv);
567 return -1;
568 }
569
570 err = sscanf(argv[1], "%u", &nr_readers);
571 if (err != 1) {
572 show_usage(argc, argv);
573 return -1;
574 }
575
576 err = sscanf(argv[2], "%u", &nr_writers);
577 if (err != 1) {
578 show_usage(argc, argv);
579 return -1;
580 }
581
582 err = sscanf(argv[3], "%lu", &duration);
583 if (err != 1) {
584 show_usage(argc, argv);
585 return -1;
586 }
587
588 for (i = 4; i < argc; i++) {
589 if (argv[i][0] != '-')
590 continue;
591 switch (argv[i][1]) {
592 #ifdef DEBUG_YIELD
593 case 'r':
594 yield_active |= YIELD_READ;
595 break;
596 case 'w':
597 yield_active |= YIELD_WRITE;
598 break;
599 #endif
600 case 'a':
601 if (argc < i + 2) {
602 show_usage(argc, argv);
603 return -1;
604 }
605 a = atoi(argv[++i]);
606 cpu_affinities[next_aff++] = a;
607 use_affinity = 1;
608 printf_verbose("Adding CPU %d affinity\n", a);
609 break;
610 case 'c':
611 if (argc < i + 2) {
612 show_usage(argc, argv);
613 return -1;
614 }
615 rduration = atol(argv[++i]);
616 break;
617 case 'd':
618 if (argc < i + 2) {
619 show_usage(argc, argv);
620 return -1;
621 }
622 wdelay = atol(argv[++i]);
623 break;
624 case 'v':
625 verbose_mode = 1;
626 break;
627 case 'p':
628 if (argc < i + 2) {
629 show_usage(argc, argv);
630 return -1;
631 }
632 rand_pool = atol(argv[++i]);
633 break;
634 case 'h':
635 if (argc < i + 2) {
636 show_usage(argc, argv);
637 return -1;
638 }
639 init_hash_size = atol(argv[++i]);
640 break;
641 case 'u':
642 add_unique = 1;
643 break;
644 case 'i':
645 add_only = 1;
646 break;
647 case 'k':
648 init_populate = atol(argv[++i]);
649 break;
650 }
651 }
652
653 /* Check if hash size is power of 2 */
654 if (init_hash_size && init_hash_size & (init_hash_size - 1)) {
655 printf("Error: Hash table size %lu is not a power of 2.\n",
656 init_hash_size);
657 return -1;
658 }
659
660 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
661 duration, nr_readers, nr_writers);
662 printf_verbose("Writer delay : %lu loops.\n", wdelay);
663 printf_verbose("Reader duration : %lu loops.\n", rduration);
664 printf_verbose("Random pool size : %lu.\n", rand_pool);
665 printf_verbose("Mode:%s%s.\n",
666 add_only ? " add only" : " add/remove",
667 add_unique ? " uniquify" : "");
668 printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size);
669 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
670 "main", pthread_self(), (unsigned long)gettid());
671
672 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
673 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
674 count_reader = malloc(sizeof(*count_reader) * nr_readers);
675 count_writer = malloc(sizeof(*count_writer) * nr_writers);
676 test_ht = g_hash_table_new_full(test_hash_fct, test_compare_fct,
677 NULL, free);
678 ret = populate_hash();
679 assert(!ret);
680 err = create_all_cpu_call_rcu_data(0);
681 assert(!err);
682
683 next_aff = 0;
684
685 for (i = 0; i < nr_readers; i++) {
686 err = pthread_create(&tid_reader[i], NULL, thr_reader,
687 &count_reader[i]);
688 if (err != 0)
689 exit(1);
690 }
691 for (i = 0; i < nr_writers; i++) {
692 err = pthread_create(&tid_writer[i], NULL, thr_writer,
693 &count_writer[i]);
694 if (err != 0)
695 exit(1);
696 }
697
698 cmm_smp_mb();
699
700 test_go = 1;
701
702 sleep(duration);
703
704 test_stop = 1;
705
706 for (i = 0; i < nr_readers; i++) {
707 err = pthread_join(tid_reader[i], &tret);
708 if (err != 0)
709 exit(1);
710 tot_reads += count_reader[i];
711 }
712 for (i = 0; i < nr_writers; i++) {
713 err = pthread_join(tid_writer[i], &tret);
714 if (err != 0)
715 exit(1);
716 tot_writes += count_writer[i].update_ops;
717 tot_add += count_writer[i].add;
718 tot_add_exist += count_writer[i].add_exist;
719 tot_remove += count_writer[i].remove;
720 }
721 printf("Counting nodes... ");
722 fflush(stdout);
723 count = 0;
724 g_hash_table_foreach(test_ht, count_nodes, &count);
725 printf("done.\n");
726 if (count)
727 printf("WARNING: nodes left in the hash table upon destroy: "
728 "%lu nodes.\n", count);
729 g_hash_table_destroy(test_ht);
730
731 if (ret)
732 printf_verbose("final delete aborted\n");
733 else
734 printf_verbose("final delete success\n");
735 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
736 tot_writes);
737 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
738 "nr_writers %3u "
739 "wdelay %6lu rand_pool %12llu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
740 "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n",
741 argv[0], duration, nr_readers, rduration,
742 nr_writers, wdelay, rand_pool, tot_reads, tot_writes,
743 tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove,
744 (long long) tot_add + init_populate - tot_remove - count);
745 free(tid_reader);
746 free(tid_writer);
747 free(count_reader);
748 free(count_writer);
749 return 0;
750 }
This page took 0.042248 seconds and 3 git commands to generate.