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>
36 #if defined(_syscall0)
37 _syscall0(pid_t
, gettid
)
38 #elif defined(__NR_gettid)
39 static inline pid_t
gettid(void)
41 return syscall(__NR_gettid
);
44 #warning "use pid as tid"
45 static inline pid_t
gettid(void)
51 #ifndef DYNAMIC_LINK_TEST
54 #define debug_yield_read()
62 static volatile int test_go
;
66 static struct test_array
*test_rcu_pointer
;
68 static unsigned long duration
;
69 static time_t start_time
;
70 static unsigned long __thread duration_interval
;
71 #define DURATION_TEST_DELAY_WRITE 4
72 #define DURATION_TEST_DELAY_READ 100
75 * returns 0 if test should end.
77 static int test_duration_write(void)
79 if (duration_interval
++ >= DURATION_TEST_DELAY_WRITE
) {
80 duration_interval
= 0;
81 if (time(NULL
) - start_time
>= duration
)
87 static int test_duration_read(void)
89 if (duration_interval
++ >= DURATION_TEST_DELAY_READ
) {
90 duration_interval
= 0;
91 if (time(NULL
) - start_time
>= duration
)
97 static unsigned long long __thread nr_writes
;
98 static unsigned long long __thread nr_reads
;
100 static unsigned int nr_readers
;
101 static unsigned int nr_writers
;
103 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
105 void rcu_copy_mutex_lock(void)
108 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
110 perror("Error in pthread mutex lock");
115 void rcu_copy_mutex_unlock(void)
119 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
121 perror("Error in pthread mutex unlock");
127 * malloc/free are reusing memory areas too quickly, which does not let us
128 * test races appropriately. Use a large circular array for allocations.
129 * ARRAY_SIZE is larger than nr_writers, which insures we never run over our tail.
131 #define ARRAY_SIZE (1048576 * nr_writers)
132 #define ARRAY_POISON 0xDEADBEEF
133 static int array_index
;
134 static struct test_array
*test_array
;
136 static struct test_array
*test_array_alloc(void)
138 struct test_array
*ret
;
141 rcu_copy_mutex_lock();
142 index
= array_index
% ARRAY_SIZE
;
143 assert(test_array
[index
].a
== ARRAY_POISON
||
144 test_array
[index
].a
== 0);
145 ret
= &test_array
[index
];
147 if (array_index
== ARRAY_SIZE
)
149 rcu_copy_mutex_unlock();
153 static void test_array_free(struct test_array
*ptr
)
157 rcu_copy_mutex_lock();
158 ptr
->a
= ARRAY_POISON
;
159 rcu_copy_mutex_unlock();
162 void *thr_reader(void *_count
)
164 unsigned long long *count
= _count
;
165 struct test_array
*local_ptr
;
167 printf("thread_begin %s, thread id : %lx, tid %lu\n",
168 "reader", pthread_self(), (unsigned long)gettid());
170 rcu_register_thread();
179 local_ptr
= rcu_dereference(test_rcu_pointer
);
182 assert(local_ptr
->a
== 8);
185 if (!test_duration_read())
189 rcu_unregister_thread();
192 printf("thread_end %s, thread id : %lx, tid %lu\n",
193 "reader", pthread_self(), (unsigned long)gettid());
198 void *thr_writer(void *_count
)
200 unsigned long long *count
= _count
;
201 struct test_array
*new, *old
;
203 printf("thread_begin %s, thread id : %lx, tid %lu\n",
204 "writer", pthread_self(), (unsigned long)gettid());
212 new = test_array_alloc();
213 rcu_copy_mutex_lock();
214 old
= test_rcu_pointer
;
218 old
= rcu_publish_content(&test_rcu_pointer
, new);
219 rcu_copy_mutex_unlock();
220 /* can be done after unlock */
223 test_array_free(old
);
225 if (!test_duration_write())
231 printf("thread_end %s, thread id : %lx, tid %lu\n",
232 "writer", pthread_self(), (unsigned long)gettid());
237 void show_usage(int argc
, char **argv
)
239 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
241 printf(" [-r] [-w] (yield reader and/or writer)");
243 printf(" [-d delay] (writer period (us))");
247 int main(int argc
, char **argv
)
250 pthread_t
*tid_reader
, *tid_writer
;
252 unsigned long long *count_reader
, *count_writer
;
253 unsigned long long tot_reads
= 0, tot_writes
= 0;
257 show_usage(argc
, argv
);
261 err
= sscanf(argv
[1], "%u", &nr_readers
);
263 show_usage(argc
, argv
);
267 err
= sscanf(argv
[2], "%u", &nr_writers
);
269 show_usage(argc
, argv
);
273 err
= sscanf(argv
[3], "%lu", &duration
);
275 show_usage(argc
, argv
);
279 for (i
= 4; i
< argc
; i
++) {
280 if (argv
[i
][0] != '-')
282 switch (argv
[i
][1]) {
285 yield_active
|= YIELD_READ
;
288 yield_active
|= YIELD_WRITE
;
293 show_usage(argc
, argv
);
296 wdelay
= atoi(argv
[++i
]);
301 printf("running test for %lu seconds, %u readers, %u writers.\n",
302 duration
, nr_readers
, nr_writers
);
303 printf("Writer delay : %u us.\n", wdelay
);
304 printf("thread %-6s, thread id : %lx, tid %lu\n",
305 "main", pthread_self(), (unsigned long)gettid());
307 test_array
= malloc(sizeof(*test_array
) * ARRAY_SIZE
);
308 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
309 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
310 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
311 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
313 for (i
= 0; i
< nr_readers
; i
++) {
314 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
319 for (i
= 0; i
< nr_writers
; i
++) {
320 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
326 start_time
= time(NULL
);
330 for (i
= 0; i
< nr_readers
; i
++) {
331 err
= pthread_join(tid_reader
[i
], &tret
);
334 tot_reads
+= count_reader
[i
];
336 for (i
= 0; i
< nr_writers
; i
++) {
337 err
= pthread_join(tid_writer
[i
], &tret
);
340 tot_writes
+= count_writer
[i
];
343 printf("total number of reads : %llu, writes %llu\n", tot_reads
,
345 test_array_free(test_rcu_pointer
);