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; |
151e7a93 | 118 | static int opt_auto_resize; |
48ed1c18 | 119 | static int add_only, add_unique, add_replace; |
6d5c0ca9 | 120 | |
8008a032 | 121 | static unsigned long init_pool_offset, lookup_pool_offset, write_pool_offset; |
d837911d MD |
122 | static unsigned long init_pool_size = DEFAULT_RAND_POOL, |
123 | lookup_pool_size = DEFAULT_RAND_POOL, | |
124 | write_pool_size = DEFAULT_RAND_POOL; | |
59b10634 | 125 | static int validate_lookup; |
8008a032 | 126 | |
7ed7682f MD |
127 | static int count_pipe[2]; |
128 | ||
ab7d5fc6 MD |
129 | static inline void loop_sleep(unsigned long l) |
130 | { | |
131 | while(l-- != 0) | |
abc490a1 | 132 | caa_cpu_relax(); |
ab7d5fc6 MD |
133 | } |
134 | ||
135 | static int verbose_mode; | |
136 | ||
137 | #define printf_verbose(fmt, args...) \ | |
138 | do { \ | |
139 | if (verbose_mode) \ | |
33c7c748 | 140 | printf(fmt, ## args); \ |
ab7d5fc6 MD |
141 | } while (0) |
142 | ||
143 | static unsigned int cpu_affinities[NR_CPUS]; | |
144 | static unsigned int next_aff = 0; | |
145 | static int use_affinity = 0; | |
146 | ||
147 | pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; | |
148 | ||
149 | static void set_affinity(void) | |
150 | { | |
151 | cpu_set_t mask; | |
152 | int cpu; | |
153 | int ret; | |
154 | ||
155 | if (!use_affinity) | |
156 | return; | |
157 | ||
158 | ret = pthread_mutex_lock(&affinity_mutex); | |
159 | if (ret) { | |
160 | perror("Error in pthread mutex lock"); | |
161 | exit(-1); | |
162 | } | |
163 | cpu = cpu_affinities[next_aff++]; | |
164 | ret = pthread_mutex_unlock(&affinity_mutex); | |
165 | if (ret) { | |
166 | perror("Error in pthread mutex unlock"); | |
167 | exit(-1); | |
168 | } | |
169 | CPU_ZERO(&mask); | |
170 | CPU_SET(cpu, &mask); | |
171 | sched_setaffinity(0, sizeof(mask), &mask); | |
172 | } | |
173 | ||
3967a8a8 MD |
174 | static enum { |
175 | AR_RANDOM = 0, | |
176 | AR_ADD = 1, | |
177 | AR_REMOVE = -1, | |
178 | } addremove; /* 1: add, -1 remove, 0: random */ | |
179 | ||
180 | static | |
181 | void sigusr1_handler(int signo) | |
182 | { | |
183 | switch (addremove) { | |
184 | case AR_ADD: | |
185 | printf("Add/Remove: random.\n"); | |
186 | addremove = AR_RANDOM; | |
187 | break; | |
188 | case AR_RANDOM: | |
189 | printf("Add/Remove: remove only.\n"); | |
190 | addremove = AR_REMOVE; | |
191 | break; | |
192 | case AR_REMOVE: | |
193 | printf("Add/Remove: add only.\n"); | |
194 | addremove = AR_ADD; | |
195 | break; | |
196 | } | |
197 | } | |
198 | ||
973e5e1b MD |
199 | static |
200 | void sigusr2_handler(int signo) | |
201 | { | |
7ed7682f MD |
202 | char msg[1] = { 0x42 }; |
203 | write(count_pipe[1], msg, 1); /* wakeup thread */ | |
973e5e1b MD |
204 | } |
205 | ||
ab7d5fc6 MD |
206 | /* |
207 | * returns 0 if test should end. | |
208 | */ | |
209 | static int test_duration_write(void) | |
210 | { | |
211 | return !test_stop; | |
212 | } | |
213 | ||
214 | static int test_duration_read(void) | |
215 | { | |
216 | return !test_stop; | |
217 | } | |
218 | ||
219 | static unsigned long long __thread nr_writes; | |
220 | static unsigned long long __thread nr_reads; | |
221 | ||
222 | static unsigned int nr_readers; | |
223 | static unsigned int nr_writers; | |
224 | ||
225 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; | |
226 | ||
227 | void rcu_copy_mutex_lock(void) | |
228 | { | |
229 | int ret; | |
230 | ret = pthread_mutex_lock(&rcu_copy_mutex); | |
231 | if (ret) { | |
232 | perror("Error in pthread mutex lock"); | |
233 | exit(-1); | |
234 | } | |
235 | } | |
236 | ||
237 | void rcu_copy_mutex_unlock(void) | |
238 | { | |
239 | int ret; | |
240 | ||
241 | ret = pthread_mutex_unlock(&rcu_copy_mutex); | |
242 | if (ret) { | |
243 | perror("Error in pthread mutex unlock"); | |
244 | exit(-1); | |
245 | } | |
246 | } | |
247 | ||
732ad076 MD |
248 | /* |
249 | * Hash function | |
250 | * Source: http://burtleburtle.net/bob/c/lookup3.c | |
251 | * Originally Public Domain | |
252 | */ | |
253 | ||
254 | #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) | |
255 | ||
256 | #define mix(a, b, c) \ | |
257 | do { \ | |
258 | a -= c; a ^= rot(c, 4); c += b; \ | |
259 | b -= a; b ^= rot(a, 6); a += c; \ | |
260 | c -= b; c ^= rot(b, 8); b += a; \ | |
261 | a -= c; a ^= rot(c, 16); c += b; \ | |
262 | b -= a; b ^= rot(a, 19); a += c; \ | |
263 | c -= b; c ^= rot(b, 4); b += a; \ | |
264 | } while (0) | |
265 | ||
266 | #define final(a, b, c) \ | |
267 | { \ | |
268 | c ^= b; c -= rot(b, 14); \ | |
269 | a ^= c; a -= rot(c, 11); \ | |
270 | b ^= a; b -= rot(a, 25); \ | |
271 | c ^= b; c -= rot(b, 16); \ | |
272 | a ^= c; a -= rot(c, 4);\ | |
273 | b ^= a; b -= rot(a, 14); \ | |
274 | c ^= b; c -= rot(b, 24); \ | |
275 | } | |
276 | ||
277 | static __attribute__((unused)) | |
278 | uint32_t hash_u32( | |
279 | const uint32_t *k, /* the key, an array of uint32_t values */ | |
280 | size_t length, /* the length of the key, in uint32_ts */ | |
281 | uint32_t initval) /* the previous hash, or an arbitrary value */ | |
282 | { | |
283 | uint32_t a, b, c; | |
284 | ||
285 | /* Set up the internal state */ | |
286 | a = b = c = 0xdeadbeef + (((uint32_t) length) << 2) + initval; | |
287 | ||
288 | /*----------------------------------------- handle most of the key */ | |
289 | while (length > 3) { | |
290 | a += k[0]; | |
291 | b += k[1]; | |
292 | c += k[2]; | |
293 | mix(a, b, c); | |
294 | length -= 3; | |
295 | k += 3; | |
296 | } | |
297 | ||
298 | /*----------------------------------- handle the last 3 uint32_t's */ | |
299 | switch (length) { /* all the case statements fall through */ | |
300 | case 3: c += k[2]; | |
301 | case 2: b += k[1]; | |
302 | case 1: a += k[0]; | |
303 | final(a, b, c); | |
304 | case 0: /* case 0: nothing left to add */ | |
305 | break; | |
306 | } | |
307 | /*---------------------------------------------- report the result */ | |
308 | return c; | |
309 | } | |
310 | ||
311 | static | |
312 | void hashword2( | |
313 | const uint32_t *k, /* the key, an array of uint32_t values */ | |
314 | size_t length, /* the length of the key, in uint32_ts */ | |
315 | uint32_t *pc, /* IN: seed OUT: primary hash value */ | |
316 | uint32_t *pb) /* IN: more seed OUT: secondary hash value */ | |
317 | { | |
318 | uint32_t a, b, c; | |
319 | ||
320 | /* Set up the internal state */ | |
321 | a = b = c = 0xdeadbeef + ((uint32_t) (length << 2)) + *pc; | |
322 | c += *pb; | |
323 | ||
324 | /*----------------------------------------- handle most of the key */ | |
325 | while (length > 3) { | |
326 | a += k[0]; | |
327 | b += k[1]; | |
328 | c += k[2]; | |
329 | mix(a, b, c); | |
330 | length -= 3; | |
331 | k += 3; | |
332 | } | |
333 | ||
334 | /*----------------------------------- handle the last 3 uint32_t's */ | |
335 | switch (length) { /* all the case statements fall through */ | |
336 | case 3: c += k[2]; | |
337 | case 2: b += k[1]; | |
338 | case 1: a += k[0]; | |
339 | final(a, b, c); | |
340 | case 0: /* case 0: nothing left to add */ | |
341 | break; | |
342 | } | |
343 | /*---------------------------------------------- report the result */ | |
344 | *pc = c; | |
345 | *pb = b; | |
346 | } | |
347 | ||
348 | #if (CAA_BITS_PER_LONG == 32) | |
abc490a1 | 349 | static |
732ad076 | 350 | unsigned long test_hash(void *_key, size_t length, unsigned long seed) |
abc490a1 | 351 | { |
abc490a1 | 352 | unsigned long key = (unsigned long) _key; |
abc490a1 | 353 | |
732ad076 | 354 | assert(length == sizeof(unsigned long)); |
bda21126 | 355 | return hash_u32(&key, 1, seed); |
732ad076 MD |
356 | } |
357 | #else | |
358 | static | |
359 | unsigned long test_hash(void *_key, size_t length, unsigned long seed) | |
360 | { | |
361 | union { | |
362 | uint64_t v64; | |
363 | uint32_t v32[2]; | |
364 | } v; | |
365 | union { | |
366 | uint64_t v64; | |
367 | uint32_t v32[2]; | |
368 | } key; | |
369 | ||
370 | assert(length == sizeof(unsigned long)); | |
371 | v.v64 = (uint64_t) seed; | |
372 | key.v64 = (uint64_t) _key; | |
373 | hashword2(key.v32, 2, &v.v32[0], &v.v32[1]); | |
374 | return v.v64; | |
375 | } | |
376 | #endif | |
abc490a1 | 377 | |
732ad076 MD |
378 | static |
379 | unsigned long test_compare(void *key1, size_t key1_len, | |
380 | void *key2, size_t key2_len) | |
381 | { | |
382 | if (unlikely(key1_len != key2_len)) | |
383 | return -1; | |
384 | assert(key1_len == sizeof(unsigned long)); | |
385 | if (key1 == key2) | |
386 | return 0; | |
387 | else | |
388 | return 1; | |
abc490a1 | 389 | } |
ab7d5fc6 | 390 | |
7ed7682f MD |
391 | void *thr_count(void *arg) |
392 | { | |
393 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
394 | "counter", pthread_self(), (unsigned long)gettid()); | |
395 | ||
396 | rcu_register_thread(); | |
397 | ||
398 | for (;;) { | |
d933dd0e MD |
399 | unsigned long count, removed; |
400 | long approx_before, approx_after; | |
7ed7682f MD |
401 | ssize_t len; |
402 | char buf[1]; | |
403 | ||
404 | rcu_thread_offline(); | |
405 | len = read(count_pipe[0], buf, 1); | |
406 | rcu_thread_online(); | |
407 | if (unlikely(!test_duration_read())) | |
408 | break; | |
409 | if (len != 1) | |
410 | continue; | |
411 | /* Accounting */ | |
412 | printf("Counting nodes... "); | |
413 | fflush(stdout); | |
414 | rcu_read_lock(); | |
415 | cds_lfht_count_nodes(test_ht, &approx_before, &count, &removed, | |
416 | &approx_after); | |
417 | rcu_read_unlock(); | |
418 | printf("done.\n"); | |
d933dd0e | 419 | printf("Approximation before node accounting: %ld nodes.\n", |
7ed7682f MD |
420 | approx_before); |
421 | printf("Accounting of nodes in the hash table: " | |
422 | "%lu nodes + %lu logically removed.\n", | |
423 | count, removed); | |
d933dd0e | 424 | printf("Approximation after node accounting: %ld nodes.\n", |
7ed7682f MD |
425 | approx_after); |
426 | } | |
427 | rcu_unregister_thread(); | |
428 | return NULL; | |
429 | } | |
430 | ||
ab7d5fc6 MD |
431 | void *thr_reader(void *_count) |
432 | { | |
433 | unsigned long long *count = _count; | |
14044b37 | 434 | struct cds_lfht_node *node; |
adc0de68 | 435 | struct cds_lfht_iter iter; |
ab7d5fc6 MD |
436 | |
437 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
438 | "reader", pthread_self(), (unsigned long)gettid()); | |
439 | ||
440 | set_affinity(); | |
441 | ||
442 | rcu_register_thread(); | |
443 | ||
444 | while (!test_go) | |
445 | { | |
446 | } | |
abc490a1 | 447 | cmm_smp_mb(); |
ab7d5fc6 MD |
448 | |
449 | for (;;) { | |
450 | rcu_read_lock(); | |
adc0de68 | 451 | cds_lfht_lookup(test_ht, |
c8f5b384 | 452 | (void *)(((unsigned long) rand_r(&rand_lookup) % lookup_pool_size) + lookup_pool_offset), |
adc0de68 MD |
453 | sizeof(void *), &iter); |
454 | node = cds_lfht_iter_get_node(&iter); | |
59b10634 MD |
455 | if (node == NULL) { |
456 | if (validate_lookup) { | |
457 | printf("[ERROR] Lookup cannot find initial node.\n"); | |
46834ba6 | 458 | exit(-1); |
59b10634 | 459 | } |
5e28c532 | 460 | lookup_fail++; |
59b10634 | 461 | } else { |
5e28c532 | 462 | lookup_ok++; |
59b10634 | 463 | } |
ab7d5fc6 | 464 | debug_yield_read(); |
ab7d5fc6 MD |
465 | if (unlikely(rduration)) |
466 | loop_sleep(rduration); | |
467 | rcu_read_unlock(); | |
468 | nr_reads++; | |
469 | if (unlikely(!test_duration_read())) | |
470 | break; | |
5567839e MD |
471 | if (unlikely((nr_reads & ((1 << 10) - 1)) == 0)) |
472 | rcu_quiescent_state(); | |
ab7d5fc6 MD |
473 | } |
474 | ||
475 | rcu_unregister_thread(); | |
476 | ||
477 | *count = nr_reads; | |
478 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", | |
479 | "reader", pthread_self(), (unsigned long)gettid()); | |
5e28c532 MD |
480 | printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n", |
481 | pthread_self(), lookup_fail, lookup_ok); | |
ab7d5fc6 MD |
482 | return ((void*)1); |
483 | ||
484 | } | |
485 | ||
abc490a1 MD |
486 | static |
487 | void free_node_cb(struct rcu_head *head) | |
488 | { | |
14044b37 MD |
489 | struct cds_lfht_node *node = |
490 | caa_container_of(head, struct cds_lfht_node, head); | |
abc490a1 MD |
491 | free(node); |
492 | } | |
493 | ||
ab7d5fc6 MD |
494 | void *thr_writer(void *_count) |
495 | { | |
14044b37 | 496 | struct cds_lfht_node *node, *ret_node; |
adc0de68 | 497 | struct cds_lfht_iter iter; |
b8e1907c | 498 | struct wr_count *count = _count; |
5e28c532 | 499 | int ret; |
ab7d5fc6 MD |
500 | |
501 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
502 | "writer", pthread_self(), (unsigned long)gettid()); | |
503 | ||
504 | set_affinity(); | |
505 | ||
5e28c532 | 506 | rcu_register_thread(); |
5e28c532 | 507 | |
ab7d5fc6 MD |
508 | while (!test_go) |
509 | { | |
510 | } | |
abc490a1 | 511 | cmm_smp_mb(); |
ab7d5fc6 MD |
512 | |
513 | for (;;) { | |
3967a8a8 MD |
514 | if ((addremove == AR_ADD || add_only) |
515 | || (addremove == AR_RANDOM && rand_r(&rand_lookup) & 1)) { | |
14044b37 | 516 | node = malloc(sizeof(struct cds_lfht_node)); |
14044b37 | 517 | cds_lfht_node_init(node, |
c8f5b384 | 518 | (void *)(((unsigned long) rand_r(&rand_lookup) % write_pool_size) + write_pool_offset), |
cb233752 | 519 | sizeof(void *)); |
6b262fd7 | 520 | rcu_read_lock(); |
48ed1c18 | 521 | if (add_unique) { |
14044b37 | 522 | ret_node = cds_lfht_add_unique(test_ht, node); |
48ed1c18 MD |
523 | } else { |
524 | if (add_replace) | |
9357c415 | 525 | ret_node = cds_lfht_add_replace(test_ht, node); |
48ed1c18 MD |
526 | else |
527 | cds_lfht_add(test_ht, node); | |
528 | } | |
abc490a1 | 529 | rcu_read_unlock(); |
6d5c0ca9 | 530 | if (add_unique && ret_node != node) { |
742aa1db | 531 | free(node); |
3eca1b8c | 532 | nr_addexist++; |
48ed1c18 MD |
533 | } else { |
534 | if (add_replace && ret_node) { | |
535 | call_rcu(&ret_node->head, free_node_cb); | |
536 | nr_addexist++; | |
537 | } else { | |
538 | nr_add++; | |
539 | } | |
540 | } | |
5e28c532 MD |
541 | } else { |
542 | /* May delete */ | |
abc490a1 | 543 | rcu_read_lock(); |
adc0de68 | 544 | cds_lfht_lookup(test_ht, |
c8f5b384 | 545 | (void *)(((unsigned long) rand_r(&rand_lookup) % write_pool_size) + write_pool_offset), |
adc0de68 | 546 | sizeof(void *), &iter); |
9357c415 | 547 | ret = cds_lfht_del(test_ht, &iter); |
abc490a1 MD |
548 | rcu_read_unlock(); |
549 | if (ret == 0) { | |
9357c415 | 550 | node = cds_lfht_iter_get_node(&iter); |
abc490a1 | 551 | call_rcu(&node->head, free_node_cb); |
5e28c532 | 552 | nr_del++; |
abc490a1 MD |
553 | } else |
554 | nr_delnoent++; | |
44395fb7 | 555 | } |
abc490a1 | 556 | #if 0 |
44395fb7 MD |
557 | //if (nr_writes % 100000 == 0) { |
558 | if (nr_writes % 1000 == 0) { | |
abc490a1 | 559 | rcu_read_lock(); |
44395fb7 MD |
560 | if (rand_r(&rand_lookup) & 1) { |
561 | ht_resize(test_ht, 1); | |
562 | } else { | |
563 | ht_resize(test_ht, -1); | |
564 | } | |
abc490a1 | 565 | rcu_read_unlock(); |
5e28c532 | 566 | } |
abc490a1 | 567 | #endif //0 |
ab7d5fc6 MD |
568 | nr_writes++; |
569 | if (unlikely(!test_duration_write())) | |
570 | break; | |
571 | if (unlikely(wdelay)) | |
572 | loop_sleep(wdelay); | |
5567839e MD |
573 | if (unlikely((nr_writes & ((1 << 10) - 1)) == 0)) |
574 | rcu_quiescent_state(); | |
ab7d5fc6 MD |
575 | } |
576 | ||
5e28c532 MD |
577 | rcu_unregister_thread(); |
578 | ||
ab7d5fc6 MD |
579 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", |
580 | "writer", pthread_self(), (unsigned long)gettid()); | |
5e28c532 MD |
581 | printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, " |
582 | "nr_delnoent %lu\n", pthread_self(), nr_add, | |
583 | nr_addexist, nr_del, nr_delnoent); | |
b8e1907c MD |
584 | count->update_ops = nr_writes; |
585 | count->add = nr_add; | |
3c16bf4b | 586 | count->add_exist = nr_addexist; |
b8e1907c | 587 | count->remove = nr_del; |
ab7d5fc6 MD |
588 | return ((void*)2); |
589 | } | |
590 | ||
5dc45f25 | 591 | static int populate_hash(void) |
cd1ae16a MD |
592 | { |
593 | struct cds_lfht_node *node, *ret_node; | |
594 | ||
595 | if (!init_populate) | |
5dc45f25 MD |
596 | return 0; |
597 | ||
48ed1c18 | 598 | if ((add_unique || add_replace) && init_populate * 10 > init_pool_size) { |
5dc45f25 | 599 | printf("WARNING: required to populate %lu nodes (-k), but random " |
48ed1c18 | 600 | "pool is quite small (%lu values) and we are in add_unique (-u) or add_replace (-s) mode. Try with a " |
d837911d | 601 | "larger random pool (-p option). This may take a while...\n", init_populate, init_pool_size); |
5dc45f25 | 602 | } |
cd1ae16a MD |
603 | |
604 | while (nr_add < init_populate) { | |
605 | node = malloc(sizeof(struct cds_lfht_node)); | |
606 | cds_lfht_node_init(node, | |
c8f5b384 | 607 | (void *)(((unsigned long) rand_r(&rand_lookup) % init_pool_size) + init_pool_offset), |
cd1ae16a | 608 | sizeof(void *)); |
48ed1c18 MD |
609 | rcu_read_lock(); |
610 | if (add_unique) { | |
cd1ae16a | 611 | ret_node = cds_lfht_add_unique(test_ht, node); |
48ed1c18 MD |
612 | } else { |
613 | if (add_replace) | |
9357c415 | 614 | ret_node = cds_lfht_add_replace(test_ht, node); |
48ed1c18 MD |
615 | else |
616 | cds_lfht_add(test_ht, node); | |
617 | } | |
618 | rcu_read_unlock(); | |
cd1ae16a MD |
619 | if (add_unique && ret_node != node) { |
620 | free(node); | |
621 | nr_addexist++; | |
48ed1c18 MD |
622 | } else { |
623 | if (add_replace && ret_node) { | |
624 | call_rcu(&ret_node->head, free_node_cb); | |
625 | nr_addexist++; | |
626 | } else { | |
627 | nr_add++; | |
628 | } | |
629 | } | |
cd1ae16a MD |
630 | nr_writes++; |
631 | } | |
5dc45f25 | 632 | return 0; |
cd1ae16a MD |
633 | } |
634 | ||
175ec0eb MD |
635 | static |
636 | void test_delete_all_nodes(struct cds_lfht *ht) | |
637 | { | |
638 | struct cds_lfht_iter iter; | |
639 | struct cds_lfht_node *node; | |
640 | unsigned long count = 0; | |
641 | ||
642 | cds_lfht_first(ht, &iter); | |
643 | while ((node = cds_lfht_iter_get_node(&iter)) != NULL) { | |
644 | int ret; | |
645 | ||
646 | ret = cds_lfht_del(test_ht, &iter); | |
647 | assert(!ret); | |
648 | call_rcu(&node->head, free_node_cb); | |
649 | cds_lfht_next(ht, &iter); | |
650 | count++; | |
651 | } | |
652 | printf("deleted %lu nodes.\n", count); | |
653 | } | |
654 | ||
ab7d5fc6 MD |
655 | void show_usage(int argc, char **argv) |
656 | { | |
48ed1c18 | 657 | printf("Usage : %s nr_readers nr_writers duration (s)\n", argv[0]); |
ab7d5fc6 | 658 | #ifdef DEBUG_YIELD |
48ed1c18 | 659 | printf(" [-r] [-w] (yield reader and/or writer)\n"); |
ab7d5fc6 | 660 | #endif |
48ed1c18 MD |
661 | printf(" [-d delay] (writer period (us))\n"); |
662 | printf(" [-c duration] (reader C.S. duration (in loops))\n"); | |
663 | printf(" [-v] (verbose output)\n"); | |
664 | printf(" [-a cpu#] [-a cpu#]... (affinity)\n"); | |
665 | printf(" [-h size] (initial hash table size)\n"); | |
666 | printf(" [not -u nor -s] Add entries (supports redundant keys).\n"); | |
667 | printf(" [-u] Uniquify add (no redundant keys).\n"); | |
668 | printf(" [-s] Replace (swap) entries.\n"); | |
669 | printf(" [-i] Add only (no removal).\n"); | |
670 | printf(" [-k nr_nodes] Number of nodes to insert initially.\n"); | |
671 | printf(" [-A] Automatically resize hash table.\n"); | |
672 | printf(" [-R offset] Lookup pool offset.\n"); | |
673 | printf(" [-S offset] Write pool offset.\n"); | |
674 | printf(" [-T offset] Init pool offset.\n"); | |
675 | printf(" [-M size] Lookup pool size.\n"); | |
676 | printf(" [-N size] Write pool size.\n"); | |
677 | printf(" [-O size] Init pool size.\n"); | |
678 | printf(" [-V] Validate lookups of init values (use with filled init pool, same lookup range, with different write range).\n"); | |
679 | printf("\n\n"); | |
ab7d5fc6 MD |
680 | } |
681 | ||
682 | int main(int argc, char **argv) | |
683 | { | |
684 | int err; | |
685 | pthread_t *tid_reader, *tid_writer; | |
7ed7682f | 686 | pthread_t tid_count; |
ab7d5fc6 | 687 | void *tret; |
b8e1907c MD |
688 | unsigned long long *count_reader; |
689 | struct wr_count *count_writer; | |
690 | unsigned long long tot_reads = 0, tot_writes = 0, | |
3c16bf4b | 691 | tot_add = 0, tot_add_exist = 0, tot_remove = 0; |
d933dd0e MD |
692 | unsigned long count, removed; |
693 | long approx_before, approx_after; | |
5e28c532 | 694 | int i, a, ret; |
3967a8a8 MD |
695 | struct sigaction act; |
696 | unsigned int remain; | |
ab7d5fc6 MD |
697 | |
698 | if (argc < 4) { | |
699 | show_usage(argc, argv); | |
700 | return -1; | |
701 | } | |
702 | ||
703 | err = sscanf(argv[1], "%u", &nr_readers); | |
704 | if (err != 1) { | |
705 | show_usage(argc, argv); | |
706 | return -1; | |
707 | } | |
708 | ||
709 | err = sscanf(argv[2], "%u", &nr_writers); | |
710 | if (err != 1) { | |
711 | show_usage(argc, argv); | |
712 | return -1; | |
713 | } | |
714 | ||
715 | err = sscanf(argv[3], "%lu", &duration); | |
716 | if (err != 1) { | |
717 | show_usage(argc, argv); | |
718 | return -1; | |
719 | } | |
720 | ||
721 | for (i = 4; i < argc; i++) { | |
722 | if (argv[i][0] != '-') | |
723 | continue; | |
724 | switch (argv[i][1]) { | |
725 | #ifdef DEBUG_YIELD | |
726 | case 'r': | |
727 | yield_active |= YIELD_READ; | |
728 | break; | |
729 | case 'w': | |
730 | yield_active |= YIELD_WRITE; | |
731 | break; | |
732 | #endif | |
733 | case 'a': | |
734 | if (argc < i + 2) { | |
735 | show_usage(argc, argv); | |
736 | return -1; | |
737 | } | |
738 | a = atoi(argv[++i]); | |
739 | cpu_affinities[next_aff++] = a; | |
740 | use_affinity = 1; | |
741 | printf_verbose("Adding CPU %d affinity\n", a); | |
742 | break; | |
743 | case 'c': | |
744 | if (argc < i + 2) { | |
745 | show_usage(argc, argv); | |
746 | return -1; | |
747 | } | |
748 | rduration = atol(argv[++i]); | |
749 | break; | |
750 | case 'd': | |
751 | if (argc < i + 2) { | |
752 | show_usage(argc, argv); | |
753 | return -1; | |
754 | } | |
755 | wdelay = atol(argv[++i]); | |
756 | break; | |
757 | case 'v': | |
758 | verbose_mode = 1; | |
759 | break; | |
6d5c0ca9 MD |
760 | case 'h': |
761 | if (argc < i + 2) { | |
762 | show_usage(argc, argv); | |
763 | return -1; | |
764 | } | |
765 | init_hash_size = atol(argv[++i]); | |
766 | break; | |
767 | case 'u': | |
48ed1c18 MD |
768 | if (add_replace) { |
769 | printf("Please specify at most one of -s or -u.\n"); | |
770 | exit(-1); | |
771 | } | |
6d5c0ca9 MD |
772 | add_unique = 1; |
773 | break; | |
48ed1c18 MD |
774 | case 's': |
775 | if (add_unique) { | |
776 | printf("Please specify at most one of -s or -u.\n"); | |
777 | exit(-1); | |
778 | } | |
779 | add_replace = 1; | |
780 | break; | |
6d5c0ca9 MD |
781 | case 'i': |
782 | add_only = 1; | |
783 | break; | |
cd1ae16a MD |
784 | case 'k': |
785 | init_populate = atol(argv[++i]); | |
786 | break; | |
151e7a93 MD |
787 | case 'A': |
788 | opt_auto_resize = 1; | |
789 | break; | |
8008a032 MD |
790 | case 'R': |
791 | lookup_pool_offset = atol(argv[++i]); | |
792 | break; | |
793 | case 'S': | |
794 | write_pool_offset = atol(argv[++i]); | |
795 | break; | |
796 | case 'T': | |
797 | init_pool_offset = atol(argv[++i]); | |
798 | break; | |
d837911d MD |
799 | case 'M': |
800 | lookup_pool_size = atol(argv[++i]); | |
801 | break; | |
802 | case 'N': | |
803 | write_pool_size = atol(argv[++i]); | |
804 | break; | |
805 | case 'O': | |
806 | init_pool_size = atol(argv[++i]); | |
807 | break; | |
59b10634 MD |
808 | case 'V': |
809 | validate_lookup = 1; | |
810 | break; | |
8008a032 | 811 | |
ab7d5fc6 MD |
812 | } |
813 | } | |
814 | ||
6d5c0ca9 MD |
815 | /* Check if hash size is power of 2 */ |
816 | if (init_hash_size && init_hash_size & (init_hash_size - 1)) { | |
817 | printf("Error: Hash table size %lu is not a power of 2.\n", | |
818 | init_hash_size); | |
819 | return -1; | |
820 | } | |
821 | ||
3967a8a8 MD |
822 | memset(&act, 0, sizeof(act)); |
823 | ret = sigemptyset(&act.sa_mask); | |
824 | if (ret == -1) { | |
825 | perror("sigemptyset"); | |
826 | return -1; | |
827 | } | |
828 | act.sa_handler = sigusr1_handler; | |
829 | act.sa_flags = SA_RESTART; | |
830 | ret = sigaction(SIGUSR1, &act, NULL); | |
831 | if (ret == -1) { | |
832 | perror("sigaction"); | |
833 | return -1; | |
834 | } | |
7ed7682f MD |
835 | |
836 | ret = pipe(count_pipe); | |
837 | if (ret == -1) { | |
838 | perror("pipe"); | |
839 | return -1; | |
840 | } | |
841 | ||
842 | /* spawn counter thread */ | |
843 | err = pthread_create(&tid_count, NULL, thr_count, | |
844 | NULL); | |
845 | if (err != 0) | |
846 | exit(1); | |
847 | ||
973e5e1b MD |
848 | act.sa_handler = sigusr2_handler; |
849 | act.sa_flags = SA_RESTART; | |
850 | ret = sigaction(SIGUSR2, &act, NULL); | |
851 | if (ret == -1) { | |
852 | perror("sigaction"); | |
853 | return -1; | |
854 | } | |
3967a8a8 | 855 | |
ab7d5fc6 MD |
856 | printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", |
857 | duration, nr_readers, nr_writers); | |
858 | printf_verbose("Writer delay : %lu loops.\n", wdelay); | |
859 | printf_verbose("Reader duration : %lu loops.\n", rduration); | |
6d5c0ca9 MD |
860 | printf_verbose("Mode:%s%s.\n", |
861 | add_only ? " add only" : " add/remove", | |
48ed1c18 | 862 | add_unique ? " uniquify" : ( add_replace ? " replace" : " insert")); |
6d5c0ca9 | 863 | printf_verbose("Initial hash table size: %lu buckets.\n", init_hash_size); |
5dc00396 MD |
864 | printf_verbose("Init pool size offset %lu size %lu.\n", |
865 | init_pool_offset, init_pool_size); | |
866 | printf_verbose("Lookup pool size offset %lu size %lu.\n", | |
867 | lookup_pool_offset, lookup_pool_size); | |
868 | printf_verbose("Update pool size offset %lu size %lu.\n", | |
869 | write_pool_offset, write_pool_size); | |
ab7d5fc6 MD |
870 | printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", |
871 | "main", pthread_self(), (unsigned long)gettid()); | |
872 | ||
ab7d5fc6 MD |
873 | tid_reader = malloc(sizeof(*tid_reader) * nr_readers); |
874 | tid_writer = malloc(sizeof(*tid_writer) * nr_writers); | |
875 | count_reader = malloc(sizeof(*count_reader) * nr_readers); | |
876 | count_writer = malloc(sizeof(*count_writer) * nr_writers); | |
48ed1c18 MD |
877 | |
878 | err = create_all_cpu_call_rcu_data(0); | |
879 | assert(!err); | |
880 | ||
881 | /* | |
882 | * Hash creation and population needs to be seen as a RCU reader | |
883 | * thread from the point of view of resize. | |
884 | */ | |
885 | rcu_register_thread(); | |
14044b37 | 886 | test_ht = cds_lfht_new(test_hash, test_compare, 0x42UL, |
151e7a93 | 887 | init_hash_size, |
b7d619b0 | 888 | opt_auto_resize ? CDS_LFHT_AUTO_RESIZE : 0, NULL); |
48ed1c18 | 889 | ret = populate_hash(); |
5dc45f25 | 890 | assert(!ret); |
59e371e3 MD |
891 | |
892 | rcu_thread_offline(); | |
14756542 | 893 | |
ab7d5fc6 MD |
894 | next_aff = 0; |
895 | ||
896 | for (i = 0; i < nr_readers; i++) { | |
897 | err = pthread_create(&tid_reader[i], NULL, thr_reader, | |
898 | &count_reader[i]); | |
899 | if (err != 0) | |
900 | exit(1); | |
901 | } | |
902 | for (i = 0; i < nr_writers; i++) { | |
903 | err = pthread_create(&tid_writer[i], NULL, thr_writer, | |
904 | &count_writer[i]); | |
905 | if (err != 0) | |
906 | exit(1); | |
907 | } | |
908 | ||
abc490a1 | 909 | cmm_smp_mb(); |
ab7d5fc6 MD |
910 | |
911 | test_go = 1; | |
912 | ||
3967a8a8 MD |
913 | remain = duration; |
914 | do { | |
915 | remain = sleep(remain); | |
916 | } while (remain > 0); | |
ab7d5fc6 MD |
917 | |
918 | test_stop = 1; | |
919 | ||
920 | for (i = 0; i < nr_readers; i++) { | |
921 | err = pthread_join(tid_reader[i], &tret); | |
922 | if (err != 0) | |
923 | exit(1); | |
924 | tot_reads += count_reader[i]; | |
925 | } | |
926 | for (i = 0; i < nr_writers; i++) { | |
927 | err = pthread_join(tid_writer[i], &tret); | |
928 | if (err != 0) | |
929 | exit(1); | |
b8e1907c MD |
930 | tot_writes += count_writer[i].update_ops; |
931 | tot_add += count_writer[i].add; | |
3c16bf4b | 932 | tot_add_exist += count_writer[i].add_exist; |
b8e1907c | 933 | tot_remove += count_writer[i].remove; |
ab7d5fc6 | 934 | } |
7ed7682f MD |
935 | |
936 | /* teardown counter thread */ | |
937 | act.sa_handler = SIG_IGN; | |
938 | act.sa_flags = SA_RESTART; | |
939 | ret = sigaction(SIGUSR2, &act, NULL); | |
940 | if (ret == -1) { | |
941 | perror("sigaction"); | |
942 | return -1; | |
943 | } | |
944 | { | |
945 | char msg[1] = { 0x42 }; | |
946 | write(count_pipe[1], msg, 1); /* wakeup thread */ | |
947 | } | |
948 | err = pthread_join(tid_count, &tret); | |
949 | if (err != 0) | |
950 | exit(1); | |
951 | ||
33c7c748 | 952 | fflush(stdout); |
59e371e3 MD |
953 | rcu_thread_online(); |
954 | rcu_read_lock(); | |
175ec0eb | 955 | printf("Counting nodes... "); |
973e5e1b MD |
956 | cds_lfht_count_nodes(test_ht, &approx_before, &count, &removed, |
957 | &approx_after); | |
175ec0eb MD |
958 | printf("done.\n"); |
959 | test_delete_all_nodes(test_ht); | |
59e371e3 MD |
960 | rcu_read_unlock(); |
961 | rcu_thread_offline(); | |
973e5e1b | 962 | if (count || removed) { |
d933dd0e | 963 | printf("Approximation before node accounting: %ld nodes.\n", |
973e5e1b | 964 | approx_before); |
175ec0eb | 965 | printf("Nodes deleted from hash table before destroy: " |
973e5e1b MD |
966 | "%lu nodes + %lu logically removed.\n", |
967 | count, removed); | |
d933dd0e | 968 | printf("Approximation after node accounting: %ld nodes.\n", |
973e5e1b MD |
969 | approx_after); |
970 | } | |
b7d619b0 | 971 | ret = cds_lfht_destroy(test_ht, NULL); |
33c7c748 MD |
972 | if (ret) |
973 | printf_verbose("final delete aborted\n"); | |
974 | else | |
975 | printf_verbose("final delete success\n"); | |
ab7d5fc6 MD |
976 | printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads, |
977 | tot_writes); | |
978 | printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu " | |
979 | "nr_writers %3u " | |
d837911d | 980 | "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu " |
cd1ae16a | 981 | "nr_add %12llu nr_add_fail %12llu nr_remove %12llu nr_leaked %12lld\n", |
ab7d5fc6 | 982 | argv[0], duration, nr_readers, rduration, |
d837911d | 983 | nr_writers, wdelay, tot_reads, tot_writes, |
3c16bf4b | 984 | tot_reads + tot_writes, tot_add, tot_add_exist, tot_remove, |
cd1ae16a | 985 | (long long) tot_add + init_populate - tot_remove - count); |
59e371e3 | 986 | rcu_unregister_thread(); |
3a22f1dd | 987 | free_all_cpu_call_rcu_data(); |
ab7d5fc6 MD |
988 | free(tid_reader); |
989 | free(tid_writer); | |
990 | free(count_reader); | |
991 | free(count_writer); | |
992 | return 0; | |
993 | } |