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 #if defined(_syscall0)
39 _syscall0(pid_t
, gettid
)
40 #elif defined(__NR_gettid)
41 static inline pid_t
gettid(void)
43 return syscall(__NR_gettid
);
46 #warning "use pid as tid"
47 static inline pid_t
gettid(void)
54 #include "urcu-qsbr.h"
60 static volatile int test_go
, test_stop
;
64 static struct test_array
*test_rcu_pointer
;
66 static unsigned long duration
;
68 /* read-side C.S. duration, in loops */
69 static unsigned long rduration
;
71 static inline void loop_sleep(unsigned long l
)
77 static int verbose_mode
;
79 #define printf_verbose(fmt, args...) \
86 * returns 0 if test should end.
88 static int test_duration_write(void)
93 static int test_duration_read(void)
98 static unsigned long long __thread nr_writes
;
99 static unsigned long long __thread nr_reads
;
101 static unsigned int nr_readers
;
102 static unsigned int nr_writers
;
104 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
106 void rcu_copy_mutex_lock(void)
109 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
111 perror("Error in pthread mutex lock");
116 void rcu_copy_mutex_unlock(void)
120 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
122 perror("Error in pthread mutex unlock");
128 * malloc/free are reusing memory areas too quickly, which does not let us
129 * test races appropriately. Use a large circular array for allocations.
130 * ARRAY_SIZE is larger than nr_writers, which insures we never run over our tail.
132 #define ARRAY_SIZE (1048576 * nr_writers)
133 #define ARRAY_POISON 0xDEADBEEF
134 static int array_index
;
135 static struct test_array
*test_array
;
137 static struct test_array
*test_array_alloc(void)
139 struct test_array
*ret
;
142 rcu_copy_mutex_lock();
143 index
= array_index
% ARRAY_SIZE
;
144 assert(test_array
[index
].a
== ARRAY_POISON
||
145 test_array
[index
].a
== 0);
146 ret
= &test_array
[index
];
148 if (array_index
== ARRAY_SIZE
)
150 rcu_copy_mutex_unlock();
154 static void test_array_free(struct test_array
*ptr
)
158 rcu_copy_mutex_lock();
159 ptr
->a
= ARRAY_POISON
;
160 rcu_copy_mutex_unlock();
163 void *thr_reader(void *_count
)
165 unsigned long long *count
= _count
;
166 struct test_array
*local_ptr
;
168 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
169 "reader", pthread_self(), (unsigned long)gettid());
171 rcu_register_thread();
180 local_ptr
= _rcu_dereference(test_rcu_pointer
);
183 assert(local_ptr
->a
== 8);
184 if (unlikely(rduration
))
185 loop_sleep(rduration
);
188 /* QS each 1024 reads */
189 if (unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
190 _rcu_quiescent_state();
191 if (unlikely(!test_duration_read()))
195 rcu_unregister_thread();
198 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
199 "reader", pthread_self(), (unsigned long)gettid());
204 void *thr_writer(void *_count
)
206 unsigned long long *count
= _count
;
207 struct test_array
*new, *old
;
209 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
210 "writer", pthread_self(), (unsigned long)gettid());
218 new = test_array_alloc();
219 rcu_copy_mutex_lock();
220 old
= test_rcu_pointer
;
224 old
= _rcu_publish_content(&test_rcu_pointer
, new);
225 rcu_copy_mutex_unlock();
226 /* can be done after unlock */
229 test_array_free(old
);
231 if (unlikely(!test_duration_write()))
233 if (unlikely(wdelay
))
237 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
238 "writer", pthread_self(), (unsigned long)gettid());
243 void show_usage(int argc
, char **argv
)
245 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
247 printf(" [-r] [-w] (yield reader and/or writer)");
249 printf(" [-d delay] (writer period (us))");
250 printf(" [-c duration] (reader C.S. duration (in loops))");
251 printf(" [-v] (verbose output)");
252 printf(" [-a cpu#] [-a cpu#]... (affinity)");
258 int main(int argc
, char **argv
)
261 pthread_t
*tid_reader
, *tid_writer
;
263 unsigned long long *count_reader
, *count_writer
;
264 unsigned long long tot_reads
= 0, tot_writes
= 0;
266 int use_affinity
= 0;
269 show_usage(argc
, argv
);
273 err
= sscanf(argv
[1], "%u", &nr_readers
);
275 show_usage(argc
, argv
);
279 err
= sscanf(argv
[2], "%u", &nr_writers
);
281 show_usage(argc
, argv
);
285 err
= sscanf(argv
[3], "%lu", &duration
);
287 show_usage(argc
, argv
);
293 for (i
= 4; i
< argc
; i
++) {
294 if (argv
[i
][0] != '-')
296 switch (argv
[i
][1]) {
299 yield_active
|= YIELD_READ
;
302 yield_active
|= YIELD_WRITE
;
307 show_usage(argc
, argv
);
311 CPU_SET(a
, &affinity
);
313 printf_verbose("Adding CPU %d affinity\n", a
);
317 show_usage(argc
, argv
);
320 rduration
= atoi(argv
[++i
]);
324 show_usage(argc
, argv
);
327 wdelay
= atoi(argv
[++i
]);
335 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
336 duration
, nr_readers
, nr_writers
);
337 printf_verbose("Writer delay : %u us.\n", wdelay
);
338 printf_verbose("Reader duration : %lu loops.\n", rduration
);
339 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
340 "main", pthread_self(), (unsigned long)gettid());
343 && sched_setaffinity(0, sizeof(affinity
), &affinity
) < 0) {
344 perror("sched_setaffinity");
348 test_array
= malloc(sizeof(*test_array
) * ARRAY_SIZE
);
349 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
350 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
351 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
352 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
354 for (i
= 0; i
< nr_readers
; i
++) {
355 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
360 for (i
= 0; i
< nr_writers
; i
++) {
361 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
375 for (i
= 0; i
< nr_readers
; i
++) {
376 err
= pthread_join(tid_reader
[i
], &tret
);
379 tot_reads
+= count_reader
[i
];
381 for (i
= 0; i
< nr_writers
; i
++) {
382 err
= pthread_join(tid_writer
[i
], &tret
);
385 tot_writes
+= count_writer
[i
];
388 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
390 printf("SUMMARY %-15s testdur %4lu nr_readers %3u rdur %6lu "
392 "wdelay %4u nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
393 argv
[0], duration
, nr_readers
, rduration
,
394 nr_writers
, wdelay
, tot_reads
, tot_writes
,
395 tot_reads
+ tot_writes
);
396 test_array_free(test_rcu_pointer
);