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