Print reader duration
[urcu.git] / test_mutex.c
CommitLineData
51299083
MD
1/*
2 * test_urcu.c
3 *
4 * Userspace RCU library - test program
5 *
6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 *
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.
12 *
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.
17 *
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.
21 */
22
23#define _GNU_SOURCE
24#include <stdio.h>
25#include <pthread.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/types.h>
29#include <sys/wait.h>
30#include <unistd.h>
31#include <stdio.h>
32#include <assert.h>
33#include <sys/syscall.h>
34#include <sched.h>
35
36#include "arch.h"
37
38#if defined(_syscall0)
39_syscall0(pid_t, gettid)
40#elif defined(__NR_gettid)
41static inline pid_t gettid(void)
42{
43 return syscall(__NR_gettid);
44}
45#else
46#warning "use pid as tid"
47static inline pid_t gettid(void)
48{
49 return getpid();
50}
51#endif
52
53#ifndef DYNAMIC_LINK_TEST
54#define _LGPL_SOURCE
55#else
56#define debug_yield_read()
57#endif
58#include "urcu.h"
59
60struct test_array {
61 int a;
62};
63
64static pthread_mutex_t lock;
65
66static volatile int test_go, test_stop;
67
68static int wdelay;
69
70static volatile struct test_array test_array = { 8 };
71
72static unsigned long duration;
73
daddf5b0 74/* read-side C.S. duration, in loops */
8b632bab
MD
75static unsigned long rduration;
76
daddf5b0
MD
77static inline void loop_sleep(unsigned long l)
78{
79 while(l-- != 0)
80 cpu_relax();
81}
82
51299083
MD
83/*
84 * returns 0 if test should end.
85 */
86static int test_duration_write(void)
87{
88 return !test_stop;
89}
90
91static int test_duration_read(void)
92{
93 return !test_stop;
94}
95
96static unsigned long long __thread nr_writes;
97static unsigned long long __thread nr_reads;
98
99static unsigned long long __attribute__((aligned(128))) *tot_nr_writes;
100static unsigned long long __attribute__((aligned(128))) *tot_nr_reads;
101
102static unsigned int nr_readers;
103static unsigned int nr_writers;
104
105pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
106
107void rcu_copy_mutex_lock(void)
108{
109 int ret;
110 ret = pthread_mutex_lock(&rcu_copy_mutex);
111 if (ret) {
112 perror("Error in pthread mutex lock");
113 exit(-1);
114 }
115}
116
117void rcu_copy_mutex_unlock(void)
118{
119 int ret;
120
121 ret = pthread_mutex_unlock(&rcu_copy_mutex);
122 if (ret) {
123 perror("Error in pthread mutex unlock");
124 exit(-1);
125 }
126}
127
128void *thr_reader(void *data)
129{
130 unsigned long tidx = (unsigned long)data;
131
132 printf("thread_begin %s, thread id : %lx, tid %lu\n",
133 "reader", pthread_self(), (unsigned long)gettid());
134
135 while (!test_go)
136 {
137 }
138
139 for (;;) {
140 pthread_mutex_lock(&lock);
141 assert(test_array.a == 8);
8b632bab 142 if (unlikely(rduration))
daddf5b0 143 loop_sleep(rduration);
51299083
MD
144 pthread_mutex_unlock(&lock);
145 nr_reads++;
146 if (unlikely(!test_duration_read()))
147 break;
148 }
149
150 tot_nr_reads[tidx] = nr_reads;
151 printf("thread_end %s, thread id : %lx, tid %lu\n",
152 "reader", pthread_self(), (unsigned long)gettid());
153 return ((void*)1);
154
155}
156
157void *thr_writer(void *data)
158{
159 unsigned long wtidx = (unsigned long)data;
160
161 printf("thread_begin %s, thread id : %lx, tid %lu\n",
162 "writer", pthread_self(), (unsigned long)gettid());
163
164 while (!test_go)
165 {
166 }
167 smp_mb();
168
169 for (;;) {
170 pthread_mutex_lock(&lock);
171 test_array.a = 0;
172 test_array.a = 8;
173 pthread_mutex_unlock(&lock);
174 nr_writes++;
175 if (unlikely(!test_duration_write()))
176 break;
177 if (unlikely(wdelay))
178 usleep(wdelay);
179 }
180
181 printf("thread_end %s, thread id : %lx, tid %lu\n",
182 "writer", pthread_self(), (unsigned long)gettid());
183 tot_nr_writes[wtidx] = nr_writes;
184 return ((void*)2);
185}
186
187void show_usage(int argc, char **argv)
188{
189 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
190#ifdef DEBUG_YIELD
191 printf(" [-r] [-w] (yield reader and/or writer)");
192#endif
193 printf(" [-d delay] (writer period (us))");
daddf5b0 194 printf(" [-c duration] (reader C.S. duration (in loops))");
51299083
MD
195 printf(" [-a cpu#] [-a cpu#]... (affinity)");
196 printf("\n");
197}
198
199cpu_set_t affinity;
200
201int main(int argc, char **argv)
202{
203 int err;
204 pthread_t *tid_reader, *tid_writer;
205 void *tret;
206 unsigned long long *count_reader, *count_writer;
207 unsigned long long tot_reads = 0, tot_writes = 0;
208 int i, a;
209 int use_affinity = 0;
210
211 if (argc < 4) {
212 show_usage(argc, argv);
213 return -1;
214 }
215 smp_mb();
216
217 err = sscanf(argv[1], "%u", &nr_readers);
218 if (err != 1) {
219 show_usage(argc, argv);
220 return -1;
221 }
222
223 err = sscanf(argv[2], "%u", &nr_writers);
224 if (err != 1) {
225 show_usage(argc, argv);
226 return -1;
227 }
228
229 err = sscanf(argv[3], "%lu", &duration);
230 if (err != 1) {
231 show_usage(argc, argv);
232 return -1;
233 }
234
235 CPU_ZERO(&affinity);
236
237 for (i = 4; i < argc; i++) {
238 if (argv[i][0] != '-')
239 continue;
240 switch (argv[i][1]) {
241#ifdef DEBUG_YIELD
242 case 'r':
243 yield_active |= YIELD_READ;
244 break;
245 case 'w':
246 yield_active |= YIELD_WRITE;
247 break;
248#endif
249 case 'a':
250 if (argc < i + 2) {
251 show_usage(argc, argv);
252 return -1;
253 }
254 a = atoi(argv[++i]);
255 CPU_SET(a, &affinity);
256 use_affinity = 1;
257 printf("Adding CPU %d affinity\n", a);
258 break;
8b632bab
MD
259 case 'c':
260 if (argc < i + 2) {
261 show_usage(argc, argv);
262 return -1;
263 }
264 rduration = atoi(argv[++i]);
265 break;
51299083
MD
266 case 'd':
267 if (argc < i + 2) {
268 show_usage(argc, argv);
269 return -1;
270 }
271 wdelay = atoi(argv[++i]);
272 break;
273 }
274 }
275
276 printf("running test for %lu seconds, %u readers, %u writers.\n",
277 duration, nr_readers, nr_writers);
278 printf("Writer delay : %u us.\n", wdelay);
900e541e 279 printf("Reader duration : %lu loops.\n", rduration);
51299083
MD
280 printf("thread %-6s, thread id : %lx, tid %lu\n",
281 "main", pthread_self(), (unsigned long)gettid());
282
283 if (use_affinity
284 && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) {
285 perror("sched_setaffinity");
286 exit(-1);
287 }
288
289 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
290 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
291 count_reader = malloc(sizeof(*count_reader) * nr_readers);
292 count_writer = malloc(sizeof(*count_writer) * nr_writers);
293 tot_nr_reads = malloc(sizeof(*tot_nr_reads) * nr_readers);
294 tot_nr_writes = malloc(sizeof(*tot_nr_writes) * nr_writers);
295
296 for (i = 0; i < nr_readers; i++) {
297 err = pthread_create(&tid_reader[i], NULL, thr_reader,
298 (void *)(long)i);
299 if (err != 0)
300 exit(1);
301 }
302 for (i = 0; i < nr_writers; i++) {
303 err = pthread_create(&tid_writer[i], NULL, thr_writer,
304 (void *)(long)i);
305 if (err != 0)
306 exit(1);
307 }
308
309 smp_mb();
310
311 test_go = 1;
312
313 sleep(duration);
314
315 test_stop = 1;
316
317 for (i = 0; i < nr_readers; i++) {
318 err = pthread_join(tid_reader[i], &tret);
319 if (err != 0)
320 exit(1);
321 tot_reads += tot_nr_reads[i];
322 }
323 for (i = 0; i < nr_writers; i++) {
324 err = pthread_join(tid_writer[i], &tret);
325 if (err != 0)
326 exit(1);
327 tot_writes += tot_nr_writes[i];
328 }
329
330 printf("total number of reads : %llu, writes %llu\n", tot_reads,
331 tot_writes);
332 free(tid_reader);
333 free(tid_writer);
334 free(count_reader);
335 free(count_writer);
336 free(tot_nr_reads);
337 free(tot_nr_writes);
338 return 0;
339}
This page took 0.035092 seconds and 4 git commands to generate.