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.
27 #include <sys/types.h>
32 #include <sys/syscall.h>
34 #if defined(_syscall0)
35 _syscall0(pid_t
, gettid
)
36 #elif defined(__NR_gettid)
37 static inline pid_t
gettid(void)
39 return syscall(__NR_gettid
);
42 #warning "use pid as tid"
43 static inline pid_t
gettid(void)
49 #ifndef DYNAMIC_LINK_TEST
52 #define debug_yield_read()
60 static volatile int test_go
;
64 static struct test_array
*test_rcu_pointer
;
66 static unsigned long duration
;
67 static time_t start_time
;
68 static unsigned long __thread duration_interval
;
69 #define DURATION_TEST_DELAY_WRITE 4
70 #define DURATION_TEST_DELAY_READ 100
73 * returns 0 if test should end.
75 static int test_duration_write(void)
77 if (duration_interval
++ >= DURATION_TEST_DELAY_WRITE
) {
78 duration_interval
= 0;
79 if (time(NULL
) - start_time
>= duration
)
85 static int test_duration_read(void)
87 if (duration_interval
++ >= DURATION_TEST_DELAY_READ
) {
88 duration_interval
= 0;
89 if (time(NULL
) - start_time
>= duration
)
95 static unsigned long long __thread nr_writes
;
96 static unsigned long long __thread nr_reads
;
98 static unsigned int nr_readers
;
99 static unsigned int nr_writers
;
101 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
103 void rcu_copy_mutex_lock(void)
106 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
108 perror("Error in pthread mutex lock");
113 void rcu_copy_mutex_unlock(void)
117 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
119 perror("Error in pthread mutex unlock");
125 * malloc/free are reusing memory areas too quickly, which does not let us
126 * test races appropriately. Use a large circular array for allocations.
127 * ARRAY_SIZE is larger than nr_writers, which insures we never run over our tail.
129 #define ARRAY_SIZE (1048576 * nr_writers)
130 #define ARRAY_POISON 0xDEADBEEF
131 static int array_index
;
132 static struct test_array
*test_array
;
134 static struct test_array
*test_array_alloc(void)
136 struct test_array
*ret
;
139 rcu_copy_mutex_lock();
140 index
= array_index
% ARRAY_SIZE
;
141 assert(test_array
[index
].a
== ARRAY_POISON
||
142 test_array
[index
].a
== 0);
143 ret
= &test_array
[index
];
145 if (array_index
== ARRAY_SIZE
)
147 rcu_copy_mutex_unlock();
151 static void test_array_free(struct test_array
*ptr
)
155 rcu_copy_mutex_lock();
156 ptr
->a
= ARRAY_POISON
;
157 rcu_copy_mutex_unlock();
160 void *thr_reader(void *_count
)
162 unsigned long long *count
= _count
;
163 struct test_array
*local_ptr
;
165 printf("thread_begin %s, thread id : %lx, tid %lu\n",
166 "reader", pthread_self(), (unsigned long)gettid());
168 rcu_register_thread();
176 local_ptr
= rcu_dereference(test_rcu_pointer
);
179 assert(local_ptr
->a
== 8);
182 if (!test_duration_read())
186 rcu_unregister_thread();
189 printf("thread_end %s, thread id : %lx, tid %lu\n",
190 "reader", pthread_self(), (unsigned long)gettid());
195 void *thr_writer(void *_count
)
197 unsigned long long *count
= _count
;
198 struct test_array
*new, *old
;
200 printf("thread_begin %s, thread id : %lx, tid %lu\n",
201 "writer", pthread_self(), (unsigned long)gettid());
208 new = test_array_alloc();
209 rcu_copy_mutex_lock();
210 old
= test_rcu_pointer
;
214 old
= rcu_publish_content(&test_rcu_pointer
, new);
215 rcu_copy_mutex_unlock();
216 /* can be done after unlock */
219 test_array_free(old
);
221 if (!test_duration_write())
227 printf("thread_end %s, thread id : %lx, tid %lu\n",
228 "writer", pthread_self(), (unsigned long)gettid());
233 void show_usage(int argc
, char **argv
)
235 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
237 printf(" [-r] [-w] (yield reader and/or writer)");
239 printf(" [-d delay] (writer period)");
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;
253 show_usage(argc
, argv
);
257 err
= sscanf(argv
[1], "%u", &nr_readers
);
259 show_usage(argc
, argv
);
263 err
= sscanf(argv
[2], "%u", &nr_writers
);
265 show_usage(argc
, argv
);
269 err
= sscanf(argv
[3], "%lu", &duration
);
271 show_usage(argc
, argv
);
275 for (i
= 4; i
< argc
; i
++) {
276 if (argv
[i
][0] != '-')
278 switch (argv
[i
][1]) {
281 yield_active
|= YIELD_READ
;
284 yield_active
|= YIELD_WRITE
;
289 show_usage(argc
, argv
);
292 wdelay
= atoi(argv
[++i
]);
297 printf("running test for %lu seconds, %u readers, %u writers.\n",
298 duration
, nr_readers
, nr_writers
);
299 printf("Writer delay : %u us.\n", wdelay
);
300 start_time
= time(NULL
);
301 printf("thread %-6s, thread id : %lx, tid %lu\n",
302 "main", pthread_self(), (unsigned long)gettid());
304 test_array
= malloc(sizeof(*test_array
) * ARRAY_SIZE
);
305 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
306 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
307 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
308 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
310 for (i
= 0; i
< nr_readers
; i
++) {
311 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
316 for (i
= 0; i
< nr_writers
; i
++) {
317 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
325 for (i
= 0; i
< nr_readers
; i
++) {
326 err
= pthread_join(tid_reader
[i
], &tret
);
329 tot_reads
+= count_reader
[i
];
331 for (i
= 0; i
< nr_writers
; i
++) {
332 err
= pthread_join(tid_writer
[i
], &tret
);
335 tot_writes
+= count_writer
[i
];
338 printf("total number of reads : %llu, writes %llu\n", tot_reads
,
340 test_array_free(test_rcu_pointer
);