Delay reader in loops, not us
[urcu.git] / test_mutex.c
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)
41 static inline pid_t gettid(void)
42 {
43 return syscall(__NR_gettid);
44 }
45 #else
46 #warning "use pid as tid"
47 static 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
60 struct test_array {
61 int a;
62 };
63
64 static pthread_mutex_t lock;
65
66 static volatile int test_go, test_stop;
67
68 static int wdelay;
69
70 static volatile struct test_array test_array = { 8 };
71
72 static unsigned long duration;
73
74 /* read-side C.S. duration, in loops */
75 static unsigned long rduration;
76
77 static inline void loop_sleep(unsigned long l)
78 {
79 while(l-- != 0)
80 cpu_relax();
81 }
82
83 /*
84 * returns 0 if test should end.
85 */
86 static int test_duration_write(void)
87 {
88 return !test_stop;
89 }
90
91 static int test_duration_read(void)
92 {
93 return !test_stop;
94 }
95
96 static unsigned long long __thread nr_writes;
97 static unsigned long long __thread nr_reads;
98
99 static unsigned long long __attribute__((aligned(128))) *tot_nr_writes;
100 static unsigned long long __attribute__((aligned(128))) *tot_nr_reads;
101
102 static unsigned int nr_readers;
103 static unsigned int nr_writers;
104
105 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
106
107 void 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
117 void 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
128 void *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);
142 if (unlikely(rduration))
143 loop_sleep(rduration);
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
157 void *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
187 void 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))");
194 printf(" [-c duration] (reader C.S. duration (in loops))");
195 printf(" [-a cpu#] [-a cpu#]... (affinity)");
196 printf("\n");
197 }
198
199 cpu_set_t affinity;
200
201 int 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;
259 case 'c':
260 if (argc < i + 2) {
261 show_usage(argc, argv);
262 return -1;
263 }
264 rduration = atoi(argv[++i]);
265 break;
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);
279 printf("thread %-6s, thread id : %lx, tid %lu\n",
280 "main", pthread_self(), (unsigned long)gettid());
281
282 if (use_affinity
283 && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) {
284 perror("sched_setaffinity");
285 exit(-1);
286 }
287
288 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
289 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
290 count_reader = malloc(sizeof(*count_reader) * nr_readers);
291 count_writer = malloc(sizeof(*count_writer) * nr_writers);
292 tot_nr_reads = malloc(sizeof(*tot_nr_reads) * nr_readers);
293 tot_nr_writes = malloc(sizeof(*tot_nr_writes) * nr_writers);
294
295 for (i = 0; i < nr_readers; i++) {
296 err = pthread_create(&tid_reader[i], NULL, thr_reader,
297 (void *)(long)i);
298 if (err != 0)
299 exit(1);
300 }
301 for (i = 0; i < nr_writers; i++) {
302 err = pthread_create(&tid_writer[i], NULL, thr_writer,
303 (void *)(long)i);
304 if (err != 0)
305 exit(1);
306 }
307
308 smp_mb();
309
310 test_go = 1;
311
312 sleep(duration);
313
314 test_stop = 1;
315
316 for (i = 0; i < nr_readers; i++) {
317 err = pthread_join(tid_reader[i], &tret);
318 if (err != 0)
319 exit(1);
320 tot_reads += tot_nr_reads[i];
321 }
322 for (i = 0; i < nr_writers; i++) {
323 err = pthread_join(tid_writer[i], &tret);
324 if (err != 0)
325 exit(1);
326 tot_writes += tot_nr_writes[i];
327 }
328
329 printf("total number of reads : %llu, writes %llu\n", tot_reads,
330 tot_writes);
331 free(tid_reader);
332 free(tid_writer);
333 free(count_reader);
334 free(count_writer);
335 free(tot_nr_reads);
336 free(tot_nr_writes);
337 return 0;
338 }
This page took 0.034975 seconds and 4 git commands to generate.