Commit | Line | Data |
---|---|---|
ce29b371 MJ |
1 | // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2 | // | |
3 | // SPDX-License-Identifier: GPL-2.0-or-later | |
4 | ||
10c48e14 | 5 | /* |
10c48e14 | 6 | * Userspace RCU library - test program |
10c48e14 MD |
7 | */ |
8 | ||
9 | #include <stdio.h> | |
10 | #include <pthread.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
13 | #include <sys/types.h> | |
14 | #include <sys/wait.h> | |
15 | #include <unistd.h> | |
16 | #include <stdio.h> | |
94b343fd | 17 | #include <errno.h> |
10c48e14 | 18 | |
ec4e58a3 | 19 | #include <urcu/arch.h> |
01477510 | 20 | #include <urcu/assert.h> |
bd252a04 | 21 | #include <urcu/tls-compat.h> |
94df6318 | 22 | #include "thread-id.h" |
65fcc7e9 | 23 | |
2a7ac59d MD |
24 | /* hardcoded number of CPUs */ |
25 | #define NR_CPUS 16384 | |
26 | ||
10c48e14 MD |
27 | #ifndef DYNAMIC_LINK_TEST |
28 | #define _LGPL_SOURCE | |
10c48e14 | 29 | #endif |
ec4e58a3 | 30 | #include <urcu.h> |
10c48e14 MD |
31 | |
32 | struct test_array { | |
33 | int a; | |
34 | }; | |
35 | ||
89a6a9ce MJ |
36 | /* |
37 | * static rwlock initializer is broken on Cygwin. Use runtime | |
38 | * initialization. | |
39 | */ | |
40 | pthread_rwlock_t lock; | |
10c48e14 | 41 | |
78efb485 | 42 | static volatile int test_go, test_stop; |
10c48e14 | 43 | |
9e31d0f0 | 44 | static unsigned long wdelay; |
10c48e14 | 45 | |
2b4e4125 | 46 | static volatile struct test_array test_array = { 8 }; |
10c48e14 MD |
47 | |
48 | static unsigned long duration; | |
10c48e14 | 49 | |
daddf5b0 | 50 | /* read-side C.S. duration, in loops */ |
8b632bab MD |
51 | static unsigned long rduration; |
52 | ||
31b598e0 MD |
53 | /* write-side C.S. duration, in loops */ |
54 | static unsigned long wduration; | |
55 | ||
ab0aacbe | 56 | static inline void loop_sleep(unsigned long loops) |
daddf5b0 | 57 | { |
ab0aacbe | 58 | while (loops-- != 0) |
06f22bdb | 59 | caa_cpu_relax(); |
daddf5b0 MD |
60 | } |
61 | ||
4bad7d45 MD |
62 | static int verbose_mode; |
63 | ||
64 | #define printf_verbose(fmt, args...) \ | |
65 | do { \ | |
66 | if (verbose_mode) \ | |
67 | printf(fmt, args); \ | |
68 | } while (0) | |
69 | ||
2a7ac59d MD |
70 | static unsigned int cpu_affinities[NR_CPUS]; |
71 | static unsigned int next_aff = 0; | |
72 | static int use_affinity = 0; | |
73 | ||
6af882ba MD |
74 | pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; |
75 | ||
2a7ac59d MD |
76 | static void set_affinity(void) |
77 | { | |
2388c075 | 78 | #ifdef HAVE_SCHED_SETAFFINITY |
2a7ac59d | 79 | cpu_set_t mask; |
95bc7fb9 MD |
80 | int cpu, ret; |
81 | #endif /* HAVE_SCHED_SETAFFINITY */ | |
2a7ac59d MD |
82 | |
83 | if (!use_affinity) | |
84 | return; | |
85 | ||
2388c075 | 86 | #ifdef HAVE_SCHED_SETAFFINITY |
6af882ba MD |
87 | ret = pthread_mutex_lock(&affinity_mutex); |
88 | if (ret) { | |
89 | perror("Error in pthread mutex lock"); | |
90 | exit(-1); | |
91 | } | |
2a7ac59d | 92 | cpu = cpu_affinities[next_aff++]; |
6af882ba MD |
93 | ret = pthread_mutex_unlock(&affinity_mutex); |
94 | if (ret) { | |
95 | perror("Error in pthread mutex unlock"); | |
96 | exit(-1); | |
97 | } | |
d8540fc5 | 98 | |
2a7ac59d MD |
99 | CPU_ZERO(&mask); |
100 | CPU_SET(cpu, &mask); | |
101 | sched_setaffinity(0, sizeof(mask), &mask); | |
d8540fc5 | 102 | #endif /* HAVE_SCHED_SETAFFINITY */ |
2a7ac59d MD |
103 | } |
104 | ||
10c48e14 MD |
105 | /* |
106 | * returns 0 if test should end. | |
107 | */ | |
108 | static int test_duration_write(void) | |
109 | { | |
78efb485 | 110 | return !test_stop; |
10c48e14 MD |
111 | } |
112 | ||
113 | static int test_duration_read(void) | |
114 | { | |
78efb485 | 115 | return !test_stop; |
10c48e14 MD |
116 | } |
117 | ||
bd252a04 MD |
118 | static DEFINE_URCU_TLS(unsigned long long, nr_writes); |
119 | static DEFINE_URCU_TLS(unsigned long long, nr_reads); | |
10c48e14 MD |
120 | |
121 | static unsigned int nr_readers; | |
122 | static unsigned int nr_writers; | |
123 | ||
124 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; | |
125 | ||
61c3fb60 | 126 | static |
10c48e14 MD |
127 | void *thr_reader(void *_count) |
128 | { | |
129 | unsigned long long *count = _count; | |
130 | ||
94df6318 MD |
131 | printf_verbose("thread_begin %s, tid %lu\n", |
132 | "reader", urcu_get_thread_id()); | |
10c48e14 | 133 | |
2a7ac59d MD |
134 | set_affinity(); |
135 | ||
10c48e14 MD |
136 | while (!test_go) |
137 | { | |
138 | } | |
139 | ||
140 | for (;;) { | |
89a6a9ce MJ |
141 | int a, ret; |
142 | ||
143 | ret = pthread_rwlock_rdlock(&lock); | |
144 | if (ret) { | |
145 | fprintf(stderr, "reader pthread_rwlock_rdlock: %s\n", strerror(ret)); | |
146 | abort(); | |
147 | } | |
efa8d2c8 | 148 | |
efa8d2c8 | 149 | a = test_array.a; |
01477510 | 150 | urcu_posix_assert(a == 8); |
a0b7f7ea | 151 | if (caa_unlikely(rduration)) |
daddf5b0 | 152 | loop_sleep(rduration); |
89a6a9ce MJ |
153 | |
154 | ret = pthread_rwlock_unlock(&lock); | |
155 | if (ret) { | |
156 | fprintf(stderr, "reader pthread_rwlock_unlock: %s\n", strerror(ret)); | |
157 | abort(); | |
158 | } | |
159 | ||
bd252a04 | 160 | URCU_TLS(nr_reads)++; |
a0b7f7ea | 161 | if (caa_unlikely(!test_duration_read())) |
10c48e14 MD |
162 | break; |
163 | } | |
164 | ||
bd252a04 | 165 | *count = URCU_TLS(nr_reads); |
aae4fe1d MJ |
166 | |
167 | printf_verbose("thread_end %s, tid %lu, count %llu\n", | |
168 | "reader", urcu_get_thread_id(), *count); | |
10c48e14 MD |
169 | return ((void*)1); |
170 | ||
171 | } | |
172 | ||
61c3fb60 | 173 | static |
10c48e14 MD |
174 | void *thr_writer(void *_count) |
175 | { | |
176 | unsigned long long *count = _count; | |
177 | ||
94df6318 MD |
178 | printf_verbose("thread_begin %s, tid %lu\n", |
179 | "writer", urcu_get_thread_id()); | |
10c48e14 | 180 | |
2a7ac59d MD |
181 | set_affinity(); |
182 | ||
10c48e14 MD |
183 | while (!test_go) |
184 | { | |
185 | } | |
5481ddb3 | 186 | cmm_smp_mb(); |
10c48e14 MD |
187 | |
188 | for (;;) { | |
89a6a9ce MJ |
189 | int ret; |
190 | ||
191 | ret = pthread_rwlock_wrlock(&lock); | |
192 | if (ret) { | |
193 | fprintf(stderr, "writer pthread_rwlock_wrlock: %s\n", strerror(ret)); | |
194 | abort(); | |
195 | } | |
196 | ||
2b4e4125 | 197 | test_array.a = 0; |
10c48e14 | 198 | test_array.a = 8; |
a0b7f7ea | 199 | if (caa_unlikely(wduration)) |
31b598e0 | 200 | loop_sleep(wduration); |
89a6a9ce MJ |
201 | |
202 | ret = pthread_rwlock_unlock(&lock); | |
203 | if (ret) { | |
204 | fprintf(stderr, "writer pthread_rwlock_unlock: %s\n", strerror(ret)); | |
205 | abort(); | |
206 | } | |
207 | ||
bd252a04 | 208 | URCU_TLS(nr_writes)++; |
a0b7f7ea | 209 | if (caa_unlikely(!test_duration_write())) |
10c48e14 | 210 | break; |
a0b7f7ea | 211 | if (caa_unlikely(wdelay)) |
9e31d0f0 | 212 | loop_sleep(wdelay); |
10c48e14 | 213 | } |
bd252a04 | 214 | *count = URCU_TLS(nr_writes); |
aae4fe1d MJ |
215 | |
216 | printf_verbose("thread_end %s, tid %lu, count %llu\n", | |
217 | "writer", urcu_get_thread_id(), *count); | |
10c48e14 MD |
218 | return ((void*)2); |
219 | } | |
220 | ||
61c3fb60 | 221 | static |
70469b43 | 222 | void show_usage(char **argv) |
10c48e14 | 223 | { |
06637138 MD |
224 | printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n", |
225 | argv[0]); | |
226 | printf("OPTIONS:\n"); | |
06637138 MD |
227 | printf(" [-d delay] (writer period (us))\n"); |
228 | printf(" [-c duration] (reader C.S. duration (in loops))\n"); | |
229 | printf(" [-e duration] (writer C.S. duration (in loops))\n"); | |
230 | printf(" [-v] (verbose output)\n"); | |
231 | printf(" [-a cpu#] [-a cpu#]... (affinity)\n"); | |
10c48e14 MD |
232 | printf("\n"); |
233 | } | |
234 | ||
235 | int main(int argc, char **argv) | |
236 | { | |
237 | int err; | |
238 | pthread_t *tid_reader, *tid_writer; | |
239 | void *tret; | |
240 | unsigned long long *count_reader, *count_writer; | |
241 | unsigned long long tot_reads = 0, tot_writes = 0; | |
9e97e478 | 242 | int i, a; |
83e334d0 | 243 | unsigned int i_thr; |
10c48e14 MD |
244 | |
245 | if (argc < 4) { | |
70469b43 | 246 | show_usage(argv); |
10c48e14 MD |
247 | return -1; |
248 | } | |
5481ddb3 | 249 | cmm_smp_mb(); |
10c48e14 MD |
250 | |
251 | err = sscanf(argv[1], "%u", &nr_readers); | |
252 | if (err != 1) { | |
70469b43 | 253 | show_usage(argv); |
10c48e14 MD |
254 | return -1; |
255 | } | |
256 | ||
257 | err = sscanf(argv[2], "%u", &nr_writers); | |
258 | if (err != 1) { | |
70469b43 | 259 | show_usage(argv); |
10c48e14 MD |
260 | return -1; |
261 | } | |
262 | ||
263 | err = sscanf(argv[3], "%lu", &duration); | |
264 | if (err != 1) { | |
70469b43 | 265 | show_usage(argv); |
10c48e14 MD |
266 | return -1; |
267 | } | |
268 | ||
269 | for (i = 4; i < argc; i++) { | |
270 | if (argv[i][0] != '-') | |
271 | continue; | |
272 | switch (argv[i][1]) { | |
9e97e478 MD |
273 | case 'a': |
274 | if (argc < i + 2) { | |
70469b43 | 275 | show_usage(argv); |
9e97e478 MD |
276 | return -1; |
277 | } | |
278 | a = atoi(argv[++i]); | |
2a7ac59d | 279 | cpu_affinities[next_aff++] = a; |
9e97e478 | 280 | use_affinity = 1; |
4bad7d45 | 281 | printf_verbose("Adding CPU %d affinity\n", a); |
9e97e478 | 282 | break; |
8b632bab MD |
283 | case 'c': |
284 | if (argc < i + 2) { | |
70469b43 | 285 | show_usage(argv); |
8b632bab MD |
286 | return -1; |
287 | } | |
9e31d0f0 | 288 | rduration = atol(argv[++i]); |
8b632bab | 289 | break; |
10c48e14 MD |
290 | case 'd': |
291 | if (argc < i + 2) { | |
70469b43 | 292 | show_usage(argv); |
10c48e14 MD |
293 | return -1; |
294 | } | |
9e31d0f0 | 295 | wdelay = atol(argv[++i]); |
10c48e14 | 296 | break; |
31b598e0 MD |
297 | case 'e': |
298 | if (argc < i + 2) { | |
70469b43 | 299 | show_usage(argv); |
31b598e0 MD |
300 | return -1; |
301 | } | |
302 | wduration = atol(argv[++i]); | |
303 | break; | |
4bad7d45 MD |
304 | case 'v': |
305 | verbose_mode = 1; | |
306 | break; | |
10c48e14 MD |
307 | } |
308 | } | |
309 | ||
4bad7d45 | 310 | printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", |
10c48e14 | 311 | duration, nr_readers, nr_writers); |
9e31d0f0 | 312 | printf_verbose("Writer delay : %lu loops.\n", wdelay); |
aae4fe1d | 313 | printf_verbose("Writer duration : %lu loops.\n", wduration); |
4bad7d45 | 314 | printf_verbose("Reader duration : %lu loops.\n", rduration); |
94df6318 MD |
315 | printf_verbose("thread %-6s, tid %lu\n", |
316 | "main", urcu_get_thread_id()); | |
10c48e14 | 317 | |
89a6a9ce MJ |
318 | err = pthread_rwlock_init(&lock, NULL); |
319 | if (err != 0) { | |
320 | fprintf(stderr, "pthread_rwlock_init: (%d) %s\n", err, strerror(err)); | |
321 | exit(1); | |
322 | } | |
323 | ||
9aa14175 MD |
324 | tid_reader = calloc(nr_readers, sizeof(*tid_reader)); |
325 | tid_writer = calloc(nr_writers, sizeof(*tid_writer)); | |
326 | count_reader = calloc(nr_readers, sizeof(*count_reader)); | |
327 | count_writer = calloc(nr_writers, sizeof(*count_writer)); | |
10c48e14 | 328 | |
2a7ac59d MD |
329 | next_aff = 0; |
330 | ||
83e334d0 MJ |
331 | for (i_thr = 0; i_thr < nr_readers; i_thr++) { |
332 | err = pthread_create(&tid_reader[i_thr], NULL, thr_reader, | |
333 | &count_reader[i_thr]); | |
10c48e14 MD |
334 | if (err != 0) |
335 | exit(1); | |
336 | } | |
83e334d0 MJ |
337 | for (i_thr = 0; i_thr < nr_writers; i_thr++) { |
338 | err = pthread_create(&tid_writer[i_thr], NULL, thr_writer, | |
339 | &count_writer[i_thr]); | |
10c48e14 MD |
340 | if (err != 0) |
341 | exit(1); | |
342 | } | |
343 | ||
5481ddb3 | 344 | cmm_smp_mb(); |
10c48e14 | 345 | |
78efb485 MD |
346 | test_go = 1; |
347 | ||
348 | sleep(duration); | |
349 | ||
350 | test_stop = 1; | |
351 | ||
83e334d0 MJ |
352 | for (i_thr = 0; i_thr < nr_readers; i_thr++) { |
353 | err = pthread_join(tid_reader[i_thr], &tret); | |
10c48e14 MD |
354 | if (err != 0) |
355 | exit(1); | |
83e334d0 | 356 | tot_reads += count_reader[i_thr]; |
10c48e14 | 357 | } |
83e334d0 MJ |
358 | for (i_thr = 0; i_thr < nr_writers; i_thr++) { |
359 | err = pthread_join(tid_writer[i_thr], &tret); | |
10c48e14 MD |
360 | if (err != 0) |
361 | exit(1); | |
83e334d0 | 362 | tot_writes += count_writer[i_thr]; |
10c48e14 | 363 | } |
4bad7d45 MD |
364 | |
365 | printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads, | |
10c48e14 | 366 | tot_writes); |
a50a7b43 | 367 | printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu " |
cda3c71d | 368 | "nr_writers %3u " |
9e31d0f0 | 369 | "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n", |
a50a7b43 | 370 | argv[0], duration, nr_readers, rduration, wduration, |
4bad7d45 MD |
371 | nr_writers, wdelay, tot_reads, tot_writes, |
372 | tot_reads + tot_writes); | |
373 | ||
89a6a9ce MJ |
374 | err = pthread_rwlock_destroy(&lock); |
375 | if (err != 0) { | |
376 | fprintf(stderr, "pthread_rwlock_destroy: (%d) %s\n", err, strerror(err)); | |
377 | exit(1); | |
378 | } | |
10c48e14 MD |
379 | free(tid_reader); |
380 | free(tid_writer); | |
381 | free(count_reader); | |
382 | free(count_writer); | |
383 | return 0; | |
384 | } |