Using AM_PROG_MKDIR_P for compatibility
[urcu.git] / tests / 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 35
ec4e58a3 36#include <urcu/arch.h>
0ee8bb0a 37
2a7ac59d
MD
38/* hardcoded number of CPUs */
39#define NR_CPUS 16384
40
0ee8bb0a
MD
41#if defined(_syscall0)
42_syscall0(pid_t, gettid)
43#elif defined(__NR_gettid)
44static inline pid_t gettid(void)
45{
46 return syscall(__NR_gettid);
47}
48#else
49#warning "use pid as tid"
50static inline pid_t gettid(void)
51{
52 return getpid();
53}
54#endif
55
56#ifndef DYNAMIC_LINK_TEST
57#define _LGPL_SOURCE
58#else
59#define debug_yield_read()
60#endif
ec4e58a3 61#include <urcu.h>
0ee8bb0a
MD
62
63struct test_array {
64 int a;
65};
66
67struct per_thread_lock {
68 pthread_mutex_t lock;
55271c13 69} __attribute__((aligned(CACHE_LINE_SIZE))); /* cache-line aligned */
0ee8bb0a
MD
70
71static struct per_thread_lock *per_thread_lock;
72
78efb485 73static volatile int test_go, test_stop;
0ee8bb0a 74
9e31d0f0 75static unsigned long wdelay;
0ee8bb0a 76
2b4e4125 77static volatile struct test_array test_array = { 8 };
0ee8bb0a
MD
78
79static unsigned long duration;
0ee8bb0a 80
daddf5b0 81/* read-side C.S. duration, in loops */
8b632bab
MD
82static unsigned long rduration;
83
daddf5b0
MD
84static inline void loop_sleep(unsigned long l)
85{
86 while(l-- != 0)
87 cpu_relax();
88}
89
4bad7d45
MD
90static int verbose_mode;
91
92#define printf_verbose(fmt, args...) \
93 do { \
94 if (verbose_mode) \
95 printf(fmt, args); \
96 } while (0)
97
2a7ac59d
MD
98static unsigned int cpu_affinities[NR_CPUS];
99static unsigned int next_aff = 0;
100static int use_affinity = 0;
101
6af882ba
MD
102pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
103
2a7ac59d
MD
104static void set_affinity(void)
105{
106 cpu_set_t mask;
107 int cpu;
6af882ba 108 int ret;
2a7ac59d
MD
109
110 if (!use_affinity)
111 return;
112
6af882ba
MD
113 ret = pthread_mutex_lock(&affinity_mutex);
114 if (ret) {
115 perror("Error in pthread mutex lock");
116 exit(-1);
117 }
2a7ac59d 118 cpu = cpu_affinities[next_aff++];
6af882ba
MD
119 ret = pthread_mutex_unlock(&affinity_mutex);
120 if (ret) {
121 perror("Error in pthread mutex unlock");
122 exit(-1);
123 }
2a7ac59d
MD
124 CPU_ZERO(&mask);
125 CPU_SET(cpu, &mask);
126 sched_setaffinity(0, sizeof(mask), &mask);
127}
128
0ee8bb0a
MD
129/*
130 * returns 0 if test should end.
131 */
132static int test_duration_write(void)
133{
78efb485 134 return !test_stop;
0ee8bb0a
MD
135}
136
137static int test_duration_read(void)
138{
78efb485 139 return !test_stop;
0ee8bb0a
MD
140}
141
142static unsigned long long __thread nr_writes;
143static unsigned long long __thread nr_reads;
144
55271c13
MD
145static
146unsigned long long __attribute__((aligned(CACHE_LINE_SIZE))) *tot_nr_writes;
147static
148unsigned long long __attribute__((aligned(CACHE_LINE_SIZE))) *tot_nr_reads;
0ee8bb0a
MD
149
150static unsigned int nr_readers;
151static unsigned int nr_writers;
152
153pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
154
155void rcu_copy_mutex_lock(void)
156{
157 int ret;
158 ret = pthread_mutex_lock(&rcu_copy_mutex);
159 if (ret) {
160 perror("Error in pthread mutex lock");
161 exit(-1);
162 }
163}
164
165void rcu_copy_mutex_unlock(void)
166{
167 int ret;
168
169 ret = pthread_mutex_unlock(&rcu_copy_mutex);
170 if (ret) {
171 perror("Error in pthread mutex unlock");
172 exit(-1);
173 }
174}
175
176void *thr_reader(void *data)
177{
178 unsigned long tidx = (unsigned long)data;
179
4bad7d45 180 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
0ee8bb0a
MD
181 "reader", pthread_self(), (unsigned long)gettid());
182
2a7ac59d
MD
183 set_affinity();
184
0ee8bb0a
MD
185 while (!test_go)
186 {
187 }
188
189 for (;;) {
190 pthread_mutex_lock(&per_thread_lock[tidx].lock);
191 assert(test_array.a == 8);
8b632bab 192 if (unlikely(rduration))
daddf5b0 193 loop_sleep(rduration);
0ee8bb0a
MD
194 pthread_mutex_unlock(&per_thread_lock[tidx].lock);
195 nr_reads++;
59d5a406 196 if (unlikely(!test_duration_read()))
0ee8bb0a
MD
197 break;
198 }
199
200 tot_nr_reads[tidx] = nr_reads;
cda3c71d 201 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
0ee8bb0a
MD
202 "reader", pthread_self(), (unsigned long)gettid());
203 return ((void*)1);
204
205}
206
207void *thr_writer(void *data)
208{
209 unsigned long wtidx = (unsigned long)data;
210 long tidx;
211
4bad7d45 212 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
0ee8bb0a
MD
213 "writer", pthread_self(), (unsigned long)gettid());
214
2a7ac59d
MD
215 set_affinity();
216
0ee8bb0a
MD
217 while (!test_go)
218 {
219 }
220 smp_mb();
221
222 for (;;) {
223 for (tidx = 0; tidx < nr_readers; tidx++) {
224 pthread_mutex_lock(&per_thread_lock[tidx].lock);
225 }
2b4e4125 226 test_array.a = 0;
0ee8bb0a 227 test_array.a = 8;
f120cc10 228 for (tidx = (long)nr_readers - 1; tidx >= 0; tidx--) {
0ee8bb0a
MD
229 pthread_mutex_unlock(&per_thread_lock[tidx].lock);
230 }
231 nr_writes++;
59d5a406 232 if (unlikely(!test_duration_write()))
0ee8bb0a 233 break;
59d5a406 234 if (unlikely(wdelay))
9e31d0f0 235 loop_sleep(wdelay);
0ee8bb0a
MD
236 }
237
4bad7d45 238 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
0ee8bb0a
MD
239 "writer", pthread_self(), (unsigned long)gettid());
240 tot_nr_writes[wtidx] = nr_writes;
241 return ((void*)2);
242}
243
244void show_usage(int argc, char **argv)
245{
246 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
247#ifdef DEBUG_YIELD
248 printf(" [-r] [-w] (yield reader and/or writer)");
249#endif
250 printf(" [-d delay] (writer period (us))");
daddf5b0 251 printf(" [-c duration] (reader C.S. duration (in loops))");
4bad7d45 252 printf(" [-v] (verbose output)");
9e97e478 253 printf(" [-a cpu#] [-a cpu#]... (affinity)");
0ee8bb0a
MD
254 printf("\n");
255}
256
257int main(int argc, char **argv)
258{
259 int err;
260 pthread_t *tid_reader, *tid_writer;
261 void *tret;
262 unsigned long long *count_reader, *count_writer;
263 unsigned long long tot_reads = 0, tot_writes = 0;
9e97e478 264 int i, a;
0ee8bb0a
MD
265
266 if (argc < 4) {
267 show_usage(argc, argv);
268 return -1;
269 }
270 smp_mb();
271
272 err = sscanf(argv[1], "%u", &nr_readers);
273 if (err != 1) {
274 show_usage(argc, argv);
275 return -1;
276 }
277
278 err = sscanf(argv[2], "%u", &nr_writers);
279 if (err != 1) {
280 show_usage(argc, argv);
281 return -1;
282 }
283
284 err = sscanf(argv[3], "%lu", &duration);
285 if (err != 1) {
286 show_usage(argc, argv);
287 return -1;
288 }
289
290 for (i = 4; i < argc; i++) {
291 if (argv[i][0] != '-')
292 continue;
293 switch (argv[i][1]) {
294#ifdef DEBUG_YIELD
295 case 'r':
296 yield_active |= YIELD_READ;
297 break;
298 case 'w':
299 yield_active |= YIELD_WRITE;
300 break;
301#endif
9e97e478
MD
302 case 'a':
303 if (argc < i + 2) {
304 show_usage(argc, argv);
305 return -1;
306 }
307 a = atoi(argv[++i]);
2a7ac59d 308 cpu_affinities[next_aff++] = a;
9e97e478 309 use_affinity = 1;
4bad7d45 310 printf_verbose("Adding CPU %d affinity\n", a);
9e97e478 311 break;
8b632bab
MD
312 case 'c':
313 if (argc < i + 2) {
314 show_usage(argc, argv);
315 return -1;
316 }
9e31d0f0 317 rduration = atol(argv[++i]);
8b632bab 318 break;
0ee8bb0a
MD
319 case 'd':
320 if (argc < i + 2) {
321 show_usage(argc, argv);
322 return -1;
323 }
9e31d0f0 324 wdelay = atol(argv[++i]);
0ee8bb0a 325 break;
4bad7d45
MD
326 case 'v':
327 verbose_mode = 1;
328 break;
0ee8bb0a
MD
329 }
330 }
331
4bad7d45 332 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
0ee8bb0a 333 duration, nr_readers, nr_writers);
9e31d0f0 334 printf_verbose("Writer delay : %lu loops.\n", wdelay);
4bad7d45
MD
335 printf_verbose("Reader duration : %lu loops.\n", rduration);
336 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
0ee8bb0a
MD
337 "main", pthread_self(), (unsigned long)gettid());
338
339 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
340 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
341 count_reader = malloc(sizeof(*count_reader) * nr_readers);
342 count_writer = malloc(sizeof(*count_writer) * nr_writers);
343 tot_nr_reads = malloc(sizeof(*tot_nr_reads) * nr_readers);
344 tot_nr_writes = malloc(sizeof(*tot_nr_writes) * nr_writers);
345 per_thread_lock = malloc(sizeof(*per_thread_lock) * nr_readers);
346
2a7ac59d
MD
347 next_aff = 0;
348
0ee8bb0a
MD
349 for (i = 0; i < nr_readers; i++) {
350 err = pthread_create(&tid_reader[i], NULL, thr_reader,
351 (void *)(long)i);
352 if (err != 0)
353 exit(1);
354 }
355 for (i = 0; i < nr_writers; i++) {
356 err = pthread_create(&tid_writer[i], NULL, thr_writer,
357 (void *)(long)i);
358 if (err != 0)
359 exit(1);
360 }
361
0ee8bb0a
MD
362 smp_mb();
363
78efb485
MD
364 test_go = 1;
365
366 sleep(duration);
367
368 test_stop = 1;
369
0ee8bb0a
MD
370 for (i = 0; i < nr_readers; i++) {
371 err = pthread_join(tid_reader[i], &tret);
372 if (err != 0)
373 exit(1);
374 tot_reads += tot_nr_reads[i];
375 }
376 for (i = 0; i < nr_writers; i++) {
377 err = pthread_join(tid_writer[i], &tret);
378 if (err != 0)
379 exit(1);
380 tot_writes += tot_nr_writes[i];
381 }
4bad7d45
MD
382
383 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
0ee8bb0a 384 tot_writes);
6a190115 385 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
cda3c71d 386 "nr_writers %3u "
9e31d0f0 387 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
4bad7d45
MD
388 argv[0], duration, nr_readers, rduration,
389 nr_writers, wdelay, tot_reads, tot_writes,
390 tot_reads + tot_writes);
391
0ee8bb0a
MD
392 free(tid_reader);
393 free(tid_writer);
394 free(count_reader);
395 free(count_writer);
396 free(tot_nr_reads);
397 free(tot_nr_writes);
398 free(per_thread_lock);
399 return 0;
400}
This page took 0.039417 seconds and 4 git commands to generate.