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