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 struct per_thread_lock
{
66 } __attribute__((aligned(128))); /* cache-line aligned */
68 static struct per_thread_lock
*per_thread_lock
;
70 static volatile int test_go
, test_stop
;
72 static unsigned long wdelay
;
74 static volatile struct test_array test_array
= { 8 };
76 static unsigned long duration
;
78 /* read-side C.S. duration, in loops */
79 static unsigned long rduration
;
81 static inline void loop_sleep(unsigned long l
)
87 static int verbose_mode
;
89 #define printf_verbose(fmt, args...) \
96 * returns 0 if test should end.
98 static int test_duration_write(void)
103 static int test_duration_read(void)
108 static unsigned long long __thread nr_writes
;
109 static unsigned long long __thread nr_reads
;
111 static unsigned long long __attribute__((aligned(128))) *tot_nr_writes
;
112 static unsigned long long __attribute__((aligned(128))) *tot_nr_reads
;
114 static unsigned int nr_readers
;
115 static unsigned int nr_writers
;
117 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
119 void rcu_copy_mutex_lock(void)
122 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
124 perror("Error in pthread mutex lock");
129 void rcu_copy_mutex_unlock(void)
133 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
135 perror("Error in pthread mutex unlock");
140 void *thr_reader(void *data
)
142 unsigned long tidx
= (unsigned long)data
;
144 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
145 "reader", pthread_self(), (unsigned long)gettid());
152 pthread_mutex_lock(&per_thread_lock
[tidx
].lock
);
153 assert(test_array
.a
== 8);
154 if (unlikely(rduration
))
155 loop_sleep(rduration
);
156 pthread_mutex_unlock(&per_thread_lock
[tidx
].lock
);
158 if (unlikely(!test_duration_read()))
162 tot_nr_reads
[tidx
] = nr_reads
;
163 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
164 "reader", pthread_self(), (unsigned long)gettid());
169 void *thr_writer(void *data
)
171 unsigned long wtidx
= (unsigned long)data
;
174 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
175 "writer", pthread_self(), (unsigned long)gettid());
183 for (tidx
= 0; tidx
< nr_readers
; tidx
++) {
184 pthread_mutex_lock(&per_thread_lock
[tidx
].lock
);
188 for (tidx
= nr_readers
- 1; tidx
>= 0; tidx
--) {
189 pthread_mutex_unlock(&per_thread_lock
[tidx
].lock
);
192 if (unlikely(!test_duration_write()))
194 if (unlikely(wdelay
))
198 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
199 "writer", pthread_self(), (unsigned long)gettid());
200 tot_nr_writes
[wtidx
] = nr_writes
;
204 void show_usage(int argc
, char **argv
)
206 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
208 printf(" [-r] [-w] (yield reader and/or writer)");
210 printf(" [-d delay] (writer period (us))");
211 printf(" [-c duration] (reader C.S. duration (in loops))");
212 printf(" [-v] (verbose output)");
213 printf(" [-a cpu#] [-a cpu#]... (affinity)");
219 int main(int argc
, char **argv
)
222 pthread_t
*tid_reader
, *tid_writer
;
224 unsigned long long *count_reader
, *count_writer
;
225 unsigned long long tot_reads
= 0, tot_writes
= 0;
227 int use_affinity
= 0;
230 show_usage(argc
, argv
);
235 err
= sscanf(argv
[1], "%u", &nr_readers
);
237 show_usage(argc
, argv
);
241 err
= sscanf(argv
[2], "%u", &nr_writers
);
243 show_usage(argc
, argv
);
247 err
= sscanf(argv
[3], "%lu", &duration
);
249 show_usage(argc
, argv
);
255 for (i
= 4; i
< argc
; i
++) {
256 if (argv
[i
][0] != '-')
258 switch (argv
[i
][1]) {
261 yield_active
|= YIELD_READ
;
264 yield_active
|= YIELD_WRITE
;
269 show_usage(argc
, argv
);
273 CPU_SET(a
, &affinity
);
275 printf_verbose("Adding CPU %d affinity\n", a
);
279 show_usage(argc
, argv
);
282 rduration
= atol(argv
[++i
]);
286 show_usage(argc
, argv
);
289 wdelay
= atol(argv
[++i
]);
297 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
298 duration
, nr_readers
, nr_writers
);
299 printf_verbose("Writer delay : %lu loops.\n", wdelay
);
300 printf_verbose("Reader duration : %lu loops.\n", rduration
);
301 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
302 "main", pthread_self(), (unsigned long)gettid());
305 && sched_setaffinity(0, sizeof(affinity
), &affinity
) < 0) {
306 perror("sched_setaffinity");
310 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
311 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
312 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
313 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
314 tot_nr_reads
= malloc(sizeof(*tot_nr_reads
) * nr_readers
);
315 tot_nr_writes
= malloc(sizeof(*tot_nr_writes
) * nr_writers
);
316 per_thread_lock
= malloc(sizeof(*per_thread_lock
) * nr_readers
);
318 for (i
= 0; i
< nr_readers
; i
++) {
319 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
324 for (i
= 0; i
< nr_writers
; i
++) {
325 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
339 for (i
= 0; i
< nr_readers
; i
++) {
340 err
= pthread_join(tid_reader
[i
], &tret
);
343 tot_reads
+= tot_nr_reads
[i
];
345 for (i
= 0; i
< nr_writers
; i
++) {
346 err
= pthread_join(tid_writer
[i
], &tret
);
349 tot_writes
+= tot_nr_writes
[i
];
352 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads
,
354 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
356 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
357 argv
[0], duration
, nr_readers
, rduration
,
358 nr_writers
, wdelay
, tot_reads
, tot_writes
,
359 tot_reads
+ tot_writes
);
367 free(per_thread_lock
);