source: mainline/uspace/app/rcubench/rcubench.c@ 40abf56a

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

Privatize <thread.h>.

  • 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 futex_t done_threads;
62
63 futex_t bench_fut;
64} bench_t;
65
66
67
68
69static void kernel_futex_bench(bench_t *bench)
70{
71 const size_t iters = bench->iters;
72 int val = 0;
73
74 for (size_t i = 0; i < iters; ++i) {
75 __SYSCALL1(SYS_FUTEX_WAKEUP, (sysarg_t) &val);
76 __SYSCALL1(SYS_FUTEX_SLEEP, (sysarg_t) &val);
77 }
78}
79
80static void libc_futex_lock_bench(bench_t *bench)
81{
82 const size_t iters = bench->iters;
83 futex_t loc_fut = FUTEX_INITIALIZER;
84
85 for (size_t i = 0; i < iters; ++i) {
86 futex_lock(&loc_fut);
87 /* no-op */
88 compiler_barrier();
89 futex_unlock(&loc_fut);
90 }
91}
92
93static void libc_futex_sema_bench(bench_t *bench)
94{
95 const size_t iters = bench->iters;
96 futex_t loc_fut = FUTEX_INITIALIZER;
97
98 for (size_t i = 0; i < iters; ++i) {
99 futex_down(&loc_fut);
100 /* no-op */
101 compiler_barrier();
102 futex_up(&loc_fut);
103 }
104}
105
106static errno_t thread_func(void *arg)
107{
108 bench_t *bench = (bench_t *)arg;
109
110 bench->func(bench);
111
112 /* Signal another thread completed. */
113 futex_up(&bench->done_threads);
114 return EOK;
115}
116
117static void run_threads_and_wait(bench_t *bench)
118{
119 assert(1 <= bench->nthreads);
120
121 if (2 <= bench->nthreads) {
122 printf("Creating %zu additional threads...\n", bench->nthreads - 1);
123 }
124
125 fibril_test_spawn_runners(bench->nthreads - 1);
126
127 /* Create and run the first nthreads - 1 threads.*/
128 for (size_t k = 1; k < bench->nthreads; ++k) {
129 fid_t f = fibril_create(thread_func, bench);
130 if (!f) {
131 printf("Error: Failed to create benchmark thread.\n");
132 abort();
133 }
134 fibril_detach(f);
135 fibril_add_ready(f);
136 }
137
138 /*
139 * Run the last thread in place so that we create multiple threads
140 * only when needed. Otherwise libc would immediately upgrade
141 * single-threaded futexes to proper multithreaded futexes
142 */
143 thread_func(bench);
144
145 printf("Waiting for remaining threads to complete.\n");
146
147 /* Wait for threads to complete. */
148 for (size_t k = 0; k < bench->nthreads; ++k) {
149 futex_down(&bench->done_threads);
150 }
151}
152
153static const char *results_txt = "/tmp/urcu-bench-results.txt";
154
155static bool open_results(void)
156{
157 results_fd = fopen(results_txt, "a");
158 return NULL != results_fd;
159}
160
161static void close_results(void)
162{
163 if (results_fd) {
164 fclose(results_fd);
165 }
166}
167
168static void print_res(const char *fmt, ...)
169{
170 va_list args;
171
172 va_start(args, fmt);
173 vfprintf(results_fd, fmt, args);
174 va_end(args);
175
176 va_start(args, fmt);
177 vprintf(fmt, args);
178 va_end(args);
179}
180
181static void print_usage(void)
182{
183 printf("rcubench [test-name] [k-iterations] [n-threads]\n");
184 printf("Available tests: \n");
185 printf(" sys-futex.. threads make wakeup/sleepdown futex syscalls in a loop\n");
186 printf(" but for separate variables/futex kernel objects.\n");
187 printf(" lock .. threads lock/unlock separate futexes.\n");
188 printf(" sema .. threads down/up separate futexes.\n");
189 printf("eg:\n");
190 printf(" rcubench sys-futex 100000 3\n");
191 printf(" rcubench lock 100000 2 ..runs futex_lock/unlock in a loop\n");
192 printf(" rcubench sema 100000 2 ..runs futex_down/up in a loop\n");
193 printf("Results are stored in %s\n", results_txt);
194}
195
196static bool parse_cmd_line(int argc, char **argv, bench_t *bench,
197 const char **err)
198{
199 if (argc < 4) {
200 *err = "Not enough parameters";
201 return false;
202 }
203
204 futex_initialize(&bench->bench_fut, 1);
205
206 if (0 == str_cmp(argv[1], "sys-futex")) {
207 bench->func = kernel_futex_bench;
208 } else if (0 == str_cmp(argv[1], "lock")) {
209 bench->func = libc_futex_lock_bench;
210 } else if (0 == str_cmp(argv[1], "sema")) {
211 bench->func = libc_futex_sema_bench;
212 } else {
213 *err = "Unknown test name";
214 return false;
215 }
216
217 bench->name = argv[1];
218
219 /* Determine iteration count. */
220 uint32_t iter_cnt = 0;
221 errno_t ret = str_uint32_t(argv[2], NULL, 0, true, &iter_cnt);
222
223 if (ret == EOK && 1 <= iter_cnt) {
224 bench->iters = iter_cnt;
225 } else {
226 *err = "Err: Invalid number of iterations";
227 return false;
228 }
229
230 /* Determine thread count. */
231 uint32_t thread_cnt = 0;
232 ret = str_uint32_t(argv[3], NULL, 0, true, &thread_cnt);
233
234 if (ret == EOK && 1 <= thread_cnt && thread_cnt <= 64) {
235 bench->nthreads = thread_cnt;
236 } else {
237 *err = "Err: Invalid number of threads";
238 return false;
239 }
240
241 return true;
242}
243
244int main(int argc, char **argv)
245{
246 const char *err = "(error)";
247 bench_t bench;
248
249 futex_initialize(&bench.done_threads, 0);
250
251 if (!parse_cmd_line(argc, argv, &bench, &err)) {
252 printf("%s\n", err);
253 print_usage();
254 return -1;
255 }
256
257 open_results();
258
259 print_res("Running '%s' futex bench in '%zu' threads with '%zu' iterations.\n",
260 bench.name, bench.nthreads, bench.iters);
261
262 struct timeval start, end;
263 getuptime(&start);
264
265 run_threads_and_wait(&bench);
266
267 getuptime(&end);
268 int64_t duration = tv_sub_diff(&end, &start);
269
270 uint64_t secs = (uint64_t)duration / 1000 / 1000;
271 uint64_t total_iters = (uint64_t)bench.iters * bench.nthreads;
272 uint64_t iters_per_sec = 0;
273
274 if (0 < duration) {
275 iters_per_sec = total_iters * 1000 * 1000 / duration;
276 }
277
278 print_res("Completed %" PRIu64 " iterations in %" PRId64 " usecs (%" PRIu64
279 " secs); %" PRIu64 " iters/sec\n",
280 total_iters, duration, secs, iters_per_sec);
281
282 close_results();
283
284 return 0;
285}
286
287
288/**
289 * @}
290 */
Note: See TracBrowser for help on using the repository browser.