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