lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Skeleton for benchmark programs. |
| 2 | Copyright (C) 2013-2015 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 <stdbool.h> |
| 22 | #include <stdio.h> |
| 23 | #include <time.h> |
| 24 | #include <inttypes.h> |
| 25 | #include "bench-timing.h" |
| 26 | #include "json-lib.h" |
| 27 | |
| 28 | volatile unsigned int dontoptimize = 0; |
| 29 | |
| 30 | void |
| 31 | startup (void) |
| 32 | { |
| 33 | /* This loop should cause CPU to switch to maximal freqency. |
| 34 | This makes subsequent measurement more accurate. We need a side effect |
| 35 | to prevent the loop being deleted by compiler. |
| 36 | This should be enough to cause CPU to speed up and it is simpler than |
| 37 | running loop for constant time. This is used when user does not have root |
| 38 | access to set a constant freqency. */ |
| 39 | for (int k = 0; k < 10000000; k++) |
| 40 | dontoptimize += 23 * dontoptimize + 2; |
| 41 | } |
| 42 | |
| 43 | #define TIMESPEC_AFTER(a, b) \ |
| 44 | (((a).tv_sec == (b).tv_sec) ? \ |
| 45 | ((a).tv_nsec > (b).tv_nsec) : \ |
| 46 | ((a).tv_sec > (b).tv_sec)) |
| 47 | int |
| 48 | main (int argc, char **argv) |
| 49 | { |
| 50 | unsigned long i, k; |
| 51 | struct timespec runtime; |
| 52 | timing_t start, end; |
| 53 | bool detailed = false; |
| 54 | json_ctx_t json_ctx; |
| 55 | |
| 56 | if (argc == 2 && !strcmp (argv[1], "-d")) |
| 57 | detailed = true; |
| 58 | |
| 59 | startup(); |
| 60 | |
| 61 | memset (&runtime, 0, sizeof (runtime)); |
| 62 | |
| 63 | unsigned long iters, res; |
| 64 | |
| 65 | #ifdef BENCH_INIT |
| 66 | BENCH_INIT (); |
| 67 | #endif |
| 68 | TIMING_INIT (res); |
| 69 | |
| 70 | iters = 1000 * res; |
| 71 | |
| 72 | json_init (&json_ctx, 2, stdout); |
| 73 | |
| 74 | /* Begin function. */ |
| 75 | json_attr_object_begin (&json_ctx, FUNCNAME); |
| 76 | |
| 77 | for (int v = 0; v < NUM_VARIANTS; v++) |
| 78 | { |
| 79 | /* Run for approximately DURATION seconds. */ |
| 80 | clock_gettime (CLOCK_MONOTONIC_RAW, &runtime); |
| 81 | runtime.tv_sec += DURATION; |
| 82 | |
| 83 | double d_total_i = 0; |
| 84 | timing_t total = 0, max = 0, min = 0x7fffffffffffffff; |
| 85 | int64_t c = 0; |
| 86 | while (1) |
| 87 | { |
| 88 | for (i = 0; i < NUM_SAMPLES (v); i++) |
| 89 | { |
| 90 | uint64_t cur; |
| 91 | TIMING_NOW (start); |
| 92 | for (k = 0; k < iters; k++) |
| 93 | BENCH_FUNC (v, i); |
| 94 | TIMING_NOW (end); |
| 95 | |
| 96 | TIMING_DIFF (cur, start, end); |
| 97 | |
| 98 | if (cur > max) |
| 99 | max = cur; |
| 100 | |
| 101 | if (cur < min) |
| 102 | min = cur; |
| 103 | |
| 104 | TIMING_ACCUM (total, cur); |
| 105 | /* Accumulate timings for the value. In the end we will divide |
| 106 | by the total iterations. */ |
| 107 | RESULT_ACCUM (cur, v, i, c * iters, (c + 1) * iters); |
| 108 | |
| 109 | d_total_i += iters; |
| 110 | } |
| 111 | c++; |
| 112 | struct timespec curtime; |
| 113 | |
| 114 | memset (&curtime, 0, sizeof (curtime)); |
| 115 | clock_gettime (CLOCK_MONOTONIC_RAW, &curtime); |
| 116 | if (TIMESPEC_AFTER (curtime, runtime)) |
| 117 | goto done; |
| 118 | } |
| 119 | |
| 120 | double d_total_s; |
| 121 | double d_iters; |
| 122 | |
| 123 | done: |
| 124 | d_total_s = total; |
| 125 | d_iters = iters; |
| 126 | |
| 127 | /* Begin variant. */ |
| 128 | json_attr_object_begin (&json_ctx, VARIANT (v)); |
| 129 | |
| 130 | json_attr_double (&json_ctx, "duration", d_total_s); |
| 131 | json_attr_double (&json_ctx, "iterations", d_total_i); |
| 132 | json_attr_double (&json_ctx, "max", max / d_iters); |
| 133 | json_attr_double (&json_ctx, "min", min / d_iters); |
| 134 | json_attr_double (&json_ctx, "mean", d_total_s / d_total_i); |
| 135 | |
| 136 | if (detailed) |
| 137 | { |
| 138 | json_array_begin (&json_ctx, "timings"); |
| 139 | |
| 140 | for (int i = 0; i < NUM_SAMPLES (v); i++) |
| 141 | json_element_double (&json_ctx, RESULT (v, i)); |
| 142 | |
| 143 | json_array_end (&json_ctx); |
| 144 | } |
| 145 | |
| 146 | /* End variant. */ |
| 147 | json_attr_object_end (&json_ctx); |
| 148 | } |
| 149 | |
| 150 | /* End function. */ |
| 151 | json_attr_object_end (&json_ctx); |
| 152 | |
| 153 | return 0; |
| 154 | } |