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.
27 #include <sys/types.h>
32 #include <sys/syscall.h>
36 #if defined(_syscall0)
37 _syscall0(pid_t
, gettid
)
38 #elif defined(__NR_gettid)
39 static inline pid_t
gettid(void)
41 return syscall(__NR_gettid
);
44 #warning "use pid as tid"
45 static inline pid_t
gettid(void)
51 #ifndef DYNAMIC_LINK_TEST
54 #define debug_yield_read()
62 pthread_rwlock_t lock
= PTHREAD_RWLOCK_INITIALIZER
;
64 static volatile int test_go
, test_stop
;
68 static volatile struct test_array test_array
= { 8 };
70 static unsigned long duration
;
73 * returns 0 if test should end.
75 static int test_duration_write(void)
80 static int test_duration_read(void)
85 static unsigned long long __thread nr_writes
;
86 static unsigned long long __thread nr_reads
;
88 static unsigned int nr_readers
;
89 static unsigned int nr_writers
;
91 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
93 void rcu_copy_mutex_lock(void)
96 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
98 perror("Error in pthread mutex lock");
103 void rcu_copy_mutex_unlock(void)
107 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
109 perror("Error in pthread mutex unlock");
114 void *thr_reader(void *_count
)
116 unsigned long long *count
= _count
;
118 printf("thread_begin %s, thread id : %lx, tid %lu\n",
119 "reader", pthread_self(), (unsigned long)gettid());
126 pthread_rwlock_rdlock(&lock
);
127 assert(test_array
.a
== 8);
128 pthread_rwlock_unlock(&lock
);
130 if (unlikely(!test_duration_read()))
135 printf("thread_end %s, thread id : %lx, tid %lu\n",
136 "reader", pthread_self(), (unsigned long)gettid());
141 void *thr_writer(void *_count
)
143 unsigned long long *count
= _count
;
145 printf("thread_begin %s, thread id : %lx, tid %lu\n",
146 "writer", pthread_self(), (unsigned long)gettid());
154 pthread_rwlock_wrlock(&lock
);
157 pthread_rwlock_unlock(&lock
);
159 if (unlikely(!test_duration_write()))
161 if (unlikely(wdelay
))
165 printf("thread_end %s, thread id : %lx, tid %lu\n",
166 "writer", pthread_self(), (unsigned long)gettid());
171 void show_usage(int argc
, char **argv
)
173 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
175 printf(" [-r] [-w] (yield reader and/or writer)");
177 printf(" [-d delay] (writer period (us))");
181 int main(int argc
, char **argv
)
184 pthread_t
*tid_reader
, *tid_writer
;
186 unsigned long long *count_reader
, *count_writer
;
187 unsigned long long tot_reads
= 0, tot_writes
= 0;
191 show_usage(argc
, argv
);
196 err
= sscanf(argv
[1], "%u", &nr_readers
);
198 show_usage(argc
, argv
);
202 err
= sscanf(argv
[2], "%u", &nr_writers
);
204 show_usage(argc
, argv
);
208 err
= sscanf(argv
[3], "%lu", &duration
);
210 show_usage(argc
, argv
);
214 for (i
= 4; i
< argc
; i
++) {
215 if (argv
[i
][0] != '-')
217 switch (argv
[i
][1]) {
220 yield_active
|= YIELD_READ
;
223 yield_active
|= YIELD_WRITE
;
228 show_usage(argc
, argv
);
231 wdelay
= atoi(argv
[++i
]);
236 printf("running test for %lu seconds, %u readers, %u writers.\n",
237 duration
, nr_readers
, nr_writers
);
238 printf("Writer delay : %u us.\n", wdelay
);
239 printf("thread %-6s, thread id : %lx, tid %lu\n",
240 "main", pthread_self(), (unsigned long)gettid());
242 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
243 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
244 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
245 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
247 for (i
= 0; i
< nr_readers
; i
++) {
248 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
253 for (i
= 0; i
< nr_writers
; i
++) {
254 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
268 for (i
= 0; i
< nr_readers
; i
++) {
269 err
= pthread_join(tid_reader
[i
], &tret
);
272 tot_reads
+= count_reader
[i
];
274 for (i
= 0; i
< nr_writers
; i
++) {
275 err
= pthread_join(tid_writer
[i
], &tret
);
278 tot_writes
+= count_writer
[i
];
281 printf("total number of reads : %llu, writes %llu\n", tot_reads
,