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>
34 #if defined(_syscall0)
35 _syscall0(pid_t
, gettid
)
36 #elif defined(__NR_gettid)
37 static inline pid_t
gettid(void)
39 return syscall(__NR_gettid
);
42 #warning "use pid as tid"
43 static inline pid_t
gettid(void)
49 #ifndef DYNAMIC_LINK_TEST
52 #define debug_yield_read()
60 pthread_rwlock_t lock
= PTHREAD_RWLOCK_INITIALIZER
;
62 static volatile int test_go
;
66 static struct test_array test_array
= { 8 };
68 static unsigned long duration
;
69 static time_t start_time
;
70 static unsigned long __thread duration_interval
;
71 #define DURATION_TEST_DELAY_WRITE 4
72 #define DURATION_TEST_DELAY_READ 100
75 * returns 0 if test should end.
77 static int test_duration_write(void)
79 if (duration_interval
++ >= DURATION_TEST_DELAY_WRITE
) {
80 duration_interval
= 0;
81 if (time(NULL
) - start_time
>= duration
)
87 static int test_duration_read(void)
89 if (duration_interval
++ >= DURATION_TEST_DELAY_READ
) {
90 duration_interval
= 0;
91 if (time(NULL
) - start_time
>= duration
)
97 static unsigned long long __thread nr_writes
;
98 static unsigned long long __thread nr_reads
;
100 static unsigned int nr_readers
;
101 static unsigned int nr_writers
;
103 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
105 void rcu_copy_mutex_lock(void)
108 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
110 perror("Error in pthread mutex lock");
115 void rcu_copy_mutex_unlock(void)
119 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
121 perror("Error in pthread mutex unlock");
126 void *thr_reader(void *_count
)
128 unsigned long long *count
= _count
;
130 printf("thread_begin %s, thread id : %lx, tid %lu\n",
131 "reader", pthread_self(), (unsigned long)gettid());
138 pthread_rwlock_rdlock(&lock
);
139 assert(test_array
.a
== 8);
140 pthread_rwlock_unlock(&lock
);
142 if (!test_duration_read())
147 printf("thread_end %s, thread id : %lx, tid %lu\n",
148 "reader", pthread_self(), (unsigned long)gettid());
153 void *thr_writer(void *_count
)
155 unsigned long long *count
= _count
;
157 printf("thread_begin %s, thread id : %lx, tid %lu\n",
158 "writer", pthread_self(), (unsigned long)gettid());
165 pthread_rwlock_wrlock(&lock
);
167 pthread_rwlock_unlock(&lock
);
169 if (!test_duration_write())
175 printf("thread_end %s, thread id : %lx, tid %lu\n",
176 "writer", pthread_self(), (unsigned long)gettid());
181 void show_usage(int argc
, char **argv
)
183 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
185 printf(" [-r] [-w] (yield reader and/or writer)");
187 printf(" [-d delay] (writer period)");
191 int main(int argc
, char **argv
)
194 pthread_t
*tid_reader
, *tid_writer
;
196 unsigned long long *count_reader
, *count_writer
;
197 unsigned long long tot_reads
= 0, tot_writes
= 0;
201 show_usage(argc
, argv
);
205 err
= sscanf(argv
[1], "%u", &nr_readers
);
207 show_usage(argc
, argv
);
211 err
= sscanf(argv
[2], "%u", &nr_writers
);
213 show_usage(argc
, argv
);
217 err
= sscanf(argv
[3], "%lu", &duration
);
219 show_usage(argc
, argv
);
223 for (i
= 4; i
< argc
; i
++) {
224 if (argv
[i
][0] != '-')
226 switch (argv
[i
][1]) {
229 yield_active
|= YIELD_READ
;
232 yield_active
|= YIELD_WRITE
;
237 show_usage(argc
, argv
);
240 wdelay
= atoi(argv
[++i
]);
245 printf("running test for %lu seconds, %u readers, %u writers.\n",
246 duration
, nr_readers
, nr_writers
);
247 printf("Writer delay : %u us.\n", wdelay
);
248 start_time
= time(NULL
);
249 printf("thread %-6s, thread id : %lx, tid %lu\n",
250 "main", pthread_self(), (unsigned long)gettid());
252 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
253 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
254 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
255 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
257 for (i
= 0; i
< nr_readers
; i
++) {
258 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
263 for (i
= 0; i
< nr_writers
; i
++) {
264 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
272 for (i
= 0; i
< nr_readers
; i
++) {
273 err
= pthread_join(tid_reader
[i
], &tret
);
276 tot_reads
+= count_reader
[i
];
278 for (i
= 0; i
< nr_writers
; i
++) {
279 err
= pthread_join(tid_writer
[i
], &tret
);
282 tot_writes
+= count_writer
[i
];
285 printf("total number of reads : %llu, writes %llu\n", tot_reads
,