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>
36 #include <urcu/arch.h>
38 /* hardcoded number of CPUs */
41 #if defined(_syscall0)
42 _syscall0(pid_t
, gettid
)
43 #elif defined(__NR_gettid)
44 static inline pid_t
gettid(void)
46 return syscall(__NR_gettid
);
49 #warning "use pid as tid"
50 static inline pid_t
gettid(void)
57 #include <urcu-qsbr.h>
63 static volatile int test_go
, test_stop
;
65 static unsigned long wdelay
;
67 static struct test_array
*test_rcu_pointer
;
69 static unsigned long duration
;
71 /* read-side C.S. duration, in loops */
72 static unsigned long rduration
;
73 static unsigned int reclaim_batch
= 1;
75 struct reclaim_queue
{
76 void **queue
; /* Beginning of queue */
77 void **head
; /* Insert position */
80 static struct reclaim_queue
*pending_reclaims
;
83 static inline void loop_sleep(unsigned long l
)
89 static int verbose_mode
;
91 #define printf_verbose(fmt, args...) \
97 static unsigned int cpu_affinities
[NR_CPUS
];
98 static unsigned int next_aff
= 0;
99 static int use_affinity
= 0;
101 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
103 static void set_affinity(void)
112 ret
= pthread_mutex_lock(&affinity_mutex
);
114 perror("Error in pthread mutex lock");
117 cpu
= cpu_affinities
[next_aff
++];
118 ret
= pthread_mutex_unlock(&affinity_mutex
);
120 perror("Error in pthread mutex unlock");
125 sched_setaffinity(0, sizeof(mask
), &mask
);
129 * returns 0 if test should end.
131 static int test_duration_write(void)
136 static int test_duration_read(void)
141 static unsigned long long __thread nr_writes
;
142 static unsigned long long __thread nr_reads
;
144 static unsigned int nr_readers
;
145 static unsigned int nr_writers
;
147 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
149 unsigned long long __attribute__((aligned(CACHE_LINE_SIZE
))) *tot_nr_writes
;
152 void rcu_copy_mutex_lock(void)
155 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
157 perror("Error in pthread mutex lock");
162 void rcu_copy_mutex_unlock(void)
166 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
168 perror("Error in pthread mutex unlock");
173 void *thr_reader(void *_count
)
175 unsigned long long *count
= _count
;
176 struct test_array
*local_ptr
;
178 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
179 "reader", pthread_self(), (unsigned long)gettid());
183 rcu_register_thread();
192 local_ptr
= _rcu_dereference(test_rcu_pointer
);
195 assert(local_ptr
->a
== 8);
196 if (unlikely(rduration
))
197 loop_sleep(rduration
);
200 /* QS each 1024 reads */
201 if (unlikely((nr_reads
& ((1 << 10) - 1)) == 0))
202 _rcu_quiescent_state();
203 if (unlikely(!test_duration_read()))
207 rcu_unregister_thread();
210 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
211 "reader", pthread_self(), (unsigned long)gettid());
216 static void rcu_gc_clear_queue(unsigned long wtidx
)
220 /* Wait for Q.S and empty queue */
223 for (p
= pending_reclaims
[wtidx
].queue
;
224 p
< pending_reclaims
[wtidx
].head
; p
++) {
227 ((struct test_array
*)*p
)->a
= 0;
230 pending_reclaims
[wtidx
].head
= pending_reclaims
[wtidx
].queue
;
233 /* Using per-thread queue */
234 static void rcu_gc_reclaim(unsigned long wtidx
, void *old
)
237 *pending_reclaims
[wtidx
].head
= old
;
238 pending_reclaims
[wtidx
].head
++;
240 if (likely(pending_reclaims
[wtidx
].head
- pending_reclaims
[wtidx
].queue
244 rcu_gc_clear_queue(wtidx
);
247 void *thr_writer(void *data
)
249 unsigned long wtidx
= (unsigned long)data
;
251 struct test_array
*old
= NULL
;
253 struct test_array
*new, *old
;
256 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
257 "writer", pthread_self(), (unsigned long)gettid());
267 #ifndef TEST_LOCAL_GC
268 new = malloc(sizeof(*new));
270 old
= _rcu_xchg_pointer(&test_rcu_pointer
, new);
272 rcu_gc_reclaim(wtidx
, old
);
274 if (unlikely(!test_duration_write()))
276 if (unlikely(wdelay
))
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
;
286 void show_usage(int argc
, char **argv
)
288 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
290 printf(" [-r] [-w] (yield reader and/or writer)");
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)");
299 int main(int argc
, char **argv
)
302 pthread_t
*tid_reader
, *tid_writer
;
304 unsigned long long *count_reader
;
305 unsigned long long tot_reads
= 0, tot_writes
= 0;
309 show_usage(argc
, argv
);
313 err
= sscanf(argv
[1], "%u", &nr_readers
);
315 show_usage(argc
, argv
);
319 err
= sscanf(argv
[2], "%u", &nr_writers
);
321 show_usage(argc
, argv
);
325 err
= sscanf(argv
[3], "%lu", &duration
);
327 show_usage(argc
, argv
);
331 for (i
= 4; i
< argc
; i
++) {
332 if (argv
[i
][0] != '-')
334 switch (argv
[i
][1]) {
337 yield_active
|= YIELD_READ
;
340 yield_active
|= YIELD_WRITE
;
345 show_usage(argc
, argv
);
349 cpu_affinities
[next_aff
++] = a
;
351 printf_verbose("Adding CPU %d affinity\n", a
);
355 show_usage(argc
, argv
);
358 reclaim_batch
= atol(argv
[++i
]);
362 show_usage(argc
, argv
);
365 rduration
= atol(argv
[++i
]);
369 show_usage(argc
, argv
);
372 wdelay
= atol(argv
[++i
]);
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());
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
)
394 for (i
= 0; i
< nr_writers
; i
++)
395 pending_reclaims
[i
].queue
= calloc(1, CACHE_LINE_SIZE
);
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
;
405 for (i
= 0; i
< nr_readers
; i
++) {
406 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
411 for (i
= 0; i
< nr_writers
; i
++) {
412 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
426 for (i
= 0; i
< nr_readers
; i
++) {
427 err
= pthread_join(tid_reader
[i
], &tret
);
430 tot_reads
+= count_reader
[i
];
432 for (i
= 0; i
< nr_writers
; i
++) {
433 err
= pthread_join(tid_writer
[i
], &tret
);
436 tot_writes
+= tot_nr_writes
[i
];
437 rcu_gc_clear_queue(i
);
440 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
442 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
444 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
446 argv
[0], duration
, nr_readers
, rduration
,
447 nr_writers
, wdelay
, tot_reads
, tot_writes
,
448 tot_reads
+ tot_writes
, reclaim_batch
);
453 for (i
= 0; i
< nr_writers
; i
++)
454 free(pending_reclaims
[i
].queue
);
455 free(pending_reclaims
);