2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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.
11 /* **************************************
13 ****************************************/
14 #ifndef BMK_TIMETEST_DEFAULT_S /* default minimum time per test */
15 # define BMK_TIMETEST_DEFAULT_S 3
18 /* *************************************
20 ***************************************/
21 /* this must be included first */
22 #include "platform.h" /* Large Files support, compiler specifics */
24 /* then following system includes */
25 #include <assert.h> /* assert */
27 #include <stdio.h> /* fprintf, fopen */
28 #include <stdlib.h> /* malloc, free */
29 #include <string.h> /* memset, strerror */
30 #include "util.h" /* UTIL_getFileSize, UTIL_sleep */
31 #include "../lib/common/mem.h"
33 #include "timefn.h" /* UTIL_time_t */
34 #ifndef ZSTD_STATIC_LINKING_ONLY
35 # define ZSTD_STATIC_LINKING_ONLY
37 #include "../lib/zstd.h"
38 #include "datagen.h" /* RDG_genBuffer */
39 #include "lorem.h" /* LOREM_genBuffer */
40 #ifndef XXH_INLINE_ALL
41 # define XXH_INLINE_ALL
43 #include "../lib/common/xxhash.h"
44 #include "../lib/zstd_errors.h"
45 #include "benchzstd.h"
47 /* *************************************
49 ***************************************/
50 #ifndef ZSTD_GIT_COMMIT
51 # define ZSTD_GIT_COMMIT_STRING ""
53 # define ZSTD_GIT_COMMIT_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_GIT_COMMIT)
56 #define TIMELOOP_MICROSEC (1 * 1000000ULL) /* 1 second */
57 #define TIMELOOP_NANOSEC (1 * 1000000000ULL) /* 1 second */
58 #define ACTIVEPERIOD_MICROSEC (70 * TIMELOOP_MICROSEC) /* 70 seconds */
59 #define COOLPERIOD_SEC 10
63 #define GB *(1U << 30)
65 #define BMK_RUNTEST_DEFAULT_MS 1000
67 static const size_t maxMemory = (sizeof(size_t) == 4)
69 /* 32-bit */ (2 GB - 64 MB)
71 /* 64-bit */ (size_t)(1ULL << ((sizeof(size_t) * 8) - 31));
73 /* *************************************
75 ***************************************/
76 #define DISPLAY(...) \
78 fprintf(stderr, __VA_ARGS__); \
81 #define DISPLAYLEVEL(l, ...) \
82 if (displayLevel >= l) { \
83 DISPLAY(__VA_ARGS__); \
85 /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : +
86 * progression; 4 : + information */
89 fprintf(stdout, __VA_ARGS__); \
92 #define OUTPUTLEVEL(l, ...) \
93 if (displayLevel >= l) { \
94 OUTPUT(__VA_ARGS__); \
97 /* *************************************
99 ***************************************/
103 #define DEBUGOUTPUT(...) \
106 DISPLAY(__VA_ARGS__); \
109 #define RETURN_ERROR_INT(errorNum, ...) \
111 DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
112 DISPLAYLEVEL(1, "Error %i : ", errorNum); \
113 DISPLAYLEVEL(1, __VA_ARGS__); \
114 DISPLAYLEVEL(1, " \n"); \
118 #define CHECK_Z(zf) \
120 size_t const zerr = zf; \
121 if (ZSTD_isError(zerr)) { \
122 DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
123 DISPLAY("Error : "); \
124 DISPLAY("%s failed : %s", #zf, ZSTD_getErrorName(zerr)); \
130 #define RETURN_ERROR(errorNum, retType, ...) \
133 memset(&r, 0, sizeof(retType)); \
134 DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
135 DISPLAYLEVEL(1, "Error %i : ", errorNum); \
136 DISPLAYLEVEL(1, __VA_ARGS__); \
137 DISPLAYLEVEL(1, " \n"); \
142 /* replacement for snprintf(), which is not supported by C89
143 * sprintf() would be the supported one, but it's labelled unsafe,
144 * so some modern static analyzer will flag it as such, making it unusable.
145 * formatString_u() replaces snprintf() for the specific case where there are only %u arguments */
146 static int formatString_u(char* buffer, size_t buffer_size, const char* formatString, unsigned int value)
150 assert(value <= 100);
152 for (i = 0; formatString[i] != '\0' && written < buffer_size - 1; ++i) {
153 if (formatString[i] != '%') {
154 buffer[written++] = formatString[i];
158 if (formatString[++i] == 'u') {
159 /* Handle single digit */
161 buffer[written++] = '0' + (char)value;
162 } else if (value < 100) {
163 /* Handle two digits */
164 if (written >= buffer_size - 2) {
165 return -1; /* buffer overflow */
167 buffer[written++] = '0' + (char)(value / 10);
168 buffer[written++] = '0' + (char)(value % 10);
170 if (written >= buffer_size - 3) {
171 return -1; /* buffer overflow */
173 buffer[written++] = '1';
174 buffer[written++] = '0';
175 buffer[written++] = '0';
177 } else if (formatString[i] == '%') { /* Check for escaped percent sign */
178 buffer[written++] = '%';
180 return -1; /* unsupported format */
184 if (written < buffer_size) {
185 buffer[written] = '\0';
187 buffer[0] = '\0'; /* Handle truncation */
193 /* *************************************
194 * Benchmark Parameters
195 ***************************************/
197 BMK_advancedParams_t BMK_initAdvancedParams(void)
199 BMK_advancedParams_t const res = {
201 BMK_TIMETEST_DEFAULT_S, /* nbSeconds */
203 0, /* targetCBlockSize */
206 0, /* additionalParam */
210 0, /* ldmBuckSizeLog */
211 0, /* ldmHashRateLog */
212 ZSTD_ps_auto, /* literalCompressionMode */
213 0 /* useRowMatchFinder */
218 /* ********************************************************
220 **********************************************************/
233 #define MIN(a, b) ((a) < (b) ? (a) : (b))
234 #define MAX(a, b) ((a) > (b) ? (a) : (b))
236 static void BMK_initCCtx(
238 const void* dictBuffer,
239 size_t dictBufferSize,
241 const ZSTD_compressionParameters* comprParams,
242 const BMK_advancedParams_t* adv)
244 ZSTD_CCtx_reset(ctx, ZSTD_reset_session_and_parameters);
245 if (adv->nbWorkers == 1) {
246 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_nbWorkers, 0));
248 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_nbWorkers, adv->nbWorkers));
250 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, cLevel));
251 CHECK_Z(ZSTD_CCtx_setParameter(
252 ctx, ZSTD_c_useRowMatchFinder, adv->useRowMatchFinder));
253 CHECK_Z(ZSTD_CCtx_setParameter(
254 ctx, ZSTD_c_enableLongDistanceMatching, adv->ldmFlag));
255 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmMinMatch, adv->ldmMinMatch));
256 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmHashLog, adv->ldmHashLog));
257 CHECK_Z(ZSTD_CCtx_setParameter(
258 ctx, ZSTD_c_ldmBucketSizeLog, adv->ldmBucketSizeLog));
259 CHECK_Z(ZSTD_CCtx_setParameter(
260 ctx, ZSTD_c_ldmHashRateLog, adv->ldmHashRateLog));
261 CHECK_Z(ZSTD_CCtx_setParameter(
262 ctx, ZSTD_c_windowLog, (int)comprParams->windowLog));
263 CHECK_Z(ZSTD_CCtx_setParameter(
264 ctx, ZSTD_c_hashLog, (int)comprParams->hashLog));
265 CHECK_Z(ZSTD_CCtx_setParameter(
266 ctx, ZSTD_c_chainLog, (int)comprParams->chainLog));
267 CHECK_Z(ZSTD_CCtx_setParameter(
268 ctx, ZSTD_c_searchLog, (int)comprParams->searchLog));
269 CHECK_Z(ZSTD_CCtx_setParameter(
270 ctx, ZSTD_c_minMatch, (int)comprParams->minMatch));
271 CHECK_Z(ZSTD_CCtx_setParameter(
272 ctx, ZSTD_c_targetLength, (int)comprParams->targetLength));
273 CHECK_Z(ZSTD_CCtx_setParameter(
275 ZSTD_c_literalCompressionMode,
276 (int)adv->literalCompressionMode));
277 CHECK_Z(ZSTD_CCtx_setParameter(
278 ctx, ZSTD_c_strategy, (int)comprParams->strategy));
279 CHECK_Z(ZSTD_CCtx_setParameter(
280 ctx, ZSTD_c_targetCBlockSize, (int)adv->targetCBlockSize));
281 CHECK_Z(ZSTD_CCtx_loadDictionary(ctx, dictBuffer, dictBufferSize));
285 BMK_initDCtx(ZSTD_DCtx* dctx, const void* dictBuffer, size_t dictBufferSize)
287 CHECK_Z(ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters));
288 CHECK_Z(ZSTD_DCtx_loadDictionary(dctx, dictBuffer, dictBufferSize));
293 const void* dictBuffer;
294 size_t dictBufferSize;
296 const ZSTD_compressionParameters* comprParams;
297 const BMK_advancedParams_t* adv;
300 static size_t local_initCCtx(void* payload)
302 BMK_initCCtxArgs* ag = (BMK_initCCtxArgs*)payload;
315 const void* dictBuffer;
316 size_t dictBufferSize;
319 static size_t local_initDCtx(void* payload)
321 BMK_initDCtxArgs* ag = (BMK_initDCtxArgs*)payload;
322 BMK_initDCtx(ag->dctx, ag->dictBuffer, ag->dictBufferSize);
326 /* `addArgs` is the context */
327 static size_t local_defaultCompress(
328 const void* srcBuffer,
334 ZSTD_CCtx* const cctx = (ZSTD_CCtx*)addArgs;
335 return ZSTD_compress2(cctx, dstBuffer, dstSize, srcBuffer, srcSize);
338 /* `addArgs` is the context */
339 static size_t local_defaultDecompress(
340 const void* srcBuffer,
346 size_t moreToFlush = 1;
347 ZSTD_DCtx* const dctx = (ZSTD_DCtx*)addArgs;
354 out.size = dstCapacity;
356 while (moreToFlush) {
357 if (out.pos == out.size) {
358 return (size_t)-ZSTD_error_dstSize_tooSmall;
360 moreToFlush = ZSTD_decompressStream(dctx, &out, &in);
361 if (ZSTD_isError(moreToFlush)) {
368 /* ================================================================= */
369 /* Benchmark Zstandard, mem-to-mem scenarios */
370 /* ================================================================= */
372 int BMK_isSuccessful_benchOutcome(BMK_benchOutcome_t outcome)
374 return outcome.tag == 0;
377 BMK_benchResult_t BMK_extract_benchResult(BMK_benchOutcome_t outcome)
379 assert(outcome.tag == 0);
380 return outcome.internal_never_use_directly;
383 static BMK_benchOutcome_t BMK_benchOutcome_error(void)
385 BMK_benchOutcome_t b;
386 memset(&b, 0, sizeof(b));
391 static BMK_benchOutcome_t BMK_benchOutcome_setValidResult(
392 BMK_benchResult_t result)
394 BMK_benchOutcome_t b;
396 b.internal_never_use_directly = result;
400 /* benchMem with no allocation */
401 static BMK_benchOutcome_t BMK_benchMemAdvancedNoAlloc(
402 const void** srcPtrs,
409 void** resultBufferPtr,
410 void* compressedBuffer,
411 size_t maxCompressedSize,
412 BMK_timedFnState_t* timeStateCompress,
413 BMK_timedFnState_t* timeStateDecompress,
415 const void* srcBuffer,
417 const size_t* fileSizes,
420 const ZSTD_compressionParameters* comprParams,
421 const void* dictBuffer,
422 size_t dictBufferSize,
426 const char* displayName,
427 const BMK_advancedParams_t* adv)
429 size_t const blockSize =
430 ((adv->blockSize >= 32 && (adv->mode != BMK_decodeOnly))
433 + (!srcSize); /* avoid div by 0 */
434 BMK_benchResult_t benchResult;
435 size_t const loadedCompressedSize = srcSize;
440 assert(cctx != NULL);
441 assert(dctx != NULL);
444 memset(&benchResult, 0, sizeof(benchResult));
445 if (strlen(displayName) > 17)
447 strlen(displayName) - 17; /* display last 17 characters */
448 if (adv->mode == BMK_decodeOnly) {
449 /* benchmark only decompression : source must be already compressed */
450 const char* srcPtr = (const char*)srcBuffer;
451 U64 totalDSize64 = 0;
453 for (fileNb = 0; fileNb < nbFiles; fileNb++) {
455 ZSTD_findDecompressedSize(srcPtr, fileSizes[fileNb]);
456 if (fSize64 == ZSTD_CONTENTSIZE_UNKNOWN) {
460 "Decompressed size cannot be determined: cannot benchmark");
462 if (fSize64 == ZSTD_CONTENTSIZE_ERROR) {
466 "Error while trying to assess decompressed size: data may be invalid");
468 totalDSize64 += fSize64;
469 srcPtr += fileSizes[fileNb];
472 size_t const decodedSize = (size_t)totalDSize64;
473 assert((U64)decodedSize == totalDSize64); /* check overflow */
474 free(*resultBufferPtr);
475 if (totalDSize64 > decodedSize) { /* size_t overflow */
479 "decompressed size is too large for local system");
481 *resultBufferPtr = malloc(decodedSize);
482 if (!(*resultBufferPtr)) {
486 "allocation error: not enough memory");
489 srcSize = decodedSize;
490 ratio = (double)srcSize / (double)cSize;
494 /* Init data blocks */
496 const char* srcPtr = (const char*)srcBuffer;
497 char* cPtr = (char*)compressedBuffer;
498 char* resPtr = (char*)(*resultBufferPtr);
500 for (nbBlocks = 0, fileNb = 0; fileNb < nbFiles; fileNb++) {
501 size_t remaining = fileSizes[fileNb];
502 U32 const nbBlocksforThisFile = (adv->mode == BMK_decodeOnly)
504 : (U32)((remaining + (blockSize - 1)) / blockSize);
505 U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
506 for (; nbBlocks < blockEnd; nbBlocks++) {
507 size_t const thisBlockSize = MIN(remaining, blockSize);
508 srcPtrs[nbBlocks] = srcPtr;
509 srcSizes[nbBlocks] = thisBlockSize;
510 cPtrs[nbBlocks] = cPtr;
511 cCapacities[nbBlocks] = (adv->mode == BMK_decodeOnly)
513 : ZSTD_compressBound(thisBlockSize);
514 resPtrs[nbBlocks] = resPtr;
515 resSizes[nbBlocks] = (adv->mode == BMK_decodeOnly)
516 ? (size_t)ZSTD_findDecompressedSize(
517 srcPtr, thisBlockSize)
519 srcPtr += thisBlockSize;
520 cPtr += cCapacities[nbBlocks];
521 resPtr += thisBlockSize;
522 remaining -= thisBlockSize;
523 if (adv->mode == BMK_decodeOnly) {
524 cSizes[nbBlocks] = thisBlockSize;
525 benchResult.cSize = thisBlockSize;
531 /* warming up `compressedBuffer` */
532 if (adv->mode == BMK_decodeOnly) {
533 memcpy(compressedBuffer, srcBuffer, loadedCompressedSize);
535 RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
538 if (!UTIL_support_MT_measurements() && adv->nbWorkers > 1) {
541 "Warning : time measurements may be incorrect in multithreading mode... \n")
546 U64 const crcOrig = (adv->mode == BMK_decodeOnly)
548 : XXH64(srcBuffer, srcSize, 0);
550 const char* marks[NB_MARKS] = { " |", " /", " =", " \\" };
552 int compressionCompleted = (adv->mode == BMK_decodeOnly);
553 int decompressionCompleted = (adv->mode == BMK_compressOnly);
554 BMK_benchParams_t cbp, dbp;
555 BMK_initCCtxArgs cctxprep;
556 BMK_initDCtxArgs dctxprep;
558 cbp.benchFn = local_defaultCompress; /* ZSTD_compress2 */
559 cbp.benchPayload = cctx;
560 cbp.initFn = local_initCCtx; /* BMK_initCCtx */
561 cbp.initPayload = &cctxprep;
562 cbp.errorFn = ZSTD_isError;
563 cbp.blockCount = nbBlocks;
564 cbp.srcBuffers = srcPtrs;
565 cbp.srcSizes = srcSizes;
566 cbp.dstBuffers = cPtrs;
567 cbp.dstCapacities = cCapacities;
568 cbp.blockResults = cSizes;
570 cctxprep.cctx = cctx;
571 cctxprep.dictBuffer = dictBuffer;
572 cctxprep.dictBufferSize = dictBufferSize;
573 cctxprep.cLevel = cLevel;
574 cctxprep.comprParams = comprParams;
577 dbp.benchFn = local_defaultDecompress;
578 dbp.benchPayload = dctx;
579 dbp.initFn = local_initDCtx;
580 dbp.initPayload = &dctxprep;
581 dbp.errorFn = ZSTD_isError;
582 dbp.blockCount = nbBlocks;
583 dbp.srcBuffers = (const void* const*)cPtrs;
584 dbp.srcSizes = cSizes;
585 dbp.dstBuffers = resPtrs;
586 dbp.dstCapacities = resSizes;
587 dbp.blockResults = NULL;
589 dctxprep.dctx = dctx;
590 dctxprep.dictBuffer = dictBuffer;
591 dctxprep.dictBufferSize = dictBufferSize;
593 OUTPUTLEVEL(2, "\r%70s\r", ""); /* blank line */
594 assert(srcSize < UINT_MAX);
597 "%2s-%-17.17s :%10u -> \r",
602 while (!(compressionCompleted && decompressionCompleted)) {
603 if (!compressionCompleted) {
604 BMK_runOutcome_t const cOutcome =
605 BMK_benchTimedFn(timeStateCompress, cbp);
607 if (!BMK_isSuccessful_runOutcome(cOutcome)) {
608 RETURN_ERROR(30, BMK_benchOutcome_t, "compression error");
612 BMK_runTime_t const cResult = BMK_extract_runTime(cOutcome);
613 cSize = cResult.sumOfReturn;
614 ratio = (double)srcSize / (double)cSize;
616 BMK_benchResult_t newResult;
618 (U64)((double)srcSize * TIMELOOP_NANOSEC
619 / cResult.nanoSecPerRun);
620 benchResult.cSize = cSize;
621 if (newResult.cSpeed > benchResult.cSpeed)
622 benchResult.cSpeed = newResult.cSpeed;
627 int const ratioAccuracy = (ratio < 10.) ? 3 : 2;
628 assert(cSize < UINT_MAX);
631 "%2s-%-17.17s :%10u ->%10u (x%5.*f), %6.*f MB/s \r",
638 benchResult.cSpeed < (10 * MB_UNIT) ? 2 : 1,
639 (double)benchResult.cSpeed / MB_UNIT);
641 compressionCompleted =
642 BMK_isCompleted_TimedFn(timeStateCompress);
645 if (!decompressionCompleted) {
646 BMK_runOutcome_t const dOutcome =
647 BMK_benchTimedFn(timeStateDecompress, dbp);
649 if (!BMK_isSuccessful_runOutcome(dOutcome)) {
650 RETURN_ERROR(30, BMK_benchOutcome_t, "decompression error");
654 BMK_runTime_t const dResult = BMK_extract_runTime(dOutcome);
655 U64 const newDSpeed =
656 (U64)((double)srcSize * TIMELOOP_NANOSEC
657 / dResult.nanoSecPerRun);
658 if (newDSpeed > benchResult.dSpeed)
659 benchResult.dSpeed = newDSpeed;
663 int const ratioAccuracy = (ratio < 10.) ? 3 : 2;
666 "%2s-%-17.17s :%10u ->%10u (x%5.*f), %6.*f MB/s, %6.1f MB/s\r",
673 benchResult.cSpeed < (10 * MB_UNIT) ? 2 : 1,
674 (double)benchResult.cSpeed / MB_UNIT,
675 (double)benchResult.dSpeed / MB_UNIT);
677 decompressionCompleted =
678 BMK_isCompleted_TimedFn(timeStateDecompress);
680 markNb = (markNb + 1) % NB_MARKS;
681 } /* while (!(compressionCompleted && decompressionCompleted)) */
685 const BYTE* resultBuffer = (const BYTE*)(*resultBufferPtr);
686 U64 const crcCheck = XXH64(resultBuffer, srcSize, 0);
687 if ((adv->mode == BMK_both) && (crcOrig != crcCheck)) {
689 DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n",
693 for (u = 0; u < srcSize; u++) {
694 if (((const BYTE*)srcBuffer)[u] != resultBuffer[u]) {
695 unsigned segNb, bNb, pos;
697 DISPLAY("Decoding error at pos %u ", (unsigned)u);
698 for (segNb = 0; segNb < nbBlocks; segNb++) {
699 if (bacc + srcSizes[segNb] > u)
701 bacc += srcSizes[segNb];
703 pos = (U32)(u - bacc);
704 bNb = pos / (128 KB);
705 DISPLAY("(sample %u, block %u, pos %u) \n",
710 size_t const lowest = (u > 5) ? 5 : u;
713 for (n = lowest; n > 0; n--)
715 ((const BYTE*)srcBuffer)[u - n]);
716 DISPLAY(" :%02X: ", ((const BYTE*)srcBuffer)[u]);
717 for (n = 1; n < 3; n++)
719 ((const BYTE*)srcBuffer)[u + n]);
722 for (n = lowest; n > 0; n--)
723 DISPLAY("%02X ", resultBuffer[u - n]);
724 DISPLAY(" :%02X: ", resultBuffer[u]);
725 for (n = 1; n < 3; n++)
726 DISPLAY("%02X ", resultBuffer[u + n]);
731 if (u == srcSize - 1) { /* should never happen */
732 DISPLAY("no difference detected\n");
734 } /* for (u=0; u<srcSize; u++) */
735 } /* if ((adv->mode == BMK_both) && (crcOrig!=crcCheck)) */
739 == 1) { /* hidden display mode -q, used by python speed benchmark */
740 double const cSpeed = (double)benchResult.cSpeed / MB_UNIT;
741 double const dSpeed = (double)benchResult.dSpeed / MB_UNIT;
742 if (adv->additionalParam) {
743 OUTPUT("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s (param=%d)\n",
750 adv->additionalParam);
752 OUTPUT("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n",
762 OUTPUTLEVEL(2, "%2i#\n", cLevel);
766 (1ULL << (comprParams->windowLog)) + ZSTD_sizeof_CCtx(cctx);
767 return BMK_benchOutcome_setValidResult(benchResult);
770 BMK_benchOutcome_t BMK_benchMemAdvanced(
771 const void* srcBuffer,
775 const size_t* fileSizes,
778 const ZSTD_compressionParameters* comprParams,
779 const void* dictBuffer,
780 size_t dictBufferSize,
782 const char* displayName,
783 const BMK_advancedParams_t* adv)
786 int const dstParamsError =
787 !dstBuffer ^ !dstCapacity; /* must be both NULL or none */
789 size_t const blockSize =
790 ((adv->blockSize >= 32 && (adv->mode != BMK_decodeOnly))
793 + (!srcSize) /* avoid div by 0 */;
794 U32 const maxNbBlocks =
795 (U32)((srcSize + (blockSize - 1)) / blockSize) + nbFiles;
797 /* these are the blockTable parameters, just split up */
798 const void** const srcPtrs =
799 (const void**)malloc(maxNbBlocks * sizeof(void*));
800 size_t* const srcSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
802 void** const cPtrs = (void**)malloc(maxNbBlocks * sizeof(void*));
803 size_t* const cSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
804 size_t* const cCapacities = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
806 void** const resPtrs = (void**)malloc(maxNbBlocks * sizeof(void*));
807 size_t* const resSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
809 BMK_timedFnState_t* timeStateCompress = BMK_createTimedFnState(
810 adv->nbSeconds * 1000, BMK_RUNTEST_DEFAULT_MS);
811 BMK_timedFnState_t* timeStateDecompress = BMK_createTimedFnState(
812 adv->nbSeconds * 1000, BMK_RUNTEST_DEFAULT_MS);
814 ZSTD_CCtx* const cctx = ZSTD_createCCtx();
815 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
817 const size_t maxCompressedSize = dstCapacity
819 : ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024);
821 void* const internalDstBuffer =
822 dstBuffer ? NULL : malloc(maxCompressedSize);
823 void* const compressedBuffer = dstBuffer ? dstBuffer : internalDstBuffer;
825 BMK_benchOutcome_t outcome =
826 BMK_benchOutcome_error(); /* error by default */
828 void* resultBuffer = srcSize ? malloc(srcSize) : NULL;
830 int const allocationincomplete = !srcPtrs || !srcSizes || !cPtrs || !cSizes
831 || !cCapacities || !resPtrs || !resSizes || !timeStateCompress
832 || !timeStateDecompress || !cctx || !dctx || !compressedBuffer
835 if (!allocationincomplete && !dstParamsError) {
836 outcome = BMK_benchMemAdvancedNoAlloc(
865 BMK_freeTimedFnState(timeStateCompress);
866 BMK_freeTimedFnState(timeStateDecompress);
871 free(internalDstBuffer);
874 free((void*)srcPtrs);
882 if (allocationincomplete) {
884 31, BMK_benchOutcome_t, "allocation error : not enough memory");
887 if (dstParamsError) {
888 RETURN_ERROR(32, BMK_benchOutcome_t, "Dst parameters not coherent");
893 BMK_benchOutcome_t BMK_benchMem(
894 const void* srcBuffer,
896 const size_t* fileSizes,
899 const ZSTD_compressionParameters* comprParams,
900 const void* dictBuffer,
901 size_t dictBufferSize,
903 const char* displayName)
905 BMK_advancedParams_t const adv = BMK_initAdvancedParams();
906 return BMK_benchMemAdvanced(
922 static BMK_benchOutcome_t BMK_benchCLevel(
923 const void* srcBuffer,
925 const size_t* fileSizes,
928 const ZSTD_compressionParameters* comprParams,
929 const void* dictBuffer,
930 size_t dictBufferSize,
932 const char* displayName,
933 BMK_advancedParams_t const* const adv)
935 const char* pch = strrchr(displayName, '\\'); /* Windows */
937 pch = strrchr(displayName, '/'); /* Linux */
939 displayName = pch + 1;
942 DISPLAYLEVEL(2, "Note : switching to real-time priority \n");
943 SET_REALTIME_PRIORITY;
946 if (displayLevel == 1 && !adv->additionalParam) /* --quiet mode */
947 OUTPUT("bench %s %s: input %u bytes, %u seconds, %u KB blocks\n",
949 ZSTD_GIT_COMMIT_STRING,
950 (unsigned)benchedSize,
952 (unsigned)(adv->blockSize >> 10));
954 return BMK_benchMemAdvanced(
970 int BMK_syntheticTest(
972 double compressibility,
973 const ZSTD_compressionParameters* compressionParams,
975 const BMK_advancedParams_t* adv)
977 char nameBuff[20] = { 0 };
978 const char* name = nameBuff;
979 size_t const benchedSize = adv->blockSize ? adv->blockSize : 10000000;
981 BMK_benchOutcome_t res;
983 if (cLevel > ZSTD_maxCLevel()) {
984 DISPLAYLEVEL(1, "Invalid Compression Level");
988 /* Memory allocation */
989 srcBuffer = malloc(benchedSize);
991 DISPLAYLEVEL(1, "allocation error : not enough memory");
995 /* Fill input buffer */
996 if (compressibility < 0.0) {
997 LOREM_genBuffer(srcBuffer, benchedSize, 0);
998 name = "Lorem ipsum";
1000 RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
1005 (unsigned)(compressibility * 100));
1009 res = BMK_benchCLevel(
1012 &benchedSize /* ? */,
1025 return !BMK_isSuccessful_benchOutcome(res);
1028 static size_t BMK_findMaxMem(U64 requiredMem)
1030 size_t const step = 64 MB;
1031 BYTE* testmem = NULL;
1033 requiredMem = (((requiredMem >> 26) + 1) << 26);
1034 requiredMem += step;
1035 if (requiredMem > maxMemory)
1036 requiredMem = maxMemory;
1039 testmem = (BYTE*)malloc((size_t)requiredMem);
1040 requiredMem -= step;
1041 } while (!testmem && requiredMem > 0);
1044 return (size_t)(requiredMem);
1047 /*! BMK_loadFiles() :
1048 * Loads `buffer` with content of files listed within `fileNamesTable`.
1049 * At most, fills `buffer` entirely. */
1050 static int BMK_loadFiles(
1054 const char* const* fileNamesTable,
1058 size_t pos = 0, totalSize = 0;
1060 for (n = 0; n < nbFiles; n++) {
1061 U64 fileSize = UTIL_getFileSize(
1062 fileNamesTable[n]); /* last file may be shortened */
1063 if (UTIL_isDirectory(fileNamesTable[n])) {
1065 2, "Ignoring %s directory... \n", fileNamesTable[n]);
1069 if (fileSize == UTIL_FILESIZE_UNKNOWN) {
1072 "Cannot evaluate size of %s, ignoring ... \n",
1078 FILE* const f = fopen(fileNamesTable[n], "rb");
1081 10, "impossible to open file %s", fileNamesTable[n]);
1082 OUTPUTLEVEL(2, "Loading %s... \r", fileNamesTable[n]);
1083 if (fileSize > bufferSize - pos)
1084 fileSize = bufferSize - pos,
1085 nbFiles = n; /* buffer too small - stop after this file */
1087 size_t const readSize =
1088 fread(((char*)buffer) + pos, 1, (size_t)fileSize, f);
1089 if (readSize != (size_t)fileSize)
1091 11, "could not read %s", fileNamesTable[n]);
1094 fileSizes[n] = (size_t)fileSize;
1095 totalSize += (size_t)fileSize;
1101 RETURN_ERROR_INT(12, "no data to bench");
1105 int BMK_benchFilesAdvanced(
1106 const char* const* fileNamesTable,
1108 const char* dictFileName,
1110 const ZSTD_compressionParameters* compressionParams,
1112 const BMK_advancedParams_t* adv)
1114 void* srcBuffer = NULL;
1116 void* dictBuffer = NULL;
1117 size_t dictBufferSize = 0;
1118 size_t* fileSizes = NULL;
1119 BMK_benchOutcome_t res;
1120 U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
1123 DISPLAYLEVEL(1, "No Files to Benchmark");
1127 if (cLevel > ZSTD_maxCLevel()) {
1128 DISPLAYLEVEL(1, "Invalid Compression Level");
1132 if (totalSizeToLoad == UTIL_FILESIZE_UNKNOWN) {
1133 DISPLAYLEVEL(1, "Error loading files");
1137 fileSizes = (size_t*)calloc(nbFiles, sizeof(size_t));
1139 DISPLAYLEVEL(1, "not enough memory for fileSizes");
1143 /* Load dictionary */
1144 if (dictFileName != NULL) {
1145 U64 const dictFileSize = UTIL_getFileSize(dictFileName);
1146 if (dictFileSize == UTIL_FILESIZE_UNKNOWN) {
1149 "error loading %s : %s \n",
1153 DISPLAYLEVEL(1, "benchmark aborted");
1156 if (dictFileSize > 64 MB) {
1158 DISPLAYLEVEL(1, "dictionary file %s too large", dictFileName);
1161 dictBufferSize = (size_t)dictFileSize;
1162 dictBuffer = malloc(dictBufferSize);
1163 if (dictBuffer == NULL) {
1167 "not enough memory for dictionary (%u bytes)",
1168 (unsigned)dictBufferSize);
1173 int const errorCode = BMK_loadFiles(
1177 &dictFileName /*?*/,
1181 res = BMK_benchOutcome_error();
1187 /* Memory allocation & restrictions */
1188 benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
1189 if ((U64)benchedSize > totalSizeToLoad)
1190 benchedSize = (size_t)totalSizeToLoad;
1191 if (benchedSize < totalSizeToLoad)
1192 DISPLAY("Not enough memory; testing %u MB only...\n",
1193 (unsigned)(benchedSize >> 20));
1195 srcBuffer = benchedSize ? malloc(benchedSize) : NULL;
1199 DISPLAYLEVEL(1, "not enough memory for srcBuffer");
1203 /* Load input buffer */
1205 int const errorCode = BMK_loadFiles(
1213 res = BMK_benchOutcome_error();
1220 char mfName[20] = { 0 };
1221 formatString_u(mfName, sizeof(mfName), " %u files", nbFiles);
1223 const char* const displayName =
1224 (nbFiles > 1) ? mfName : fileNamesTable[0];
1225 res = BMK_benchCLevel(
1244 return !BMK_isSuccessful_benchOutcome(res);
1248 const char* const* fileNamesTable,
1250 const char* dictFileName,
1252 const ZSTD_compressionParameters* compressionParams,
1255 BMK_advancedParams_t const adv = BMK_initAdvancedParams();
1256 return BMK_benchFilesAdvanced(