Remove glibc < 2.4 compat code for sched_setaffinity
[urcu.git] / tests / benchmark / test_urcu_assign.c
1 /*
2 * test_urcu_assign.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 #include "../common/debug-yield.h"
38
39 /* hardcoded number of CPUs */
40 #define NR_CPUS 16384
41
42 #ifndef DYNAMIC_LINK_TEST
43 #define _LGPL_SOURCE
44 #endif
45 #include <urcu.h>
46
47 struct test_array {
48 int a;
49 };
50
51 static volatile int test_go, test_stop;
52
53 static unsigned long wdelay;
54
55 static struct test_array *test_rcu_pointer;
56
57 static unsigned long duration;
58
59 /* read-side C.S. duration, in loops */
60 static unsigned long rduration;
61
62 /* write-side C.S. duration, in loops */
63 static unsigned long wduration;
64
65 static inline void loop_sleep(unsigned long loops)
66 {
67 while (loops-- != 0)
68 caa_cpu_relax();
69 }
70
71 static int verbose_mode;
72
73 #define printf_verbose(fmt, args...) \
74 do { \
75 if (verbose_mode) \
76 printf(fmt, args); \
77 } while (0)
78
79 static unsigned int cpu_affinities[NR_CPUS];
80 static unsigned int next_aff = 0;
81 static int use_affinity = 0;
82
83 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
84
85 static void set_affinity(void)
86 {
87 #if HAVE_SCHED_SETAFFINITY
88 cpu_set_t mask;
89 int cpu, ret;
90 #endif /* HAVE_SCHED_SETAFFINITY */
91
92 if (!use_affinity)
93 return;
94
95 #if HAVE_SCHED_SETAFFINITY
96 ret = pthread_mutex_lock(&affinity_mutex);
97 if (ret) {
98 perror("Error in pthread mutex lock");
99 exit(-1);
100 }
101 cpu = cpu_affinities[next_aff++];
102 ret = pthread_mutex_unlock(&affinity_mutex);
103 if (ret) {
104 perror("Error in pthread mutex unlock");
105 exit(-1);
106 }
107
108 CPU_ZERO(&mask);
109 CPU_SET(cpu, &mask);
110 sched_setaffinity(0, sizeof(mask), &mask);
111 #endif /* HAVE_SCHED_SETAFFINITY */
112 }
113
114 /*
115 * returns 0 if test should end.
116 */
117 static int test_duration_write(void)
118 {
119 return !test_stop;
120 }
121
122 static int test_duration_read(void)
123 {
124 return !test_stop;
125 }
126
127 static DEFINE_URCU_TLS(unsigned long long, nr_writes);
128 static DEFINE_URCU_TLS(unsigned long long, nr_reads);
129
130 static unsigned int nr_readers;
131 static unsigned int nr_writers;
132
133 pthread_mutex_t rcu_copy_mutex = PTHREAD_MUTEX_INITIALIZER;
134
135 void rcu_copy_mutex_lock(void)
136 {
137 int ret;
138 ret = pthread_mutex_lock(&rcu_copy_mutex);
139 if (ret) {
140 perror("Error in pthread mutex lock");
141 exit(-1);
142 }
143 }
144
145 void rcu_copy_mutex_unlock(void)
146 {
147 int ret;
148
149 ret = pthread_mutex_unlock(&rcu_copy_mutex);
150 if (ret) {
151 perror("Error in pthread mutex unlock");
152 exit(-1);
153 }
154 }
155
156 /*
157 * malloc/free are reusing memory areas too quickly, which does not let us
158 * test races appropriately. Use a large circular array for allocations.
159 * ARRAY_SIZE is larger than nr_writers, and we keep the mutex across
160 * both alloc and free, which insures we never run over our tail.
161 */
162 #define ARRAY_SIZE (1048576 * nr_writers)
163 #define ARRAY_POISON (int) 0xDEADBEEF
164 static unsigned int array_index;
165 static struct test_array *test_array;
166
167 static struct test_array *test_array_alloc(void)
168 {
169 struct test_array *ret;
170 int index;
171
172 index = array_index % ARRAY_SIZE;
173 assert(test_array[index].a == ARRAY_POISON ||
174 test_array[index].a == 0);
175 ret = &test_array[index];
176 array_index++;
177 if (array_index == ARRAY_SIZE)
178 array_index = 0;
179 return ret;
180 }
181
182 static void test_array_free(struct test_array *ptr)
183 {
184 if (!ptr)
185 return;
186 ptr->a = ARRAY_POISON;
187 }
188
189 void *thr_reader(void *_count)
190 {
191 unsigned long long *count = _count;
192 struct test_array *local_ptr;
193
194 printf_verbose("thread_begin %s, tid %lu\n",
195 "reader", urcu_get_thread_id());
196
197 set_affinity();
198
199 rcu_register_thread();
200
201 while (!test_go)
202 {
203 }
204 cmm_smp_mb();
205
206 for (;;) {
207 rcu_read_lock();
208 local_ptr = rcu_dereference(test_rcu_pointer);
209 rcu_debug_yield_read();
210 if (local_ptr)
211 assert(local_ptr->a == 8);
212 if (caa_unlikely(rduration))
213 loop_sleep(rduration);
214 rcu_read_unlock();
215 URCU_TLS(nr_reads)++;
216 if (caa_unlikely(!test_duration_read()))
217 break;
218 }
219
220 rcu_unregister_thread();
221
222 *count = URCU_TLS(nr_reads);
223 printf_verbose("thread_end %s, tid %lu\n",
224 "reader", urcu_get_thread_id());
225 return ((void*)1);
226
227 }
228
229 void *thr_writer(void *_count)
230 {
231 unsigned long long *count = _count;
232 struct test_array *new, *old;
233
234 printf_verbose("thread_begin %s, tid %lu\n",
235 "writer", urcu_get_thread_id());
236
237 set_affinity();
238
239 while (!test_go)
240 {
241 }
242 cmm_smp_mb();
243
244 for (;;) {
245 rcu_copy_mutex_lock();
246 new = test_array_alloc();
247 new->a = 8;
248 old = test_rcu_pointer;
249 rcu_assign_pointer(test_rcu_pointer, new);
250 if (caa_unlikely(wduration))
251 loop_sleep(wduration);
252 synchronize_rcu();
253 if (old)
254 old->a = 0;
255 test_array_free(old);
256 rcu_copy_mutex_unlock();
257 URCU_TLS(nr_writes)++;
258 if (caa_unlikely(!test_duration_write()))
259 break;
260 if (caa_unlikely(wdelay))
261 loop_sleep(wdelay);
262 }
263
264 printf_verbose("thread_end %s, tid %lu\n",
265 "writer", urcu_get_thread_id());
266 *count = URCU_TLS(nr_writes);
267 return ((void*)2);
268 }
269
270 void show_usage(int argc, char **argv)
271 {
272 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
273 argv[0]);
274 printf("OPTIONS:\n");
275 printf(" [-r] [-w] (yield reader and/or writer)\n");
276 printf(" [-d delay] (writer period (us))\n");
277 printf(" [-c duration] (reader C.S. duration (in loops))\n");
278 printf(" [-e duration] (writer C.S. duration (in loops))\n");
279 printf(" [-v] (verbose output)\n");
280 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
281 printf("\n");
282 }
283
284 int main(int argc, char **argv)
285 {
286 int err;
287 pthread_t *tid_reader, *tid_writer;
288 void *tret;
289 unsigned long long *count_reader, *count_writer;
290 unsigned long long tot_reads = 0, tot_writes = 0;
291 int i, a;
292 unsigned int i_thr;
293
294 if (argc < 4) {
295 show_usage(argc, argv);
296 return -1;
297 }
298
299 err = sscanf(argv[1], "%u", &nr_readers);
300 if (err != 1) {
301 show_usage(argc, argv);
302 return -1;
303 }
304
305 err = sscanf(argv[2], "%u", &nr_writers);
306 if (err != 1) {
307 show_usage(argc, argv);
308 return -1;
309 }
310
311 err = sscanf(argv[3], "%lu", &duration);
312 if (err != 1) {
313 show_usage(argc, argv);
314 return -1;
315 }
316
317 for (i = 4; i < argc; i++) {
318 if (argv[i][0] != '-')
319 continue;
320 switch (argv[i][1]) {
321 case 'r':
322 rcu_debug_yield_enable(RCU_YIELD_READ);
323 break;
324 case 'w':
325 rcu_debug_yield_enable(RCU_YIELD_WRITE);
326 break;
327 case 'a':
328 if (argc < i + 2) {
329 show_usage(argc, argv);
330 return -1;
331 }
332 a = atoi(argv[++i]);
333 cpu_affinities[next_aff++] = a;
334 use_affinity = 1;
335 printf_verbose("Adding CPU %d affinity\n", a);
336 break;
337 case 'c':
338 if (argc < i + 2) {
339 show_usage(argc, argv);
340 return -1;
341 }
342 rduration = atol(argv[++i]);
343 break;
344 case 'd':
345 if (argc < i + 2) {
346 show_usage(argc, argv);
347 return -1;
348 }
349 wdelay = atol(argv[++i]);
350 break;
351 case 'e':
352 if (argc < i + 2) {
353 show_usage(argc, argv);
354 return -1;
355 }
356 wduration = atol(argv[++i]);
357 break;
358 case 'v':
359 verbose_mode = 1;
360 break;
361 }
362 }
363
364 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
365 duration, nr_readers, nr_writers);
366 printf_verbose("Writer delay : %lu loops.\n", wdelay);
367 printf_verbose("Reader duration : %lu loops.\n", rduration);
368 printf_verbose("thread %-6s, tid %lu\n",
369 "main", urcu_get_thread_id());
370
371 test_array = calloc(1, sizeof(*test_array) * ARRAY_SIZE);
372 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
373 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
374 count_reader = calloc(nr_readers, sizeof(*count_reader));
375 count_writer = calloc(nr_writers, sizeof(*count_writer));
376
377 next_aff = 0;
378
379 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
380 err = pthread_create(&tid_reader[i_thr], NULL, thr_reader,
381 &count_reader[i_thr]);
382 if (err != 0)
383 exit(1);
384 }
385 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
386 err = pthread_create(&tid_writer[i_thr], NULL, thr_writer,
387 &count_writer[i_thr]);
388 if (err != 0)
389 exit(1);
390 }
391
392 cmm_smp_mb();
393
394 test_go = 1;
395
396 sleep(duration);
397
398 test_stop = 1;
399
400 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
401 err = pthread_join(tid_reader[i_thr], &tret);
402 if (err != 0)
403 exit(1);
404 tot_reads += count_reader[i_thr];
405 }
406 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
407 err = pthread_join(tid_writer[i_thr], &tret);
408 if (err != 0)
409 exit(1);
410 tot_writes += count_writer[i_thr];
411 }
412
413 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
414 tot_writes);
415 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
416 "nr_writers %3u "
417 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
418 argv[0], duration, nr_readers, rduration, wduration,
419 nr_writers, wdelay, tot_reads, tot_writes,
420 tot_reads + tot_writes);
421
422 test_array_free(test_rcu_pointer);
423 free(test_array);
424 free(tid_reader);
425 free(tid_writer);
426 free(count_reader);
427 free(count_writer);
428
429 return 0;
430 }
This page took 0.037321 seconds and 4 git commands to generate.