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