Path: blob/main/sys/contrib/zstd/programs/timefn.c
48254 views
/*1* Copyright (c) Yann Collet, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/91011/* === Dependencies === */1213#include "timefn.h"141516/*-****************************************17* Time functions18******************************************/1920#if defined(_WIN32) /* Windows */2122#include <stdlib.h> /* abort */23#include <stdio.h> /* perror */2425UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; }2627PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)28{29static LARGE_INTEGER ticksPerSecond;30static int init = 0;31if (!init) {32if (!QueryPerformanceFrequency(&ticksPerSecond)) {33perror("timefn::QueryPerformanceFrequency");34abort();35}36init = 1;37}38return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;39}4041PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)42{43static LARGE_INTEGER ticksPerSecond;44static int init = 0;45if (!init) {46if (!QueryPerformanceFrequency(&ticksPerSecond)) {47perror("timefn::QueryPerformanceFrequency");48abort();49}50init = 1;51}52return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;53}54555657#elif defined(__APPLE__) && defined(__MACH__)5859UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); }6061PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)62{63static mach_timebase_info_data_t rate;64static int init = 0;65if (!init) {66mach_timebase_info(&rate);67init = 1;68}69return (((clockEnd - clockStart) * (PTime)rate.numer) / ((PTime)rate.denom))/1000ULL;70}7172PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)73{74static mach_timebase_info_data_t rate;75static int init = 0;76if (!init) {77mach_timebase_info(&rate);78init = 1;79}80return ((clockEnd - clockStart) * (PTime)rate.numer) / ((PTime)rate.denom);81}828384/* C11 requires timespec_get, but FreeBSD 11 lacks it, while still claiming C11 compliance.85Android also lacks it but does define TIME_UTC. */86#elif (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */) \87&& defined(TIME_UTC) && !defined(__ANDROID__)8889#include <stdlib.h> /* abort */90#include <stdio.h> /* perror */9192UTIL_time_t UTIL_getTime(void)93{94/* time must be initialized, othersize it may fail msan test.95* No good reason, likely a limitation of timespec_get() for some target */96UTIL_time_t time = UTIL_TIME_INITIALIZER;97if (timespec_get(&time, TIME_UTC) != TIME_UTC) {98perror("timefn::timespec_get");99abort();100}101return time;102}103104static UTIL_time_t UTIL_getSpanTime(UTIL_time_t begin, UTIL_time_t end)105{106UTIL_time_t diff;107if (end.tv_nsec < begin.tv_nsec) {108diff.tv_sec = (end.tv_sec - 1) - begin.tv_sec;109diff.tv_nsec = (end.tv_nsec + 1000000000ULL) - begin.tv_nsec;110} else {111diff.tv_sec = end.tv_sec - begin.tv_sec;112diff.tv_nsec = end.tv_nsec - begin.tv_nsec;113}114return diff;115}116117PTime UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)118{119UTIL_time_t const diff = UTIL_getSpanTime(begin, end);120PTime micro = 0;121micro += 1000000ULL * diff.tv_sec;122micro += diff.tv_nsec / 1000ULL;123return micro;124}125126PTime UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)127{128UTIL_time_t const diff = UTIL_getSpanTime(begin, end);129PTime nano = 0;130nano += 1000000000ULL * diff.tv_sec;131nano += diff.tv_nsec;132return nano;133}134135136137#else /* relies on standard C90 (note : clock_t measurements can be wrong when using multi-threading) */138139UTIL_time_t UTIL_getTime(void) { return clock(); }140PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }141PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }142143#endif144145146147/* returns time span in microseconds */148PTime UTIL_clockSpanMicro(UTIL_time_t clockStart )149{150UTIL_time_t const clockEnd = UTIL_getTime();151return UTIL_getSpanTimeMicro(clockStart, clockEnd);152}153154/* returns time span in microseconds */155PTime UTIL_clockSpanNano(UTIL_time_t clockStart )156{157UTIL_time_t const clockEnd = UTIL_getTime();158return UTIL_getSpanTimeNano(clockStart, clockEnd);159}160161void UTIL_waitForNextTick(void)162{163UTIL_time_t const clockStart = UTIL_getTime();164UTIL_time_t clockEnd;165do {166clockEnd = UTIL_getTime();167} while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0);168}169170171