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)
59 #ifndef DYNAMIC_LINK_TEST
62 #define debug_yield_read()
70 static volatile int test_go
, test_stop
;
72 static unsigned long wdelay
;
74 static struct test_array
*test_rcu_pointer
;
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
;
85 static unsigned long duration
;
87 /* read-side C.S. duration, in loops */
88 static unsigned long rduration
;
90 static inline void loop_sleep(unsigned long l
)
96 static int verbose_mode
;
98 #define printf_verbose(fmt, args...) \
104 static unsigned int cpu_affinities
[NR_CPUS
];
105 static unsigned int next_aff
= 0;
106 static int use_affinity
= 0;
108 pthread_mutex_t affinity_mutex
= PTHREAD_MUTEX_INITIALIZER
;
110 static void set_affinity(void)
119 ret
= pthread_mutex_lock(&affinity_mutex
);
121 perror("Error in pthread mutex lock");
124 cpu
= cpu_affinities
[next_aff
++];
125 ret
= pthread_mutex_unlock(&affinity_mutex
);
127 perror("Error in pthread mutex unlock");
132 sched_setaffinity(0, sizeof(mask
), &mask
);
136 * returns 0 if test should end.
138 static int test_duration_write(void)
143 static int test_duration_read(void)
148 static unsigned long long __thread nr_writes
;
149 static unsigned long long __thread nr_reads
;
152 unsigned long long __attribute__((aligned(CACHE_LINE_SIZE
))) *tot_nr_writes
;
154 static unsigned int nr_readers
;
155 static unsigned int nr_writers
;
157 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
159 void rcu_copy_mutex_lock(void)
162 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
164 perror("Error in pthread mutex lock");
169 void rcu_copy_mutex_unlock(void)
173 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
175 perror("Error in pthread mutex unlock");
180 void *thr_reader(void *_count
)
182 unsigned long long *count
= _count
;
183 struct test_array
*local_ptr
;
185 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
186 "reader", pthread_self(), (unsigned long)gettid());
190 rcu_register_thread();
199 local_ptr
= rcu_dereference(test_rcu_pointer
);
202 assert(local_ptr
->a
== 8);
203 if (unlikely(rduration
))
204 loop_sleep(rduration
);
207 if (unlikely(!test_duration_read()))
211 rcu_unregister_thread();
214 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
215 "reader", pthread_self(), (unsigned long)gettid());
220 static void rcu_gc_clear_queue(unsigned long wtidx
)
224 /* Wait for Q.S and empty queue */
227 for (p
= pending_reclaims
[wtidx
].queue
;
228 p
< pending_reclaims
[wtidx
].head
; p
++) {
231 ((struct test_array
*)*p
)->a
= 0;
234 pending_reclaims
[wtidx
].head
= pending_reclaims
[wtidx
].queue
;
237 /* Using per-thread queue */
238 static void rcu_gc_reclaim(unsigned long wtidx
, void *old
)
241 *pending_reclaims
[wtidx
].head
= old
;
242 pending_reclaims
[wtidx
].head
++;
244 if (likely(pending_reclaims
[wtidx
].head
- pending_reclaims
[wtidx
].queue
248 rcu_gc_clear_queue(wtidx
);
251 void *thr_writer(void *data
)
253 unsigned long wtidx
= (unsigned long)data
;
255 struct test_array
*old
= NULL
;
257 struct test_array
*new, *old
;
260 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
261 "writer", pthread_self(), (unsigned long)gettid());
271 #ifndef TEST_LOCAL_GC
272 new = malloc(sizeof(*new));
274 old
= rcu_xchg_pointer(&test_rcu_pointer
, new);
276 rcu_gc_reclaim(wtidx
, old
);
278 if (unlikely(!test_duration_write()))
280 if (unlikely(wdelay
))
284 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
285 "writer", pthread_self(), (unsigned long)gettid());
286 tot_nr_writes
[wtidx
] = nr_writes
;
290 void show_usage(int argc
, char **argv
)
292 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
294 printf(" [-r] [-w] (yield reader and/or writer)");
296 printf(" [-d delay] (writer period (us))");
297 printf(" [-c duration] (reader C.S. duration (in loops))");
298 printf(" [-v] (verbose output)");
299 printf(" [-a cpu#] [-a cpu#]... (affinity)");
303 int main(int argc
, char **argv
)
306 pthread_t
*tid_reader
, *tid_writer
;
308 unsigned long long *count_reader
;
309 unsigned long long tot_reads
= 0, tot_writes
= 0;
313 show_usage(argc
, argv
);
317 err
= sscanf(argv
[1], "%u", &nr_readers
);
319 show_usage(argc
, argv
);
323 err
= sscanf(argv
[2], "%u", &nr_writers
);
325 show_usage(argc
, argv
);
329 err
= sscanf(argv
[3], "%lu", &duration
);
331 show_usage(argc
, argv
);
335 for (i
= 4; i
< argc
; i
++) {
336 if (argv
[i
][0] != '-')
338 switch (argv
[i
][1]) {
341 yield_active
|= YIELD_READ
;
344 yield_active
|= YIELD_WRITE
;
349 show_usage(argc
, argv
);
353 cpu_affinities
[next_aff
++] = a
;
355 printf_verbose("Adding CPU %d affinity\n", a
);
359 show_usage(argc
, argv
);
362 reclaim_batch
= atol(argv
[++i
]);
366 show_usage(argc
, argv
);
369 rduration
= atol(argv
[++i
]);
373 show_usage(argc
, argv
);
376 wdelay
= atol(argv
[++i
]);
384 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
385 duration
, nr_readers
, nr_writers
);
386 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
387 printf_verbose("Reader duration : %lu loops.\n", rduration
);
388 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
389 "main", pthread_self(), (unsigned long)gettid());
391 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
392 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
393 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
394 tot_nr_writes
= malloc(sizeof(*tot_nr_writes
) * nr_writers
);
395 pending_reclaims
= malloc(sizeof(*pending_reclaims
) * nr_writers
);
396 if (reclaim_batch
* sizeof(*pending_reclaims
[i
].queue
)
398 for (i
= 0; i
< nr_writers
; i
++)
399 pending_reclaims
[i
].queue
= calloc(1, CACHE_LINE_SIZE
);
401 for (i
= 0; i
< nr_writers
; i
++)
402 pending_reclaims
[i
].queue
= calloc(reclaim_batch
,
403 sizeof(*pending_reclaims
[i
].queue
));
404 for (i
= 0; i
< nr_writers
; i
++)
405 pending_reclaims
[i
].head
= pending_reclaims
[i
].queue
;
409 for (i
= 0; i
< nr_readers
; i
++) {
410 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
415 for (i
= 0; i
< nr_writers
; i
++) {
416 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
430 for (i
= 0; i
< nr_readers
; i
++) {
431 err
= pthread_join(tid_reader
[i
], &tret
);
434 tot_reads
+= count_reader
[i
];
436 for (i
= 0; i
< nr_writers
; i
++) {
437 err
= pthread_join(tid_writer
[i
], &tret
);
440 tot_writes
+= tot_nr_writes
[i
];
441 rcu_gc_clear_queue(i
);
444 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
446 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
448 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu "
450 argv
[0], duration
, nr_readers
, rduration
,
451 nr_writers
, wdelay
, tot_reads
, tot_writes
,
452 tot_reads
+ tot_writes
, reclaim_batch
);
457 for (i
= 0; i
< nr_writers
; i
++)
458 free(pending_reclaims
[i
].queue
);
459 free(pending_reclaims
);