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