source: mainline/uspace/app/rcubench/rcubench.c@ 6a585dd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6a585dd was 6a585dd, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

rcubench: Removed unused code.

  • Property mode set to 100644
File size: 7.0 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 <thread.h>
43#include <assert.h>
44#include <async.h>
45#include <fibril.h>
46#include <fibril_synch.h>
47#include <compiler/barrier.h>
48#include <futex.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 void 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}
115
116static void run_threads_and_wait(bench_t *bench)
117{
118 assert(1 <= bench->nthreads);
119
120 if (2 <= bench->nthreads) {
121 printf("Creating %zu additional threads...\n", bench->nthreads - 1);
122 }
123
124 /* Create and run the first nthreads - 1 threads.*/
125 for (size_t k = 1; k < bench->nthreads; ++k) {
126 thread_id_t tid;
127 /* Also sets up a fibril for the thread. */
128 int ret = thread_create(thread_func, bench, "rcubench-t", &tid);
129 if (ret != EOK) {
130 printf("Error: Failed to create benchmark thread.\n");
131 abort();
132 }
133 thread_detach(tid);
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 futex_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 futex_initialize(&bench->bench_fut, 1);
203
204 if (0 == str_cmp(argv[1], "sys-futex")) {
205 bench->func = kernel_futex_bench;
206 } else if (0 == str_cmp(argv[1], "lock")) {
207 bench->func = libc_futex_lock_bench;
208 } else if (0 == str_cmp(argv[1], "sema")) {
209 bench->func = libc_futex_sema_bench;
210 } else {
211 *err = "Unknown test name";
212 return false;
213 }
214
215 bench->name = argv[1];
216
217 /* Determine iteration count. */
218 uint32_t iter_cnt = 0;
219 int ret = str_uint32_t(argv[2], NULL, 0, true, &iter_cnt);
220
221 if (ret == EOK && 1 <= iter_cnt) {
222 bench->iters = iter_cnt;
223 } else {
224 *err = "Err: Invalid number of iterations";
225 return false;
226 }
227
228 /* Determine thread count. */
229 uint32_t thread_cnt = 0;
230 ret = str_uint32_t(argv[3], NULL, 0, true, &thread_cnt);
231
232 if (ret == EOK && 1 <= thread_cnt && thread_cnt <= 64) {
233 bench->nthreads = thread_cnt;
234 } else {
235 *err = "Err: Invalid number of threads";
236 return false;
237 }
238
239 return true;
240}
241
242int main(int argc, char **argv)
243{
244 const char *err = "(error)";
245 bench_t bench;
246
247 futex_initialize(&bench.done_threads, 0);
248
249 if (!parse_cmd_line(argc, argv, &bench, &err)) {
250 printf("%s\n", err);
251 print_usage();
252 return -1;
253 }
254
255 open_results();
256
257 print_res("Running '%s' futex bench in '%zu' threads with '%zu' iterations.\n",
258 bench.name, bench.nthreads, bench.iters);
259
260 struct timeval start, end;
261 getuptime(&start);
262
263 run_threads_and_wait(&bench);
264
265 getuptime(&end);
266 int64_t duration = tv_sub(&end, &start);
267
268 uint64_t secs = (uint64_t)duration / 1000 / 1000;
269 uint64_t total_iters = (uint64_t)bench.iters * bench.nthreads;
270 uint64_t iters_per_sec = 0;
271
272 if (0 < duration) {
273 iters_per_sec = total_iters * 1000 * 1000 / duration;
274 }
275
276 print_res("Completed %" PRIu64 " iterations in %" PRId64 " usecs (%" PRIu64
277 " secs); %" PRIu64 " iters/sec\n",
278 total_iters, duration, secs, iters_per_sec);
279
280 close_results();
281
282 return 0;
283}
284
285
286/**
287 * @}
288 */
Note: See TracBrowser for help on using the repository browser.