1
0
mirror of https://github.com/facebook/zstd.git synced 2025-07-30 22:23:13 +03:00

minor simplification refactoring for timefn

`UTIL_getSpanTimeMicro()` can be factored in a generic way,
reducing OS-dependent code.
This commit is contained in:
Yann Collet
2023-01-06 15:25:36 -08:00
parent 71dbe8f9d4
commit 8b130009e3
5 changed files with 28 additions and 56 deletions

View File

@ -28,6 +28,7 @@
#include <stdio.h> /* fprintf, open, fdopen, fread, _fileno, stdin, stdout */ #include <stdio.h> /* fprintf, open, fdopen, fread, _fileno, stdin, stdout */
#include <stdlib.h> /* malloc, free */ #include <stdlib.h> /* malloc, free */
#include <string.h> /* strcmp, strlen */ #include <string.h> /* strcmp, strlen */
#include <time.h> /* clock_t, to measure process time */
#include <fcntl.h> /* O_WRONLY */ #include <fcntl.h> /* O_WRONLY */
#include <assert.h> #include <assert.h>
#include <errno.h> /* errno */ #include <errno.h> /* errno */

View File

@ -25,20 +25,6 @@
UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; } UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; }
PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
{
static LARGE_INTEGER ticksPerSecond;
static int init = 0;
if (!init) {
if (!QueryPerformanceFrequency(&ticksPerSecond)) {
perror("timefn::QueryPerformanceFrequency");
abort();
}
init = 1;
}
return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
}
PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
{ {
static LARGE_INTEGER ticksPerSecond; static LARGE_INTEGER ticksPerSecond;
@ -59,17 +45,6 @@ PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); } UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); }
PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
{
static mach_timebase_info_data_t rate;
static int init = 0;
if (!init) {
mach_timebase_info(&rate);
init = 1;
}
return (((clockEnd - clockStart) * (PTime)rate.numer) / ((PTime)rate.denom))/1000ULL;
}
PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
{ {
static mach_timebase_info_data_t rate; static mach_timebase_info_data_t rate;
@ -115,15 +90,6 @@ static UTIL_time_t UTIL_getSpanTime(UTIL_time_t begin, UTIL_time_t end)
return diff; return diff;
} }
PTime UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
{
UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
PTime micro = 0;
micro += 1000000ULL * diff.tv_sec;
micro += diff.tv_nsec / 1000ULL;
return micro;
}
PTime UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end) PTime UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)
{ {
UTIL_time_t const diff = UTIL_getSpanTime(begin, end); UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
@ -134,25 +100,26 @@ PTime UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)
} }
#else /* relies on standard C90 (note : clock_t produces wrong measurements for multi-threaded workloads) */
#else /* relies on standard C90 (note : clock_t measurements can be wrong when using multi-threading) */
UTIL_time_t UTIL_getTime(void) { return clock(); } UTIL_time_t UTIL_getTime(void) { return clock(); }
PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; } PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
#endif #endif
/* ==== Common functions, valid for all time API ==== */
PTime UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
{
return UTIL_getSpanTimeNano(begin, end) / 1000ULL;
}
/* returns time span in microseconds */
PTime UTIL_clockSpanMicro(UTIL_time_t clockStart ) PTime UTIL_clockSpanMicro(UTIL_time_t clockStart )
{ {
UTIL_time_t const clockEnd = UTIL_getTime(); UTIL_time_t const clockEnd = UTIL_getTime();
return UTIL_getSpanTimeMicro(clockStart, clockEnd); return UTIL_getSpanTimeMicro(clockStart, clockEnd);
} }
/* returns time span in microseconds */
PTime UTIL_clockSpanNano(UTIL_time_t clockStart ) PTime UTIL_clockSpanNano(UTIL_time_t clockStart )
{ {
UTIL_time_t const clockEnd = UTIL_getTime(); UTIL_time_t const clockEnd = UTIL_getTime();

View File

@ -16,12 +16,6 @@ extern "C" {
#endif #endif
/*-****************************************
* Dependencies
******************************************/
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
/*-**************************************** /*-****************************************
* Local Types * Local Types
@ -31,7 +25,7 @@ extern "C" {
# if defined(_AIX) # if defined(_AIX)
# include <inttypes.h> # include <inttypes.h>
# else # else
# include <stdint.h> /* intptr_t */ # include <stdint.h> /* uint64_t */
# endif # endif
typedef uint64_t PTime; /* Precise Time */ typedef uint64_t PTime; /* Precise Time */
#else #else
@ -41,8 +35,10 @@ extern "C" {
/*-**************************************** /*-****************************************
* Time functions * Time types (note: OS dependent)
******************************************/ ******************************************/
#include <time.h> /* TIME_UTC, then struct timespec and clock_t */
#if defined(_WIN32) /* Windows */ #if defined(_WIN32) /* Windows */
#include <windows.h> /* LARGE_INTEGER */ #include <windows.h> /* LARGE_INTEGER */
@ -63,7 +59,7 @@ extern "C" {
typedef struct timespec UTIL_time_t; typedef struct timespec UTIL_time_t;
#define UTIL_TIME_INITIALIZER { 0, 0 } #define UTIL_TIME_INITIALIZER { 0, 0 }
#else /* relies on standard C90 (note : clock_t measurements can be wrong when using multi-threading) */ #else /* relies on standard C90 (note : clock_t produces wrong measurements for multi-threaded workloads) */
#define UTIL_TIME_USES_C90_CLOCK #define UTIL_TIME_USES_C90_CLOCK
typedef clock_t UTIL_time_t; typedef clock_t UTIL_time_t;
@ -72,15 +68,21 @@ extern "C" {
#endif #endif
UTIL_time_t UTIL_getTime(void); /*-****************************************
PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd); * Time functions
PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd); ******************************************/
#define SEC_TO_MICRO ((PTime)1000000) UTIL_time_t UTIL_getTime(void);
PTime UTIL_clockSpanMicro(UTIL_time_t clockStart); void UTIL_waitForNextTick(void);
PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd);
PTime UTIL_clockSpanNano(UTIL_time_t clockStart); PTime UTIL_clockSpanNano(UTIL_time_t clockStart);
void UTIL_waitForNextTick(void); #define SEC_TO_MICRO ((PTime)1000000)
PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd);
PTime UTIL_clockSpanMicro(UTIL_time_t clockStart);
#if defined (__cplusplus) #if defined (__cplusplus)

View File

@ -14,6 +14,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h> /* time(), for seed random initialization */
#include "util.h" #include "util.h"
#include "timefn.h" /* UTIL_clockSpanMicro, SEC_TO_MICRO, UTIL_TIME_INITIALIZER */ #include "timefn.h" /* UTIL_clockSpanMicro, SEC_TO_MICRO, UTIL_TIME_INITIALIZER */
@ -24,7 +25,7 @@
#include "zdict.h" #include "zdict.h"
/* Direct access to internal compression functions is required */ /* Direct access to internal compression functions is required */
#include "zstd_compress.c" #include "zstd_compress.c" /* ZSTD_resetSeqStore, ZSTD_storeSeq, *_TO_OFFBASE, HIST_countFast_wksp, HIST_isError */
#define XXH_STATIC_LINKING_ONLY #define XXH_STATIC_LINKING_ONLY
#include "xxhash.h" /* XXH64 */ #include "xxhash.h" /* XXH64 */
@ -165,7 +166,7 @@ static double RAND_exp(U32* seed, double mean)
/*-******************************************************* /*-*******************************************************
* Constants and Structs * Constants and Structs
*********************************************************/ *********************************************************/
const char *BLOCK_TYPES[] = {"raw", "rle", "compressed"}; const char* BLOCK_TYPES[] = {"raw", "rle", "compressed"};
#define MAX_DECOMPRESSED_SIZE_LOG 20 #define MAX_DECOMPRESSED_SIZE_LOG 20
#define MAX_DECOMPRESSED_SIZE (1ULL << MAX_DECOMPRESSED_SIZE_LOG) #define MAX_DECOMPRESSED_SIZE (1ULL << MAX_DECOMPRESSED_SIZE_LOG)

View File

@ -25,6 +25,7 @@
#include <stdlib.h> /* free */ #include <stdlib.h> /* free */
#include <stdio.h> /* fgets, sscanf */ #include <stdio.h> /* fgets, sscanf */
#include <string.h> /* strcmp */ #include <string.h> /* strcmp */
#include <time.h> /* time_t, time(), to randomize seed */
#include <assert.h> /* assert */ #include <assert.h> /* assert */
#include "timefn.h" /* UTIL_time_t, UTIL_getTime */ #include "timefn.h" /* UTIL_time_t, UTIL_getTime */
#include "mem.h" #include "mem.h"