Merge commit 'origin/urcu/ht' into urcu/ht
[urcu.git] / tests / test_ht.c
CommitLineData
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>
33#include <sys/syscall.h>
34#include <sched.h>
5e28c532
MD
35#include <urcu-ht.h>
36#include <urcu-defer.h>
37#include <errno.h>
ab7d5fc6
MD
38
39#include "../arch.h"
40
5e28c532
MD
41#define HASH_SIZE 32
42#define RAND_POOL 1000
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
50#if defined(_syscall0)
51_syscall0(pid_t, gettid)
52#elif defined(__NR_gettid)
53static inline pid_t gettid(void)
54{
55 return syscall(__NR_gettid);
56}
57#else
58#warning "use pid as tid"
59static 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
5e28c532
MD
72static unsigned int __thread rand_lookup;
73static unsigned long __thread nr_add;
74static unsigned long __thread nr_addexist;
75static unsigned long __thread nr_del;
76static unsigned long __thread nr_delnoent;
77static unsigned long __thread lookup_fail;
78static unsigned long __thread lookup_ok;
79
ab7d5fc6
MD
80static struct rcu_ht *test_ht;
81
5e28c532 82struct test_data {
ab7d5fc6 83 int a;
5e28c532 84 int b;
ab7d5fc6
MD
85};
86
87static volatile int test_go, test_stop;
88
89static unsigned long wdelay;
90
ab7d5fc6
MD
91static unsigned long duration;
92
93/* read-side C.S. duration, in loops */
94static unsigned long rduration;
95
96static inline void loop_sleep(unsigned long l)
97{
98 while(l-- != 0)
99 cpu_relax();
100}
101
102static int verbose_mode;
103
104#define printf_verbose(fmt, args...) \
105 do { \
106 if (verbose_mode) \
107 printf(fmt, args); \
108 } while (0)
109
110static unsigned int cpu_affinities[NR_CPUS];
111static unsigned int next_aff = 0;
112static int use_affinity = 0;
113
114pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
115
116static 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 */
144static int test_duration_write(void)
145{
146 return !test_stop;
147}
148
149static int test_duration_read(void)
150{
151 return !test_stop;
152}
153
154static unsigned long long __thread nr_writes;
155static unsigned long long __thread nr_reads;
156
157static unsigned int nr_readers;
158static unsigned int nr_writers;
159
160pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
161
162void 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
172void 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
ab7d5fc6 183#define ARRAY_POISON 0xDEADBEEF
ab7d5fc6
MD
184
185void *thr_reader(void *_count)
186{
187 unsigned long long *count = _count;
5e28c532 188 struct test_data *local_ptr;
ab7d5fc6
MD
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();
5e28c532
MD
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++;
ab7d5fc6 210 debug_yield_read();
ab7d5fc6
MD
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());
5e28c532
MD
224 printf_verbose("readid : %lx, lookupfail %lu, lookupok %lu\n",
225 pthread_self(), lookup_fail, lookup_ok);
ab7d5fc6
MD
226 return ((void*)1);
227
228}
229
230void *thr_writer(void *_count)
231{
232 unsigned long long *count = _count;
5e28c532
MD
233 struct test_data *data;
234 int ret;
ab7d5fc6
MD
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
5e28c532
MD
241 rcu_register_thread();
242 rcu_defer_register_thread();
243
ab7d5fc6
MD
244 while (!test_go)
245 {
246 }
247 smp_mb();
248
249 for (;;) {
5e28c532
MD
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 }
ab7d5fc6
MD
270 nr_writes++;
271 if (unlikely(!test_duration_write()))
272 break;
273 if (unlikely(wdelay))
274 loop_sleep(wdelay);
275 }
276
5e28c532
MD
277 rcu_defer_unregister_thread();
278 rcu_unregister_thread();
279
ab7d5fc6
MD
280 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
281 "writer", pthread_self(), (unsigned long)gettid());
5e28c532
MD
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);
ab7d5fc6
MD
285 *count = nr_writes;
286 return ((void*)2);
287}
288
289void 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
302int 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;
5e28c532 309 int i, a, ret;
ab7d5fc6
MD
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
ab7d5fc6
MD
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);
5e28c532
MD
387 test_ht = ht_new(ht_jhash, free, HASH_SIZE, sizeof(unsigned long),
388 43223455);
ab7d5fc6
MD
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 }
5e28c532
MD
424 rcu_register_thread();
425 rcu_defer_register_thread();
426 ret = ht_destroy(test_ht);
427 rcu_defer_unregister_thread();
428 rcu_unregister_thread();
ab7d5fc6 429
5e28c532 430 printf_verbose("final delete: %d items\n", ret);
ab7d5fc6
MD
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);
ab7d5fc6
MD
439 free(tid_reader);
440 free(tid_writer);
441 free(count_reader);
442 free(count_writer);
443 return 0;
444}
This page took 0.039504 seconds and 4 git commands to generate.