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 struct per_thread_lock
{
64 } __attribute__((aligned(128))); /* cache-line aligned */
66 static struct per_thread_lock
*per_thread_lock
;
68 static volatile int test_go
;
72 static struct test_array test_array
= { 8 };
74 static unsigned long duration
;
75 static time_t start_time
;
76 static unsigned long __thread duration_interval
;
77 #define DURATION_TEST_DELAY_WRITE 4
78 #define DURATION_TEST_DELAY_READ 100
81 * returns 0 if test should end.
83 static int test_duration_write(void)
85 if (duration_interval
++ >= DURATION_TEST_DELAY_WRITE
) {
86 duration_interval
= 0;
87 if (time(NULL
) - start_time
>= duration
)
93 static int test_duration_read(void)
95 if (duration_interval
++ >= DURATION_TEST_DELAY_READ
) {
96 duration_interval
= 0;
97 if (time(NULL
) - start_time
>= duration
)
103 static unsigned long long __thread nr_writes
;
104 static unsigned long long __thread nr_reads
;
106 static unsigned long long __attribute__((aligned(128))) *tot_nr_writes
;
107 static unsigned long long __attribute__((aligned(128))) *tot_nr_reads
;
109 static unsigned int nr_readers
;
110 static unsigned int nr_writers
;
112 pthread_mutex_t rcu_copy_mutex
= PTHREAD_MUTEX_INITIALIZER
;
114 void rcu_copy_mutex_lock(void)
117 ret
= pthread_mutex_lock(&rcu_copy_mutex
);
119 perror("Error in pthread mutex lock");
124 void rcu_copy_mutex_unlock(void)
128 ret
= pthread_mutex_unlock(&rcu_copy_mutex
);
130 perror("Error in pthread mutex unlock");
135 void *thr_reader(void *data
)
137 unsigned long tidx
= (unsigned long)data
;
139 printf("thread_begin %s, thread id : %lx, tid %lu\n",
140 "reader", pthread_self(), (unsigned long)gettid());
147 pthread_mutex_lock(&per_thread_lock
[tidx
].lock
);
148 assert(test_array
.a
== 8);
149 pthread_mutex_unlock(&per_thread_lock
[tidx
].lock
);
151 if (!test_duration_read())
155 tot_nr_reads
[tidx
] = nr_reads
;
156 printf("thread_end %s, thread id : %lx, tid %lu\n",
157 "reader", pthread_self(), (unsigned long)gettid());
162 void *thr_writer(void *data
)
164 unsigned long wtidx
= (unsigned long)data
;
167 printf("thread_begin %s, thread id : %lx, tid %lu\n",
168 "writer", pthread_self(), (unsigned long)gettid());
176 for (tidx
= 0; tidx
< nr_readers
; tidx
++) {
177 pthread_mutex_lock(&per_thread_lock
[tidx
].lock
);
180 for (tidx
= nr_readers
- 1; tidx
>= 0; tidx
--) {
181 pthread_mutex_unlock(&per_thread_lock
[tidx
].lock
);
184 if (!test_duration_write())
190 printf("thread_end %s, thread id : %lx, tid %lu\n",
191 "writer", pthread_self(), (unsigned long)gettid());
192 tot_nr_writes
[wtidx
] = nr_writes
;
196 void show_usage(int argc
, char **argv
)
198 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
200 printf(" [-r] [-w] (yield reader and/or writer)");
202 printf(" [-d delay] (writer period (us))");
206 int main(int argc
, char **argv
)
209 pthread_t
*tid_reader
, *tid_writer
;
211 unsigned long long *count_reader
, *count_writer
;
212 unsigned long long tot_reads
= 0, tot_writes
= 0;
216 show_usage(argc
, argv
);
221 err
= sscanf(argv
[1], "%u", &nr_readers
);
223 show_usage(argc
, argv
);
227 err
= sscanf(argv
[2], "%u", &nr_writers
);
229 show_usage(argc
, argv
);
233 err
= sscanf(argv
[3], "%lu", &duration
);
235 show_usage(argc
, argv
);
239 for (i
= 4; i
< argc
; i
++) {
240 if (argv
[i
][0] != '-')
242 switch (argv
[i
][1]) {
245 yield_active
|= YIELD_READ
;
248 yield_active
|= YIELD_WRITE
;
253 show_usage(argc
, argv
);
256 wdelay
= atoi(argv
[++i
]);
261 printf("running test for %lu seconds, %u readers, %u writers.\n",
262 duration
, nr_readers
, nr_writers
);
263 printf("Writer delay : %u us.\n", wdelay
);
264 printf("thread %-6s, thread id : %lx, tid %lu\n",
265 "main", pthread_self(), (unsigned long)gettid());
267 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
268 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
269 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
270 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
271 tot_nr_reads
= malloc(sizeof(*tot_nr_reads
) * nr_readers
);
272 tot_nr_writes
= malloc(sizeof(*tot_nr_writes
) * nr_writers
);
273 per_thread_lock
= malloc(sizeof(*per_thread_lock
) * nr_readers
);
275 for (i
= 0; i
< nr_readers
; i
++) {
276 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
281 for (i
= 0; i
< nr_writers
; i
++) {
282 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
288 start_time
= time(NULL
);
292 for (i
= 0; i
< nr_readers
; i
++) {
293 err
= pthread_join(tid_reader
[i
], &tret
);
296 tot_reads
+= tot_nr_reads
[i
];
298 for (i
= 0; i
< nr_writers
; i
++) {
299 err
= pthread_join(tid_writer
[i
], &tret
);
302 tot_writes
+= tot_nr_writes
[i
];
305 printf("total number of reads : %llu, writes %llu\n", tot_reads
,
313 free(per_thread_lock
);