source: mainline/uspace/app/rcubench/rcubench.c@ d54b303

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

Fixed division by zero in rcubench.

  • Property mode set to 100644
File size: 7.5 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
52typedef struct bench {
53 enum {
54 T_KERN_FUTEX,
55 T_LIBC_FUTEX
56 } type;
57 size_t iters;
58 size_t nthreads;
59 size_t array_size;
60 size_t *array;
61 futex_t done_threads;
62
63 futex_t ke_bench_fut;
64 fibril_mutex_t libc_bench_mtx;
65} bench_t;
66
67
68/* Combats compiler optimizations. */
69static volatile size_t dummy = 0;
70
71static size_t sum_array(size_t *array, size_t len)
72{
73 size_t sum = 0;
74
75 for (size_t k = 0; k < len; ++k)
76 sum += array[k];
77
78 return sum;
79}
80
81static void kernel_futex_bench(bench_t *bench)
82{
83 futex_t * const fut = &bench->ke_bench_fut;
84 const size_t iters = bench->iters;
85 size_t sum = 0;
86
87 for (size_t i = 0; i < iters; ++i) {
88 /* Do some work with the futex locked to encourage contention. */
89 futex_down(fut);
90 sum += sum_array(bench->array, bench->array_size);
91 futex_up(fut);
92
93 /*
94 * Do half as much work to give other threads a chance to acquire
95 * the futex.
96 */
97 sum += sum_array(bench->array, bench->array_size / 2);
98 }
99
100 /*
101 * Writing to a global volatile variable separated with a cc-barrier
102 * should discourage the compiler from optimizing away sum_array()s.
103 */
104 compiler_barrier();
105 dummy = sum;
106}
107
108static void libc_futex_bench(bench_t *bench)
109{
110 fibril_mutex_t * const mtx = &bench->libc_bench_mtx;
111 const size_t iters = bench->iters;
112
113 for (size_t i = 0; i < iters; ++i) {
114 fibril_mutex_lock(mtx);
115 /* no-op */
116 compiler_barrier();
117 fibril_mutex_unlock(mtx);
118 }
119}
120
121
122static void thread_func(void *arg)
123{
124 bench_t *bench = (bench_t*)arg;
125
126 switch (bench->type) {
127 case T_KERN_FUTEX:
128 kernel_futex_bench(bench);
129 break;
130 case T_LIBC_FUTEX:
131 libc_futex_bench(bench);
132 break;
133 default:
134 assert(false);
135 }
136
137 /* Signal another thread completed. */
138 futex_up(&bench->done_threads);
139}
140
141static void run_threads_and_wait(bench_t *bench)
142{
143 assert(1 <= bench->nthreads);
144
145 if (2 <= bench->nthreads) {
146 printf("Creating %zu additional threads...\n", bench->nthreads - 1);
147 }
148
149 /* Create and run the first nthreads - 1 threads.*/
150 for (size_t k = 1; k < bench->nthreads; ++k) {
151 thread_id_t tid;
152 /* Also sets up a fibril for the thread. */
153 int ret = thread_create(thread_func, bench, "rcubench-t", &tid);
154 if (ret != EOK) {
155 printf("Error: Failed to create benchmark thread.\n");
156 abort();
157 }
158 thread_detach(tid);
159 }
160
161 /*
162 * Run the last thread in place so that we create multiple threads
163 * only when needed. Otherwise libc would immediately upgrade
164 * single-threaded futexes to proper multithreaded futexes
165 */
166 thread_func(bench);
167
168 printf("Waiting for remaining threads to complete.\n");
169
170 /* Wait for threads to complete. */
171 for (size_t k = 0; k < bench->nthreads; ++k) {
172 futex_down(&bench->done_threads);
173 }
174}
175
176static void print_usage(void)
177{
178 printf("rcubench [test-name] [k-iterations] [n-threads] {work-size}\n");
179 printf("eg:\n");
180 printf(" rcubench ke 100000 3 4\n");
181 printf(" rcubench libc 100000 2\n");
182 printf(" rcubench def-ke \n");
183 printf(" rcubench def-libc\n");
184}
185
186static bool parse_cmd_line(int argc, char **argv, bench_t *bench,
187 const char **err)
188{
189 if (argc < 2) {
190 *err = "Benchmark name not specified";
191 return false;
192 }
193
194 futex_initialize(&bench->ke_bench_fut, 1);
195 fibril_mutex_initialize(&bench->libc_bench_mtx);
196
197 if (0 == str_cmp(argv[1], "def-ke")) {
198 bench->type = T_KERN_FUTEX;
199 bench->nthreads = 4;
200 bench->iters = 1000 * 1000;
201 bench->array_size = 10;
202 bench->array = malloc(bench->array_size * sizeof(size_t));
203 return NULL != bench->array;
204 } else if (0 == str_cmp(argv[1], "def-libc")) {
205 bench->type = T_LIBC_FUTEX;
206 bench->nthreads = 4;
207 bench->iters = 1000 * 1000;
208 bench->array_size = 0;
209 bench->array = NULL;
210 return true;
211 } else if (0 == str_cmp(argv[1], "ke")) {
212 bench->type = T_KERN_FUTEX;
213 } else if (0 == str_cmp(argv[1], "libc")) {
214 bench->type = T_LIBC_FUTEX;
215 } else {
216 *err = "Unknown test name";
217 return false;
218 }
219
220 if (argc < 4) {
221 *err = "Not enough parameters";
222 return false;
223 }
224
225 uint32_t iter_cnt = 0;
226 int ret = str_uint32_t(argv[2], NULL, 0, true, &iter_cnt);
227
228 if (ret == EOK && 1 <= iter_cnt) {
229 bench->iters = iter_cnt;
230 } else {
231 *err = "Err: Invalid number of iterations";
232 return false;
233 }
234
235 uint32_t thread_cnt = 0;
236 ret = str_uint32_t(argv[3], NULL, 0, true, &thread_cnt);
237
238 if (ret == EOK && 1 <= thread_cnt && thread_cnt <= 64) {
239 bench->nthreads = thread_cnt;
240 } else {
241 *err = "Err: Invalid number of threads";
242 return false;
243 }
244
245 if (argc > 4) {
246 uint32_t work_size = 0;
247 ret = str_uint32_t(argv[4], NULL, 0, true, &work_size);
248
249 if (ret == EOK && work_size <= 10000) {
250 bench->array_size = work_size;
251 } else {
252 *err = "Err: Work size too large";
253 return false;
254 }
255 } else {
256 bench->array_size = 0;
257 }
258
259 if (0 < bench->array_size) {
260 bench->array = malloc(bench->array_size * sizeof(size_t));
261 if (!bench->array) {
262 *err = "Err: Failed to allocate work array";
263 return false;
264 }
265 } else {
266 bench->array = NULL;
267 }
268
269 return true;
270}
271
272int main(int argc, char **argv)
273{
274 const char *err = "(error)";
275 bench_t bench;
276
277 futex_initialize(&bench.done_threads, 0);
278
279 if (!parse_cmd_line(argc, argv, &bench, &err)) {
280 printf("%s\n", err);
281 print_usage();
282 return -1;
283 }
284
285 printf("Running '%s' futex bench in '%zu' threads with '%zu' iterations.\n",
286 bench.type == T_KERN_FUTEX ? "kernel" : "libc",
287 bench.nthreads, bench.iters);
288
289 struct timeval start, end;
290 getuptime(&start);
291
292 run_threads_and_wait(&bench);
293
294 getuptime(&end);
295 int64_t duration = tv_sub(&end, &start);
296
297 if (0 == duration)
298 duration = 1;
299
300 uint64_t secs = (uint64_t)duration / 1000 / 1000;
301 uint64_t total_iters = (uint64_t)bench.iters * bench.nthreads;
302 uint64_t iters_per_sec = total_iters * 1000 * 1000 / duration;
303
304 printf("Completed %" PRIu64 " iterations in %" PRId64 " usecs (%" PRIu64
305 " secs); %" PRIu64 " iters/sec\n",
306 total_iters, duration, secs, iters_per_sec);
307
308 return 0;
309}
310
311
312/**
313 * @}
314 */
Note: See TracBrowser for help on using the repository browser.