bench correctly measures time for multi-threaded compression (posix only)

This commit is contained in:
Yann Collet
2016-12-31 03:31:26 +01:00
parent f765a375a5
commit c6a6417458
3 changed files with 32 additions and 19 deletions
+5 -9
View File
@@ -60,9 +60,8 @@ static void *POOL_thread(void *opaque) {
if (pthread_mutex_unlock(&ctx->queueMutex)) { return NULL; } if (pthread_mutex_unlock(&ctx->queueMutex)) { return NULL; }
return opaque; return opaque;
} }
{ /* Pop a job off the queue */
/* Pop a job off the queue */ { POOL_job job = ctx->queue[ctx->queueHead];
POOL_job job = ctx->queue[ctx->queueHead];
ctx->queueHead = (ctx->queueHead + 1) % ctx->queueSize; ctx->queueHead = (ctx->queueHead + 1) % ctx->queueSize;
/* Unlock the mutex, signal a pusher, and run the job */ /* Unlock the mutex, signal a pusher, and run the job */
if (pthread_mutex_unlock(&ctx->queueMutex)) { return NULL; } if (pthread_mutex_unlock(&ctx->queueMutex)) { return NULL; }
@@ -105,8 +104,7 @@ POOL_ctx *POOL_create(size_t numThreads, size_t queueSize) {
ctx->numThreads = i; ctx->numThreads = i;
POOL_free(ctx); POOL_free(ctx);
return NULL; return NULL;
} } }
}
ctx->numThreads = numThreads; ctx->numThreads = numThreads;
} }
return ctx; return ctx;
@@ -127,8 +125,7 @@ static void POOL_join(POOL_ctx *ctx) {
{ size_t i; { size_t i;
for (i = 0; i < ctx->numThreads; ++i) { for (i = 0; i < ctx->numThreads; ++i) {
pthread_join(ctx->threads[i], NULL); pthread_join(ctx->threads[i], NULL);
} } }
}
} }
void POOL_free(POOL_ctx *ctx) { void POOL_free(POOL_ctx *ctx) {
@@ -147,8 +144,7 @@ void POOL_add(void *ctxVoid, POOL_function function, void *opaque) {
if (!ctx) { return; } if (!ctx) { return; }
pthread_mutex_lock(&ctx->queueMutex); pthread_mutex_lock(&ctx->queueMutex);
{ { POOL_job const job = {function, opaque};
POOL_job job = {function, opaque};
/* Wait until there is space in the queue for the new job */ /* Wait until there is space in the queue for the new job */
size_t newTail = (ctx->queueTail + 1) % ctx->queueSize; size_t newTail = (ctx->queueTail + 1) % ctx->queueSize;
while (ctx->queueHead == newTail && !ctx->shutdown) { while (ctx->queueHead == newTail && !ctx->shutdown) {
+3 -3
View File
@@ -322,16 +322,16 @@ size_t ZSTDMT_freeCCtx(ZSTDMT_CCtx* cctx)
return 0; return 0;
} }
size_t ZSTDMT_compressCCtx(ZSTDMT_CCtx* cctx, size_t ZSTDMT_compressCCtx(ZSTDMT_CCtx* mtctx,
void* dst, size_t dstCapacity, void* dst, size_t dstCapacity,
const void* src, size_t srcSize, const void* src, size_t srcSize,
int compressionLevel) int compressionLevel)
{ {
ZSTDMT_jobAgency* jobAgency = &cctx->jobAgency; ZSTDMT_jobAgency* jobAgency = &mtctx->jobAgency;
ZSTD_parameters const params = ZSTD_getParams(compressionLevel, srcSize, 0); ZSTD_parameters const params = ZSTD_getParams(compressionLevel, srcSize, 0);
size_t const frameSizeTarget = (size_t)1 << (params.cParams.windowLog + 2); size_t const frameSizeTarget = (size_t)1 << (params.cParams.windowLog + 2);
unsigned const nbFramesMax = (unsigned)(srcSize / frameSizeTarget) + (srcSize < frameSizeTarget) /* min 1 */; unsigned const nbFramesMax = (unsigned)(srcSize / frameSizeTarget) + (srcSize < frameSizeTarget) /* min 1 */;
unsigned const nbFrames = MIN(nbFramesMax, cctx->nbThreads); unsigned const nbFrames = MIN(nbFramesMax, mtctx->nbThreads);
size_t const avgFrameSize = (srcSize + (nbFrames-1)) / nbFrames; size_t const avgFrameSize = (srcSize + (nbFrames-1)) / nbFrames;
size_t remainingSrcSize = srcSize; size_t remainingSrcSize = srcSize;
const char* const srcStart = (const char*)src; const char* const srcStart = (const char*)src;
+24 -7
View File
@@ -24,7 +24,7 @@
#include "util.h" /* UTIL_getFileSize, UTIL_sleep */ #include "util.h" /* UTIL_getFileSize, UTIL_sleep */
#include <stdlib.h> /* malloc, free */ #include <stdlib.h> /* malloc, free */
#include <string.h> /* memset */ #include <string.h> /* memset */
#include <stdio.h> /* fprintf, fopen, ftello64 */ #include <stdio.h> /* fprintf, fopen */
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */ #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
#include "mem.h" #include "mem.h"
@@ -88,6 +88,23 @@ static clock_t g_time = 0;
exit(error); \ exit(error); \
} }
/* *************************************
* Time
***************************************/
/* for posix only - proper detection macros to setup */
#include <unistd.h>
#include <sys/times.h>
typedef unsigned long long clock_us_t;
static clock_us_t BMK_clockMicroSec()
{
static clock_t _ticksPerSecond = 0;
if (_ticksPerSecond <= 0) _ticksPerSecond = sysconf(_SC_CLK_TCK);
struct tms junk; clock_t newTicks = (clock_t) times(&junk); (void)junk;
return ((((clock_us_t)newTicks)*(1000000))/_ticksPerSecond);
}
/* ************************************* /* *************************************
* Benchmark Parameters * Benchmark Parameters
@@ -231,7 +248,6 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
UTIL_getTime(&coolTime); UTIL_getTime(&coolTime);
DISPLAYLEVEL(2, "\r%79s\r", ""); DISPLAYLEVEL(2, "\r%79s\r", "");
while (!cCompleted || !dCompleted) { while (!cCompleted || !dCompleted) {
UTIL_time_t clockStart;
/* overheat protection */ /* overheat protection */
if (UTIL_clockSpanMicro(coolTime, ticksPerSecond) > ACTIVEPERIOD_MICROSEC) { if (UTIL_clockSpanMicro(coolTime, ticksPerSecond) > ACTIVEPERIOD_MICROSEC) {
@@ -241,13 +257,14 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
} }
if (!g_decodeOnly) { if (!g_decodeOnly) {
clock_us_t clockStart;
/* Compression */ /* Compression */
DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (U32)srcSize); DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (U32)srcSize);
if (!cCompleted) memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */ if (!cCompleted) memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */
UTIL_sleepMilli(1); /* give processor time to other processes */ UTIL_sleepMilli(1); /* give processor time to other processes */
UTIL_waitForNextTick(ticksPerSecond); UTIL_waitForNextTick(ticksPerSecond);
UTIL_getTime(&clockStart); clockStart = BMK_clockMicroSec();
if (!cCompleted) { /* still some time to do compression tests */ if (!cCompleted) { /* still some time to do compression tests */
ZSTD_parameters zparams = ZSTD_getParams(cLevel, avgSize, dictBufferSize); ZSTD_parameters zparams = ZSTD_getParams(cLevel, avgSize, dictBufferSize);
@@ -286,11 +303,11 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
blockTable[blockNb].cSize = rSize; blockTable[blockNb].cSize = rSize;
} }
nbLoops++; nbLoops++;
} while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop); } while (BMK_clockMicroSec() - clockStart < clockLoop);
ZSTD_freeCDict(cdict); ZSTD_freeCDict(cdict);
{ U64 const clockSpan = UTIL_clockSpanMicro(clockStart, ticksPerSecond); { clock_us_t const clockSpanMicro = BMK_clockMicroSec() - clockStart;
if (clockSpan < fastestC*nbLoops) fastestC = clockSpan / nbLoops; if (clockSpanMicro < fastestC*nbLoops) fastestC = clockSpanMicro / nbLoops;
totalCTime += clockSpan; totalCTime += clockSpanMicro;
cCompleted = (totalCTime >= maxTime); cCompleted = (totalCTime >= maxTime);
} } } }