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