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