From 0e808d608bd0d4bb503093b809497c0705e0efbd Mon Sep 17 00:00:00 2001 From: George Lu Date: Mon, 4 Jun 2018 16:32:37 -0700 Subject: [PATCH 1/2] Make paramgrill use bench.c benchmarking --- programs/bench.c | 184 +++++++++++++++++++++--------- programs/bench.h | 19 ++++ tests/Makefile | 2 +- tests/paramgrill.c | 274 ++++++++++++--------------------------------- tests/playTests.sh | 1 + 5 files changed, 225 insertions(+), 255 deletions(-) diff --git a/programs/bench.c b/programs/bench.c index 014a4fd41..6a81cc8f2 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -41,6 +41,7 @@ #include "zstd.h" #include "datagen.h" /* RDG_genBuffer */ #include "xxhash.h" +#include "bench.h" /* ************************************* @@ -71,6 +72,7 @@ static U32 g_compressibilityDefault = 50; ***************************************/ #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } +#define DISPLAYLEVELP(l, ...) if (g_displayLevel>=l && printable) { DISPLAY(__VA_ARGS__); } static int g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ static const U64 g_refreshRate = SEC_TO_MICRO / 6; @@ -91,9 +93,9 @@ static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; #define DEBUGOUTPUT(...) { if (DEBUG) DISPLAY(__VA_ARGS__); } #define EXM_THROW(error, ...) { \ DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \ - DISPLAYLEVEL(1, "Error %i : ", error); \ - DISPLAYLEVEL(1, __VA_ARGS__); \ - DISPLAYLEVEL(1, " \n"); \ + DISPLAYLEVELP(1, "Error %i : ", error); \ + DISPLAYLEVELP(1, __VA_ARGS__); \ + DISPLAYLEVELP(1, " \n"); \ exit(error); \ } @@ -168,7 +170,6 @@ void BMK_setLdmHashEveryLog(unsigned ldmHashEveryLog) { g_ldmHashEveryLog = ldmHashEveryLog; } - /* ******************************************************** * Bench functions **********************************************************/ @@ -182,18 +183,17 @@ typedef struct { size_t resSize; } blockParam_t; - - #undef MIN #undef MAX #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MAX(a,b) ((a) > (b) ? (a) : (b)) -static int BMK_benchMem(const void* srcBuffer, size_t srcSize, +BMK_result_t BMK_benchMem(const void* srcBuffer, size_t srcSize, const char* displayName, int cLevel, const size_t* fileSizes, U32 nbFiles, const void* dictBuffer, size_t dictBufferSize, - const ZSTD_compressionParameters* const comprParams) + const ZSTD_compressionParameters* const comprParams, + ZSTD_CCtx* ctx, ZSTD_DCtx* dctx, int printable) { size_t const blockSize = ((g_blockSize>=32 && !g_decodeOnly) ? g_blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ; U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles; @@ -201,13 +201,16 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, size_t const maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */ void* const compressedBuffer = malloc(maxCompressedSize); void* resultBuffer = malloc(srcSize); - ZSTD_CCtx* const ctx = ZSTD_createCCtx(); - ZSTD_DCtx* const dctx = ZSTD_createDCtx(); + BMK_result_t results; + size_t const loadedCompressedSize = srcSize; size_t cSize = 0; double ratio = 0.; U32 nbBlocks; + assert(ctx != NULL); + assert(dctx != NULL); + /* checks */ if (!compressedBuffer || !resultBuffer || !blockTable || !ctx || !dctx) EXM_THROW(31, "allocation error : not enough memory"); @@ -281,19 +284,19 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, U32 markNb = 0; coolTime = UTIL_getTime(); - DISPLAYLEVEL(2, "\r%79s\r", ""); + DISPLAYLEVELP(2, "\r%79s\r", ""); while (!cCompleted || !dCompleted) { /* overheat protection */ if (UTIL_clockSpanMicro(coolTime) > ACTIVEPERIOD_MICROSEC) { - DISPLAYLEVEL(2, "\rcooling down ... \r"); + DISPLAYLEVELP(2, "\rcooling down ... \r"); UTIL_sleep(COOLPERIOD_SEC); coolTime = UTIL_getTime(); } if (!g_decodeOnly) { /* Compression */ - DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (U32)srcSize); + DISPLAYLEVELP(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (U32)srcSize); if (!cCompleted) memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */ UTIL_sleepMilli(5); /* give processor time to other processes */ @@ -369,11 +372,13 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, cSize = 0; { U32 blockNb; for (blockNb=0; blockNb%10u (%5.*f),%6.*f MB/s\r", + results.cSpeed = compressionSpeed * 1000000; + DISPLAYLEVELP(2, "%2s-%-17.17s :%10u ->%10u (%5.*f),%6.*f MB/s\r", marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratioAccuracy, ratio, cSpeedAccuracy, compressionSpeed ); @@ -428,7 +433,9 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, double const compressionSpeed = ((double)srcSize / fastestC) * 1000; int const cSpeedAccuracy = (compressionSpeed < 10.) ? 2 : 1; double const decompressionSpeed = ((double)srcSize / fastestD) * 1000; - DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.*f),%6.*f MB/s ,%6.1f MB/s \r", + results.cSpeed = compressionSpeed * 1000000; + results.dSpeed = decompressionSpeed * 1000000; + DISPLAYLEVELP(2, "%2s-%-17.17s :%10u ->%10u (%5.*f),%6.*f MB/s ,%6.1f MB/s \r", marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratioAccuracy, ratio, cSpeedAccuracy, compressionSpeed, @@ -483,18 +490,67 @@ static int BMK_benchMem(const void* srcBuffer, size_t srcSize, else DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName); } - DISPLAYLEVEL(2, "%2i#\n", cLevel); + DISPLAYLEVELP(2, "%2i#\n", cLevel); } /* Bench */ /* clean up */ free(blockTable); free(compressedBuffer); free(resultBuffer); - ZSTD_freeCCtx(ctx); - ZSTD_freeDCtx(dctx); - return 0; + return results; } +/* + * combines multiple results into single + * currently normal averaging + * consider weighing by file size? or maybe by total time? + * size is cumulative though + */ +static BMK_result_t BMK_average(BMK_result_t* results, const int count, const size_t* sampleSizes) { + double cSpeedTotal = 0; + double dSpeedTotal = 0; + size_t cSizeTotal = 0; + BMK_result_t returnval; + int i; + if (sampleSizes == NULL) { + for(i = 0; i < count; i++) { + cSpeedTotal += results[i].cSpeed; + dSpeedTotal += results[i].dSpeed; + cSizeTotal += results[i].cSize; + } + returnval.cSpeed = cSpeedTotal / count; + returnval.dSpeed = dSpeedTotal / count; + returnval.cSize = cSizeTotal; + } else { + size_t sSizeTotal = 0; + /* in this case, SpeedTotal will keep track of total time used */ + for(i = 0; i < count; i++) { + cSpeedTotal += sampleSizes[i] / results[i].cSpeed; + dSpeedTotal += sampleSizes[i] / results[i].cSpeed; + cSizeTotal += results[i].cSize; + sSizeTotal += sampleSizes[i]; + } + returnval.cSpeed = (double)sSizeTotal / cSpeedTotal; + returnval.dSpeed = (double)sSizeTotal / dSpeedTotal; + returnval.cSize = cSizeTotal; + } + return returnval; +} + +static BMK_result_t BMK_benchMemCtxless(const void* srcBuffer, size_t srcSize, + const char* displayName, int cLevel, + const size_t* fileSizes, U32 nbFiles, + const void* dictBuffer, size_t dictBufferSize, + const ZSTD_compressionParameters* const comprParams, int printable) +{ + BMK_result_t returnval; + ZSTD_CCtx* ctx = ZSTD_createCCtx(); + ZSTD_DCtx* dctx = ZSTD_createDCtx(); + returnval = BMK_benchMem(srcBuffer, srcSize, displayName, cLevel, fileSizes, nbFiles, dictBuffer, dictBufferSize, comprParams, ctx, dctx, printable); + ZSTD_freeCCtx(ctx); + ZSTD_freeDCtx(dctx); + return returnval; +} static size_t BMK_findMaxMem(U64 requiredMem) { @@ -514,20 +570,28 @@ static size_t BMK_findMaxMem(U64 requiredMem) return (size_t)(requiredMem); } -static void BMK_benchCLevel(const void* srcBuffer, size_t benchedSize, +/* returns average stats over all range [cLevel, cLevelLast] */ +static BMK_result_t BMK_benchCLevel(const void* srcBuffer, size_t benchedSize, const char* displayName, int cLevel, int cLevelLast, const size_t* fileSizes, unsigned nbFiles, const void* dictBuffer, size_t dictBufferSize, - const ZSTD_compressionParameters* const compressionParams) + const ZSTD_compressionParameters* const compressionParams, + int printable) { int l; + BMK_result_t returnval; + BMK_result_t* resultarray = (BMK_result_t*)malloc(sizeof(BMK_result_t) * (cLevelLast - cLevel + 1)); const char* pch = strrchr(displayName, '\\'); /* Windows */ if (!pch) pch = strrchr(displayName, '/'); /* Linux */ if (pch) displayName = pch+1; + if(resultarray == NULL) EXM_THROW(12, "Not enough memory"); + returnval.cSpeed = 0; + returnval.dSpeed = 0; + returnval.cSize = 0; if (g_realTime) { - DISPLAYLEVEL(2, "Note : switching to real-time priority \n"); + DISPLAYLEVELP(2, "Note : switching to real-time priority \n"); SET_REALTIME_PRIORITY; } @@ -536,11 +600,13 @@ static void BMK_benchCLevel(const void* srcBuffer, size_t benchedSize, for (l=cLevel; l <= cLevelLast; l++) { if (l==0) continue; /* skip level 0 */ - BMK_benchMem(srcBuffer, benchedSize, - displayName, l, - fileSizes, nbFiles, - dictBuffer, dictBufferSize, compressionParams); + resultarray[l - cLevel] = BMK_benchMemCtxless(srcBuffer, benchedSize, + displayName, l, fileSizes, nbFiles, + dictBuffer, dictBufferSize, compressionParams, printable); } + returnval = BMK_average(resultarray, cLevelLast - cLevel + 1, NULL); + free(resultarray); + return returnval; } @@ -548,8 +614,8 @@ static void BMK_benchCLevel(const void* srcBuffer, size_t benchedSize, * Loads `buffer` with content of files listed within `fileNamesTable`. * At most, fills `buffer` entirely. */ static void BMK_loadFiles(void* buffer, size_t bufferSize, - size_t* fileSizes, - const char* const * const fileNamesTable, unsigned nbFiles) + size_t* fileSizes, const char* const * const fileNamesTable, + unsigned nbFiles, int printable) { size_t pos = 0, totalSize = 0; unsigned n; @@ -557,12 +623,12 @@ static void BMK_loadFiles(void* buffer, size_t bufferSize, FILE* f; U64 fileSize = UTIL_getFileSize(fileNamesTable[n]); if (UTIL_isDirectory(fileNamesTable[n])) { - DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]); + DISPLAYLEVELP(2, "Ignoring %s directory... \n", fileNamesTable[n]); fileSizes[n] = 0; continue; } if (fileSize == UTIL_FILESIZE_UNKNOWN) { - DISPLAYLEVEL(2, "Cannot evaluate size of %s, ignoring ... \n", fileNamesTable[n]); + DISPLAYLEVELP(2, "Cannot evaluate size of %s, ignoring ... \n", fileNamesTable[n]); fileSizes[n] = 0; continue; } @@ -581,16 +647,16 @@ static void BMK_loadFiles(void* buffer, size_t bufferSize, if (totalSize == 0) EXM_THROW(12, "no data to bench"); } -static void BMK_benchFileTable(const char* const * const fileNamesTable, unsigned const nbFiles, - const char* const dictFileName, - int const cLevel, int const cLevelLast, - const ZSTD_compressionParameters* const compressionParams) +static BMK_result_t BMK_benchFileTable(const char* const * const fileNamesTable, unsigned const nbFiles, + const char* const dictFileName, int const cLevel, int const cLevelLast, + const ZSTD_compressionParameters* const compressionParams, int printable) { void* srcBuffer; size_t benchedSize; void* dictBuffer = NULL; size_t dictBufferSize = 0; size_t* const fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t)); + BMK_result_t returnval; U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles); if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes"); @@ -605,7 +671,7 @@ static void BMK_benchFileTable(const char* const * const fileNamesTable, unsigne if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize); - BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1); + BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1, printable); } /* Memory allocation & restrictions */ @@ -617,43 +683,50 @@ static void BMK_benchFileTable(const char* const * const fileNamesTable, unsigne if (!srcBuffer) EXM_THROW(12, "not enough memory"); /* Load input buffer */ - BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles); + BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles, printable); /* Bench */ if (g_separateFiles) { const BYTE* srcPtr = (const BYTE*)srcBuffer; U32 fileNb; + BMK_result_t* resultarray = (BMK_result_t*)malloc(sizeof(BMK_result_t) * nbFiles); + if(resultarray == NULL) EXM_THROW(12, "not enough memory"); for (fileNb=0; fileNb 1) ? mfName : fileNamesTable[0]; - BMK_benchCLevel(srcBuffer, benchedSize, + returnval = BMK_benchCLevel(srcBuffer, benchedSize, displayName, cLevel, cLevelLast, - fileSizes, nbFiles, - dictBuffer, dictBufferSize, compressionParams); + fileSizes, nbFiles, dictBuffer, dictBufferSize, + compressionParams, printable); } } /* clean up */ free(srcBuffer); free(dictBuffer); free(fileSizes); + return returnval; } -static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility, - const ZSTD_compressionParameters* compressionParams) +static BMK_result_t BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility, + const ZSTD_compressionParameters* compressionParams, + int printable) { char name[20] = {0}; size_t benchedSize = 10000000; void* const srcBuffer = malloc(benchedSize); + BMK_result_t returnval; /* Memory allocation */ if (!srcBuffer) EXM_THROW(21, "not enough memory"); @@ -663,17 +736,17 @@ static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility /* Bench */ snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100)); - BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0, compressionParams); + returnval = BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0, compressionParams, printable); /* clean up */ free(srcBuffer); + return returnval; } -int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, - const char* dictFileName, - int cLevel, int cLevelLast, - const ZSTD_compressionParameters* compressionParams) +BMK_result_t BMK_benchFilesFull(const char** fileNamesTable, unsigned nbFiles, + const char* dictFileName, int cLevel, int cLevelLast, + const ZSTD_compressionParameters* compressionParams, int printable) { double const compressibility = (double)g_compressibilityDefault / 100; @@ -681,11 +754,18 @@ int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, if (cLevelLast > ZSTD_maxCLevel()) cLevelLast = ZSTD_maxCLevel(); if (cLevelLast < cLevel) cLevelLast = cLevel; if (cLevelLast > cLevel) - DISPLAYLEVEL(2, "Benchmarking levels from %d to %d\n", cLevel, cLevelLast); + DISPLAYLEVELP(2, "Benchmarking levels from %d to %d\n", cLevel, cLevelLast); if (nbFiles == 0) - BMK_syntheticTest(cLevel, cLevelLast, compressibility, compressionParams); + return BMK_syntheticTest(cLevel, cLevelLast, compressibility, compressionParams, printable); else - BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast, compressionParams); + return BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast, compressionParams, printable); +} + +int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, + const char* dictFileName, + int cLevel, int cLevelLast, + const ZSTD_compressionParameters* compressionParams) { + BMK_benchFilesFull(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast, compressionParams, 1); return 0; } diff --git a/programs/bench.h b/programs/bench.h index bf1087013..3bb725243 100644 --- a/programs/bench.h +++ b/programs/bench.h @@ -16,9 +16,28 @@ #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressionParameters */ #include "zstd.h" /* ZSTD_compressionParameters */ +typedef struct { + size_t cSize; + double cSpeed; /* bytes / sec */ + double dSpeed; +} BMK_result_t; + +/* called in cli */ int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, const char* dictFileName, int cLevel, int cLevelLast, const ZSTD_compressionParameters* compressionParams); +/* more options */ +BMK_result_t BMK_benchFilesFull(const char** fileNamesTable, unsigned nbFiles, const char* dictFileName, + int cLevel, int cLevelLast, const ZSTD_compressionParameters* compressionParams, + int printable); + +/* basic benchmarking function, called in paramgrill + * results, cctx, dctx, dictbuffer can be null or passed in */ +BMK_result_t BMK_benchMem(const void* srcBuffer, size_t srcSize, const char* displayName, int cLevel, + const size_t* fileSizes, unsigned nbFiles, const void* dictBuffer, size_t dictBufferSize, + const ZSTD_compressionParameters* const comprParams, ZSTD_CCtx* ctx, ZSTD_DCtx* dctx, + int printable); + /* Set Parameters */ void BMK_setNbSeconds(unsigned nbLoops); void BMK_setBlockSize(size_t blockSize); diff --git a/tests/Makefile b/tests/Makefile index c4cbe1bdf..602bbe335 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -203,7 +203,7 @@ zstreamtest-dll : $(ZSTREAM_LOCAL_FILES) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) $(LDFLAGS) -o $@$(EXT) paramgrill : DEBUGFLAGS = # turn off assert() for speed measurements -paramgrill : $(ZSTD_FILES) $(PRGDIR)/datagen.c paramgrill.c +paramgrill : $(ZSTD_FILES) $(PRGDIR)/bench.c $(PRGDIR)/datagen.c paramgrill.c $(CC) $(FLAGS) $^ -lm -o $@$(EXT) datagen : $(PRGDIR)/datagen.c datagencli.c diff --git a/tests/paramgrill.c b/tests/paramgrill.c index 44e3cb37b..7fd4513c4 100644 --- a/tests/paramgrill.c +++ b/tests/paramgrill.c @@ -26,6 +26,7 @@ #include "datagen.h" #include "xxhash.h" #include "util.h" +#include "bench.h" /*-************************************ @@ -48,12 +49,9 @@ static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t #define COMPRESSIBILITY_DEFAULT 0.50 -static const double g_grillDuration_s = 99999; /* about 27 hours */ -static const U64 g_maxParamTime = 15 * SEC_TO_MICRO; static const U64 g_maxVariationTime = 60 * SEC_TO_MICRO; static const int g_maxNbVariations = 64; - /*-************************************ * Macros **************************************/ @@ -63,11 +61,13 @@ static const int g_maxNbVariations = 64; #undef MAX #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) #define MAX(a,b) ( (a) > (b) ? (a) : (b) ) - +#define CUSTOM_LEVEL 99 /*-************************************ * Benchmark Parameters **************************************/ + +static double g_grillDuration_s = 99999; /* about 27 hours */ static U32 g_nbIterations = NBLOOPS; static double g_compressibility = COMPRESSIBILITY_DEFAULT; static U32 g_blockSize = 0; @@ -83,7 +83,6 @@ void BMK_SetNbIterations(int nbLoops) DISPLAY("- %u iterations -\n", g_nbIterations); } - /*-******************************************************* * Private functions *********************************************************/ @@ -144,11 +143,6 @@ static unsigned longCommandWArg(const char** stringPtr, const char* longCommand) /*-******************************************************* * Bench functions *********************************************************/ -typedef struct { - size_t cSize; - double cSpeed; /* bytes / sec */ - double dSpeed; -} BMK_result_t; typedef struct { @@ -162,176 +156,39 @@ typedef struct } blockParam_t; -static size_t -BMK_benchParam(BMK_result_t* resultPtr, - const void* srcBuffer, size_t srcSize, - ZSTD_CCtx* ctx, - const ZSTD_compressionParameters cParams) -{ - const size_t blockSize = g_blockSize ? g_blockSize : srcSize; - const U32 nbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize); - blockParam_t* const blockTable = (blockParam_t*) malloc(nbBlocks * sizeof(blockParam_t)); - const size_t maxCompressedSize = (size_t)nbBlocks * ZSTD_compressBound(blockSize); - void* const compressedBuffer = malloc(maxCompressedSize); - void* const resultBuffer = malloc(srcSize); - ZSTD_parameters params; - U32 Wlog = cParams.windowLog; - U32 Clog = cParams.chainLog; - U32 Hlog = cParams.hashLog; - U32 Slog = cParams.searchLog; - U32 Slength = cParams.searchLength; - U32 Tlength = cParams.targetLength; - ZSTD_strategy strat = cParams.strategy; - char name[30] = { 0 }; - U64 crcOrig; - - /* init result for early exit */ - resultPtr->cSize = srcSize; - resultPtr->cSpeed = 0.; - resultPtr->dSpeed = 0.; - - /* Memory allocation & restrictions */ - snprintf(name, 30, "Sw%02uc%02uh%02us%02ul%1ut%03uS%1u", Wlog, Clog, Hlog, Slog, Slength, Tlength, strat); - if (!compressedBuffer || !resultBuffer || !blockTable) { - DISPLAY("\nError: not enough memory!\n"); - free(compressedBuffer); - free(resultBuffer); - free(blockTable); - return 12; - } - - /* Calculating input Checksum */ - crcOrig = XXH64(srcBuffer, srcSize, 0); - - /* Init blockTable data */ - { U32 i; - size_t remaining = srcSize; - const char* srcPtr = (const char*)srcBuffer; - char* cPtr = (char*)compressedBuffer; - char* resPtr = (char*)resultBuffer; - for (i=0; i g_maxParamTime) break; } - - /* Compression */ - DISPLAY("\r%1u-%s : %9u ->", loopNb, name, (U32)srcSize); - memset(compressedBuffer, 0xE5, maxCompressedSize); - - nbLoops = 0; - 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.); - resultPtr->cSize = cSize; - resultPtr->cSpeed = (double)srcSize / fastestC; - -#if 1 - /* Decompression */ - memset(resultBuffer, 0xD6, srcSize); - - nbLoops = 0; - 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.); - DISPLAY("%7.1f MB/s", (double)srcSize / fastestD / 1000000.); - resultPtr->dSpeed = (double)srcSize / fastestD; - - /* CRC Checking */ - { U64 const crcCheck = XXH64(resultBuffer, srcSize, 0); - if (crcOrig!=crcCheck) { - unsigned u; - unsigned eBlockSize = (unsigned)(MIN(65536*2, blockSize)); - DISPLAY("\n!!! WARNING !!! Invalid Checksum : %x != %x\n", (unsigned)crcOrig, (unsigned)crcCheck); - for (u=0; u search more */ BMK_printWinners(f, winners, srcSize); - playAround(f, winners, p, srcBuffer, srcSize, ctx); + playAround(f, winners, p, srcBuffer, srcSize, ctx, dctx); } } @@ -608,29 +465,30 @@ static ZSTD_compressionParameters randomParams(void) static void BMK_selectRandomStart( FILE* f, winnerInfo_t* winners, const void* srcBuffer, size_t srcSize, - ZSTD_CCtx* ctx) + ZSTD_CCtx* ctx, ZSTD_DCtx* dctx) { U32 const id = FUZ_rand(&g_rand) % (NB_LEVELS_TRACKED+1); if ((id==0) || (winners[id].params.windowLog==0)) { /* use some random entry */ ZSTD_compressionParameters const p = ZSTD_adjustCParams(randomParams(), srcSize, 0); - playAround(f, winners, p, srcBuffer, srcSize, ctx); + playAround(f, winners, p, srcBuffer, srcSize, ctx, dctx); } else { - playAround(f, winners, winners[id].params, srcBuffer, srcSize, ctx); + playAround(f, winners, winners[id].params, srcBuffer, srcSize, ctx, dctx); } } -static void BMK_benchOnce(ZSTD_CCtx* cctx, const void* srcBuffer, size_t srcSize) +static void BMK_benchOnce(ZSTD_CCtx* cctx, ZSTD_DCtx* dctx, const void* srcBuffer, size_t srcSize) { BMK_result_t testResult; g_params = ZSTD_adjustCParams(g_params, srcSize, 0); - BMK_benchParam(&testResult, srcBuffer, srcSize, cctx, g_params); - DISPLAY("\n"); + BMK_benchParam(&testResult, srcBuffer, srcSize, cctx, dctx, g_params); + DISPLAY("Compression Ratio: %.3f Compress Speed: %.1f MB/s Decompress Speed: %.1f MB/s\n", (double)srcSize / testResult.cSize, + testResult.cSpeed / 1000000, testResult.dSpeed / 1000000); return; } -static void BMK_benchFullTable(ZSTD_CCtx* cctx, const void* srcBuffer, size_t srcSize) +static void BMK_benchFullTable(ZSTD_CCtx* cctx, ZSTD_DCtx* dctx, const void* srcBuffer, size_t srcSize) { ZSTD_compressionParameters params; winnerInfo_t winners[NB_LEVELS_TRACKED+1]; @@ -649,7 +507,7 @@ static void BMK_benchFullTable(ZSTD_CCtx* cctx, const void* srcBuffer, size_t sr /* baseline config for level 1 */ ZSTD_compressionParameters const l1params = ZSTD_getCParams(1, blockSize, 0); BMK_result_t testResult; - BMK_benchParam(&testResult, srcBuffer, srcSize, cctx, l1params); + BMK_benchParam(&testResult, srcBuffer, srcSize, cctx, dctx, l1params); BMK_init_level_constraints((int)((testResult.cSpeed * 31) / 32)); } @@ -658,14 +516,14 @@ static void BMK_benchFullTable(ZSTD_CCtx* cctx, const void* srcBuffer, size_t sr int i; for (i=0; i<=maxSeeds; i++) { params = ZSTD_getCParams(i, blockSize, 0); - BMK_seed(winners, params, srcBuffer, srcSize, cctx); + BMK_seed(winners, params, srcBuffer, srcSize, cctx, dctx); } } BMK_printWinners(f, winners, srcSize); /* start tests */ { const time_t grillStart = time(NULL); do { - BMK_selectRandomStart(f, winners, srcBuffer, srcSize, cctx); + BMK_selectRandomStart(f, winners, srcBuffer, srcSize, cctx, dctx); } while (BMK_timeSpan(grillStart) < g_grillDuration_s); } @@ -677,19 +535,20 @@ static void BMK_benchFullTable(ZSTD_CCtx* cctx, const void* srcBuffer, size_t sr fclose(f); } -static void BMK_benchMem_usingCCtx(ZSTD_CCtx* cctx, const void* srcBuffer, size_t srcSize) +static void BMK_benchMem_usingCCtx(ZSTD_CCtx* const cctx, ZSTD_DCtx* const dctx, const void* srcBuffer, size_t srcSize) { if (g_singleRun) - return BMK_benchOnce(cctx, srcBuffer, srcSize); + return BMK_benchOnce(cctx, dctx, srcBuffer, srcSize); else - return BMK_benchFullTable(cctx, srcBuffer, srcSize); + return BMK_benchFullTable(cctx, dctx, srcBuffer, srcSize); } -static void BMK_benchMem(const void* srcBuffer, size_t srcSize) +static void BMK_benchMemCCtxInit(const void* srcBuffer, size_t srcSize) { ZSTD_CCtx* const cctx = ZSTD_createCCtx(); - if (cctx==NULL) { DISPLAY("ZSTD_createCCtx() failed \n"); exit(1); } - BMK_benchMem_usingCCtx(cctx, srcBuffer, srcSize); + ZSTD_DCtx* const dctx = ZSTD_createDCtx(); + if (cctx==NULL || dctx==NULL) { DISPLAY("Context Creation failed \n"); exit(1); } + BMK_benchMem_usingCCtx(cctx, dctx, srcBuffer, srcSize); ZSTD_freeCCtx(cctx); } @@ -708,7 +567,7 @@ static int benchSample(void) /* bench */ DISPLAY("\r%79s\r", ""); DISPLAY("using %s %i%%: \n", name, (int)(g_compressibility*100)); - BMK_benchMem(origBuff, benchedSize); + BMK_benchMemCCtxInit(origBuff, benchedSize); free(origBuff); return 0; @@ -766,7 +625,7 @@ int benchFiles(const char** fileNamesTable, int nbFiles) /* bench */ DISPLAY("\r%79s\r", ""); DISPLAY("using %s : \n", inFileName); - BMK_benchMem(origBuff, benchedSize); + BMK_benchMemCCtxInit(origBuff, benchedSize); /* clean */ free(origBuff); @@ -790,7 +649,6 @@ int optimizeForSize(const char* inFileName, U32 targetSpeed) U64 const inFileSize = UTIL_getFileSize(inFileName); size_t benchedSize = BMK_findMaxMem(inFileSize*3) / 3; void* origBuff; - /* Init */ if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; } if (inFileSize == UTIL_FILESIZE_UNKNOWN) { @@ -829,8 +687,8 @@ int optimizeForSize(const char* inFileName, U32 targetSpeed) DISPLAY("\r%79s\r", ""); DISPLAY("optimizing for %s - limit speed %u MB/s \n", inFileName, targetSpeed); targetSpeed *= 1000000; - { ZSTD_CCtx* const ctx = ZSTD_createCCtx(); + ZSTD_DCtx* const dctx = ZSTD_createDCtx(); winnerInfo_t winner; BMK_result_t candidate; const size_t blockSize = g_blockSize ? g_blockSize : benchedSize; @@ -845,9 +703,10 @@ int optimizeForSize(const char* inFileName, U32 targetSpeed) int i; for (i=1; i<=maxSeeds; i++) { ZSTD_compressionParameters const CParams = ZSTD_getCParams(i, blockSize, 0); - BMK_benchParam(&candidate, origBuff, benchedSize, ctx, CParams); - if (candidate.cSpeed < targetSpeed) + BMK_benchParam(&candidate, origBuff, benchedSize, ctx, dctx, CParams); + if (candidate.cSpeed < (double)targetSpeed) { break; + } if ( (candidate.cSize < winner.result.cSize) | ((candidate.cSize == winner.result.cSize) & (candidate.cSpeed > winner.result.cSpeed)) ) { @@ -856,7 +715,9 @@ int optimizeForSize(const char* inFileName, U32 targetSpeed) BMK_printWinner(stdout, i, winner.result, winner.params, benchedSize); } } } - BMK_printWinner(stdout, 99, winner.result, winner.params, benchedSize); + + BMK_printWinner(stdout, CUSTOM_LEVEL, winner.result, winner.params, benchedSize); + BMK_translateAdvancedParams(winner.params); /* start tests */ @@ -872,7 +733,7 @@ int optimizeForSize(const char* inFileName, U32 targetSpeed) /* test */ NB_TESTS_PLAYED(params)++; - BMK_benchParam(&candidate, origBuff, benchedSize, ctx, params); + BMK_benchParam(&candidate, origBuff, benchedSize, ctx, dctx, params); /* improvement found => new winner */ if ( (candidate.cSpeed > targetSpeed) @@ -881,19 +742,20 @@ int optimizeForSize(const char* inFileName, U32 targetSpeed) { winner.params = params; winner.result = candidate; - BMK_printWinner(stdout, 99, winner.result, winner.params, benchedSize); + BMK_printWinner(stdout, CUSTOM_LEVEL, winner.result, winner.params, benchedSize); BMK_translateAdvancedParams(winner.params); } } while (BMK_timeSpan(grillStart) < g_grillDuration_s); } - /* end summary */ - BMK_printWinner(stdout, 99, winner.result, winner.params, benchedSize); + + BMK_printWinner(stdout, CUSTOM_LEVEL, winner.result, winner.params, benchedSize); BMK_translateAdvancedParams(winner.params); DISPLAY("grillParams size - optimizer completed \n"); /* clean up*/ ZSTD_freeCCtx(ctx); + ZSTD_freeDCtx(dctx); } free(origBuff); @@ -954,6 +816,8 @@ static int usage_advanced(void) DISPLAY( " -S : Single run \n"); DISPLAY( " --zstd : Single run, parameter selection same as zstdcli \n"); DISPLAY( " -P# : generated sample compressibility (default : %.1f%%) \n", COMPRESSIBILITY_DEFAULT * 100); + DISPLAY( " -t# : Caps runtime of operation in seconds (default : %u seconds (%.1f hours)) \n", (U32)g_grillDuration_s, g_grillDuration_s / 3600); + DISPLAY( " -v : Prints Benchmarking output\n"); return 0; } @@ -1007,7 +871,7 @@ int main(int argc, const char** argv) DISPLAY("invalid --zstd= format\n"); return 1; /* check the end of string */ } - //if not return, success + /* if not return, success */ } else if (argument[0]=='-') { argument++; @@ -1021,8 +885,8 @@ int main(int argc, const char** argv) /* Pause at the end (hidden option) */ case 'p': main_pause = 1; argument++; break; - /* Modify Nb Iterations */ + case 'i': argument++; g_nbIterations = readU32FromChar(&argument); @@ -1103,6 +967,12 @@ int main(int argc, const char** argv) DISPLAY("using %u KB block size \n", g_blockSize>>10); break; + /* caps runtime (in seconds) */ + case 't': + argument++; + g_grillDuration_s = (double)readU32FromChar(&argument); + break; + /* Unknown command */ default : return badusage(exename); } diff --git a/tests/playTests.sh b/tests/playTests.sh index 48001c2eb..2a6e13a05 100755 --- a/tests/playTests.sh +++ b/tests/playTests.sh @@ -186,6 +186,7 @@ $ZSTD -f tmp && die "tmp not present : should have failed" test ! -f tmp.zst # tmp.zst should not be created $ECHO "test : compress multiple files" +rm tmp* $ECHO hello > tmp1 $ECHO world > tmp2 $ZSTD tmp1 tmp2 -o "$INTOVOID" From 01d940b670a47bfa3a197f672e4cacea03dcc02a Mon Sep 17 00:00:00 2001 From: George Lu Date: Mon, 11 Jun 2018 10:59:05 -0400 Subject: [PATCH 2/2] Requested changes -Remove g_displaylevel/setNotificationLevel function -Add extern "C" -Remove averaging -Reorder arguments More fixes -Added BMK_return_t (result + possible error) -Correct comment' -Nullcheck ctx, dctx when allocated -Remove extra assert --- programs/bench.c | 229 ++++++++++++++++++++------------------------- programs/bench.h | 33 ++++--- programs/zstdcli.c | 3 +- tests/paramgrill.c | 6 +- 4 files changed, 126 insertions(+), 145 deletions(-) diff --git a/programs/bench.c b/programs/bench.c index 6a81cc8f2..09697d1fe 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -71,17 +71,16 @@ static U32 g_compressibilityDefault = 50; * console display ***************************************/ #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) -#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } -#define DISPLAYLEVELP(l, ...) if (g_displayLevel>=l && printable) { DISPLAY(__VA_ARGS__); } -static int g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ +#define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); } +/* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ 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)) \ +#define DISPLAYUPDATE(l, ...) { if (displayLevel>=l) { \ + if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (displayLevel>=4)) \ { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \ - if (g_displayLevel>=4) fflush(stderr); } } } + if (displayLevel>=4) fflush(stderr); } } } /* ************************************* @@ -93,9 +92,9 @@ static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; #define DEBUGOUTPUT(...) { if (DEBUG) DISPLAY(__VA_ARGS__); } #define EXM_THROW(error, ...) { \ DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \ - DISPLAYLEVELP(1, "Error %i : ", error); \ - DISPLAYLEVELP(1, __VA_ARGS__); \ - DISPLAYLEVELP(1, " \n"); \ + DISPLAYLEVEL(1, "Error %i : ", error); \ + DISPLAYLEVEL(1, __VA_ARGS__); \ + DISPLAYLEVEL(1, " \n"); \ exit(error); \ } @@ -106,30 +105,34 @@ static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; static int g_additionalParam = 0; static U32 g_decodeOnly = 0; -void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; } - void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; } + +//TODO : Deal with DISPLAYLEVEL for all these set functions + static U32 g_nbSeconds = BMK_TIMETEST_DEFAULT_S; + void BMK_setNbSeconds(unsigned nbSeconds) { g_nbSeconds = nbSeconds; - DISPLAYLEVEL(3, "- test >= %u seconds per compression / decompression - \n", g_nbSeconds); + DISPLAY("- test >= %u seconds per compression / decompression - \n", g_nbSeconds); } static size_t g_blockSize = 0; + void BMK_setBlockSize(size_t blockSize) { g_blockSize = blockSize; - if (g_blockSize) DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10)); + if (g_blockSize) DISPLAY("using blocks of size %u KB \n", (U32)(blockSize>>10)); } void BMK_setDecodeOnlyMode(unsigned decodeFlag) { g_decodeOnly = (decodeFlag>0); } static U32 g_nbWorkers = 0; + void BMK_setNbWorkers(unsigned nbWorkers) { #ifndef ZSTD_MULTITHREAD - if (nbWorkers > 0) DISPLAYLEVEL(2, "Note : multi-threading is disabled \n"); + if (nbWorkers > 0) DISPLAY("Note : multi-threading is disabled \n"); #endif g_nbWorkers = nbWorkers; } @@ -188,12 +191,13 @@ typedef struct { #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MAX(a,b) ((a) > (b) ? (a) : (b)) -BMK_result_t BMK_benchMem(const void* srcBuffer, size_t srcSize, - const char* displayName, int cLevel, - const size_t* fileSizes, U32 nbFiles, +BMK_return_t BMK_benchMem(const void* srcBuffer, size_t srcSize, + const size_t* fileSizes, unsigned nbFiles, + const int cLevel, const ZSTD_compressionParameters* comprParams, const void* dictBuffer, size_t dictBufferSize, - const ZSTD_compressionParameters* const comprParams, - ZSTD_CCtx* ctx, ZSTD_DCtx* dctx, int printable) + ZSTD_CCtx* ctx, ZSTD_DCtx* dctx, + int displayLevel, const char* displayName) + { size_t const blockSize = ((g_blockSize>=32 && !g_decodeOnly) ? g_blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ; U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles; @@ -201,20 +205,20 @@ BMK_result_t BMK_benchMem(const void* srcBuffer, size_t srcSize, size_t const maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */ void* const compressedBuffer = malloc(maxCompressedSize); void* resultBuffer = malloc(srcSize); - BMK_result_t results; + BMK_return_t results; size_t const loadedCompressedSize = srcSize; size_t cSize = 0; double ratio = 0.; U32 nbBlocks; - assert(ctx != NULL); - assert(dctx != NULL); - /* checks */ - if (!compressedBuffer || !resultBuffer || !blockTable || !ctx || !dctx) + if (!compressedBuffer || !resultBuffer || !blockTable) EXM_THROW(31, "allocation error : not enough memory"); + if(!ctx || !dctx) + EXM_THROW(31, "error: passed in null context"); + /* init */ if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* display last 17 characters */ if (g_nbWorkers==1) g_nbWorkers=0; /* prefer synchronous mode */ @@ -284,19 +288,19 @@ BMK_result_t BMK_benchMem(const void* srcBuffer, size_t srcSize, U32 markNb = 0; coolTime = UTIL_getTime(); - DISPLAYLEVELP(2, "\r%79s\r", ""); + DISPLAYLEVEL(2, "\r%79s\r", ""); while (!cCompleted || !dCompleted) { /* overheat protection */ if (UTIL_clockSpanMicro(coolTime) > ACTIVEPERIOD_MICROSEC) { - DISPLAYLEVELP(2, "\rcooling down ... \r"); + DISPLAYLEVEL(2, "\rcooling down ... \r"); UTIL_sleep(COOLPERIOD_SEC); coolTime = UTIL_getTime(); } if (!g_decodeOnly) { /* Compression */ - DISPLAYLEVELP(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 */ UTIL_sleepMilli(5); /* give processor time to other processes */ @@ -372,13 +376,13 @@ BMK_result_t BMK_benchMem(const void* srcBuffer, size_t srcSize, cSize = 0; { U32 blockNb; for (blockNb=0; blockNb%10u (%5.*f),%6.*f MB/s\r", + results.result.cSpeed = compressionSpeed * 1000000; + DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.*f),%6.*f MB/s\r", marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratioAccuracy, ratio, cSpeedAccuracy, compressionSpeed ); @@ -433,9 +437,9 @@ BMK_result_t BMK_benchMem(const void* srcBuffer, size_t srcSize, double const compressionSpeed = ((double)srcSize / fastestC) * 1000; int const cSpeedAccuracy = (compressionSpeed < 10.) ? 2 : 1; double const decompressionSpeed = ((double)srcSize / fastestD) * 1000; - results.cSpeed = compressionSpeed * 1000000; - results.dSpeed = decompressionSpeed * 1000000; - DISPLAYLEVELP(2, "%2s-%-17.17s :%10u ->%10u (%5.*f),%6.*f MB/s ,%6.1f MB/s \r", + results.result.cSpeed = compressionSpeed * 1000000; + results.result.dSpeed = decompressionSpeed * 1000000; + DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.*f),%6.*f MB/s ,%6.1f MB/s \r", marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratioAccuracy, ratio, cSpeedAccuracy, compressionSpeed, @@ -482,7 +486,7 @@ BMK_result_t BMK_benchMem(const void* srcBuffer, size_t srcSize, #endif } /* for (testNb = 1; testNb <= (g_nbSeconds + !g_nbSeconds); testNb++) */ - if (g_displayLevel == 1) { /* hidden display mode -q, used by python speed benchmark */ + if (displayLevel == 1) { /* hidden display mode -q, used by python speed benchmark */ double const cSpeed = ((double)srcSize / fastestC) * 1000; double const dSpeed = ((double)srcSize / fastestD) * 1000; if (g_additionalParam) @@ -490,66 +494,36 @@ BMK_result_t BMK_benchMem(const void* srcBuffer, size_t srcSize, else DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName); } - DISPLAYLEVELP(2, "%2i#\n", cLevel); + DISPLAYLEVEL(2, "%2i#\n", cLevel); } /* Bench */ /* clean up */ free(blockTable); free(compressedBuffer); free(resultBuffer); + results.errorCode = 0; return results; } -/* - * combines multiple results into single - * currently normal averaging - * consider weighing by file size? or maybe by total time? - * size is cumulative though - */ -static BMK_result_t BMK_average(BMK_result_t* results, const int count, const size_t* sampleSizes) { - double cSpeedTotal = 0; - double dSpeedTotal = 0; - size_t cSizeTotal = 0; - BMK_result_t returnval; - int i; - if (sampleSizes == NULL) { - for(i = 0; i < count; i++) { - cSpeedTotal += results[i].cSpeed; - dSpeedTotal += results[i].dSpeed; - cSizeTotal += results[i].cSize; - } - returnval.cSpeed = cSpeedTotal / count; - returnval.dSpeed = dSpeedTotal / count; - returnval.cSize = cSizeTotal; - } else { - size_t sSizeTotal = 0; - /* in this case, SpeedTotal will keep track of total time used */ - for(i = 0; i < count; i++) { - cSpeedTotal += sampleSizes[i] / results[i].cSpeed; - dSpeedTotal += sampleSizes[i] / results[i].cSpeed; - cSizeTotal += results[i].cSize; - sSizeTotal += sampleSizes[i]; - } - returnval.cSpeed = (double)sSizeTotal / cSpeedTotal; - returnval.dSpeed = (double)sSizeTotal / dSpeedTotal; - returnval.cSize = cSizeTotal; - } - return returnval; -} - -static BMK_result_t BMK_benchMemCtxless(const void* srcBuffer, size_t srcSize, - const char* displayName, int cLevel, - const size_t* fileSizes, U32 nbFiles, +static void BMK_benchMemCtxless(const void* srcBuffer, size_t srcSize, + const size_t* fileSizes, unsigned nbFiles, + int cLevel, const ZSTD_compressionParameters* const comprParams, const void* dictBuffer, size_t dictBufferSize, - const ZSTD_compressionParameters* const comprParams, int printable) + int displayLevel, const char* displayName) { - BMK_result_t returnval; ZSTD_CCtx* ctx = ZSTD_createCCtx(); ZSTD_DCtx* dctx = ZSTD_createDCtx(); - returnval = BMK_benchMem(srcBuffer, srcSize, displayName, cLevel, fileSizes, nbFiles, dictBuffer, dictBufferSize, comprParams, ctx, dctx, printable); + if(ctx == NULL || dctx == NULL) { + EXM_THROW(12, "not enough memory for contexts"); + } + BMK_benchMem(srcBuffer, srcSize, + fileSizes, nbFiles, + cLevel, comprParams, + dictBuffer, dictBufferSize, + ctx, dctx, + displayLevel, displayName); ZSTD_freeCCtx(ctx); ZSTD_freeDCtx(dctx); - return returnval; } static size_t BMK_findMaxMem(U64 requiredMem) @@ -571,42 +545,36 @@ static size_t BMK_findMaxMem(U64 requiredMem) } /* returns average stats over all range [cLevel, cLevelLast] */ -static BMK_result_t BMK_benchCLevel(const void* srcBuffer, size_t benchedSize, - const char* displayName, int cLevel, int cLevelLast, +static void BMK_benchCLevel(const void* srcBuffer, size_t benchedSize, const size_t* fileSizes, unsigned nbFiles, + const int cLevel, const int cLevelLast, const ZSTD_compressionParameters* comprParams, const void* dictBuffer, size_t dictBufferSize, - const ZSTD_compressionParameters* const compressionParams, - int printable) + int displayLevel, const char* displayName) { int l; - BMK_result_t returnval; - BMK_result_t* resultarray = (BMK_result_t*)malloc(sizeof(BMK_result_t) * (cLevelLast - cLevel + 1)); const char* pch = strrchr(displayName, '\\'); /* Windows */ if (!pch) pch = strrchr(displayName, '/'); /* Linux */ if (pch) displayName = pch+1; - if(resultarray == NULL) EXM_THROW(12, "Not enough memory"); - returnval.cSpeed = 0; - returnval.dSpeed = 0; - returnval.cSize = 0; if (g_realTime) { - DISPLAYLEVELP(2, "Note : switching to real-time priority \n"); + DISPLAYLEVEL(2, "Note : switching to real-time priority \n"); SET_REALTIME_PRIORITY; } - if (g_displayLevel == 1 && !g_additionalParam) + if (displayLevel == 1 && !g_additionalParam) DISPLAY("bench %s %s: input %u bytes, %u seconds, %u KB blocks\n", ZSTD_VERSION_STRING, ZSTD_GIT_COMMIT_STRING, (U32)benchedSize, g_nbSeconds, (U32)(g_blockSize>>10)); for (l=cLevel; l <= cLevelLast; l++) { if (l==0) continue; /* skip level 0 */ - resultarray[l - cLevel] = BMK_benchMemCtxless(srcBuffer, benchedSize, - displayName, l, fileSizes, nbFiles, - dictBuffer, dictBufferSize, compressionParams, printable); + BMK_benchMemCtxless(srcBuffer, benchedSize, + fileSizes, nbFiles, + l, comprParams, + dictBuffer, dictBufferSize, + displayLevel, displayName); } - returnval = BMK_average(resultarray, cLevelLast - cLevel + 1, NULL); - free(resultarray); - return returnval; + + return; } @@ -615,7 +583,7 @@ static BMK_result_t BMK_benchCLevel(const void* srcBuffer, size_t benchedSize, * At most, fills `buffer` entirely. */ static void BMK_loadFiles(void* buffer, size_t bufferSize, size_t* fileSizes, const char* const * const fileNamesTable, - unsigned nbFiles, int printable) + unsigned nbFiles, int displayLevel) { size_t pos = 0, totalSize = 0; unsigned n; @@ -623,12 +591,12 @@ static void BMK_loadFiles(void* buffer, size_t bufferSize, FILE* f; U64 fileSize = UTIL_getFileSize(fileNamesTable[n]); if (UTIL_isDirectory(fileNamesTable[n])) { - DISPLAYLEVELP(2, "Ignoring %s directory... \n", fileNamesTable[n]); + DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]); fileSizes[n] = 0; continue; } if (fileSize == UTIL_FILESIZE_UNKNOWN) { - DISPLAYLEVELP(2, "Cannot evaluate size of %s, ignoring ... \n", fileNamesTable[n]); + DISPLAYLEVEL(2, "Cannot evaluate size of %s, ignoring ... \n", fileNamesTable[n]); fileSizes[n] = 0; continue; } @@ -647,16 +615,15 @@ static void BMK_loadFiles(void* buffer, size_t bufferSize, if (totalSize == 0) EXM_THROW(12, "no data to bench"); } -static BMK_result_t BMK_benchFileTable(const char* const * const fileNamesTable, unsigned const nbFiles, +static void BMK_benchFileTable(const char* const * const fileNamesTable, unsigned const nbFiles, const char* const dictFileName, int const cLevel, int const cLevelLast, - const ZSTD_compressionParameters* const compressionParams, int printable) + const ZSTD_compressionParameters* const compressionParams, int displayLevel) { void* srcBuffer; size_t benchedSize; void* dictBuffer = NULL; size_t dictBufferSize = 0; size_t* const fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t)); - BMK_result_t returnval; U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles); if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes"); @@ -671,7 +638,7 @@ static BMK_result_t BMK_benchFileTable(const char* const * const fileNamesTable, if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize); - BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1, printable); + BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1, displayLevel); } /* Memory allocation & restrictions */ @@ -683,7 +650,7 @@ static BMK_result_t BMK_benchFileTable(const char* const * const fileNamesTable, if (!srcBuffer) EXM_THROW(12, "not enough memory"); /* Load input buffer */ - BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles, printable); + BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles, displayLevel); /* Bench */ if (g_separateFiles) { @@ -693,40 +660,39 @@ static BMK_result_t BMK_benchFileTable(const char* const * const fileNamesTable, if(resultarray == NULL) EXM_THROW(12, "not enough memory"); for (fileNb=0; fileNb 1) ? mfName : fileNamesTable[0]; - returnval = BMK_benchCLevel(srcBuffer, benchedSize, - displayName, cLevel, cLevelLast, - fileSizes, nbFiles, dictBuffer, dictBufferSize, - compressionParams, printable); + BMK_benchCLevel(srcBuffer, benchedSize, + fileSizes, nbFiles, + cLevel, cLevelLast, compressionParams, + dictBuffer, dictBufferSize, + displayLevel, displayName); } } /* clean up */ free(srcBuffer); free(dictBuffer); free(fileSizes); - return returnval; } -static BMK_result_t BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility, +static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility, const ZSTD_compressionParameters* compressionParams, - int printable) + int displayLevel) { char name[20] = {0}; size_t benchedSize = 10000000; void* const srcBuffer = malloc(benchedSize); - BMK_result_t returnval; /* Memory allocation */ if (!srcBuffer) EXM_THROW(21, "not enough memory"); @@ -736,17 +702,21 @@ static BMK_result_t BMK_syntheticTest(int cLevel, int cLevelLast, double compres /* Bench */ snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100)); - returnval = BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0, compressionParams, printable); + BMK_benchCLevel(srcBuffer, benchedSize, + &benchedSize, 1, + cLevel, cLevelLast, compressionParams, + NULL, 0, + displayLevel, name); /* clean up */ free(srcBuffer); - return returnval; } -BMK_result_t BMK_benchFilesFull(const char** fileNamesTable, unsigned nbFiles, - const char* dictFileName, int cLevel, int cLevelLast, - const ZSTD_compressionParameters* compressionParams, int printable) +static void BMK_benchFilesFull(const char** fileNamesTable, unsigned nbFiles, + const char* dictFileName, + int cLevel, int cLevelLast, + const ZSTD_compressionParameters* compressionParams, int displayLevel) { double const compressibility = (double)g_compressibilityDefault / 100; @@ -754,18 +724,19 @@ BMK_result_t BMK_benchFilesFull(const char** fileNamesTable, unsigned nbFiles, if (cLevelLast > ZSTD_maxCLevel()) cLevelLast = ZSTD_maxCLevel(); if (cLevelLast < cLevel) cLevelLast = cLevel; if (cLevelLast > cLevel) - DISPLAYLEVELP(2, "Benchmarking levels from %d to %d\n", cLevel, cLevelLast); + DISPLAYLEVEL(2, "Benchmarking levels from %d to %d\n", cLevel, cLevelLast); if (nbFiles == 0) - return BMK_syntheticTest(cLevel, cLevelLast, compressibility, compressionParams, printable); + BMK_syntheticTest(cLevel, cLevelLast, compressibility, compressionParams, displayLevel); else - return BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast, compressionParams, printable); + BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast, compressionParams, displayLevel); } int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, const char* dictFileName, int cLevel, int cLevelLast, - const ZSTD_compressionParameters* compressionParams) { - BMK_benchFilesFull(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast, compressionParams, 1); + const ZSTD_compressionParameters* compressionParams, + int displayLevel) { + BMK_benchFilesFull(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast, compressionParams, displayLevel); return 0; } diff --git a/programs/bench.h b/programs/bench.h index 3bb725243..0ba6f8985 100644 --- a/programs/bench.h +++ b/programs/bench.h @@ -8,6 +8,9 @@ * You may select, at your option, one of the above-listed licenses. */ +#if defined (__cplusplus) +extern "C" { +#endif #ifndef BENCH_H_121279284357 #define BENCH_H_121279284357 @@ -22,21 +25,25 @@ typedef struct { double dSpeed; } BMK_result_t; +/* 0 = no Error */ +typedef struct { + int errorCode; + BMK_result_t result; +} BMK_return_t; + /* called in cli */ int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, const char* dictFileName, - int cLevel, int cLevelLast, const ZSTD_compressionParameters* compressionParams); - -/* more options */ -BMK_result_t BMK_benchFilesFull(const char** fileNamesTable, unsigned nbFiles, const char* dictFileName, - int cLevel, int cLevelLast, const ZSTD_compressionParameters* compressionParams, - int printable); + int cLevel, int cLevelLast, const ZSTD_compressionParameters* compressionParams, + int displayLevel); /* basic benchmarking function, called in paramgrill - * results, cctx, dctx, dictbuffer can be null or passed in */ -BMK_result_t BMK_benchMem(const void* srcBuffer, size_t srcSize, const char* displayName, int cLevel, - const size_t* fileSizes, unsigned nbFiles, const void* dictBuffer, size_t dictBufferSize, - const ZSTD_compressionParameters* const comprParams, ZSTD_CCtx* ctx, ZSTD_DCtx* dctx, - int printable); + * ctx, dctx must be valid */ +BMK_return_t BMK_benchMem(const void* srcBuffer, size_t srcSize, + const size_t* fileSizes, unsigned nbFiles, + const int cLevel, const ZSTD_compressionParameters* comprParams, + const void* dictBuffer, size_t dictBufferSize, + ZSTD_CCtx* ctx, ZSTD_DCtx* dctx, + int displayLevel, const char* displayName); /* Set Parameters */ void BMK_setNbSeconds(unsigned nbLoops); @@ -54,3 +61,7 @@ void BMK_setLdmBucketSizeLog(unsigned ldmBucketSizeLog); void BMK_setLdmHashEveryLog(unsigned ldmHashEveryLog); #endif /* BENCH_H_121279284357 */ + +#if defined (__cplusplus) +} +#endif diff --git a/programs/zstdcli.c b/programs/zstdcli.c index 0fa7ef325..6b6a93528 100644 --- a/programs/zstdcli.c +++ b/programs/zstdcli.c @@ -801,7 +801,6 @@ int main(int argCount, const char* argv[]) /* Check if benchmark is selected */ if (operation==zom_bench) { #ifndef ZSTD_NOBENCH - BMK_setNotificationLevel(g_displayLevel); BMK_setSeparateFiles(separateFiles); BMK_setBlockSize(blockSize); BMK_setNbWorkers(nbWorkers); @@ -816,7 +815,7 @@ int main(int argCount, const char* argv[]) if (g_ldmHashEveryLog != LDM_PARAM_DEFAULT) { BMK_setLdmHashEveryLog(g_ldmHashEveryLog); } - BMK_benchFiles(filenameTable, filenameIdx, dictFileName, cLevel, cLevelLast, &compressionParams); + BMK_benchFiles(filenameTable, filenameIdx, dictFileName, cLevel, cLevelLast, &compressionParams, g_displayLevel); #else (void)bench_nbSeconds; (void)blockSize; (void)setRealTimePrio; (void)separateFiles; #endif diff --git a/tests/paramgrill.c b/tests/paramgrill.c index 7fd4513c4..0e6c58762 100644 --- a/tests/paramgrill.c +++ b/tests/paramgrill.c @@ -170,10 +170,10 @@ BMK_benchParam(BMK_result_t* resultPtr, ZSTD_CCtx* ctx, ZSTD_DCtx* dctx, const ZSTD_compressionParameters cParams) { - *resultPtr = BMK_benchMem(srcBuffer,srcSize, "File", 0, &srcSize, 1, - NULL, 0, &cParams, ctx, dctx, 0); - return 0; + BMK_return_t res = BMK_benchMem(srcBuffer,srcSize, &srcSize, 1, 0, &cParams, NULL, 0, ctx, dctx, 0, "File"); + *resultPtr = res.result; + return res.errorCode; } static void BMK_printWinner(FILE* f, U32 cLevel, BMK_result_t result, ZSTD_compressionParameters params, size_t srcSize)