]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Use util.h for timing
authorNick Terrell <terrelln@fb.com>
Thu, 30 Nov 2017 03:11:12 +0000 (19:11 -0800)
committerNick Terrell <terrelln@fb.com>
Thu, 30 Nov 2017 22:57:25 +0000 (14:57 -0800)
programs/bench.c
programs/dibio.c
programs/fileio.c
programs/util.h
tests/decodecorpus.c
tests/fuzzer.c
tests/paramgrill.c
tests/zbufftest.c
tests/zstreamtest.c

index 1df8aeb0d0125559d01133518e34a94272ae9d2b..384b982f400a82987c53549b8fb19fa3d843c4c9 100644 (file)
@@ -34,7 +34,6 @@
 #include <stdlib.h>      /* malloc, free */
 #include <string.h>      /* memset */
 #include <stdio.h>       /* fprintf, fopen */
-#include <time.h>        /* 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); } } }
 
 
 /* *************************************
index dea3ec44f4f21d8bf28c0695820532403dac0a4f..9cc5264d27ac56b9a86cdd73d10dd192139c6867 100644 (file)
@@ -26,7 +26,6 @@
 #include <stdlib.h>         /* malloc, free */
 #include <string.h>         /* memset */
 #include <stdio.h>          /* fprintf, fopen, ftello64 */
-#include <time.h>           /* clock_t, clock, CLOCKS_PER_SEC */
 #include <errno.h>          /* 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
index eb004f916040530aa7e98fcb704d7506dbf2e4e1..0ea1ad1521d59cd867303ec62261d17d0794c667 100644 (file)
@@ -29,7 +29,6 @@
 #include <stdio.h>      /* fprintf, fopen, fread, _fileno, stdin, stdout */
 #include <stdlib.h>     /* malloc, free */
 #include <string.h>     /* strcmp, strlen */
-#include <time.h>       /* clock */
 #include <errno.h>      /* 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)
 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))
index e44d7459ddfdce3b6cf0dfc495ac704f63d97496..41155694edd526cd0289bf27f1d91e0816eb712f 100644 (file)
@@ -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 <mach/mach_time.h>
+    #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 <time.h>
+    #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; }
index e697e519cfc1ea235788c9c0f95b19ff107b525c..7d7b64664f87de61543c60c9173a67d579442285 100644 (file)
@@ -14,8 +14,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <time.h>
 
+#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
index be9dc3e062e19e323bafe00e8c3762181873ebe5..980c361768f864dd2d61eab748d23f8dc244a2f9 100644 (file)
@@ -25,7 +25,6 @@
 #include <stdlib.h>       /* free */
 #include <stdio.h>        /* fgets, sscanf */
 #include <string.h>       /* strcmp */
-#include <time.h>         /* 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;
index 081a284e42fb46a58d3516b838e15648c4c02e22..e258932dabbf2736f96f9177d66ae4ba3de1f7da 100644 (file)
 #include <stdio.h>     /* fprintf, fopen, ftello64 */
 #include <string.h>    /* strcmp */
 #include <math.h>      /* log */
-#include <time.h>      /* clock_t */
+#include <time.h>
 
 #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(&params, 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<nbBlocks; blockNb++)
                     blockTable[blockNb].cSize = ZSTD_compress_advanced(ctx,
                                                     blockTable[blockNb].cPtr,  blockTable[blockNb].cRoom,
@@ -251,13 +249,13 @@ static size_t BMK_benchParam(BMK_result_t* resultPtr,
                                                     params);
                 nbLoops++;
             }
-            roundClock = BMK_clockSpan(roundStart);
+            roundClock = UTIL_clockSpanMicro(roundStart);
 
             cSize = 0;
             for (blockNb=0; blockNb<nbBlocks; blockNb++)
                 cSize += blockTable[blockNb].cSize;
             ratio = (double)srcSize / (double)cSize;
-            if ((double)roundClock < fastestC * CLOCKS_PER_SEC * nbLoops) fastestC = ((double)roundClock / CLOCKS_PER_SEC) / nbLoops;
+            if ((double)roundClock < fastestC * SEC_TO_MICRO * nbLoops) fastestC = ((double)roundClock / SEC_TO_MICRO) / nbLoops;
             DISPLAY("\r");
             DISPLAY("%1u-%s : %9u ->", 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<nbBlocks; blockNb++)
                     blockTable[blockNb].resSize = ZSTD_decompress(blockTable[blockNb].resPtr, blockTable[blockNb].srcSize,
                                                                   blockTable[blockNb].cPtr, blockTable[blockNb].cSize);
             }
-            roundClock = BMK_clockSpan(roundStart);
+            roundClock = UTIL_clockSpanMicro(roundStart);
 
-            if ((double)roundClock < fastestD * CLOCKS_PER_SEC * nbLoops) fastestD = ((double)roundClock / CLOCKS_PER_SEC) / nbLoops;
+            if ((double)roundClock < fastestD * SEC_TO_MICRO * nbLoops) fastestD = ((double)roundClock / SEC_TO_MICRO) / nbLoops;
             DISPLAY("\r");
             DISPLAY("%1u-%s : %9u -> ", 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;
index ce5e5518a5e652bcef51b78328f1430c4859d335..f770c8395d29d33f21bcf0e320fe839436797eec 100644 (file)
@@ -24,7 +24,6 @@
 **************************************/
 #include <stdlib.h>       /* free */
 #include <stdio.h>        /* fgets, sscanf */
-#include <time.h>         /* clock_t, clock() */
 #include <string.h>       /* 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 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 ((FUZ_GetClockSpan(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
-            { g_displayClock = 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 g_refreshRate = CLOCKS_PER_SEC * 15 / 100;
-static clock_t g_displayClock = 0;
 
-static clock_t g_clockTime = 0;
+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':
index b8c437a41977bc805e92a1bc1e41bd6b4a4bc26b..aad9cba71deabf92491c7f91b1000571363f74a0 100644 (file)
@@ -24,7 +24,6 @@
 **************************************/
 #include <stdlib.h>       /* free */
 #include <stdio.h>        /* fgets, sscanf */
-#include <time.h>         /* clock_t, clock() */
 #include <string.h>       /* strcmp */
 #include <assert.h>       /* 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 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 ((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;
+            if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (g_displayLevel>=4)) \
+            { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \
+            if (g_displayLevel>=4) fflush(stderr); } }
 
-static clock_t g_clockTime = 0;
+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 */