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