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