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