4 * Userspace RCU library - test program
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
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.
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.
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.
28 #include <sys/types.h>
33 #include <sys/syscall.h>
38 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
39 #define CACHE_LINE_SIZE 4096
41 #if defined(_syscall0)
42 _syscall0(pid_t
, gettid
)
43 #elif defined(__NR_gettid)
44 static inline pid_t
gettid(void)
46 return syscall(__NR_gettid
);
49 #warning "use pid as tid"
50 static inline pid_t
gettid(void)
57 #include "urcu-qsbr.h"
63 static volatile int test_go
, test_stop
;
65 static unsigned long wdelay
;
67 static struct test_array
*test_rcu_pointer
;
69 static unsigned long duration
;
71 /* read-side C.S. duration, in loops */
72 static unsigned long rduration
;
74 static inline void loop_sleep(unsigned long l
)
80 static int verbose_mode
;
82 #define printf_verbose(fmt, args...) \
89 * returns 0 if test should end.
91 static int test_duration_write(void)
96 static int test_duration_read(void)
101 static unsigned long long __thread nr_writes
;
102 static unsigned long long __thread nr_reads
;
104 static unsigned int nr_readers
;
105 static unsigned int nr_writers
;
107 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
109 void rcu_copy_mutex_lock(void)
112 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
114 perror("Error in pthread mutex lock");
119 void rcu_copy_mutex_unlock(void)
123 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
125 perror("Error in pthread mutex unlock");
131 * malloc/free are reusing memory areas too quickly, which does not let us
132 * test races appropriately. Use a large circular array for allocations.
133 * ARRAY_SIZE is larger than nr_writers, which insures we never run over our tail.
135 #define ARRAY_SIZE (1048576 * nr_writers)
136 #define ARRAY_POISON 0xDEADBEEF
137 static int array_index
;
138 static struct test_array
*test_array
;
140 static struct test_array
*test_array_alloc(void)
142 struct test_array
*ret
;
145 rcu_copy_mutex_lock();
146 index
= array_index
% ARRAY_SIZE
;
147 assert(test_array
[index
].a
== ARRAY_POISON
||
148 test_array
[index
].a
== 0);
149 ret
= &test_array
[index
];
151 if (array_index
== ARRAY_SIZE
)
153 rcu_copy_mutex_unlock();
157 static void test_array_free(struct test_array
*ptr
)
161 rcu_copy_mutex_lock();
162 ptr
->a
= ARRAY_POISON
;
163 rcu_copy_mutex_unlock();
166 void *thr_reader(void *_count
)
168 unsigned long long *count
= _count
;
169 struct test_array
*local_ptr
;
171 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
172 "reader", pthread_self(), (unsigned long)gettid());
174 rcu_register_thread();
183 local_ptr
= _rcu_dereference(test_rcu_pointer
);
186 assert(local_ptr
->a
== 8);
187 if (unlikely(rduration
))
188 loop_sleep(rduration
);
191 /* QS each 1024 reads */
192 if (unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
193 _rcu_quiescent_state();
194 if (unlikely(!test_duration_read()))
198 rcu_unregister_thread();
201 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
202 "reader", pthread_self(), (unsigned long)gettid());
207 void *thr_writer(void *_count
)
209 unsigned long long *count
= _count
;
210 struct test_array
*new, *old
;
212 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
213 "writer", pthread_self(), (unsigned long)gettid());
221 new = test_array_alloc();
222 rcu_copy_mutex_lock();
223 old
= test_rcu_pointer
;
227 old
= _rcu_publish_content(&test_rcu_pointer
, new);
228 rcu_copy_mutex_unlock();
229 /* can be done after unlock */
232 test_array_free(old
);
234 if (unlikely(!test_duration_write()))
236 if (unlikely(wdelay
))
240 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
241 "writer", pthread_self(), (unsigned long)gettid());
246 void show_usage(int argc
, char **argv
)
248 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
250 printf(" [-r] [-w] (yield reader and/or writer)");
252 printf(" [-d delay] (writer period (us))");
253 printf(" [-c duration] (reader C.S. duration (in loops))");
254 printf(" [-v] (verbose output)");
255 printf(" [-a cpu#] [-a cpu#]... (affinity)");
261 int main(int argc
, char **argv
)
264 pthread_t
*tid_reader
, *tid_writer
;
266 unsigned long long *count_reader
, *count_writer
;
267 unsigned long long tot_reads
= 0, tot_writes
= 0;
269 int use_affinity
= 0;
272 show_usage(argc
, argv
);
276 err
= sscanf(argv
[1], "%u", &nr_readers
);
278 show_usage(argc
, argv
);
282 err
= sscanf(argv
[2], "%u", &nr_writers
);
284 show_usage(argc
, argv
);
288 err
= sscanf(argv
[3], "%lu", &duration
);
290 show_usage(argc
, argv
);
296 for (i
= 4; i
< argc
; i
++) {
297 if (argv
[i
][0] != '-')
299 switch (argv
[i
][1]) {
302 yield_active
|= YIELD_READ
;
305 yield_active
|= YIELD_WRITE
;
310 show_usage(argc
, argv
);
314 CPU_SET(a
, &affinity
);
316 printf_verbose("Adding CPU %d affinity\n", a
);
320 show_usage(argc
, argv
);
323 rduration
= atol(argv
[++i
]);
327 show_usage(argc
, argv
);
330 wdelay
= atol(argv
[++i
]);
338 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
339 duration
, nr_readers
, nr_writers
);
340 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
341 printf_verbose("Reader duration : %lu loops.\n", rduration
);
342 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
343 "main", pthread_self(), (unsigned long)gettid());
346 && sched_setaffinity(0, sizeof(affinity
), &affinity
) < 0) {
347 perror("sched_setaffinity");
351 test_array
= malloc(sizeof(*test_array
) * ARRAY_SIZE
);
352 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
353 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
354 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
355 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
357 for (i
= 0; i
< nr_readers
; i
++) {
358 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
363 for (i
= 0; i
< nr_writers
; i
++) {
364 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
378 for (i
= 0; i
< nr_readers
; i
++) {
379 err
= pthread_join(tid_reader
[i
], &tret
);
382 tot_reads
+= count_reader
[i
];
384 for (i
= 0; i
< nr_writers
; i
++) {
385 err
= pthread_join(tid_writer
[i
], &tret
);
388 tot_writes
+= count_writer
[i
];
391 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
393 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
395 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
396 argv
[0], duration
, nr_readers
, rduration
,
397 nr_writers
, wdelay
, tot_reads
, tot_writes
,
398 tot_reads
+ tot_writes
);
399 test_array_free(test_rcu_pointer
);