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)
53 #ifndef DYNAMIC_LINK_TEST
56 #define debug_yield_read()
64 static volatile int test_go
, test_stop
;
68 static struct test_array
*test_rcu_pointer
;
70 static unsigned long duration
;
72 /* read-side C.S. duration, in loops */
73 static unsigned long rduration
;
75 static inline void loop_sleep(unsigned long l
)
82 * returns 0 if test should end.
84 static int test_duration_write(void)
89 static int test_duration_read(void)
94 static unsigned long long __thread nr_writes
;
95 static unsigned long long __thread nr_reads
;
97 static unsigned int nr_readers
;
98 static unsigned int nr_writers
;
100 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
102 void rcu_copy_mutex_lock(void)
105 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
107 perror("Error in pthread mutex lock");
112 void rcu_copy_mutex_unlock(void)
116 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
118 perror("Error in pthread mutex unlock");
124 * malloc/free are reusing memory areas too quickly, which does not let us
125 * test races appropriately. Use a large circular array for allocations.
126 * ARRAY_SIZE is larger than nr_writers, which insures we never run over our tail.
128 #define ARRAY_SIZE (1048576 * nr_writers)
129 #define ARRAY_POISON 0xDEADBEEF
130 static int array_index
;
131 static struct test_array
*test_array
;
133 static struct test_array
*test_array_alloc(void)
135 struct test_array
*ret
;
138 rcu_copy_mutex_lock();
139 index
= array_index
% ARRAY_SIZE
;
140 assert(test_array
[index
].a
== ARRAY_POISON
||
141 test_array
[index
].a
== 0);
142 ret
= &test_array
[index
];
144 if (array_index
== ARRAY_SIZE
)
146 rcu_copy_mutex_unlock();
150 static void test_array_free(struct test_array
*ptr
)
154 rcu_copy_mutex_lock();
155 ptr
->a
= ARRAY_POISON
;
156 rcu_copy_mutex_unlock();
159 void *thr_reader(void *_count
)
161 unsigned long long *count
= _count
;
162 struct test_array
*local_ptr
;
164 printf("thread_begin %s, thread id : %lx, tid %lu\n",
165 "reader", pthread_self(), (unsigned long)gettid());
167 rcu_register_thread();
176 local_ptr
= rcu_dereference(test_rcu_pointer
);
179 assert(local_ptr
->a
== 8);
180 if (unlikely(rduration
))
181 loop_sleep(rduration
);
184 if (unlikely(!test_duration_read()))
188 rcu_unregister_thread();
191 printf("thread_end %s, thread id : %lx, tid %lu\n",
192 "reader", pthread_self(), (unsigned long)gettid());
197 void *thr_writer(void *_count
)
199 unsigned long long *count
= _count
;
200 struct test_array
*new, *old
;
202 printf("thread_begin %s, thread id : %lx, tid %lu\n",
203 "writer", pthread_self(), (unsigned long)gettid());
211 new = test_array_alloc();
212 rcu_copy_mutex_lock();
213 old
= test_rcu_pointer
;
217 old
= rcu_publish_content(&test_rcu_pointer
, new);
218 rcu_copy_mutex_unlock();
219 /* can be done after unlock */
222 test_array_free(old
);
224 if (unlikely(!test_duration_write()))
226 if (unlikely(wdelay
))
230 printf("thread_end %s, thread id : %lx, tid %lu\n",
231 "writer", pthread_self(), (unsigned long)gettid());
236 void show_usage(int argc
, char **argv
)
238 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
240 printf(" [-r] [-w] (yield reader and/or writer)");
242 printf(" [-d delay] (writer period (us))");
243 printf(" [-c duration] (reader C.S. duration (in loops))");
244 printf(" [-a cpu#] [-a cpu#]... (affinity)");
250 int main(int argc
, char **argv
)
253 pthread_t
*tid_reader
, *tid_writer
;
255 unsigned long long *count_reader
, *count_writer
;
256 unsigned long long tot_reads
= 0, tot_writes
= 0;
258 int use_affinity
= 0;
261 show_usage(argc
, argv
);
265 err
= sscanf(argv
[1], "%u", &nr_readers
);
267 show_usage(argc
, argv
);
271 err
= sscanf(argv
[2], "%u", &nr_writers
);
273 show_usage(argc
, argv
);
277 err
= sscanf(argv
[3], "%lu", &duration
);
279 show_usage(argc
, argv
);
285 for (i
= 4; i
< argc
; i
++) {
286 if (argv
[i
][0] != '-')
288 switch (argv
[i
][1]) {
291 yield_active
|= YIELD_READ
;
294 yield_active
|= YIELD_WRITE
;
299 show_usage(argc
, argv
);
303 CPU_SET(a
, &affinity
);
305 printf("Adding CPU %d affinity\n", a
);
309 show_usage(argc
, argv
);
312 rduration
= atoi(argv
[++i
]);
316 show_usage(argc
, argv
);
319 wdelay
= atoi(argv
[++i
]);
324 printf("running test for %lu seconds, %u readers, %u writers.\n",
325 duration
, nr_readers
, nr_writers
);
326 printf("Writer delay : %u us.\n", wdelay
);
327 printf("thread %-6s, thread id : %lx, tid %lu\n",
328 "main", pthread_self(), (unsigned long)gettid());
331 && sched_setaffinity(0, sizeof(affinity
), &affinity
) < 0) {
332 perror("sched_setaffinity");
336 test_array
= malloc(sizeof(*test_array
) * ARRAY_SIZE
);
337 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
338 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
339 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
340 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
342 for (i
= 0; i
< nr_readers
; i
++) {
343 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
348 for (i
= 0; i
< nr_writers
; i
++) {
349 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
363 for (i
= 0; i
< nr_readers
; i
++) {
364 err
= pthread_join(tid_reader
[i
], &tret
);
367 tot_reads
+= count_reader
[i
];
369 for (i
= 0; i
< nr_writers
; i
++) {
370 err
= pthread_join(tid_writer
[i
], &tret
);
373 tot_writes
+= count_writer
[i
];
376 printf("total number of reads : %llu, writes %llu\n", tot_reads
,
378 test_array_free(test_rcu_pointer
);