From 9a2f6f477ba8cefa0040bb0323203aadde1e7c32 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Wed, 29 Nov 2017 19:11:12 -0800 Subject: [PATCH] Use util.h for timing --- programs/bench.c | 15 +++++++------- programs/dibio.c | 16 +++++++-------- programs/fileio.c | 12 ++++++----- programs/util.h | 4 ++++ tests/decodecorpus.c | 21 +++++++++----------- tests/fuzzer.c | 28 ++++++++++++-------------- tests/paramgrill.c | 47 +++++++++++++++++++++----------------------- tests/zbufftest.c | 35 +++++++++++++++------------------ tests/zstreamtest.c | 43 +++++++++++++++++++--------------------- 9 files changed, 106 insertions(+), 115 deletions(-) diff --git a/programs/bench.c b/programs/bench.c index 1df8aeb0..384b982f 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -34,7 +34,6 @@ #include /* malloc, free */ #include /* memset */ #include /* fprintf, fopen */ -#include /* clock_t, clock, CLOCKS_PER_SEC */ #include "mem.h" #define ZSTD_STATIC_LINKING_ONLY @@ -72,12 +71,14 @@ static U32 g_compressibilityDefault = 50; #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } static int g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ -#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ - if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \ - { g_time = clock(); DISPLAY(__VA_ARGS__); \ - if (g_displayLevel>=4) fflush(stderr); } } -static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100; -static clock_t g_time = 0; +#define SEC_TO_MICRO 1000000 +static const U64 g_refreshRate = SEC_TO_MICRO / 6; +static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; + +#define DISPLAYUPDATE(l, ...) { if (g_displayLevel>=l) { \ + if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ + { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \ + if (g_displayLevel>=4) fflush(stderr); } } } /* ************************************* diff --git a/programs/dibio.c b/programs/dibio.c index dea3ec44..9cc5264d 100644 --- a/programs/dibio.c +++ b/programs/dibio.c @@ -26,7 +26,6 @@ #include /* malloc, free */ #include /* memset */ #include /* fprintf, fopen, ftello64 */ -#include /* clock_t, clock, CLOCKS_PER_SEC */ #include /* errno */ #include "mem.h" /* read */ @@ -55,15 +54,14 @@ static const size_t g_maxMemory = (sizeof(size_t) == 4) ? (2 GB - 64 MB) : ((siz #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) #define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } -#define DISPLAYUPDATE(l, ...) if (displayLevel>=l) { \ - if ((DIB_clockSpan(g_time) > refreshRate) || (displayLevel>=4)) \ - { g_time = clock(); DISPLAY(__VA_ARGS__); \ - if (displayLevel>=4) fflush(stderr); } } -static const clock_t refreshRate = CLOCKS_PER_SEC * 2 / 10; -static clock_t g_time = 0; - -static clock_t DIB_clockSpan(clock_t nPrevious) { return clock() - nPrevious; } +#define SEC_TO_MICRO 1000000 +static const U64 g_refreshRate = SEC_TO_MICRO / 6; +static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; +#define DISPLAYUPDATE(l, ...) { if (displayLevel>=l) { \ + if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (displayLevel>=4)) \ + { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \ + if (displayLevel>=4) fflush(stderr); } } } /*-************************************* * Exceptions diff --git a/programs/fileio.c b/programs/fileio.c index eb004f91..0ea1ad15 100644 --- a/programs/fileio.c +++ b/programs/fileio.c @@ -29,7 +29,6 @@ #include /* fprintf, fopen, fread, _fileno, stdin, stdout */ #include /* malloc, free */ #include /* strcmp, strlen */ -#include /* clock */ #include /* errno */ #if defined (_MSC_VER) @@ -40,6 +39,7 @@ #include "bitstream.h" #include "mem.h" #include "fileio.h" +#include "util.h" #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_magicNumber, ZSTD_frameHeaderSize_max */ #include "zstd.h" #if defined(ZSTD_GZCOMPRESS) || defined(ZSTD_GZDECOMPRESS) @@ -81,12 +81,14 @@ static int g_displayLevel = 2; /* 0 : no display; 1: errors; 2: + result + interaction + warnings; 3: + progression; 4: + information */ void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; } +#define SEC_TO_MICRO 1000000 +static const U64 g_refreshRate = SEC_TO_MICRO / 6; +static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; + #define DISPLAYUPDATE(l, ...) { if (g_displayLevel>=l) { \ - if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \ - { g_time = clock(); DISPLAY(__VA_ARGS__); \ + if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ + { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \ if (g_displayLevel>=4) fflush(stderr); } } } -static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100; -static clock_t g_time = 0; #undef MIN /* in case it would be already defined */ #define MIN(a,b) ((a) < (b) ? (a) : (b)) diff --git a/programs/util.h b/programs/util.h index e44d7459..41155694 100644 --- a/programs/util.h +++ b/programs/util.h @@ -118,6 +118,7 @@ static int g_utilDisplayLevel; * Time functions ******************************************/ #if defined(_WIN32) /* Windows */ + #define UTIL_TIME_INITIALIZER { { 0, 0 } } typedef LARGE_INTEGER UTIL_time_t; UTIL_STATIC UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; } UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) @@ -144,6 +145,7 @@ static int g_utilDisplayLevel; } #elif defined(__APPLE__) && defined(__MACH__) #include + #define UTIL_TIME_INITIALIZER 0 typedef U64 UTIL_time_t; UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); } UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) @@ -168,6 +170,7 @@ static int g_utilDisplayLevel; } #elif (PLATFORM_POSIX_VERSION >= 200112L) #include + #define UTIL_TIME_INITIALIZER { 0, 0 } typedef struct timespec UTIL_freq_t; typedef struct timespec UTIL_time_t; UTIL_STATIC UTIL_time_t UTIL_getTime(void) @@ -207,6 +210,7 @@ static int g_utilDisplayLevel; } #else /* relies on standard C (note : clock_t measurements can be wrong when using multi-threading) */ typedef clock_t UTIL_time_t; + #define UTIL_TIME_INITIALIZER 0 UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return clock(); } UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; } UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; } diff --git a/tests/decodecorpus.c b/tests/decodecorpus.c index e697e519..7d7b6466 100644 --- a/tests/decodecorpus.c +++ b/tests/decodecorpus.c @@ -14,8 +14,8 @@ #include #include #include -#include +#include "util.h" #include "zstd.h" #include "zstd_internal.h" #include "mem.h" @@ -49,20 +49,17 @@ static U32 g_displayLevel = 2; #define DISPLAYUPDATE(...) \ do { \ - if ((clockSpan(g_displayClock) > g_refreshRate) || \ + if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || \ (g_displayLevel >= 4)) { \ - g_displayClock = clock(); \ + g_displayClock = UTIL_getTime(); \ DISPLAY(__VA_ARGS__); \ if (g_displayLevel >= 4) fflush(stderr); \ } \ } while (0) -static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6; -static clock_t g_displayClock = 0; -static clock_t clockSpan(clock_t cStart) -{ - return clock() - cStart; /* works even when overflow; max span ~ 30mn */ -} +#define SEC_TO_MICRO 1000000 +static const U64 g_refreshRate = SEC_TO_MICRO / 6; +static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; #define CHECKERR(code) \ do { \ @@ -1531,14 +1528,14 @@ static int runTestMode(U32 seed, unsigned numFiles, unsigned const testDurationS { unsigned fnum; - clock_t const startClock = clock(); - clock_t const maxClockSpan = testDurationS * CLOCKS_PER_SEC; + UTIL_time_t const startClock = UTIL_getTime(); + U64 const maxClockSpan = testDurationS * SEC_TO_MICRO; if (numFiles == 0 && !testDurationS) numFiles = 1; DISPLAY("seed: %u\n", seed); - for (fnum = 0; fnum < numFiles || clockSpan(startClock) < maxClockSpan; fnum++) { + for (fnum = 0; fnum < numFiles || UTIL_clockSpanMicro(startClock) < maxClockSpan; fnum++) { if (fnum < numFiles) DISPLAYUPDATE("\r%u/%u ", fnum, numFiles); else diff --git a/tests/fuzzer.c b/tests/fuzzer.c index be9dc3e0..980c3617 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -25,7 +25,6 @@ #include /* free */ #include /* fgets, sscanf */ #include /* strcmp */ -#include /* clock_t */ #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressContinue, ZSTD_compressBlock */ #include "zstd.h" /* ZSTD_VERSION_STRING */ #include "zstd_errors.h" /* ZSTD_getErrorCode */ @@ -36,6 +35,7 @@ #include "mem.h" #define XXH_STATIC_LINKING_ONLY #include "xxhash.h" /* XXH64 */ +#include "util.h" /*-************************************ @@ -56,25 +56,23 @@ static const U32 nbTestsDefault = 30000; #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } static U32 g_displayLevel = 2; -#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ - if ((FUZ_clockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ - { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \ - if (g_displayLevel>=4) fflush(stdout); } } -static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6; -static clock_t g_displayClock = 0; +#define SEC_TO_MICRO 1000000 +static const U64 g_refreshRate = SEC_TO_MICRO / 6; +static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; +#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ + if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ + { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \ + if (g_displayLevel>=4) fflush(stderr); } } /*-******************************************************* * Fuzzer functions *********************************************************/ +#undef MIN +#undef MAX #define MIN(a,b) ((a)<(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b)) -static clock_t FUZ_clockSpan(clock_t cStart) -{ - return clock() - cStart; /* works even when overflow; max span ~ 30mn */ -} - #define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) static unsigned FUZ_rand(unsigned* src) { @@ -1208,8 +1206,8 @@ static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, U32 const maxD U32 result = 0; U32 testNb = 0; U32 coreSeed = seed, lseed = 0; - clock_t const startClock = clock(); - clock_t const maxClockSpan = maxDurationS * CLOCKS_PER_SEC; + UTIL_time_t const startClock = UTIL_getTime(); + U64 const maxClockSpan = maxDurationS * SEC_TO_MICRO; int const cLevelLimiter = bigTests ? 3 : 2; /* allocation */ @@ -1234,7 +1232,7 @@ static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, U32 const maxD for (testNb=1; testNb < startTest; testNb++) FUZ_rand(&coreSeed); /* main test loop */ - for ( ; (testNb <= nbTests) || (FUZ_clockSpan(startClock) < maxClockSpan); testNb++ ) { + for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < maxClockSpan); testNb++ ) { size_t sampleSize, maxTestSize, totalTestSize; size_t cSize, totalCSize, totalGenSize; U64 crcOrig; diff --git a/tests/paramgrill.c b/tests/paramgrill.c index 081a284e..e258932d 100644 --- a/tests/paramgrill.c +++ b/tests/paramgrill.c @@ -17,13 +17,14 @@ #include /* fprintf, fopen, ftello64 */ #include /* strcmp */ #include /* log */ -#include /* clock_t */ +#include #include "mem.h" #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_parameters, ZSTD_estimateCCtxSize */ #include "zstd.h" #include "datagen.h" #include "xxhash.h" +#include "util.h" /*-************************************ @@ -38,8 +39,9 @@ #define MB *(1<<20) #define GB *(1ULL<<30) +#define SEC_TO_MICRO 1000000 #define NBLOOPS 2 -#define TIMELOOP (2 * CLOCKS_PER_SEC) +#define TIMELOOP (2 * SEC_TO_MICRO) #define NB_LEVELS_TRACKED 30 @@ -49,8 +51,8 @@ static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t static const size_t sampleSize = 10000000; static const double g_grillDuration_s = 90000; /* about 24 hours */ -static const clock_t g_maxParamTime = 15 * CLOCKS_PER_SEC; -static const clock_t g_maxVariationTime = 60 * CLOCKS_PER_SEC; +static const U64 g_maxParamTime = 15 * SEC_TO_MICRO; +static const U64 g_maxVariationTime = 60 * SEC_TO_MICRO; static const int g_maxNbVariations = 64; @@ -88,13 +90,9 @@ void BMK_SetNbIterations(int nbLoops) * Private functions *********************************************************/ -/* works even if overflow ; max span ~ 30 mn */ -static clock_t BMK_clockSpan(clock_t cStart) { return clock() - cStart; } - /* accuracy in seconds only, span can be multiple years */ static double BMK_timeSpan(time_t tStart) { return difftime(time(NULL), tStart); } - static size_t BMK_findMaxMem(U64 requiredMem) { size_t const step = 64 MB; @@ -221,7 +219,7 @@ static size_t BMK_benchParam(BMK_result_t* resultPtr, size_t cSize = 0; double fastestC = 100000000., fastestD = 100000000.; double ratio = 0.; - clock_t const benchStart = clock(); + UTIL_time_t const benchStart = UTIL_getTime(); DISPLAY("\r%79s\r", ""); memset(¶ms, 0, sizeof(params)); @@ -229,9 +227,10 @@ static size_t BMK_benchParam(BMK_result_t* resultPtr, for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) { int nbLoops; U32 blockNb; - clock_t roundStart, roundClock; + UTIL_time_t roundStart; + U64 roundClock; - { clock_t const benchTime = BMK_clockSpan(benchStart); + { U64 const benchTime = UTIL_clockSpanMicro(benchStart); if (benchTime > g_maxParamTime) break; } /* Compression */ @@ -239,10 +238,9 @@ static size_t BMK_benchParam(BMK_result_t* resultPtr, memset(compressedBuffer, 0xE5, maxCompressedSize); nbLoops = 0; - roundStart = clock(); - while (clock() == roundStart); - roundStart = clock(); - while (BMK_clockSpan(roundStart) < TIMELOOP) { + UTIL_waitForNextTick(); + roundStart = UTIL_getTime(); + while (UTIL_clockSpanMicro(roundStart) < TIMELOOP) { for (blockNb=0; blockNb", loopNb, name, (U32)srcSize); DISPLAY(" %9u (%4.3f),%7.1f MB/s", (U32)cSize, ratio, (double)srcSize / fastestC / 1000000.); @@ -269,17 +267,16 @@ static size_t BMK_benchParam(BMK_result_t* resultPtr, memset(resultBuffer, 0xD6, srcSize); nbLoops = 0; - roundStart = clock(); - while (clock() == roundStart); - roundStart = clock(); - for ( ; BMK_clockSpan(roundStart) < TIMELOOP; nbLoops++) { + UTIL_waitForNextTick(); + roundStart = UTIL_getTime(); + for ( ; UTIL_clockSpanMicro(roundStart) < TIMELOOP; nbLoops++) { for (blockNb=0; blockNb ", loopNb, name, (U32)srcSize); DISPLAY("%9u (%4.3f),%7.1f MB/s, ", (U32)cSize, ratio, (double)srcSize / fastestC / 1000000.); @@ -521,9 +518,9 @@ static void playAround(FILE* f, winnerInfo_t* winners, ZSTD_CCtx* ctx) { int nbVariations = 0; - clock_t const clockStart = clock(); + UTIL_time_t const clockStart = UTIL_getTime(); - while (BMK_clockSpan(clockStart) < g_maxVariationTime) { + while (UTIL_clockSpanMicro(clockStart) < g_maxVariationTime) { ZSTD_compressionParameters p = params; if (nbVariations++ > g_maxNbVariations) break; diff --git a/tests/zbufftest.c b/tests/zbufftest.c index ce5e5518..f770c839 100644 --- a/tests/zbufftest.c +++ b/tests/zbufftest.c @@ -24,7 +24,6 @@ **************************************/ #include /* free */ #include /* fgets, sscanf */ -#include /* clock_t, clock() */ #include /* strcmp */ #include "mem.h" #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_maxCLevel */ @@ -34,6 +33,7 @@ #include "datagen.h" /* RDG_genBuffer */ #define XXH_STATIC_LINKING_ONLY #include "xxhash.h" /* XXH64_* */ +#include "util.h" /*-************************************ @@ -58,26 +58,25 @@ static const U32 prime2 = 2246822519U; #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } static U32 g_displayLevel = 2; -#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ - if ((FUZ_GetClockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ - { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \ - if (g_displayLevel>=4) fflush(stderr); } } -static const clock_t g_refreshRate = CLOCKS_PER_SEC * 15 / 100; -static clock_t g_displayClock = 0; +#define SEC_TO_MICRO 1000000 +static const U64 g_refreshRate = SEC_TO_MICRO / 6; +static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; -static clock_t g_clockTime = 0; +#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ + if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ + { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \ + if (g_displayLevel>=4) fflush(stderr); } } + +static U64 g_clockTime = 0; /*-******************************************************* * Fuzzer functions *********************************************************/ +#undef MIN +#undef MAX +#define MIN(a,b) ((a)<(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b)) - -static clock_t FUZ_GetClockSpan(clock_t clockStart) -{ - return clock() - clockStart; /* works even when overflow. Max span ~ 30 mn */ -} - /*! FUZ_rand() : @return : a 27 bits random value, from a 32-bits `seed`. `seed` is also modified */ @@ -256,8 +255,6 @@ static size_t FUZ_randomLength(U32* seed, U32 maxLog) return FUZ_rLogLength(seed, logLength); } -#define MIN(a,b) ( (a) < (b) ? (a) : (b) ) - #define CHECK(cond, ...) if (cond) { DISPLAY("Error => "); DISPLAY(__VA_ARGS__); \ DISPLAY(" (seed %u, test nb %u) \n", seed, testNb); goto _output_error; } @@ -278,7 +275,7 @@ static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compres U32 coreSeed = seed; ZBUFF_CCtx* zc; ZBUFF_DCtx* zd; - clock_t startClock = clock(); + UTIL_time_t startClock = UTIL_getTime(); /* allocations */ zc = ZBUFF_createCCtx(); @@ -308,7 +305,7 @@ static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compres FUZ_rand(&coreSeed); /* test loop */ - for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) { + for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) { U32 lseed; const BYTE* srcBuffer; const BYTE* dict; @@ -548,7 +545,7 @@ int main(int argc, const char** argv) } if (*argument=='m') g_clockTime *=60, argument++; if (*argument=='n') argument++; - g_clockTime *= CLOCKS_PER_SEC; + g_clockTime *= SEC_TO_MICRO; break; case 's': diff --git a/tests/zstreamtest.c b/tests/zstreamtest.c index b8c437a4..aad9cba7 100644 --- a/tests/zstreamtest.c +++ b/tests/zstreamtest.c @@ -24,7 +24,6 @@ **************************************/ #include /* free */ #include /* fgets, sscanf */ -#include /* clock_t, clock() */ #include /* strcmp */ #include /* assert */ #include "mem.h" @@ -37,6 +36,7 @@ #define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */ #include "xxhash.h" /* XXH64_* */ #include "seqgen.h" +#include "util.h" /*-************************************ @@ -62,26 +62,25 @@ static const U32 prime32 = 2654435761U; if (g_displayLevel>=4) fflush(stderr); } static U32 g_displayLevel = 2; -#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ - if ((FUZ_GetClockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ - { g_displayClock = clock(); DISPLAY(__VA_ARGS__); \ - if (g_displayLevel>=4) fflush(stderr); } } -static const clock_t g_refreshRate = CLOCKS_PER_SEC / 6; -static clock_t g_displayClock = 0; +#define SEC_TO_MICRO 1000000 +static const U64 g_refreshRate = SEC_TO_MICRO / 6; +static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; -static clock_t g_clockTime = 0; +#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ + if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \ + { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \ + if (g_displayLevel>=4) fflush(stderr); } } + +static U64 g_clockTime = 0; /*-******************************************************* * Fuzzer functions *********************************************************/ +#undef MIN +#undef MAX +#define MIN(a,b) ((a)<(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b)) - -static clock_t FUZ_GetClockSpan(clock_t clockStart) -{ - return clock() - clockStart; /* works even when overflow. Max span ~ 30 mn */ -} - /*! FUZ_rand() : @return : a 27 bits random value, from a 32-bits `seed`. `seed` is also modified */ @@ -815,8 +814,6 @@ static size_t FUZ_randomLength(U32* seed, U32 maxLog) return FUZ_rLogLength(seed, logLength); } -#define MIN(a,b) ( (a) < (b) ? (a) : (b) ) - /* Return value in range minVal <= v <= maxVal */ static U32 FUZ_randomClampedLength(U32* seed, U32 minVal, U32 maxVal) { @@ -842,7 +839,7 @@ static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compres ZSTD_CStream* zc = ZSTD_createCStream(); /* will be re-created sometimes */ ZSTD_DStream* zd = ZSTD_createDStream(); /* will be re-created sometimes */ ZSTD_DStream* const zd_noise = ZSTD_createDStream(); - clock_t const startClock = clock(); + UTIL_time_t const startClock = UTIL_getTime(); const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */ size_t dictSize = 0; U32 oldTestLog = 0; @@ -872,7 +869,7 @@ static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, double compres FUZ_rand(&coreSeed); /* test loop */ - for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) { + for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) { U32 lseed; const BYTE* srcBuffer; size_t totalTestSize, totalGenSize, cSize; @@ -1092,7 +1089,7 @@ static int fuzzerTests_MT(U32 seed, U32 nbTests, unsigned startTest, double comp ZSTDMT_CCtx* zc = ZSTDMT_createCCtx(nbThreads); /* will be reset sometimes */ ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */ ZSTD_DStream* const zd_noise = ZSTD_createDStream(); - clock_t const startClock = clock(); + UTIL_time_t const startClock = UTIL_getTime(); const BYTE* dict=NULL; /* can keep same dict on 2 consecutive tests */ size_t dictSize = 0; U32 oldTestLog = 0; @@ -1123,7 +1120,7 @@ static int fuzzerTests_MT(U32 seed, U32 nbTests, unsigned startTest, double comp FUZ_rand(&coreSeed); /* test loop */ - for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) { + for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) { U32 lseed; const BYTE* srcBuffer; size_t totalTestSize, totalGenSize, cSize; @@ -1364,7 +1361,7 @@ static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double ZSTD_CCtx* zc = ZSTD_createCCtx(); /* will be reset sometimes */ ZSTD_DStream* zd = ZSTD_createDStream(); /* will be reset sometimes */ ZSTD_DStream* const zd_noise = ZSTD_createDStream(); - clock_t const startClock = clock(); + UTIL_time_t const startClock = UTIL_getTime(); const BYTE* dict = NULL; /* can keep same dict on 2 consecutive tests */ size_t dictSize = 0; U32 oldTestLog = 0; @@ -1397,7 +1394,7 @@ static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double FUZ_rand(&coreSeed); /* test loop */ - for ( ; (testNb <= nbTests) || (FUZ_GetClockSpan(startClock) < g_clockTime) ; testNb++ ) { + for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) { U32 lseed; const BYTE* srcBuffer; size_t totalTestSize, totalGenSize, cSize; @@ -1772,7 +1769,7 @@ int main(int argc, const char** argv) g_clockTime *=60, argument++; if (*argument=='n') argument++; /* -T1mn == -T60 */ } else if (*argument=='s') argument++; /* -T10s == -T10 */ - g_clockTime *= CLOCKS_PER_SEC; + g_clockTime *= SEC_TO_MICRO; break; case 's': /* manually select seed */