|
| 1 | +/* |
| 2 | + * libbenchtsc is a simple benchmarking library that uses the rdtsc |
| 3 | + * x86 instruction. |
| 4 | + * Copyright (c) 2014, Simon Gerber <[email protected]> |
| 5 | + * |
| 6 | + * This library is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 2.1 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This library is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public |
| 17 | + * License along with this library; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#include "bench_rdtsc.h" |
| 22 | +#include <time.h> |
| 23 | + |
| 24 | +double ticks_per_nano = 0.0; |
| 25 | +bool rdtscp_flag = false; |
| 26 | + |
| 27 | +const int NANO_SECONDS_IN_SEC = 1000000000; |
| 28 | +/* returns a static buffer of struct timespec with the time difference of ts1 and ts2 |
| 29 | + ts1 is assumed to be greater than ts2 */ |
| 30 | +static struct timespec *TimeSpecDiff(struct timespec *ts1, struct timespec *ts2) |
| 31 | +{ |
| 32 | + static struct timespec ts; |
| 33 | + ts.tv_sec = ts1->tv_sec - ts2->tv_sec; |
| 34 | + ts.tv_nsec = ts1->tv_nsec - ts2->tv_nsec; |
| 35 | + if (ts.tv_nsec < 0) { |
| 36 | + ts.tv_sec--; |
| 37 | + ts.tv_nsec += NANO_SECONDS_IN_SEC; |
| 38 | + } |
| 39 | + return &ts; |
| 40 | +} |
| 41 | + |
| 42 | +static void calibrate_ticks(void) |
| 43 | +{ |
| 44 | + struct timespec begints, endts; |
| 45 | + uint64_t begin = 0, end = 0; |
| 46 | + clock_gettime(CLOCK_MONOTONIC, &begints); |
| 47 | + begin = bench_tsc(); |
| 48 | + uint64_t i; |
| 49 | + for (i = 0; i < 1000000; i++); /* must be CPU intensive */ |
| 50 | + end = bench_tsc(); |
| 51 | + clock_gettime(CLOCK_MONOTONIC, &endts); |
| 52 | + struct timespec *tmpts = TimeSpecDiff(&endts, &begints); |
| 53 | + uint64_t nsecElapsed = tmpts->tv_sec * 1000000000LL + tmpts->tv_nsec; |
| 54 | + ticks_per_nano = (double)(end - begin)/(double)nsecElapsed; |
| 55 | +} |
| 56 | + |
| 57 | +void bench_init(void) |
| 58 | +{ |
| 59 | + uint32_t eax, ebx, ecx, edx; |
| 60 | + // Check for rdtscp instruction |
| 61 | + cpuid(0x80000001, &eax, &ebx, &ecx, &edx); |
| 62 | + if ((edx >> 27) & 1) { |
| 63 | + rdtscp_flag = true; |
| 64 | + } else { |
| 65 | + rdtscp_flag = false; |
| 66 | + } |
| 67 | + |
| 68 | + calibrate_ticks(); |
| 69 | +} |
| 70 | + |
| 71 | +struct bench_calc_st { |
| 72 | + double *series; |
| 73 | + int runs; |
| 74 | + double avg; |
| 75 | + double sdev; |
| 76 | +}; |
| 77 | + |
| 78 | +static void calculate_(double *measurements, int runs, struct bench_calc_st *st) |
| 79 | +{ |
| 80 | + double mean = 0.0, M2 = 0.0; |
| 81 | + for (int i = 0; i < runs; i++) { |
| 82 | + double delta = measurements[i] - mean; |
| 83 | + mean = mean + delta / (i+1); |
| 84 | + M2 = M2 + delta*(measurements[i]-mean); |
| 85 | + } |
| 86 | + |
| 87 | + st->series = measurements; |
| 88 | + st->runs = runs; |
| 89 | + st->avg = mean; |
| 90 | + st->sdev = sqrt(M2/(runs-1)); |
| 91 | +} |
| 92 | + |
| 93 | +double bench_avg(double *measurements, int runs, struct bench_calc_st *st) |
| 94 | +{ |
| 95 | + struct bench_calc_st tmp = { 0 }; |
| 96 | + if (!st) { |
| 97 | + st = &tmp; |
| 98 | + } |
| 99 | + if (st->series != measurements || st->runs != runs) { |
| 100 | + calculate_(measurements, runs, st); |
| 101 | + } |
| 102 | + return st->avg; |
| 103 | +} |
| 104 | + |
| 105 | +double bench_sdev(double *measurements, int runs, struct bench_calc_st *st) |
| 106 | +{ |
| 107 | + struct bench_calc_st tmp = { 0 }; |
| 108 | + if (!st) { |
| 109 | + st = &tmp; |
| 110 | + } |
| 111 | + if (st->series != measurements || st->runs != runs) { |
| 112 | + calculate_(measurements, runs, st); |
| 113 | + } |
| 114 | + return st->sdev; |
| 115 | +} |
| 116 | + |
| 117 | +size_t bench_calc_st_sz(void) |
| 118 | +{ |
| 119 | + return sizeof(struct bench_calc_st); |
| 120 | +} |
0 commit comments