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
;
68 static struct test_array test_array
= { 8 };
70 static unsigned long duration
;
71 static time_t start_time
;
72 static unsigned long __thread duration_interval
;
73 #define DURATION_TEST_DELAY_WRITE 4
74 #define DURATION_TEST_DELAY_READ 100
77 * returns 0 if test should end.
79 static int test_duration_write(void)
81 if (duration_interval
++ >= DURATION_TEST_DELAY_WRITE
) {
82 duration_interval
= 0;
83 if (time(NULL
) - start_time
>= duration
)
89 static int test_duration_read(void)
91 if (duration_interval
++ >= DURATION_TEST_DELAY_READ
) {
92 duration_interval
= 0;
93 if (time(NULL
) - start_time
>= duration
)
99 static unsigned long long __thread nr_writes
;
100 static unsigned long long __thread nr_reads
;
102 static unsigned int nr_readers
;
103 static unsigned int nr_writers
;
105 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
107 void rcu_copy_mutex_lock(void)
110 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
112 perror("Error in pthread mutex lock");
117 void rcu_copy_mutex_unlock(void)
121 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
123 perror("Error in pthread mutex unlock");
128 void *thr_reader(void *_count
)
130 unsigned long long *count
= _count
;
132 printf("thread_begin %s, thread id : %lx, tid %lu\n",
133 "reader", pthread_self(), (unsigned long)gettid());
140 pthread_rwlock_rdlock(&lock
);
141 assert(test_array
.a
== 8);
142 pthread_rwlock_unlock(&lock
);
144 if (!test_duration_read())
149 printf("thread_end %s, thread id : %lx, tid %lu\n",
150 "reader", pthread_self(), (unsigned long)gettid());
155 void *thr_writer(void *_count
)
157 unsigned long long *count
= _count
;
159 printf("thread_begin %s, thread id : %lx, tid %lu\n",
160 "writer", pthread_self(), (unsigned long)gettid());
168 pthread_rwlock_wrlock(&lock
);
170 pthread_rwlock_unlock(&lock
);
172 if (!test_duration_write())
178 printf("thread_end %s, thread id : %lx, tid %lu\n",
179 "writer", pthread_self(), (unsigned long)gettid());
184 void show_usage(int argc
, char **argv
)
186 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
188 printf(" [-r] [-w] (yield reader and/or writer)");
190 printf(" [-d delay] (writer period (us))");
194 int main(int argc
, char **argv
)
197 pthread_t
*tid_reader
, *tid_writer
;
199 unsigned long long *count_reader
, *count_writer
;
200 unsigned long long tot_reads
= 0, tot_writes
= 0;
204 show_usage(argc
, argv
);
209 err
= sscanf(argv
[1], "%u", &nr_readers
);
211 show_usage(argc
, argv
);
215 err
= sscanf(argv
[2], "%u", &nr_writers
);
217 show_usage(argc
, argv
);
221 err
= sscanf(argv
[3], "%lu", &duration
);
223 show_usage(argc
, argv
);
227 for (i
= 4; i
< argc
; i
++) {
228 if (argv
[i
][0] != '-')
230 switch (argv
[i
][1]) {
233 yield_active
|= YIELD_READ
;
236 yield_active
|= YIELD_WRITE
;
241 show_usage(argc
, argv
);
244 wdelay
= atoi(argv
[++i
]);
249 printf("running test for %lu seconds, %u readers, %u writers.\n",
250 duration
, nr_readers
, nr_writers
);
251 printf("Writer delay : %u us.\n", wdelay
);
252 printf("thread %-6s, thread id : %lx, tid %lu\n",
253 "main", pthread_self(), (unsigned long)gettid());
255 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
256 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
257 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
258 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
260 for (i
= 0; i
< nr_readers
; i
++) {
261 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
266 for (i
= 0; i
< nr_writers
; i
++) {
267 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
273 start_time
= time(NULL
);
277 for (i
= 0; i
< nr_readers
; i
++) {
278 err
= pthread_join(tid_reader
[i
], &tret
);
281 tot_reads
+= count_reader
[i
];
283 for (i
= 0; i
< nr_writers
; i
++) {
284 err
= pthread_join(tid_writer
[i
], &tret
);
287 tot_writes
+= count_writer
[i
];
290 printf("total number of reads : %llu, writes %llu\n", tot_reads
,