fix cpuload measurements on Windows

clock() does not behave the same (as posix) on Windows
This commit is contained in:
Yann Collet
2024-07-06 16:47:05 -07:00
parent a6fe6bbd7d
commit 5301a75c5a
2 changed files with 20 additions and 3 deletions

View File

@@ -109,6 +109,20 @@ static int g_displayLevel = 0; /* 0 : no display ; 1: errors ; 2 : + result
static const Duration_ns refreshRate = 200000000;
static TIME_t g_time = { 0 };
static double cpuLoad_sec(clock_t cpuStart)
{
#ifdef _WIN32
FILETIME creationTime, exitTime, kernelTime, userTime;
(void)cpuStart;
GetProcessTimes(GetCurrentProcess(), &creationTime, &exitTime, &kernelTime, &userTime);
assert(kernelTime.dwHighDateTime == 0);
assert(userTime.dwHighDateTime == 0);
return ((double)kernelTime.dwLowDateTime + (double)userTime.dwLowDateTime) * 100. / 1000000000.;
#else
return (double)(clock() - cpuStart) / CLOCKS_PER_SEC;
#endif
}
static void LZ4IO_finalTimeDisplay(TIME_t timeStart, clock_t cpuStart, unsigned long long size)
{
#if LZ4IO_MULTITHREAD
@@ -119,7 +133,7 @@ static void LZ4IO_finalTimeDisplay(TIME_t timeStart, clock_t cpuStart, unsigned
{
Duration_ns duration_ns = TIME_clockSpan_ns(timeStart);
double const seconds = (double)(duration_ns + !duration_ns) / (double)1000000000.;
double const cpuLoad_s = (double)(clock() - cpuStart) / CLOCKS_PER_SEC;
double const cpuLoad_s = cpuLoad_sec(cpuStart);
DISPLAYLEVEL(3,"Done in %.2f s ==> %.2f MiB/s (cpu load : %.0f%%)\n", seconds,
(double)size / seconds / 1024. / 1024.,
(cpuLoad_s / seconds) * 100.);

View File

@@ -48,14 +48,17 @@ typedef struct {
* Time functions
******************************************/
/* @return a TIME_t value to be compared to another one in order to compute a duration.
* The absolute value returned is meaningless */
TIME_t TIME_getTime(void);
/* Timer resolution can be low on some platforms.
* To improve accuracy, it's recommended to wait for a new tick
* before starting benchmark measurements */
void TIME_waitForNextTick(void);
/* tells if timefn will return correct time measurements
* in presence of multi-threaded workload.
/* tells if TIME_getTime() returns correct time measurements
* in scenarios involving multi-threaded workload.
* note : this is not the case if only C90 clock_t measurements are available */
int TIME_support_MT_measurements(void);