Deal with POWER5+ 256B L3 cachefalse sharing for per thread lock
[urcu.git] / test_qsbr.c
CommitLineData
061159a0
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
061159a0
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>
061159a0 35
7685d963
MD
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
061159a0
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#define _LGPL_SOURCE
57#include "urcu-qsbr.h"
58
59struct test_array {
60 int a;
61};
62
78efb485 63static volatile int test_go, test_stop;
061159a0 64
9e31d0f0 65static unsigned long wdelay;
061159a0
MD
66
67static struct test_array *test_rcu_pointer;
68
69static unsigned long duration;
061159a0 70
daddf5b0 71/* read-side C.S. duration, in loops */
8b632bab
MD
72static unsigned long rduration;
73
daddf5b0
MD
74static inline void loop_sleep(unsigned long l)
75{
76 while(l-- != 0)
77 cpu_relax();
78}
79
4bad7d45
MD
80static int verbose_mode;
81
82#define printf_verbose(fmt, args...) \
83 do { \
84 if (verbose_mode) \
85 printf(fmt, args); \
86 } while (0)
87
061159a0
MD
88/*
89 * returns 0 if test should end.
90 */
91static int test_duration_write(void)
92{
78efb485 93 return !test_stop;
061159a0
MD
94}
95
96static int test_duration_read(void)
97{
78efb485 98 return !test_stop;
061159a0
MD
99}
100
101static unsigned long long __thread nr_writes;
102static unsigned long long __thread nr_reads;
103
104static unsigned int nr_readers;
105static unsigned int nr_writers;
106
107pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
108
109void rcu_copy_mutex_lock(void)
110{
111 int ret;
112 ret = pthread_mutex_lock(&rcu_copy_mutex);
113 if (ret) {
114 perror("Error in pthread mutex lock");
115 exit(-1);
116 }
117}
118
119void rcu_copy_mutex_unlock(void)
120{
121 int ret;
122
123 ret = pthread_mutex_unlock(&rcu_copy_mutex);
124 if (ret) {
125 perror("Error in pthread mutex unlock");
126 exit(-1);
127 }
128}
129
130/*
131 * malloc/free are reusing memory areas too quickly, which does not let us
132 * test races appropriately. Use a large circular array for allocations.
133 * ARRAY_SIZE is larger than nr_writers, which insures we never run over our tail.
134 */
135#define ARRAY_SIZE (1048576 * nr_writers)
136#define ARRAY_POISON 0xDEADBEEF
137static int array_index;
138static struct test_array *test_array;
139
140static struct test_array *test_array_alloc(void)
141{
142 struct test_array *ret;
143 int index;
144
145 rcu_copy_mutex_lock();
146 index = array_index % ARRAY_SIZE;
147 assert(test_array[index].a == ARRAY_POISON ||
148 test_array[index].a == 0);
149 ret = &test_array[index];
150 array_index++;
151 if (array_index == ARRAY_SIZE)
152 array_index = 0;
153 rcu_copy_mutex_unlock();
154 return ret;
155}
156
157static void test_array_free(struct test_array *ptr)
158{
159 if (!ptr)
160 return;
161 rcu_copy_mutex_lock();
162 ptr->a = ARRAY_POISON;
163 rcu_copy_mutex_unlock();
164}
165
166void *thr_reader(void *_count)
167{
168 unsigned long long *count = _count;
169 struct test_array *local_ptr;
170
4bad7d45 171 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
061159a0
MD
172 "reader", pthread_self(), (unsigned long)gettid());
173
174 rcu_register_thread();
175
176 while (!test_go)
177 {
178 }
7685d963 179 smp_mb();
061159a0
MD
180
181 for (;;) {
182 _rcu_read_lock();
183 local_ptr = _rcu_dereference(test_rcu_pointer);
184 debug_yield_read();
185 if (local_ptr)
186 assert(local_ptr->a == 8);
8b632bab 187 if (unlikely(rduration))
daddf5b0 188 loop_sleep(rduration);
061159a0
MD
189 _rcu_read_unlock();
190 nr_reads++;
78efb485 191 /* QS each 1024 reads */
59d5a406 192 if (unlikely((nr_reads & ((1 << 10) - 1)) == 0))
061159a0 193 _rcu_quiescent_state();
59d5a406 194 if (unlikely(!test_duration_read()))
061159a0
MD
195 break;
196 }
197
198 rcu_unregister_thread();
199
200 *count = nr_reads;
4bad7d45 201 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
061159a0
MD
202 "reader", pthread_self(), (unsigned long)gettid());
203 return ((void*)1);
204
205}
206
207void *thr_writer(void *_count)
208{
209 unsigned long long *count = _count;
210 struct test_array *new, *old;
211
4bad7d45 212 printf_verbose("thread_begin %s, thread id : %lx, tid %lu\n",
061159a0
MD
213 "writer", pthread_self(), (unsigned long)gettid());
214
215 while (!test_go)
216 {
217 }
7685d963 218 smp_mb();
061159a0
MD
219
220 for (;;) {
221 new = test_array_alloc();
222 rcu_copy_mutex_lock();
223 old = test_rcu_pointer;
224 if (old)
225 assert(old->a == 8);
226 new->a = 8;
227 old = _rcu_publish_content(&test_rcu_pointer, new);
228 rcu_copy_mutex_unlock();
229 /* can be done after unlock */
230 if (old)
231 old->a = 0;
232 test_array_free(old);
233 nr_writes++;
59d5a406 234 if (unlikely(!test_duration_write()))
061159a0 235 break;
59d5a406 236 if (unlikely(wdelay))
9e31d0f0 237 loop_sleep(wdelay);
061159a0
MD
238 }
239
4bad7d45 240 printf_verbose("thread_end %s, thread id : %lx, tid %lu\n",
061159a0
MD
241 "writer", pthread_self(), (unsigned long)gettid());
242 *count = nr_writes;
243 return ((void*)2);
244}
245
246void show_usage(int argc, char **argv)
247{
248 printf("Usage : %s nr_readers nr_writers duration (s)", argv[0]);
249#ifdef DEBUG_YIELD
250 printf(" [-r] [-w] (yield reader and/or writer)");
251#endif
7685d963 252 printf(" [-d delay] (writer period (us))");
daddf5b0 253 printf(" [-c duration] (reader C.S. duration (in loops))");
4bad7d45 254 printf(" [-v] (verbose output)");
9e97e478 255 printf(" [-a cpu#] [-a cpu#]... (affinity)");
061159a0
MD
256 printf("\n");
257}
258
9e97e478
MD
259cpu_set_t affinity;
260
061159a0
MD
261int main(int argc, char **argv)
262{
263 int err;
264 pthread_t *tid_reader, *tid_writer;
265 void *tret;
266 unsigned long long *count_reader, *count_writer;
267 unsigned long long tot_reads = 0, tot_writes = 0;
9e97e478
MD
268 int i, a;
269 int use_affinity = 0;
061159a0
MD
270
271 if (argc < 4) {
272 show_usage(argc, argv);
273 return -1;
274 }
275
276 err = sscanf(argv[1], "%u", &nr_readers);
277 if (err != 1) {
278 show_usage(argc, argv);
279 return -1;
280 }
281
282 err = sscanf(argv[2], "%u", &nr_writers);
283 if (err != 1) {
284 show_usage(argc, argv);
285 return -1;
286 }
287
288 err = sscanf(argv[3], "%lu", &duration);
289 if (err != 1) {
290 show_usage(argc, argv);
291 return -1;
292 }
293
9e97e478
MD
294 CPU_ZERO(&affinity);
295
061159a0
MD
296 for (i = 4; i < argc; i++) {
297 if (argv[i][0] != '-')
298 continue;
299 switch (argv[i][1]) {
300#ifdef DEBUG_YIELD
301 case 'r':
302 yield_active |= YIELD_READ;
303 break;
304 case 'w':
305 yield_active |= YIELD_WRITE;
306 break;
307#endif
9e97e478
MD
308 case 'a':
309 if (argc < i + 2) {
310 show_usage(argc, argv);
311 return -1;
312 }
313 a = atoi(argv[++i]);
314 CPU_SET(a, &affinity);
315 use_affinity = 1;
4bad7d45 316 printf_verbose("Adding CPU %d affinity\n", a);
9e97e478 317 break;
8b632bab
MD
318 case 'c':
319 if (argc < i + 2) {
320 show_usage(argc, argv);
321 return -1;
322 }
9e31d0f0 323 rduration = atol(argv[++i]);
8b632bab 324 break;
061159a0
MD
325 case 'd':
326 if (argc < i + 2) {
327 show_usage(argc, argv);
328 return -1;
329 }
9e31d0f0 330 wdelay = atol(argv[++i]);
061159a0 331 break;
4bad7d45
MD
332 case 'v':
333 verbose_mode = 1;
334 break;
061159a0
MD
335 }
336 }
337
4bad7d45 338 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
061159a0 339 duration, nr_readers, nr_writers);
9e31d0f0 340 printf_verbose("Writer delay : %lu loops.\n", wdelay);
4bad7d45
MD
341 printf_verbose("Reader duration : %lu loops.\n", rduration);
342 printf_verbose("thread %-6s, thread id : %lx, tid %lu\n",
061159a0
MD
343 "main", pthread_self(), (unsigned long)gettid());
344
9e97e478
MD
345 if (use_affinity
346 && sched_setaffinity(0, sizeof(affinity), &affinity) < 0) {
347 perror("sched_setaffinity");
348 exit(-1);
349 }
350
061159a0
MD
351 test_array = malloc(sizeof(*test_array) * ARRAY_SIZE);
352 tid_reader = malloc(sizeof(*tid_reader) * nr_readers);
353 tid_writer = malloc(sizeof(*tid_writer) * nr_writers);
354 count_reader = malloc(sizeof(*count_reader) * nr_readers);
355 count_writer = malloc(sizeof(*count_writer) * nr_writers);
356
357 for (i = 0; i < nr_readers; i++) {
358 err = pthread_create(&tid_reader[i], NULL, thr_reader,
359 &count_reader[i]);
360 if (err != 0)
361 exit(1);
362 }
363 for (i = 0; i < nr_writers; i++) {
364 err = pthread_create(&tid_writer[i], NULL, thr_writer,
365 &count_writer[i]);
366 if (err != 0)
367 exit(1);
368 }
369
7685d963 370 smp_mb();
061159a0 371
78efb485
MD
372 test_go = 1;
373
374 sleep(duration);
375
376 test_stop = 1;
377
061159a0
MD
378 for (i = 0; i < nr_readers; i++) {
379 err = pthread_join(tid_reader[i], &tret);
380 if (err != 0)
381 exit(1);
382 tot_reads += count_reader[i];
383 }
384 for (i = 0; i < nr_writers; i++) {
385 err = pthread_join(tid_writer[i], &tret);
386 if (err != 0)
387 exit(1);
388 tot_writes += count_writer[i];
389 }
390
4bad7d45 391 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
061159a0 392 tot_writes);
6a190115 393 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu "
cda3c71d 394 "nr_writers %3u "
9e31d0f0 395 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
4bad7d45
MD
396 argv[0], duration, nr_readers, rduration,
397 nr_writers, wdelay, tot_reads, tot_writes,
398 tot_reads + tot_writes);
061159a0
MD
399 test_array_free(test_rcu_pointer);
400 free(test_array);
401 free(tid_reader);
402 free(tid_writer);
403 free(count_reader);
404 free(count_writer);
405 return 0;
406}
This page took 0.03867 seconds and 4 git commands to generate.