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