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