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 #if defined(_syscall0)
39 _syscall0(pid_t
, gettid
)
40 #elif defined(__NR_gettid)
41 static inline pid_t
gettid(void)
43 return syscall(__NR_gettid
);
46 #warning "use pid as tid"
47 static inline pid_t
gettid(void)
53 #ifndef DYNAMIC_LINK_TEST
56 #define debug_yield_read()
64 pthread_rwlock_t lock
= PTHREAD_RWLOCK_INITIALIZER
;
66 static volatile int test_go
, test_stop
;
70 static volatile struct test_array test_array
= { 8 };
72 static unsigned long duration
;
74 /* read-side C.S. duration, in loops */
75 static unsigned long rduration
;
77 static inline void loop_sleep(unsigned long l
)
83 static int verbose_mode
;
85 #define printf_verbose(fmt, args...) \
92 * returns 0 if test should end.
94 static int test_duration_write(void)
99 static int test_duration_read(void)
104 static unsigned long long __thread nr_writes
;
105 static unsigned long long __thread nr_reads
;
107 static unsigned int nr_readers
;
108 static unsigned int nr_writers
;
110 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
112 void rcu_copy_mutex_lock(void)
115 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
117 perror("Error in pthread mutex lock");
122 void rcu_copy_mutex_unlock(void)
126 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
128 perror("Error in pthread mutex unlock");
133 void *thr_reader(void *_count
)
135 unsigned long long *count
= _count
;
137 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
138 "reader", pthread_self(), (unsigned long)gettid());
145 pthread_rwlock_rdlock(&lock
);
146 assert(test_array
.a
== 8);
147 if (unlikely(rduration
))
148 loop_sleep(rduration
);
149 pthread_rwlock_unlock(&lock
);
151 if (unlikely(!test_duration_read()))
156 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
157 "reader", pthread_self(), (unsigned long)gettid());
162 void *thr_writer(void *_count
)
164 unsigned long long *count
= _count
;
166 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
167 "writer", pthread_self(), (unsigned long)gettid());
175 pthread_rwlock_wrlock(&lock
);
178 pthread_rwlock_unlock(&lock
);
180 if (unlikely(!test_duration_write()))
182 if (unlikely(wdelay
))
186 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
187 "writer", pthread_self(), (unsigned long)gettid());
192 void show_usage(int argc
, char **argv
)
194 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
196 printf(" [-r] [-w] (yield reader and/or writer)");
198 printf(" [-d delay] (writer period (us))");
199 printf(" [-c duration] (reader C.S. duration (in loops))");
200 printf(" [-v] (verbose output)");
201 printf(" [-a cpu#] [-a cpu#]... (affinity)");
207 int main(int argc
, char **argv
)
210 pthread_t
*tid_reader
, *tid_writer
;
212 unsigned long long *count_reader
, *count_writer
;
213 unsigned long long tot_reads
= 0, tot_writes
= 0;
215 int use_affinity
= 0;
218 show_usage(argc
, argv
);
223 err
= sscanf(argv
[1], "%u", &nr_readers
);
225 show_usage(argc
, argv
);
229 err
= sscanf(argv
[2], "%u", &nr_writers
);
231 show_usage(argc
, argv
);
235 err
= sscanf(argv
[3], "%lu", &duration
);
237 show_usage(argc
, argv
);
243 for (i
= 4; i
< argc
; i
++) {
244 if (argv
[i
][0] != '-')
246 switch (argv
[i
][1]) {
249 yield_active
|= YIELD_READ
;
252 yield_active
|= YIELD_WRITE
;
257 show_usage(argc
, argv
);
261 CPU_SET(a
, &affinity
);
263 printf_verbose("Adding CPU %d affinity\n", a
);
267 show_usage(argc
, argv
);
270 rduration
= atoi(argv
[++i
]);
274 show_usage(argc
, argv
);
277 wdelay
= atoi(argv
[++i
]);
285 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
286 duration
, nr_readers
, nr_writers
);
287 printf_verbose("Writer delay : %u us.\n", wdelay
);
288 printf_verbose("Reader duration : %lu loops.\n", rduration
);
289 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
290 "main", pthread_self(), (unsigned long)gettid());
292 && sched_setaffinity(0, sizeof(affinity
), &affinity
) < 0) {
293 perror("sched_setaffinity");
297 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
298 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
299 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
300 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
302 for (i
= 0; i
< nr_readers
; i
++) {
303 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
308 for (i
= 0; i
< nr_writers
; i
++) {
309 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
323 for (i
= 0; i
< nr_readers
; i
++) {
324 err
= pthread_join(tid_reader
[i
], &tret
);
327 tot_reads
+= count_reader
[i
];
329 for (i
= 0; i
< nr_writers
; i
++) {
330 err
= pthread_join(tid_writer
[i
], &tret
);
333 tot_writes
+= count_writer
[i
];
336 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
338 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
340 "wdelay %4u nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
341 argv
[0], duration
, nr_readers
, rduration
,
342 nr_writers
, wdelay
, tot_reads
, tot_writes
,
343 tot_reads
+ tot_writes
);