Cleanup: enable signed/unsigned compare compiler warning
[urcu.git] / tests / benchmark / test_urcu_qsbr.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 "cpuset.h"
37 #include "thread-id.h"
38 #include "../common/debug-yield.h"
39
40 /* hardcoded number of CPUs */
41 #define NR_CPUS 16384
42
43 #ifndef DYNAMIC_LINK_TEST
44 #define _LGPL_SOURCE
45 #endif
46 #include "urcu-qsbr.h"
47
48 static volatile int test_go, test_stop;
49
50 static unsigned long wdelay;
51
52 static int *test_rcu_pointer;
53
54 static unsigned long duration;
55
56 /* read-side C.S. duration, in loops */
57 static unsigned long rduration;
58
59 /* write-side C.S. duration, in loops */
60 static unsigned long wduration;
61
62 static inline void loop_sleep(unsigned long loops)
63 {
64 while (loops-- != 0)
65 caa_cpu_relax();
66 }
67
68 static int verbose_mode;
69
70 #define printf_verbose(fmt, args...) \
71 do { \
72 if (verbose_mode) \
73 printf(fmt, args); \
74 } while (0)
75
76 static unsigned int cpu_affinities[NR_CPUS];
77 static unsigned int next_aff = 0;
78 static int use_affinity = 0;
79
80 pthread_mutex_t affinity_mutex = PTHREAD_MUTEX_INITIALIZER;
81
82 static void set_affinity(void)
83 {
84 #if HAVE_SCHED_SETAFFINITY
85 cpu_set_t mask;
86 int cpu, ret;
87 #endif /* HAVE_SCHED_SETAFFINITY */
88
89 if (!use_affinity)
90 return;
91
92 #if HAVE_SCHED_SETAFFINITY
93 ret = pthread_mutex_lock(&affinity_mutex);
94 if (ret) {
95 perror("Error in pthread mutex lock");
96 exit(-1);
97 }
98 cpu = cpu_affinities[next_aff++];
99 ret = pthread_mutex_unlock(&affinity_mutex);
100 if (ret) {
101 perror("Error in pthread mutex unlock");
102 exit(-1);
103 }
104 CPU_ZERO(&mask);
105 CPU_SET(cpu, &mask);
106 #if SCHED_SETAFFINITY_ARGS == 2
107 sched_setaffinity(0, &mask);
108 #else
109 sched_setaffinity(0, sizeof(mask), &mask);
110 #endif
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 void *thr_reader(void *_count)
157 {
158 unsigned long long *count = _count;
159 int *local_ptr;
160
161 printf_verbose("thread_begin %s, tid %lu\n",
162 "reader", urcu_get_thread_id());
163
164 set_affinity();
165
166 rcu_register_thread();
167
168 assert(rcu_read_ongoing());
169 rcu_thread_offline();
170 assert(!rcu_read_ongoing());
171 rcu_thread_online();
172
173 while (!test_go)
174 {
175 }
176 cmm_smp_mb();
177
178 for (;;) {
179 rcu_read_lock();
180 assert(rcu_read_ongoing());
181 local_ptr = rcu_dereference(test_rcu_pointer);
182 rcu_debug_yield_read();
183 if (local_ptr)
184 assert(*local_ptr == 8);
185 if (caa_unlikely(rduration))
186 loop_sleep(rduration);
187 rcu_read_unlock();
188 URCU_TLS(nr_reads)++;
189 /* QS each 1024 reads */
190 if (caa_unlikely((URCU_TLS(nr_reads) & ((1 << 10) - 1)) == 0))
191 rcu_quiescent_state();
192 if (caa_unlikely(!test_duration_read()))
193 break;
194 }
195
196 rcu_unregister_thread();
197
198 /* test extra thread registration */
199 rcu_register_thread();
200 rcu_unregister_thread();
201
202 *count = URCU_TLS(nr_reads);
203 printf_verbose("thread_end %s, tid %lu\n",
204 "reader", urcu_get_thread_id());
205 return ((void*)1);
206
207 }
208
209 void *thr_writer(void *_count)
210 {
211 unsigned long long *count = _count;
212 int *new, *old;
213
214 printf_verbose("thread_begin %s, tid %lu\n",
215 "writer", urcu_get_thread_id());
216
217 set_affinity();
218
219 while (!test_go)
220 {
221 }
222 cmm_smp_mb();
223
224 for (;;) {
225 new = malloc(sizeof(int));
226 assert(new);
227 *new = 8;
228 old = rcu_xchg_pointer(&test_rcu_pointer, new);
229 if (caa_unlikely(wduration))
230 loop_sleep(wduration);
231 synchronize_rcu();
232 if (old)
233 *old = 0;
234 free(old);
235 URCU_TLS(nr_writes)++;
236 if (caa_unlikely(!test_duration_write()))
237 break;
238 if (caa_unlikely(wdelay))
239 loop_sleep(wdelay);
240 }
241
242 printf_verbose("thread_end %s, tid %lu\n",
243 "writer", urcu_get_thread_id());
244 *count = URCU_TLS(nr_writes);
245 return ((void*)2);
246 }
247
248 void show_usage(int argc, char **argv)
249 {
250 printf("Usage : %s nr_readers nr_writers duration (s) <OPTIONS>\n",
251 argv[0]);
252 printf("OPTIONS:\n");
253 printf(" [-r] [-w] (yield reader and/or writer)\n");
254 printf(" [-d delay] (writer period (us))\n");
255 printf(" [-c duration] (reader C.S. duration (in loops))\n");
256 printf(" [-e duration] (writer C.S. duration (in loops))\n");
257 printf(" [-v] (verbose output)\n");
258 printf(" [-a cpu#] [-a cpu#]... (affinity)\n");
259 printf("\n");
260 }
261
262 int main(int argc, char **argv)
263 {
264 int err;
265 pthread_t *tid_reader, *tid_writer;
266 void *tret;
267 unsigned long long *count_reader, *count_writer;
268 unsigned long long tot_reads = 0, tot_writes = 0;
269 int i, a;
270 unsigned int i_thr;
271
272 if (argc < 4) {
273 show_usage(argc, argv);
274 return -1;
275 }
276
277 err = sscanf(argv[1], "%u", &nr_readers);
278 if (err != 1) {
279 show_usage(argc, argv);
280 return -1;
281 }
282
283 err = sscanf(argv[2], "%u", &nr_writers);
284 if (err != 1) {
285 show_usage(argc, argv);
286 return -1;
287 }
288
289 err = sscanf(argv[3], "%lu", &duration);
290 if (err != 1) {
291 show_usage(argc, argv);
292 return -1;
293 }
294
295 for (i = 4; i < argc; i++) {
296 if (argv[i][0] != '-')
297 continue;
298 switch (argv[i][1]) {
299 case 'r':
300 rcu_debug_yield_enable(RCU_YIELD_READ);
301 break;
302 case 'w':
303 rcu_debug_yield_enable(RCU_YIELD_WRITE);
304 break;
305 case 'a':
306 if (argc < i + 2) {
307 show_usage(argc, argv);
308 return -1;
309 }
310 a = atoi(argv[++i]);
311 cpu_affinities[next_aff++] = a;
312 use_affinity = 1;
313 printf_verbose("Adding CPU %d affinity\n", a);
314 break;
315 case 'c':
316 if (argc < i + 2) {
317 show_usage(argc, argv);
318 return -1;
319 }
320 rduration = atol(argv[++i]);
321 break;
322 case 'd':
323 if (argc < i + 2) {
324 show_usage(argc, argv);
325 return -1;
326 }
327 wdelay = atol(argv[++i]);
328 break;
329 case 'e':
330 if (argc < i + 2) {
331 show_usage(argc, argv);
332 return -1;
333 }
334 wduration = atol(argv[++i]);
335 break;
336 case 'v':
337 verbose_mode = 1;
338 break;
339 }
340 }
341
342 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
343 duration, nr_readers, nr_writers);
344 printf_verbose("Writer delay : %lu loops.\n", wdelay);
345 printf_verbose("Reader duration : %lu loops.\n", rduration);
346 printf_verbose("thread %-6s, tid %lu\n",
347 "main", urcu_get_thread_id());
348
349 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
350 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
351 count_reader = calloc(nr_readers, sizeof(*count_reader));
352 count_writer = calloc(nr_writers, sizeof(*count_writer));
353
354 next_aff = 0;
355
356 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
357 err = pthread_create(&tid_reader[i_thr], NULL, thr_reader,
358 &count_reader[i_thr]);
359 if (err != 0)
360 exit(1);
361 }
362 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
363 err = pthread_create(&tid_writer[i_thr], NULL, thr_writer,
364 &count_writer[i_thr]);
365 if (err != 0)
366 exit(1);
367 }
368
369 cmm_smp_mb();
370
371 test_go = 1;
372
373 sleep(duration);
374
375 test_stop = 1;
376
377 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
378 err = pthread_join(tid_reader[i_thr], &tret);
379 if (err != 0)
380 exit(1);
381 tot_reads += count_reader[i_thr];
382 }
383 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
384 err = pthread_join(tid_writer[i_thr], &tret);
385 if (err != 0)
386 exit(1);
387 tot_writes += count_writer[i_thr];
388 }
389
390 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
391 tot_writes);
392 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
393 "nr_writers %3u "
394 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
395 argv[0], duration, nr_readers, rduration, wduration,
396 nr_writers, wdelay, tot_reads, tot_writes,
397 tot_reads + tot_writes);
398 free(test_rcu_pointer);
399 free(tid_reader);
400 free(tid_writer);
401 free(count_reader);
402 free(count_writer);
403 return 0;
404 }
This page took 0.061403 seconds and 4 git commands to generate.