blob: 6203af6bdb93359d1a862cae306d06a196879e31 [file] [log] [blame]
Yann Collet4ded9e52016-08-30 10:04:33 -07001/**
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 */
Yann Collet4856a002015-01-24 01:58:16 +01009
Yann Collet4856a002015-01-24 01:58:16 +010010
Yann Colletf3eca252015-10-22 15:31:46 +010011/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010012* Includes
Yann Colletf3eca252015-10-22 15:31:46 +010013***************************************/
inikep957823f2016-05-25 15:30:55 +020014#include "util.h" /* Compiler options, UTIL_GetFileSize, UTIL_sleep */
Yann Collet4856a002015-01-24 01:58:16 +010015#include <stdlib.h> /* malloc, free */
16#include <string.h> /* memset */
Yann Colleteeb8ba12015-10-22 16:55:40 +010017#include <stdio.h> /* fprintf, fopen, ftello64 */
Yann Collet7ae67bb2016-09-06 06:28:05 +020018#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
inikep4c12f232016-03-29 14:52:13 +020019
Yann Colletf3eca252015-10-22 15:31:46 +010020#include "mem.h"
Yann Colletd3b7f8d2016-06-04 19:47:02 +020021#define ZSTD_STATIC_LINKING_ONLY
22#include "zstd.h"
inikep69fcd7c2016-04-28 12:23:33 +020023#include "datagen.h" /* RDG_genBuffer */
Yann Collet4856a002015-01-24 01:58:16 +010024#include "xxhash.h"
25
26
Yann Colletf3eca252015-10-22 15:31:46 +010027/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010028* Constants
Yann Colletf3eca252015-10-22 15:31:46 +010029***************************************/
inikepf2f59d72016-06-22 15:42:26 +020030#ifndef ZSTD_GIT_COMMIT
inikepd7d251c2016-06-22 16:13:25 +020031# define ZSTD_GIT_COMMIT_STRING ""
32#else
33# define ZSTD_GIT_COMMIT_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_GIT_COMMIT)
inikepf2f59d72016-06-22 15:42:26 +020034#endif
35
Przemyslaw Skibinski26306fc2016-11-03 11:38:01 +010036#define NBSECONDS 3
inikep83c76b42016-04-28 13:16:01 +020037#define TIMELOOP_MICROSEC 1*1000000ULL /* 1 second */
38#define ACTIVEPERIOD_MICROSEC 70*1000000ULL /* 70 seconds */
39#define COOLPERIOD_SEC 10
Yann Collet4856a002015-01-24 01:58:16 +010040
41#define KB *(1 <<10)
42#define MB *(1 <<20)
43#define GB *(1U<<30)
44
Yann Collet87c18b22016-08-26 01:43:47 +020045static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31));
Yann Collet4856a002015-01-24 01:58:16 +010046
47static U32 g_compressibilityDefault = 50;
Yann Collet4856a002015-01-24 01:58:16 +010048
49
Yann Colletf3eca252015-10-22 15:31:46 +010050/* *************************************
Yann Colleted699e62015-12-16 02:37:24 +010051* console display
Yann Colletf3eca252015-10-22 15:31:46 +010052***************************************/
Yann Colleted699e62015-12-16 02:37:24 +010053#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
54#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
55static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
56
Yann Colletbf2bc112016-08-02 23:48:13 +020057#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
58 if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \
59 { g_time = clock(); DISPLAY(__VA_ARGS__); \
60 if (g_displayLevel>=4) fflush(stdout); } }
61static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100;
62static clock_t g_time = 0;
63
Yann Colleted699e62015-12-16 02:37:24 +010064
65/* *************************************
66* Exceptions
67***************************************/
68#ifndef DEBUG
69# define DEBUG 0
70#endif
71#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
72#define EXM_THROW(error, ...) \
73{ \
74 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
75 DISPLAYLEVEL(1, "Error %i : ", error); \
76 DISPLAYLEVEL(1, __VA_ARGS__); \
Yann Colletd9465012016-12-06 16:49:23 -080077 DISPLAYLEVEL(1, " \n"); \
Yann Colleted699e62015-12-16 02:37:24 +010078 exit(error); \
79}
Yann Collet4856a002015-01-24 01:58:16 +010080
81
Yann Colletf3eca252015-10-22 15:31:46 +010082/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010083* Benchmark Parameters
Yann Colletf3eca252015-10-22 15:31:46 +010084***************************************/
Przemyslaw Skibinski26306fc2016-11-03 11:38:01 +010085static U32 g_nbSeconds = NBSECONDS;
Yann Collet1c00dc32015-10-21 08:22:25 +010086static size_t g_blockSize = 0;
Yann Colletd9465012016-12-06 16:49:23 -080087static int g_additionalParam = 0;
88static U32 g_decodeOnly = 0;
Yann Collet4856a002015-01-24 01:58:16 +010089
inikep4e26bb62016-03-14 12:48:51 +010090void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; }
91
inikepeaba91a2016-03-23 20:30:26 +010092void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; }
93
Przemyslaw Skibinski26306fc2016-11-03 11:38:01 +010094void BMK_SetNbSeconds(unsigned nbSeconds)
Yann Collet4856a002015-01-24 01:58:16 +010095{
Przemyslaw Skibinski26306fc2016-11-03 11:38:01 +010096 g_nbSeconds = nbSeconds;
97 DISPLAYLEVEL(3, "- test >= %u seconds per compression / decompression -\n", g_nbSeconds);
Yann Collet4856a002015-01-24 01:58:16 +010098}
99
Yann Collet1c00dc32015-10-21 08:22:25 +0100100void BMK_SetBlockSize(size_t blockSize)
101{
102 g_blockSize = blockSize;
inikep4e26bb62016-03-14 12:48:51 +0100103 DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10));
Yann Collet1c00dc32015-10-21 08:22:25 +0100104}
105
Yann Colletd9465012016-12-06 16:49:23 -0800106void BMK_setDecodeOnly(unsigned decodeFlag) { g_decodeOnly = (decodeFlag>0); }
Yann Collet4856a002015-01-24 01:58:16 +0100107
Yann Colletf3eca252015-10-22 15:31:46 +0100108/* ********************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100109* Bench functions
Yann Colletf3eca252015-10-22 15:31:46 +0100110**********************************************************/
Yann Colletd9465012016-12-06 16:49:23 -0800111typedef struct {
112 const void* srcPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100113 size_t srcSize;
Yann Colletd9465012016-12-06 16:49:23 -0800114 void* cPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100115 size_t cRoom;
116 size_t cSize;
Yann Colletd9465012016-12-06 16:49:23 -0800117 void* resPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100118 size_t resSize;
119} blockParam_t;
120
Yann Collet99909862016-04-09 16:17:18 +0200121
Yann Colletbe2010e2015-10-31 12:57:14 +0100122#define MIN(a,b) ((a)<(b) ? (a) : (b))
Yann Collet2ce49232016-02-02 14:36:49 +0100123#define MAX(a,b) ((a)>(b) ? (a) : (b))
Yann Collet1c00dc32015-10-21 08:22:25 +0100124
Yann Colleted699e62015-12-16 02:37:24 +0100125static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
inikep472638c2016-03-23 12:28:28 +0100126 const char* displayName, int cLevel,
Yann Collet31683c02015-12-18 01:26:48 +0100127 const size_t* fileSizes, U32 nbFiles,
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100128 const void* dictBuffer, size_t dictBufferSize,
129 ZSTD_compressionParameters *comprParams)
Yann Collet1c00dc32015-10-21 08:22:25 +0100130{
Yann Collete63c6312016-12-06 17:46:49 -0800131 size_t const blockSize = ((g_blockSize>=32 && !g_decodeOnly) ? g_blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ;
Yann Collet6a219712016-08-03 00:06:24 +0200132 size_t const avgSize = MIN(g_blockSize, (srcSize / nbFiles));
Yann Colletd64f4352016-03-21 00:07:42 +0100133 U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
Yann Colleted699e62015-12-16 02:37:24 +0100134 blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t));
Yann Colletb9151402016-03-26 17:18:11 +0100135 size_t const maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */
Yann Collet1c00dc32015-10-21 08:22:25 +0100136 void* const compressedBuffer = malloc(maxCompressedSize);
Yann Colletd9465012016-12-06 16:49:23 -0800137 void* resultBuffer = malloc(srcSize);
Yann Collet3c242e72016-07-13 14:56:24 +0200138 ZSTD_CCtx* const ctx = ZSTD_createCCtx();
139 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
Yann Collete63c6312016-12-06 17:46:49 -0800140 size_t const loadedCompressedSize = srcSize;
141 size_t cSize = 0;
142 double ratio = 0.;
Yann Colletde406ee2016-03-20 15:46:10 +0100143 U32 nbBlocks;
inikep83c76b42016-04-28 13:16:01 +0200144 UTIL_time_t ticksPerSecond;
Yann Colletde406ee2016-03-20 15:46:10 +0100145
146 /* checks */
Yann Colletec224d22016-06-27 13:39:30 +0200147 if (!compressedBuffer || !resultBuffer || !blockTable || !ctx || !dctx)
Yann Collet3d2cd7f2016-06-27 15:12:26 +0200148 EXM_THROW(31, "allocation error : not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100149
Yann Collet4856a002015-01-24 01:58:16 +0100150 /* init */
151 if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */
inikepaaaf9232016-05-09 16:19:25 +0200152 UTIL_initTimer(&ticksPerSecond);
inikep4c12f232016-03-29 14:52:13 +0200153
Yann Collete63c6312016-12-06 17:46:49 -0800154 if (g_decodeOnly) {
155 const char* srcPtr = (const char*) srcBuffer;
156 U64 dSize64 = 0;
157 U32 fileNb;
158 for (fileNb=0; fileNb<nbFiles; fileNb++) {
159 U64 const fSize64 = ZSTD_getDecompressedSize(srcPtr, fileSizes[fileNb]);
160 if (fSize64==0) EXM_THROW(32, "Impossible to determine original size ");
161 dSize64 += fSize64;
162 srcPtr += fileSizes[fileNb];
163 }
164 { size_t const decodedSize = (size_t)dSize64;
165 if (dSize64 > decodedSize) EXM_THROW(32, "original size is too large");
166 if (decodedSize==0) EXM_THROW(32, "Impossible to determine original size ");
167 free(resultBuffer);
168 resultBuffer = malloc(decodedSize);
169 if (!resultBuffer) EXM_THROW(33, "not enough memory");
170 cSize = srcSize;
171 srcSize = decodedSize;
172 ratio = (double)srcSize / (double)cSize;
173 } }
174
Yann Collet1c00dc32015-10-21 08:22:25 +0100175 /* Init blockTable data */
Yann Collete63c6312016-12-06 17:46:49 -0800176 { const char* srcPtr = (const char*)srcBuffer;
Yann Collet1c00dc32015-10-21 08:22:25 +0100177 char* cPtr = (char*)compressedBuffer;
178 char* resPtr = (char*)resultBuffer;
Yann Collet699b14d2016-03-17 19:37:33 +0100179 U32 fileNb;
Yann Colletde406ee2016-03-20 15:46:10 +0100180 for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
Yann Collet70611352015-12-16 03:01:03 +0100181 size_t remaining = fileSizes[fileNb];
Yann Collete63c6312016-12-06 17:46:49 -0800182 U32 const nbBlocksforThisFile = g_decodeOnly ? 1 : (U32)((remaining + (blockSize-1)) / blockSize);
Yann Collet699b14d2016-03-17 19:37:33 +0100183 U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
Yann Colletfd416f12016-01-30 03:14:15 +0100184 for ( ; nbBlocks<blockEnd; nbBlocks++) {
Yann Colletde406ee2016-03-20 15:46:10 +0100185 size_t const thisBlockSize = MIN(remaining, blockSize);
Yann Colletd9465012016-12-06 16:49:23 -0800186 blockTable[nbBlocks].srcPtr = (const void*)srcPtr;
Yann Colleted699e62015-12-16 02:37:24 +0100187 blockTable[nbBlocks].srcSize = thisBlockSize;
Yann Colletd9465012016-12-06 16:49:23 -0800188 blockTable[nbBlocks].cPtr = (void*)cPtr;
Yann Collete63c6312016-12-06 17:46:49 -0800189 blockTable[nbBlocks].cRoom = g_decodeOnly ? thisBlockSize : ZSTD_compressBound(thisBlockSize);
190 blockTable[nbBlocks].cSize = blockTable[nbBlocks].cRoom;
Yann Colletd9465012016-12-06 16:49:23 -0800191 blockTable[nbBlocks].resPtr = (void*)resPtr;
Yann Collete63c6312016-12-06 17:46:49 -0800192 blockTable[nbBlocks].resSize = g_decodeOnly ? (size_t) ZSTD_getDecompressedSize(srcPtr, thisBlockSize) : thisBlockSize;
Yann Colleted699e62015-12-16 02:37:24 +0100193 srcPtr += thisBlockSize;
194 cPtr += blockTable[nbBlocks].cRoom;
195 resPtr += thisBlockSize;
196 remaining -= thisBlockSize;
Yann Collete63c6312016-12-06 17:46:49 -0800197 } } }
Yann Collet1c00dc32015-10-21 08:22:25 +0100198
Yann Collet4856a002015-01-24 01:58:16 +0100199 /* warmimg up memory */
Yann Colletd062f132015-12-01 01:31:17 +0100200 RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
Yann Collet4856a002015-01-24 01:58:16 +0100201
202 /* Bench */
inikep6d157f12016-04-15 16:54:11 +0200203 { U64 fastestC = (U64)(-1LL), fastestD = (U64)(-1LL);
Yann Colletd9465012016-12-06 16:49:23 -0800204 U64 const crcOrig = g_decodeOnly ? 0 : XXH64(srcBuffer, srcSize, 0);
inikep83c76b42016-04-28 13:16:01 +0200205 UTIL_time_t coolTime;
Yann Collet4f5350f2016-11-29 13:12:24 -0800206 U64 const maxTime = (g_nbSeconds * TIMELOOP_MICROSEC) + 1;
Yann Colleta9febe82016-08-01 13:37:17 +0200207 U64 totalCTime=0, totalDTime=0;
Yann Colletd9465012016-12-06 16:49:23 -0800208 U32 cCompleted=g_decodeOnly, dCompleted=0;
Yann Colleta9febe82016-08-01 13:37:17 +0200209# define NB_MARKS 4
210 const char* const marks[NB_MARKS] = { " |", " /", " =", "\\" };
211 U32 markNb = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100212
inikepaaaf9232016-05-09 16:19:25 +0200213 UTIL_getTime(&coolTime);
inikep4e26bb62016-03-14 12:48:51 +0100214 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Colletd9465012016-12-06 16:49:23 -0800215 while (!cCompleted || !dCompleted) {
inikep83c76b42016-04-28 13:16:01 +0200216 UTIL_time_t clockStart;
Yann Collet4856a002015-01-24 01:58:16 +0100217
Yann Collet27d3dad2016-03-11 13:41:20 +0100218 /* overheat protection */
inikep83c76b42016-04-28 13:16:01 +0200219 if (UTIL_clockSpanMicro(coolTime, ticksPerSecond) > ACTIVEPERIOD_MICROSEC) {
Yann Collet235911e2016-07-31 01:32:48 +0200220 DISPLAYLEVEL(2, "\rcooling down ... \r");
inikep83c76b42016-04-28 13:16:01 +0200221 UTIL_sleep(COOLPERIOD_SEC);
inikepaaaf9232016-05-09 16:19:25 +0200222 UTIL_getTime(&coolTime);
Yann Collet27d3dad2016-03-11 13:41:20 +0100223 }
Yann Collet4856a002015-01-24 01:58:16 +0100224
Yann Colletd9465012016-12-06 16:49:23 -0800225 if (!g_decodeOnly) {
226 /* Compression */
227 DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (U32)srcSize);
228 if (!cCompleted) memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100229
Yann Colletd9465012016-12-06 16:49:23 -0800230 UTIL_sleepMilli(1); /* give processor time to other processes */
231 UTIL_waitForNextTick(ticksPerSecond);
232 UTIL_getTime(&clockStart);
Yann Colletde406ee2016-03-20 15:46:10 +0100233
Yann Colletd9465012016-12-06 16:49:23 -0800234 if (!cCompleted) { /* still some time to do compression tests */
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100235 ZSTD_parameters zparams = ZSTD_getParams(cLevel, avgSize, dictBufferSize);
Yann Colletd9465012016-12-06 16:49:23 -0800236 ZSTD_customMem const cmem = { NULL, NULL, NULL };
237 U64 clockLoop = g_nbSeconds ? TIMELOOP_MICROSEC : 1;
238 U32 nbLoops = 0;
239 ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictBufferSize, zparams, cmem);
240 if (cdict==NULL) EXM_THROW(1, "ZSTD_createCDict_advanced() allocation failure");
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100241 if (comprParams->windowLog) zparams.cParams.windowLog = comprParams->windowLog;
242 if (comprParams->chainLog) zparams.cParams.chainLog = comprParams->chainLog;
243 if (comprParams->hashLog) zparams.cParams.hashLog = comprParams->hashLog;
244 if (comprParams->searchLog) zparams.cParams.searchLog = comprParams->searchLog;
245 if (comprParams->searchLength) zparams.cParams.searchLength = comprParams->searchLength;
246 if (comprParams->targetLength) zparams.cParams.targetLength = comprParams->targetLength;
247 if (comprParams->strategy) zparams.cParams.strategy = comprParams->strategy;
Yann Colletd9465012016-12-06 16:49:23 -0800248 do {
249 U32 blockNb;
250 size_t rSize;
251 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
252 if (dictBufferSize) {
253 rSize = ZSTD_compress_usingCDict(ctx,
254 blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
255 blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize,
256 cdict);
257 } else {
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100258 rSize = ZSTD_compress_advanced (ctx,
Yann Colletd9465012016-12-06 16:49:23 -0800259 blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100260 blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize, NULL, 0, zparams);
Yann Colletd9465012016-12-06 16:49:23 -0800261 }
262 if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingCDict() failed : %s", ZSTD_getErrorName(rSize));
263 blockTable[blockNb].cSize = rSize;
Przemyslaw Skibinski2558b4c2016-11-18 11:46:30 +0100264 }
Yann Colletd9465012016-12-06 16:49:23 -0800265 nbLoops++;
266 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
267 ZSTD_freeCDict(cdict);
268 { U64 const clockSpan = UTIL_clockSpanMicro(clockStart, ticksPerSecond);
269 if (clockSpan < fastestC*nbLoops) fastestC = clockSpan / nbLoops;
270 totalCTime += clockSpan;
271 cCompleted = (totalCTime >= maxTime);
272 } }
Yann Collet4856a002015-01-24 01:58:16 +0100273
Yann Colletd9465012016-12-06 16:49:23 -0800274 cSize = 0;
275 { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; }
276 ratio = (double)srcSize / (double)cSize;
277 markNb = (markNb+1) % NB_MARKS;
278 DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r",
279 marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratio,
280 (double)srcSize / fastestC );
281 } else { /* g_decodeOnly */
Yann Collete63c6312016-12-06 17:46:49 -0800282 memcpy(compressedBuffer, srcBuffer, loadedCompressedSize);
Yann Colletd9465012016-12-06 16:49:23 -0800283 }
Yann Collet4856a002015-01-24 01:58:16 +0100284
Yann Colleta5b66e32016-03-26 01:48:27 +0100285 (void)fastestD; (void)crcOrig; /* unused when decompression disabled */
Yann Collete93d6ce2016-01-31 00:58:06 +0100286#if 1
Yann Collet4856a002015-01-24 01:58:16 +0100287 /* Decompression */
Yann Colletd1733f72016-08-21 01:04:46 +0200288 if (!dCompleted) memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100289
inikep83c76b42016-04-28 13:16:01 +0200290 UTIL_sleepMilli(1); /* give processor time to other processes */
291 UTIL_waitForNextTick(ticksPerSecond);
inikepaaaf9232016-05-09 16:19:25 +0200292 UTIL_getTime(&clockStart);
Yann Collet7b51a292016-01-26 15:58:49 +0100293
Yann Colleta9febe82016-08-01 13:37:17 +0200294 if (!dCompleted) {
Yann Colletd9465012016-12-06 16:49:23 -0800295 U64 clockLoop = g_nbSeconds ? TIMELOOP_MICROSEC : 1;
Yann Colleta9febe82016-08-01 13:37:17 +0200296 U32 nbLoops = 0;
Yann Colletd9465012016-12-06 16:49:23 -0800297 ZSTD_DDict* const ddict = ZSTD_createDDict(dictBuffer, dictBufferSize);
Yann Colletee1a0842016-06-07 01:40:49 +0200298 if (!ddict) EXM_THROW(2, "ZSTD_createDDict() allocation failure");
inikepc5e1d292016-04-19 09:37:59 +0200299 do {
Yann Colleta5b66e32016-03-26 01:48:27 +0100300 U32 blockNb;
Yann Colleta5b66e32016-03-26 01:48:27 +0100301 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
Yann Colletee1a0842016-06-07 01:40:49 +0200302 size_t const regenSize = ZSTD_decompress_usingDDict(dctx,
Yann Collete63c6312016-12-06 17:46:49 -0800303 blockTable[blockNb].resPtr, blockTable[blockNb].resSize,
Yann Colletee1a0842016-06-07 01:40:49 +0200304 blockTable[blockNb].cPtr, blockTable[blockNb].cSize,
305 ddict);
Yann Colleta5b66e32016-03-26 01:48:27 +0100306 if (ZSTD_isError(regenSize)) {
Yann Collet4c56f4a2016-06-27 13:36:54 +0200307 DISPLAY("ZSTD_decompress_usingDDict() failed on block %u : %s \n",
Yann Colleta5b66e32016-03-26 01:48:27 +0100308 blockNb, ZSTD_getErrorName(regenSize));
inikep19bd48f2016-04-04 12:10:00 +0200309 clockLoop = 0; /* force immediate test end */
Yann Colleta5b66e32016-03-26 01:48:27 +0100310 break;
311 }
312 blockTable[blockNb].resSize = regenSize;
inikepc5e1d292016-04-19 09:37:59 +0200313 }
314 nbLoops++;
inikep83c76b42016-04-28 13:16:01 +0200315 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
Yann Colletee1a0842016-06-07 01:40:49 +0200316 ZSTD_freeDDict(ddict);
inikep83c76b42016-04-28 13:16:01 +0200317 { U64 const clockSpan = UTIL_clockSpanMicro(clockStart, ticksPerSecond);
inikep6d157f12016-04-15 16:54:11 +0200318 if (clockSpan < fastestD*nbLoops) fastestD = clockSpan / nbLoops;
Yann Colleta9febe82016-08-01 13:37:17 +0200319 totalDTime += clockSpan;
Yann Collet4f5350f2016-11-29 13:12:24 -0800320 dCompleted = (totalDTime >= maxTime);
Yann Collet7b51a292016-01-26 15:58:49 +0100321 } }
322
Yann Colleta9febe82016-08-01 13:37:17 +0200323 markNb = (markNb+1) % NB_MARKS;
324 DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r",
325 marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratio,
inikep06f793a2016-03-29 11:17:58 +0200326 (double)srcSize / fastestC,
327 (double)srcSize / fastestD );
Yann Collet4856a002015-01-24 01:58:16 +0100328
329 /* CRC Checking */
Yann Collete162ace2016-05-20 11:24:35 +0200330 { U64 const crcCheck = XXH64(resultBuffer, srcSize, 0);
Yann Colletd9465012016-12-06 16:49:23 -0800331 if (!g_decodeOnly && (crcOrig!=crcCheck)) {
Yann Colleta5b66e32016-03-26 01:48:27 +0100332 size_t u;
333 DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck);
334 for (u=0; u<srcSize; u++) {
335 if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) {
336 U32 segNb, bNb, pos;
337 size_t bacc = 0;
338 DISPLAY("Decoding error at pos %u ", (U32)u);
339 for (segNb = 0; segNb < nbBlocks; segNb++) {
340 if (bacc + blockTable[segNb].srcSize > u) break;
341 bacc += blockTable[segNb].srcSize;
342 }
343 pos = (U32)(u - bacc);
344 bNb = pos / (128 KB);
345 DISPLAY("(block %u, sub %u, pos %u) \n", segNb, bNb, pos);
346 break;
Yann Collet03a6dab2016-01-21 02:21:17 +0100347 }
Yann Colleta5b66e32016-03-26 01:48:27 +0100348 if (u==srcSize-1) { /* should never happen */
349 DISPLAY("no difference detected\n");
350 } }
351 break;
352 } } /* CRC Checking */
Yann Collete8c6bb12015-07-26 00:23:57 +0100353#endif
Przemyslaw Skibinski26306fc2016-11-03 11:38:01 +0100354 } /* for (testNb = 1; testNb <= (g_nbSeconds + !g_nbSeconds); testNb++) */
Yann Collet4856a002015-01-24 01:58:16 +0100355
inikep7132fb12016-08-10 14:59:18 +0200356 if (g_displayLevel == 1) {
357 double cSpeed = (double)srcSize / fastestC;
358 double dSpeed = (double)srcSize / fastestD;
359 if (g_additionalParam)
360 DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s (param=%d)\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName, g_additionalParam);
361 else
362 DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName);
363 }
inikep2872b6f2016-03-22 14:38:34 +0100364 DISPLAYLEVEL(2, "%2i#\n", cLevel);
Yann Colletde406ee2016-03-20 15:46:10 +0100365 } /* Bench */
Yann Collet4856a002015-01-24 01:58:16 +0100366
Yann Colleted699e62015-12-16 02:37:24 +0100367 /* clean up */
inikep0bd0fae2016-05-05 13:10:57 +0200368 free(blockTable);
Yann Collet4856a002015-01-24 01:58:16 +0100369 free(compressedBuffer);
370 free(resultBuffer);
Yann Collet31683c02015-12-18 01:26:48 +0100371 ZSTD_freeCCtx(ctx);
372 ZSTD_freeDCtx(dctx);
Yann Collet4856a002015-01-24 01:58:16 +0100373 return 0;
374}
375
376
Yann Collet4856a002015-01-24 01:58:16 +0100377static size_t BMK_findMaxMem(U64 requiredMem)
378{
Yann Colletde406ee2016-03-20 15:46:10 +0100379 size_t const step = 64 MB;
Yann Collet4856a002015-01-24 01:58:16 +0100380 BYTE* testmem = NULL;
381
382 requiredMem = (((requiredMem >> 26) + 1) << 26);
Yann Colletde406ee2016-03-20 15:46:10 +0100383 requiredMem += step;
Yann Collet050efba2015-11-03 09:49:30 +0100384 if (requiredMem > maxMemory) requiredMem = maxMemory;
Yann Collet4856a002015-01-24 01:58:16 +0100385
Yann Colletde406ee2016-03-20 15:46:10 +0100386 do {
Yann Collet4856a002015-01-24 01:58:16 +0100387 testmem = (BYTE*)malloc((size_t)requiredMem);
Yann Colletde406ee2016-03-20 15:46:10 +0100388 requiredMem -= step;
389 } while (!testmem);
390
Yann Collet4856a002015-01-24 01:58:16 +0100391 free(testmem);
Yann Colletde406ee2016-03-20 15:46:10 +0100392 return (size_t)(requiredMem);
Yann Collet4856a002015-01-24 01:58:16 +0100393}
394
Yann Colleted699e62015-12-16 02:37:24 +0100395static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize,
inikepeaba91a2016-03-23 20:30:26 +0100396 const char* displayName, int cLevel, int cLevelLast,
Yann Collet31683c02015-12-18 01:26:48 +0100397 const size_t* fileSizes, unsigned nbFiles,
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100398 const void* dictBuffer, size_t dictBufferSize,
399 ZSTD_compressionParameters *compressionParams)
Yann Collet4856a002015-01-24 01:58:16 +0100400{
inikepe9554b72016-03-14 18:10:30 +0100401 int l;
inikep4e26bb62016-03-14 12:48:51 +0100402
inikepe9554b72016-03-14 18:10:30 +0100403 const char* pch = strrchr(displayName, '\\'); /* Windows */
404 if (!pch) pch = strrchr(displayName, '/'); /* Linux */
405 if (pch) displayName = pch+1;
inikep4e26bb62016-03-14 12:48:51 +0100406
inikepea4ee3e2016-04-25 13:09:06 +0200407 SET_HIGH_PRIORITY;
408
inikepeaba91a2016-03-23 20:30:26 +0100409 if (g_displayLevel == 1 && !g_additionalParam)
Przemyslaw Skibinski26306fc2016-11-03 11:38:01 +0100410 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));
inikepe9554b72016-03-14 18:10:30 +0100411
412 if (cLevelLast < cLevel) cLevelLast = cLevel;
413
Yann Collet99909862016-04-09 16:17:18 +0200414 for (l=cLevel; l <= cLevelLast; l++) {
inikepe9554b72016-03-14 18:10:30 +0100415 BMK_benchMem(srcBuffer, benchedSize,
inikep472638c2016-03-23 12:28:28 +0100416 displayName, l,
inikepe9554b72016-03-14 18:10:30 +0100417 fileSizes, nbFiles,
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100418 dictBuffer, dictBufferSize, compressionParams);
inikep7132fb12016-08-10 14:59:18 +0200419 }
Yann Colleted699e62015-12-16 02:37:24 +0100420}
421
Yann Colleted699e62015-12-16 02:37:24 +0100422
Yann Colleta5b66e32016-03-26 01:48:27 +0100423/*! BMK_loadFiles() :
424 Loads `buffer` with content of files listed within `fileNamesTable`.
425 At most, fills `buffer` entirely */
Yann Collet70611352015-12-16 03:01:03 +0100426static void BMK_loadFiles(void* buffer, size_t bufferSize,
427 size_t* fileSizes,
Yann Colleta5b66e32016-03-26 01:48:27 +0100428 const char** fileNamesTable, unsigned nbFiles)
Yann Colleted699e62015-12-16 02:37:24 +0100429{
inikepc0d5f4e2016-04-13 10:48:04 +0200430 size_t pos = 0, totalSize = 0;
Yann Collet699b14d2016-03-17 19:37:33 +0100431 unsigned n;
Yann Colletfd416f12016-01-30 03:14:15 +0100432 for (n=0; n<nbFiles; n++) {
Yann Collete162ace2016-05-20 11:24:35 +0200433 FILE* f;
inikep69fcd7c2016-04-28 12:23:33 +0200434 U64 fileSize = UTIL_getFileSize(fileNamesTable[n]);
435 if (UTIL_isDirectory(fileNamesTable[n])) {
inikepc0d5f4e2016-04-13 10:48:04 +0200436 DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]);
inikepbab43172016-04-29 15:19:40 +0200437 fileSizes[n] = 0;
inikepc0d5f4e2016-04-13 10:48:04 +0200438 continue;
439 }
inikepea4ee3e2016-04-25 13:09:06 +0200440 f = fopen(fileNamesTable[n], "rb");
Yann Colleted699e62015-12-16 02:37:24 +0100441 if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]);
Yann Colletbf2bc112016-08-02 23:48:13 +0200442 DISPLAYUPDATE(2, "Loading %s... \r", fileNamesTable[n]);
Yann Colleta5b66e32016-03-26 01:48:27 +0100443 if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */
444 { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
445 if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]);
446 pos += readSize; }
Yann Colleta52c98d2015-12-16 03:12:31 +0100447 fileSizes[n] = (size_t)fileSize;
inikepc0d5f4e2016-04-13 10:48:04 +0200448 totalSize += (size_t)fileSize;
Yann Colleted699e62015-12-16 02:37:24 +0100449 fclose(f);
450 }
Yann Collet6f9c0562016-05-01 10:26:30 +0200451
inikepc0d5f4e2016-04-13 10:48:04 +0200452 if (totalSize == 0) EXM_THROW(12, "no data to bench");
Yann Colleted699e62015-12-16 02:37:24 +0100453}
454
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100455static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles, const char* dictFileName,
456 int cLevel, int cLevelLast, ZSTD_compressionParameters *compressionParams)
Yann Colleted699e62015-12-16 02:37:24 +0100457{
458 void* srcBuffer;
459 size_t benchedSize;
Yann Collet31683c02015-12-18 01:26:48 +0100460 void* dictBuffer = NULL;
461 size_t dictBufferSize = 0;
462 size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t));
Yann Collete162ace2016-05-20 11:24:35 +0200463 U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +0100464 char mfName[20] = {0};
Yann Colleted699e62015-12-16 02:37:24 +0100465
Yann Collet31683c02015-12-18 01:26:48 +0100466 if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes");
467
468 /* Load dictionary */
Yann Colletfd416f12016-01-30 03:14:15 +0100469 if (dictFileName != NULL) {
inikep69fcd7c2016-04-28 12:23:33 +0200470 U64 dictFileSize = UTIL_getFileSize(dictFileName);
Yann Collet31683c02015-12-18 01:26:48 +0100471 if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName);
472 dictBufferSize = (size_t)dictFileSize;
473 dictBuffer = malloc(dictBufferSize);
474 if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize);
475 BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1);
476 }
477
Yann Colleted699e62015-12-16 02:37:24 +0100478 /* Memory allocation & restrictions */
479 benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
480 if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
481 if (benchedSize < totalSizeToLoad)
482 DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20));
483 srcBuffer = malloc(benchedSize);
484 if (!srcBuffer) EXM_THROW(12, "not enough memory");
485
486 /* Load input buffer */
Yann Collet70611352015-12-16 03:01:03 +0100487 BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +0100488
489 /* Bench */
490 snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
Yann Collete162ace2016-05-20 11:24:35 +0200491 { const char* displayName = (nbFiles > 1) ? mfName : fileNamesTable[0];
492 BMK_benchCLevel(srcBuffer, benchedSize,
493 displayName, cLevel, cLevelLast,
494 fileSizes, nbFiles,
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100495 dictBuffer, dictBufferSize, compressionParams);
Yann Collete162ace2016-05-20 11:24:35 +0200496 }
Yann Collet4856a002015-01-24 01:58:16 +0100497
Yann Colleteeb8ba12015-10-22 16:55:40 +0100498 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100499 free(srcBuffer);
Yann Collet31683c02015-12-18 01:26:48 +0100500 free(dictBuffer);
Yann Collet70611352015-12-16 03:01:03 +0100501 free(fileSizes);
Yann Collet4856a002015-01-24 01:58:16 +0100502}
503
504
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100505static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility, ZSTD_compressionParameters* compressionParams)
Yann Collet4856a002015-01-24 01:58:16 +0100506{
Yann Colleted699e62015-12-16 02:37:24 +0100507 char name[20] = {0};
Yann Collet4856a002015-01-24 01:58:16 +0100508 size_t benchedSize = 10000000;
Yann Collete162ace2016-05-20 11:24:35 +0200509 void* const srcBuffer = malloc(benchedSize);
Yann Collet4856a002015-01-24 01:58:16 +0100510
Yann Collet4856a002015-01-24 01:58:16 +0100511 /* Memory allocation */
Yann Colleted699e62015-12-16 02:37:24 +0100512 if (!srcBuffer) EXM_THROW(21, "not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100513
514 /* Fill input buffer */
Yann Colletd062f132015-12-01 01:31:17 +0100515 RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100516
517 /* Bench */
Yann Colleted699e62015-12-16 02:37:24 +0100518 snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100519 BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0, compressionParams);
Yann Collet4856a002015-01-24 01:58:16 +0100520
Yann Colleted699e62015-12-16 02:37:24 +0100521 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100522 free(srcBuffer);
Yann Collet4856a002015-01-24 01:58:16 +0100523}
524
525
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100526int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, const char* dictFileName,
527 int cLevel, int cLevelLast, ZSTD_compressionParameters* compressionParams)
Yann Collet4856a002015-01-24 01:58:16 +0100528{
Yann Collet699b14d2016-03-17 19:37:33 +0100529 double const compressibility = (double)g_compressibilityDefault / 100;
Yann Collet4856a002015-01-24 01:58:16 +0100530
Przemyslaw Skibinskifd0ac932016-11-23 21:45:29 +0100531 if (cLevel > ZSTD_maxCLevel()) cLevel = ZSTD_maxCLevel();
532 if (cLevelLast > ZSTD_maxCLevel()) cLevelLast = ZSTD_maxCLevel();
Przemyslaw Skibinski5ddcd9d2016-11-21 16:37:56 +0100533 if (cLevelLast < cLevel) cLevelLast = cLevel;
Yann Colletd9465012016-12-06 16:49:23 -0800534 if (cLevelLast > cLevel) DISPLAYLEVEL(2, "Benchmarking levels from %d to %d\n", cLevel, cLevelLast);
Przemyslaw Skibinski5ddcd9d2016-11-21 16:37:56 +0100535
Yann Collet4856a002015-01-24 01:58:16 +0100536 if (nbFiles == 0)
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100537 BMK_syntheticTest(cLevel, cLevelLast, compressibility, compressionParams);
Yann Collet4856a002015-01-24 01:58:16 +0100538 else
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100539 BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast, compressionParams);
Yann Collet4856a002015-01-24 01:58:16 +0100540 return 0;
541}