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