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 struct per_thread_lock
{
66 } __attribute__((aligned(128))); /* cache-line aligned */
68 static struct per_thread_lock
*per_thread_lock
;
70 static volatile int test_go
, test_stop
;
74 static volatile struct test_array test_array
= { 8 };
76 static unsigned long duration
;
78 /* read-side C.S. duration, in us */
79 static unsigned long rduration
;
82 * returns 0 if test should end.
84 static int test_duration_write(void)
89 static int test_duration_read(void)
94 static unsigned long long __thread nr_writes
;
95 static unsigned long long __thread nr_reads
;
97 static unsigned long long __attribute__((aligned(128))) *tot_nr_writes
;
98 static unsigned long long __attribute__((aligned(128))) *tot_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 *data
)
128 unsigned long tidx
= (unsigned long)data
;
130 printf("thread_begin %s, thread id : %lx, tid %lu\n",
131 "reader", pthread_self(), (unsigned long)gettid());
138 pthread_mutex_lock(&per_thread_lock
[tidx
].lock
);
139 assert(test_array
.a
== 8);
140 if (unlikely(rduration
))
142 pthread_mutex_unlock(&per_thread_lock
[tidx
].lock
);
144 if (unlikely(!test_duration_read()))
148 tot_nr_reads
[tidx
] = nr_reads
;
149 printf("thread_end %s, thread id : %lx, tid %lu\n",
150 "reader", pthread_self(), (unsigned long)gettid());
155 void *thr_writer(void *data
)
157 unsigned long wtidx
= (unsigned long)data
;
160 printf("thread_begin %s, thread id : %lx, tid %lu\n",
161 "writer", pthread_self(), (unsigned long)gettid());
169 for (tidx
= 0; tidx
< nr_readers
; tidx
++) {
170 pthread_mutex_lock(&per_thread_lock
[tidx
].lock
);
174 for (tidx
= nr_readers
- 1; tidx
>= 0; tidx
--) {
175 pthread_mutex_unlock(&per_thread_lock
[tidx
].lock
);
178 if (unlikely(!test_duration_write()))
180 if (unlikely(wdelay
))
184 printf("thread_end %s, thread id : %lx, tid %lu\n",
185 "writer", pthread_self(), (unsigned long)gettid());
186 tot_nr_writes
[wtidx
] = nr_writes
;
190 void show_usage(int argc
, char **argv
)
192 printf("Usage : %s nr_readers nr_writers duration (s)", argv
[0]);
194 printf(" [-r] [-w] (yield reader and/or writer)");
196 printf(" [-d delay] (writer period (us))");
197 printf(" [-c duration] (reader C.S. duration (us))");
198 printf(" [-a cpu#] [-a cpu#]... (affinity)");
204 int main(int argc
, char **argv
)
207 pthread_t
*tid_reader
, *tid_writer
;
209 unsigned long long *count_reader
, *count_writer
;
210 unsigned long long tot_reads
= 0, tot_writes
= 0;
212 int use_affinity
= 0;
215 show_usage(argc
, argv
);
220 err
= sscanf(argv
[1], "%u", &nr_readers
);
222 show_usage(argc
, argv
);
226 err
= sscanf(argv
[2], "%u", &nr_writers
);
228 show_usage(argc
, argv
);
232 err
= sscanf(argv
[3], "%lu", &duration
);
234 show_usage(argc
, argv
);
240 for (i
= 4; i
< argc
; i
++) {
241 if (argv
[i
][0] != '-')
243 switch (argv
[i
][1]) {
246 yield_active
|= YIELD_READ
;
249 yield_active
|= YIELD_WRITE
;
254 show_usage(argc
, argv
);
258 CPU_SET(a
, &affinity
);
260 printf("Adding CPU %d affinity\n", a
);
264 show_usage(argc
, argv
);
267 rduration
= atoi(argv
[++i
]);
271 show_usage(argc
, argv
);
274 wdelay
= atoi(argv
[++i
]);
279 printf("running test for %lu seconds, %u readers, %u writers.\n",
280 duration
, nr_readers
, nr_writers
);
281 printf("Writer delay : %u us.\n", wdelay
);
282 printf("thread %-6s, thread id : %lx, tid %lu\n",
283 "main", pthread_self(), (unsigned long)gettid());
286 && sched_setaffinity(0, sizeof(affinity
), &affinity
) < 0) {
287 perror("sched_setaffinity");
291 tid_reader
= malloc(sizeof(*tid_reader
) * nr_readers
);
292 tid_writer
= malloc(sizeof(*tid_writer
) * nr_writers
);
293 count_reader
= malloc(sizeof(*count_reader
) * nr_readers
);
294 count_writer
= malloc(sizeof(*count_writer
) * nr_writers
);
295 tot_nr_reads
= malloc(sizeof(*tot_nr_reads
) * nr_readers
);
296 tot_nr_writes
= malloc(sizeof(*tot_nr_writes
) * nr_writers
);
297 per_thread_lock
= malloc(sizeof(*per_thread_lock
) * nr_readers
);
299 for (i
= 0; i
< nr_readers
; i
++) {
300 err
= pthread_create(&tid_reader
[i
], NULL
, thr_reader
,
305 for (i
= 0; i
< nr_writers
; i
++) {
306 err
= pthread_create(&tid_writer
[i
], NULL
, thr_writer
,
320 for (i
= 0; i
< nr_readers
; i
++) {
321 err
= pthread_join(tid_reader
[i
], &tret
);
324 tot_reads
+= tot_nr_reads
[i
];
326 for (i
= 0; i
< nr_writers
; i
++) {
327 err
= pthread_join(tid_writer
[i
], &tret
);
330 tot_writes
+= tot_nr_writes
[i
];
333 printf("total number of reads : %llu, writes %llu\n", tot_reads
,
341 free(per_thread_lock
);