Rename CONFIG_URCU_AVOID_SIGNALS to URCU_MB, and README updates
[urcu.git] / tests / test_perthreadlock.c
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
23 #define _GNU_SOURCE
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>
34 #include <sched.h>
35
36 #include "../arch.h"
37
38 /* Make this big enough to include the POWER5+ L3 cacheline size of 256B */
39 #define CACHE_LINE_SIZE 4096
40
41 /* hardcoded number of CPUs */
42 #define NR_CPUS 16384
43
44 #if defined(_syscall0)
45 _syscall0(pid_t, gettid)
46 #elif defined(__NR_gettid)
47 static inline pid_t gettid(void)
48 {
49 return syscall(__NR_gettid);
50 }
51 #else
52 #warning "use pid as tid"
53 static 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
64 #include "../urcu.h"
65
66 struct test_array {
67 int a;
68 };
69
70 struct per_thread_lock {
71 pthread_mutex_t lock;
72 } __attribute__((aligned(CACHE_LINE_SIZE))); /* cache-line aligned */
73
74 static struct per_thread_lock *per_thread_lock;
75
76 static volatile int test_go, test_stop;
77
78 static unsigned long wdelay;
79
80 static volatile struct test_array test_array = { 8 };
81
82 static unsigned long duration;
83
84 /* read-side C.S. duration, in loops */
85 static unsigned long rduration;
86
87 static inline void loop_sleep(unsigned long l)
88 {
89 while(l-- != 0)
90 cpu_relax();
91 }
92
93 static int verbose_mode;
94
95 #define printf_verbose(fmt, args...) \
96 do { \
97 if (verbose_mode) \
98 printf(fmt, args); \
99 } while (0)
100
101 static unsigned int cpu_affinities[NR_CPUS];
102 static unsigned int next_aff = 0;
103 static int use_affinity = 0;
104
105 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
106
107 static void set_affinity(void)
108 {
109 cpu_set_t mask;
110 int cpu;
111 int ret;
112
113 if (!use_affinity)
114 return;
115
116 ret = pthread_mutex_lock(&affinity_mutex);
117 if (ret) {
118 perror("Error in pthread mutex lock");
119 exit(-1);
120 }
121 cpu = cpu_affinities[next_aff++];
122 ret = pthread_mutex_unlock(&affinity_mutex);
123 if (ret) {
124 perror("Error in pthread mutex unlock");
125 exit(-1);
126 }
127 CPU_ZERO(&mask);
128 CPU_SET(cpu, &mask);
129 sched_setaffinity(0, sizeof(mask), &mask);
130 }
131
132 /*
133 * returns 0 if test should end.
134 */
135 static int test_duration_write(void)
136 {
137 return !test_stop;
138 }
139
140 static int test_duration_read(void)
141 {
142 return !test_stop;
143 }
144
145 static unsigned long long __thread nr_writes;
146 static unsigned long long __thread nr_reads;
147
148 static
149 unsigned long long __attribute__((aligned(CACHE_LINE_SIZE))) *tot_nr_writes;
150 static
151 unsigned long long __attribute__((aligned(CACHE_LINE_SIZE))) *tot_nr_reads;
152
153 static unsigned int nr_readers;
154 static unsigned int nr_writers;
155
156 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
157
158 void 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
168 void 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
179 void *thr_reader(void *data)
180 {
181 unsigned long tidx = (unsigned long)data;
182
183 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
184 "reader", pthread_self(), (unsigned long)gettid());
185
186 set_affinity();
187
188 while (!test_go)
189 {
190 }
191
192 for (;;) {
193 pthread_mutex_lock(&per_thread_lock[tidx].lock);
194 assert(test_array.a == 8);
195 if (unlikely(rduration))
196 loop_sleep(rduration);
197 pthread_mutex_unlock(&per_thread_lock[tidx].lock);
198 nr_reads++;
199 if (unlikely(!test_duration_read()))
200 break;
201 }
202
203 tot_nr_reads[tidx] = nr_reads;
204 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
205 "reader", pthread_self(), (unsigned long)gettid());
206 return ((void*)1);
207
208 }
209
210 void *thr_writer(void *data)
211 {
212 unsigned long wtidx = (unsigned long)data;
213 long tidx;
214
215 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
216 "writer", pthread_self(), (unsigned long)gettid());
217
218 set_affinity();
219
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 }
229 test_array.a = 0;
230 test_array.a = 8;
231 for (tidx = (long)nr_readers - 1; tidx >= 0; tidx--) {
232 pthread_mutex_unlock(&per_thread_lock[tidx].lock);
233 }
234 nr_writes++;
235 if (unlikely(!test_duration_write()))
236 break;
237 if (unlikely(wdelay))
238 loop_sleep(wdelay);
239 }
240
241 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
242 "writer", pthread_self(), (unsigned long)gettid());
243 tot_nr_writes[wtidx] = nr_writes;
244 return ((void*)2);
245 }
246
247 void 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))");
254 printf(" [-c duration] (reader C.S. duration (in loops))");
255 printf(" [-v] (verbose output)");
256 printf(" [-a cpu#] [-a cpu#]... (affinity)");
257 printf("\n");
258 }
259
260 int 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;
267 int i, a;
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
305 case 'a':
306 if (argc < i + 2) {
307 show_usage(argc, argv);
308 return -1;
309 }
310 a = atoi(argv[++i]);
311 cpu_affinities[next_aff++] = a;
312 use_affinity = 1;
313 printf_verbose("Adding CPU %d affinity\n", a);
314 break;
315 case 'c':
316 if (argc < i + 2) {
317 show_usage(argc, argv);
318 return -1;
319 }
320 rduration = atol(argv[++i]);
321 break;
322 case 'd':
323 if (argc < i + 2) {
324 show_usage(argc, argv);
325 return -1;
326 }
327 wdelay = atol(argv[++i]);
328 break;
329 case 'v':
330 verbose_mode = 1;
331 break;
332 }
333 }
334
335 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
336 duration, nr_readers, nr_writers);
337 printf_verbose("Writer delay : %lu loops.\n", wdelay);
338 printf_verbose("Reader duration : %lu loops.\n", rduration);
339 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
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
350 next_aff = 0;
351
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
365 smp_mb();
366
367 test_go = 1;
368
369 sleep(duration);
370
371 test_stop = 1;
372
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 }
385
386 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
387 tot_writes);
388 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
389 "nr_writers %3u "
390 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
391 argv[0], duration, nr_readers, rduration,
392 nr_writers, wdelay, tot_reads, tot_writes,
393 tot_reads + tot_writes);
394
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.03637 seconds and 4 git commands to generate.