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