Commit | Line | Data |
---|---|---|
a813abf8 MD |
1 | /* |
2 | * test_urcu_gc.c | |
3 | * | |
4 | * Userspace RCU library - test program (with baatch reclamation) | |
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> |
a813abf8 | 37 | |
2a7ac59d MD |
38 | /* hardcoded number of CPUs */ |
39 | #define NR_CPUS 16384 | |
40 | ||
a813abf8 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 | #define _LGPL_SOURCE | |
ec4e58a3 | 57 | #include <urcu-qsbr.h> |
a813abf8 MD |
58 | |
59 | struct test_array { | |
60 | int a; | |
61 | }; | |
62 | ||
63 | static volatile int test_go, test_stop; | |
64 | ||
65 | static unsigned long wdelay; | |
66 | ||
67 | static struct test_array *test_rcu_pointer; | |
68 | ||
69 | static unsigned long duration; | |
70 | ||
71 | /* read-side C.S. duration, in loops */ | |
72 | static unsigned long rduration; | |
4a3e0004 | 73 | static unsigned int reclaim_batch = 1; |
a813abf8 MD |
74 | |
75 | struct reclaim_queue { | |
76 | void **queue; /* Beginning of queue */ | |
77 | void **head; /* Insert position */ | |
78 | }; | |
79 | ||
80 | static struct reclaim_queue *pending_reclaims; | |
81 | ||
82 | ||
83 | static inline void loop_sleep(unsigned long l) | |
84 | { | |
85 | while(l-- != 0) | |
86 | cpu_relax(); | |
87 | } | |
88 | ||
89 | static int verbose_mode; | |
90 | ||
91 | #define printf_verbose(fmt, args...) \ | |
92 | do { \ | |
93 | if (verbose_mode) \ | |
94 | printf(fmt, args); \ | |
95 | } while (0) | |
96 | ||
2a7ac59d MD |
97 | static unsigned int cpu_affinities[NR_CPUS]; |
98 | static unsigned int next_aff = 0; | |
99 | static int use_affinity = 0; | |
100 | ||
6af882ba MD |
101 | pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER; |
102 | ||
2a7ac59d MD |
103 | static void set_affinity(void) |
104 | { | |
105 | cpu_set_t mask; | |
106 | int cpu; | |
6af882ba | 107 | int ret; |
2a7ac59d MD |
108 | |
109 | if (!use_affinity) | |
110 | return; | |
111 | ||
6af882ba MD |
112 | ret = pthread_mutex_lock(&affinity_mutex); |
113 | if (ret) { | |
114 | perror("Error in pthread mutex lock"); | |
115 | exit(-1); | |
116 | } | |
2a7ac59d | 117 | cpu = cpu_affinities[next_aff++]; |
6af882ba MD |
118 | ret = pthread_mutex_unlock(&affinity_mutex); |
119 | if (ret) { | |
120 | perror("Error in pthread mutex unlock"); | |
121 | exit(-1); | |
122 | } | |
2a7ac59d MD |
123 | CPU_ZERO(&mask); |
124 | CPU_SET(cpu, &mask); | |
125 | sched_setaffinity(0, sizeof(mask), &mask); | |
126 | } | |
127 | ||
a813abf8 MD |
128 | /* |
129 | * returns 0 if test should end. | |
130 | */ | |
131 | static int test_duration_write(void) | |
132 | { | |
133 | return !test_stop; | |
134 | } | |
135 | ||
136 | static int test_duration_read(void) | |
137 | { | |
138 | return !test_stop; | |
139 | } | |
140 | ||
141 | static unsigned long long __thread nr_writes; | |
142 | static unsigned long long __thread nr_reads; | |
143 | ||
144 | static unsigned int nr_readers; | |
145 | static unsigned int nr_writers; | |
146 | ||
147 | pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER; | |
148 | static | |
149 | unsigned long long __attribute__((aligned(CACHE_LINE_SIZE))) *tot_nr_writes; | |
150 | ||
151 | ||
152 | void rcu_copy_mutex_lock(void) | |
153 | { | |
154 | int ret; | |
155 | ret = pthread_mutex_lock(&rcu_copy_mutex); | |
156 | if (ret) { | |
157 | perror("Error in pthread mutex lock"); | |
158 | exit(-1); | |
159 | } | |
160 | } | |
161 | ||
162 | void rcu_copy_mutex_unlock(void) | |
163 | { | |
164 | int ret; | |
165 | ||
166 | ret = pthread_mutex_unlock(&rcu_copy_mutex); | |
167 | if (ret) { | |
168 | perror("Error in pthread mutex unlock"); | |
169 | exit(-1); | |
170 | } | |
171 | } | |
172 | ||
173 | void *thr_reader(void *_count) | |
174 | { | |
175 | unsigned long long *count = _count; | |
176 | struct test_array *local_ptr; | |
177 | ||
178 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
179 | "reader", pthread_self(), (unsigned long)gettid()); | |
180 | ||
2a7ac59d MD |
181 | set_affinity(); |
182 | ||
a813abf8 MD |
183 | rcu_register_thread(); |
184 | ||
185 | while (!test_go) | |
186 | { | |
187 | } | |
188 | smp_mb(); | |
189 | ||
190 | for (;;) { | |
191 | _rcu_read_lock(); | |
192 | local_ptr = _rcu_dereference(test_rcu_pointer); | |
193 | debug_yield_read(); | |
194 | if (local_ptr) | |
195 | assert(local_ptr->a == 8); | |
196 | if (unlikely(rduration)) | |
197 | loop_sleep(rduration); | |
198 | _rcu_read_unlock(); | |
199 | nr_reads++; | |
200 | /* QS each 1024 reads */ | |
201 | if (unlikely((nr_reads & ((1 << 10) - 1)) == 0)) | |
202 | _rcu_quiescent_state(); | |
203 | if (unlikely(!test_duration_read())) | |
204 | break; | |
205 | } | |
206 | ||
207 | rcu_unregister_thread(); | |
208 | ||
209 | *count = nr_reads; | |
210 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", | |
211 | "reader", pthread_self(), (unsigned long)gettid()); | |
212 | return ((void*)1); | |
213 | ||
214 | } | |
215 | ||
53091fe5 | 216 | static void rcu_gc_clear_queue(unsigned long wtidx) |
a813abf8 MD |
217 | { |
218 | void **p; | |
219 | ||
53091fe5 | 220 | /* Wait for Q.S and empty queue */ |
a813abf8 MD |
221 | synchronize_rcu(); |
222 | ||
223 | for (p = pending_reclaims[wtidx].queue; | |
224 | p < pending_reclaims[wtidx].head; p++) { | |
225 | /* poison */ | |
226 | if (*p) | |
227 | ((struct test_array *)*p)->a = 0; | |
228 | free(*p); | |
229 | } | |
230 | pending_reclaims[wtidx].head = pending_reclaims[wtidx].queue; | |
231 | } | |
232 | ||
53091fe5 MD |
233 | /* Using per-thread queue */ |
234 | static void rcu_gc_reclaim(unsigned long wtidx, void *old) | |
a813abf8 | 235 | { |
53091fe5 MD |
236 | /* Queue pointer */ |
237 | *pending_reclaims[wtidx].head = old; | |
238 | pending_reclaims[wtidx].head++; | |
a813abf8 | 239 | |
53091fe5 MD |
240 | if (likely(pending_reclaims[wtidx].head - pending_reclaims[wtidx].queue |
241 | < reclaim_batch)) | |
242 | return; | |
a813abf8 | 243 | |
53091fe5 | 244 | rcu_gc_clear_queue(wtidx); |
a813abf8 MD |
245 | } |
246 | ||
247 | void *thr_writer(void *data) | |
248 | { | |
249 | unsigned long wtidx = (unsigned long)data; | |
321e29d9 MD |
250 | #ifdef TEST_LOCAL_GC |
251 | struct test_array *old = NULL; | |
252 | #else | |
a813abf8 | 253 | struct test_array *new, *old; |
321e29d9 | 254 | #endif |
a813abf8 MD |
255 | |
256 | printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n", | |
257 | "writer", pthread_self(), (unsigned long)gettid()); | |
258 | ||
2a7ac59d MD |
259 | set_affinity(); |
260 | ||
a813abf8 MD |
261 | while (!test_go) |
262 | { | |
263 | } | |
264 | smp_mb(); | |
265 | ||
266 | for (;;) { | |
321e29d9 | 267 | #ifndef TEST_LOCAL_GC |
a813abf8 | 268 | new = malloc(sizeof(*new)); |
a813abf8 MD |
269 | new->a = 8; |
270 | old = _rcu_xchg_pointer(&test_rcu_pointer, new); | |
321e29d9 | 271 | #endif |
a813abf8 MD |
272 | rcu_gc_reclaim(wtidx, old); |
273 | nr_writes++; | |
274 | if (unlikely(!test_duration_write())) | |
275 | break; | |
276 | if (unlikely(wdelay)) | |
277 | loop_sleep(wdelay); | |
278 | } | |
279 | ||
280 | printf_verbose("thread_end %s, thread id : %lx, tid %lu\n", | |
281 | "writer", pthread_self(), (unsigned long)gettid()); | |
282 | tot_nr_writes[wtidx] = nr_writes; | |
283 | return ((void*)2); | |
284 | } | |
285 | ||
286 | void show_usage(int argc, char **argv) | |
287 | { | |
288 | printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]); | |
289 | #ifdef DEBUG_YIELD | |
290 | printf(" [-r] [-w] (yield reader and/or writer)"); | |
291 | #endif | |
292 | printf(" [-d delay] (writer period (us))"); | |
293 | printf(" [-c duration] (reader C.S. duration (in loops))"); | |
294 | printf(" [-v] (verbose output)"); | |
295 | printf(" [-a cpu#] [-a cpu#]... (affinity)"); | |
296 | printf("\n"); | |
297 | } | |
298 | ||
a813abf8 MD |
299 | int main(int argc, char **argv) |
300 | { | |
301 | int err; | |
302 | pthread_t *tid_reader, *tid_writer; | |
303 | void *tret; | |
304 | unsigned long long *count_reader; | |
305 | unsigned long long tot_reads = 0, tot_writes = 0; | |
306 | int i, a; | |
a813abf8 MD |
307 | |
308 | if (argc < 4) { | |
309 | show_usage(argc, argv); | |
310 | return -1; | |
311 | } | |
312 | ||
313 | err = sscanf(argv[1], "%u", &nr_readers); | |
314 | if (err != 1) { | |
315 | show_usage(argc, argv); | |
316 | return -1; | |
317 | } | |
318 | ||
319 | err = sscanf(argv[2], "%u", &nr_writers); | |
320 | if (err != 1) { | |
321 | show_usage(argc, argv); | |
322 | return -1; | |
323 | } | |
324 | ||
325 | err = sscanf(argv[3], "%lu", &duration); | |
326 | if (err != 1) { | |
327 | show_usage(argc, argv); | |
328 | return -1; | |
329 | } | |
330 | ||
a813abf8 MD |
331 | for (i = 4; i < argc; i++) { |
332 | if (argv[i][0] != '-') | |
333 | continue; | |
334 | switch (argv[i][1]) { | |
335 | #ifdef DEBUG_YIELD | |
336 | case 'r': | |
337 | yield_active |= YIELD_READ; | |
338 | break; | |
339 | case 'w': | |
340 | yield_active |= YIELD_WRITE; | |
341 | break; | |
342 | #endif | |
343 | case 'a': | |
344 | if (argc < i + 2) { | |
345 | show_usage(argc, argv); | |
346 | return -1; | |
347 | } | |
348 | a = atoi(argv[++i]); | |
2a7ac59d | 349 | cpu_affinities[next_aff++] = a; |
a813abf8 MD |
350 | use_affinity = 1; |
351 | printf_verbose("Adding CPU %d affinity\n", a); | |
352 | break; | |
353 | case 'b': | |
354 | if (argc < i + 2) { | |
355 | show_usage(argc, argv); | |
356 | return -1; | |
357 | } | |
358 | reclaim_batch = atol(argv[++i]); | |
359 | break; | |
360 | case 'c': | |
361 | if (argc < i + 2) { | |
362 | show_usage(argc, argv); | |
363 | return -1; | |
364 | } | |
365 | rduration = atol(argv[++i]); | |
366 | break; | |
367 | case 'd': | |
368 | if (argc < i + 2) { | |
369 | show_usage(argc, argv); | |
370 | return -1; | |
371 | } | |
372 | wdelay = atol(argv[++i]); | |
373 | break; | |
374 | case 'v': | |
375 | verbose_mode = 1; | |
376 | break; | |
377 | } | |
378 | } | |
379 | ||
380 | printf_verbose("running test for %lu seconds, %u readers, %u writers.\n", | |
381 | duration, nr_readers, nr_writers); | |
382 | printf_verbose("Writer delay : %lu loops.\n", wdelay); | |
383 | printf_verbose("Reader duration : %lu loops.\n", rduration); | |
384 | printf_verbose("thread %-6s, thread id : %lx, tid %lu\n", | |
385 | "main", pthread_self(), (unsigned long)gettid()); | |
386 | ||
a813abf8 MD |
387 | tid_reader = malloc(sizeof(*tid_reader) * nr_readers); |
388 | tid_writer = malloc(sizeof(*tid_writer) * nr_writers); | |
389 | count_reader = malloc(sizeof(*count_reader) * nr_readers); | |
390 | tot_nr_writes = malloc(sizeof(*tot_nr_writes) * nr_writers); | |
391 | pending_reclaims = malloc(sizeof(*pending_reclaims) * nr_writers); | |
392 | if (reclaim_batch * sizeof(*pending_reclaims[i].queue) | |
393 | < CACHE_LINE_SIZE) | |
394 | for (i = 0; i < nr_writers; i++) | |
395 | pending_reclaims[i].queue = calloc(1, CACHE_LINE_SIZE); | |
396 | else | |
397 | for (i = 0; i < nr_writers; i++) | |
398 | pending_reclaims[i].queue = calloc(reclaim_batch, | |
399 | sizeof(*pending_reclaims[i].queue)); | |
400 | for (i = 0; i < nr_writers; i++) | |
401 | pending_reclaims[i].head = pending_reclaims[i].queue; | |
402 | ||
2a7ac59d MD |
403 | next_aff = 0; |
404 | ||
a813abf8 MD |
405 | for (i = 0; i < nr_readers; i++) { |
406 | err = pthread_create(&tid_reader[i], NULL, thr_reader, | |
407 | &count_reader[i]); | |
408 | if (err != 0) | |
409 | exit(1); | |
410 | } | |
411 | for (i = 0; i < nr_writers; i++) { | |
412 | err = pthread_create(&tid_writer[i], NULL, thr_writer, | |
413 | (void *)(long)i); | |
414 | if (err != 0) | |
415 | exit(1); | |
416 | } | |
417 | ||
418 | smp_mb(); | |
419 | ||
420 | test_go = 1; | |
421 | ||
422 | sleep(duration); | |
423 | ||
424 | test_stop = 1; | |
425 | ||
426 | for (i = 0; i < nr_readers; i++) { | |
427 | err = pthread_join(tid_reader[i], &tret); | |
428 | if (err != 0) | |
429 | exit(1); | |
430 | tot_reads += count_reader[i]; | |
431 | } | |
432 | for (i = 0; i < nr_writers; i++) { | |
433 | err = pthread_join(tid_writer[i], &tret); | |
434 | if (err != 0) | |
435 | exit(1); | |
436 | tot_writes += tot_nr_writes[i]; | |
53091fe5 | 437 | rcu_gc_clear_queue(i); |
a813abf8 MD |
438 | } |
439 | ||
440 | printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads, | |
441 | tot_writes); | |
442 | printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu " | |
443 | "nr_writers %3u " | |
4a3e0004 MD |
444 | "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu " |
445 | "batch %u\n", | |
a813abf8 MD |
446 | argv[0], duration, nr_readers, rduration, |
447 | nr_writers, wdelay, tot_reads, tot_writes, | |
4a3e0004 | 448 | tot_reads + tot_writes, reclaim_batch); |
a813abf8 MD |
449 | free(tid_reader); |
450 | free(tid_writer); | |
451 | free(count_reader); | |
452 | free(tot_nr_writes); | |
453 | for (i = 0; i < nr_writers; i++) | |
454 | free(pending_reclaims[i].queue); | |
455 | free(pending_reclaims); | |
456 | ||
457 | return 0; | |
458 | } |