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