Add -c option for read-side C.S. length to tests
[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
8b632bab
MD
74/* read-side C.S. duration, in us */
75static unsigned long rduration;
76
51299083
MD
77/*
78 * returns 0 if test should end.
79 */
80static int test_duration_write(void)
81{
82 return !test_stop;
83}
84
85static int test_duration_read(void)
86{
87 return !test_stop;
88}
89
90static unsigned long long __thread nr_writes;
91static unsigned long long __thread nr_reads;
92
93static unsigned long long __attribute__((aligned(128))) *tot_nr_writes;
94static unsigned long long __attribute__((aligned(128))) *tot_nr_reads;
95
96static unsigned int nr_readers;
97static unsigned int nr_writers;
98
99pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
100
101void rcu_copy_mutex_lock(void)
102{
103 int ret;
104 ret = pthread_mutex_lock(&rcu_copy_mutex);
105 if (ret) {
106 perror("Error in pthread mutex lock");
107 exit(-1);
108 }
109}
110
111void rcu_copy_mutex_unlock(void)
112{
113 int ret;
114
115 ret = pthread_mutex_unlock(&rcu_copy_mutex);
116 if (ret) {
117 perror("Error in pthread mutex unlock");
118 exit(-1);
119 }
120}
121
122void *thr_reader(void *data)
123{
124 unsigned long tidx = (unsigned long)data;
125
126 printf("thread_begin %s, thread id : %lx, tid %lu\n",
127 "reader", pthread_self(), (unsigned long)gettid());
128
129 while (!test_go)
130 {
131 }
132
133 for (;;) {
134 pthread_mutex_lock(&lock);
135 assert(test_array.a == 8);
8b632bab
MD
136 if (unlikely(rduration))
137 usleep(rduration);
51299083
MD
138 pthread_mutex_unlock(&lock);
139 nr_reads++;
140 if (unlikely(!test_duration_read()))
141 break;
142 }
143
144 tot_nr_reads[tidx] = nr_reads;
145 printf("thread_end %s, thread id : %lx, tid %lu\n",
146 "reader", pthread_self(), (unsigned long)gettid());
147 return ((void*)1);
148
149}
150
151void *thr_writer(void *data)
152{
153 unsigned long wtidx = (unsigned long)data;
154
155 printf("thread_begin %s, thread id : %lx, tid %lu\n",
156 "writer", pthread_self(), (unsigned long)gettid());
157
158 while (!test_go)
159 {
160 }
161 smp_mb();
162
163 for (;;) {
164 pthread_mutex_lock(&lock);
165 test_array.a = 0;
166 test_array.a = 8;
167 pthread_mutex_unlock(&lock);
168 nr_writes++;
169 if (unlikely(!test_duration_write()))
170 break;
171 if (unlikely(wdelay))
172 usleep(wdelay);
173 }
174
175 printf("thread_end %s, thread id : %lx, tid %lu\n",
176 "writer", pthread_self(), (unsigned long)gettid());
177 tot_nr_writes[wtidx] = nr_writes;
178 return ((void*)2);
179}
180
181void show_usage(int argc, char **argv)
182{
183 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
184#ifdef DEBUG_YIELD
185 printf(" [-r] [-w] (yield reader and/or writer)");
186#endif
187 printf(" [-d delay] (writer period (us))");
8b632bab 188 printf(" [-c duration] (reader C.S. duration (us))");
51299083
MD
189 printf(" [-a cpu#] [-a cpu#]... (affinity)");
190 printf("\n");
191}
192
193cpu_set_t affinity;
194
195int main(int argc, char **argv)
196{
197 int err;
198 pthread_t *tid_reader, *tid_writer;
199 void *tret;
200 unsigned long long *count_reader, *count_writer;
201 unsigned long long tot_reads = 0, tot_writes = 0;
202 int i, a;
203 int use_affinity = 0;
204
205 if (argc < 4) {
206 show_usage(argc, argv);
207 return -1;
208 }
209 smp_mb();
210
211 err = sscanf(argv[1], "%u", &nr_readers);
212 if (err != 1) {
213 show_usage(argc, argv);
214 return -1;
215 }
216
217 err = sscanf(argv[2], "%u", &nr_writers);
218 if (err != 1) {
219 show_usage(argc, argv);
220 return -1;
221 }
222
223 err = sscanf(argv[3], "%lu", &duration);
224 if (err != 1) {
225 show_usage(argc, argv);
226 return -1;
227 }
228
229 CPU_ZERO(&affinity);
230
231 for (i = 4; i < argc; i++) {
232 if (argv[i][0] != '-')
233 continue;
234 switch (argv[i][1]) {
235#ifdef DEBUG_YIELD
236 case 'r':
237 yield_active |= YIELD_READ;
238 break;
239 case 'w':
240 yield_active |= YIELD_WRITE;
241 break;
242#endif
243 case 'a':
244 if (argc < i + 2) {
245 show_usage(argc, argv);
246 return -1;
247 }
248 a = atoi(argv[++i]);
249 CPU_SET(a, &affinity);
250 use_affinity = 1;
251 printf("Adding CPU %d affinity\n", a);
252 break;
8b632bab
MD
253 case 'c':
254 if (argc < i + 2) {
255 show_usage(argc, argv);
256 return -1;
257 }
258 rduration = atoi(argv[++i]);
259 break;
51299083
MD
260 case 'd':
261 if (argc < i + 2) {
262 show_usage(argc, argv);
263 return -1;
264 }
265 wdelay = atoi(argv[++i]);
266 break;
267 }
268 }
269
270 printf("running test for %lu seconds, %u readers, %u writers.\n",
271 duration, nr_readers, nr_writers);
272 printf("Writer delay : %u us.\n", wdelay);
273 printf("thread %-6s, thread id : %lx, tid %lu\n",
274 "main", pthread_self(), (unsigned long)gettid());
275
276 if (use_affinity
277 && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) {
278 perror("sched_setaffinity");
279 exit(-1);
280 }
281
282 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
283 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
284 count_reader = malloc(sizeof(*count_reader) * nr_readers);
285 count_writer = malloc(sizeof(*count_writer) * nr_writers);
286 tot_nr_reads = malloc(sizeof(*tot_nr_reads) * nr_readers);
287 tot_nr_writes = malloc(sizeof(*tot_nr_writes) * nr_writers);
288
289 for (i = 0; i < nr_readers; i++) {
290 err = pthread_create(&tid_reader[i], NULL, thr_reader,
291 (void *)(long)i);
292 if (err != 0)
293 exit(1);
294 }
295 for (i = 0; i < nr_writers; i++) {
296 err = pthread_create(&tid_writer[i], NULL, thr_writer,
297 (void *)(long)i);
298 if (err != 0)
299 exit(1);
300 }
301
302 smp_mb();
303
304 test_go = 1;
305
306 sleep(duration);
307
308 test_stop = 1;
309
310 for (i = 0; i < nr_readers; i++) {
311 err = pthread_join(tid_reader[i], &tret);
312 if (err != 0)
313 exit(1);
314 tot_reads += tot_nr_reads[i];
315 }
316 for (i = 0; i < nr_writers; i++) {
317 err = pthread_join(tid_writer[i], &tret);
318 if (err != 0)
319 exit(1);
320 tot_writes += tot_nr_writes[i];
321 }
322
323 printf("total number of reads : %llu, writes %llu\n", tot_reads,
324 tot_writes);
325 free(tid_reader);
326 free(tid_writer);
327 free(count_reader);
328 free(count_writer);
329 free(tot_nr_reads);
330 free(tot_nr_writes);
331 return 0;
332}
This page took 0.033587 seconds and 4 git commands to generate.