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
;
69 * returns 0 if test should end.
71 static int test_duration_write(void)
76 static int test_duration_read(void)
81 static unsigned long long __thread nr_writes
;
82 static unsigned long long __thread nr_reads
;
84 static unsigned int nr_readers
;
85 static unsigned int nr_writers
;
87 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
89 void rcu_copy_mutex_lock(void)
92 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
94 perror("Error in pthread mutex lock");
99 void rcu_copy_mutex_unlock(void)
103 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
105 perror("Error in pthread mutex unlock");
111 * malloc/free are reusing memory areas too quickly, which does not let us
112 * test races appropriately. Use a large circular array for allocations.
113 * ARRAY_SIZE is larger than nr_writers, which insures we never run over our tail.
115 #define ARRAY_SIZE (1048576 * nr_writers)
116 #define ARRAY_POISON 0xDEADBEEF
117 static int array_index
;
118 static struct test_array
*test_array
;
120 static struct test_array
*test_array_alloc(void)
122 struct test_array
*ret
;
125 rcu_copy_mutex_lock();
126 index
= array_index
% ARRAY_SIZE
;
127 assert(test_array
[index
].a
== ARRAY_POISON
||
128 test_array
[index
].a
== 0);
129 ret
= &test_array
[index
];
131 if (array_index
== ARRAY_SIZE
)
133 rcu_copy_mutex_unlock();
137 static void test_array_free(struct test_array
*ptr
)
141 rcu_copy_mutex_lock();
142 ptr
->a
= ARRAY_POISON
;
143 rcu_copy_mutex_unlock();
146 void *thr_reader(void *_count
)
148 unsigned long long *count
= _count
;
149 struct test_array
*local_ptr
;
151 printf("thread_begin %s, thread id : %lx, tid %lu\n",
152 "reader", pthread_self(), (unsigned long)gettid());
154 rcu_register_thread();
163 local_ptr
= _rcu_dereference(test_rcu_pointer
);
166 assert(local_ptr
->a
== 8);
169 /* QS each 1024 reads */
170 if (unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
171 _rcu_quiescent_state();
172 if (unlikely(!test_duration_read()))
176 rcu_unregister_thread();
179 printf("thread_end %s, thread id : %lx, tid %lu\n",
180 "reader", pthread_self(), (unsigned long)gettid());
185 void *thr_writer(void *_count
)
187 unsigned long long *count
= _count
;
188 struct test_array
*new, *old
;
190 printf("thread_begin %s, thread id : %lx, tid %lu\n",
191 "writer", pthread_self(), (unsigned long)gettid());
199 new = test_array_alloc();
200 rcu_copy_mutex_lock();
201 old
= test_rcu_pointer
;
205 old
= _rcu_publish_content(&test_rcu_pointer
, new);
206 rcu_copy_mutex_unlock();
207 /* can be done after unlock */
210 test_array_free(old
);
212 if (unlikely(!test_duration_write()))
214 if (unlikely(wdelay
))
218 printf("thread_end %s, thread id : %lx, tid %lu\n",
219 "writer", pthread_self(), (unsigned long)gettid());
224 void show_usage(int argc
, char **argv
)
226 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
228 printf(" [-r] [-w] (yield reader and/or writer)");
230 printf(" [-d delay] (writer period (us))");
231 printf(" [-a cpu#] [-a cpu#]... (affinity)");
237 int main(int argc
, char **argv
)
240 pthread_t
*tid_reader
, *tid_writer
;
242 unsigned long long *count_reader
, *count_writer
;
243 unsigned long long tot_reads
= 0, tot_writes
= 0;
245 int use_affinity
= 0;
248 show_usage(argc
, argv
);
252 err
= sscanf(argv
[1], "%u", &nr_readers
);
254 show_usage(argc
, argv
);
258 err
= sscanf(argv
[2], "%u", &nr_writers
);
260 show_usage(argc
, argv
);
264 err
= sscanf(argv
[3], "%lu", &duration
);
266 show_usage(argc
, argv
);
272 for (i
= 4; i
< argc
; i
++) {
273 if (argv
[i
][0] != '-')
275 switch (argv
[i
][1]) {
278 yield_active
|= YIELD_READ
;
281 yield_active
|= YIELD_WRITE
;
286 show_usage(argc
, argv
);
290 CPU_SET(a
, &affinity
);
292 printf("Adding CPU %d affinity\n", a
);
296 show_usage(argc
, argv
);
299 wdelay
= atoi(argv
[++i
]);
304 printf("running test for %lu seconds, %u readers, %u writers.\n",
305 duration
, nr_readers
, nr_writers
);
306 printf("Writer delay : %u us.\n", wdelay
);
307 printf("thread %-6s, thread id : %lx, tid %lu\n",
308 "main", pthread_self(), (unsigned long)gettid());
311 && sched_setaffinity(0, sizeof(affinity
), &affinity
) < 0) {
312 perror("sched_setaffinity");
316 test_array
= malloc(sizeof(*test_array
) * ARRAY_SIZE
);
317 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
318 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
319 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
320 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
322 for (i
= 0; i
< nr_readers
; i
++) {
323 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
328 for (i
= 0; i
< nr_writers
; i
++) {
329 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
343 for (i
= 0; i
< nr_readers
; i
++) {
344 err
= pthread_join(tid_reader
[i
], &tret
);
347 tot_reads
+= count_reader
[i
];
349 for (i
= 0; i
< nr_writers
; i
++) {
350 err
= pthread_join(tid_writer
[i
], &tret
);
353 tot_writes
+= count_writer
[i
];
356 printf("total number of reads : %llu, writes %llu\n", tot_reads
,
358 test_array_free(test_rcu_pointer
);