cleanup: all functions have declarations (-Wmissing-prototypes)
[urcu.git] / tests / benchmark / test_perthreadlock.c
CommitLineData
0ee8bb0a
MD
1/*
2 * test_urcu.c
3 *
4 * Userspace RCU library - test program
5 *
6982d6d7 6 * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
0ee8bb0a
MD
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#include <stdio.h>
24#include <pthread.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/types.h>
28#include <sys/wait.h>
29#include <unistd.h>
30#include <stdio.h>
31#include <assert.h>
94b343fd 32#include <errno.h>
0ee8bb0a 33
ec4e58a3 34#include <urcu/arch.h>
bd252a04 35#include <urcu/tls-compat.h>
94df6318 36#include "thread-id.h"
65fcc7e9 37
2a7ac59d
MD
38/* hardcoded number of CPUs */
39#define NR_CPUS 16384
40
0ee8bb0a
MD
41#ifndef DYNAMIC_LINK_TEST
42#define _LGPL_SOURCE
0ee8bb0a 43#endif
ec4e58a3 44#include <urcu.h>
0ee8bb0a
MD
45
46struct test_array {
47 int a;
48};
49
50struct per_thread_lock {
51 pthread_mutex_t lock;
06f22bdb 52} __attribute__((aligned(CAA_CACHE_LINE_SIZE))); /* cache-line aligned */
0ee8bb0a
MD
53
54static struct per_thread_lock *per_thread_lock;
55
78efb485 56static volatile int test_go, test_stop;
0ee8bb0a 57
9e31d0f0 58static unsigned long wdelay;
0ee8bb0a 59
2b4e4125 60static volatile struct test_array test_array = { 8 };
0ee8bb0a
MD
61
62static unsigned long duration;
0ee8bb0a 63
daddf5b0 64/* read-side C.S. duration, in loops */
8b632bab
MD
65static unsigned long rduration;
66
31b598e0
MD
67/* write-side C.S. duration, in loops */
68static unsigned long wduration;
69
ab0aacbe 70static inline void loop_sleep(unsigned long loops)
daddf5b0 71{
ab0aacbe 72 while (loops-- != 0)
06f22bdb 73 caa_cpu_relax();
daddf5b0
MD
74}
75
4bad7d45
MD
76static int verbose_mode;
77
78#define printf_verbose(fmt, args...) \
79 do { \
80 if (verbose_mode) \
81 printf(fmt, args); \
82 } while (0)
83
2a7ac59d
MD
84static unsigned int cpu_affinities[NR_CPUS];
85static unsigned int next_aff = 0;
86static int use_affinity = 0;
87
6af882ba
MD
88pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
89
2a7ac59d
MD
90static void set_affinity(void)
91{
95bc7fb9 92#if HAVE_SCHED_SETAFFINITY
2a7ac59d 93 cpu_set_t mask;
95bc7fb9
MD
94 int cpu, ret;
95#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
96
97 if (!use_affinity)
98 return;
99
d8540fc5 100#if HAVE_SCHED_SETAFFINITY
6af882ba
MD
101 ret = pthread_mutex_lock(&affinity_mutex);
102 if (ret) {
f597a8b9 103 errno = ret;
6af882ba 104 perror("Error in pthread mutex lock");
f597a8b9 105 abort();
6af882ba 106 }
2a7ac59d 107 cpu = cpu_affinities[next_aff++];
6af882ba
MD
108 ret = pthread_mutex_unlock(&affinity_mutex);
109 if (ret) {
f597a8b9 110 errno = ret;
6af882ba 111 perror("Error in pthread mutex unlock");
f597a8b9 112 abort();
6af882ba 113 }
2a7ac59d
MD
114 CPU_ZERO(&mask);
115 CPU_SET(cpu, &mask);
116 sched_setaffinity(0, sizeof(mask), &mask);
d8540fc5 117#endif /* HAVE_SCHED_SETAFFINITY */
2a7ac59d
MD
118}
119
0ee8bb0a
MD
120/*
121 * returns 0 if test should end.
122 */
123static int test_duration_write(void)
124{
78efb485 125 return !test_stop;
0ee8bb0a
MD
126}
127
128static int test_duration_read(void)
129{
78efb485 130 return !test_stop;
0ee8bb0a
MD
131}
132
bd252a04
MD
133static DEFINE_URCU_TLS(unsigned long long, nr_writes);
134static DEFINE_URCU_TLS(unsigned long long, nr_reads);
0ee8bb0a 135
55271c13 136static
06f22bdb 137unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *tot_nr_writes;
55271c13 138static
06f22bdb 139unsigned long long __attribute__((aligned(CAA_CACHE_LINE_SIZE))) *tot_nr_reads;
0ee8bb0a
MD
140
141static unsigned int nr_readers;
142static unsigned int nr_writers;
143
f597a8b9 144static void urcu_mutex_lock(pthread_mutex_t *lock)
0ee8bb0a
MD
145{
146 int ret;
f597a8b9
MD
147
148 ret = pthread_mutex_lock(lock);
0ee8bb0a 149 if (ret) {
f597a8b9 150 errno = ret;
0ee8bb0a 151 perror("Error in pthread mutex lock");
f597a8b9 152 abort();
0ee8bb0a
MD
153 }
154}
155
f597a8b9 156static void urcu_mutex_unlock(pthread_mutex_t *lock)
0ee8bb0a
MD
157{
158 int ret;
159
f597a8b9 160 ret = pthread_mutex_unlock(lock);
0ee8bb0a 161 if (ret) {
f597a8b9 162 errno = ret;
0ee8bb0a 163 perror("Error in pthread mutex unlock");
f597a8b9 164 abort();
0ee8bb0a
MD
165 }
166}
167
61c3fb60 168static
0ee8bb0a
MD
169void *thr_reader(void *data)
170{
171 unsigned long tidx = (unsigned long)data;
172
94df6318
MD
173 printf_verbose("thread_begin %s, tid %lu\n",
174 "reader", urcu_get_thread_id());
0ee8bb0a 175
2a7ac59d
MD
176 set_affinity();
177
0ee8bb0a
MD
178 while (!test_go)
179 {
180 }
181
182 for (;;) {
19d0e7ef
MD
183 int v;
184
f597a8b9 185 urcu_mutex_lock(&per_thread_lock[tidx].lock);
19d0e7ef
MD
186 v = test_array.a;
187 assert(v == 8);
a0b7f7ea 188 if (caa_unlikely(rduration))
daddf5b0 189 loop_sleep(rduration);
f597a8b9 190 urcu_mutex_unlock(&per_thread_lock[tidx].lock);
bd252a04 191 URCU_TLS(nr_reads)++;
a0b7f7ea 192 if (caa_unlikely(!test_duration_read()))
0ee8bb0a
MD
193 break;
194 }
195
bd252a04 196 tot_nr_reads[tidx] = URCU_TLS(nr_reads);
94df6318
MD
197 printf_verbose("thread_end %s, tid %lu\n",
198 "reader", urcu_get_thread_id());
0ee8bb0a
MD
199 return ((void*)1);
200
201}
202
61c3fb60 203static
0ee8bb0a
MD
204void *thr_writer(void *data)
205{
206 unsigned long wtidx = (unsigned long)data;
207 long tidx;
208
94df6318
MD
209 printf_verbose("thread_begin %s, tid %lu\n",
210 "writer", urcu_get_thread_id());
0ee8bb0a 211
2a7ac59d
MD
212 set_affinity();
213
0ee8bb0a
MD
214 while (!test_go)
215 {
216 }
5481ddb3 217 cmm_smp_mb();
0ee8bb0a
MD
218
219 for (;;) {
d266df35 220 for (tidx = 0; tidx < (long)nr_readers; tidx++) {
f597a8b9 221 urcu_mutex_lock(&per_thread_lock[tidx].lock);
0ee8bb0a 222 }
2b4e4125 223 test_array.a = 0;
0ee8bb0a 224 test_array.a = 8;
a0b7f7ea 225 if (caa_unlikely(wduration))
31b598e0 226 loop_sleep(wduration);
f120cc10 227 for (tidx = (long)nr_readers - 1; tidx >= 0; tidx--) {
f597a8b9 228 urcu_mutex_unlock(&per_thread_lock[tidx].lock);
0ee8bb0a 229 }
bd252a04 230 URCU_TLS(nr_writes)++;
a0b7f7ea 231 if (caa_unlikely(!test_duration_write()))
0ee8bb0a 232 break;
a0b7f7ea 233 if (caa_unlikely(wdelay))
9e31d0f0 234 loop_sleep(wdelay);
0ee8bb0a
MD
235 }
236
94df6318
MD
237 printf_verbose("thread_end %s, tid %lu\n",
238 "writer", urcu_get_thread_id());
bd252a04 239 tot_nr_writes[wtidx] = URCU_TLS(nr_writes);
0ee8bb0a
MD
240 return ((void*)2);
241}
242
61c3fb60 243static
0ee8bb0a
MD
244void show_usage(int argc, char **argv)
245{
06637138
MD
246 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
247 argv[0]);
248 printf("OPTIONS:\n");
06637138
MD
249 printf(" [-d delay] (writer period (us))\n");
250 printf(" [-c duration] (reader C.S. duration (in loops))\n");
251 printf(" [-e duration] (writer C.S. duration (in loops))\n");
252 printf(" [-v] (verbose output)\n");
253 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
0ee8bb0a
MD
254 printf("\n");
255}
256
257int main(int argc, char **argv)
258{
259 int err;
260 pthread_t *tid_reader, *tid_writer;
261 void *tret;
0ee8bb0a 262 unsigned long long tot_reads = 0, tot_writes = 0;
9e97e478 263 int i, a;
83e334d0 264 unsigned int i_thr;
0ee8bb0a
MD
265
266 if (argc < 4) {
267 show_usage(argc, argv);
268 return -1;
269 }
5481ddb3 270 cmm_smp_mb();
0ee8bb0a
MD
271
272 err = sscanf(argv[1], "%u", &nr_readers);
273 if (err != 1) {
274 show_usage(argc, argv);
275 return -1;
276 }
277
278 err = sscanf(argv[2], "%u", &nr_writers);
279 if (err != 1) {
280 show_usage(argc, argv);
281 return -1;
282 }
83e334d0 283
0ee8bb0a
MD
284 err = sscanf(argv[3], "%lu", &duration);
285 if (err != 1) {
286 show_usage(argc, argv);
287 return -1;
288 }
289
290 for (i = 4; i < argc; i++) {
291 if (argv[i][0] != '-')
292 continue;
293 switch (argv[i][1]) {
9e97e478
MD
294 case 'a':
295 if (argc < i + 2) {
296 show_usage(argc, argv);
297 return -1;
298 }
299 a = atoi(argv[++i]);
2a7ac59d 300 cpu_affinities[next_aff++] = a;
9e97e478 301 use_affinity = 1;
4bad7d45 302 printf_verbose("Adding CPU %d affinity\n", a);
9e97e478 303 break;
8b632bab
MD
304 case 'c':
305 if (argc < i + 2) {
306 show_usage(argc, argv);
307 return -1;
308 }
9e31d0f0 309 rduration = atol(argv[++i]);
8b632bab 310 break;
0ee8bb0a
MD
311 case 'd':
312 if (argc < i + 2) {
313 show_usage(argc, argv);
314 return -1;
315 }
9e31d0f0 316 wdelay = atol(argv[++i]);
0ee8bb0a 317 break;
31b598e0
MD
318 case 'e':
319 if (argc < i + 2) {
320 show_usage(argc, argv);
321 return -1;
322 }
323 wduration = atol(argv[++i]);
324 break;
4bad7d45
MD
325 case 'v':
326 verbose_mode = 1;
327 break;
0ee8bb0a
MD
328 }
329 }
330
4bad7d45 331 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
0ee8bb0a 332 duration, nr_readers, nr_writers);
9e31d0f0 333 printf_verbose("Writer delay : %lu loops.\n", wdelay);
4bad7d45 334 printf_verbose("Reader duration : %lu loops.\n", rduration);
94df6318
MD
335 printf_verbose("thread %-6s, tid %lu\n",
336 "main", urcu_get_thread_id());
0ee8bb0a 337
9aa14175
MD
338 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
339 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
340 tot_nr_reads = calloc(nr_readers, sizeof(*tot_nr_reads));
341 tot_nr_writes = calloc(nr_writers, sizeof(*tot_nr_writes));
342 per_thread_lock = calloc(nr_readers, sizeof(*per_thread_lock));
83e334d0
MJ
343 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
344 pthread_mutex_init(&per_thread_lock[i_thr].lock, NULL);
dee09338 345 }
0ee8bb0a 346
2a7ac59d
MD
347 next_aff = 0;
348
83e334d0
MJ
349 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
350 err = pthread_create(&tid_reader[i_thr], NULL, thr_reader,
351 (void *)(long)i_thr);
0ee8bb0a
MD
352 if (err != 0)
353 exit(1);
354 }
83e334d0
MJ
355 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
356 err = pthread_create(&tid_writer[i_thr], NULL, thr_writer,
357 (void *)(long)i_thr);
0ee8bb0a
MD
358 if (err != 0)
359 exit(1);
360 }
361
5481ddb3 362 cmm_smp_mb();
0ee8bb0a 363
78efb485
MD
364 test_go = 1;
365
366 sleep(duration);
367
368 test_stop = 1;
369
83e334d0
MJ
370 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
371 err = pthread_join(tid_reader[i_thr], &tret);
0ee8bb0a
MD
372 if (err != 0)
373 exit(1);
83e334d0 374 tot_reads += tot_nr_reads[i_thr];
0ee8bb0a 375 }
83e334d0
MJ
376 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
377 err = pthread_join(tid_writer[i_thr], &tret);
0ee8bb0a
MD
378 if (err != 0)
379 exit(1);
83e334d0 380 tot_writes += tot_nr_writes[i_thr];
0ee8bb0a 381 }
4bad7d45
MD
382
383 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
0ee8bb0a 384 tot_writes);
a50a7b43 385 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
cda3c71d 386 "nr_writers %3u "
9e31d0f0 387 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
a50a7b43 388 argv[0], duration, nr_readers, rduration, wduration,
4bad7d45
MD
389 nr_writers, wdelay, tot_reads, tot_writes,
390 tot_reads + tot_writes);
391
0ee8bb0a
MD
392 free(tid_reader);
393 free(tid_writer);
0ee8bb0a
MD
394 free(tot_nr_reads);
395 free(tot_nr_writes);
396 free(per_thread_lock);
397 return 0;
398}
This page took 0.061269 seconds and 4 git commands to generate.