2013-03-15 08:00:03 +01:00
|
|
|
/* Skeleton for benchmark programs.
|
2015-01-02 17:28:19 +01:00
|
|
|
Copyright (C) 2013-2015 Free Software Foundation, Inc.
|
2013-03-15 08:00:03 +01: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 05:10:19 +01:00
|
|
|
#include <stdbool.h>
|
2013-03-15 08:00:03 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <inttypes.h>
|
2013-05-13 10:14:32 +02:00
|
|
|
#include "bench-timing.h"
|
2014-04-08 11:18:16 +02:00
|
|
|
#include "json-lib.h"
|
2013-03-15 08:00:03 +01:00
|
|
|
|
2013-05-08 08:21:05 +02:00
|
|
|
volatile unsigned int dontoptimize = 0;
|
2013-05-10 14:14:27 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
startup (void)
|
2013-05-08 08:21:05 +02:00
|
|
|
{
|
|
|
|
/* This loop should cause CPU to switch to maximal freqency.
|
|
|
|
This makes subsequent measurement more accurate. We need a side effect
|
|
|
|
to prevent the loop being deleted by compiler.
|
|
|
|
This should be enough to cause CPU to speed up and it is simpler than
|
|
|
|
running loop for constant time. This is used when user does not have root
|
|
|
|
access to set a constant freqency. */
|
2013-05-10 14:14:27 +02:00
|
|
|
for (int k = 0; k < 10000000; k++)
|
2013-05-08 08:21:05 +02:00
|
|
|
dontoptimize += 23 * dontoptimize + 2;
|
|
|
|
}
|
|
|
|
|
2013-04-30 10:40:20 +02:00
|
|
|
#define TIMESPEC_AFTER(a, b) \
|
|
|
|
(((a).tv_sec == (b).tv_sec) ? \
|
|
|
|
((a).tv_nsec > (b).tv_nsec) : \
|
2013-04-30 10:47:57 +02:00
|
|
|
((a).tv_sec > (b).tv_sec))
|
2013-03-15 08:00:03 +01:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2013-04-30 10:40:20 +02:00
|
|
|
unsigned long i, k;
|
2013-05-13 10:14:32 +02:00
|
|
|
struct timespec runtime;
|
|
|
|
timing_t start, end;
|
2014-03-29 05:10:19 +01:00
|
|
|
bool detailed = false;
|
2014-04-08 11:18:16 +02:00
|
|
|
json_ctx_t json_ctx;
|
2014-03-29 05:10:19 +01:00
|
|
|
|
|
|
|
if (argc == 2 && !strcmp (argv[1], "-d"))
|
|
|
|
detailed = true;
|
2013-03-15 08:00:03 +01:00
|
|
|
|
2013-05-08 08:21:05 +02:00
|
|
|
startup();
|
|
|
|
|
2013-04-30 10:40:20 +02:00
|
|
|
memset (&runtime, 0, sizeof (runtime));
|
2013-03-15 08:00:03 +01:00
|
|
|
|
2013-09-11 14:07:33 +02:00
|
|
|
unsigned long iters, res;
|
2013-03-15 08:00:03 +01:00
|
|
|
|
2014-02-22 05:39:27 +01:00
|
|
|
#ifdef BENCH_INIT
|
|
|
|
BENCH_INIT ();
|
|
|
|
#endif
|
2013-09-11 14:07:33 +02:00
|
|
|
TIMING_INIT (res);
|
|
|
|
|
|
|
|
iters = 1000 * res;
|
2013-03-15 08:00:03 +01:00
|
|
|
|
2014-04-08 11:18:16 +02:00
|
|
|
json_init (&json_ctx, 2, stdout);
|
|
|
|
|
2014-03-29 05:07:44 +01:00
|
|
|
/* Begin function. */
|
2014-04-08 11:18:16 +02:00
|
|
|
json_attr_object_begin (&json_ctx, FUNCNAME);
|
2014-03-29 05:07:44 +01:00
|
|
|
|
2013-04-30 10:47:57 +02:00
|
|
|
for (int v = 0; v < NUM_VARIANTS; v++)
|
2013-03-15 08:00:03 +01:00
|
|
|
{
|
2013-04-30 10:47:57 +02:00
|
|
|
/* Run for approximately DURATION seconds. */
|
|
|
|
clock_gettime (CLOCK_MONOTONIC_RAW, &runtime);
|
|
|
|
runtime.tv_sec += DURATION;
|
2013-03-15 08:00:03 +01:00
|
|
|
|
2013-04-30 10:47:57 +02:00
|
|
|
double d_total_i = 0;
|
2013-05-13 10:14:32 +02:00
|
|
|
timing_t total = 0, max = 0, min = 0x7fffffffffffffff;
|
2014-03-29 05:10:19 +01:00
|
|
|
int64_t c = 0;
|
2013-04-30 10:47:57 +02:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
for (i = 0; i < NUM_SAMPLES (v); i++)
|
|
|
|
{
|
2013-05-13 10:14:32 +02:00
|
|
|
uint64_t cur;
|
|
|
|
TIMING_NOW (start);
|
2013-04-30 10:47:57 +02:00
|
|
|
for (k = 0; k < iters; k++)
|
|
|
|
BENCH_FUNC (v, i);
|
2013-05-13 10:14:32 +02:00
|
|
|
TIMING_NOW (end);
|
2013-03-15 08:00:03 +01:00
|
|
|
|
2013-05-13 10:14:32 +02:00
|
|
|
TIMING_DIFF (cur, start, end);
|
2013-03-15 08:00:03 +01:00
|
|
|
|
2013-04-30 10:47:57 +02:00
|
|
|
if (cur > max)
|
|
|
|
max = cur;
|
2013-03-15 08:00:03 +01:00
|
|
|
|
2013-04-30 10:47:57 +02:00
|
|
|
if (cur < min)
|
|
|
|
min = cur;
|
2013-04-30 10:40:20 +02:00
|
|
|
|
2013-05-13 10:14:32 +02:00
|
|
|
TIMING_ACCUM (total, cur);
|
2014-03-29 05:10:19 +01:00
|
|
|
/* 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);
|
|
|
|
|
2013-04-30 10:47:57 +02:00
|
|
|
d_total_i += iters;
|
|
|
|
}
|
2014-03-29 05:10:19 +01:00
|
|
|
c++;
|
2013-04-30 10:47:57 +02:00
|
|
|
struct timespec curtime;
|
2013-04-30 10:40:20 +02:00
|
|
|
|
2013-04-30 10:47:57 +02:00
|
|
|
memset (&curtime, 0, sizeof (curtime));
|
|
|
|
clock_gettime (CLOCK_MONOTONIC_RAW, &curtime);
|
|
|
|
if (TIMESPEC_AFTER (curtime, runtime))
|
|
|
|
goto done;
|
|
|
|
}
|
2013-03-15 08:00:03 +01:00
|
|
|
|
2013-04-30 10:47:57 +02:00
|
|
|
double d_total_s;
|
|
|
|
double d_iters;
|
2013-04-30 10:40:20 +02:00
|
|
|
|
2013-04-30 10:47:57 +02:00
|
|
|
done:
|
2013-05-13 10:14:32 +02:00
|
|
|
d_total_s = total;
|
2013-04-30 10:47:57 +02:00
|
|
|
d_iters = iters;
|
2013-04-30 10:40:20 +02:00
|
|
|
|
2014-04-08 11:18:16 +02:00
|
|
|
/* Begin variant. */
|
|
|
|
json_attr_object_begin (&json_ctx, VARIANT (v));
|
|
|
|
|
|
|
|
json_attr_double (&json_ctx, "duration", d_total_s);
|
|
|
|
json_attr_double (&json_ctx, "iterations", d_total_i);
|
|
|
|
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 05:07:44 +01:00
|
|
|
|
2014-03-29 05:10:19 +01:00
|
|
|
if (detailed)
|
|
|
|
{
|
2014-04-08 11:18:16 +02:00
|
|
|
json_array_begin (&json_ctx, "timings");
|
|
|
|
|
2014-03-29 05:10:19 +01:00
|
|
|
for (int i = 0; i < NUM_SAMPLES (v); i++)
|
2014-04-08 11:18:16 +02:00
|
|
|
json_element_double (&json_ctx, RESULT (v, i));
|
|
|
|
|
|
|
|
json_array_end (&json_ctx);
|
2014-03-29 05:10:19 +01:00
|
|
|
}
|
2014-04-08 11:18:16 +02:00
|
|
|
|
|
|
|
/* End variant. */
|
|
|
|
json_attr_object_end (&json_ctx);
|
2013-04-30 10:47:57 +02:00
|
|
|
}
|
2013-03-15 08:00:03 +01:00
|
|
|
|
2014-03-29 05:07:44 +01:00
|
|
|
/* End function. */
|
2014-04-08 11:18:16 +02:00
|
|
|
json_attr_object_end (&json_ctx);
|
2014-03-29 05:07:44 +01:00
|
|
|
|
2013-03-15 08:00:03 +01:00
|
|
|
return 0;
|
|
|
|
}
|