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