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