Commit | Line | Data |
---|---|---|
ab7d5fc6 MD |
1 | /* |
2 | * test_ht.c | |
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 | |
5e28c532 MD |
40 | #define HASH_SIZE 32 |
41 | #define RAND_POOL 1000 | |
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 | ||
49 | #if defined(_syscall0) | |
50 | _syscall0(pid_t, gettid) | |
51 | #elif defined(__NR_gettid) | |
52 | static inline pid_t gettid(void) | |
53 | { | |
54 | return syscall(__NR_gettid); | |
55 | } | |
56 | #else | |
57 | #warning "use pid as tid" | |
58 | static inline pid_t gettid(void) | |
59 | { | |
60 | return getpid(); | |
61 | } | |
62 | #endif | |
63 | ||
64 | #ifndef DYNAMIC_LINK_TEST | |
65 | #define _LGPL_SOURCE | |
66 | #else | |
67 | #define debug_yield_read() | |
68 | #endif | |
abc490a1 MD |
69 | #include <urcu.h> |
70 | #include <urcu/rculfhash.h> | |
71 | #include <urcu-call-rcu.h> | |
ab7d5fc6 | 72 | |
5e28c532 MD |
73 | static unsigned int __thread rand_lookup; |
74 | static unsigned long __thread nr_add; | |
75 | static unsigned long __thread nr_addexist; | |
76 | static unsigned long __thread nr_del; | |
77 | static unsigned long __thread nr_delnoent; | |
78 | static unsigned long __thread lookup_fail; | |
79 | static unsigned long __thread lookup_ok; | |
80 | ||
ab7d5fc6 MD |
81 | static struct rcu_ht *test_ht; |
82 | ||
5e28c532 | 83 | struct test_data { |
ab7d5fc6 | 84 | int a; |
5e28c532 | 85 | int b; |
ab7d5fc6 MD |
86 | }; |
87 | ||
88 | static volatile int test_go, test_stop; | |
89 | ||
90 | static unsigned long wdelay; | |
91 | ||
ab7d5fc6 MD |
92 | static unsigned long duration; |
93 | ||
94 | /* read-side C.S. duration, in loops */ | |
95 | static unsigned long rduration; | |
96 | ||
97 | static inline void loop_sleep(unsigned long l) | |
98 | { | |
99 | while(l-- != 0) | |
abc490a1 | 100 | caa_cpu_relax(); |
ab7d5fc6 MD |
101 | } |
102 | ||
103 | static int verbose_mode; | |
104 | ||
105 | #define printf_verbose(fmt, args...) \ | |
106 | do { \ | |
107 | if (verbose_mode) \ | |
108 | printf(fmt, args); \ | |
109 | } while (0) | |
110 | ||
111 | static unsigned int cpu_affinities[NR_CPUS]; | |
112 | static unsigned int next_aff = 0; | |
113 | static int use_affinity = 0; | |
114 | ||
115 | pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; | |
116 | ||
117 | static void set_affinity(void) | |
118 | { | |
119 | cpu_set_t mask; | |
120 | int cpu; | |
121 | int ret; | |
122 | ||
123 | if (!use_affinity) | |
124 | return; | |
125 | ||
126 | ret = pthread_mutex_lock(&affinity_mutex); | |
127 | if (ret) { | |
128 | perror("Error in pthread mutex lock"); | |
129 | exit(-1); | |
130 | } | |
131 | cpu = cpu_affinities[next_aff++]; | |
132 | ret = pthread_mutex_unlock(&affinity_mutex); | |
133 | if (ret) { | |
134 | perror("Error in pthread mutex unlock"); | |
135 | exit(-1); | |
136 | } | |
137 | CPU_ZERO(&mask); | |
138 | CPU_SET(cpu, &mask); | |
139 | sched_setaffinity(0, sizeof(mask), &mask); | |
140 | } | |
141 | ||
142 | /* | |
143 | * returns 0 if test should end. | |
144 | */ | |
145 | static int test_duration_write(void) | |
146 | { | |
147 | return !test_stop; | |
148 | } | |
149 | ||
150 | static int test_duration_read(void) | |
151 | { | |
152 | return !test_stop; | |
153 | } | |
154 | ||
155 | static unsigned long long __thread nr_writes; | |
156 | static unsigned long long __thread nr_reads; | |
157 | ||
158 | static unsigned int nr_readers; | |
159 | static unsigned int nr_writers; | |
160 | ||
161 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; | |
162 | ||
163 | void rcu_copy_mutex_lock(void) | |
164 | { | |
165 | int ret; | |
166 | ret = pthread_mutex_lock(&rcu_copy_mutex); | |
167 | if (ret) { | |
168 | perror("Error in pthread mutex lock"); | |
169 | exit(-1); | |
170 | } | |
171 | } | |
172 | ||
173 | void rcu_copy_mutex_unlock(void) | |
174 | { | |
175 | int ret; | |
176 | ||
177 | ret = pthread_mutex_unlock(&rcu_copy_mutex); | |
178 | if (ret) { | |
179 | perror("Error in pthread mutex unlock"); | |
180 | exit(-1); | |
181 | } | |
182 | } | |
183 | ||
abc490a1 MD |
184 | static |
185 | unsigned long test_hash(void *_hashseed, void *_key) | |
186 | { | |
187 | unsigned long seed = (unsigned long) _hashseed; | |
188 | unsigned long key = (unsigned long) _key; | |
189 | unsigned long v; | |
190 | ||
191 | v = key ^ seed; | |
192 | v ^= v << 8; | |
193 | v ^= v << 16; | |
194 | v ^= v << 27; | |
195 | v ^= v >> 8; | |
196 | v ^= v >> 16; | |
197 | v ^= v >> 27; | |
198 | ||
199 | return v; | |
200 | } | |
ab7d5fc6 MD |
201 | |
202 | void *thr_reader(void *_count) | |
203 | { | |
204 | unsigned long long *count = _count; | |
abc490a1 | 205 | struct rcu_ht_node *node; |
ab7d5fc6 MD |
206 | |
207 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
208 | "reader", pthread_self(), (unsigned long)gettid()); | |
209 | ||
210 | set_affinity(); | |
211 | ||
212 | rcu_register_thread(); | |
213 | ||
214 | while (!test_go) | |
215 | { | |
216 | } | |
abc490a1 | 217 | cmm_smp_mb(); |
ab7d5fc6 MD |
218 | |
219 | for (;;) { | |
220 | rcu_read_lock(); | |
abc490a1 | 221 | node = ht_lookup(test_ht, |
5e28c532 | 222 | (void *)(unsigned long)(rand_r(&rand_lookup) % RAND_POOL)); |
abc490a1 | 223 | if (node == NULL) |
5e28c532 MD |
224 | lookup_fail++; |
225 | else | |
226 | lookup_ok++; | |
ab7d5fc6 | 227 | debug_yield_read(); |
ab7d5fc6 MD |
228 | if (unlikely(rduration)) |
229 | loop_sleep(rduration); | |
230 | rcu_read_unlock(); | |
231 | nr_reads++; | |
232 | if (unlikely(!test_duration_read())) | |
233 | break; | |
234 | } | |
235 | ||
236 | rcu_unregister_thread(); | |
237 | ||
238 | *count = nr_reads; | |
239 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", | |
240 | "reader", pthread_self(), (unsigned long)gettid()); | |
5e28c532 MD |
241 | printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n", |
242 | pthread_self(), lookup_fail, lookup_ok); | |
ab7d5fc6 MD |
243 | return ((void*)1); |
244 | ||
245 | } | |
246 | ||
abc490a1 MD |
247 | static |
248 | void free_node_cb(struct rcu_head *head) | |
249 | { | |
250 | struct rcu_ht_node *node = | |
251 | caa_container_of(head, struct rcu_ht_node, head); | |
252 | free(node); | |
253 | } | |
254 | ||
ab7d5fc6 MD |
255 | void *thr_writer(void *_count) |
256 | { | |
abc490a1 | 257 | struct rcu_ht_node *node; |
ab7d5fc6 | 258 | unsigned long long *count = _count; |
5e28c532 | 259 | int ret; |
ab7d5fc6 MD |
260 | |
261 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
262 | "writer", pthread_self(), (unsigned long)gettid()); | |
263 | ||
264 | set_affinity(); | |
265 | ||
5e28c532 | 266 | rcu_register_thread(); |
5e28c532 | 267 | |
ab7d5fc6 MD |
268 | while (!test_go) |
269 | { | |
270 | } | |
abc490a1 | 271 | cmm_smp_mb(); |
ab7d5fc6 MD |
272 | |
273 | for (;;) { | |
5e28c532 | 274 | if (rand_r(&rand_lookup) & 1) { |
abc490a1 MD |
275 | node = malloc(sizeof(struct rcu_ht_node)); |
276 | rcu_read_lock(); | |
277 | ht_node_init(node, | |
278 | (void *)(unsigned long)(rand_r(&rand_lookup) % RAND_POOL), | |
279 | (void *) 0x42); | |
280 | ret = ht_add(test_ht, node); | |
281 | rcu_read_unlock(); | |
5e28c532 | 282 | if (ret == -EEXIST) { |
abc490a1 | 283 | free(node); |
5e28c532 MD |
284 | nr_addexist++; |
285 | } else { | |
286 | nr_add++; | |
287 | } | |
288 | } else { | |
289 | /* May delete */ | |
abc490a1 MD |
290 | rcu_read_lock(); |
291 | node = ht_lookup(test_ht, | |
292 | (void *)(unsigned long)(rand_r(&rand_lookup) % RAND_POOL)); | |
293 | if (node) | |
294 | ret = ht_remove(test_ht, node); | |
5e28c532 | 295 | else |
abc490a1 MD |
296 | ret = -ENOENT; |
297 | rcu_read_unlock(); | |
298 | if (ret == 0) { | |
299 | call_rcu(&node->head, free_node_cb); | |
5e28c532 | 300 | nr_del++; |
abc490a1 MD |
301 | } else |
302 | nr_delnoent++; | |
44395fb7 | 303 | } |
abc490a1 | 304 | #if 0 |
44395fb7 MD |
305 | //if (nr_writes % 100000 == 0) { |
306 | if (nr_writes % 1000 == 0) { | |
abc490a1 | 307 | rcu_read_lock(); |
44395fb7 MD |
308 | if (rand_r(&rand_lookup) & 1) { |
309 | ht_resize(test_ht, 1); | |
310 | } else { | |
311 | ht_resize(test_ht, -1); | |
312 | } | |
abc490a1 | 313 | rcu_read_unlock(); |
5e28c532 | 314 | } |
abc490a1 | 315 | #endif //0 |
ab7d5fc6 MD |
316 | nr_writes++; |
317 | if (unlikely(!test_duration_write())) | |
318 | break; | |
319 | if (unlikely(wdelay)) | |
320 | loop_sleep(wdelay); | |
321 | } | |
322 | ||
5e28c532 MD |
323 | rcu_unregister_thread(); |
324 | ||
ab7d5fc6 MD |
325 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", |
326 | "writer", pthread_self(), (unsigned long)gettid()); | |
5e28c532 MD |
327 | printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, " |
328 | "nr_delnoent %lu\n", pthread_self(), nr_add, | |
329 | nr_addexist, nr_del, nr_delnoent); | |
ab7d5fc6 MD |
330 | *count = nr_writes; |
331 | return ((void*)2); | |
332 | } | |
333 | ||
334 | void show_usage(int argc, char **argv) | |
335 | { | |
336 | printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]); | |
337 | #ifdef DEBUG_YIELD | |
338 | printf(" [-r] [-w] (yield reader and/or writer)"); | |
339 | #endif | |
340 | printf(" [-d delay] (writer period (us))"); | |
341 | printf(" [-c duration] (reader C.S. duration (in loops))"); | |
342 | printf(" [-v] (verbose output)"); | |
343 | printf(" [-a cpu#] [-a cpu#]... (affinity)"); | |
344 | printf("\n"); | |
345 | } | |
346 | ||
347 | int main(int argc, char **argv) | |
348 | { | |
349 | int err; | |
350 | pthread_t *tid_reader, *tid_writer; | |
351 | void *tret; | |
352 | unsigned long long *count_reader, *count_writer; | |
353 | unsigned long long tot_reads = 0, tot_writes = 0; | |
5e28c532 | 354 | int i, a, ret; |
ab7d5fc6 MD |
355 | |
356 | if (argc < 4) { | |
357 | show_usage(argc, argv); | |
358 | return -1; | |
359 | } | |
360 | ||
361 | err = sscanf(argv[1], "%u", &nr_readers); | |
362 | if (err != 1) { | |
363 | show_usage(argc, argv); | |
364 | return -1; | |
365 | } | |
366 | ||
367 | err = sscanf(argv[2], "%u", &nr_writers); | |
368 | if (err != 1) { | |
369 | show_usage(argc, argv); | |
370 | return -1; | |
371 | } | |
372 | ||
373 | err = sscanf(argv[3], "%lu", &duration); | |
374 | if (err != 1) { | |
375 | show_usage(argc, argv); | |
376 | return -1; | |
377 | } | |
378 | ||
379 | for (i = 4; i < argc; i++) { | |
380 | if (argv[i][0] != '-') | |
381 | continue; | |
382 | switch (argv[i][1]) { | |
383 | #ifdef DEBUG_YIELD | |
384 | case 'r': | |
385 | yield_active |= YIELD_READ; | |
386 | break; | |
387 | case 'w': | |
388 | yield_active |= YIELD_WRITE; | |
389 | break; | |
390 | #endif | |
391 | case 'a': | |
392 | if (argc < i + 2) { | |
393 | show_usage(argc, argv); | |
394 | return -1; | |
395 | } | |
396 | a = atoi(argv[++i]); | |
397 | cpu_affinities[next_aff++] = a; | |
398 | use_affinity = 1; | |
399 | printf_verbose("Adding CPU %d affinity\n", a); | |
400 | break; | |
401 | case 'c': | |
402 | if (argc < i + 2) { | |
403 | show_usage(argc, argv); | |
404 | return -1; | |
405 | } | |
406 | rduration = atol(argv[++i]); | |
407 | break; | |
408 | case 'd': | |
409 | if (argc < i + 2) { | |
410 | show_usage(argc, argv); | |
411 | return -1; | |
412 | } | |
413 | wdelay = atol(argv[++i]); | |
414 | break; | |
415 | case 'v': | |
416 | verbose_mode = 1; | |
417 | break; | |
418 | } | |
419 | } | |
420 | ||
421 | printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", | |
422 | duration, nr_readers, nr_writers); | |
423 | printf_verbose("Writer delay : %lu loops.\n", wdelay); | |
424 | printf_verbose("Reader duration : %lu loops.\n", rduration); | |
425 | printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", | |
426 | "main", pthread_self(), (unsigned long)gettid()); | |
427 | ||
ab7d5fc6 MD |
428 | tid_reader = malloc(sizeof(*tid_reader) * nr_readers); |
429 | tid_writer = malloc(sizeof(*tid_writer) * nr_writers); | |
430 | count_reader = malloc(sizeof(*count_reader) * nr_readers); | |
431 | count_writer = malloc(sizeof(*count_writer) * nr_writers); | |
abc490a1 MD |
432 | test_ht = ht_new(test_hash, (void *) 0x42, |
433 | HASH_SIZE, call_rcu); | |
ab7d5fc6 MD |
434 | next_aff = 0; |
435 | ||
436 | for (i = 0; i < nr_readers; i++) { | |
437 | err = pthread_create(&tid_reader[i], NULL, thr_reader, | |
438 | &count_reader[i]); | |
439 | if (err != 0) | |
440 | exit(1); | |
441 | } | |
442 | for (i = 0; i < nr_writers; i++) { | |
443 | err = pthread_create(&tid_writer[i], NULL, thr_writer, | |
444 | &count_writer[i]); | |
445 | if (err != 0) | |
446 | exit(1); | |
447 | } | |
448 | ||
abc490a1 | 449 | cmm_smp_mb(); |
ab7d5fc6 MD |
450 | |
451 | test_go = 1; | |
452 | ||
453 | sleep(duration); | |
454 | ||
455 | test_stop = 1; | |
456 | ||
457 | for (i = 0; i < nr_readers; i++) { | |
458 | err = pthread_join(tid_reader[i], &tret); | |
459 | if (err != 0) | |
460 | exit(1); | |
461 | tot_reads += count_reader[i]; | |
462 | } | |
463 | for (i = 0; i < nr_writers; i++) { | |
464 | err = pthread_join(tid_writer[i], &tret); | |
465 | if (err != 0) | |
466 | exit(1); | |
467 | tot_writes += count_writer[i]; | |
468 | } | |
5e28c532 | 469 | rcu_register_thread(); |
5e28c532 | 470 | ret = ht_destroy(test_ht); |
5e28c532 | 471 | rcu_unregister_thread(); |
ab7d5fc6 | 472 | |
5e28c532 | 473 | printf_verbose("final delete: %d items\n", ret); |
ab7d5fc6 MD |
474 | printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads, |
475 | tot_writes); | |
476 | printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu " | |
477 | "nr_writers %3u " | |
478 | "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n", | |
479 | argv[0], duration, nr_readers, rduration, | |
480 | nr_writers, wdelay, tot_reads, tot_writes, | |
481 | tot_reads + tot_writes); | |
ab7d5fc6 MD |
482 | free(tid_reader); |
483 | free(tid_writer); | |
484 | free(count_reader); | |
485 | free(count_writer); | |
486 | return 0; | |
487 | } |