Fix: add missing m68k headers to dist
[urcu.git] / tests / benchmark / test_urcu_lfs_rcu.c
1 /*
2 * test_urcu_lfs_rcu.c
3 *
4 * Userspace RCU library - example RCU-based lock-free stack
5 *
6 * Copyright February 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright February 2010 - Paolo Bonzini <pbonzini@redhat.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #include <stdio.h>
25 #include <pthread.h>
26 #include <stdlib.h>
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 #include <unistd.h>
33 #include <stdio.h>
34 #include <assert.h>
35 #include <errno.h>
36
37 #include <urcu/arch.h>
38 #include <urcu/tls-compat.h>
39 #include "cpuset.h"
40 #include "thread-id.h"
41
42 /* hardcoded number of CPUs */
43 #define NR_CPUS 16384
44
45 #ifndef DYNAMIC_LINK_TEST
46 #define _LGPL_SOURCE
47 #endif
48 #include <urcu.h>
49
50 /* Remove deprecation warnings from test build. */
51 #define CDS_LFS_RCU_DEPRECATED
52
53 #include <urcu/cds.h>
54
55 static volatile int test_go, test_stop;
56
57 static unsigned long rduration;
58
59 static unsigned long duration;
60
61 /* read-side C.S. duration, in loops */
62 static unsigned long wdelay;
63
64 static inline void loop_sleep(unsigned long loops)
65 {
66 while (loops-- != 0)
67 caa_cpu_relax();
68 }
69
70 static int verbose_mode;
71
72 #define printf_verbose(fmt, args...) \
73 do { \
74 if (verbose_mode) \
75 printf(fmt, args); \
76 } while (0)
77
78 static unsigned int cpu_affinities[NR_CPUS];
79 static unsigned int next_aff = 0;
80 static int use_affinity = 0;
81
82 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
83
84 static void set_affinity(void)
85 {
86 cpu_set_t mask;
87 int cpu;
88 int ret;
89
90 if (!use_affinity)
91 return;
92
93 #if HAVE_SCHED_SETAFFINITY
94 ret = pthread_mutex_lock(&affinity_mutex);
95 if (ret) {
96 perror("Error in pthread mutex lock");
97 exit(-1);
98 }
99 cpu = cpu_affinities[next_aff++];
100 ret = pthread_mutex_unlock(&affinity_mutex);
101 if (ret) {
102 perror("Error in pthread mutex unlock");
103 exit(-1);
104 }
105
106 CPU_ZERO(&mask);
107 CPU_SET(cpu, &mask);
108 #if SCHED_SETAFFINITY_ARGS == 2
109 sched_setaffinity(0, &mask);
110 #else
111 sched_setaffinity(0, sizeof(mask), &mask);
112 #endif
113 #endif /* HAVE_SCHED_SETAFFINITY */
114 }
115
116 /*
117 * returns 0 if test should end.
118 */
119 static int test_duration_dequeue(void)
120 {
121 return !test_stop;
122 }
123
124 static int test_duration_enqueue(void)
125 {
126 return !test_stop;
127 }
128
129 static DEFINE_URCU_TLS(unsigned long long, nr_dequeues);
130 static DEFINE_URCU_TLS(unsigned long long, nr_enqueues);
131
132 static DEFINE_URCU_TLS(unsigned long long, nr_successful_dequeues);
133 static DEFINE_URCU_TLS(unsigned long long, nr_successful_enqueues);
134
135 static unsigned int nr_enqueuers;
136 static unsigned int nr_dequeuers;
137
138 struct test {
139 struct cds_lfs_node_rcu list;
140 struct rcu_head rcu;
141 };
142
143 static struct cds_lfs_stack_rcu s;
144
145 void *thr_enqueuer(void *_count)
146 {
147 unsigned long long *count = _count;
148
149 printf_verbose("thread_begin %s, tid %lu\n",
150 "enqueuer", urcu_get_thread_id());
151
152 set_affinity();
153
154 rcu_register_thread();
155
156 while (!test_go)
157 {
158 }
159 cmm_smp_mb();
160
161 for (;;) {
162 struct test *node = malloc(sizeof(*node));
163 if (!node)
164 goto fail;
165 cds_lfs_node_init_rcu(&node->list);
166 /* No rcu read-side is needed for push */
167 cds_lfs_push_rcu(&s, &node->list);
168 URCU_TLS(nr_successful_enqueues)++;
169
170 if (caa_unlikely(wdelay))
171 loop_sleep(wdelay);
172 fail:
173 URCU_TLS(nr_enqueues)++;
174 if (caa_unlikely(!test_duration_enqueue()))
175 break;
176 }
177
178 rcu_unregister_thread();
179
180 count[0] = URCU_TLS(nr_enqueues);
181 count[1] = URCU_TLS(nr_successful_enqueues);
182 printf_verbose("enqueuer thread_end, tid %lu, "
183 "enqueues %llu successful_enqueues %llu\n",
184 urcu_get_thread_id(),
185 URCU_TLS(nr_enqueues),
186 URCU_TLS(nr_successful_enqueues));
187 return ((void*)1);
188
189 }
190
191 static
192 void free_node_cb(struct rcu_head *head)
193 {
194 struct test *node =
195 caa_container_of(head, struct test, rcu);
196 free(node);
197 }
198
199 void *thr_dequeuer(void *_count)
200 {
201 unsigned long long *count = _count;
202
203 printf_verbose("thread_begin %s, tid %lu\n",
204 "dequeuer", urcu_get_thread_id());
205
206 set_affinity();
207
208 rcu_register_thread();
209
210 while (!test_go)
211 {
212 }
213 cmm_smp_mb();
214
215 for (;;) {
216 struct cds_lfs_node_rcu *snode;
217
218 rcu_read_lock();
219 snode = cds_lfs_pop_rcu(&s);
220 rcu_read_unlock();
221 if (snode) {
222 struct test *node;
223
224 node = caa_container_of(snode, struct test, list);
225 call_rcu(&node->rcu, free_node_cb);
226 URCU_TLS(nr_successful_dequeues)++;
227 }
228 URCU_TLS(nr_dequeues)++;
229 if (caa_unlikely(!test_duration_dequeue()))
230 break;
231 if (caa_unlikely(rduration))
232 loop_sleep(rduration);
233 }
234
235 rcu_unregister_thread();
236
237 printf_verbose("dequeuer thread_end, tid %lu, "
238 "dequeues %llu, successful_dequeues %llu\n",
239 urcu_get_thread_id(),
240 URCU_TLS(nr_dequeues),
241 URCU_TLS(nr_successful_dequeues));
242 count[0] = URCU_TLS(nr_dequeues);
243 count[1] = URCU_TLS(nr_successful_dequeues);
244 return ((void*)2);
245 }
246
247 void test_end(struct cds_lfs_stack_rcu *s, unsigned long long *nr_dequeues)
248 {
249 struct cds_lfs_node_rcu *snode;
250
251 do {
252 snode = cds_lfs_pop_rcu(s);
253 if (snode) {
254 struct test *node;
255
256 node = caa_container_of(snode, struct test, list);
257 free(node);
258 (*nr_dequeues)++;
259 }
260 } while (snode);
261 }
262
263 void show_usage(int argc, char **argv)
264 {
265 printf("Usage : %s nr_dequeuers nr_enqueuers duration (s) <OPTIONS>\n",
266 argv[0]);
267 printf("OPTIONS:\n");
268 printf(" [-d delay] (enqueuer period (in loops))\n");
269 printf(" [-c duration] (dequeuer period (in loops))\n");
270 printf(" [-v] (verbose output)\n");
271 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
272 printf("\n");
273 }
274
275 int main(int argc, char **argv)
276 {
277 int err;
278 pthread_t *tid_enqueuer, *tid_dequeuer;
279 void *tret;
280 unsigned long long *count_enqueuer, *count_dequeuer;
281 unsigned long long tot_enqueues = 0, tot_dequeues = 0;
282 unsigned long long tot_successful_enqueues = 0,
283 tot_successful_dequeues = 0;
284 unsigned long long end_dequeues = 0;
285 int i, a;
286
287 if (argc < 4) {
288 show_usage(argc, argv);
289 return -1;
290 }
291
292 err = sscanf(argv[1], "%u", &nr_dequeuers);
293 if (err != 1) {
294 show_usage(argc, argv);
295 return -1;
296 }
297
298 err = sscanf(argv[2], "%u", &nr_enqueuers);
299 if (err != 1) {
300 show_usage(argc, argv);
301 return -1;
302 }
303
304 err = sscanf(argv[3], "%lu", &duration);
305 if (err != 1) {
306 show_usage(argc, argv);
307 return -1;
308 }
309
310 for (i = 4; i < argc; i++) {
311 if (argv[i][0] != '-')
312 continue;
313 switch (argv[i][1]) {
314 case 'a':
315 if (argc < i + 2) {
316 show_usage(argc, argv);
317 return -1;
318 }
319 a = atoi(argv[++i]);
320 cpu_affinities[next_aff++] = a;
321 use_affinity = 1;
322 printf_verbose("Adding CPU %d affinity\n", a);
323 break;
324 case 'c':
325 if (argc < i + 2) {
326 show_usage(argc, argv);
327 return -1;
328 }
329 rduration = atol(argv[++i]);
330 break;
331 case 'd':
332 if (argc < i + 2) {
333 show_usage(argc, argv);
334 return -1;
335 }
336 wdelay = atol(argv[++i]);
337 break;
338 case 'v':
339 verbose_mode = 1;
340 break;
341 }
342 }
343
344 printf_verbose("running test for %lu seconds, %u enqueuers, "
345 "%u dequeuers.\n",
346 duration, nr_enqueuers, nr_dequeuers);
347 printf_verbose("Writer delay : %lu loops.\n", rduration);
348 printf_verbose("Reader duration : %lu loops.\n", wdelay);
349 printf_verbose("thread %-6s, tid %lu\n",
350 "main", urcu_get_thread_id());
351
352 tid_enqueuer = calloc(nr_enqueuers, sizeof(*tid_enqueuer));
353 tid_dequeuer = calloc(nr_dequeuers, sizeof(*tid_dequeuer));
354 count_enqueuer = calloc(nr_enqueuers, 2 * sizeof(*count_enqueuer));
355 count_dequeuer = calloc(nr_dequeuers, 2 * sizeof(*count_dequeuer));
356 cds_lfs_init_rcu(&s);
357 err = create_all_cpu_call_rcu_data(0);
358 if (err) {
359 printf("Per-CPU call_rcu() worker threads unavailable. Using default global worker thread.\n");
360 }
361
362 next_aff = 0;
363
364 for (i = 0; i < nr_enqueuers; i++) {
365 err = pthread_create(&tid_enqueuer[i], NULL, thr_enqueuer,
366 &count_enqueuer[2 * i]);
367 if (err != 0)
368 exit(1);
369 }
370 for (i = 0; i < nr_dequeuers; i++) {
371 err = pthread_create(&tid_dequeuer[i], NULL, thr_dequeuer,
372 &count_dequeuer[2 * i]);
373 if (err != 0)
374 exit(1);
375 }
376
377 cmm_smp_mb();
378
379 test_go = 1;
380
381 for (i = 0; i < duration; i++) {
382 sleep(1);
383 if (verbose_mode) {
384 fwrite(".", sizeof(char), 1, stdout);
385 fflush(stdout);
386 }
387 }
388
389 test_stop = 1;
390
391 for (i = 0; i < nr_enqueuers; i++) {
392 err = pthread_join(tid_enqueuer[i], &tret);
393 if (err != 0)
394 exit(1);
395 tot_enqueues += count_enqueuer[2 * i];
396 tot_successful_enqueues += count_enqueuer[2 * i + 1];
397 }
398 for (i = 0; i < nr_dequeuers; i++) {
399 err = pthread_join(tid_dequeuer[i], &tret);
400 if (err != 0)
401 exit(1);
402 tot_dequeues += count_dequeuer[2 * i];
403 tot_successful_dequeues += count_dequeuer[2 * i + 1];
404 }
405
406 test_end(&s, &end_dequeues);
407
408 printf_verbose("total number of enqueues : %llu, dequeues %llu\n",
409 tot_enqueues, tot_dequeues);
410 printf_verbose("total number of successful enqueues : %llu, "
411 "successful dequeues %llu\n",
412 tot_successful_enqueues, tot_successful_dequeues);
413 printf("SUMMARY %-25s testdur %4lu nr_enqueuers %3u wdelay %6lu "
414 "nr_dequeuers %3u "
415 "rdur %6lu nr_enqueues %12llu nr_dequeues %12llu "
416 "successful enqueues %12llu successful dequeues %12llu "
417 "end_dequeues %llu nr_ops %12llu\n",
418 argv[0], duration, nr_enqueuers, wdelay,
419 nr_dequeuers, rduration, tot_enqueues, tot_dequeues,
420 tot_successful_enqueues,
421 tot_successful_dequeues, end_dequeues,
422 tot_enqueues + tot_dequeues);
423 if (tot_successful_enqueues != tot_successful_dequeues + end_dequeues)
424 printf("WARNING! Discrepancy between nr succ. enqueues %llu vs "
425 "succ. dequeues + end dequeues %llu.\n",
426 tot_successful_enqueues,
427 tot_successful_dequeues + end_dequeues);
428
429 free_all_cpu_call_rcu_data();
430 free(count_enqueuer);
431 free(count_dequeuer);
432 free(tid_enqueuer);
433 free(tid_dequeuer);
434 return 0;
435 }
This page took 0.044694 seconds and 4 git commands to generate.