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
;
75 * returns 0 if test should end.
77 static int test_duration_write(void)
82 static int test_duration_read(void)
87 static unsigned long long __thread nr_writes
;
88 static unsigned long long __thread nr_reads
;
90 static unsigned int nr_readers
;
91 static unsigned int nr_writers
;
93 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
95 void rcu_copy_mutex_lock(void)
98 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
100 perror("Error in pthread mutex lock");
105 void rcu_copy_mutex_unlock(void)
109 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
111 perror("Error in pthread mutex unlock");
116 void *thr_reader(void *_count
)
118 unsigned long long *count
= _count
;
120 printf("thread_begin %s, thread id : %lx, tid %lu\n",
121 "reader", pthread_self(), (unsigned long)gettid());
128 pthread_rwlock_rdlock(&lock
);
129 assert(test_array
.a
== 8);
130 pthread_rwlock_unlock(&lock
);
132 if (unlikely(!test_duration_read()))
137 printf("thread_end %s, thread id : %lx, tid %lu\n",
138 "reader", pthread_self(), (unsigned long)gettid());
143 void *thr_writer(void *_count
)
145 unsigned long long *count
= _count
;
147 printf("thread_begin %s, thread id : %lx, tid %lu\n",
148 "writer", pthread_self(), (unsigned long)gettid());
156 pthread_rwlock_wrlock(&lock
);
159 pthread_rwlock_unlock(&lock
);
161 if (unlikely(!test_duration_write()))
163 if (unlikely(wdelay
))
167 printf("thread_end %s, thread id : %lx, tid %lu\n",
168 "writer", pthread_self(), (unsigned long)gettid());
173 void show_usage(int argc
, char **argv
)
175 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
177 printf(" [-r] [-w] (yield reader and/or writer)");
179 printf(" [-d delay] (writer period (us))");
180 printf(" [-a cpu#] [-a cpu#]... (affinity)");
186 int main(int argc
, char **argv
)
189 pthread_t
*tid_reader
, *tid_writer
;
191 unsigned long long *count_reader
, *count_writer
;
192 unsigned long long tot_reads
= 0, tot_writes
= 0;
194 int use_affinity
= 0;
197 show_usage(argc
, argv
);
202 err
= sscanf(argv
[1], "%u", &nr_readers
);
204 show_usage(argc
, argv
);
208 err
= sscanf(argv
[2], "%u", &nr_writers
);
210 show_usage(argc
, argv
);
214 err
= sscanf(argv
[3], "%lu", &duration
);
216 show_usage(argc
, argv
);
222 for (i
= 4; i
< argc
; i
++) {
223 if (argv
[i
][0] != '-')
225 switch (argv
[i
][1]) {
228 yield_active
|= YIELD_READ
;
231 yield_active
|= YIELD_WRITE
;
236 show_usage(argc
, argv
);
240 CPU_SET(a
, &affinity
);
242 printf("Adding CPU %d affinity\n", a
);
246 show_usage(argc
, argv
);
249 wdelay
= atoi(argv
[++i
]);
254 printf("running test for %lu seconds, %u readers, %u writers.\n",
255 duration
, nr_readers
, nr_writers
);
256 printf("Writer delay : %u us.\n", wdelay
);
257 printf("thread %-6s, thread id : %lx, tid %lu\n",
258 "main", pthread_self(), (unsigned long)gettid());
260 && sched_setaffinity(0, sizeof(affinity
), &affinity
) < 0) {
261 perror("sched_setaffinity");
265 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
266 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
267 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
268 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
270 for (i
= 0; i
< nr_readers
; i
++) {
271 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
276 for (i
= 0; i
< nr_writers
; i
++) {
277 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
291 for (i
= 0; i
< nr_readers
; i
++) {
292 err
= pthread_join(tid_reader
[i
], &tret
);
295 tot_reads
+= count_reader
[i
];
297 for (i
= 0; i
< nr_writers
; i
++) {
298 err
= pthread_join(tid_writer
[i
], &tret
);
301 tot_writes
+= count_writer
[i
];
304 printf("total number of reads : %llu, writes %llu\n", tot_reads
,