4 * Userspace RCU library - test program
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 #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)
56 #ifndef DYNAMIC_LINK_TEST
59 #define debug_yield_read()
67 static volatile int test_go
, test_stop
;
69 static unsigned long wdelay
;
71 static struct test_array
*test_rcu_pointer
;
73 static unsigned long duration
;
75 /* read-side C.S. duration, in loops */
76 static unsigned long rduration
;
78 static inline void loop_sleep(unsigned long l
)
84 static int verbose_mode
;
86 #define printf_verbose(fmt, args...) \
93 * returns 0 if test should end.
95 static int test_duration_write(void)
100 static int test_duration_read(void)
105 static unsigned long long __thread nr_writes
;
106 static unsigned long long __thread nr_reads
;
108 static unsigned int nr_readers
;
109 static unsigned int nr_writers
;
111 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
113 void rcu_copy_mutex_lock(void)
116 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
118 perror("Error in pthread mutex lock");
123 void rcu_copy_mutex_unlock(void)
127 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
129 perror("Error in pthread mutex unlock");
135 * malloc/free are reusing memory areas too quickly, which does not let us
136 * test races appropriately. Use a large circular array for allocations.
137 * ARRAY_SIZE is larger than nr_writers, which insures we never run over our tail.
139 #define ARRAY_SIZE (1048576 * nr_writers)
140 #define ARRAY_POISON 0xDEADBEEF
141 static int array_index
;
142 static struct test_array
*test_array
;
144 static struct test_array
*test_array_alloc(void)
146 struct test_array
*ret
;
149 rcu_copy_mutex_lock();
150 index
= array_index
% ARRAY_SIZE
;
151 assert(test_array
[index
].a
== ARRAY_POISON
||
152 test_array
[index
].a
== 0);
153 ret
= &test_array
[index
];
155 if (array_index
== ARRAY_SIZE
)
157 rcu_copy_mutex_unlock();
161 static void test_array_free(struct test_array
*ptr
)
165 rcu_copy_mutex_lock();
166 ptr
->a
= ARRAY_POISON
;
167 rcu_copy_mutex_unlock();
170 void *thr_reader(void *_count
)
172 unsigned long long *count
= _count
;
173 struct test_array
*local_ptr
;
175 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
176 "reader", pthread_self(), (unsigned long)gettid());
178 rcu_register_thread();
187 local_ptr
= rcu_dereference(test_rcu_pointer
);
190 assert(local_ptr
->a
== 8);
191 if (unlikely(rduration
))
192 loop_sleep(rduration
);
195 if (unlikely(!test_duration_read()))
199 rcu_unregister_thread();
202 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
203 "reader", pthread_self(), (unsigned long)gettid());
208 void *thr_writer(void *_count
)
210 unsigned long long *count
= _count
;
211 struct test_array
*new, *old
;
213 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
214 "writer", pthread_self(), (unsigned long)gettid());
222 new = test_array_alloc();
223 rcu_copy_mutex_lock();
224 old
= test_rcu_pointer
;
228 old
= rcu_publish_content(&test_rcu_pointer
, new);
229 rcu_copy_mutex_unlock();
230 /* can be done after unlock */
233 test_array_free(old
);
235 if (unlikely(!test_duration_write()))
237 if (unlikely(wdelay
))
241 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
242 "writer", pthread_self(), (unsigned long)gettid());
247 void show_usage(int argc
, char **argv
)
249 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
251 printf(" [-r] [-w] (yield reader and/or writer)");
253 printf(" [-d delay] (writer period (us))");
254 printf(" [-c duration] (reader C.S. duration (in loops))");
255 printf(" [-v] (verbose output)");
256 printf(" [-a cpu#] [-a cpu#]... (affinity)");
262 int main(int argc
, char **argv
)
265 pthread_t
*tid_reader
, *tid_writer
;
267 unsigned long long *count_reader
, *count_writer
;
268 unsigned long long tot_reads
= 0, tot_writes
= 0;
270 int use_affinity
= 0;
273 show_usage(argc
, argv
);
277 err
= sscanf(argv
[1], "%u", &nr_readers
);
279 show_usage(argc
, argv
);
283 err
= sscanf(argv
[2], "%u", &nr_writers
);
285 show_usage(argc
, argv
);
289 err
= sscanf(argv
[3], "%lu", &duration
);
291 show_usage(argc
, argv
);
297 for (i
= 4; i
< argc
; i
++) {
298 if (argv
[i
][0] != '-')
300 switch (argv
[i
][1]) {
303 yield_active
|= YIELD_READ
;
306 yield_active
|= YIELD_WRITE
;
311 show_usage(argc
, argv
);
315 CPU_SET(a
, &affinity
);
317 printf_verbose("Adding CPU %d affinity\n", a
);
321 show_usage(argc
, argv
);
324 rduration
= atol(argv
[++i
]);
328 show_usage(argc
, argv
);
331 wdelay
= atol(argv
[++i
]);
339 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
340 duration
, nr_readers
, nr_writers
);
341 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
342 printf_verbose("Reader duration : %lu loops.\n", rduration
);
343 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
344 "main", pthread_self(), (unsigned long)gettid());
347 && sched_setaffinity(0, sizeof(affinity
), &affinity
) < 0) {
348 perror("sched_setaffinity");
352 test_array
= malloc(sizeof(*test_array
) * ARRAY_SIZE
);
353 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
354 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
355 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
356 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
358 for (i
= 0; i
< nr_readers
; i
++) {
359 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
364 for (i
= 0; i
< nr_writers
; i
++) {
365 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
379 for (i
= 0; i
< nr_readers
; i
++) {
380 err
= pthread_join(tid_reader
[i
], &tret
);
383 tot_reads
+= count_reader
[i
];
385 for (i
= 0; i
< nr_writers
; i
++) {
386 err
= pthread_join(tid_writer
[i
], &tret
);
389 tot_writes
+= count_writer
[i
];
392 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
394 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
396 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
397 argv
[0], duration
, nr_readers
, rduration
,
398 nr_writers
, wdelay
, tot_reads
, tot_writes
,
399 tot_reads
+ tot_writes
);
400 test_array_free(test_rcu_pointer
);