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