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