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