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