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