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