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