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.
12 /*_************************************
14 **************************************/
15 #define ZSTD_DISABLE_DEPRECATE_WARNINGS /* No deprecation warnings, we still bench some deprecated functions */
16 #include "util.h" /* Compiler options, UTIL_GetFileSize */
17 #include <stdlib.h> /* malloc */
18 #include <stdio.h> /* fprintf, fopen, ftello64 */
21 #include "timefn.h" /* UTIL_clockSpanNano, UTIL_getTime */
22 #include "mem.h" /* U32 */
23 #ifndef ZSTD_DLL_IMPORT
24 #include "zstd_internal.h" /* ZSTD_decodeSeqHeaders, ZSTD_blockHeaderSize, ZSTD_getcBlockSize, blockType_e, KB, MB */
25 #include "decompress/zstd_decompress_internal.h" /* ZSTD_DCtx struct */
30 typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
32 #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressBegin, ZSTD_compressContinue, etc. */
33 #include "zstd.h" /* ZSTD_versionString */
34 #include "util.h" /* time functions */
36 #include "benchfn.h" /* CustomBench */
37 #include "benchzstd.h" /* MB_UNIT */
40 /*_************************************
42 **************************************/
43 #define PROGRAM_DESCRIPTION "Zstandard speed analyzer"
44 #define AUTHOR "Yann Collet"
45 #define WELCOME_MESSAGE "*** %s %s %i-bits, by %s (%s) ***\n", PROGRAM_DESCRIPTION, ZSTD_versionString(), (int)(sizeof(void*)*8), AUTHOR, __DATE__
50 #define MAX_MEM (1984 MB)
52 #define DEFAULT_CLEVEL 1
54 #define COMPRESSIBILITY_DEFAULT 0.50
55 static const size_t kSampleSizeDefault = 10000000;
57 #define TIMELOOP_NANOSEC (1*1000000000ULL) /* 1 second */
60 /*_************************************
62 **************************************/
63 #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
65 #define CONTROL(c) { if (!(c)) { abort(); } } /* like assert(), but cannot be disabled */
67 /*_************************************
68 * Benchmark Parameters
69 **************************************/
70 static unsigned g_nbIterations = NBLOOPS;
73 /*_*******************************************************
75 *********************************************************/
76 static size_t BMK_findMaxMem(U64 requiredMem)
78 size_t const step = 64 MB;
81 requiredMem = (((requiredMem >> 26) + 1) << 26);
82 if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
86 testmem = malloc ((size_t)requiredMem);
91 return (size_t) requiredMem;
95 /*_*******************************************************
97 *********************************************************/
99 static ZSTD_CCtx* g_zcc = NULL;
102 local_ZSTD_compress(const void* src, size_t srcSize,
103 void* dst, size_t dstSize,
107 ZSTD_frameParameters f = { 1 /* contentSizeHeader*/, 0, 0 };
109 p.cParams = *(ZSTD_compressionParameters*)payload;
110 return ZSTD_compress_advanced (g_zcc, dst, dstSize, src, srcSize, NULL ,0, p);
114 local_ZSTD_compress_freshCCtx(const void* src, size_t srcSize,
115 void* dst, size_t dstSize,
119 ZSTD_frameParameters f = { 1 /* contentSizeHeader*/, 0, 0 };
121 p.cParams = *(ZSTD_compressionParameters*)payload;
122 if (g_zcc != NULL) ZSTD_freeCCtx(g_zcc);
123 g_zcc = ZSTD_createCCtx();
124 assert(g_zcc != NULL);
125 { size_t const r = ZSTD_compress_advanced (g_zcc, dst, dstSize, src, srcSize, NULL ,0, p);
126 ZSTD_freeCCtx(g_zcc);
132 static size_t g_cSize = 0;
133 static size_t local_ZSTD_decompress(const void* src, size_t srcSize,
134 void* dst, size_t dstSize,
137 (void)src; (void)srcSize;
138 return ZSTD_decompress(dst, dstSize, buff2, g_cSize);
141 static ZSTD_DCtx* g_zdc = NULL;
143 #ifndef ZSTD_DLL_IMPORT
147 } streaming_operation;
148 extern size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* ctx, const void* src, size_t srcSize, void* dst, size_t dstCapacity, const streaming_operation streaming);
149 static size_t local_ZSTD_decodeLiteralsBlock(const void* src, size_t srcSize, void* dst, size_t dstSize, void* buff2)
151 (void)src; (void)srcSize; (void)dst; (void)dstSize;
152 return ZSTD_decodeLiteralsBlock(g_zdc, buff2, g_cSize, dst, dstSize, not_streaming);
155 static size_t local_ZSTD_decodeSeqHeaders(const void* src, size_t srcSize, void* dst, size_t dstSize, void* buff2)
158 (void)src; (void)srcSize; (void)dst; (void)dstSize;
159 return ZSTD_decodeSeqHeaders(g_zdc, &nbSeq, buff2, g_cSize);
162 FORCE_NOINLINE size_t ZSTD_decodeLiteralsHeader(ZSTD_DCtx* dctx, void const* src, size_t srcSize)
164 RETURN_ERROR_IF(srcSize < MIN_CBLOCK_SIZE, corruption_detected, "");
166 BYTE const* istart = (BYTE const*)src;
167 symbolEncodingType_e const litEncType = (symbolEncodingType_e)(istart[0] & 3);
168 if (litEncType == set_compressed) {
169 RETURN_ERROR_IF(srcSize < 5, corruption_detected, "srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for case 3");
171 size_t lhSize, litSize, litCSize;
172 U32 const lhlCode = (istart[0] >> 2) & 3;
173 U32 const lhc = MEM_readLE32(istart);
174 int const flags = ZSTD_DCtx_get_bmi2(dctx) ? HUF_flags_bmi2 : 0;
177 case 0: case 1: default: /* note : default is impossible, since lhlCode into [0..3] */
178 /* 2 - 2 - 10 - 10 */
180 litSize = (lhc >> 4) & 0x3FF;
181 litCSize = (lhc >> 14) & 0x3FF;
184 /* 2 - 2 - 14 - 14 */
186 litSize = (lhc >> 4) & 0x3FFF;
187 litCSize = lhc >> 18;
190 /* 2 - 2 - 18 - 18 */
192 litSize = (lhc >> 4) & 0x3FFFF;
193 litCSize = (lhc >> 22) + ((size_t)istart[4] << 10);
196 RETURN_ERROR_IF(litSize > ZSTD_BLOCKSIZE_MAX, corruption_detected, "");
197 RETURN_ERROR_IF(litCSize + lhSize > srcSize, corruption_detected, "");
198 #ifndef HUF_FORCE_DECOMPRESS_X2
199 return HUF_readDTableX1_wksp(
200 dctx->entropy.hufTable,
201 istart+lhSize, litCSize,
202 dctx->workspace, sizeof(dctx->workspace),
205 return HUF_readDTableX2_wksp(
206 dctx->entropy.hufTable,
207 istart+lhSize, litCSize,
208 dctx->workspace, sizeof(dctx->workspace), flags);
216 static size_t local_ZSTD_decodeLiteralsHeader(const void* src, size_t srcSize, void* dst, size_t dstSize, void* buff2)
218 (void)dst, (void)dstSize, (void)src, (void)srcSize;
219 return ZSTD_decodeLiteralsHeader(g_zdc, buff2, g_cSize);
223 static ZSTD_CStream* g_cstream= NULL;
225 local_ZSTD_compressStream(const void* src, size_t srcSize,
226 void* dst, size_t dstCapacity,
229 ZSTD_outBuffer buffOut;
230 ZSTD_inBuffer buffIn;
232 ZSTD_frameParameters f = {1 /* contentSizeHeader*/, 0, 0};
234 p.cParams = *(ZSTD_compressionParameters*)payload;
235 ZSTD_initCStream_advanced(g_cstream, NULL, 0, p, ZSTD_CONTENTSIZE_UNKNOWN);
237 buffOut.size = dstCapacity;
240 buffIn.size = srcSize;
242 ZSTD_compressStream(g_cstream, &buffOut, &buffIn);
243 ZSTD_endStream(g_cstream, &buffOut);
248 local_ZSTD_compressStream_freshCCtx(const void* src, size_t srcSize,
249 void* dst, size_t dstCapacity,
252 if (g_cstream != NULL) ZSTD_freeCCtx(g_cstream);
253 g_cstream = ZSTD_createCCtx();
254 assert(g_cstream != NULL);
256 { size_t const r = local_ZSTD_compressStream(src, srcSize, dst, dstCapacity, payload);
257 ZSTD_freeCCtx(g_cstream);
264 local_ZSTD_compress2(const void* src, size_t srcSize,
265 void* dst, size_t dstCapacity,
269 return ZSTD_compress2(g_cstream, dst, dstCapacity, src, srcSize);
273 local_ZSTD_compressStream2_end(const void* src, size_t srcSize,
274 void* dst, size_t dstCapacity,
277 ZSTD_outBuffer buffOut;
278 ZSTD_inBuffer buffIn;
281 buffOut.size = dstCapacity;
284 buffIn.size = srcSize;
286 ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_end);
291 local_ZSTD_compressStream2_continue(const void* src, size_t srcSize,
292 void* dst, size_t dstCapacity,
295 ZSTD_outBuffer buffOut;
296 ZSTD_inBuffer buffIn;
299 buffOut.size = dstCapacity;
302 buffIn.size = srcSize;
304 ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
305 ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_end);
310 local_ZSTD_compress_generic_T2_end(const void* src, size_t srcSize,
311 void* dst, size_t dstCapacity,
315 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_nbWorkers, 2);
316 return ZSTD_compress2(g_cstream, dst, dstCapacity, src, srcSize);
320 local_ZSTD_compress_generic_T2_continue(const void* src, size_t srcSize,
321 void* dst, size_t dstCapacity,
324 ZSTD_outBuffer buffOut;
325 ZSTD_inBuffer buffIn;
327 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_nbWorkers, 2);
329 buffOut.size = dstCapacity;
332 buffIn.size = srcSize;
334 ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
335 while(ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_end)) {}
339 static ZSTD_DStream* g_dstream= NULL;
341 local_ZSTD_decompressStream(const void* src, size_t srcSize,
342 void* dst, size_t dstCapacity,
345 ZSTD_outBuffer buffOut;
346 ZSTD_inBuffer buffIn;
347 (void)src; (void)srcSize;
348 ZSTD_initDStream(g_dstream);
350 buffOut.size = dstCapacity;
353 buffIn.size = g_cSize;
355 ZSTD_decompressStream(g_dstream, &buffOut, &buffIn);
359 #ifndef ZSTD_DLL_IMPORT
360 static size_t local_ZSTD_compressContinue(const void* src, size_t srcSize,
361 void* dst, size_t dstCapacity,
365 ZSTD_frameParameters f = { 1 /* contentSizeHeader*/, 0, 0 };
367 p.cParams = *(ZSTD_compressionParameters*)payload;
368 ZSTD_compressBegin_advanced(g_zcc, NULL, 0, p, srcSize);
369 return ZSTD_compressEnd(g_zcc, dst, dstCapacity, src, srcSize);
372 #define FIRST_BLOCK_SIZE 8
374 local_ZSTD_compressContinue_extDict(const void* src, size_t srcSize,
375 void* dst, size_t dstCapacity,
378 BYTE firstBlockBuf[FIRST_BLOCK_SIZE];
381 ZSTD_frameParameters const f = { 1, 0, 0 };
383 p.cParams = *(ZSTD_compressionParameters*)payload;
384 ZSTD_compressBegin_advanced(g_zcc, NULL, 0, p, srcSize);
385 memcpy(firstBlockBuf, src, FIRST_BLOCK_SIZE);
387 { size_t const compressResult = ZSTD_compressContinue(g_zcc,
389 firstBlockBuf, FIRST_BLOCK_SIZE);
390 if (ZSTD_isError(compressResult)) {
391 DISPLAY("local_ZSTD_compressContinue_extDict error : %s\n",
392 ZSTD_getErrorName(compressResult));
393 return compressResult;
395 dst = (BYTE*)dst + compressResult;
396 dstCapacity -= compressResult;
398 return ZSTD_compressEnd(g_zcc, dst, dstCapacity,
399 (const BYTE*)src + FIRST_BLOCK_SIZE,
400 srcSize - FIRST_BLOCK_SIZE);
403 static size_t local_ZSTD_decompressContinue(const void* src, size_t srcSize,
404 void* dst, size_t dstCapacity,
407 size_t regeneratedSize = 0;
408 const BYTE* ip = (const BYTE*)buff2;
409 const BYTE* const iend = ip + g_cSize;
410 BYTE* op = (BYTE*)dst;
411 size_t remainingCapacity = dstCapacity;
413 (void)src; (void)srcSize; /* unused */
414 ZSTD_decompressBegin(g_zdc);
416 size_t const iSize = ZSTD_nextSrcSizeToDecompress(g_zdc);
417 size_t const decodedSize = ZSTD_decompressContinue(g_zdc, op, remainingCapacity, ip, iSize);
419 regeneratedSize += decodedSize;
421 remainingCapacity -= decodedSize;
424 return regeneratedSize;
429 /*_*******************************************************
431 *********************************************************/
432 static int benchMem(unsigned benchNb,
433 const void* src, size_t srcSize,
434 int cLevel, ZSTD_compressionParameters cparams)
436 size_t dstBuffSize = ZSTD_compressBound(srcSize);
440 const char* benchName;
441 BMK_benchFn_t benchFunction;
448 benchFunction = local_ZSTD_compress; benchName = "compress";
451 benchFunction = local_ZSTD_decompress; benchName = "decompress";
454 benchFunction = local_ZSTD_compress_freshCCtx; benchName = "compress_freshCCtx";
456 #ifndef ZSTD_DLL_IMPORT
458 benchFunction = local_ZSTD_compressContinue; benchName = "compressContinue";
461 benchFunction = local_ZSTD_compressContinue_extDict; benchName = "compressContinue_extDict";
464 benchFunction = local_ZSTD_decompressContinue; benchName = "decompressContinue";
467 benchFunction = local_ZSTD_decodeLiteralsHeader; benchName = "decodeLiteralsHeader";
470 benchFunction = local_ZSTD_decodeLiteralsBlock; benchName = "decodeLiteralsBlock";
473 benchFunction = local_ZSTD_decodeSeqHeaders; benchName = "decodeSeqHeaders";
477 benchFunction = local_ZSTD_compressStream; benchName = "compressStream";
480 benchFunction = local_ZSTD_decompressStream; benchName = "decompressStream";
483 benchFunction = local_ZSTD_compressStream_freshCCtx; benchName = "compressStream_freshCCtx";
486 benchFunction = local_ZSTD_compress2; benchName = "compress2";
489 benchFunction = local_ZSTD_compressStream2_end; benchName = "compressStream2, end";
492 benchFunction = local_ZSTD_compressStream2_end; benchName = "compressStream2, end & short";
495 benchFunction = local_ZSTD_compressStream2_continue; benchName = "compressStream2, continue";
498 benchFunction = local_ZSTD_compress_generic_T2_continue; benchName = "compress_generic, -T2, continue";
501 benchFunction = local_ZSTD_compress_generic_T2_end; benchName = "compress_generic, -T2, end";
508 dstBuff = (BYTE*)malloc(dstBuffSize);
509 dstBuff2 = malloc(dstBuffSize);
510 if ((!dstBuff) || (!dstBuff2)) {
511 DISPLAY("\nError: not enough memory!\n");
512 free(dstBuff); free(dstBuff2);
516 if (g_zcc==NULL) g_zcc = ZSTD_createCCtx();
517 if (g_zdc==NULL) g_zdc = ZSTD_createDCtx();
518 if (g_cstream==NULL) g_cstream = ZSTD_createCStream();
519 if (g_dstream==NULL) g_dstream = ZSTD_createDStream();
521 /* DISPLAY("params: cLevel %d, wlog %d hlog %d clog %d slog %d mml %d tlen %d strat %d \n",
522 cLevel, cparams->windowLog, cparams->hashLog, cparams->chainLog, cparams->searchLog,
523 cparams->minMatch, cparams->targetLength, cparams->strategy); */
525 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_compressionLevel, cLevel);
526 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_windowLog, (int)cparams.windowLog);
527 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_hashLog, (int)cparams.hashLog);
528 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_chainLog, (int)cparams.chainLog);
529 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_searchLog, (int)cparams.searchLog);
530 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_minMatch, (int)cparams.minMatch);
531 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_targetLength, (int)cparams.targetLength);
532 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_strategy, cparams.strategy);
534 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_compressionLevel, cLevel);
535 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_windowLog, (int)cparams.windowLog);
536 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_hashLog, (int)cparams.hashLog);
537 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_chainLog, (int)cparams.chainLog);
538 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_searchLog, (int)cparams.searchLog);
539 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_minMatch, (int)cparams.minMatch);
540 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_targetLength, (int)cparams.targetLength);
541 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_strategy, cparams.strategy);
550 g_cSize = ZSTD_compress(dstBuff2, dstBuffSize, src, srcSize, cLevel);
555 #ifndef ZSTD_DLL_IMPORT
563 g_cSize = ZSTD_compress(dstBuff2, dstBuffSize, src, srcSize, cLevel);
565 case 30: /* ZSTD_decodeLiteralsHeader */
567 case 31: /* ZSTD_decodeLiteralsBlock : starts literals block in dstBuff2 */
568 { size_t frameHeaderSize;
569 g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, cLevel);
570 frameHeaderSize = ZSTD_frameHeaderSize(dstBuff, ZSTD_FRAMEHEADERSIZE_PREFIX(ZSTD_f_zstd1));
571 CONTROL(!ZSTD_isError(frameHeaderSize));
572 /* check block is compressible, hence contains a literals section */
573 { blockProperties_t bp;
574 ZSTD_getcBlockSize(dstBuff+frameHeaderSize, dstBuffSize, &bp); /* Get 1st block type */
575 if (bp.blockType != bt_compressed) {
576 DISPLAY("ZSTD_decodeLiteralsBlock : impossible to test on this sample (not compressible)\n");
579 { size_t const skippedSize = frameHeaderSize + ZSTD_blockHeaderSize;
580 memcpy(dstBuff2, dstBuff+skippedSize, g_cSize-skippedSize);
582 srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */
583 ZSTD_decompressBegin(g_zdc);
586 case 32: /* ZSTD_decodeSeqHeaders */
587 { blockProperties_t bp;
588 const BYTE* ip = dstBuff;
590 { size_t const cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, cLevel);
591 CONTROL(cSize > ZSTD_FRAMEHEADERSIZE_PREFIX(ZSTD_f_zstd1));
593 /* Skip frame Header */
594 { size_t const frameHeaderSize = ZSTD_frameHeaderSize(dstBuff, ZSTD_FRAMEHEADERSIZE_PREFIX(ZSTD_f_zstd1));
595 CONTROL(!ZSTD_isError(frameHeaderSize));
596 ip += frameHeaderSize;
598 /* Find end of block */
599 { size_t const cBlockSize = ZSTD_getcBlockSize(ip, dstBuffSize, &bp); /* Get 1st block type */
600 if (bp.blockType != bt_compressed) {
601 DISPLAY("ZSTD_decodeSeqHeaders : impossible to test on this sample (not compressible)\n");
604 iend = ip + ZSTD_blockHeaderSize + cBlockSize; /* End of first block */
606 ip += ZSTD_blockHeaderSize; /* skip block header */
607 ZSTD_decompressBegin(g_zdc);
609 ip += ZSTD_decodeLiteralsBlock(g_zdc, ip, (size_t)(iend-ip), dstBuff, dstBuffSize, not_streaming); /* skip literal segment */
610 g_cSize = (size_t)(iend-ip);
611 memcpy(dstBuff2, ip, g_cSize); /* copy rest of block (it starts by SeqHeader) */
612 srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */
623 g_cSize = ZSTD_compress(payload, dstBuffSize, src, srcSize, cLevel);
630 /* compressStream2, short dstCapacity */
635 /* convention: test functions have ID > 100 */
640 /* warming up dstBuff */
641 { size_t i; for (i=0; i<dstBuffSize; i++) dstBuff[i]=(BYTE)i; }
644 { BMK_timedFnState_t* const tfs = BMK_createTimedFnState(g_nbIterations * 1000, 1000);
645 void* const avoidStrictAliasingPtr = &dstBuff;
646 BMK_benchParams_t bp;
647 BMK_runTime_t bestResult;
648 bestResult.sumOfReturn = 0;
649 bestResult.nanoSecPerRun = (double)TIMELOOP_NANOSEC * 2000000000; /* hopefully large enough : must be larger than any potential measurement */
650 CONTROL(tfs != NULL);
652 bp.benchFn = benchFunction;
653 bp.benchPayload = payload;
655 bp.initPayload = NULL;
656 bp.errorFn = ZSTD_isError;
658 bp.srcBuffers = &src;
659 bp.srcSizes = &srcSize;
660 bp.dstBuffers = (void* const*) avoidStrictAliasingPtr; /* circumvent strict aliasing warning on gcc-8,
661 * because gcc considers that `void* const *` and `void**` are 2 different types */
662 bp.dstCapacities = &dstBuffSize;
663 bp.blockResults = NULL;
666 BMK_runOutcome_t const bOutcome = BMK_benchTimedFn(tfs, bp);
668 if (!BMK_isSuccessful_runOutcome(bOutcome)) {
669 DISPLAY("ERROR benchmarking function ! ! \n");
674 { BMK_runTime_t const newResult = BMK_extract_runTime(bOutcome);
675 if (newResult.nanoSecPerRun < bestResult.nanoSecPerRun )
676 bestResult.nanoSecPerRun = newResult.nanoSecPerRun;
677 DISPLAY("\r%2u#%-29.29s:%8.1f MB/s (%8u) ",
679 (double)srcSize * TIMELOOP_NANOSEC / bestResult.nanoSecPerRun / MB_UNIT,
680 (unsigned)newResult.sumOfReturn );
683 if ( BMK_isCompleted_TimedFn(tfs) ) break;
685 BMK_freeTimedFnState(tfs);
692 ZSTD_freeCCtx(g_zcc); g_zcc=NULL;
693 ZSTD_freeDCtx(g_zdc); g_zdc=NULL;
694 ZSTD_freeCStream(g_cstream); g_cstream=NULL;
695 ZSTD_freeDStream(g_dstream); g_dstream=NULL;
700 static int benchSample(U32 benchNb,
701 size_t benchedSize, double compressibility,
702 int cLevel, ZSTD_compressionParameters cparams)
705 void* const origBuff = malloc(benchedSize);
706 if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); return 12; }
709 RDG_genBuffer(origBuff, benchedSize, compressibility, 0.0, 0);
712 DISPLAY("\r%70s\r", "");
713 DISPLAY(" Sample %u bytes : \n", (unsigned)benchedSize);
715 benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
716 } else { /* 0 == run all tests */
717 for (benchNb=0; benchNb<100; benchNb++) {
718 benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
726 static int benchFiles(U32 benchNb,
727 const char** fileNamesTable, const int nbFiles,
728 int cLevel, ZSTD_compressionParameters cparams)
730 /* Loop for each file */
732 for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
733 const char* const inFileName = fileNamesTable[fileIdx];
734 FILE* const inFile = fopen( inFileName, "rb" );
737 /* Check file existence */
738 if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
740 /* Memory allocation & restrictions */
741 { U64 const inFileSize = UTIL_getFileSize(inFileName);
742 if (inFileSize == UTIL_FILESIZE_UNKNOWN) {
743 DISPLAY( "Cannot measure size of %s\n", inFileName);
747 benchedSize = BMK_findMaxMem(inFileSize*3) / 3;
748 if ((U64)benchedSize > inFileSize)
749 benchedSize = (size_t)inFileSize;
750 if ((U64)benchedSize < inFileSize) {
751 DISPLAY("Not enough memory for '%s' full size; testing %u MB only... \n",
752 inFileName, (unsigned)(benchedSize>>20));
756 { void* const origBuff = malloc(benchedSize);
757 if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); fclose(inFile); return 12; }
759 /* Fill input buffer */
760 DISPLAY("Loading %s... \r", inFileName);
761 { size_t const readSize = fread(origBuff, 1, benchedSize, inFile);
763 if (readSize != benchedSize) {
764 DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
770 DISPLAY("\r%70s\r", ""); /* blank line */
771 DISPLAY(" %s : \n", inFileName);
773 benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
775 for (benchNb=0; benchNb<100; benchNb++) {
776 benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
789 /*_*******************************************************
791 *********************************************************/
793 #define ERROR_OUT(msg) { DISPLAY("%s \n", msg); exit(1); }
795 static unsigned readU32FromChar(const char** stringPtr)
797 const char errorMsg[] = "error: numeric value too large";
799 while ((**stringPtr >='0') && (**stringPtr <='9')) {
800 unsigned const max = (((unsigned)(-1)) / 10) - 1;
801 if (result > max) ERROR_OUT(errorMsg);
803 result += (unsigned)(**stringPtr - '0');
806 if ((**stringPtr=='K') || (**stringPtr=='M')) {
807 unsigned const maxK = ((unsigned)(-1)) >> 10;
808 if (result > maxK) ERROR_OUT(errorMsg);
810 if (**stringPtr=='M') {
811 if (result > maxK) ERROR_OUT(errorMsg);
814 (*stringPtr)++; /* skip `K` or `M` */
815 if (**stringPtr=='i') (*stringPtr)++;
816 if (**stringPtr=='B') (*stringPtr)++;
821 static int longCommandWArg(const char** stringPtr, const char* longCommand)
823 size_t const comSize = strlen(longCommand);
824 int const result = !strncmp(*stringPtr, longCommand, comSize);
825 if (result) *stringPtr += comSize;
830 /*_*******************************************************
832 *********************************************************/
834 static int usage(const char* exename)
836 DISPLAY( "Usage :\n");
837 DISPLAY( " %s [arg] file1 file2 ... fileX\n", exename);
838 DISPLAY( "Arguments :\n");
839 DISPLAY( " -H/-h : Help (this text + advanced options)\n");
843 static int usage_advanced(const char* exename)
846 DISPLAY( "\nAdvanced options :\n");
847 DISPLAY( " -b# : test only function # \n");
848 DISPLAY( " -l# : benchmark functions at that compression level (default : %i)\n", DEFAULT_CLEVEL);
849 DISPLAY( "--zstd= : custom parameter selection. Format same as zstdcli \n");
850 DISPLAY( " -P# : sample compressibility (default : %.1f%%)\n", COMPRESSIBILITY_DEFAULT * 100);
851 DISPLAY( " -B# : sample size (default : %u)\n", (unsigned)kSampleSizeDefault);
852 DISPLAY( " -i# : iteration loops [1-9](default : %i)\n", NBLOOPS);
856 static int badusage(const char* exename)
858 DISPLAY("Wrong parameters\n");
863 int main(int argc, const char** argv)
865 int argNb, filenamesStart=0, result;
866 const char* const exename = argv[0];
867 const char* input_filename = NULL;
868 U32 benchNb = 0, main_pause = 0;
869 int cLevel = DEFAULT_CLEVEL;
870 ZSTD_compressionParameters cparams = ZSTD_getCParams(cLevel, 0, 0);
871 size_t sampleSize = kSampleSizeDefault;
872 double compressibility = COMPRESSIBILITY_DEFAULT;
874 DISPLAY(WELCOME_MESSAGE);
875 if (argc<1) return badusage(exename);
877 for (argNb=1; argNb<argc; argNb++) {
878 const char* argument = argv[argNb];
879 CONTROL(argument != NULL);
881 if (longCommandWArg(&argument, "--zstd=")) {
883 if (longCommandWArg(&argument, "windowLog=") || longCommandWArg(&argument, "wlog=")) { cparams.windowLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
884 if (longCommandWArg(&argument, "chainLog=") || longCommandWArg(&argument, "clog=")) { cparams.chainLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
885 if (longCommandWArg(&argument, "hashLog=") || longCommandWArg(&argument, "hlog=")) { cparams.hashLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
886 if (longCommandWArg(&argument, "searchLog=") || longCommandWArg(&argument, "slog=")) { cparams.searchLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
887 if (longCommandWArg(&argument, "minMatch=") || longCommandWArg(&argument, "mml=")) { cparams.minMatch = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
888 if (longCommandWArg(&argument, "targetLength=") || longCommandWArg(&argument, "tlen=")) { cparams.targetLength = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
889 if (longCommandWArg(&argument, "strategy=") || longCommandWArg(&argument, "strat=")) { cparams.strategy = (ZSTD_strategy)(readU32FromChar(&argument)); if (argument[0]==',') { argument++; continue; } else break; }
890 if (longCommandWArg(&argument, "level=") || longCommandWArg(&argument, "lvl=")) { cLevel = (int)readU32FromChar(&argument); cparams = ZSTD_getCParams(cLevel, 0, 0); if (argument[0]==',') { argument++; continue; } else break; }
891 DISPLAY("invalid compression parameter \n");
895 /* check end of string */
896 if (argument[0] != 0) {
897 DISPLAY("invalid --zstd= format \n");
903 } else if (argument[0]=='-') { /* Commands (note : aggregated commands are allowed) */
905 while (argument[0]!=0) {
909 /* Display help on usage */
911 case 'H': return usage_advanced(exename);
913 /* Pause at the end (hidden option) */
914 case 'p': main_pause = 1; break;
916 /* Select specific algorithm to bench */
919 benchNb = readU32FromChar(&argument);
922 /* Select compression level to use */
925 cLevel = (int)readU32FromChar(&argument);
926 cparams = ZSTD_getCParams(cLevel, 0, 0);
929 /* Select compressibility of synthetic sample */
932 compressibility = (double)readU32FromChar(&argument) / 100.;
935 /* Select size of synthetic sample */
938 sampleSize = (size_t)readU32FromChar(&argument);
941 /* Modify Nb Iterations */
944 g_nbIterations = readU32FromChar(&argument);
947 /* Unknown command */
948 default : return badusage(exename);
954 /* first provided filename is input */
955 if (!input_filename) { input_filename=argument; filenamesStart=argNb; continue; }
960 if (filenamesStart==0) /* no input file */
961 result = benchSample(benchNb, sampleSize, compressibility, cLevel, cparams);
963 result = benchFiles(benchNb, argv+filenamesStart, argc-filenamesStart, cLevel, cparams);
965 if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; }