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