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