4 * Userspace RCU library - test program (with baatch reclamation)
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
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.
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.
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.
28 #include <sys/types.h>
33 #include <sys/syscall.h>
38 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
39 #define CACHE_LINE_SIZE 4096
41 /* hardcoded number of CPUs */
44 #if defined(_syscall0)
45 _syscall0(pid_t
, gettid
)
46 #elif defined(__NR_gettid)
47 static inline pid_t
gettid(void)
49 return syscall(__NR_gettid
);
52 #warning "use pid as tid"
53 static inline pid_t
gettid(void)
60 #include "../urcu-qsbr.h"
66 static volatile int test_go
, test_stop
;
68 static unsigned long wdelay
;
70 static struct test_array
*test_rcu_pointer
;
72 static unsigned long duration
;
74 /* read-side C.S. duration, in loops */
75 static unsigned long rduration
;
76 static unsigned int reclaim_batch
= 1;
78 struct reclaim_queue
{
79 void **queue
; /* Beginning of queue */
80 void **head
; /* Insert position */
83 static struct reclaim_queue
*pending_reclaims
;
86 static inline void loop_sleep(unsigned long l
)
92 static int verbose_mode
;
94 #define printf_verbose(fmt, args...) \
100 static unsigned int cpu_affinities
[NR_CPUS
];
101 static unsigned int next_aff
= 0;
102 static int use_affinity
= 0;
104 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
106 static void set_affinity(void)
115 ret
= pthread_mutex_lock(&affinity_mutex
);
117 perror("Error in pthread mutex lock");
120 cpu
= cpu_affinities
[next_aff
++];
121 ret
= pthread_mutex_unlock(&affinity_mutex
);
123 perror("Error in pthread mutex unlock");
128 sched_setaffinity(0, sizeof(mask
), &mask
);
132 * returns 0 if test should end.
134 static int test_duration_write(void)
139 static int test_duration_read(void)
144 static unsigned long long __thread nr_writes
;
145 static unsigned long long __thread nr_reads
;
147 static unsigned int nr_readers
;
148 static unsigned int nr_writers
;
150 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
152 unsigned long long __attribute__((aligned(CACHE_LINE_SIZE
))) *tot_nr_writes
;
155 void rcu_copy_mutex_lock(void)
158 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
160 perror("Error in pthread mutex lock");
165 void rcu_copy_mutex_unlock(void)
169 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
171 perror("Error in pthread mutex unlock");
176 void *thr_reader(void *_count
)
178 unsigned long long *count
= _count
;
179 struct test_array
*local_ptr
;
181 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
182 "reader", pthread_self(), (unsigned long)gettid());
186 rcu_register_thread();
195 local_ptr
= _rcu_dereference(test_rcu_pointer
);
198 assert(local_ptr
->a
== 8);
199 if (unlikely(rduration
))
200 loop_sleep(rduration
);
203 /* QS each 1024 reads */
204 if (unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
205 _rcu_quiescent_state();
206 if (unlikely(!test_duration_read()))
210 rcu_unregister_thread();
213 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
214 "reader", pthread_self(), (unsigned long)gettid());
219 static void rcu_gc_clear_queue(unsigned long wtidx
)
223 /* Wait for Q.S and empty queue */
226 for (p
= pending_reclaims
[wtidx
].queue
;
227 p
< pending_reclaims
[wtidx
].head
; p
++) {
230 ((struct test_array
*)*p
)->a
= 0;
233 pending_reclaims
[wtidx
].head
= pending_reclaims
[wtidx
].queue
;
236 /* Using per-thread queue */
237 static void rcu_gc_reclaim(unsigned long wtidx
, void *old
)
240 *pending_reclaims
[wtidx
].head
= old
;
241 pending_reclaims
[wtidx
].head
++;
243 if (likely(pending_reclaims
[wtidx
].head
- pending_reclaims
[wtidx
].queue
247 rcu_gc_clear_queue(wtidx
);
250 void *thr_writer(void *data
)
252 unsigned long wtidx
= (unsigned long)data
;
254 struct test_array
*old
= NULL
;
256 struct test_array
*new, *old
;
259 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
260 "writer", pthread_self(), (unsigned long)gettid());
270 #ifndef TEST_LOCAL_GC
271 new = malloc(sizeof(*new));
273 old
= _rcu_xchg_pointer(&test_rcu_pointer
, new);
275 rcu_gc_reclaim(wtidx
, old
);
277 if (unlikely(!test_duration_write()))
279 if (unlikely(wdelay
))
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
;
289 void show_usage(int argc
, char **argv
)
291 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
293 printf(" [-r] [-w] (yield reader and/or writer)");
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)");
302 int main(int argc
, char **argv
)
305 pthread_t
*tid_reader
, *tid_writer
;
307 unsigned long long *count_reader
;
308 unsigned long long tot_reads
= 0, tot_writes
= 0;
312 show_usage(argc
, argv
);
316 err
= sscanf(argv
[1], "%u", &nr_readers
);
318 show_usage(argc
, argv
);
322 err
= sscanf(argv
[2], "%u", &nr_writers
);
324 show_usage(argc
, argv
);
328 err
= sscanf(argv
[3], "%lu", &duration
);
330 show_usage(argc
, argv
);
334 for (i
= 4; i
< argc
; i
++) {
335 if (argv
[i
][0] != '-')
337 switch (argv
[i
][1]) {
340 yield_active
|= YIELD_READ
;
343 yield_active
|= YIELD_WRITE
;
348 show_usage(argc
, argv
);
352 cpu_affinities
[next_aff
++] = a
;
354 printf_verbose("Adding CPU %d affinity\n", a
);
358 show_usage(argc
, argv
);
361 reclaim_batch
= atol(argv
[++i
]);
365 show_usage(argc
, argv
);
368 rduration
= atol(argv
[++i
]);
372 show_usage(argc
, argv
);
375 wdelay
= atol(argv
[++i
]);
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());
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
)
397 for (i
= 0; i
< nr_writers
; i
++)
398 pending_reclaims
[i
].queue
= calloc(1, CACHE_LINE_SIZE
);
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
;
408 for (i
= 0; i
< nr_readers
; i
++) {
409 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
414 for (i
= 0; i
< nr_writers
; i
++) {
415 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
429 for (i
= 0; i
< nr_readers
; i
++) {
430 err
= pthread_join(tid_reader
[i
], &tret
);
433 tot_reads
+= count_reader
[i
];
435 for (i
= 0; i
< nr_writers
; i
++) {
436 err
= pthread_join(tid_writer
[i
], &tret
);
439 tot_writes
+= tot_nr_writes
[i
];
440 rcu_gc_clear_queue(i
);
443 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
445 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
447 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
449 argv
[0], duration
, nr_readers
, rduration
,
450 nr_writers
, wdelay
, tot_reads
, tot_writes
,
451 tot_reads
+ tot_writes
, reclaim_batch
);
456 for (i
= 0; i
< nr_writers
; i
++)
457 free(pending_reclaims
[i
].queue
);
458 free(pending_reclaims
);