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