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