Remove glibc < 2.4 compat code for sched_setaffinity
[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
168void *thr_reader(void *data)
169{
170 unsigned long tidx = (unsigned long)data;
171
94df6318
MD
172 printf_verbose("thread_begin %s, tid %lu\n",
173 "reader", urcu_get_thread_id());
0ee8bb0a 174
2a7ac59d
MD
175 set_affinity();
176
0ee8bb0a
MD
177 while (!test_go)
178 {
179 }
180
181 for (;;) {
19d0e7ef
MD
182 int v;
183
f597a8b9 184 urcu_mutex_lock(&per_thread_lock[tidx].lock);
19d0e7ef
MD
185 v = test_array.a;
186 assert(v == 8);
a0b7f7ea 187 if (caa_unlikely(rduration))
daddf5b0 188 loop_sleep(rduration);
f597a8b9 189 urcu_mutex_unlock(&per_thread_lock[tidx].lock);
bd252a04 190 URCU_TLS(nr_reads)++;
a0b7f7ea 191 if (caa_unlikely(!test_duration_read()))
0ee8bb0a
MD
192 break;
193 }
194
bd252a04 195 tot_nr_reads[tidx] = URCU_TLS(nr_reads);
94df6318
MD
196 printf_verbose("thread_end %s, tid %lu\n",
197 "reader", urcu_get_thread_id());
0ee8bb0a
MD
198 return ((void*)1);
199
200}
201
202void *thr_writer(void *data)
203{
204 unsigned long wtidx = (unsigned long)data;
205 long tidx;
206
94df6318
MD
207 printf_verbose("thread_begin %s, tid %lu\n",
208 "writer", urcu_get_thread_id());
0ee8bb0a 209
2a7ac59d
MD
210 set_affinity();
211
0ee8bb0a
MD
212 while (!test_go)
213 {
214 }
5481ddb3 215 cmm_smp_mb();
0ee8bb0a
MD
216
217 for (;;) {
d266df35 218 for (tidx = 0; tidx < (long)nr_readers; tidx++) {
f597a8b9 219 urcu_mutex_lock(&per_thread_lock[tidx].lock);
0ee8bb0a 220 }
2b4e4125 221 test_array.a = 0;
0ee8bb0a 222 test_array.a = 8;
a0b7f7ea 223 if (caa_unlikely(wduration))
31b598e0 224 loop_sleep(wduration);
f120cc10 225 for (tidx = (long)nr_readers - 1; tidx >= 0; tidx--) {
f597a8b9 226 urcu_mutex_unlock(&per_thread_lock[tidx].lock);
0ee8bb0a 227 }
bd252a04 228 URCU_TLS(nr_writes)++;
a0b7f7ea 229 if (caa_unlikely(!test_duration_write()))
0ee8bb0a 230 break;
a0b7f7ea 231 if (caa_unlikely(wdelay))
9e31d0f0 232 loop_sleep(wdelay);
0ee8bb0a
MD
233 }
234
94df6318
MD
235 printf_verbose("thread_end %s, tid %lu\n",
236 "writer", urcu_get_thread_id());
bd252a04 237 tot_nr_writes[wtidx] = URCU_TLS(nr_writes);
0ee8bb0a
MD
238 return ((void*)2);
239}
240
241void show_usage(int argc, char **argv)
242{
06637138
MD
243 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
244 argv[0]);
245 printf("OPTIONS:\n");
06637138
MD
246 printf(" [-d delay] (writer period (us))\n");
247 printf(" [-c duration] (reader C.S. duration (in loops))\n");
248 printf(" [-e duration] (writer C.S. duration (in loops))\n");
249 printf(" [-v] (verbose output)\n");
250 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
0ee8bb0a
MD
251 printf("\n");
252}
253
254int main(int argc, char **argv)
255{
256 int err;
257 pthread_t *tid_reader, *tid_writer;
258 void *tret;
0ee8bb0a 259 unsigned long long tot_reads = 0, tot_writes = 0;
9e97e478 260 int i, a;
83e334d0 261 unsigned int i_thr;
0ee8bb0a
MD
262
263 if (argc < 4) {
264 show_usage(argc, argv);
265 return -1;
266 }
5481ddb3 267 cmm_smp_mb();
0ee8bb0a
MD
268
269 err = sscanf(argv[1], "%u", &nr_readers);
270 if (err != 1) {
271 show_usage(argc, argv);
272 return -1;
273 }
274
275 err = sscanf(argv[2], "%u", &nr_writers);
276 if (err != 1) {
277 show_usage(argc, argv);
278 return -1;
279 }
83e334d0 280
0ee8bb0a
MD
281 err = sscanf(argv[3], "%lu", &duration);
282 if (err != 1) {
283 show_usage(argc, argv);
284 return -1;
285 }
286
287 for (i = 4; i < argc; i++) {
288 if (argv[i][0] != '-')
289 continue;
290 switch (argv[i][1]) {
9e97e478
MD
291 case 'a':
292 if (argc < i + 2) {
293 show_usage(argc, argv);
294 return -1;
295 }
296 a = atoi(argv[++i]);
2a7ac59d 297 cpu_affinities[next_aff++] = a;
9e97e478 298 use_affinity = 1;
4bad7d45 299 printf_verbose("Adding CPU %d affinity\n", a);
9e97e478 300 break;
8b632bab
MD
301 case 'c':
302 if (argc < i + 2) {
303 show_usage(argc, argv);
304 return -1;
305 }
9e31d0f0 306 rduration = atol(argv[++i]);
8b632bab 307 break;
0ee8bb0a
MD
308 case 'd':
309 if (argc < i + 2) {
310 show_usage(argc, argv);
311 return -1;
312 }
9e31d0f0 313 wdelay = atol(argv[++i]);
0ee8bb0a 314 break;
31b598e0
MD
315 case 'e':
316 if (argc < i + 2) {
317 show_usage(argc, argv);
318 return -1;
319 }
320 wduration = atol(argv[++i]);
321 break;
4bad7d45
MD
322 case 'v':
323 verbose_mode = 1;
324 break;
0ee8bb0a
MD
325 }
326 }
327
4bad7d45 328 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
0ee8bb0a 329 duration, nr_readers, nr_writers);
9e31d0f0 330 printf_verbose("Writer delay : %lu loops.\n", wdelay);
4bad7d45 331 printf_verbose("Reader duration : %lu loops.\n", rduration);
94df6318
MD
332 printf_verbose("thread %-6s, tid %lu\n",
333 "main", urcu_get_thread_id());
0ee8bb0a 334
9aa14175
MD
335 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
336 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
337 tot_nr_reads = calloc(nr_readers, sizeof(*tot_nr_reads));
338 tot_nr_writes = calloc(nr_writers, sizeof(*tot_nr_writes));
339 per_thread_lock = calloc(nr_readers, sizeof(*per_thread_lock));
83e334d0
MJ
340 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
341 pthread_mutex_init(&per_thread_lock[i_thr].lock, NULL);
dee09338 342 }
0ee8bb0a 343
2a7ac59d
MD
344 next_aff = 0;
345
83e334d0
MJ
346 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
347 err = pthread_create(&tid_reader[i_thr], NULL, thr_reader,
348 (void *)(long)i_thr);
0ee8bb0a
MD
349 if (err != 0)
350 exit(1);
351 }
83e334d0
MJ
352 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
353 err = pthread_create(&tid_writer[i_thr], NULL, thr_writer,
354 (void *)(long)i_thr);
0ee8bb0a
MD
355 if (err != 0)
356 exit(1);
357 }
358
5481ddb3 359 cmm_smp_mb();
0ee8bb0a 360
78efb485
MD
361 test_go = 1;
362
363 sleep(duration);
364
365 test_stop = 1;
366
83e334d0
MJ
367 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
368 err = pthread_join(tid_reader[i_thr], &tret);
0ee8bb0a
MD
369 if (err != 0)
370 exit(1);
83e334d0 371 tot_reads += tot_nr_reads[i_thr];
0ee8bb0a 372 }
83e334d0
MJ
373 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
374 err = pthread_join(tid_writer[i_thr], &tret);
0ee8bb0a
MD
375 if (err != 0)
376 exit(1);
83e334d0 377 tot_writes += tot_nr_writes[i_thr];
0ee8bb0a 378 }
4bad7d45
MD
379
380 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
0ee8bb0a 381 tot_writes);
a50a7b43 382 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
cda3c71d 383 "nr_writers %3u "
9e31d0f0 384 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
a50a7b43 385 argv[0], duration, nr_readers, rduration, wduration,
4bad7d45
MD
386 nr_writers, wdelay, tot_reads, tot_writes,
387 tot_reads + tot_writes);
388
0ee8bb0a
MD
389 free(tid_reader);
390 free(tid_writer);
0ee8bb0a
MD
391 free(tot_nr_reads);
392 free(tot_nr_writes);
393 free(per_thread_lock);
394 return 0;
395}
This page took 0.060646 seconds and 4 git commands to generate.