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