2 * Copyright (c) 2015-present, Yann Collet, Facebook, Inc.
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 #include "util.h" /* Compiler options, UTIL_GetFileSize */
16 #include <stdlib.h> /* malloc */
17 #include <stdio.h> /* fprintf, fopen, ftello64 */
18 #include <assert.h> /* assert */
20 #include "timefn.h" /* UTIL_clockSpanNano, UTIL_getTime */
21 #include "mem.h" /* U32 */
22 #ifndef ZSTD_DLL_IMPORT
23 #include "zstd_internal.h" /* ZSTD_decodeSeqHeaders, ZSTD_blockHeaderSize, blockType_e, KB, MB */
28 typedef enum { bt_raw
, bt_rle
, bt_compressed
, bt_reserved
} blockType_e
;
30 #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressBegin, ZSTD_compressContinue, etc. */
31 #include "zstd.h" /* ZSTD_versionString */
32 #include "util.h" /* time functions */
34 #include "benchfn.h" /* CustomBench*/
35 #include "benchzstd.h" /* MB_UNIT */
38 /*_************************************
40 **************************************/
41 #define PROGRAM_DESCRIPTION "Zstandard speed analyzer"
42 #define AUTHOR "Yann Collet"
43 #define WELCOME_MESSAGE "*** %s %s %i-bits, by %s (%s) ***\n", PROGRAM_DESCRIPTION, ZSTD_versionString(), (int)(sizeof(void*)*8), AUTHOR, __DATE__
48 #define KNUTH 2654435761U
49 #define MAX_MEM (1984 MB)
51 #define DEFAULT_CLEVEL 1
53 #define COMPRESSIBILITY_DEFAULT 0.50
54 static const size_t g_sampleSize
= 10000000;
56 #define TIMELOOP_NANOSEC (1*1000000000ULL) /* 1 second */
59 /*_************************************
61 **************************************/
62 #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
65 /*_************************************
66 * Benchmark Parameters
67 **************************************/
68 static unsigned g_nbIterations
= NBLOOPS
;
69 static double g_compressibility
= COMPRESSIBILITY_DEFAULT
;
72 /*_*******************************************************
74 *********************************************************/
75 static size_t BMK_findMaxMem(U64 requiredMem
)
77 size_t const step
= 64 MB
;
80 requiredMem
= (((requiredMem
>> 26) + 1) << 26);
81 if (requiredMem
> MAX_MEM
) requiredMem
= MAX_MEM
;
85 testmem
= malloc ((size_t)requiredMem
);
90 return (size_t) requiredMem
;
94 /*_*******************************************************
96 *********************************************************/
98 static ZSTD_CCtx
* g_zcc
= NULL
;
101 local_ZSTD_compress(const void* src
, size_t srcSize
,
102 void* dst
, size_t dstSize
,
106 ZSTD_frameParameters f
= { 1 /* contentSizeHeader*/, 0, 0 };
108 p
.cParams
= *(ZSTD_compressionParameters
*)buff2
;
109 return ZSTD_compress_advanced (g_zcc
, dst
, dstSize
, src
, srcSize
, NULL
,0, p
);
110 //return ZSTD_compress(dst, dstSize, src, srcSize, cLevel);
113 static size_t g_cSize
= 0;
114 static size_t local_ZSTD_decompress(const void* src
, size_t srcSize
,
115 void* dst
, size_t dstSize
,
118 (void)src
; (void)srcSize
;
119 return ZSTD_decompress(dst
, dstSize
, buff2
, g_cSize
);
122 static ZSTD_DCtx
* g_zdc
= NULL
;
124 #ifndef ZSTD_DLL_IMPORT
125 extern size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx
* ctx
, const void* src
, size_t srcSize
);
126 static size_t local_ZSTD_decodeLiteralsBlock(const void* src
, size_t srcSize
, void* dst
, size_t dstSize
, void* buff2
)
128 (void)src
; (void)srcSize
; (void)dst
; (void)dstSize
;
129 return ZSTD_decodeLiteralsBlock((ZSTD_DCtx
*)g_zdc
, buff2
, g_cSize
);
132 static size_t local_ZSTD_decodeSeqHeaders(const void* src
, size_t srcSize
, void* dst
, size_t dstSize
, void* buff2
)
135 (void)src
; (void)srcSize
; (void)dst
; (void)dstSize
;
136 return ZSTD_decodeSeqHeaders(g_zdc
, &nbSeq
, buff2
, g_cSize
);
140 static ZSTD_CStream
* g_cstream
= NULL
;
142 local_ZSTD_compressStream(const void* src
, size_t srcSize
,
143 void* dst
, size_t dstCapacity
,
146 ZSTD_outBuffer buffOut
;
147 ZSTD_inBuffer buffIn
;
149 ZSTD_frameParameters f
= {1 /* contentSizeHeader*/, 0, 0};
151 p
.cParams
= *(ZSTD_compressionParameters
*)buff2
;
152 ZSTD_initCStream_advanced(g_cstream
, NULL
, 0, p
, ZSTD_CONTENTSIZE_UNKNOWN
);
154 buffOut
.size
= dstCapacity
;
157 buffIn
.size
= srcSize
;
159 ZSTD_compressStream(g_cstream
, &buffOut
, &buffIn
);
160 ZSTD_endStream(g_cstream
, &buffOut
);
165 local_ZSTD_compress_generic_end(const void* src
, size_t srcSize
,
166 void* dst
, size_t dstCapacity
,
170 return ZSTD_compress2(g_cstream
, dst
, dstCapacity
, src
, srcSize
);
174 local_ZSTD_compress_generic_continue(const void* src
, size_t srcSize
,
175 void* dst
, size_t dstCapacity
,
178 ZSTD_outBuffer buffOut
;
179 ZSTD_inBuffer buffIn
;
182 buffOut
.size
= dstCapacity
;
185 buffIn
.size
= srcSize
;
187 ZSTD_compressStream2(g_cstream
, &buffOut
, &buffIn
, ZSTD_e_continue
);
188 ZSTD_compressStream2(g_cstream
, &buffOut
, &buffIn
, ZSTD_e_end
);
193 local_ZSTD_compress_generic_T2_end(const void* src
, size_t srcSize
,
194 void* dst
, size_t dstCapacity
,
198 ZSTD_CCtx_setParameter(g_cstream
, ZSTD_c_nbWorkers
, 2);
199 return ZSTD_compress2(g_cstream
, dst
, dstCapacity
, src
, srcSize
);
203 local_ZSTD_compress_generic_T2_continue(const void* src
, size_t srcSize
,
204 void* dst
, size_t dstCapacity
,
207 ZSTD_outBuffer buffOut
;
208 ZSTD_inBuffer buffIn
;
210 ZSTD_CCtx_setParameter(g_cstream
, ZSTD_c_nbWorkers
, 2);
212 buffOut
.size
= dstCapacity
;
215 buffIn
.size
= srcSize
;
217 ZSTD_compressStream2(g_cstream
, &buffOut
, &buffIn
, ZSTD_e_continue
);
218 while(ZSTD_compressStream2(g_cstream
, &buffOut
, &buffIn
, ZSTD_e_end
)) {}
222 static ZSTD_DStream
* g_dstream
= NULL
;
224 local_ZSTD_decompressStream(const void* src
, size_t srcSize
,
225 void* dst
, size_t dstCapacity
,
228 ZSTD_outBuffer buffOut
;
229 ZSTD_inBuffer buffIn
;
230 (void)src
; (void)srcSize
;
231 ZSTD_initDStream(g_dstream
);
233 buffOut
.size
= dstCapacity
;
236 buffIn
.size
= g_cSize
;
238 ZSTD_decompressStream(g_dstream
, &buffOut
, &buffIn
);
242 #ifndef ZSTD_DLL_IMPORT
243 static size_t local_ZSTD_compressContinue(const void* src
, size_t srcSize
,
244 void* dst
, size_t dstCapacity
,
248 ZSTD_frameParameters f
= { 1 /* contentSizeHeader*/, 0, 0 };
250 p
.cParams
= *(ZSTD_compressionParameters
*)buff2
;
251 ZSTD_compressBegin_advanced(g_zcc
, NULL
, 0, p
, srcSize
);
252 return ZSTD_compressEnd(g_zcc
, dst
, dstCapacity
, src
, srcSize
);
255 #define FIRST_BLOCK_SIZE 8
256 static size_t local_ZSTD_compressContinue_extDict(const void* src
, size_t srcSize
,
257 void* dst
, size_t dstCapacity
,
260 BYTE firstBlockBuf
[FIRST_BLOCK_SIZE
];
263 ZSTD_frameParameters f
= { 1, 0, 0 };
265 p
.cParams
= *(ZSTD_compressionParameters
*)buff2
;
266 ZSTD_compressBegin_advanced(g_zcc
, NULL
, 0, p
, srcSize
);
267 memcpy(firstBlockBuf
, src
, FIRST_BLOCK_SIZE
);
269 { size_t const compressResult
= ZSTD_compressContinue(g_zcc
,
271 firstBlockBuf
, FIRST_BLOCK_SIZE
);
272 if (ZSTD_isError(compressResult
)) {
273 DISPLAY("local_ZSTD_compressContinue_extDict error : %s\n",
274 ZSTD_getErrorName(compressResult
));
275 return compressResult
;
277 dst
= (BYTE
*)dst
+ compressResult
;
278 dstCapacity
-= compressResult
;
280 return ZSTD_compressEnd(g_zcc
, dst
, dstCapacity
,
281 (const BYTE
*)src
+ FIRST_BLOCK_SIZE
,
282 srcSize
- FIRST_BLOCK_SIZE
);
285 static size_t local_ZSTD_decompressContinue(const void* src
, size_t srcSize
,
286 void* dst
, size_t dstCapacity
,
289 size_t regeneratedSize
= 0;
290 const BYTE
* ip
= (const BYTE
*)buff2
;
291 const BYTE
* const iend
= ip
+ g_cSize
;
292 BYTE
* op
= (BYTE
*)dst
;
293 size_t remainingCapacity
= dstCapacity
;
295 (void)src
; (void)srcSize
; /* unused */
296 ZSTD_decompressBegin(g_zdc
);
298 size_t const iSize
= ZSTD_nextSrcSizeToDecompress(g_zdc
);
299 size_t const decodedSize
= ZSTD_decompressContinue(g_zdc
, op
, remainingCapacity
, ip
, iSize
);
301 regeneratedSize
+= decodedSize
;
303 remainingCapacity
-= decodedSize
;
306 return regeneratedSize
;
311 /*_*******************************************************
313 *********************************************************/
314 static int benchMem(unsigned benchNb
,
315 const void* src
, size_t srcSize
,
316 int cLevel
, ZSTD_compressionParameters cparams
)
318 size_t dstBuffSize
= ZSTD_compressBound(srcSize
);
322 const char* benchName
;
323 BMK_benchFn_t benchFunction
;
330 benchFunction
= local_ZSTD_compress
; benchName
= "compress";
333 benchFunction
= local_ZSTD_decompress
; benchName
= "decompress";
335 #ifndef ZSTD_DLL_IMPORT
337 benchFunction
= local_ZSTD_compressContinue
; benchName
= "compressContinue";
340 benchFunction
= local_ZSTD_compressContinue_extDict
; benchName
= "compressContinue_extDict";
343 benchFunction
= local_ZSTD_decompressContinue
; benchName
= "decompressContinue";
346 benchFunction
= local_ZSTD_decodeLiteralsBlock
; benchName
= "decodeLiteralsBlock";
349 benchFunction
= local_ZSTD_decodeSeqHeaders
; benchName
= "decodeSeqHeaders";
353 benchFunction
= local_ZSTD_compressStream
; benchName
= "compressStream";
356 benchFunction
= local_ZSTD_decompressStream
; benchName
= "decompressStream";
359 benchFunction
= local_ZSTD_compress_generic_continue
; benchName
= "compress_generic, continue";
362 benchFunction
= local_ZSTD_compress_generic_end
; benchName
= "compress_generic, end";
365 benchFunction
= local_ZSTD_compress_generic_T2_continue
; benchName
= "compress_generic, -T2, continue";
368 benchFunction
= local_ZSTD_compress_generic_T2_end
; benchName
= "compress_generic, -T2, end";
375 dstBuff
= (BYTE
*)malloc(dstBuffSize
);
376 dstBuff2
= malloc(dstBuffSize
);
377 if ((!dstBuff
) || (!dstBuff2
)) {
378 DISPLAY("\nError: not enough memory!\n");
379 free(dstBuff
); free(dstBuff2
);
383 if (g_zcc
==NULL
) g_zcc
= ZSTD_createCCtx();
384 if (g_zdc
==NULL
) g_zdc
= ZSTD_createDCtx();
385 if (g_cstream
==NULL
) g_cstream
= ZSTD_createCStream();
386 if (g_dstream
==NULL
) g_dstream
= ZSTD_createDStream();
388 /* DISPLAY("params: cLevel %d, wlog %d hlog %d clog %d slog %d mml %d tlen %d strat %d \n",
389 cLevel, cparams->windowLog, cparams->hashLog, cparams->chainLog, cparams->searchLog,
390 cparams->minMatch, cparams->targetLength, cparams->strategy); */
392 ZSTD_CCtx_setParameter(g_zcc
, ZSTD_c_compressionLevel
, cLevel
);
393 ZSTD_CCtx_setParameter(g_zcc
, ZSTD_c_windowLog
, (int)cparams
.windowLog
);
394 ZSTD_CCtx_setParameter(g_zcc
, ZSTD_c_hashLog
, (int)cparams
.hashLog
);
395 ZSTD_CCtx_setParameter(g_zcc
, ZSTD_c_chainLog
, (int)cparams
.chainLog
);
396 ZSTD_CCtx_setParameter(g_zcc
, ZSTD_c_searchLog
, (int)cparams
.searchLog
);
397 ZSTD_CCtx_setParameter(g_zcc
, ZSTD_c_minMatch
, (int)cparams
.minMatch
);
398 ZSTD_CCtx_setParameter(g_zcc
, ZSTD_c_targetLength
, (int)cparams
.targetLength
);
399 ZSTD_CCtx_setParameter(g_zcc
, ZSTD_c_strategy
, cparams
.strategy
);
402 ZSTD_CCtx_setParameter(g_cstream
, ZSTD_c_compressionLevel
, cLevel
);
403 ZSTD_CCtx_setParameter(g_cstream
, ZSTD_c_windowLog
, (int)cparams
.windowLog
);
404 ZSTD_CCtx_setParameter(g_cstream
, ZSTD_c_hashLog
, (int)cparams
.hashLog
);
405 ZSTD_CCtx_setParameter(g_cstream
, ZSTD_c_chainLog
, (int)cparams
.chainLog
);
406 ZSTD_CCtx_setParameter(g_cstream
, ZSTD_c_searchLog
, (int)cparams
.searchLog
);
407 ZSTD_CCtx_setParameter(g_cstream
, ZSTD_c_minMatch
, (int)cparams
.minMatch
);
408 ZSTD_CCtx_setParameter(g_cstream
, ZSTD_c_targetLength
, (int)cparams
.targetLength
);
409 ZSTD_CCtx_setParameter(g_cstream
, ZSTD_c_strategy
, cparams
.strategy
);
418 g_cSize
= ZSTD_compress(buff2
, dstBuffSize
, src
, srcSize
, cLevel
);
420 #ifndef ZSTD_DLL_IMPORT
428 g_cSize
= ZSTD_compress(buff2
, dstBuffSize
, src
, srcSize
, cLevel
);
430 case 31: /* ZSTD_decodeLiteralsBlock */
431 { blockProperties_t bp
;
432 ZSTD_frameHeader zfp
;
433 size_t frameHeaderSize
, skippedSize
;
434 g_cSize
= ZSTD_compress(dstBuff
, dstBuffSize
, src
, srcSize
, cLevel
);
435 frameHeaderSize
= ZSTD_getFrameHeader(&zfp
, dstBuff
, ZSTD_FRAMEHEADERSIZE_MIN
);
436 if (frameHeaderSize
==0) frameHeaderSize
= ZSTD_FRAMEHEADERSIZE_MIN
;
437 ZSTD_getcBlockSize(dstBuff
+frameHeaderSize
, dstBuffSize
, &bp
); /* Get 1st block type */
438 if (bp
.blockType
!= bt_compressed
) {
439 DISPLAY("ZSTD_decodeLiteralsBlock : impossible to test on this sample (not compressible)\n");
442 skippedSize
= frameHeaderSize
+ ZSTD_blockHeaderSize
;
443 memcpy(buff2
, dstBuff
+skippedSize
, g_cSize
-skippedSize
);
444 srcSize
= srcSize
> 128 KB
? 128 KB
: srcSize
; /* speed relative to block */
445 ZSTD_decompressBegin(g_zdc
);
448 case 32: /* ZSTD_decodeSeqHeaders */
449 { blockProperties_t bp
;
450 ZSTD_frameHeader zfp
;
451 const BYTE
* ip
= dstBuff
;
453 size_t frameHeaderSize
, cBlockSize
;
454 ZSTD_compress(dstBuff
, dstBuffSize
, src
, srcSize
, cLevel
); /* it would be better to use direct block compression here */
455 g_cSize
= ZSTD_compress(dstBuff
, dstBuffSize
, src
, srcSize
, cLevel
);
456 frameHeaderSize
= ZSTD_getFrameHeader(&zfp
, dstBuff
, ZSTD_FRAMEHEADERSIZE_MIN
);
457 if (frameHeaderSize
==0) frameHeaderSize
= ZSTD_FRAMEHEADERSIZE_MIN
;
458 ip
+= frameHeaderSize
; /* Skip frame Header */
459 cBlockSize
= ZSTD_getcBlockSize(ip
, dstBuffSize
, &bp
); /* Get 1st block type */
460 if (bp
.blockType
!= bt_compressed
) {
461 DISPLAY("ZSTD_decodeSeqHeaders : impossible to test on this sample (not compressible)\n");
464 iend
= ip
+ ZSTD_blockHeaderSize
+ cBlockSize
; /* End of first block */
465 ip
+= ZSTD_blockHeaderSize
; /* skip block header */
466 ZSTD_decompressBegin(g_zdc
);
468 ip
+= ZSTD_decodeLiteralsBlock(g_zdc
, ip
, (size_t)(iend
-ip
)); /* skip literal segment */
469 g_cSize
= (size_t)(iend
-ip
);
470 memcpy(buff2
, ip
, g_cSize
); /* copy rest of block (it starts by SeqHeader) */
471 srcSize
= srcSize
> 128 KB
? 128 KB
: srcSize
; /* speed relative to block */
482 g_cSize
= ZSTD_compress(buff2
, dstBuffSize
, src
, srcSize
, cLevel
);
486 /* convention: test functions have ID > 100 */
491 /* warming up dstBuff */
492 { size_t i
; for (i
=0; i
<dstBuffSize
; i
++) dstBuff
[i
]=(BYTE
)i
; }
495 { BMK_timedFnState_t
* const tfs
= BMK_createTimedFnState(g_nbIterations
* 1000, 1000);
496 void* const avoidStrictAliasingPtr
= &dstBuff
;
497 BMK_benchParams_t bp
;
498 BMK_runTime_t bestResult
;
499 bestResult
.sumOfReturn
= 0;
500 bestResult
.nanoSecPerRun
= (double)TIMELOOP_NANOSEC
* 2000000000; /* hopefully large enough : must be larger than any potential measurement */
503 bp
.benchFn
= benchFunction
;
504 bp
.benchPayload
= buff2
;
506 bp
.initPayload
= NULL
;
507 bp
.errorFn
= ZSTD_isError
;
509 bp
.srcBuffers
= &src
;
510 bp
.srcSizes
= &srcSize
;
511 bp
.dstBuffers
= (void* const*) avoidStrictAliasingPtr
; /* circumvent strict aliasing warning on gcc-8,
512 * because gcc considers that `void* const *` and `void**` are 2 different types */
513 bp
.dstCapacities
= &dstBuffSize
;
514 bp
.blockResults
= NULL
;
517 BMK_runOutcome_t
const bOutcome
= BMK_benchTimedFn(tfs
, bp
);
519 if (!BMK_isSuccessful_runOutcome(bOutcome
)) {
520 DISPLAY("ERROR benchmarking function ! ! \n");
525 { BMK_runTime_t
const newResult
= BMK_extract_runTime(bOutcome
);
526 if (newResult
.nanoSecPerRun
< bestResult
.nanoSecPerRun
)
527 bestResult
.nanoSecPerRun
= newResult
.nanoSecPerRun
;
528 DISPLAY("\r%2u#%-29.29s:%8.1f MB/s (%8u) ",
530 (double)srcSize
* TIMELOOP_NANOSEC
/ bestResult
.nanoSecPerRun
/ MB_UNIT
,
531 (unsigned)newResult
.sumOfReturn
);
534 if ( BMK_isCompleted_TimedFn(tfs
) ) break;
536 BMK_freeTimedFnState(tfs
);
543 ZSTD_freeCCtx(g_zcc
); g_zcc
=NULL
;
544 ZSTD_freeDCtx(g_zdc
); g_zdc
=NULL
;
545 ZSTD_freeCStream(g_cstream
); g_cstream
=NULL
;
546 ZSTD_freeDStream(g_dstream
); g_dstream
=NULL
;
551 static int benchSample(U32 benchNb
,
552 int cLevel
, ZSTD_compressionParameters cparams
)
554 size_t const benchedSize
= g_sampleSize
;
555 const char* const name
= "Sample 10MiB";
558 void* const origBuff
= malloc(benchedSize
);
559 if (!origBuff
) { DISPLAY("\nError: not enough memory!\n"); return 12; }
562 RDG_genBuffer(origBuff
, benchedSize
, g_compressibility
, 0.0, 0);
565 DISPLAY("\r%70s\r", "");
566 DISPLAY(" %s : \n", name
);
568 benchMem(benchNb
, origBuff
, benchedSize
, cLevel
, cparams
);
569 } else { /* 0 == run all tests */
570 for (benchNb
=0; benchNb
<100; benchNb
++) {
571 benchMem(benchNb
, origBuff
, benchedSize
, cLevel
, cparams
);
579 static int benchFiles(U32 benchNb
,
580 const char** fileNamesTable
, const int nbFiles
,
581 int cLevel
, ZSTD_compressionParameters cparams
)
583 /* Loop for each file */
585 for (fileIdx
=0; fileIdx
<nbFiles
; fileIdx
++) {
586 const char* const inFileName
= fileNamesTable
[fileIdx
];
587 FILE* const inFile
= fopen( inFileName
, "rb" );
590 /* Check file existence */
591 if (inFile
==NULL
) { DISPLAY( "Pb opening %s\n", inFileName
); return 11; }
593 /* Memory allocation & restrictions */
594 { U64
const inFileSize
= UTIL_getFileSize(inFileName
);
595 if (inFileSize
== UTIL_FILESIZE_UNKNOWN
) {
596 DISPLAY( "Cannot measure size of %s\n", inFileName
);
600 benchedSize
= BMK_findMaxMem(inFileSize
*3) / 3;
601 if ((U64
)benchedSize
> inFileSize
)
602 benchedSize
= (size_t)inFileSize
;
603 if ((U64
)benchedSize
< inFileSize
) {
604 DISPLAY("Not enough memory for '%s' full size; testing %u MB only... \n",
605 inFileName
, (unsigned)(benchedSize
>>20));
609 { void* const origBuff
= malloc(benchedSize
);
610 if (!origBuff
) { DISPLAY("\nError: not enough memory!\n"); fclose(inFile
); return 12; }
612 /* Fill input buffer */
613 DISPLAY("Loading %s... \r", inFileName
);
614 { size_t const readSize
= fread(origBuff
, 1, benchedSize
, inFile
);
616 if (readSize
!= benchedSize
) {
617 DISPLAY("\nError: problem reading file '%s' !! \n", inFileName
);
623 DISPLAY("\r%70s\r", ""); /* blank line */
624 DISPLAY(" %s : \n", inFileName
);
626 benchMem(benchNb
, origBuff
, benchedSize
, cLevel
, cparams
);
628 for (benchNb
=0; benchNb
<100; benchNb
++) {
629 benchMem(benchNb
, origBuff
, benchedSize
, cLevel
, cparams
);
640 /*_*******************************************************
642 *********************************************************/
644 #define ERROR_OUT(msg) { DISPLAY("%s \n", msg); exit(1); }
646 static unsigned readU32FromChar(const char** stringPtr
)
648 const char errorMsg
[] = "error: numeric value too large";
650 while ((**stringPtr
>='0') && (**stringPtr
<='9')) {
651 unsigned const max
= (((unsigned)(-1)) / 10) - 1;
652 if (result
> max
) ERROR_OUT(errorMsg
);
654 result
+= (unsigned)(**stringPtr
- '0');
657 if ((**stringPtr
=='K') || (**stringPtr
=='M')) {
658 unsigned const maxK
= ((unsigned)(-1)) >> 10;
659 if (result
> maxK
) ERROR_OUT(errorMsg
);
661 if (**stringPtr
=='M') {
662 if (result
> maxK
) ERROR_OUT(errorMsg
);
665 (*stringPtr
)++; /* skip `K` or `M` */
666 if (**stringPtr
=='i') (*stringPtr
)++;
667 if (**stringPtr
=='B') (*stringPtr
)++;
672 static int longCommandWArg(const char** stringPtr
, const char* longCommand
)
674 size_t const comSize
= strlen(longCommand
);
675 int const result
= !strncmp(*stringPtr
, longCommand
, comSize
);
676 if (result
) *stringPtr
+= comSize
;
681 /*_*******************************************************
683 *********************************************************/
685 static int usage(const char* exename
)
687 DISPLAY( "Usage :\n");
688 DISPLAY( " %s [arg] file1 file2 ... fileX\n", exename
);
689 DISPLAY( "Arguments :\n");
690 DISPLAY( " -H/-h : Help (this text + advanced options)\n");
694 static int usage_advanced(const char* exename
)
697 DISPLAY( "\nAdvanced options :\n");
698 DISPLAY( " -b# : test only function # \n");
699 DISPLAY( " -i# : iteration loops [1-9](default : %i)\n", NBLOOPS
);
700 DISPLAY( " -P# : sample compressibility (default : %.1f%%)\n", COMPRESSIBILITY_DEFAULT
* 100);
701 DISPLAY( " -l# : benchmark functions at that compression level (default : %i)\n", DEFAULT_CLEVEL
);
702 DISPLAY( " --zstd : custom parameter selection. Format same as zstdcli \n");
706 static int badusage(const char* exename
)
708 DISPLAY("Wrong parameters\n");
713 int main(int argc
, const char** argv
)
715 int argNb
, filenamesStart
=0, result
;
716 const char* const exename
= argv
[0];
717 const char* input_filename
= NULL
;
718 U32 benchNb
= 0, main_pause
= 0;
719 int cLevel
= DEFAULT_CLEVEL
;
720 ZSTD_compressionParameters cparams
= ZSTD_getCParams(cLevel
, 0, 0);
722 DISPLAY(WELCOME_MESSAGE
);
723 if (argc
<1) return badusage(exename
);
725 for (argNb
=1; argNb
<argc
; argNb
++) {
726 const char* argument
= argv
[argNb
];
727 assert(argument
!= NULL
);
729 if (longCommandWArg(&argument
, "--zstd=")) {
731 if (longCommandWArg(&argument
, "windowLog=") || longCommandWArg(&argument
, "wlog=")) { cparams
.windowLog
= readU32FromChar(&argument
); if (argument
[0]==',') { argument
++; continue; } else break; }
732 if (longCommandWArg(&argument
, "chainLog=") || longCommandWArg(&argument
, "clog=")) { cparams
.chainLog
= readU32FromChar(&argument
); if (argument
[0]==',') { argument
++; continue; } else break; }
733 if (longCommandWArg(&argument
, "hashLog=") || longCommandWArg(&argument
, "hlog=")) { cparams
.hashLog
= readU32FromChar(&argument
); if (argument
[0]==',') { argument
++; continue; } else break; }
734 if (longCommandWArg(&argument
, "searchLog=") || longCommandWArg(&argument
, "slog=")) { cparams
.searchLog
= readU32FromChar(&argument
); if (argument
[0]==',') { argument
++; continue; } else break; }
735 if (longCommandWArg(&argument
, "minMatch=") || longCommandWArg(&argument
, "mml=")) { cparams
.minMatch
= readU32FromChar(&argument
); if (argument
[0]==',') { argument
++; continue; } else break; }
736 if (longCommandWArg(&argument
, "targetLength=") || longCommandWArg(&argument
, "tlen=")) { cparams
.targetLength
= readU32FromChar(&argument
); if (argument
[0]==',') { argument
++; continue; } else break; }
737 if (longCommandWArg(&argument
, "strategy=") || longCommandWArg(&argument
, "strat=")) { cparams
.strategy
= (ZSTD_strategy
)(readU32FromChar(&argument
)); if (argument
[0]==',') { argument
++; continue; } else break; }
738 if (longCommandWArg(&argument
, "level=") || longCommandWArg(&argument
, "lvl=")) { cLevel
= (int)readU32FromChar(&argument
); cparams
= ZSTD_getCParams(cLevel
, 0, 0); if (argument
[0]==',') { argument
++; continue; } else break; }
739 DISPLAY("invalid compression parameter \n");
743 /* check end of string */
744 if (argument
[0] != 0) {
745 DISPLAY("invalid --zstd= format \n");
751 } else if (argument
[0]=='-') { /* Commands (note : aggregated commands are allowed) */
753 while (argument
[0]!=0) {
757 /* Display help on usage */
759 case 'H': return usage_advanced(exename
);
761 /* Pause at the end (hidden option) */
762 case 'p': main_pause
= 1; break;
764 /* Select specific algorithm to bench */
767 benchNb
= readU32FromChar(&argument
);
770 /* Modify Nb Iterations */
773 g_nbIterations
= readU32FromChar(&argument
);
776 /* Select compressibility of synthetic sample */
779 g_compressibility
= (double)readU32FromChar(&argument
) / 100.;
783 cLevel
= (int)readU32FromChar(&argument
);
784 cparams
= ZSTD_getCParams(cLevel
, 0, 0);
787 /* Unknown command */
788 default : return badusage(exename
);
794 /* first provided filename is input */
795 if (!input_filename
) { input_filename
=argument
; filenamesStart
=argNb
; continue; }
800 if (filenamesStart
==0) /* no input file */
801 result
= benchSample(benchNb
, cLevel
, cparams
);
803 result
= benchFiles(benchNb
, argv
+filenamesStart
, argc
-filenamesStart
, cLevel
, cparams
);
805 if (main_pause
) { int unused
; printf("press enter...\n"); unused
= getchar(); (void)unused
; }