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