26361454997e98b6cd352410853909f36befffc1
[urcu.git] / tests / test_ht.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 ret = ht_add(test_ht,
253 (void *)(unsigned long)(rand_r(&rand_lookup) % RAND_POOL),
254 data);
255 if (ret == -EEXIST) {
256 free(data);
257 nr_addexist++;
258 } else {
259 nr_add++;
260 }
261 } else {
262 /* May delete */
263 ret = ht_delete(test_ht,
264 (void *)(unsigned long)(rand_r(&rand_lookup) % RAND_POOL));
265 if (ret == -ENOENT)
266 nr_delnoent++;
267 else
268 nr_del++;
269 }
270 nr_writes++;
271 if (unlikely(!test_duration_write()))
272 break;
273 if (unlikely(wdelay))
274 loop_sleep(wdelay);
275 }
276
277 rcu_defer_unregister_thread();
278 rcu_unregister_thread();
279
280 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
281 "writer", pthread_self(), (unsigned long)gettid());
282 printf_verbose("info id %lx: nr_add %lu, nr_addexist %lu, nr_del %lu, "
283 "nr_delnoent %lu\n", pthread_self(), nr_add,
284 nr_addexist, nr_del, nr_delnoent);
285 *count = nr_writes;
286 return ((void*)2);
287 }
288
289 void show_usage(int argc, char **argv)
290 {
291 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
292 #ifdef DEBUG_YIELD
293 printf(" [-r] [-w] (yield reader and/or writer)");
294 #endif
295 printf(" [-d delay] (writer period (us))");
296 printf(" [-c duration] (reader C.S. duration (in loops))");
297 printf(" [-v] (verbose output)");
298 printf(" [-a cpu#] [-a cpu#]... (affinity)");
299 printf("\n");
300 }
301
302 int main(int argc, char **argv)
303 {
304 int err;
305 pthread_t *tid_reader, *tid_writer;
306 void *tret;
307 unsigned long long *count_reader, *count_writer;
308 unsigned long long tot_reads = 0, tot_writes = 0;
309 int i, a, ret;
310
311 if (argc < 4) {
312 show_usage(argc, argv);
313 return -1;
314 }
315
316 err = sscanf(argv[1], "%u", &nr_readers);
317 if (err != 1) {
318 show_usage(argc, argv);
319 return -1;
320 }
321
322 err = sscanf(argv[2], "%u", &nr_writers);
323 if (err != 1) {
324 show_usage(argc, argv);
325 return -1;
326 }
327
328 err = sscanf(argv[3], "%lu", &duration);
329 if (err != 1) {
330 show_usage(argc, argv);
331 return -1;
332 }
333
334 for (i = 4; i < argc; i++) {
335 if (argv[i][0] != '-')
336 continue;
337 switch (argv[i][1]) {
338 #ifdef DEBUG_YIELD
339 case 'r':
340 yield_active |= YIELD_READ;
341 break;
342 case 'w':
343 yield_active |= YIELD_WRITE;
344 break;
345 #endif
346 case 'a':
347 if (argc < i + 2) {
348 show_usage(argc, argv);
349 return -1;
350 }
351 a = atoi(argv[++i]);
352 cpu_affinities[next_aff++] = a;
353 use_affinity = 1;
354 printf_verbose("Adding CPU %d affinity\n", a);
355 break;
356 case 'c':
357 if (argc < i + 2) {
358 show_usage(argc, argv);
359 return -1;
360 }
361 rduration = atol(argv[++i]);
362 break;
363 case 'd':
364 if (argc < i + 2) {
365 show_usage(argc, argv);
366 return -1;
367 }
368 wdelay = atol(argv[++i]);
369 break;
370 case 'v':
371 verbose_mode = 1;
372 break;
373 }
374 }
375
376 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
377 duration, nr_readers, nr_writers);
378 printf_verbose("Writer delay : %lu loops.\n", wdelay);
379 printf_verbose("Reader duration : %lu loops.\n", rduration);
380 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
381 "main", pthread_self(), (unsigned long)gettid());
382
383 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
384 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
385 count_reader = malloc(sizeof(*count_reader) * nr_readers);
386 count_writer = malloc(sizeof(*count_writer) * nr_writers);
387 test_ht = ht_new(ht_jhash, free, HASH_SIZE, sizeof(unsigned long),
388 43223455);
389 next_aff = 0;
390
391 for (i = 0; i < nr_readers; i++) {
392 err = pthread_create(&tid_reader[i], NULL, thr_reader,
393 &count_reader[i]);
394 if (err != 0)
395 exit(1);
396 }
397 for (i = 0; i < nr_writers; i++) {
398 err = pthread_create(&tid_writer[i], NULL, thr_writer,
399 &count_writer[i]);
400 if (err != 0)
401 exit(1);
402 }
403
404 smp_mb();
405
406 test_go = 1;
407
408 sleep(duration);
409
410 test_stop = 1;
411
412 for (i = 0; i < nr_readers; i++) {
413 err = pthread_join(tid_reader[i], &tret);
414 if (err != 0)
415 exit(1);
416 tot_reads += count_reader[i];
417 }
418 for (i = 0; i < nr_writers; i++) {
419 err = pthread_join(tid_writer[i], &tret);
420 if (err != 0)
421 exit(1);
422 tot_writes += count_writer[i];
423 }
424 rcu_register_thread();
425 rcu_defer_register_thread();
426 ret = ht_destroy(test_ht);
427 rcu_defer_unregister_thread();
428 rcu_unregister_thread();
429
430 printf_verbose("final delete: %d items\n", ret);
431 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
432 tot_writes);
433 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
434 "nr_writers %3u "
435 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
436 argv[0], duration, nr_readers, rduration,
437 nr_writers, wdelay, tot_reads, tot_writes,
438 tot_reads + tot_writes);
439 free(tid_reader);
440 free(tid_writer);
441 free(count_reader);
442 free(count_writer);
443 return 0;
444 }
This page took 0.037621 seconds and 4 git commands to generate.