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