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