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