]>
Commit | Line | Data |
---|---|---|
8cfdb7e0 SP |
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 | ||
d569c6ee SP |
25 | #define TIMESPEC_AFTER(a, b) \ |
26 | (((a).tv_sec == (b).tv_sec) ? \ | |
27 | ((a).tv_nsec > (b).tv_nsec) : \ | |
28 | ((a).tv_sec > (b).tv_sec)) | |
8cfdb7e0 SP |
29 | int |
30 | main (int argc, char **argv) | |
31 | { | |
d569c6ee | 32 | unsigned long i, k; |
8cfdb7e0 | 33 | uint64_t total = 0, max = 0, min = 0x7fffffffffffffff; |
d569c6ee | 34 | struct timespec start, end, runtime; |
8cfdb7e0 | 35 | |
d569c6ee | 36 | memset (&runtime, 0, sizeof (runtime)); |
8cfdb7e0 SP |
37 | memset (&start, 0, sizeof (start)); |
38 | memset (&end, 0, sizeof (end)); | |
39 | ||
40 | clock_getres (CLOCK_PROCESS_CPUTIME_ID, &start); | |
41 | ||
42 | /* Measure 1000 times the resolution of the clock. So for a 1ns resolution | |
43 | clock, we measure 1000 iterations of the function call at a time. | |
44 | Measurements close to the minimum clock resolution won't make much sense, | |
45 | but it's better than having nothing at all. */ | |
46 | unsigned long iters = 1000 * start.tv_nsec; | |
8cfdb7e0 | 47 | |
d569c6ee SP |
48 | /* Run for approxmately DURATION seconds. */ |
49 | clock_gettime (CLOCK_MONOTONIC_RAW, &runtime); | |
50 | runtime.tv_sec += DURATION; | |
51 | ||
52 | double d_total_i = 0; | |
53 | while (1) | |
8cfdb7e0 | 54 | { |
d569c6ee | 55 | for (i = 0; i < NUM_SAMPLES; i++) |
8cfdb7e0 SP |
56 | { |
57 | clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &start); | |
58 | for (k = 0; k < iters; k++) | |
59 | BENCH_FUNC(i); | |
60 | clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &end); | |
61 | ||
62 | uint64_t cur = (end.tv_nsec - start.tv_nsec | |
63 | + ((end.tv_sec - start.tv_sec) | |
64 | * (uint64_t) 1000000000)); | |
65 | ||
66 | if (cur > max) | |
67 | max = cur; | |
68 | ||
69 | if (cur < min) | |
70 | min = cur; | |
71 | ||
72 | total += cur; | |
d569c6ee SP |
73 | |
74 | d_total_i += iters; | |
8cfdb7e0 | 75 | } |
d569c6ee SP |
76 | |
77 | struct timespec curtime; | |
78 | ||
79 | memset (&curtime, 0, sizeof (curtime)); | |
80 | clock_gettime (CLOCK_MONOTONIC_RAW, &curtime); | |
81 | if (TIMESPEC_AFTER (curtime, runtime)) | |
82 | goto done; | |
8cfdb7e0 SP |
83 | } |
84 | ||
d569c6ee SP |
85 | double d_total_s; |
86 | double d_iters; | |
87 | ||
88 | done: | |
89 | d_total_s = total * 1e-9; | |
90 | d_iters = iters; | |
91 | ||
8cfdb7e0 SP |
92 | printf (FUNCNAME ": ITERS:%g: TOTAL:%gs, MAX:%gns, MIN:%gns, %g iter/s\n", |
93 | d_total_i, d_total_s, max / d_iters, min / d_iters, | |
94 | d_total_i / d_total_s); | |
95 | ||
96 | return 0; | |
97 | } |