]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-skeleton.c
05edc69c5e082ea3b08ba6176f9ec9ce57f32ade
[thirdparty/glibc.git] / benchtests / bench-skeleton.c
1 /* Skeleton for benchmark programs.
2 Copyright (C) 2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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 <http://www.gnu.org/licenses/>. */
18
19 #include <string.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <time.h>
23 #include <inttypes.h>
24 #include "bench-timing.h"
25
26 volatile unsigned int dontoptimize = 0;
27
28 void
29 startup (void)
30 {
31 /* This loop should cause CPU to switch to maximal freqency.
32 This makes subsequent measurement more accurate. We need a side effect
33 to prevent the loop being deleted by compiler.
34 This should be enough to cause CPU to speed up and it is simpler than
35 running loop for constant time. This is used when user does not have root
36 access to set a constant freqency. */
37 for (int k = 0; k < 10000000; k++)
38 dontoptimize += 23 * dontoptimize + 2;
39 }
40
41 #define TIMESPEC_AFTER(a, b) \
42 (((a).tv_sec == (b).tv_sec) ? \
43 ((a).tv_nsec > (b).tv_nsec) : \
44 ((a).tv_sec > (b).tv_sec))
45 int
46 main (int argc, char **argv)
47 {
48 unsigned long i, k;
49 struct timespec runtime;
50 timing_t start, end;
51
52 startup();
53
54 memset (&runtime, 0, sizeof (runtime));
55
56 unsigned long iters, res;
57
58 TIMING_INIT (res);
59
60 iters = 1000 * res;
61
62 for (int v = 0; v < NUM_VARIANTS; v++)
63 {
64 /* Run for approximately DURATION seconds. */
65 clock_gettime (CLOCK_MONOTONIC_RAW, &runtime);
66 runtime.tv_sec += DURATION;
67
68 double d_total_i = 0;
69 timing_t total = 0, max = 0, min = 0x7fffffffffffffff;
70 while (1)
71 {
72 for (i = 0; i < NUM_SAMPLES (v); i++)
73 {
74 uint64_t cur;
75 TIMING_NOW (start);
76 for (k = 0; k < iters; k++)
77 BENCH_FUNC (v, i);
78 TIMING_NOW (end);
79
80 TIMING_DIFF (cur, start, end);
81
82 if (cur > max)
83 max = cur;
84
85 if (cur < min)
86 min = cur;
87
88 TIMING_ACCUM (total, cur);
89
90 d_total_i += iters;
91 }
92 struct timespec curtime;
93
94 memset (&curtime, 0, sizeof (curtime));
95 clock_gettime (CLOCK_MONOTONIC_RAW, &curtime);
96 if (TIMESPEC_AFTER (curtime, runtime))
97 goto done;
98 }
99
100 double d_total_s;
101 double d_iters;
102
103 done:
104 d_total_s = total;
105 d_iters = iters;
106
107 TIMING_PRINT_STATS (VARIANT (v), d_total_s, d_iters, d_total_i, max,
108 min);
109 }
110
111 return 0;
112 }