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