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 | ||
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 | struct per_thread_lock { | |
65 | pthread_mutex_t lock; | |
66 | } __attribute__((aligned(128))); /* cache-line aligned */ | |
67 | ||
68 | static struct per_thread_lock *per_thread_lock; | |
69 | ||
78efb485 | 70 | static volatile int test_go, test_stop; |
0ee8bb0a MD |
71 | |
72 | static int wdelay; | |
73 | ||
2b4e4125 | 74 | static volatile struct test_array test_array = { 8 }; |
0ee8bb0a MD |
75 | |
76 | static unsigned long duration; | |
0ee8bb0a | 77 | |
daddf5b0 | 78 | /* read-side C.S. duration, in loops */ |
8b632bab MD |
79 | static unsigned long rduration; |
80 | ||
daddf5b0 MD |
81 | static inline void loop_sleep(unsigned long l) |
82 | { | |
83 | while(l-- != 0) | |
84 | cpu_relax(); | |
85 | } | |
86 | ||
0ee8bb0a MD |
87 | /* |
88 | * returns 0 if test should end. | |
89 | */ | |
90 | static int test_duration_write(void) | |
91 | { | |
78efb485 | 92 | return !test_stop; |
0ee8bb0a MD |
93 | } |
94 | ||
95 | static int test_duration_read(void) | |
96 | { | |
78efb485 | 97 | return !test_stop; |
0ee8bb0a MD |
98 | } |
99 | ||
100 | static unsigned long long __thread nr_writes; | |
101 | static unsigned long long __thread nr_reads; | |
102 | ||
103 | static unsigned long long __attribute__((aligned(128))) *tot_nr_writes; | |
104 | static unsigned long long __attribute__((aligned(128))) *tot_nr_reads; | |
105 | ||
106 | static unsigned int nr_readers; | |
107 | static unsigned int nr_writers; | |
108 | ||
109 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; | |
110 | ||
111 | void rcu_copy_mutex_lock(void) | |
112 | { | |
113 | int ret; | |
114 | ret = pthread_mutex_lock(&rcu_copy_mutex); | |
115 | if (ret) { | |
116 | perror("Error in pthread mutex lock"); | |
117 | exit(-1); | |
118 | } | |
119 | } | |
120 | ||
121 | void rcu_copy_mutex_unlock(void) | |
122 | { | |
123 | int ret; | |
124 | ||
125 | ret = pthread_mutex_unlock(&rcu_copy_mutex); | |
126 | if (ret) { | |
127 | perror("Error in pthread mutex unlock"); | |
128 | exit(-1); | |
129 | } | |
130 | } | |
131 | ||
132 | void *thr_reader(void *data) | |
133 | { | |
134 | unsigned long tidx = (unsigned long)data; | |
135 | ||
136 | printf("thread_begin %s, thread id : %lx, tid %lu\n", | |
137 | "reader", pthread_self(), (unsigned long)gettid()); | |
138 | ||
139 | while (!test_go) | |
140 | { | |
141 | } | |
142 | ||
143 | for (;;) { | |
144 | pthread_mutex_lock(&per_thread_lock[tidx].lock); | |
145 | assert(test_array.a == 8); | |
8b632bab | 146 | if (unlikely(rduration)) |
daddf5b0 | 147 | loop_sleep(rduration); |
0ee8bb0a MD |
148 | pthread_mutex_unlock(&per_thread_lock[tidx].lock); |
149 | nr_reads++; | |
59d5a406 | 150 | if (unlikely(!test_duration_read())) |
0ee8bb0a MD |
151 | break; |
152 | } | |
153 | ||
154 | tot_nr_reads[tidx] = nr_reads; | |
155 | printf("thread_end %s, thread id : %lx, tid %lu\n", | |
156 | "reader", pthread_self(), (unsigned long)gettid()); | |
157 | return ((void*)1); | |
158 | ||
159 | } | |
160 | ||
161 | void *thr_writer(void *data) | |
162 | { | |
163 | unsigned long wtidx = (unsigned long)data; | |
164 | long tidx; | |
165 | ||
166 | printf("thread_begin %s, thread id : %lx, tid %lu\n", | |
167 | "writer", pthread_self(), (unsigned long)gettid()); | |
168 | ||
169 | while (!test_go) | |
170 | { | |
171 | } | |
172 | smp_mb(); | |
173 | ||
174 | for (;;) { | |
175 | for (tidx = 0; tidx < nr_readers; tidx++) { | |
176 | pthread_mutex_lock(&per_thread_lock[tidx].lock); | |
177 | } | |
2b4e4125 | 178 | test_array.a = 0; |
0ee8bb0a MD |
179 | test_array.a = 8; |
180 | for (tidx = nr_readers - 1; tidx >= 0; tidx--) { | |
181 | pthread_mutex_unlock(&per_thread_lock[tidx].lock); | |
182 | } | |
183 | nr_writes++; | |
59d5a406 | 184 | if (unlikely(!test_duration_write())) |
0ee8bb0a | 185 | break; |
59d5a406 | 186 | if (unlikely(wdelay)) |
0ee8bb0a MD |
187 | usleep(wdelay); |
188 | } | |
189 | ||
190 | printf("thread_end %s, thread id : %lx, tid %lu\n", | |
191 | "writer", pthread_self(), (unsigned long)gettid()); | |
192 | tot_nr_writes[wtidx] = nr_writes; | |
193 | return ((void*)2); | |
194 | } | |
195 | ||
196 | void show_usage(int argc, char **argv) | |
197 | { | |
198 | printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]); | |
199 | #ifdef DEBUG_YIELD | |
200 | printf(" [-r] [-w] (yield reader and/or writer)"); | |
201 | #endif | |
202 | printf(" [-d delay] (writer period (us))"); | |
daddf5b0 | 203 | printf(" [-c duration] (reader C.S. duration (in loops))"); |
9e97e478 | 204 | printf(" [-a cpu#] [-a cpu#]... (affinity)"); |
0ee8bb0a MD |
205 | printf("\n"); |
206 | } | |
207 | ||
9e97e478 MD |
208 | cpu_set_t affinity; |
209 | ||
0ee8bb0a MD |
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; | |
9e97e478 MD |
217 | int i, a; |
218 | int use_affinity = 0; | |
0ee8bb0a MD |
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 | ||
9e97e478 MD |
244 | CPU_ZERO(&affinity); |
245 | ||
0ee8bb0a MD |
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 | |
9e97e478 MD |
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; | |
266 | printf("Adding CPU %d affinity\n", a); | |
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; | |
0ee8bb0a 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; | |
282 | } | |
283 | } | |
284 | ||
285 | printf("running test for %lu seconds, %u readers, %u writers.\n", | |
286 | duration, nr_readers, nr_writers); | |
287 | printf("Writer delay : %u us.\n", wdelay); | |
900e541e | 288 | printf("Reader duration : %lu loops.\n", rduration); |
0ee8bb0a MD |
289 | printf("thread %-6s, thread id : %lx, tid %lu\n", |
290 | "main", pthread_self(), (unsigned long)gettid()); | |
291 | ||
9e97e478 MD |
292 | if (use_affinity |
293 | && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) { | |
294 | perror("sched_setaffinity"); | |
295 | exit(-1); | |
296 | } | |
297 | ||
0ee8bb0a MD |
298 | tid_reader = malloc(sizeof(*tid_reader) * nr_readers); |
299 | tid_writer = malloc(sizeof(*tid_writer) * nr_writers); | |
300 | count_reader = malloc(sizeof(*count_reader) * nr_readers); | |
301 | count_writer = malloc(sizeof(*count_writer) * nr_writers); | |
302 | tot_nr_reads = malloc(sizeof(*tot_nr_reads) * nr_readers); | |
303 | tot_nr_writes = malloc(sizeof(*tot_nr_writes) * nr_writers); | |
304 | per_thread_lock = malloc(sizeof(*per_thread_lock) * nr_readers); | |
305 | ||
306 | for (i = 0; i < nr_readers; i++) { | |
307 | err = pthread_create(&tid_reader[i], NULL, thr_reader, | |
308 | (void *)(long)i); | |
309 | if (err != 0) | |
310 | exit(1); | |
311 | } | |
312 | for (i = 0; i < nr_writers; i++) { | |
313 | err = pthread_create(&tid_writer[i], NULL, thr_writer, | |
314 | (void *)(long)i); | |
315 | if (err != 0) | |
316 | exit(1); | |
317 | } | |
318 | ||
0ee8bb0a MD |
319 | smp_mb(); |
320 | ||
78efb485 MD |
321 | test_go = 1; |
322 | ||
323 | sleep(duration); | |
324 | ||
325 | test_stop = 1; | |
326 | ||
0ee8bb0a MD |
327 | for (i = 0; i < nr_readers; i++) { |
328 | err = pthread_join(tid_reader[i], &tret); | |
329 | if (err != 0) | |
330 | exit(1); | |
331 | tot_reads += tot_nr_reads[i]; | |
332 | } | |
333 | for (i = 0; i < nr_writers; i++) { | |
334 | err = pthread_join(tid_writer[i], &tret); | |
335 | if (err != 0) | |
336 | exit(1); | |
337 | tot_writes += tot_nr_writes[i]; | |
338 | } | |
339 | ||
340 | printf("total number of reads : %llu, writes %llu\n", tot_reads, | |
341 | tot_writes); | |
342 | free(tid_reader); | |
343 | free(tid_writer); | |
344 | free(count_reader); | |
345 | free(count_writer); | |
346 | free(tot_nr_reads); | |
347 | free(tot_nr_writes); | |
348 | free(per_thread_lock); | |
349 | return 0; | |
350 | } |