#if 0#endif
#ifndef DS_BENCHMARK_H
#define DS_BENCHMARK_H
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#if !defined(_WIN32)
#include <sys/time.h>
#endif
#include <math.h>
#include <time.h>
#if defined(_WIN32)
#include <windows.h>
int gettimeofday(struct timeval *tp, struct timezone *tzp) {
static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL);
SYSTEMTIME system_time;
FILETIME file_time;
uint64_t time;
GetSystemTime(&system_time);
SystemTimeToFileTime(&system_time, &file_time);
time = ((uint64_t) file_time.dwLowDateTime);
time += ((uint64_t) file_time.dwHighDateTime) << 32;
tp->tv_sec = (long) ((time - EPOCH) / 10000000L);
tp->tv_usec = (long) (system_time.wMilliseconds * 1000);
return 0;
}
#endif
static uint64_t _bench_rdtsc(void) {
#if defined(_WIN32) || defined(_WIN64)
LARGE_INTEGER li;
if (!QueryPerformanceCounter(&li)) {
return 0;
}
return li.QuadPart;
#elif defined(__i586__) || defined(__amd64__)
uint64_t x;
__asm__ volatile(".byte 0x0f, 0x31"
: "=A"(x));
return x;
#elif defined(SPEED_USE_ARM_PMU)
uint64_t value;
__asm__ volatile("mrs %0, PMCCNTR_EL0" : "=r" (value));
return value;
#elif defined(__s390x__)
#define USING_TIME_RATHER_THAN_CYCLES
uint64_t tod;
__asm__ volatile("stckf %0\n" : "=Q" (tod) : : "cc");
return (tod * 1000 / 4096);
#else
#define USING_TIME_RATHER_THAN_CYCLES
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
return (uint64_t)((double)time.tv_sec * 1e9 + (double)time.tv_nsec);
#endif
}
#if defined(SPEED_USE_ARM_PMU)
static void _bench_init_perfcounters(void) {
__asm__ volatile("MSR PMCR_EL0, %0" ::"r"(1));
__asm__ volatile("MSR PMCNTENSET_EL0, %0" ::"r"(0x80000000));
}
#endif
#define DEFINE_TIMER_VARIABLES \
volatile uint64_t _bench_cycles_start, _bench_cycles_end; \
uint64_t _bench_cycles_cumulative = 0; \
uint64_t _bench_cycles_diff; \
struct timeval _bench_timeval_start, _bench_timeval_end; \
uint64_t _bench_iterations, _bench_time_cumulative; \
double _bench_cycles_x, _bench_cycles_mean, _bench_cycles_delta, _bench_cycles_M2, _bench_cycles_stdev; \
double _bench_time_x, _bench_time_mean, _bench_time_delta, _bench_time_M2, _bench_time_stdev;
#if defined(SPEED_USE_ARM_PMU)
#define INITIALIZE_TIMER \
_bench_init_perfcounters(); \
_bench_iterations = 0; \
_bench_cycles_mean = 0.0; \
_bench_cycles_M2 = 0.0; \
_bench_time_cumulative = 0; \
_bench_time_mean = 0.0; \
_bench_time_M2 = 0.0;
#else
#define INITIALIZE_TIMER \
_bench_iterations = 0; \
_bench_cycles_mean = 0.0; \
_bench_cycles_M2 = 0.0; \
_bench_time_cumulative = 0; \
_bench_time_mean = 0.0; \
_bench_time_M2 = 0.0;
#endif
#define START_TIMER \
gettimeofday(&_bench_timeval_start, NULL); \
_bench_cycles_start = _bench_rdtsc();
#define STOP_TIMER \
_bench_cycles_end = _bench_rdtsc(); \
gettimeofday(&_bench_timeval_end, NULL); \
_bench_iterations += 1; \
if (_bench_cycles_end < _bench_cycles_start) { \
_bench_cycles_end += (uint64_t) 1 << 32; \
} \
_bench_cycles_diff = _bench_cycles_end; \
_bench_cycles_diff -= _bench_cycles_start; \
_bench_cycles_cumulative += _bench_cycles_diff; \
_bench_cycles_x = (double) (_bench_cycles_diff); \
_bench_cycles_delta = _bench_cycles_x - _bench_cycles_mean; \
_bench_cycles_mean += _bench_cycles_delta / (double) _bench_iterations; \
_bench_cycles_M2 += _bench_cycles_delta * (_bench_cycles_x - _bench_cycles_mean); \
_bench_time_x = (double) ((((uint64_t) _bench_timeval_end.tv_sec) * 1000000 + (uint64_t) _bench_timeval_end.tv_usec) - (((uint64_t) _bench_timeval_start.tv_sec) * 1000000 + (uint64_t) _bench_timeval_start.tv_usec)); \
_bench_time_delta = _bench_time_x - _bench_time_mean; \
_bench_time_mean += _bench_time_delta / (double) _bench_iterations; \
_bench_time_M2 += _bench_time_delta * (_bench_time_x - _bench_time_mean); \
_bench_time_cumulative += (uint64_t) _bench_time_x;
#define FINALIZE_TIMER \
if (_bench_iterations < 2) { \
_bench_cycles_stdev = 0.0; \
} else { \
_bench_cycles_stdev = sqrt(_bench_cycles_M2 / (double) _bench_iterations); \
} \
if (_bench_iterations < 2) { \
_bench_time_stdev = 0.0; \
} else { \
_bench_time_stdev = sqrt(_bench_time_M2 / (double) _bench_iterations); \
}
#define PRINT_CURRENT_TIME \
{ \
char _bench_time_buff[20]; \
time_t _bench_time_now = time(0); \
strftime(_bench_time_buff, 20, "%Y-%m-%d %H:%M:%S", localtime(&_bench_time_now)); \
printf("%s", _bench_time_buff); \
}
#ifdef USING_TIME_RATHER_THAN_CYCLES
#define HIGH_PREC_HEADER "High-prec time (ns): mean"
#else
#define HIGH_PREC_HEADER "CPU cycles: mean "
#endif
#define PRINT_TIMER_HEADER \
printf("Started at "); \
PRINT_CURRENT_TIME \
printf("\n"); \
printf("%-36s | %10s | %14s | %15s | %10s | %25s | %10s\n", "Operation ", "Iterations", "Total time (s)", "Time (us): mean", "pop. stdev", HIGH_PREC_HEADER, "pop. stdev"); \
printf("%-36s | %10s:| %14s:| %15s:| %10s:| %25s:| %10s:\n", "------------------------------------", "----------", "--------------", "---------------", "----------", "-------------------------", "----------");
#define PRINT_TIMER_FOOTER \
printf("Ended at "); \
PRINT_CURRENT_TIME \
printf("\n");
#define PRINT_TIMER_AVG(op_name) \
printf("%-36s | %10" PRIu64 " | %14.3f | %15.3f | %10.3f | %25.0f | %10.0f\n", (op_name), _bench_iterations, ((double) _bench_time_cumulative) / 1000000.0, _bench_time_mean, _bench_time_stdev, ((double) _bench_cycles_cumulative) / (double) _bench_iterations, _bench_cycles_stdev);
#define TIME_OPERATION_ITERATIONS(op, op_name, it) \
{ \
DEFINE_TIMER_VARIABLES \
INITIALIZE_TIMER \
for (int i = 0; i < (it); i++) { \
START_TIMER { op; } \
STOP_TIMER \
} \
FINALIZE_TIMER \
PRINT_TIMER_AVG(op_name) \
}
#define TIME_OPERATION_SECONDS(op, op_name, secs) \
{ \
DEFINE_TIMER_VARIABLES \
INITIALIZE_TIMER \
uint64_t _bench_time_goal_usecs = 1000000 * secs; \
while (_bench_time_cumulative < _bench_time_goal_usecs) { \
START_TIMER { op; } \
STOP_TIMER \
} \
FINALIZE_TIMER \
PRINT_TIMER_AVG(op_name) \
}
#define TIME_OPERATION_SECONDS_MAXIT(op, op_name, secs, maxit, refresh) \
{ \
DEFINE_TIMER_VARIABLES \
INITIALIZE_TIMER \
uint64_t _bench_time_goal_usecs = 1000000 * secs; \
while (_bench_time_cumulative < _bench_time_goal_usecs) { \
for (unsigned long long i = 0; i < (maxit) && _bench_time_cumulative < _bench_time_goal_usecs; i++) { \
START_TIMER { op; } \
STOP_TIMER \
} \
if (_bench_time_cumulative < _bench_time_goal_usecs) { refresh; } \
} \
FINALIZE_TIMER \
PRINT_TIMER_AVG(op_name) \
}
#endif