source: mainline/uspace/app/hbench/main.c@ 77b01fe

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 77b01fe was d926f42, checked in by Vojtech Horky <vojtech.horky@…>, 7 years ago

hbench: one header is enough

  • Property mode set to 100644
File size: 10.5 KB
Line 
1/*
2 * Copyright (c) 2018 Jiri Svoboda
3 * Copyright (c) 2018 Vojtech Horky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup hbench
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <assert.h>
38#include <getopt.h>
39#include <math.h>
40#include <stdio.h>
41#include <stddef.h>
42#include <stdlib.h>
43#include <str.h>
44#include <time.h>
45#include <errno.h>
46#include <str_error.h>
47#include <perf.h>
48#include <types/casting.h>
49#include "hbench.h"
50
51#define MIN_DURATION_SECS 10
52#define NUM_SAMPLES 10
53#define MAX_ERROR_STR_LENGTH 1024
54
55static void short_report(stopwatch_t *stopwatch, int run_index,
56 benchmark_t *bench, uint64_t workload_size)
57{
58 csv_report_add_entry(stopwatch, run_index, bench, workload_size);
59
60 usec_t duration_usec = NSEC2USEC(stopwatch_get_nanos(stopwatch));
61
62 printf("Completed %" PRIu64 " operations in %llu us",
63 workload_size, duration_usec);
64 if (duration_usec > 0) {
65 double nanos = stopwatch_get_nanos(stopwatch);
66 double thruput = (double) workload_size / (nanos / 1000000000.0l);
67 printf(", %.0f ops/s.\n", thruput);
68 } else {
69 printf(".\n");
70 }
71}
72
73/*
74 * This is a temporary solution until we have proper sqrt() implementation
75 * in libmath.
76 *
77 * The algorithm uses Babylonian method [1].
78 *
79 * [1] https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
80 */
81static double estimate_square_root(double value, double precision)
82{
83 double estimate = 1.;
84 double prev_estimate = estimate + 10 * precision;
85
86 while (fabs(estimate - prev_estimate) > precision) {
87 prev_estimate = estimate;
88 estimate = (prev_estimate + value / prev_estimate) / 2.;
89 }
90
91 return estimate;
92}
93
94/*
95 * Compute available statistics from given stopwatches.
96 *
97 * We compute normal mean for average duration of the workload and geometric
98 * mean for average thruput. Note that geometric mean is necessary to compute
99 * average throughput correctly - consider the following example:
100 * - we run always 60 operations,
101 * - first run executes in 30 s (i.e. 2 ops/s)
102 * - and second one in 10 s (6 ops/s).
103 * Then, naively, average throughput would be (2+6)/2 = 4 [ops/s]. However, we
104 * actually executed 60 + 60 ops in 30 + 10 seconds. So the actual average
105 * throughput is 3 ops/s (which is exactly what geometric mean means).
106 *
107 */
108static void compute_stats(stopwatch_t *stopwatch, size_t stopwatch_count,
109 uint64_t workload_size, double precision, double *out_duration_avg,
110 double *out_duration_sigma, double *out_thruput_avg)
111{
112 double inv_thruput_sum = 0.0;
113 double nanos_sum = 0.0;
114 double nanos_sum2 = 0.0;
115
116 for (size_t i = 0; i < stopwatch_count; i++) {
117 double nanos = stopwatch_get_nanos(&stopwatch[i]);
118 double thruput = (double) workload_size / nanos;
119
120 inv_thruput_sum += 1.0 / thruput;
121 nanos_sum += nanos;
122 nanos_sum2 += nanos * nanos;
123 }
124 *out_duration_avg = nanos_sum / stopwatch_count;
125 double sigma2 = (nanos_sum2 - nanos_sum * (*out_duration_avg)) /
126 ((double) stopwatch_count - 1);
127 // FIXME: implement sqrt properly
128 *out_duration_sigma = estimate_square_root(sigma2, precision);
129 *out_thruput_avg = 1.0 / (inv_thruput_sum / stopwatch_count);
130}
131
132static void summary_stats(stopwatch_t *stopwatch, size_t stopwatch_count,
133 benchmark_t *bench, uint64_t workload_size)
134{
135 double duration_avg, duration_sigma, thruput_avg;
136 compute_stats(stopwatch, stopwatch_count, workload_size, 0.001,
137 &duration_avg, &duration_sigma, &thruput_avg);
138
139 printf("Average: %" PRIu64 " ops in %.0f us (sd %.0f us); "
140 "%.0f ops/s; Samples: %zu\n",
141 workload_size, duration_avg / 1000.0, duration_sigma / 1000.0,
142 thruput_avg * 1000000000.0, stopwatch_count);
143}
144
145static bool run_benchmark(benchmark_t *bench)
146{
147 printf("Warm up and determine workload size...\n");
148
149 char *error_msg = malloc(MAX_ERROR_STR_LENGTH + 1);
150 if (error_msg == NULL) {
151 printf("Out of memory!\n");
152 return false;
153 }
154 str_cpy(error_msg, MAX_ERROR_STR_LENGTH, "");
155
156 bool ret = true;
157
158 if (bench->setup != NULL) {
159 ret = bench->setup(error_msg, MAX_ERROR_STR_LENGTH);
160 if (!ret) {
161 goto leave_error;
162 }
163 }
164
165 /*
166 * Find workload size that is big enough to last few seconds.
167 * We also check that uint64_t is big enough.
168 */
169 uint64_t workload_size = 0;
170 for (size_t bits = 0; bits <= 64; bits++) {
171 if (bits == 64) {
172 str_cpy(error_msg, MAX_ERROR_STR_LENGTH, "Workload too small even for 1 << 63");
173 goto leave_error;
174 }
175 workload_size = ((uint64_t) 1) << bits;
176
177 stopwatch_t stopwatch = STOPWATCH_INITIALIZE_STATIC;
178
179 bool ok = bench->entry(&stopwatch, workload_size,
180 error_msg, MAX_ERROR_STR_LENGTH);
181 if (!ok) {
182 goto leave_error;
183 }
184 short_report(&stopwatch, -1, bench, workload_size);
185
186 nsec_t duration = stopwatch_get_nanos(&stopwatch);
187 if (duration > SEC2NSEC(MIN_DURATION_SECS)) {
188 break;
189 }
190 }
191
192 printf("Workload size set to %" PRIu64 ", measuring %d samples.\n", workload_size, NUM_SAMPLES);
193
194 stopwatch_t *stopwatch = calloc(NUM_SAMPLES, sizeof(stopwatch_t));
195 if (stopwatch == NULL) {
196 snprintf(error_msg, MAX_ERROR_STR_LENGTH, "failed allocating memory");
197 goto leave_error;
198 }
199 for (int i = 0; i < NUM_SAMPLES; i++) {
200 stopwatch_init(&stopwatch[i]);
201
202 bool ok = bench->entry(&stopwatch[i], workload_size,
203 error_msg, MAX_ERROR_STR_LENGTH);
204 if (!ok) {
205 free(stopwatch);
206 goto leave_error;
207 }
208 short_report(&stopwatch[i], i, bench, workload_size);
209 }
210
211 summary_stats(stopwatch, NUM_SAMPLES, bench, workload_size);
212 printf("\nBenchmark completed\n");
213
214 free(stopwatch);
215
216 goto leave;
217
218leave_error:
219 printf("Error: %s\n", error_msg);
220 ret = false;
221
222leave:
223 if (bench->teardown != NULL) {
224 bool ok = bench->teardown(error_msg, MAX_ERROR_STR_LENGTH);
225 if (!ok) {
226 printf("Error: %s\n", error_msg);
227 ret = false;
228 }
229 }
230
231 free(error_msg);
232
233 return ret;
234}
235
236static int run_benchmarks(void)
237{
238 unsigned int count_ok = 0;
239 unsigned int count_fail = 0;
240
241 char *failed_names = NULL;
242
243 printf("\n*** Running all benchmarks ***\n\n");
244
245 for (size_t it = 0; it < benchmark_count; it++) {
246 printf("%s (%s)\n", benchmarks[it]->name, benchmarks[it]->desc);
247 if (run_benchmark(benchmarks[it])) {
248 count_ok++;
249 continue;
250 }
251
252 if (!failed_names) {
253 failed_names = str_dup(benchmarks[it]->name);
254 } else {
255 char *f = NULL;
256 asprintf(&f, "%s, %s", failed_names, benchmarks[it]->name);
257 if (!f) {
258 printf("Out of memory.\n");
259 abort();
260 }
261 free(failed_names);
262 failed_names = f;
263 }
264 count_fail++;
265 }
266
267 printf("\nCompleted, %u benchmarks run, %u succeeded.\n",
268 count_ok + count_fail, count_ok);
269 if (failed_names)
270 printf("Failed benchmarks: %s\n", failed_names);
271
272 return count_fail;
273}
274
275static void list_benchmarks(void)
276{
277 size_t len = 0;
278 for (size_t i = 0; i < benchmark_count; i++) {
279 size_t len_now = str_length(benchmarks[i]->name);
280 if (len_now > len)
281 len = len_now;
282 }
283
284 assert(can_cast_size_t_to_int(len) && "benchmark name length overflow");
285
286 for (size_t i = 0; i < benchmark_count; i++)
287 printf(" %-*s %s\n", (int) len, benchmarks[i]->name, benchmarks[i]->desc);
288
289 printf(" %-*s Run all benchmarks\n", (int) len, "*");
290}
291
292static void print_usage(const char *progname)
293{
294 printf("Usage: %s [options] <benchmark>\n", progname);
295 printf("-h, --help "
296 "Print this help and exit\n");
297 printf("-o, --output filename.csv "
298 "Store machine-readable data in filename.csv\n");
299 printf("-p, --param KEY=VALUE "
300 "Additional parameters for the benchmark\n");
301 printf("<benchmark> is one of the following:\n");
302 list_benchmarks();
303}
304
305static void handle_param_arg(char *arg)
306{
307 char *value = NULL;
308 char *key = str_tok(arg, "=", &value);
309 bench_param_set(key, value);
310}
311
312int main(int argc, char *argv[])
313{
314 errno_t rc = bench_param_init();
315 if (rc != EOK) {
316 fprintf(stderr, "Failed to initialize internal params structure: %s\n",
317 str_error(rc));
318 return -5;
319 }
320
321 const char *short_options = "ho:p:";
322 struct option long_options[] = {
323 { "help", optional_argument, NULL, 'h' },
324 { "param", required_argument, NULL, 'p' },
325 { "output", required_argument, NULL, 'o' },
326 { 0, 0, NULL, 0 }
327 };
328
329 char *csv_output_filename = NULL;
330
331 int opt = 0;
332 while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) > 0) {
333 switch (opt) {
334 case 'h':
335 print_usage(*argv);
336 return 0;
337 case 'o':
338 csv_output_filename = optarg;
339 break;
340 case 'p':
341 handle_param_arg(optarg);
342 break;
343 case -1:
344 default:
345 break;
346 }
347 }
348
349 if (optind + 1 != argc) {
350 print_usage(*argv);
351 fprintf(stderr, "Error: specify one benchmark to run or * for all.\n");
352 return -3;
353 }
354
355 const char *benchmark = argv[optind];
356
357 if (csv_output_filename != NULL) {
358 errno_t rc = csv_report_open(csv_output_filename);
359 if (rc != EOK) {
360 fprintf(stderr, "Failed to open CSV report '%s': %s\n",
361 csv_output_filename, str_error(rc));
362 return -4;
363 }
364 }
365
366 int exit_code = 0;
367
368 if (str_cmp(benchmark, "*") == 0) {
369 exit_code = run_benchmarks();
370 } else {
371 bool benchmark_exists = false;
372 for (size_t i = 0; i < benchmark_count; i++) {
373 if (str_cmp(benchmark, benchmarks[i]->name) == 0) {
374 benchmark_exists = true;
375 exit_code = run_benchmark(benchmarks[i]) ? 0 : -1;
376 break;
377 }
378 }
379 if (!benchmark_exists) {
380 printf("Unknown benchmark \"%s\"\n", benchmark);
381 exit_code = -2;
382 }
383 }
384
385 csv_report_close();
386 bench_param_cleanup();
387
388 return exit_code;
389}
390
391/** @}
392 */
Note: See TracBrowser for help on using the repository browser.