xref: /freebsd/sys/contrib/zstd/programs/benchzstd.c (revision f976241773df2260e6170317080761d1c5814fe5)
1 /*
2  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under both the BSD-style license (found in the
6  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7  * in the COPYING file in the root directory of this source tree).
8  * You may select, at your option, one of the above-listed licenses.
9  */
10 
11 
12 /* **************************************
13 *  Tuning parameters
14 ****************************************/
15 #ifndef BMK_TIMETEST_DEFAULT_S   /* default minimum time per test */
16 #define BMK_TIMETEST_DEFAULT_S 3
17 #endif
18 
19 
20 /* *************************************
21 *  Includes
22 ***************************************/
23 #include "platform.h"    /* Large Files support */
24 #include "util.h"        /* UTIL_getFileSize, UTIL_sleep */
25 #include <stdlib.h>      /* malloc, free */
26 #include <string.h>      /* memset, strerror */
27 #include <stdio.h>       /* fprintf, fopen */
28 #include <errno.h>
29 #include <assert.h>      /* assert */
30 
31 #include "timefn.h"      /* UTIL_time_t */
32 #include "benchfn.h"
33 #include "mem.h"
34 #define ZSTD_STATIC_LINKING_ONLY
35 #include "zstd.h"
36 #include "datagen.h"     /* RDG_genBuffer */
37 #include "xxhash.h"
38 #include "benchzstd.h"
39 #include "zstd_errors.h"
40 
41 
42 /* *************************************
43 *  Constants
44 ***************************************/
45 #ifndef ZSTD_GIT_COMMIT
46 #  define ZSTD_GIT_COMMIT_STRING ""
47 #else
48 #  define ZSTD_GIT_COMMIT_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_GIT_COMMIT)
49 #endif
50 
51 #define TIMELOOP_MICROSEC     (1*1000000ULL) /* 1 second */
52 #define TIMELOOP_NANOSEC      (1*1000000000ULL) /* 1 second */
53 #define ACTIVEPERIOD_MICROSEC (70*TIMELOOP_MICROSEC) /* 70 seconds */
54 #define COOLPERIOD_SEC        10
55 
56 #define KB *(1 <<10)
57 #define MB *(1 <<20)
58 #define GB *(1U<<30)
59 
60 #define BMK_RUNTEST_DEFAULT_MS 1000
61 
62 static const size_t maxMemory = (sizeof(size_t)==4)  ?
63                     /* 32-bit */ (2 GB - 64 MB) :
64                     /* 64-bit */ (size_t)(1ULL << ((sizeof(size_t)*8)-31));
65 
66 
67 /* *************************************
68 *  console display
69 ***************************************/
70 #define DISPLAY(...)         fprintf(stderr, __VA_ARGS__)
71 #define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); }
72 /* 0 : no display;   1: errors;   2 : + result + interaction + warnings;   3 : + progression;   4 : + information */
73 
74 static const U64 g_refreshRate = SEC_TO_MICRO / 6;
75 static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
76 
77 #define DISPLAYUPDATE(l, ...) { if (displayLevel>=l) { \
78             if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (displayLevel>=4)) \
79             { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \
80             if (displayLevel>=4) fflush(stderr); } } }
81 
82 
83 /* *************************************
84 *  Exceptions
85 ***************************************/
86 #ifndef DEBUG
87 #  define DEBUG 0
88 #endif
89 #define DEBUGOUTPUT(...) { if (DEBUG) DISPLAY(__VA_ARGS__); }
90 
91 #define RETURN_ERROR_INT(errorNum, ...)  {               \
92     DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__);    \
93     DISPLAYLEVEL(1, "Error %i : ", errorNum);         \
94     DISPLAYLEVEL(1, __VA_ARGS__);                     \
95     DISPLAYLEVEL(1, " \n");                           \
96     return errorNum;                                  \
97 }
98 
99 #define CHECK_Z(zf) {              \
100     size_t const zerr = zf;        \
101     if (ZSTD_isError(zerr)) {      \
102         DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__);  \
103         DISPLAY("Error : ");       \
104         DISPLAY("%s failed : %s",  \
105                 #zf, ZSTD_getErrorName(zerr));   \
106         DISPLAY(" \n");            \
107         exit(1);                   \
108     }                              \
109 }
110 
111 #define RETURN_ERROR(errorNum, retType, ...)  {       \
112     retType r;                                        \
113     memset(&r, 0, sizeof(retType));                   \
114     DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__);    \
115     DISPLAYLEVEL(1, "Error %i : ", errorNum);         \
116     DISPLAYLEVEL(1, __VA_ARGS__);                     \
117     DISPLAYLEVEL(1, " \n");                           \
118     r.tag = errorNum;                                 \
119     return r;                                         \
120 }
121 
122 
123 /* *************************************
124 *  Benchmark Parameters
125 ***************************************/
126 
127 BMK_advancedParams_t BMK_initAdvancedParams(void) {
128     BMK_advancedParams_t const res = {
129         BMK_both, /* mode */
130         BMK_TIMETEST_DEFAULT_S, /* nbSeconds */
131         0, /* blockSize */
132         0, /* nbWorkers */
133         0, /* realTime */
134         0, /* additionalParam */
135         0, /* ldmFlag */
136         0, /* ldmMinMatch */
137         0, /* ldmHashLog */
138         0, /* ldmBuckSizeLog */
139         0,  /* ldmHashRateLog */
140         ZSTD_lcm_auto /* literalCompressionMode */
141     };
142     return res;
143 }
144 
145 
146 /* ********************************************************
147 *  Bench functions
148 **********************************************************/
149 typedef struct {
150     const void* srcPtr;
151     size_t srcSize;
152     void*  cPtr;
153     size_t cRoom;
154     size_t cSize;
155     void*  resPtr;
156     size_t resSize;
157 } blockParam_t;
158 
159 #undef MIN
160 #undef MAX
161 #define MIN(a,b)    ((a) < (b) ? (a) : (b))
162 #define MAX(a,b)    ((a) > (b) ? (a) : (b))
163 
164 static void
165 BMK_initCCtx(ZSTD_CCtx* ctx,
166             const void* dictBuffer, size_t dictBufferSize,
167             int cLevel,
168             const ZSTD_compressionParameters* comprParams,
169             const BMK_advancedParams_t* adv)
170 {
171     ZSTD_CCtx_reset(ctx, ZSTD_reset_session_and_parameters);
172     if (adv->nbWorkers==1) {
173         CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_nbWorkers, 0));
174     } else {
175         CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_nbWorkers, adv->nbWorkers));
176     }
177     CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, cLevel));
178     CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_enableLongDistanceMatching, adv->ldmFlag));
179     CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmMinMatch, adv->ldmMinMatch));
180     CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmHashLog, adv->ldmHashLog));
181     CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmBucketSizeLog, adv->ldmBucketSizeLog));
182     CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmHashRateLog, adv->ldmHashRateLog));
183     CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_windowLog, (int)comprParams->windowLog));
184     CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_hashLog, (int)comprParams->hashLog));
185     CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_chainLog, (int)comprParams->chainLog));
186     CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_searchLog, (int)comprParams->searchLog));
187     CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_minMatch, (int)comprParams->minMatch));
188     CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_targetLength, (int)comprParams->targetLength));
189     CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_literalCompressionMode, (int)adv->literalCompressionMode));
190     CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_strategy, comprParams->strategy));
191     CHECK_Z(ZSTD_CCtx_loadDictionary(ctx, dictBuffer, dictBufferSize));
192 }
193 
194 static void BMK_initDCtx(ZSTD_DCtx* dctx,
195     const void* dictBuffer, size_t dictBufferSize) {
196     CHECK_Z(ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters));
197     CHECK_Z(ZSTD_DCtx_loadDictionary(dctx, dictBuffer, dictBufferSize));
198 }
199 
200 
201 typedef struct {
202     ZSTD_CCtx* cctx;
203     const void* dictBuffer;
204     size_t dictBufferSize;
205     int cLevel;
206     const ZSTD_compressionParameters* comprParams;
207     const BMK_advancedParams_t* adv;
208 } BMK_initCCtxArgs;
209 
210 static size_t local_initCCtx(void* payload) {
211     BMK_initCCtxArgs* ag = (BMK_initCCtxArgs*)payload;
212     BMK_initCCtx(ag->cctx, ag->dictBuffer, ag->dictBufferSize, ag->cLevel, ag->comprParams, ag->adv);
213     return 0;
214 }
215 
216 typedef struct {
217     ZSTD_DCtx* dctx;
218     const void* dictBuffer;
219     size_t dictBufferSize;
220 } BMK_initDCtxArgs;
221 
222 static size_t local_initDCtx(void* payload) {
223     BMK_initDCtxArgs* ag = (BMK_initDCtxArgs*)payload;
224     BMK_initDCtx(ag->dctx, ag->dictBuffer, ag->dictBufferSize);
225     return 0;
226 }
227 
228 
229 /* `addArgs` is the context */
230 static size_t local_defaultCompress(
231                     const void* srcBuffer, size_t srcSize,
232                     void* dstBuffer, size_t dstSize,
233                     void* addArgs)
234 {
235     ZSTD_CCtx* const cctx = (ZSTD_CCtx*)addArgs;
236     return ZSTD_compress2(cctx, dstBuffer, dstSize, srcBuffer, srcSize);
237 }
238 
239 /* `addArgs` is the context */
240 static size_t local_defaultDecompress(
241                     const void* srcBuffer, size_t srcSize,
242                     void* dstBuffer, size_t dstCapacity,
243                     void* addArgs)
244 {
245     size_t moreToFlush = 1;
246     ZSTD_DCtx* const dctx = (ZSTD_DCtx*)addArgs;
247     ZSTD_inBuffer in;
248     ZSTD_outBuffer out;
249     in.src = srcBuffer; in.size = srcSize; in.pos = 0;
250     out.dst = dstBuffer; out.size = dstCapacity; out.pos = 0;
251     while (moreToFlush) {
252         if(out.pos == out.size) {
253             return (size_t)-ZSTD_error_dstSize_tooSmall;
254         }
255         moreToFlush = ZSTD_decompressStream(dctx, &out, &in);
256         if (ZSTD_isError(moreToFlush)) {
257             return moreToFlush;
258         }
259     }
260     return out.pos;
261 
262 }
263 
264 
265 /* ================================================================= */
266 /*      Benchmark Zstandard, mem-to-mem scenarios                    */
267 /* ================================================================= */
268 
269 int BMK_isSuccessful_benchOutcome(BMK_benchOutcome_t outcome)
270 {
271     return outcome.tag == 0;
272 }
273 
274 BMK_benchResult_t BMK_extract_benchResult(BMK_benchOutcome_t outcome)
275 {
276     assert(outcome.tag == 0);
277     return outcome.internal_never_use_directly;
278 }
279 
280 static BMK_benchOutcome_t BMK_benchOutcome_error(void)
281 {
282     BMK_benchOutcome_t b;
283     memset(&b, 0, sizeof(b));
284     b.tag = 1;
285     return b;
286 }
287 
288 static BMK_benchOutcome_t BMK_benchOutcome_setValidResult(BMK_benchResult_t result)
289 {
290     BMK_benchOutcome_t b;
291     b.tag = 0;
292     b.internal_never_use_directly = result;
293     return b;
294 }
295 
296 
297 /* benchMem with no allocation */
298 static BMK_benchOutcome_t
299 BMK_benchMemAdvancedNoAlloc(
300                     const void** srcPtrs, size_t* srcSizes,
301                     void** cPtrs, size_t* cCapacities, size_t* cSizes,
302                     void** resPtrs, size_t* resSizes,
303                     void** resultBufferPtr, void* compressedBuffer,
304                     size_t maxCompressedSize,
305                     BMK_timedFnState_t* timeStateCompress,
306                     BMK_timedFnState_t* timeStateDecompress,
307 
308                     const void* srcBuffer, size_t srcSize,
309                     const size_t* fileSizes, unsigned nbFiles,
310                     const int cLevel,
311                     const ZSTD_compressionParameters* comprParams,
312                     const void* dictBuffer, size_t dictBufferSize,
313                     ZSTD_CCtx* cctx, ZSTD_DCtx* dctx,
314                     int displayLevel, const char* displayName,
315                     const BMK_advancedParams_t* adv)
316 {
317     size_t const blockSize = ((adv->blockSize>=32 && (adv->mode != BMK_decodeOnly)) ? adv->blockSize : srcSize) + (!srcSize);  /* avoid div by 0 */
318     BMK_benchResult_t benchResult;
319     size_t const loadedCompressedSize = srcSize;
320     size_t cSize = 0;
321     double ratio = 0.;
322     U32 nbBlocks;
323 
324     assert(cctx != NULL); assert(dctx != NULL);
325 
326     /* init */
327     memset(&benchResult, 0, sizeof(benchResult));
328     if (strlen(displayName)>17) displayName += strlen(displayName) - 17;   /* display last 17 characters */
329     if (adv->mode == BMK_decodeOnly) {  /* benchmark only decompression : source must be already compressed */
330         const char* srcPtr = (const char*)srcBuffer;
331         U64 totalDSize64 = 0;
332         U32 fileNb;
333         for (fileNb=0; fileNb<nbFiles; fileNb++) {
334             U64 const fSize64 = ZSTD_findDecompressedSize(srcPtr, fileSizes[fileNb]);
335             if (fSize64==0) RETURN_ERROR(32, BMK_benchOutcome_t, "Impossible to determine original size ");
336             totalDSize64 += fSize64;
337             srcPtr += fileSizes[fileNb];
338         }
339         {   size_t const decodedSize = (size_t)totalDSize64;
340             assert((U64)decodedSize == totalDSize64);   /* check overflow */
341             free(*resultBufferPtr);
342             *resultBufferPtr = malloc(decodedSize);
343             if (!(*resultBufferPtr)) {
344                 RETURN_ERROR(33, BMK_benchOutcome_t, "not enough memory");
345             }
346             if (totalDSize64 > decodedSize) {  /* size_t overflow */
347                 free(*resultBufferPtr);
348                 RETURN_ERROR(32, BMK_benchOutcome_t, "original size is too large");
349             }
350             cSize = srcSize;
351             srcSize = decodedSize;
352             ratio = (double)srcSize / (double)cSize;
353         }
354     }
355 
356     /* Init data blocks  */
357     {   const char* srcPtr = (const char*)srcBuffer;
358         char* cPtr = (char*)compressedBuffer;
359         char* resPtr = (char*)(*resultBufferPtr);
360         U32 fileNb;
361         for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
362             size_t remaining = fileSizes[fileNb];
363             U32 const nbBlocksforThisFile = (adv->mode == BMK_decodeOnly) ? 1 : (U32)((remaining + (blockSize-1)) / blockSize);
364             U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
365             for ( ; nbBlocks<blockEnd; nbBlocks++) {
366                 size_t const thisBlockSize = MIN(remaining, blockSize);
367                 srcPtrs[nbBlocks] = srcPtr;
368                 srcSizes[nbBlocks] = thisBlockSize;
369                 cPtrs[nbBlocks] = cPtr;
370                 cCapacities[nbBlocks] = (adv->mode == BMK_decodeOnly) ? thisBlockSize : ZSTD_compressBound(thisBlockSize);
371                 resPtrs[nbBlocks] = resPtr;
372                 resSizes[nbBlocks] = (adv->mode == BMK_decodeOnly) ? (size_t) ZSTD_findDecompressedSize(srcPtr, thisBlockSize) : thisBlockSize;
373                 srcPtr += thisBlockSize;
374                 cPtr += cCapacities[nbBlocks];
375                 resPtr += thisBlockSize;
376                 remaining -= thisBlockSize;
377                 if (adv->mode == BMK_decodeOnly) {
378                     assert(nbBlocks==0);
379                     cSizes[nbBlocks] = thisBlockSize;
380                     benchResult.cSize = thisBlockSize;
381                 }
382             }
383         }
384     }
385 
386     /* warming up `compressedBuffer` */
387     if (adv->mode == BMK_decodeOnly) {
388         memcpy(compressedBuffer, srcBuffer, loadedCompressedSize);
389     } else {
390         RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
391     }
392 
393     /* Bench */
394     {   U64 const crcOrig = (adv->mode == BMK_decodeOnly) ? 0 : XXH64(srcBuffer, srcSize, 0);
395 #       define NB_MARKS 4
396         const char* marks[NB_MARKS] = { " |", " /", " =", " \\" };
397         U32 markNb = 0;
398         int compressionCompleted = (adv->mode == BMK_decodeOnly);
399         int decompressionCompleted = (adv->mode == BMK_compressOnly);
400         BMK_benchParams_t cbp, dbp;
401         BMK_initCCtxArgs cctxprep;
402         BMK_initDCtxArgs dctxprep;
403 
404         cbp.benchFn = local_defaultCompress;   /* ZSTD_compress2 */
405         cbp.benchPayload = cctx;
406         cbp.initFn = local_initCCtx;   /* BMK_initCCtx */
407         cbp.initPayload = &cctxprep;
408         cbp.errorFn = ZSTD_isError;
409         cbp.blockCount = nbBlocks;
410         cbp.srcBuffers = srcPtrs;
411         cbp.srcSizes = srcSizes;
412         cbp.dstBuffers = cPtrs;
413         cbp.dstCapacities = cCapacities;
414         cbp.blockResults = cSizes;
415 
416         cctxprep.cctx = cctx;
417         cctxprep.dictBuffer = dictBuffer;
418         cctxprep.dictBufferSize = dictBufferSize;
419         cctxprep.cLevel = cLevel;
420         cctxprep.comprParams = comprParams;
421         cctxprep.adv = adv;
422 
423         dbp.benchFn = local_defaultDecompress;
424         dbp.benchPayload = dctx;
425         dbp.initFn = local_initDCtx;
426         dbp.initPayload = &dctxprep;
427         dbp.errorFn = ZSTD_isError;
428         dbp.blockCount = nbBlocks;
429         dbp.srcBuffers = (const void* const *) cPtrs;
430         dbp.srcSizes = cSizes;
431         dbp.dstBuffers = resPtrs;
432         dbp.dstCapacities = resSizes;
433         dbp.blockResults = NULL;
434 
435         dctxprep.dctx = dctx;
436         dctxprep.dictBuffer = dictBuffer;
437         dctxprep.dictBufferSize = dictBufferSize;
438 
439         DISPLAYLEVEL(2, "\r%70s\r", "");   /* blank line */
440         DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (unsigned)srcSize);
441 
442         while (!(compressionCompleted && decompressionCompleted)) {
443             if (!compressionCompleted) {
444                 BMK_runOutcome_t const cOutcome = BMK_benchTimedFn( timeStateCompress, cbp);
445 
446                 if (!BMK_isSuccessful_runOutcome(cOutcome)) {
447                     return BMK_benchOutcome_error();
448                 }
449 
450                 {   BMK_runTime_t const cResult = BMK_extract_runTime(cOutcome);
451                     cSize = cResult.sumOfReturn;
452                     ratio = (double)srcSize / cSize;
453                     {   BMK_benchResult_t newResult;
454                         newResult.cSpeed = (U64)((double)srcSize * TIMELOOP_NANOSEC / cResult.nanoSecPerRun);
455                         benchResult.cSize = cSize;
456                         if (newResult.cSpeed > benchResult.cSpeed)
457                             benchResult.cSpeed = newResult.cSpeed;
458                 }   }
459 
460                 {   int const ratioAccuracy = (ratio < 10.) ? 3 : 2;
461                     DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.*f),%6.*f MB/s\r",
462                             marks[markNb], displayName,
463                             (unsigned)srcSize, (unsigned)cSize,
464                             ratioAccuracy, ratio,
465                             benchResult.cSpeed < (10 MB) ? 2 : 1, (double)benchResult.cSpeed / MB_UNIT);
466                 }
467                 compressionCompleted = BMK_isCompleted_TimedFn(timeStateCompress);
468             }
469 
470             if(!decompressionCompleted) {
471                 BMK_runOutcome_t const dOutcome = BMK_benchTimedFn(timeStateDecompress, dbp);
472 
473                 if(!BMK_isSuccessful_runOutcome(dOutcome)) {
474                     return BMK_benchOutcome_error();
475                 }
476 
477                 {   BMK_runTime_t const dResult = BMK_extract_runTime(dOutcome);
478                     U64 const newDSpeed = (U64)((double)srcSize * TIMELOOP_NANOSEC / dResult.nanoSecPerRun);
479                     if (newDSpeed > benchResult.dSpeed)
480                         benchResult.dSpeed = newDSpeed;
481                 }
482 
483                 {   int const ratioAccuracy = (ratio < 10.) ? 3 : 2;
484                     DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.*f),%6.*f MB/s ,%6.1f MB/s \r",
485                             marks[markNb], displayName,
486                             (unsigned)srcSize, (unsigned)benchResult.cSize,
487                             ratioAccuracy, ratio,
488                             benchResult.cSpeed < (10 MB) ? 2 : 1, (double)benchResult.cSpeed / MB_UNIT,
489                             (double)benchResult.dSpeed / MB_UNIT);
490                 }
491                 decompressionCompleted = BMK_isCompleted_TimedFn(timeStateDecompress);
492             }
493             markNb = (markNb+1) % NB_MARKS;
494         }   /* while (!(compressionCompleted && decompressionCompleted)) */
495 
496         /* CRC Checking */
497         {   const BYTE* resultBuffer = (const BYTE*)(*resultBufferPtr);
498             U64 const crcCheck = XXH64(resultBuffer, srcSize, 0);
499             if ((adv->mode == BMK_both) && (crcOrig!=crcCheck)) {
500                 size_t u;
501                 DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x   \n",
502                         displayName, (unsigned)crcOrig, (unsigned)crcCheck);
503                 for (u=0; u<srcSize; u++) {
504                     if (((const BYTE*)srcBuffer)[u] != resultBuffer[u]) {
505                         unsigned segNb, bNb, pos;
506                         size_t bacc = 0;
507                         DISPLAY("Decoding error at pos %u ", (unsigned)u);
508                         for (segNb = 0; segNb < nbBlocks; segNb++) {
509                             if (bacc + srcSizes[segNb] > u) break;
510                             bacc += srcSizes[segNb];
511                         }
512                         pos = (U32)(u - bacc);
513                         bNb = pos / (128 KB);
514                         DISPLAY("(sample %u, block %u, pos %u) \n", segNb, bNb, pos);
515                         {   size_t const lowest = (u>5) ? 5 : u;
516                             size_t n;
517                             DISPLAY("origin: ");
518                             for (n=lowest; n>0; n--)
519                                 DISPLAY("%02X ", ((const BYTE*)srcBuffer)[u-n]);
520                             DISPLAY(" :%02X:  ", ((const BYTE*)srcBuffer)[u]);
521                             for (n=1; n<3; n++)
522                                 DISPLAY("%02X ", ((const BYTE*)srcBuffer)[u+n]);
523                             DISPLAY(" \n");
524                             DISPLAY("decode: ");
525                             for (n=lowest; n>0; n++)
526                                 DISPLAY("%02X ", resultBuffer[u-n]);
527                             DISPLAY(" :%02X:  ", resultBuffer[u]);
528                             for (n=1; n<3; n++)
529                                 DISPLAY("%02X ", resultBuffer[u+n]);
530                             DISPLAY(" \n");
531                         }
532                         break;
533                     }
534                     if (u==srcSize-1) {  /* should never happen */
535                         DISPLAY("no difference detected\n");
536                     }
537                 }   /* for (u=0; u<srcSize; u++) */
538             }   /* if ((adv->mode == BMK_both) && (crcOrig!=crcCheck)) */
539         }   /* CRC Checking */
540 
541         if (displayLevel == 1) {   /* hidden display mode -q, used by python speed benchmark */
542             double const cSpeed = (double)benchResult.cSpeed / MB_UNIT;
543             double const dSpeed = (double)benchResult.dSpeed / MB_UNIT;
544             if (adv->additionalParam) {
545                 DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s  %s (param=%d)\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName, adv->additionalParam);
546             } else {
547                 DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s  %s\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName);
548             }
549         }
550 
551         DISPLAYLEVEL(2, "%2i#\n", cLevel);
552     }   /* Bench */
553 
554     benchResult.cMem = (1ULL << (comprParams->windowLog)) + ZSTD_sizeof_CCtx(cctx);
555     return BMK_benchOutcome_setValidResult(benchResult);
556 }
557 
558 BMK_benchOutcome_t BMK_benchMemAdvanced(const void* srcBuffer, size_t srcSize,
559                         void* dstBuffer, size_t dstCapacity,
560                         const size_t* fileSizes, unsigned nbFiles,
561                         int cLevel, const ZSTD_compressionParameters* comprParams,
562                         const void* dictBuffer, size_t dictBufferSize,
563                         int displayLevel, const char* displayName, const BMK_advancedParams_t* adv)
564 
565 {
566     int const dstParamsError = !dstBuffer ^ !dstCapacity;  /* must be both NULL or none */
567 
568     size_t const blockSize = ((adv->blockSize>=32 && (adv->mode != BMK_decodeOnly)) ? adv->blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ;
569     U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
570 
571     /* these are the blockTable parameters, just split up */
572     const void ** const srcPtrs = (const void**)malloc(maxNbBlocks * sizeof(void*));
573     size_t* const srcSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
574 
575 
576     void ** const cPtrs = (void**)malloc(maxNbBlocks * sizeof(void*));
577     size_t* const cSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
578     size_t* const cCapacities = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
579 
580     void ** const resPtrs = (void**)malloc(maxNbBlocks * sizeof(void*));
581     size_t* const resSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
582 
583     BMK_timedFnState_t* timeStateCompress = BMK_createTimedFnState(adv->nbSeconds * 1000, BMK_RUNTEST_DEFAULT_MS);
584     BMK_timedFnState_t* timeStateDecompress = BMK_createTimedFnState(adv->nbSeconds * 1000, BMK_RUNTEST_DEFAULT_MS);
585 
586     ZSTD_CCtx* const cctx = ZSTD_createCCtx();
587     ZSTD_DCtx* const dctx = ZSTD_createDCtx();
588 
589     const size_t maxCompressedSize = dstCapacity ? dstCapacity : ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024);
590 
591     void* const internalDstBuffer = dstBuffer ? NULL : malloc(maxCompressedSize);
592     void* const compressedBuffer = dstBuffer ? dstBuffer : internalDstBuffer;
593 
594     BMK_benchOutcome_t outcome = BMK_benchOutcome_error();  /* error by default */
595 
596     void* resultBuffer = srcSize ? malloc(srcSize) : NULL;
597 
598     int allocationincomplete = !srcPtrs || !srcSizes || !cPtrs ||
599         !cSizes || !cCapacities || !resPtrs || !resSizes ||
600         !timeStateCompress || !timeStateDecompress ||
601         !cctx || !dctx ||
602         !compressedBuffer || !resultBuffer;
603 
604 
605     if (!allocationincomplete && !dstParamsError) {
606         outcome = BMK_benchMemAdvancedNoAlloc(srcPtrs, srcSizes,
607                                             cPtrs, cCapacities, cSizes,
608                                             resPtrs, resSizes,
609                                             &resultBuffer,
610                                             compressedBuffer, maxCompressedSize,
611                                             timeStateCompress, timeStateDecompress,
612                                             srcBuffer, srcSize,
613                                             fileSizes, nbFiles,
614                                             cLevel, comprParams,
615                                             dictBuffer, dictBufferSize,
616                                             cctx, dctx,
617                                             displayLevel, displayName, adv);
618     }
619 
620     /* clean up */
621     BMK_freeTimedFnState(timeStateCompress);
622     BMK_freeTimedFnState(timeStateDecompress);
623 
624     ZSTD_freeCCtx(cctx);
625     ZSTD_freeDCtx(dctx);
626 
627     free(internalDstBuffer);
628     free(resultBuffer);
629 
630     free((void*)srcPtrs);
631     free(srcSizes);
632     free(cPtrs);
633     free(cSizes);
634     free(cCapacities);
635     free(resPtrs);
636     free(resSizes);
637 
638     if(allocationincomplete) {
639         RETURN_ERROR(31, BMK_benchOutcome_t, "allocation error : not enough memory");
640     }
641 
642     if(dstParamsError) {
643         RETURN_ERROR(32, BMK_benchOutcome_t, "Dst parameters not coherent");
644     }
645     return outcome;
646 }
647 
648 BMK_benchOutcome_t BMK_benchMem(const void* srcBuffer, size_t srcSize,
649                         const size_t* fileSizes, unsigned nbFiles,
650                         int cLevel, const ZSTD_compressionParameters* comprParams,
651                         const void* dictBuffer, size_t dictBufferSize,
652                         int displayLevel, const char* displayName) {
653 
654     BMK_advancedParams_t const adv = BMK_initAdvancedParams();
655     return BMK_benchMemAdvanced(srcBuffer, srcSize,
656                                 NULL, 0,
657                                 fileSizes, nbFiles,
658                                 cLevel, comprParams,
659                                 dictBuffer, dictBufferSize,
660                                 displayLevel, displayName, &adv);
661 }
662 
663 static BMK_benchOutcome_t BMK_benchCLevel(const void* srcBuffer, size_t benchedSize,
664                             const size_t* fileSizes, unsigned nbFiles,
665                             int cLevel, const ZSTD_compressionParameters* comprParams,
666                             const void* dictBuffer, size_t dictBufferSize,
667                             int displayLevel, const char* displayName,
668                             BMK_advancedParams_t const * const adv)
669 {
670     const char* pch = strrchr(displayName, '\\'); /* Windows */
671     if (!pch) pch = strrchr(displayName, '/');    /* Linux */
672     if (pch) displayName = pch+1;
673 
674     if (adv->realTime) {
675         DISPLAYLEVEL(2, "Note : switching to real-time priority \n");
676         SET_REALTIME_PRIORITY;
677     }
678 
679     if (displayLevel == 1 && !adv->additionalParam)   /* --quiet mode */
680         DISPLAY("bench %s %s: input %u bytes, %u seconds, %u KB blocks\n",
681                 ZSTD_VERSION_STRING, ZSTD_GIT_COMMIT_STRING,
682                 (unsigned)benchedSize, adv->nbSeconds, (unsigned)(adv->blockSize>>10));
683 
684     return BMK_benchMemAdvanced(srcBuffer, benchedSize,
685                                 NULL, 0,
686                                 fileSizes, nbFiles,
687                                 cLevel, comprParams,
688                                 dictBuffer, dictBufferSize,
689                                 displayLevel, displayName, adv);
690 }
691 
692 BMK_benchOutcome_t BMK_syntheticTest(int cLevel, double compressibility,
693                           const ZSTD_compressionParameters* compressionParams,
694                           int displayLevel, const BMK_advancedParams_t* adv)
695 {
696     char name[20] = {0};
697     size_t const benchedSize = 10000000;
698     void* srcBuffer;
699     BMK_benchOutcome_t res;
700 
701     if (cLevel > ZSTD_maxCLevel()) {
702         RETURN_ERROR(15, BMK_benchOutcome_t, "Invalid Compression Level");
703     }
704 
705     /* Memory allocation */
706     srcBuffer = malloc(benchedSize);
707     if (!srcBuffer) RETURN_ERROR(21, BMK_benchOutcome_t, "not enough memory");
708 
709     /* Fill input buffer */
710     RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
711 
712     /* Bench */
713     snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
714     res = BMK_benchCLevel(srcBuffer, benchedSize,
715                     &benchedSize /* ? */, 1 /* ? */,
716                     cLevel, compressionParams,
717                     NULL, 0,  /* dictionary */
718                     displayLevel, name, adv);
719 
720     /* clean up */
721     free(srcBuffer);
722 
723     return res;
724 }
725 
726 
727 
728 static size_t BMK_findMaxMem(U64 requiredMem)
729 {
730     size_t const step = 64 MB;
731     BYTE* testmem = NULL;
732 
733     requiredMem = (((requiredMem >> 26) + 1) << 26);
734     requiredMem += step;
735     if (requiredMem > maxMemory) requiredMem = maxMemory;
736 
737     do {
738         testmem = (BYTE*)malloc((size_t)requiredMem);
739         requiredMem -= step;
740     } while (!testmem && requiredMem > 0);
741 
742     free(testmem);
743     return (size_t)(requiredMem);
744 }
745 
746 /*! BMK_loadFiles() :
747  *  Loads `buffer` with content of files listed within `fileNamesTable`.
748  *  At most, fills `buffer` entirely. */
749 static int BMK_loadFiles(void* buffer, size_t bufferSize,
750                          size_t* fileSizes,
751                          const char* const * fileNamesTable, unsigned nbFiles,
752                          int displayLevel)
753 {
754     size_t pos = 0, totalSize = 0;
755     unsigned n;
756     for (n=0; n<nbFiles; n++) {
757         U64 fileSize = UTIL_getFileSize(fileNamesTable[n]);  /* last file may be shortened */
758         if (UTIL_isDirectory(fileNamesTable[n])) {
759             DISPLAYLEVEL(2, "Ignoring %s directory...       \n", fileNamesTable[n]);
760             fileSizes[n] = 0;
761             continue;
762         }
763         if (fileSize == UTIL_FILESIZE_UNKNOWN) {
764             DISPLAYLEVEL(2, "Cannot evaluate size of %s, ignoring ... \n", fileNamesTable[n]);
765             fileSizes[n] = 0;
766             continue;
767         }
768         {   FILE* const f = fopen(fileNamesTable[n], "rb");
769             if (f==NULL) RETURN_ERROR_INT(10, "impossible to open file %s", fileNamesTable[n]);
770             DISPLAYUPDATE(2, "Loading %s...       \r", fileNamesTable[n]);
771             if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n;   /* buffer too small - stop after this file */
772             {   size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
773                 if (readSize != (size_t)fileSize) RETURN_ERROR_INT(11, "could not read %s", fileNamesTable[n]);
774                 pos += readSize;
775             }
776             fileSizes[n] = (size_t)fileSize;
777             totalSize += (size_t)fileSize;
778             fclose(f);
779     }   }
780 
781     if (totalSize == 0) RETURN_ERROR_INT(12, "no data to bench");
782     return 0;
783 }
784 
785 BMK_benchOutcome_t BMK_benchFilesAdvanced(
786                         const char* const * fileNamesTable, unsigned nbFiles,
787                         const char* dictFileName, int cLevel,
788                         const ZSTD_compressionParameters* compressionParams,
789                         int displayLevel, const BMK_advancedParams_t* adv)
790 {
791     void* srcBuffer = NULL;
792     size_t benchedSize;
793     void* dictBuffer = NULL;
794     size_t dictBufferSize = 0;
795     size_t* fileSizes = NULL;
796     BMK_benchOutcome_t res;
797     U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
798 
799     if (!nbFiles) {
800         RETURN_ERROR(14, BMK_benchOutcome_t, "No Files to Benchmark");
801     }
802 
803     if (cLevel > ZSTD_maxCLevel()) {
804         RETURN_ERROR(15, BMK_benchOutcome_t, "Invalid Compression Level");
805     }
806 
807     fileSizes = (size_t*)calloc(nbFiles, sizeof(size_t));
808     if (!fileSizes) RETURN_ERROR(12, BMK_benchOutcome_t, "not enough memory for fileSizes");
809 
810     /* Load dictionary */
811     if (dictFileName != NULL) {
812         U64 const dictFileSize = UTIL_getFileSize(dictFileName);
813         if (dictFileSize == UTIL_FILESIZE_UNKNOWN) {
814             DISPLAYLEVEL(1, "error loading %s : %s \n", dictFileName, strerror(errno));
815             free(fileSizes);
816             RETURN_ERROR(9, BMK_benchOutcome_t, "benchmark aborted");
817         }
818         if (dictFileSize > 64 MB) {
819             free(fileSizes);
820             RETURN_ERROR(10, BMK_benchOutcome_t, "dictionary file %s too large", dictFileName);
821         }
822         dictBufferSize = (size_t)dictFileSize;
823         dictBuffer = malloc(dictBufferSize);
824         if (dictBuffer==NULL) {
825             free(fileSizes);
826             RETURN_ERROR(11, BMK_benchOutcome_t, "not enough memory for dictionary (%u bytes)",
827                             (unsigned)dictBufferSize);
828         }
829 
830         {   int const errorCode = BMK_loadFiles(dictBuffer, dictBufferSize,
831                                                 fileSizes, &dictFileName /*?*/,
832                                                 1 /*?*/, displayLevel);
833             if (errorCode) {
834                 res = BMK_benchOutcome_error();
835                 goto _cleanUp;
836         }   }
837     }
838 
839     /* Memory allocation & restrictions */
840     benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
841     if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
842     if (benchedSize < totalSizeToLoad)
843         DISPLAY("Not enough memory; testing %u MB only...\n", (unsigned)(benchedSize >> 20));
844 
845     srcBuffer = benchedSize ? malloc(benchedSize) : NULL;
846     if (!srcBuffer) {
847         free(dictBuffer);
848         free(fileSizes);
849         RETURN_ERROR(12, BMK_benchOutcome_t, "not enough memory");
850     }
851 
852     /* Load input buffer */
853     {   int const errorCode = BMK_loadFiles(srcBuffer, benchedSize,
854                                         fileSizes, fileNamesTable, nbFiles,
855                                         displayLevel);
856         if (errorCode) {
857             res = BMK_benchOutcome_error();
858             goto _cleanUp;
859     }   }
860 
861     /* Bench */
862     {   char mfName[20] = {0};
863         snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
864         {   const char* const displayName = (nbFiles > 1) ? mfName : fileNamesTable[0];
865             res = BMK_benchCLevel(srcBuffer, benchedSize,
866                                 fileSizes, nbFiles,
867                                 cLevel, compressionParams,
868                                 dictBuffer, dictBufferSize,
869                                 displayLevel, displayName,
870                                 adv);
871     }   }
872 
873 _cleanUp:
874     free(srcBuffer);
875     free(dictBuffer);
876     free(fileSizes);
877     return res;
878 }
879 
880 
881 BMK_benchOutcome_t BMK_benchFiles(
882                     const char* const * fileNamesTable, unsigned nbFiles,
883                     const char* dictFileName,
884                     int cLevel, const ZSTD_compressionParameters* compressionParams,
885                     int displayLevel)
886 {
887     BMK_advancedParams_t const adv = BMK_initAdvancedParams();
888     return BMK_benchFilesAdvanced(fileNamesTable, nbFiles, dictFileName, cLevel, compressionParams, displayLevel, &adv);
889 }
890