1 /* Skeleton for benchmark programs.
2 Copyright (C) 2013-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
25 #include "bench-timing.h"
27 #include "bench-util.h"
29 #include "bench-util.c"
31 #define TIMESPEC_AFTER(a, b) \
32 (((a).tv_sec == (b).tv_sec) \
33 ? ((a).tv_nsec > (b).tv_nsec) \
34 : ((a).tv_sec > (b).tv_sec))
36 main (int argc, char **argv)
39 struct timespec runtime;
41 bool detailed = false;
44 if (argc == 2 && !strcmp (argv[1], "-d"))
49 memset (&runtime, 0, sizeof (runtime));
51 unsigned long iters = 1000;
57 json_init (&json_ctx, 2, stdout);
60 json_attr_object_begin (&json_ctx, FUNCNAME);
62 for (int v = 0; v < NUM_VARIANTS; v++)
64 /* Run for approximately DURATION seconds. */
65 clock_gettime (CLOCK_MONOTONIC_RAW, &runtime);
66 runtime.tv_sec += DURATION;
68 bool is_bench = strncmp (VARIANT (v), "workload-", 9) == 0;
70 timing_t total = 0, max = 0, min = 0x7fffffffffffffff;
71 timing_t throughput = 0, latency = 0;
79 /* Benchmark a real trace of calls - all samples are iterated
80 over once before repeating. This models actual use more
81 accurately than repeating the same sample many times. */
83 for (k = 0; k < iters; k++)
84 for (i = 0; i < NUM_SAMPLES (v); i++)
87 TIMING_DIFF (cur, start, end);
88 TIMING_ACCUM (throughput, cur);
91 for (k = 0; k < iters; k++)
92 for (i = 0; i < NUM_SAMPLES (v); i++)
93 BENCH_FUNC_LAT (v, i);
95 TIMING_DIFF (cur, start, end);
96 TIMING_ACCUM (latency, cur);
98 d_total_i += iters * NUM_SAMPLES (v);
101 for (i = 0; i < NUM_SAMPLES (v); i++)
104 for (k = 0; k < iters; k++)
108 TIMING_DIFF (cur, start, end);
116 TIMING_ACCUM (total, cur);
117 /* Accumulate timings for the value. In the end we will divide
118 by the total iterations. */
119 RESULT_ACCUM (cur, v, i, c * iters, (c + 1) * iters);
124 struct timespec curtime;
126 memset (&curtime, 0, sizeof (curtime));
127 clock_gettime (CLOCK_MONOTONIC_RAW, &curtime);
128 if (TIMESPEC_AFTER (curtime, runtime))
140 json_attr_object_begin (&json_ctx, VARIANT (v));
144 json_attr_double (&json_ctx, "duration", throughput + latency);
145 json_attr_double (&json_ctx, "iterations", 2 * d_total_i);
146 json_attr_double (&json_ctx, "reciprocal-throughput",
147 throughput / d_total_i);
148 json_attr_double (&json_ctx, "latency", latency / d_total_i);
149 json_attr_double (&json_ctx, "max-throughput",
150 d_total_i / throughput * 1000000000.0);
151 json_attr_double (&json_ctx, "min-throughput",
152 d_total_i / latency * 1000000000.0);
156 json_attr_double (&json_ctx, "duration", d_total_s);
157 json_attr_double (&json_ctx, "iterations", d_total_i);
158 json_attr_double (&json_ctx, "max", max / d_iters);
159 json_attr_double (&json_ctx, "min", min / d_iters);
160 json_attr_double (&json_ctx, "mean", d_total_s / d_total_i);
163 if (detailed && !is_bench)
165 json_array_begin (&json_ctx, "timings");
167 for (int i = 0; i < NUM_SAMPLES (v); i++)
168 json_element_double (&json_ctx, RESULT (v, i));
170 json_array_end (&json_ctx);
174 json_attr_object_end (&json_ctx);
178 json_attr_object_end (&json_ctx);