Commit | Line | Data |
---|---|---|
ab7d5fc6 | 1 | /* |
cd1ae16a | 2 | * test_urcu_hash.c |
ab7d5fc6 MD |
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> | |
ab7d5fc6 | 33 | #include <sched.h> |
5e28c532 | 34 | #include <errno.h> |
ab7d5fc6 | 35 | |
abc490a1 MD |
36 | #ifdef __linux__ |
37 | #include <syscall.h> | |
38 | #endif | |
ab7d5fc6 | 39 | |
6d5c0ca9 MD |
40 | #define DEFAULT_HASH_SIZE 32 |
41 | #define DEFAULT_RAND_POOL 1000000 | |
5e28c532 | 42 | |
ab7d5fc6 MD |
43 | /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */ |
44 | #define CACHE_LINE_SIZE 4096 | |
45 | ||
46 | /* hardcoded number of CPUs */ | |
47 | #define NR_CPUS 16384 | |
48 | ||
98808fb1 MD |
49 | #ifdef POISON_FREE |
50 | #define poison_free(ptr) \ | |
51 | do { \ | |
52 | memset(ptr, 0x42, sizeof(*(ptr))); \ | |
53 | free(ptr); \ | |
54 | } while (0) | |
55 | #else | |
56 | #define poison_free(ptr) free(ptr) | |
57 | #endif | |
58 | ||
59 | ||
60 | ||
ab7d5fc6 MD |
61 | #if defined(_syscall0) |
62 | _syscall0(pid_t, gettid) | |
63 | #elif defined(__NR_gettid) | |
64 | static inline pid_t gettid(void) | |
65 | { | |
66 | return syscall(__NR_gettid); | |
67 | } | |
68 | #else | |
69 | #warning "use pid as tid" | |
70 | static inline pid_t gettid(void) | |
71 | { | |
72 | return getpid(); | |
73 | } | |
74 | #endif | |
75 | ||
76 | #ifndef DYNAMIC_LINK_TEST | |
77 | #define _LGPL_SOURCE | |
78 | #else | |
79 | #define debug_yield_read() | |
80 | #endif | |
5567839e | 81 | #include <urcu-qsbr.h> |
abc490a1 MD |
82 | #include <urcu/rculfhash.h> |
83 | #include <urcu-call-rcu.h> | |
ab7d5fc6 | 84 | |
b8e1907c MD |
85 | struct wr_count { |
86 | unsigned long update_ops; | |
87 | unsigned long add; | |
3c16bf4b | 88 | unsigned long add_exist; |
b8e1907c MD |
89 | unsigned long remove; |
90 | }; | |
91 | ||
5e28c532 MD |
92 | static unsigned int __thread rand_lookup; |
93 | static unsigned long __thread nr_add; | |
94 | static unsigned long __thread nr_addexist; | |
95 | static unsigned long __thread nr_del; | |
96 | static unsigned long __thread nr_delnoent; | |
97 | static unsigned long __thread lookup_fail; | |
98 | static unsigned long __thread lookup_ok; | |
99 | ||
14044b37 | 100 | static struct cds_lfht *test_ht; |
ab7d5fc6 | 101 | |
5e28c532 | 102 | struct test_data { |
ab7d5fc6 | 103 | int a; |
5e28c532 | 104 | int b; |
ab7d5fc6 MD |
105 | }; |
106 | ||
107 | static volatile int test_go, test_stop; | |
108 | ||
109 | static unsigned long wdelay; | |
110 | ||
ab7d5fc6 MD |
111 | static unsigned long duration; |
112 | ||
113 | /* read-side C.S. duration, in loops */ | |
114 | static unsigned long rduration; | |
115 | ||
6d5c0ca9 | 116 | static unsigned long init_hash_size = DEFAULT_HASH_SIZE; |
cd1ae16a | 117 | static unsigned long init_populate; |
6d5c0ca9 | 118 | static unsigned long rand_pool = DEFAULT_RAND_POOL; |
151e7a93 | 119 | static int opt_auto_resize; |
6d5c0ca9 MD |
120 | static int add_only, add_unique; |
121 | ||
8008a032 MD |
122 | static unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset; |
123 | ||
ab7d5fc6 MD |
124 | static inline void loop_sleep(unsigned long l) |
125 | { | |
126 | while(l-- != 0) | |
abc490a1 | 127 | caa_cpu_relax(); |
ab7d5fc6 MD |
128 | } |
129 | ||
130 | static int verbose_mode; | |
131 | ||
132 | #define printf_verbose(fmt, args...) \ | |
133 | do { \ | |
134 | if (verbose_mode) \ | |
33c7c748 | 135 | printf(fmt, ## args); \ |
ab7d5fc6 MD |
136 | } while (0) |
137 | ||
138 | static unsigned int cpu_affinities[NR_CPUS]; | |
139 | static unsigned int next_aff = 0; | |
140 | static int use_affinity = 0; | |
141 | ||
142 | pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; | |
143 | ||
144 | static void set_affinity(void) | |
145 | { | |
146 | cpu_set_t mask; | |
147 | int cpu; | |
148 | int ret; | |
149 | ||
150 | if (!use_affinity) | |
151 | return; | |
152 | ||
153 | ret = pthread_mutex_lock(&affinity_mutex); | |
154 | if (ret) { | |
155 | perror("Error in pthread mutex lock"); | |
156 | exit(-1); | |
157 | } | |
158 | cpu = cpu_affinities[next_aff++]; | |
159 | ret = pthread_mutex_unlock(&affinity_mutex); | |
160 | if (ret) { | |
161 | perror("Error in pthread mutex unlock"); | |
162 | exit(-1); | |
163 | } | |
164 | CPU_ZERO(&mask); | |
165 | CPU_SET(cpu, &mask); | |
166 | sched_setaffinity(0, sizeof(mask), &mask); | |
167 | } | |
168 | ||
3967a8a8 MD |
169 | static enum { |
170 | AR_RANDOM = 0, | |
171 | AR_ADD = 1, | |
172 | AR_REMOVE = -1, | |
173 | } addremove; /* 1: add, -1 remove, 0: random */ | |
174 | ||
175 | static | |
176 | void sigusr1_handler(int signo) | |
177 | { | |
178 | switch (addremove) { | |
179 | case AR_ADD: | |
180 | printf("Add/Remove: random.\n"); | |
181 | addremove = AR_RANDOM; | |
182 | break; | |
183 | case AR_RANDOM: | |
184 | printf("Add/Remove: remove only.\n"); | |
185 | addremove = AR_REMOVE; | |
186 | break; | |
187 | case AR_REMOVE: | |
188 | printf("Add/Remove: add only.\n"); | |
189 | addremove = AR_ADD; | |
190 | break; | |
191 | } | |
192 | } | |
193 | ||
ab7d5fc6 MD |
194 | /* |
195 | * returns 0 if test should end. | |
196 | */ | |
197 | static int test_duration_write(void) | |
198 | { | |
199 | return !test_stop; | |
200 | } | |
201 | ||
202 | static int test_duration_read(void) | |
203 | { | |
204 | return !test_stop; | |
205 | } | |
206 | ||
207 | static unsigned long long __thread nr_writes; | |
208 | static unsigned long long __thread nr_reads; | |
209 | ||
210 | static unsigned int nr_readers; | |
211 | static unsigned int nr_writers; | |
212 | ||
213 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; | |
214 | ||
215 | void rcu_copy_mutex_lock(void) | |
216 | { | |
217 | int ret; | |
218 | ret = pthread_mutex_lock(&rcu_copy_mutex); | |
219 | if (ret) { | |
220 | perror("Error in pthread mutex lock"); | |
221 | exit(-1); | |
222 | } | |
223 | } | |
224 | ||
225 | void rcu_copy_mutex_unlock(void) | |
226 | { | |
227 | int ret; | |
228 | ||
229 | ret = pthread_mutex_unlock(&rcu_copy_mutex); | |
230 | if (ret) { | |
231 | perror("Error in pthread mutex unlock"); | |
232 | exit(-1); | |
233 | } | |
234 | } | |
235 | ||
732ad076 MD |
236 | /* |
237 | * Hash function | |
238 | * Source: http://burtleburtle.net/bob/c/lookup3.c | |
239 | * Originally Public Domain | |
240 | */ | |
241 | ||
242 | #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) | |
243 | ||
244 | #define mix(a, b, c) \ | |
245 | do { \ | |
246 | a -= c; a ^= rot(c, 4); c += b; \ | |
247 | b -= a; b ^= rot(a, 6); a += c; \ | |
248 | c -= b; c ^= rot(b, 8); b += a; \ | |
249 | a -= c; a ^= rot(c, 16); c += b; \ | |
250 | b -= a; b ^= rot(a, 19); a += c; \ | |
251 | c -= b; c ^= rot(b, 4); b += a; \ | |
252 | } while (0) | |
253 | ||
254 | #define final(a, b, c) \ | |
255 | { \ | |
256 | c ^= b; c -= rot(b, 14); \ | |
257 | a ^= c; a -= rot(c, 11); \ | |
258 | b ^= a; b -= rot(a, 25); \ | |
259 | c ^= b; c -= rot(b, 16); \ | |
260 | a ^= c; a -= rot(c, 4);\ | |
261 | b ^= a; b -= rot(a, 14); \ | |
262 | c ^= b; c -= rot(b, 24); \ | |
263 | } | |
264 | ||
265 | static __attribute__((unused)) | |
266 | uint32_t hash_u32( | |
267 | const uint32_t *k, /* the key, an array of uint32_t values */ | |
268 | size_t length, /* the length of the key, in uint32_ts */ | |
269 | uint32_t initval) /* the previous hash, or an arbitrary value */ | |
270 | { | |
271 | uint32_t a, b, c; | |
272 | ||
273 | /* Set up the internal state */ | |
274 | a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval; | |
275 | ||
276 | /*----------------------------------------- handle most of the key */ | |
277 | while (length > 3) { | |
278 | a += k[0]; | |
279 | b += k[1]; | |
280 | c += k[2]; | |
281 | mix(a, b, c); | |
282 | length -= 3; | |
283 | k += 3; | |
284 | } | |
285 | ||
286 | /*----------------------------------- handle the last 3 uint32_t's */ | |
287 | switch (length) { /* all the case statements fall through */ | |
288 | case 3: c += k[2]; | |
289 | case 2: b += k[1]; | |
290 | case 1: a += k[0]; | |
291 | final(a, b, c); | |
292 | case 0: /* case 0: nothing left to add */ | |
293 | break; | |
294 | } | |
295 | /*---------------------------------------------- report the result */ | |
296 | return c; | |
297 | } | |
298 | ||
299 | static | |
300 | void hashword2( | |
301 | const uint32_t *k, /* the key, an array of uint32_t values */ | |
302 | size_t length, /* the length of the key, in uint32_ts */ | |
303 | uint32_t *pc, /* IN: seed OUT: primary hash value */ | |
304 | uint32_t *pb) /* IN: more seed OUT: secondary hash value */ | |
305 | { | |
306 | uint32_t a, b, c; | |
307 | ||
308 | /* Set up the internal state */ | |
309 | a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc; | |
310 | c += *pb; | |
311 | ||
312 | /*----------------------------------------- handle most of the key */ | |
313 | while (length > 3) { | |
314 | a += k[0]; | |
315 | b += k[1]; | |
316 | c += k[2]; | |
317 | mix(a, b, c); | |
318 | length -= 3; | |
319 | k += 3; | |
320 | } | |
321 | ||
322 | /*----------------------------------- handle the last 3 uint32_t's */ | |
323 | switch (length) { /* all the case statements fall through */ | |
324 | case 3: c += k[2]; | |
325 | case 2: b += k[1]; | |
326 | case 1: a += k[0]; | |
327 | final(a, b, c); | |
328 | case 0: /* case 0: nothing left to add */ | |
329 | break; | |
330 | } | |
331 | /*---------------------------------------------- report the result */ | |
332 | *pc = c; | |
333 | *pb = b; | |
334 | } | |
335 | ||
336 | #if (CAA_BITS_PER_LONG == 32) | |
abc490a1 | 337 | static |
732ad076 | 338 | unsigned long test_hash(void *_key, size_t length, unsigned long seed) |
abc490a1 | 339 | { |
abc490a1 MD |
340 | unsigned long key = (unsigned long) _key; |
341 | unsigned long v; | |
342 | ||
732ad076 MD |
343 | assert(length == sizeof(unsigned long)); |
344 | return hash_u32(&v, 1, seed); | |
345 | } | |
346 | #else | |
347 | static | |
348 | unsigned long test_hash(void *_key, size_t length, unsigned long seed) | |
349 | { | |
350 | union { | |
351 | uint64_t v64; | |
352 | uint32_t v32[2]; | |
353 | } v; | |
354 | union { | |
355 | uint64_t v64; | |
356 | uint32_t v32[2]; | |
357 | } key; | |
358 | ||
359 | assert(length == sizeof(unsigned long)); | |
360 | v.v64 = (uint64_t) seed; | |
361 | key.v64 = (uint64_t) _key; | |
362 | hashword2(key.v32, 2, &v.v32[0], &v.v32[1]); | |
363 | return v.v64; | |
364 | } | |
365 | #endif | |
abc490a1 | 366 | |
732ad076 MD |
367 | static |
368 | unsigned long test_compare(void *key1, size_t key1_len, | |
369 | void *key2, size_t key2_len) | |
370 | { | |
371 | if (unlikely(key1_len != key2_len)) | |
372 | return -1; | |
373 | assert(key1_len == sizeof(unsigned long)); | |
374 | if (key1 == key2) | |
375 | return 0; | |
376 | else | |
377 | return 1; | |
abc490a1 | 378 | } |
ab7d5fc6 MD |
379 | |
380 | void *thr_reader(void *_count) | |
381 | { | |
382 | unsigned long long *count = _count; | |
14044b37 | 383 | struct cds_lfht_node *node; |
ab7d5fc6 MD |
384 | |
385 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
386 | "reader", pthread_self(), (unsigned long)gettid()); | |
387 | ||
388 | set_affinity(); | |
389 | ||
390 | rcu_register_thread(); | |
391 | ||
392 | while (!test_go) | |
393 | { | |
394 | } | |
abc490a1 | 395 | cmm_smp_mb(); |
ab7d5fc6 MD |
396 | |
397 | for (;;) { | |
398 | rcu_read_lock(); | |
14044b37 | 399 | node = cds_lfht_lookup(test_ht, |
8008a032 | 400 | (void *)(unsigned long)((rand_r(&rand_lookup) % rand_pool) + lookup_pool_offset), |
732ad076 | 401 | sizeof(void *)); |
abc490a1 | 402 | if (node == NULL) |
5e28c532 MD |
403 | lookup_fail++; |
404 | else | |
405 | lookup_ok++; | |
ab7d5fc6 | 406 | debug_yield_read(); |
ab7d5fc6 MD |
407 | if (unlikely(rduration)) |
408 | loop_sleep(rduration); | |
409 | rcu_read_unlock(); | |
410 | nr_reads++; | |
411 | if (unlikely(!test_duration_read())) | |
412 | break; | |
5567839e MD |
413 | if (unlikely((nr_reads & ((1 << 10) - 1)) == 0)) |
414 | rcu_quiescent_state(); | |
ab7d5fc6 MD |
415 | } |
416 | ||
417 | rcu_unregister_thread(); | |
418 | ||
419 | *count = nr_reads; | |
420 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", | |
421 | "reader", pthread_self(), (unsigned long)gettid()); | |
5e28c532 MD |
422 | printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n", |
423 | pthread_self(), lookup_fail, lookup_ok); | |
ab7d5fc6 MD |
424 | return ((void*)1); |
425 | ||
426 | } | |
427 | ||
abc490a1 MD |
428 | static |
429 | void free_node_cb(struct rcu_head *head) | |
430 | { | |
14044b37 MD |
431 | struct cds_lfht_node *node = |
432 | caa_container_of(head, struct cds_lfht_node, head); | |
abc490a1 MD |
433 | free(node); |
434 | } | |
435 | ||
ab7d5fc6 MD |
436 | void *thr_writer(void *_count) |
437 | { | |
14044b37 | 438 | struct cds_lfht_node *node, *ret_node; |
b8e1907c | 439 | struct wr_count *count = _count; |
5e28c532 | 440 | int ret; |
ab7d5fc6 MD |
441 | |
442 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
443 | "writer", pthread_self(), (unsigned long)gettid()); | |
444 | ||
445 | set_affinity(); | |
446 | ||
5e28c532 | 447 | rcu_register_thread(); |
5e28c532 | 448 | |
ab7d5fc6 MD |
449 | while (!test_go) |
450 | { | |
451 | } | |
abc490a1 | 452 | cmm_smp_mb(); |
ab7d5fc6 MD |
453 | |
454 | for (;;) { | |
3967a8a8 MD |
455 | if ((addremove == AR_ADD || add_only) |
456 | || (addremove == AR_RANDOM && rand_r(&rand_lookup) & 1)) { | |
14044b37 | 457 | node = malloc(sizeof(struct cds_lfht_node)); |
abc490a1 | 458 | rcu_read_lock(); |
14044b37 | 459 | cds_lfht_node_init(node, |
8008a032 | 460 | (void *)(unsigned long)((rand_r(&rand_lookup) % rand_pool) + write_pool_offset), |
cb233752 | 461 | sizeof(void *)); |
6d5c0ca9 | 462 | if (add_unique) |
14044b37 | 463 | ret_node = cds_lfht_add_unique(test_ht, node); |
6d5c0ca9 | 464 | else |
14044b37 | 465 | cds_lfht_add(test_ht, node); |
abc490a1 | 466 | rcu_read_unlock(); |
6d5c0ca9 | 467 | if (add_unique && ret_node != node) { |
742aa1db | 468 | free(node); |
3eca1b8c | 469 | nr_addexist++; |
742aa1db | 470 | } else |
3eca1b8c | 471 | nr_add++; |
5e28c532 MD |
472 | } else { |
473 | /* May delete */ | |
abc490a1 | 474 | rcu_read_lock(); |
14044b37 | 475 | node = cds_lfht_lookup(test_ht, |
8008a032 | 476 | (void *)(unsigned long)((rand_r(&rand_lookup) % rand_pool) + write_pool_offset), |
732ad076 | 477 | sizeof(void *)); |
abc490a1 | 478 | if (node) |
14044b37 | 479 | ret = cds_lfht_remove(test_ht, node); |
5e28c532 | 480 | else |
abc490a1 MD |
481 | ret = -ENOENT; |
482 | rcu_read_unlock(); | |
483 | if (ret == 0) { | |
484 | call_rcu(&node->head, free_node_cb); | |
5e28c532 | 485 | nr_del++; |
abc490a1 MD |
486 | } else |
487 | nr_delnoent++; | |
44395fb7 | 488 | } |
abc490a1 | 489 | #if 0 |
44395fb7 MD |
490 | //if (nr_writes % 100000 == 0) { |
491 | if (nr_writes % 1000 == 0) { | |
abc490a1 | 492 | rcu_read_lock(); |
44395fb7 MD |
493 | if (rand_r(&rand_lookup) & 1) { |
494 | ht_resize(test_ht, 1); | |
495 | } else { | |
496 | ht_resize(test_ht, -1); | |
497 | } | |
abc490a1 | 498 | rcu_read_unlock(); |
5e28c532 | 499 | } |
abc490a1 | 500 | #endif //0 |
ab7d5fc6 MD |
501 | nr_writes++; |
502 | if (unlikely(!test_duration_write())) | |
503 | break; | |
504 | if (unlikely(wdelay)) | |
505 | loop_sleep(wdelay); | |
5567839e MD |
506 | if (unlikely((nr_writes & ((1 << 10) - 1)) == 0)) |
507 | rcu_quiescent_state(); | |
ab7d5fc6 MD |
508 | } |
509 | ||
5e28c532 MD |
510 | rcu_unregister_thread(); |
511 | ||
ab7d5fc6 MD |
512 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", |
513 | "writer", pthread_self(), (unsigned long)gettid()); | |
5e28c532 MD |
514 | printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, " |
515 | "nr_delnoent %lu\n", pthread_self(), nr_add, | |
516 | nr_addexist, nr_del, nr_delnoent); | |
b8e1907c MD |
517 | count->update_ops = nr_writes; |
518 | count->add = nr_add; | |
3c16bf4b | 519 | count->add_exist = nr_addexist; |
b8e1907c | 520 | count->remove = nr_del; |
ab7d5fc6 MD |
521 | return ((void*)2); |
522 | } | |
523 | ||
5dc45f25 | 524 | static int populate_hash(void) |
cd1ae16a MD |
525 | { |
526 | struct cds_lfht_node *node, *ret_node; | |
527 | ||
528 | if (!init_populate) | |
5dc45f25 MD |
529 | return 0; |
530 | ||
531 | if (add_unique && init_populate * 10 > rand_pool) { | |
532 | printf("WARNING: required to populate %lu nodes (-k), but random " | |
533 | "pool is quite small (%lu values) and we are in add_unique (-u) mode. Try with a " | |
534 | "larger random pool (-p option).\n", init_populate, rand_pool); | |
535 | return -1; | |
536 | } | |
cd1ae16a MD |
537 | |
538 | while (nr_add < init_populate) { | |
539 | node = malloc(sizeof(struct cds_lfht_node)); | |
540 | cds_lfht_node_init(node, | |
8008a032 | 541 | (void *)(unsigned long)((rand_r(&rand_lookup) % rand_pool) + init_pool_offset), |
cd1ae16a MD |
542 | sizeof(void *)); |
543 | if (add_unique) | |
544 | ret_node = cds_lfht_add_unique(test_ht, node); | |
545 | else | |
546 | cds_lfht_add(test_ht, node); | |
547 | if (add_unique && ret_node != node) { | |
548 | free(node); | |
549 | nr_addexist++; | |
550 | } else | |
551 | nr_add++; | |
552 | nr_writes++; | |
553 | } | |
5dc45f25 | 554 | return 0; |
cd1ae16a MD |
555 | } |
556 | ||
ab7d5fc6 MD |
557 | void show_usage(int argc, char **argv) |
558 | { | |
559 | printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]); | |
560 | #ifdef DEBUG_YIELD | |
561 | printf(" [-r] [-w] (yield reader and/or writer)"); | |
562 | #endif | |
563 | printf(" [-d delay] (writer period (us))"); | |
564 | printf(" [-c duration] (reader C.S. duration (in loops))"); | |
565 | printf(" [-v] (verbose output)"); | |
566 | printf(" [-a cpu#] [-a cpu#]... (affinity)"); | |
6d5c0ca9 MD |
567 | printf(" [-p size] (random key value pool size)"); |
568 | printf(" [-h size] (initial hash table size)"); | |
569 | printf(" [-u] Uniquify add."); | |
570 | printf(" [-i] Add only (no removal)."); | |
cd1ae16a | 571 | printf(" [-k nr_nodes] Number of nodes to insert initially."); |
151e7a93 | 572 | printf(" [-A] Automatically resize hash table."); |
8008a032 MD |
573 | printf(" [-R offset] Lookup pool offset\n"); |
574 | printf(" [-S offset] Write pool offset\n"); | |
575 | printf(" [-T offset] Init pool offset\n"); | |
ab7d5fc6 MD |
576 | printf("\n"); |
577 | } | |
578 | ||
579 | int main(int argc, char **argv) | |
580 | { | |
581 | int err; | |
582 | pthread_t *tid_reader, *tid_writer; | |
583 | void *tret; | |
b8e1907c MD |
584 | unsigned long long *count_reader; |
585 | struct wr_count *count_writer; | |
586 | unsigned long long tot_reads = 0, tot_writes = 0, | |
3c16bf4b | 587 | tot_add = 0, tot_add_exist = 0, tot_remove = 0; |
b8e1907c | 588 | unsigned long count, removed; |
5e28c532 | 589 | int i, a, ret; |
3967a8a8 MD |
590 | struct sigaction act; |
591 | unsigned int remain; | |
ab7d5fc6 MD |
592 | |
593 | if (argc < 4) { | |
594 | show_usage(argc, argv); | |
595 | return -1; | |
596 | } | |
597 | ||
598 | err = sscanf(argv[1], "%u", &nr_readers); | |
599 | if (err != 1) { | |
600 | show_usage(argc, argv); | |
601 | return -1; | |
602 | } | |
603 | ||
604 | err = sscanf(argv[2], "%u", &nr_writers); | |
605 | if (err != 1) { | |
606 | show_usage(argc, argv); | |
607 | return -1; | |
608 | } | |
609 | ||
610 | err = sscanf(argv[3], "%lu", &duration); | |
611 | if (err != 1) { | |
612 | show_usage(argc, argv); | |
613 | return -1; | |
614 | } | |
615 | ||
616 | for (i = 4; i < argc; i++) { | |
617 | if (argv[i][0] != '-') | |
618 | continue; | |
619 | switch (argv[i][1]) { | |
620 | #ifdef DEBUG_YIELD | |
621 | case 'r': | |
622 | yield_active |= YIELD_READ; | |
623 | break; | |
624 | case 'w': | |
625 | yield_active |= YIELD_WRITE; | |
626 | break; | |
627 | #endif | |
628 | case 'a': | |
629 | if (argc < i + 2) { | |
630 | show_usage(argc, argv); | |
631 | return -1; | |
632 | } | |
633 | a = atoi(argv[++i]); | |
634 | cpu_affinities[next_aff++] = a; | |
635 | use_affinity = 1; | |
636 | printf_verbose("Adding CPU %d affinity\n", a); | |
637 | break; | |
638 | case 'c': | |
639 | if (argc < i + 2) { | |
640 | show_usage(argc, argv); | |
641 | return -1; | |
642 | } | |
643 | rduration = atol(argv[++i]); | |
644 | break; | |
645 | case 'd': | |
646 | if (argc < i + 2) { | |
647 | show_usage(argc, argv); | |
648 | return -1; | |
649 | } | |
650 | wdelay = atol(argv[++i]); | |
651 | break; | |
652 | case 'v': | |
653 | verbose_mode = 1; | |
654 | break; | |
6d5c0ca9 MD |
655 | case 'p': |
656 | if (argc < i + 2) { | |
657 | show_usage(argc, argv); | |
658 | return -1; | |
659 | } | |
660 | rand_pool = atol(argv[++i]); | |
661 | break; | |
662 | case 'h': | |
663 | if (argc < i + 2) { | |
664 | show_usage(argc, argv); | |
665 | return -1; | |
666 | } | |
667 | init_hash_size = atol(argv[++i]); | |
668 | break; | |
669 | case 'u': | |
670 | add_unique = 1; | |
671 | break; | |
672 | case 'i': | |
673 | add_only = 1; | |
674 | break; | |
cd1ae16a MD |
675 | case 'k': |
676 | init_populate = atol(argv[++i]); | |
677 | break; | |
151e7a93 MD |
678 | case 'A': |
679 | opt_auto_resize = 1; | |
680 | break; | |
8008a032 MD |
681 | case 'R': |
682 | lookup_pool_offset = atol(argv[++i]); | |
683 | break; | |
684 | case 'S': | |
685 | write_pool_offset = atol(argv[++i]); | |
686 | break; | |
687 | case 'T': | |
688 | init_pool_offset = atol(argv[++i]); | |
689 | break; | |
690 | ||
ab7d5fc6 MD |
691 | } |
692 | } | |
693 | ||
6d5c0ca9 MD |
694 | /* Check if hash size is power of 2 */ |
695 | if (init_hash_size && init_hash_size & (init_hash_size - 1)) { | |
696 | printf("Error: Hash table size %lu is not a power of 2.\n", | |
697 | init_hash_size); | |
698 | return -1; | |
699 | } | |
700 | ||
3967a8a8 MD |
701 | memset(&act, 0, sizeof(act)); |
702 | ret = sigemptyset(&act.sa_mask); | |
703 | if (ret == -1) { | |
704 | perror("sigemptyset"); | |
705 | return -1; | |
706 | } | |
707 | act.sa_handler = sigusr1_handler; | |
708 | act.sa_flags = SA_RESTART; | |
709 | ret = sigaction(SIGUSR1, &act, NULL); | |
710 | if (ret == -1) { | |
711 | perror("sigaction"); | |
712 | return -1; | |
713 | } | |
714 | ||
ab7d5fc6 MD |
715 | printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", |
716 | duration, nr_readers, nr_writers); | |
717 | printf_verbose("Writer delay : %lu loops.\n", wdelay); | |
718 | printf_verbose("Reader duration : %lu loops.\n", rduration); | |
6d5c0ca9 MD |
719 | printf_verbose("Random pool size : %lu.\n", rand_pool); |
720 | printf_verbose("Mode:%s%s.\n", | |
721 | add_only ? " add only" : " add/remove", | |
722 | add_unique ? " uniquify" : ""); | |
723 | printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size); | |
ab7d5fc6 MD |
724 | printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", |
725 | "main", pthread_self(), (unsigned long)gettid()); | |
726 | ||
ab7d5fc6 MD |
727 | tid_reader = malloc(sizeof(*tid_reader) * nr_readers); |
728 | tid_writer = malloc(sizeof(*tid_writer) * nr_writers); | |
729 | count_reader = malloc(sizeof(*count_reader) * nr_readers); | |
730 | count_writer = malloc(sizeof(*count_writer) * nr_writers); | |
14044b37 | 731 | test_ht = cds_lfht_new(test_hash, test_compare, 0x42UL, |
151e7a93 MD |
732 | init_hash_size, |
733 | opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0, | |
01dbfa62 | 734 | call_rcu, synchronize_rcu, rcu_read_lock, |
5f511391 MD |
735 | rcu_read_unlock, rcu_thread_offline, |
736 | rcu_thread_online); | |
5dc45f25 MD |
737 | ret = populate_hash(); |
738 | assert(!ret); | |
14756542 MD |
739 | err = create_all_cpu_call_rcu_data(0); |
740 | assert(!err); | |
741 | ||
ab7d5fc6 MD |
742 | next_aff = 0; |
743 | ||
744 | for (i = 0; i < nr_readers; i++) { | |
745 | err = pthread_create(&tid_reader[i], NULL, thr_reader, | |
746 | &count_reader[i]); | |
747 | if (err != 0) | |
748 | exit(1); | |
749 | } | |
750 | for (i = 0; i < nr_writers; i++) { | |
751 | err = pthread_create(&tid_writer[i], NULL, thr_writer, | |
752 | &count_writer[i]); | |
753 | if (err != 0) | |
754 | exit(1); | |
755 | } | |
756 | ||
abc490a1 | 757 | cmm_smp_mb(); |
ab7d5fc6 MD |
758 | |
759 | test_go = 1; | |
760 | ||
3967a8a8 MD |
761 | remain = duration; |
762 | do { | |
763 | remain = sleep(remain); | |
764 | } while (remain > 0); | |
ab7d5fc6 MD |
765 | |
766 | test_stop = 1; | |
767 | ||
768 | for (i = 0; i < nr_readers; i++) { | |
769 | err = pthread_join(tid_reader[i], &tret); | |
770 | if (err != 0) | |
771 | exit(1); | |
772 | tot_reads += count_reader[i]; | |
773 | } | |
774 | for (i = 0; i < nr_writers; i++) { | |
775 | err = pthread_join(tid_writer[i], &tret); | |
776 | if (err != 0) | |
777 | exit(1); | |
b8e1907c MD |
778 | tot_writes += count_writer[i].update_ops; |
779 | tot_add += count_writer[i].add; | |
3c16bf4b | 780 | tot_add_exist += count_writer[i].add_exist; |
b8e1907c | 781 | tot_remove += count_writer[i].remove; |
ab7d5fc6 | 782 | } |
33c7c748 MD |
783 | printf("Counting nodes... "); |
784 | fflush(stdout); | |
14044b37 | 785 | cds_lfht_count_nodes(test_ht, &count, &removed); |
33c7c748 | 786 | printf("done.\n"); |
b8e1907c | 787 | if (count || removed) |
273399de MD |
788 | printf("WARNING: nodes left in the hash table upon destroy: " |
789 | "%lu nodes + %lu logically removed.\n", count, removed); | |
14044b37 | 790 | ret = cds_lfht_destroy(test_ht); |
f000907d | 791 | |
33c7c748 MD |
792 | if (ret) |
793 | printf_verbose("final delete aborted\n"); | |
794 | else | |
795 | printf_verbose("final delete success\n"); | |
ab7d5fc6 MD |
796 | printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads, |
797 | tot_writes); | |
798 | printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu " | |
799 | "nr_writers %3u " | |
6d5c0ca9 | 800 | "wdelay %6lu rand_pool %12llu nr_reads %12llu nr_writes %12llu nr_ops %12llu " |
cd1ae16a | 801 | "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n", |
ab7d5fc6 | 802 | argv[0], duration, nr_readers, rduration, |
6d5c0ca9 | 803 | nr_writers, wdelay, rand_pool, tot_reads, tot_writes, |
3c16bf4b | 804 | tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove, |
cd1ae16a | 805 | (long long) tot_add + init_populate - tot_remove - count); |
3a22f1dd | 806 | free_all_cpu_call_rcu_data(); |
ab7d5fc6 MD |
807 | free(tid_reader); |
808 | free(tid_writer); | |
809 | free(count_reader); | |
810 | free(count_writer); | |
811 | return 0; | |
812 | } |