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