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