512b1fc8ff1dc1f6697c28911ce382cec3c1ebe3
[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 unsigned int i_thr;
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 case 'a':
297 if (argc < i + 2) {
298 show_usage(argc, argv);
299 return -1;
300 }
301 a = atoi(argv[++i]);
302 cpu_affinities[next_aff++] = a;
303 use_affinity = 1;
304 printf_verbose("Adding CPU %d affinity\n", a);
305 break;
306 case 'c':
307 if (argc < i + 2) {
308 show_usage(argc, argv);
309 return -1;
310 }
311 rduration = atol(argv[++i]);
312 break;
313 case 'd':
314 if (argc < i + 2) {
315 show_usage(argc, argv);
316 return -1;
317 }
318 wdelay = atol(argv[++i]);
319 break;
320 case 'e':
321 if (argc < i + 2) {
322 show_usage(argc, argv);
323 return -1;
324 }
325 wduration = atol(argv[++i]);
326 break;
327 case 'v':
328 verbose_mode = 1;
329 break;
330 }
331 }
332
333 printf_verbose("running test for %lu seconds, %u readers, %u writers.\n",
334 duration, nr_readers, nr_writers);
335 printf_verbose("Writer delay : %lu loops.\n", wdelay);
336 printf_verbose("Reader duration : %lu loops.\n", rduration);
337 printf_verbose("thread %-6s, tid %lu\n",
338 "main", urcu_get_thread_id());
339
340 tid_reader = calloc(nr_readers, sizeof(*tid_reader));
341 tid_writer = calloc(nr_writers, sizeof(*tid_writer));
342 tot_nr_reads = calloc(nr_readers, sizeof(*tot_nr_reads));
343 tot_nr_writes = calloc(nr_writers, sizeof(*tot_nr_writes));
344 per_thread_lock = calloc(nr_readers, sizeof(*per_thread_lock));
345 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
346 pthread_mutex_init(&per_thread_lock[i_thr].lock, NULL);
347 }
348
349 next_aff = 0;
350
351 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
352 err = pthread_create(&tid_reader[i_thr], NULL, thr_reader,
353 (void *)(long)i_thr);
354 if (err != 0)
355 exit(1);
356 }
357 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
358 err = pthread_create(&tid_writer[i_thr], NULL, thr_writer,
359 (void *)(long)i_thr);
360 if (err != 0)
361 exit(1);
362 }
363
364 cmm_smp_mb();
365
366 test_go = 1;
367
368 sleep(duration);
369
370 test_stop = 1;
371
372 for (i_thr = 0; i_thr < nr_readers; i_thr++) {
373 err = pthread_join(tid_reader[i_thr], &tret);
374 if (err != 0)
375 exit(1);
376 tot_reads += tot_nr_reads[i_thr];
377 }
378 for (i_thr = 0; i_thr < nr_writers; i_thr++) {
379 err = pthread_join(tid_writer[i_thr], &tret);
380 if (err != 0)
381 exit(1);
382 tot_writes += tot_nr_writes[i_thr];
383 }
384
385 printf_verbose("total number of reads : %llu, writes %llu\n", tot_reads,
386 tot_writes);
387 printf("SUMMARY %-25s testdur %4lu nr_readers %3u rdur %6lu wdur %6lu "
388 "nr_writers %3u "
389 "wdelay %6lu nr_reads %12llu nr_writes %12llu nr_ops %12llu\n",
390 argv[0], duration, nr_readers, rduration, wduration,
391 nr_writers, wdelay, tot_reads, tot_writes,
392 tot_reads + tot_writes);
393
394 free(tid_reader);
395 free(tid_writer);
396 free(tot_nr_reads);
397 free(tot_nr_writes);
398 free(per_thread_lock);
399 return 0;
400 }
This page took 0.039346 seconds and 3 git commands to generate.