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 us */
69 static unsigned long rduration
;
72 * returns 0 if test should end.
74 static int test_duration_write(void)
79 static int test_duration_read(void)
84 static unsigned long long __thread nr_writes
;
85 static unsigned long long __thread nr_reads
;
87 static unsigned int nr_readers
;
88 static unsigned int nr_writers
;
90 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
92 void rcu_copy_mutex_lock(void)
95 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
97 perror("Error in pthread mutex lock");
102 void rcu_copy_mutex_unlock(void)
106 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
108 perror("Error in pthread mutex unlock");
114 * malloc/free are reusing memory areas too quickly, which does not let us
115 * test races appropriately. Use a large circular array for allocations.
116 * ARRAY_SIZE is larger than nr_writers, which insures we never run over our tail.
118 #define ARRAY_SIZE (1048576 * nr_writers)
119 #define ARRAY_POISON 0xDEADBEEF
120 static int array_index
;
121 static struct test_array
*test_array
;
123 static struct test_array
*test_array_alloc(void)
125 struct test_array
*ret
;
128 rcu_copy_mutex_lock();
129 index
= array_index
% ARRAY_SIZE
;
130 assert(test_array
[index
].a
== ARRAY_POISON
||
131 test_array
[index
].a
== 0);
132 ret
= &test_array
[index
];
134 if (array_index
== ARRAY_SIZE
)
136 rcu_copy_mutex_unlock();
140 static void test_array_free(struct test_array
*ptr
)
144 rcu_copy_mutex_lock();
145 ptr
->a
= ARRAY_POISON
;
146 rcu_copy_mutex_unlock();
149 void *thr_reader(void *_count
)
151 unsigned long long *count
= _count
;
152 struct test_array
*local_ptr
;
154 printf("thread_begin %s, thread id : %lx, tid %lu\n",
155 "reader", pthread_self(), (unsigned long)gettid());
157 rcu_register_thread();
166 local_ptr
= _rcu_dereference(test_rcu_pointer
);
169 assert(local_ptr
->a
== 8);
170 if (unlikely(rduration
))
174 /* QS each 1024 reads */
175 if (unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
176 _rcu_quiescent_state();
177 if (unlikely(!test_duration_read()))
181 rcu_unregister_thread();
184 printf("thread_end %s, thread id : %lx, tid %lu\n",
185 "reader", pthread_self(), (unsigned long)gettid());
190 void *thr_writer(void *_count
)
192 unsigned long long *count
= _count
;
193 struct test_array
*new, *old
;
195 printf("thread_begin %s, thread id : %lx, tid %lu\n",
196 "writer", pthread_self(), (unsigned long)gettid());
204 new = test_array_alloc();
205 rcu_copy_mutex_lock();
206 old
= test_rcu_pointer
;
210 old
= _rcu_publish_content(&test_rcu_pointer
, new);
211 rcu_copy_mutex_unlock();
212 /* can be done after unlock */
215 test_array_free(old
);
217 if (unlikely(!test_duration_write()))
219 if (unlikely(wdelay
))
223 printf("thread_end %s, thread id : %lx, tid %lu\n",
224 "writer", pthread_self(), (unsigned long)gettid());
229 void show_usage(int argc
, char **argv
)
231 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
233 printf(" [-r] [-w] (yield reader and/or writer)");
235 printf(" [-d delay] (writer period (us))");
236 printf(" [-c duration] (reader C.S. duration (us))");
237 printf(" [-a cpu#] [-a cpu#]... (affinity)");
243 int main(int argc
, char **argv
)
246 pthread_t
*tid_reader
, *tid_writer
;
248 unsigned long long *count_reader
, *count_writer
;
249 unsigned long long tot_reads
= 0, tot_writes
= 0;
251 int use_affinity
= 0;
254 show_usage(argc
, argv
);
258 err
= sscanf(argv
[1], "%u", &nr_readers
);
260 show_usage(argc
, argv
);
264 err
= sscanf(argv
[2], "%u", &nr_writers
);
266 show_usage(argc
, argv
);
270 err
= sscanf(argv
[3], "%lu", &duration
);
272 show_usage(argc
, argv
);
278 for (i
= 4; i
< argc
; i
++) {
279 if (argv
[i
][0] != '-')
281 switch (argv
[i
][1]) {
284 yield_active
|= YIELD_READ
;
287 yield_active
|= YIELD_WRITE
;
292 show_usage(argc
, argv
);
296 CPU_SET(a
, &affinity
);
298 printf("Adding CPU %d affinity\n", a
);
302 show_usage(argc
, argv
);
305 rduration
= atoi(argv
[++i
]);
309 show_usage(argc
, argv
);
312 wdelay
= atoi(argv
[++i
]);
317 printf("running test for %lu seconds, %u readers, %u writers.\n",
318 duration
, nr_readers
, nr_writers
);
319 printf("Writer delay : %u us.\n", wdelay
);
320 printf("thread %-6s, thread id : %lx, tid %lu\n",
321 "main", pthread_self(), (unsigned long)gettid());
324 && sched_setaffinity(0, sizeof(affinity
), &affinity
) < 0) {
325 perror("sched_setaffinity");
329 test_array
= malloc(sizeof(*test_array
) * ARRAY_SIZE
);
330 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
331 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
332 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
333 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
335 for (i
= 0; i
< nr_readers
; i
++) {
336 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
341 for (i
= 0; i
< nr_writers
; i
++) {
342 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
356 for (i
= 0; i
< nr_readers
; i
++) {
357 err
= pthread_join(tid_reader
[i
], &tret
);
360 tot_reads
+= count_reader
[i
];
362 for (i
= 0; i
< nr_writers
; i
++) {
363 err
= pthread_join(tid_writer
[i
], &tret
);
366 tot_writes
+= count_writer
[i
];
369 printf("total number of reads : %llu, writes %llu\n", tot_reads
,
371 test_array_free(test_rcu_pointer
);