Commit | Line | Data |
---|---|---|
b257a10b MD |
1 | /* |
2 | * test_urcu.c | |
3 | * | |
4 | * Userspace RCU library - test program | |
5 | * | |
6 | * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> | |
7 | * | |
af02d47e MD |
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. | |
12 | * | |
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. | |
17 | * | |
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. | |
b257a10b MD |
21 | */ |
22 | ||
ac260fd9 | 23 | #include <stdio.h> |
f69f195a MD |
24 | #include <pthread.h> |
25 | #include <stdlib.h> | |
41718ff9 | 26 | #include <string.h> |
f69f195a MD |
27 | #include <sys/types.h> |
28 | #include <sys/wait.h> | |
29 | #include <unistd.h> | |
30 | #include <stdio.h> | |
41718ff9 | 31 | #include <assert.h> |
87bd15cd BW |
32 | #include <sys/syscall.h> |
33 | ||
34 | #if defined(_syscall0) | |
35 | _syscall0(pid_t, gettid) | |
36 | #elif defined(__NR_gettid) | |
37 | static inline pid_t gettid(void) | |
38 | { | |
39 | return syscall(__NR_gettid); | |
40 | } | |
41 | #else | |
42 | #warning "use pid as tid" | |
43 | static inline pid_t gettid(void) | |
44 | { | |
45 | return getpid(); | |
46 | } | |
47 | #endif | |
48 | ||
121a5d44 MD |
49 | #ifndef DYNAMIC_LINK_TEST |
50 | #define _LGPL_SOURCE | |
51 | #else | |
52 | #define debug_yield_read() | |
53 | #endif | |
ac260fd9 MD |
54 | #include "urcu.h" |
55 | ||
41718ff9 MD |
56 | struct test_array { |
57 | int a; | |
41718ff9 MD |
58 | }; |
59 | ||
8c86b9a1 MD |
60 | static volatile int test_go; |
61 | ||
62 | static int wdelay; | |
bb488185 | 63 | |
41718ff9 MD |
64 | static struct test_array *test_rcu_pointer; |
65 | ||
cf380c2f MD |
66 | static unsigned long duration; |
67 | static time_t start_time; | |
68 | static unsigned long __thread duration_interval; | |
5873cd17 MD |
69 | #define DURATION_TEST_DELAY_WRITE 4 |
70 | #define DURATION_TEST_DELAY_READ 100 | |
cf380c2f MD |
71 | |
72 | /* | |
73 | * returns 0 if test should end. | |
74 | */ | |
5873cd17 | 75 | static int test_duration_write(void) |
cf380c2f | 76 | { |
5873cd17 MD |
77 | if (duration_interval++ >= DURATION_TEST_DELAY_WRITE) { |
78 | duration_interval = 0; | |
79 | if (time(NULL) - start_time >= duration) | |
80 | return 0; | |
81 | } | |
82 | return 1; | |
83 | } | |
84 | ||
85 | static int test_duration_read(void) | |
86 | { | |
87 | if (duration_interval++ >= DURATION_TEST_DELAY_READ) { | |
cf380c2f MD |
88 | duration_interval = 0; |
89 | if (time(NULL) - start_time >= duration) | |
90 | return 0; | |
91 | } | |
92 | return 1; | |
93 | } | |
94 | ||
1eec319e MD |
95 | static unsigned long long __thread nr_writes; |
96 | static unsigned long long __thread nr_reads; | |
97 | ||
8c86b9a1 MD |
98 | static unsigned int nr_readers; |
99 | static unsigned int nr_writers; | |
100 | ||
c265818b MD |
101 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; |
102 | ||
103 | void rcu_copy_mutex_lock(void) | |
104 | { | |
105 | int ret; | |
106 | ret = pthread_mutex_lock(&rcu_copy_mutex); | |
107 | if (ret) { | |
108 | perror("Error in pthread mutex lock"); | |
109 | exit(-1); | |
110 | } | |
111 | } | |
112 | ||
113 | void rcu_copy_mutex_unlock(void) | |
114 | { | |
115 | int ret; | |
116 | ||
117 | ret = pthread_mutex_unlock(&rcu_copy_mutex); | |
118 | if (ret) { | |
119 | perror("Error in pthread mutex unlock"); | |
120 | exit(-1); | |
121 | } | |
122 | } | |
f69f195a | 123 | |
bb488185 MD |
124 | /* |
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. | |
8c86b9a1 | 127 | * ARRAY_SIZE is larger than nr_writers, which insures we never run over our tail. |
bb488185 | 128 | */ |
8c86b9a1 | 129 | #define ARRAY_SIZE (1048576 * nr_writers) |
bb488185 MD |
130 | #define ARRAY_POISON 0xDEADBEEF |
131 | static int array_index; | |
8c86b9a1 | 132 | static struct test_array *test_array; |
bb488185 MD |
133 | |
134 | static struct test_array *test_array_alloc(void) | |
135 | { | |
136 | struct test_array *ret; | |
137 | int index; | |
138 | ||
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]; | |
144 | array_index++; | |
145 | if (array_index == ARRAY_SIZE) | |
146 | array_index = 0; | |
147 | rcu_copy_mutex_unlock(); | |
148 | return ret; | |
149 | } | |
150 | ||
151 | static void test_array_free(struct test_array *ptr) | |
152 | { | |
153 | if (!ptr) | |
154 | return; | |
155 | rcu_copy_mutex_lock(); | |
156 | ptr->a = ARRAY_POISON; | |
157 | rcu_copy_mutex_unlock(); | |
158 | } | |
159 | ||
1eec319e | 160 | void *thr_reader(void *_count) |
f69f195a | 161 | { |
1eec319e | 162 | unsigned long long *count = _count; |
41718ff9 MD |
163 | struct test_array *local_ptr; |
164 | ||
cf380c2f | 165 | printf("thread_begin %s, thread id : %lx, tid %lu\n", |
87bd15cd | 166 | "reader", pthread_self(), (unsigned long)gettid()); |
f69f195a | 167 | |
121a5d44 | 168 | rcu_register_thread(); |
f69f195a | 169 | |
8c86b9a1 MD |
170 | while (!test_go) |
171 | { | |
172 | } | |
173 | ||
cf380c2f | 174 | for (;;) { |
1430ee0b | 175 | rcu_read_lock(); |
cf380c2f | 176 | local_ptr = rcu_dereference(test_rcu_pointer); |
bb488185 | 177 | debug_yield_read(); |
cf380c2f MD |
178 | if (local_ptr) |
179 | assert(local_ptr->a == 8); | |
1430ee0b | 180 | rcu_read_unlock(); |
1eec319e | 181 | nr_reads++; |
5873cd17 | 182 | if (!test_duration_read()) |
cf380c2f | 183 | break; |
41718ff9 | 184 | } |
f69f195a | 185 | |
121a5d44 | 186 | rcu_unregister_thread(); |
41718ff9 | 187 | |
1eec319e | 188 | *count = nr_reads; |
cf380c2f MD |
189 | printf("thread_end %s, thread id : %lx, tid %lu\n", |
190 | "reader", pthread_self(), (unsigned long)gettid()); | |
f69f195a MD |
191 | return ((void*)1); |
192 | ||
193 | } | |
194 | ||
1eec319e | 195 | void *thr_writer(void *_count) |
f69f195a | 196 | { |
1eec319e | 197 | unsigned long long *count = _count; |
41718ff9 | 198 | struct test_array *new, *old; |
f69f195a | 199 | |
cf380c2f | 200 | printf("thread_begin %s, thread id : %lx, tid %lu\n", |
87bd15cd | 201 | "writer", pthread_self(), (unsigned long)gettid()); |
f69f195a | 202 | |
8c86b9a1 MD |
203 | while (!test_go) |
204 | { | |
205 | } | |
206 | ||
cf380c2f | 207 | for (;;) { |
bb488185 | 208 | new = test_array_alloc(); |
c265818b | 209 | rcu_copy_mutex_lock(); |
41718ff9 | 210 | old = test_rcu_pointer; |
cf380c2f | 211 | if (old) |
41718ff9 | 212 | assert(old->a == 8); |
ad6ce6ae | 213 | new->a = 8; |
121a5d44 | 214 | old = rcu_publish_content(&test_rcu_pointer, new); |
c265818b | 215 | rcu_copy_mutex_unlock(); |
41718ff9 | 216 | /* can be done after unlock */ |
cf380c2f | 217 | if (old) |
8c8eed97 | 218 | old->a = 0; |
bb488185 | 219 | test_array_free(old); |
1eec319e | 220 | nr_writes++; |
5873cd17 | 221 | if (!test_duration_write()) |
cf380c2f | 222 | break; |
8c86b9a1 MD |
223 | if (wdelay) |
224 | usleep(wdelay); | |
f69f195a MD |
225 | } |
226 | ||
cf380c2f MD |
227 | printf("thread_end %s, thread id : %lx, tid %lu\n", |
228 | "writer", pthread_self(), (unsigned long)gettid()); | |
1eec319e | 229 | *count = nr_writes; |
f69f195a MD |
230 | return ((void*)2); |
231 | } | |
ac260fd9 | 232 | |
1430ee0b MD |
233 | void show_usage(int argc, char **argv) |
234 | { | |
8c86b9a1 | 235 | printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]); |
1430ee0b MD |
236 | #ifdef DEBUG_YIELD |
237 | printf(" [-r] [-w] (yield reader and/or writer)"); | |
238 | #endif | |
8c86b9a1 | 239 | printf(" [-d delay] (writer period)"); |
1430ee0b MD |
240 | printf("\n"); |
241 | } | |
242 | ||
cf380c2f | 243 | int main(int argc, char **argv) |
ac260fd9 | 244 | { |
f69f195a | 245 | int err; |
8c86b9a1 | 246 | pthread_t *tid_reader, *tid_writer; |
f69f195a | 247 | void *tret; |
8c86b9a1 | 248 | unsigned long long *count_reader, *count_writer; |
1eec319e | 249 | unsigned long long tot_reads = 0, tot_writes = 0; |
f69f195a MD |
250 | int i; |
251 | ||
8c86b9a1 MD |
252 | if (argc < 4) { |
253 | show_usage(argc, argv); | |
254 | return -1; | |
255 | } | |
256 | ||
257 | err = sscanf(argv[1], "%u", &nr_readers); | |
258 | if (err != 1) { | |
1430ee0b | 259 | show_usage(argc, argv); |
cf380c2f MD |
260 | return -1; |
261 | } | |
262 | ||
8c86b9a1 MD |
263 | err = sscanf(argv[2], "%u", &nr_writers); |
264 | if (err != 1) { | |
265 | show_usage(argc, argv); | |
266 | return -1; | |
267 | } | |
268 | ||
269 | err = sscanf(argv[3], "%lu", &duration); | |
cf380c2f | 270 | if (err != 1) { |
1430ee0b | 271 | show_usage(argc, argv); |
cf380c2f MD |
272 | return -1; |
273 | } | |
274 | ||
8c86b9a1 | 275 | for (i = 4; i < argc; i++) { |
cf380c2f MD |
276 | if (argv[i][0] != '-') |
277 | continue; | |
278 | switch (argv[i][1]) { | |
bb488185 | 279 | #ifdef DEBUG_YIELD |
cf380c2f MD |
280 | case 'r': |
281 | yield_active |= YIELD_READ; | |
282 | break; | |
283 | case 'w': | |
284 | yield_active |= YIELD_WRITE; | |
285 | break; | |
bb488185 | 286 | #endif |
8c86b9a1 MD |
287 | case 'd': |
288 | if (argc < i + 1) { | |
289 | show_usage(argc, argv); | |
290 | return -1; | |
291 | } | |
292 | wdelay = atoi(argv[++i]); | |
bb488185 | 293 | break; |
cf380c2f MD |
294 | } |
295 | } | |
cf380c2f | 296 | |
8c86b9a1 MD |
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); | |
cf380c2f | 300 | start_time = time(NULL); |
87bd15cd BW |
301 | printf("thread %-6s, thread id : %lx, tid %lu\n", |
302 | "main", pthread_self(), (unsigned long)gettid()); | |
303 | ||
8c86b9a1 MD |
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); | |
309 | ||
310 | for (i = 0; i < nr_readers; i++) { | |
1eec319e MD |
311 | err = pthread_create(&tid_reader[i], NULL, thr_reader, |
312 | &count_reader[i]); | |
f69f195a MD |
313 | if (err != 0) |
314 | exit(1); | |
315 | } | |
8c86b9a1 | 316 | for (i = 0; i < nr_writers; i++) { |
1eec319e MD |
317 | err = pthread_create(&tid_writer[i], NULL, thr_writer, |
318 | &count_writer[i]); | |
f69f195a MD |
319 | if (err != 0) |
320 | exit(1); | |
321 | } | |
322 | ||
8c86b9a1 MD |
323 | test_go = 1; |
324 | ||
325 | for (i = 0; i < nr_readers; i++) { | |
f69f195a MD |
326 | err = pthread_join(tid_reader[i], &tret); |
327 | if (err != 0) | |
328 | exit(1); | |
1eec319e | 329 | tot_reads += count_reader[i]; |
f69f195a | 330 | } |
8c86b9a1 | 331 | for (i = 0; i < nr_writers; i++) { |
f69f195a MD |
332 | err = pthread_join(tid_writer[i], &tret); |
333 | if (err != 0) | |
334 | exit(1); | |
1eec319e | 335 | tot_writes += count_writer[i]; |
f69f195a | 336 | } |
5873cd17 | 337 | |
1eec319e MD |
338 | printf("total number of reads : %llu, writes %llu\n", tot_reads, |
339 | tot_writes); | |
bb488185 | 340 | test_array_free(test_rcu_pointer); |
8c86b9a1 MD |
341 | free(test_array); |
342 | free(tid_reader); | |
343 | free(tid_writer); | |
344 | free(count_reader); | |
345 | free(count_writer); | |
f69f195a | 346 | return 0; |
ac260fd9 | 347 | } |