2013-03-15 15:00:03 +08:00
|
|
|
/* Skeleton for benchmark programs.
|
2019-01-01 08:11:28 +08:00
|
|
|
Copyright (C) 2013-2019 Free Software Foundation, Inc.
|
2013-03-15 15:00:03 +08:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
2014-03-29 12:10:19 +08:00
|
|
|
#include <stdbool.h>
|
2013-03-15 15:00:03 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <inttypes.h>
|
2013-05-13 16:14:32 +08:00
|
|
|
#include "bench-timing.h"
|
2014-04-08 17:18:16 +08:00
|
|
|
#include "json-lib.h"
|
2015-09-18 23:02:38 +08:00
|
|
|
#include "bench-util.h"
|
2013-03-15 15:00:03 +08:00
|
|
|
|
2015-09-18 23:02:38 +08:00
|
|
|
#include "bench-util.c"
|
2013-05-08 14:21:05 +08:00
|
|
|
|
2013-04-30 16:40:20 +08:00
|
|
|
#define TIMESPEC_AFTER(a, b) \
|
|
|
|
(((a).tv_sec == (b).tv_sec) ? \
|
|
|
|
((a).tv_nsec > (b).tv_nsec) : \
|
2013-04-30 16:47:57 +08:00
|
|
|
((a).tv_sec > (b).tv_sec))
|
2013-03-15 15:00:03 +08:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2013-04-30 16:40:20 +08:00
|
|
|
unsigned long i, k;
|
2013-05-13 16:14:32 +08:00
|
|
|
struct timespec runtime;
|
|
|
|
timing_t start, end;
|
2014-03-29 12:10:19 +08:00
|
|
|
bool detailed = false;
|
2014-04-08 17:18:16 +08:00
|
|
|
json_ctx_t json_ctx;
|
2014-03-29 12:10:19 +08:00
|
|
|
|
|
|
|
if (argc == 2 && !strcmp (argv[1], "-d"))
|
|
|
|
detailed = true;
|
2013-03-15 15:00:03 +08:00
|
|
|
|
2015-09-18 23:02:38 +08:00
|
|
|
bench_start ();
|
2013-05-08 14:21:05 +08:00
|
|
|
|
2013-04-30 16:40:20 +08:00
|
|
|
memset (&runtime, 0, sizeof (runtime));
|
2013-03-15 15:00:03 +08:00
|
|
|
|
2013-09-11 20:07:33 +08:00
|
|
|
unsigned long iters, res;
|
2013-03-15 15:00:03 +08:00
|
|
|
|
2014-02-22 12:39:27 +08:00
|
|
|
#ifdef BENCH_INIT
|
|
|
|
BENCH_INIT ();
|
|
|
|
#endif
|
2013-09-11 20:07:33 +08:00
|
|
|
TIMING_INIT (res);
|
|
|
|
|
|
|
|
iters = 1000 * res;
|
2013-03-15 15:00:03 +08:00
|
|
|
|
2014-04-08 17:18:16 +08:00
|
|
|
json_init (&json_ctx, 2, stdout);
|
|
|
|
|
2014-03-29 12:07:44 +08:00
|
|
|
/* Begin function. */
|
2014-04-08 17:18:16 +08:00
|
|
|
json_attr_object_begin (&json_ctx, FUNCNAME);
|
2014-03-29 12:07:44 +08:00
|
|
|
|
2013-04-30 16:47:57 +08:00
|
|
|
for (int v = 0; v < NUM_VARIANTS; v++)
|
2013-03-15 15:00:03 +08:00
|
|
|
{
|
2013-04-30 16:47:57 +08:00
|
|
|
/* Run for approximately DURATION seconds. */
|
|
|
|
clock_gettime (CLOCK_MONOTONIC_RAW, &runtime);
|
|
|
|
runtime.tv_sec += DURATION;
|
2013-03-15 15:00:03 +08:00
|
|
|
|
2017-06-20 23:26:26 +08:00
|
|
|
bool is_bench = strncmp (VARIANT (v), "workload-", 9) == 0;
|
2013-04-30 16:47:57 +08:00
|
|
|
double d_total_i = 0;
|
2013-05-13 16:14:32 +08:00
|
|
|
timing_t total = 0, max = 0, min = 0x7fffffffffffffff;
|
2017-08-17 23:27:20 +08:00
|
|
|
timing_t throughput = 0, latency = 0;
|
2014-03-29 12:10:19 +08:00
|
|
|
int64_t c = 0;
|
2017-06-20 23:26:26 +08:00
|
|
|
uint64_t cur;
|
2017-08-17 23:27:20 +08:00
|
|
|
BENCH_VARS;
|
2013-04-30 16:47:57 +08:00
|
|
|
while (1)
|
|
|
|
{
|
2017-06-20 23:26:26 +08:00
|
|
|
if (is_bench)
|
2013-04-30 16:47:57 +08:00
|
|
|
{
|
2017-06-20 23:26:26 +08:00
|
|
|
/* Benchmark a real trace of calls - all samples are iterated
|
|
|
|
over once before repeating. This models actual use more
|
|
|
|
accurately than repeating the same sample many times. */
|
2013-05-13 16:14:32 +08:00
|
|
|
TIMING_NOW (start);
|
2013-04-30 16:47:57 +08:00
|
|
|
for (k = 0; k < iters; k++)
|
2017-06-20 23:26:26 +08:00
|
|
|
for (i = 0; i < NUM_SAMPLES (v); i++)
|
|
|
|
BENCH_FUNC (v, i);
|
2013-05-13 16:14:32 +08:00
|
|
|
TIMING_NOW (end);
|
|
|
|
TIMING_DIFF (cur, start, end);
|
2017-08-17 23:27:20 +08:00
|
|
|
TIMING_ACCUM (throughput, cur);
|
|
|
|
|
|
|
|
TIMING_NOW (start);
|
|
|
|
for (k = 0; k < iters; k++)
|
|
|
|
for (i = 0; i < NUM_SAMPLES (v); i++)
|
|
|
|
BENCH_FUNC_LAT (v, i);
|
|
|
|
TIMING_NOW (end);
|
|
|
|
TIMING_DIFF (cur, start, end);
|
|
|
|
TIMING_ACCUM (latency, cur);
|
|
|
|
|
2017-06-20 23:26:26 +08:00
|
|
|
d_total_i += iters * NUM_SAMPLES (v);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for (i = 0; i < NUM_SAMPLES (v); i++)
|
|
|
|
{
|
|
|
|
TIMING_NOW (start);
|
|
|
|
for (k = 0; k < iters; k++)
|
|
|
|
BENCH_FUNC (v, i);
|
|
|
|
TIMING_NOW (end);
|
2013-03-15 15:00:03 +08:00
|
|
|
|
2017-06-20 23:26:26 +08:00
|
|
|
TIMING_DIFF (cur, start, end);
|
2013-03-15 15:00:03 +08:00
|
|
|
|
2017-06-20 23:26:26 +08:00
|
|
|
if (cur > max)
|
|
|
|
max = cur;
|
2013-04-30 16:40:20 +08:00
|
|
|
|
2017-06-20 23:26:26 +08:00
|
|
|
if (cur < min)
|
|
|
|
min = cur;
|
2014-03-29 12:10:19 +08:00
|
|
|
|
2017-06-20 23:26:26 +08:00
|
|
|
TIMING_ACCUM (total, cur);
|
|
|
|
/* Accumulate timings for the value. In the end we will divide
|
|
|
|
by the total iterations. */
|
|
|
|
RESULT_ACCUM (cur, v, i, c * iters, (c + 1) * iters);
|
|
|
|
|
|
|
|
d_total_i += iters;
|
|
|
|
}
|
2014-03-29 12:10:19 +08:00
|
|
|
c++;
|
2013-04-30 16:47:57 +08:00
|
|
|
struct timespec curtime;
|
2013-04-30 16:40:20 +08:00
|
|
|
|
2013-04-30 16:47:57 +08:00
|
|
|
memset (&curtime, 0, sizeof (curtime));
|
|
|
|
clock_gettime (CLOCK_MONOTONIC_RAW, &curtime);
|
|
|
|
if (TIMESPEC_AFTER (curtime, runtime))
|
|
|
|
goto done;
|
|
|
|
}
|
2013-03-15 15:00:03 +08:00
|
|
|
|
2013-04-30 16:47:57 +08:00
|
|
|
double d_total_s;
|
|
|
|
double d_iters;
|
2013-04-30 16:40:20 +08:00
|
|
|
|
2013-04-30 16:47:57 +08:00
|
|
|
done:
|
2013-05-13 16:14:32 +08:00
|
|
|
d_total_s = total;
|
2013-04-30 16:47:57 +08:00
|
|
|
d_iters = iters;
|
2013-04-30 16:40:20 +08:00
|
|
|
|
2014-04-08 17:18:16 +08:00
|
|
|
/* Begin variant. */
|
|
|
|
json_attr_object_begin (&json_ctx, VARIANT (v));
|
|
|
|
|
2017-06-20 23:26:26 +08:00
|
|
|
if (is_bench)
|
2017-08-17 23:27:20 +08:00
|
|
|
{
|
2018-08-11 21:15:03 +08:00
|
|
|
json_attr_double (&json_ctx, "duration", throughput + latency);
|
|
|
|
json_attr_double (&json_ctx, "iterations", 2 * d_total_i);
|
2017-08-17 23:27:20 +08:00
|
|
|
json_attr_double (&json_ctx, "reciprocal-throughput",
|
|
|
|
throughput / d_total_i);
|
|
|
|
json_attr_double (&json_ctx, "latency", latency / d_total_i);
|
|
|
|
json_attr_double (&json_ctx, "max-throughput",
|
|
|
|
d_total_i / throughput * 1000000000.0);
|
|
|
|
json_attr_double (&json_ctx, "min-throughput",
|
|
|
|
d_total_i / latency * 1000000000.0);
|
|
|
|
}
|
2017-06-20 23:26:26 +08:00
|
|
|
else
|
|
|
|
{
|
2017-08-17 23:27:20 +08:00
|
|
|
json_attr_double (&json_ctx, "duration", d_total_s);
|
|
|
|
json_attr_double (&json_ctx, "iterations", d_total_i);
|
2017-06-20 23:26:26 +08:00
|
|
|
json_attr_double (&json_ctx, "max", max / d_iters);
|
|
|
|
json_attr_double (&json_ctx, "min", min / d_iters);
|
|
|
|
json_attr_double (&json_ctx, "mean", d_total_s / d_total_i);
|
|
|
|
}
|
2014-03-29 12:07:44 +08:00
|
|
|
|
2017-06-20 23:26:26 +08:00
|
|
|
if (detailed && !is_bench)
|
2014-03-29 12:10:19 +08:00
|
|
|
{
|
2014-04-08 17:18:16 +08:00
|
|
|
json_array_begin (&json_ctx, "timings");
|
|
|
|
|
2014-03-29 12:10:19 +08:00
|
|
|
for (int i = 0; i < NUM_SAMPLES (v); i++)
|
2014-04-08 17:18:16 +08:00
|
|
|
json_element_double (&json_ctx, RESULT (v, i));
|
|
|
|
|
|
|
|
json_array_end (&json_ctx);
|
2014-03-29 12:10:19 +08:00
|
|
|
}
|
2014-04-08 17:18:16 +08:00
|
|
|
|
|
|
|
/* End variant. */
|
|
|
|
json_attr_object_end (&json_ctx);
|
2013-04-30 16:47:57 +08:00
|
|
|
}
|
2013-03-15 15:00:03 +08:00
|
|
|
|
2014-03-29 12:07:44 +08:00
|
|
|
/* End function. */
|
2014-04-08 17:18:16 +08:00
|
|
|
json_attr_object_end (&json_ctx);
|
2014-03-29 12:07:44 +08:00
|
|
|
|
2013-03-15 15:00:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|