Update default value for batch size
[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 #if defined(_syscall0)
42 _syscall0(pid_t, gettid)
43 #elif defined(__NR_gettid)
44 static inline pid_t gettid(void)
45 {
46 return syscall(__NR_gettid);
47 }
48 #else
49 #warning "use pid as tid"
50 static 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
63 struct test_array {
64 int a;
65 };
66
67 struct per_thread_lock {
68 pthread_mutex_t lock;
69 } __attribute__((aligned(CACHE_LINE_SIZE))); /* cache-line aligned */
70
71 static struct per_thread_lock *per_thread_lock;
72
73 static volatile int test_go, test_stop;
74
75 static unsigned long wdelay;
76
77 static volatile struct test_array test_array = { 8 };
78
79 static unsigned long duration;
80
81 /* read-side C.S. duration, in loops */
82 static unsigned long rduration;
83
84 static inline void loop_sleep(unsigned long l)
85 {
86 while(l-- != 0)
87 cpu_relax();
88 }
89
90 static int verbose_mode;
91
92 #define printf_verbose(fmt, args...) \
93 do { \
94 if (verbose_mode) \
95 printf(fmt, args); \
96 } while (0)
97
98 /*
99 * returns 0 if test should end.
100 */
101 static int test_duration_write(void)
102 {
103 return !test_stop;
104 }
105
106 static int test_duration_read(void)
107 {
108 return !test_stop;
109 }
110
111 static unsigned long long __thread nr_writes;
112 static unsigned long long __thread nr_reads;
113
114 static
115 unsigned long long __attribute__((aligned(CACHE_LINE_SIZE))) *tot_nr_writes;
116 static
117 unsigned long long __attribute__((aligned(CACHE_LINE_SIZE))) *tot_nr_reads;
118
119 static unsigned int nr_readers;
120 static unsigned int nr_writers;
121
122 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
123
124 void 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
134 void 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
145 void *thr_reader(void *data)
146 {
147 unsigned long tidx = (unsigned long)data;
148
149 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
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);
159 if (unlikely(rduration))
160 loop_sleep(rduration);
161 pthread_mutex_unlock(&per_thread_lock[tidx].lock);
162 nr_reads++;
163 if (unlikely(!test_duration_read()))
164 break;
165 }
166
167 tot_nr_reads[tidx] = nr_reads;
168 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
169 "reader", pthread_self(), (unsigned long)gettid());
170 return ((void*)1);
171
172 }
173
174 void *thr_writer(void *data)
175 {
176 unsigned long wtidx = (unsigned long)data;
177 long tidx;
178
179 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
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 }
191 test_array.a = 0;
192 test_array.a = 8;
193 for (tidx = (long)nr_readers - 1; tidx >= 0; tidx--) {
194 pthread_mutex_unlock(&per_thread_lock[tidx].lock);
195 }
196 nr_writes++;
197 if (unlikely(!test_duration_write()))
198 break;
199 if (unlikely(wdelay))
200 loop_sleep(wdelay);
201 }
202
203 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
204 "writer", pthread_self(), (unsigned long)gettid());
205 tot_nr_writes[wtidx] = nr_writes;
206 return ((void*)2);
207 }
208
209 void 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))");
216 printf(" [-c duration] (reader C.S. duration (in loops))");
217 printf(" [-v] (verbose output)");
218 printf(" [-a cpu#] [-a cpu#]... (affinity)");
219 printf("\n");
220 }
221
222 cpu_set_t affinity;
223
224 int 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;
231 int i, a;
232 int use_affinity = 0;
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
258 CPU_ZERO(&affinity);
259
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
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;
280 printf_verbose("Adding CPU %d affinity\n", a);
281 break;
282 case 'c':
283 if (argc < i + 2) {
284 show_usage(argc, argv);
285 return -1;
286 }
287 rduration = atol(argv[++i]);
288 break;
289 case 'd':
290 if (argc < i + 2) {
291 show_usage(argc, argv);
292 return -1;
293 }
294 wdelay = atol(argv[++i]);
295 break;
296 case 'v':
297 verbose_mode = 1;
298 break;
299 }
300 }
301
302 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
303 duration, nr_readers, nr_writers);
304 printf_verbose("Writer delay : %lu loops.\n", wdelay);
305 printf_verbose("Reader duration : %lu loops.\n", rduration);
306 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
307 "main", pthread_self(), (unsigned long)gettid());
308
309 if (use_affinity
310 && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) {
311 perror("sched_setaffinity");
312 exit(-1);
313 }
314
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
336 smp_mb();
337
338 test_go = 1;
339
340 sleep(duration);
341
342 test_stop = 1;
343
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 }
356
357 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
358 tot_writes);
359 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
360 "nr_writers %3u "
361 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
362 argv[0], duration, nr_readers, rduration,
363 nr_writers, wdelay, tot_reads, tot_writes,
364 tot_reads + tot_writes);
365
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.038948 seconds and 4 git commands to generate.