source: mainline/uspace/app/rcubench/rcubench.c@ 7b87e1d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7b87e1d was 9bde0d5, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Replace a bunch of direct uses of futex_t.

  • Property mode set to 100644
File size: 6.9 KB
Line 
1/*
2 * Copyright (c) 2012 Adam Hraska
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup test
30 * @{
31 */
32
33/**
34 * @file rcubench.c
35 */
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <stdint.h>
40#include <mem.h>
41#include <errno.h>
42#include <assert.h>
43#include <async.h>
44#include <fibril.h>
45#include <fibril_synch.h>
46#include <compiler/barrier.h>
47#include <futex.h>
48#include <str.h>
49
50#include <rcu.h>
51
52
53/* Results are printed to this file in addition to stdout. */
54static FILE *results_fd = NULL;
55
56typedef struct bench {
57 const char *name;
58 void (*func)(struct bench *);
59 size_t iters;
60 size_t nthreads;
61 fibril_semaphore_t done_threads;
62} bench_t;
63
64
65
66
67static void kernel_futex_bench(bench_t *bench)
68{
69 const size_t iters = bench->iters;
70 int val = 0;
71
72 for (size_t i = 0; i < iters; ++i) {
73 __SYSCALL1(SYS_FUTEX_WAKEUP, (sysarg_t) &val);
74 __SYSCALL1(SYS_FUTEX_SLEEP, (sysarg_t) &val);
75 }
76}
77
78static void libc_futex_lock_bench(bench_t *bench)
79{
80 const size_t iters = bench->iters;
81 futex_t loc_fut = FUTEX_INITIALIZER;
82
83 for (size_t i = 0; i < iters; ++i) {
84 futex_lock(&loc_fut);
85 /* no-op */
86 compiler_barrier();
87 futex_unlock(&loc_fut);
88 }
89}
90
91static void libc_futex_sema_bench(bench_t *bench)
92{
93 const size_t iters = bench->iters;
94 futex_t loc_fut = FUTEX_INITIALIZER;
95
96 for (size_t i = 0; i < iters; ++i) {
97 futex_down(&loc_fut);
98 /* no-op */
99 compiler_barrier();
100 futex_up(&loc_fut);
101 }
102}
103
104static errno_t thread_func(void *arg)
105{
106 bench_t *bench = (bench_t *)arg;
107
108 bench->func(bench);
109
110 /* Signal another thread completed. */
111 fibril_semaphore_up(&bench->done_threads);
112 return EOK;
113}
114
115static void run_threads_and_wait(bench_t *bench)
116{
117 assert(1 <= bench->nthreads);
118
119 if (2 <= bench->nthreads) {
120 printf("Creating %zu additional threads...\n", bench->nthreads - 1);
121 }
122
123 fibril_test_spawn_runners(bench->nthreads - 1);
124
125 /* Create and run the first nthreads - 1 threads.*/
126 for (size_t k = 1; k < bench->nthreads; ++k) {
127 fid_t f = fibril_create(thread_func, bench);
128 if (!f) {
129 printf("Error: Failed to create benchmark thread.\n");
130 abort();
131 }
132 fibril_detach(f);
133 fibril_add_ready(f);
134 }
135
136 /*
137 * Run the last thread in place so that we create multiple threads
138 * only when needed. Otherwise libc would immediately upgrade
139 * single-threaded futexes to proper multithreaded futexes
140 */
141 thread_func(bench);
142
143 printf("Waiting for remaining threads to complete.\n");
144
145 /* Wait for threads to complete. */
146 for (size_t k = 0; k < bench->nthreads; ++k) {
147 fibril_semaphore_down(&bench->done_threads);
148 }
149}
150
151static const char *results_txt = "/tmp/urcu-bench-results.txt";
152
153static bool open_results(void)
154{
155 results_fd = fopen(results_txt, "a");
156 return NULL != results_fd;
157}
158
159static void close_results(void)
160{
161 if (results_fd) {
162 fclose(results_fd);
163 }
164}
165
166static void print_res(const char *fmt, ...)
167{
168 va_list args;
169
170 va_start(args, fmt);
171 vfprintf(results_fd, fmt, args);
172 va_end(args);
173
174 va_start(args, fmt);
175 vprintf(fmt, args);
176 va_end(args);
177}
178
179static void print_usage(void)
180{
181 printf("rcubench [test-name] [k-iterations] [n-threads]\n");
182 printf("Available tests: \n");
183 printf(" sys-futex.. threads make wakeup/sleepdown futex syscalls in a loop\n");
184 printf(" but for separate variables/futex kernel objects.\n");
185 printf(" lock .. threads lock/unlock separate futexes.\n");
186 printf(" sema .. threads down/up separate futexes.\n");
187 printf("eg:\n");
188 printf(" rcubench sys-futex 100000 3\n");
189 printf(" rcubench lock 100000 2 ..runs futex_lock/unlock in a loop\n");
190 printf(" rcubench sema 100000 2 ..runs futex_down/up in a loop\n");
191 printf("Results are stored in %s\n", results_txt);
192}
193
194static bool parse_cmd_line(int argc, char **argv, bench_t *bench,
195 const char **err)
196{
197 if (argc < 4) {
198 *err = "Not enough parameters";
199 return false;
200 }
201
202 if (0 == str_cmp(argv[1], "sys-futex")) {
203 bench->func = kernel_futex_bench;
204 } else if (0 == str_cmp(argv[1], "lock")) {
205 bench->func = libc_futex_lock_bench;
206 } else if (0 == str_cmp(argv[1], "sema")) {
207 bench->func = libc_futex_sema_bench;
208 } else {
209 *err = "Unknown test name";
210 return false;
211 }
212
213 bench->name = argv[1];
214
215 /* Determine iteration count. */
216 uint32_t iter_cnt = 0;
217 errno_t ret = str_uint32_t(argv[2], NULL, 0, true, &iter_cnt);
218
219 if (ret == EOK && 1 <= iter_cnt) {
220 bench->iters = iter_cnt;
221 } else {
222 *err = "Err: Invalid number of iterations";
223 return false;
224 }
225
226 /* Determine thread count. */
227 uint32_t thread_cnt = 0;
228 ret = str_uint32_t(argv[3], NULL, 0, true, &thread_cnt);
229
230 if (ret == EOK && 1 <= thread_cnt && thread_cnt <= 64) {
231 bench->nthreads = thread_cnt;
232 } else {
233 *err = "Err: Invalid number of threads";
234 return false;
235 }
236
237 return true;
238}
239
240int main(int argc, char **argv)
241{
242 const char *err = "(error)";
243 bench_t bench;
244
245 fibril_semaphore_initialize(&bench.done_threads, 0);
246
247 if (!parse_cmd_line(argc, argv, &bench, &err)) {
248 printf("%s\n", err);
249 print_usage();
250 return -1;
251 }
252
253 open_results();
254
255 print_res("Running '%s' futex bench in '%zu' threads with '%zu' iterations.\n",
256 bench.name, bench.nthreads, bench.iters);
257
258 struct timeval start, end;
259 getuptime(&start);
260
261 run_threads_and_wait(&bench);
262
263 getuptime(&end);
264 int64_t duration = tv_sub_diff(&end, &start);
265
266 uint64_t secs = (uint64_t)duration / 1000 / 1000;
267 uint64_t total_iters = (uint64_t)bench.iters * bench.nthreads;
268 uint64_t iters_per_sec = 0;
269
270 if (0 < duration) {
271 iters_per_sec = total_iters * 1000 * 1000 / duration;
272 }
273
274 print_res("Completed %" PRIu64 " iterations in %" PRId64 " usecs (%" PRIu64
275 " secs); %" PRIu64 " iters/sec\n",
276 total_iters, duration, secs, iters_per_sec);
277
278 close_results();
279
280 return 0;
281}
282
283
284/**
285 * @}
286 */
Note: See TracBrowser for help on using the repository browser.