]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/zstd/tests/fullbench.c
Merge lld trunk r351319, resolve conflicts, and update FREEBSD-Xlist.
[FreeBSD/FreeBSD.git] / sys / contrib / zstd / tests / fullbench.c
1 /*
2  * Copyright (c) 2015-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 *  Includes
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 */
19
20 #include "mem.h"         /* U32 */
21 #ifndef ZSTD_DLL_IMPORT
22     #include "zstd_internal.h"   /* ZSTD_decodeSeqHeaders, ZSTD_blockHeaderSize, blockType_e, KB, MB */
23 #else
24     #define KB *(1 <<10)
25     #define MB *(1 <<20)
26     #define GB *(1U<<30)
27     typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
28 #endif
29 #define ZSTD_STATIC_LINKING_ONLY  /* ZSTD_compressBegin, ZSTD_compressContinue, etc. */
30 #include "zstd.h"        /* ZSTD_versionString */
31 #include "util.h"        /* time functions */
32 #include "datagen.h"
33 #include "benchfn.h"       /* CustomBench*/
34 #include "benchzstd.h"     /* MB_UNIT */
35
36
37 /*_************************************
38 *  Constants
39 **************************************/
40 #define PROGRAM_DESCRIPTION "Zstandard speed analyzer"
41 #define AUTHOR "Yann Collet"
42 #define WELCOME_MESSAGE "*** %s %s %i-bits, by %s (%s) ***\n", PROGRAM_DESCRIPTION, ZSTD_versionString(), (int)(sizeof(void*)*8), AUTHOR, __DATE__
43
44 #define NBLOOPS    6
45 #define TIMELOOP_S 2
46
47 #define KNUTH      2654435761U
48 #define MAX_MEM    (1984 MB)
49
50 #define DEFAULT_CLEVEL 1
51
52 #define COMPRESSIBILITY_DEFAULT 0.50
53 static const size_t g_sampleSize = 10000000;
54
55 #define TIMELOOP_NANOSEC      (1*1000000000ULL) /* 1 second */
56
57
58 /*_************************************
59 *  Macros
60 **************************************/
61 #define DISPLAY(...)  fprintf(stderr, __VA_ARGS__)
62
63
64 /*_************************************
65 *  Benchmark Parameters
66 **************************************/
67 static unsigned g_nbIterations = NBLOOPS;
68 static double g_compressibility = COMPRESSIBILITY_DEFAULT;
69
70 static void BMK_SetNbIterations(unsigned nbLoops)
71 {
72     g_nbIterations = nbLoops;
73     DISPLAY("- %i iterations -\n", g_nbIterations);
74 }
75
76
77 /*_*******************************************************
78 *  Private functions
79 *********************************************************/
80 static size_t BMK_findMaxMem(U64 requiredMem)
81 {
82     size_t const step = 64 MB;
83     void* testmem = NULL;
84
85     requiredMem = (((requiredMem >> 26) + 1) << 26);
86     if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
87
88     requiredMem += step;
89     do {
90         testmem = malloc ((size_t)requiredMem);
91         requiredMem -= step;
92     } while (!testmem);
93
94     free (testmem);
95     return (size_t) requiredMem;
96 }
97
98
99 /*_*******************************************************
100 *  Benchmark wrappers
101 *********************************************************/
102
103 static ZSTD_CCtx* g_zcc = NULL;
104
105 static size_t
106 local_ZSTD_compress(const void* src, size_t srcSize,
107                     void* dst, size_t dstSize,
108                     void* buff2)
109 {
110     ZSTD_parameters p;
111     ZSTD_frameParameters f = { 1 /* contentSizeHeader*/, 0, 0 };
112     p.fParams = f;
113     p.cParams = *(ZSTD_compressionParameters*)buff2;
114     return ZSTD_compress_advanced (g_zcc, dst, dstSize, src, srcSize, NULL ,0, p);
115     //return ZSTD_compress(dst, dstSize, src, srcSize, cLevel);
116 }
117
118 static size_t g_cSize = 0;
119 static size_t local_ZSTD_decompress(const void* src, size_t srcSize,
120                                     void* dst, size_t dstSize,
121                                     void* buff2)
122 {
123     (void)src; (void)srcSize;
124     return ZSTD_decompress(dst, dstSize, buff2, g_cSize);
125 }
126
127 static ZSTD_DCtx* g_zdc = NULL;
128
129 #ifndef ZSTD_DLL_IMPORT
130 extern size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* ctx, const void* src, size_t srcSize);
131 static size_t local_ZSTD_decodeLiteralsBlock(const void* src, size_t srcSize, void* dst, size_t dstSize, void* buff2)
132 {
133     (void)src; (void)srcSize; (void)dst; (void)dstSize;
134     return ZSTD_decodeLiteralsBlock((ZSTD_DCtx*)g_zdc, buff2, g_cSize);
135 }
136
137 static size_t local_ZSTD_decodeSeqHeaders(const void* src, size_t srcSize, void* dst, size_t dstSize, void* buff2)
138 {
139     int nbSeq;
140     (void)src; (void)srcSize; (void)dst; (void)dstSize;
141     return ZSTD_decodeSeqHeaders(g_zdc, &nbSeq, buff2, g_cSize);
142 }
143 #endif
144
145 static ZSTD_CStream* g_cstream= NULL;
146 static size_t
147 local_ZSTD_compressStream(const void* src, size_t srcSize,
148                           void* dst, size_t dstCapacity,
149                           void* buff2)
150 {
151     ZSTD_outBuffer buffOut;
152     ZSTD_inBuffer buffIn;
153     ZSTD_parameters p;
154     ZSTD_frameParameters f = {1 /* contentSizeHeader*/, 0, 0};
155     p.fParams = f;
156     p.cParams = *(ZSTD_compressionParameters*)buff2;
157     ZSTD_initCStream_advanced(g_cstream, NULL, 0, p, ZSTD_CONTENTSIZE_UNKNOWN);
158     buffOut.dst = dst;
159     buffOut.size = dstCapacity;
160     buffOut.pos = 0;
161     buffIn.src = src;
162     buffIn.size = srcSize;
163     buffIn.pos = 0;
164     ZSTD_compressStream(g_cstream, &buffOut, &buffIn);
165     ZSTD_endStream(g_cstream, &buffOut);
166     return buffOut.pos;
167 }
168
169 static size_t
170 local_ZSTD_compress_generic_end(const void* src, size_t srcSize,
171                                 void* dst, size_t dstCapacity,
172                                 void* buff2)
173 {
174     (void)buff2;
175     return ZSTD_compress2(g_cstream, dst, dstCapacity, src, srcSize);
176 }
177
178 static size_t
179 local_ZSTD_compress_generic_continue(const void* src, size_t srcSize,
180                                      void* dst, size_t dstCapacity,
181                                      void* buff2)
182 {
183     ZSTD_outBuffer buffOut;
184     ZSTD_inBuffer buffIn;
185     (void)buff2;
186     buffOut.dst = dst;
187     buffOut.size = dstCapacity;
188     buffOut.pos = 0;
189     buffIn.src = src;
190     buffIn.size = srcSize;
191     buffIn.pos = 0;
192     ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
193     ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_end);
194     return buffOut.pos;
195 }
196
197 static size_t
198 local_ZSTD_compress_generic_T2_end(const void* src, size_t srcSize,
199                                    void* dst, size_t dstCapacity,
200                                    void* buff2)
201 {
202     (void)buff2;
203     ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_nbWorkers, 2);
204     return ZSTD_compress2(g_cstream, dst, dstCapacity, src, srcSize);
205 }
206
207 static size_t
208 local_ZSTD_compress_generic_T2_continue(const void* src, size_t srcSize,
209                                         void* dst, size_t dstCapacity,
210                                         void* buff2)
211 {
212     ZSTD_outBuffer buffOut;
213     ZSTD_inBuffer buffIn;
214     (void)buff2;
215     ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_nbWorkers, 2);
216     buffOut.dst = dst;
217     buffOut.size = dstCapacity;
218     buffOut.pos = 0;
219     buffIn.src = src;
220     buffIn.size = srcSize;
221     buffIn.pos = 0;
222     ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
223     while(ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_end)) {}
224     return buffOut.pos;
225 }
226
227 static ZSTD_DStream* g_dstream= NULL;
228 static size_t
229 local_ZSTD_decompressStream(const void* src, size_t srcSize,
230                             void* dst, size_t dstCapacity,
231                             void* buff2)
232 {
233     ZSTD_outBuffer buffOut;
234     ZSTD_inBuffer buffIn;
235     (void)src; (void)srcSize;
236     ZSTD_initDStream(g_dstream);
237     buffOut.dst = dst;
238     buffOut.size = dstCapacity;
239     buffOut.pos = 0;
240     buffIn.src = buff2;
241     buffIn.size = g_cSize;
242     buffIn.pos = 0;
243     ZSTD_decompressStream(g_dstream, &buffOut, &buffIn);
244     return buffOut.pos;
245 }
246
247 #ifndef ZSTD_DLL_IMPORT
248 static size_t local_ZSTD_compressContinue(const void* src, size_t srcSize,
249                                           void* dst, size_t dstCapacity,
250                                           void* buff2)
251 {
252     ZSTD_parameters p;
253     ZSTD_frameParameters f = { 1 /* contentSizeHeader*/, 0, 0 };
254     p.fParams = f;
255     p.cParams = *(ZSTD_compressionParameters*)buff2;
256     ZSTD_compressBegin_advanced(g_zcc, NULL, 0, p, srcSize);
257     return ZSTD_compressEnd(g_zcc, dst, dstCapacity, src, srcSize);
258 }
259
260 #define FIRST_BLOCK_SIZE 8
261 static size_t local_ZSTD_compressContinue_extDict(const void* src, size_t srcSize,
262                                                   void* dst, size_t dstCapacity,
263                                                   void* buff2)
264 {
265     BYTE firstBlockBuf[FIRST_BLOCK_SIZE];
266
267     ZSTD_parameters p;
268     ZSTD_frameParameters f = { 1, 0, 0 };
269     p.fParams = f;
270     p.cParams = *(ZSTD_compressionParameters*)buff2;
271     ZSTD_compressBegin_advanced(g_zcc, NULL, 0, p, srcSize);
272     memcpy(firstBlockBuf, src, FIRST_BLOCK_SIZE);
273
274     {   size_t const compressResult = ZSTD_compressContinue(g_zcc,
275                                             dst, dstCapacity,
276                                             firstBlockBuf, FIRST_BLOCK_SIZE);
277         if (ZSTD_isError(compressResult)) {
278             DISPLAY("local_ZSTD_compressContinue_extDict error : %s\n",
279                     ZSTD_getErrorName(compressResult));
280             return compressResult;
281         }
282         dst = (BYTE*)dst + compressResult;
283         dstCapacity -= compressResult;
284     }
285     return ZSTD_compressEnd(g_zcc, dst, dstCapacity,
286                             (const BYTE*)src + FIRST_BLOCK_SIZE,
287                             srcSize - FIRST_BLOCK_SIZE);
288 }
289
290 static size_t local_ZSTD_decompressContinue(const void* src, size_t srcSize,
291                                             void* dst, size_t dstCapacity,
292                                             void* buff2)
293 {
294     size_t regeneratedSize = 0;
295     const BYTE* ip = (const BYTE*)buff2;
296     const BYTE* const iend = ip + g_cSize;
297     BYTE* op = (BYTE*)dst;
298     size_t remainingCapacity = dstCapacity;
299
300     (void)src; (void)srcSize;  /* unused */
301     ZSTD_decompressBegin(g_zdc);
302     while (ip < iend) {
303         size_t const iSize = ZSTD_nextSrcSizeToDecompress(g_zdc);
304         size_t const decodedSize = ZSTD_decompressContinue(g_zdc, op, remainingCapacity, ip, iSize);
305         ip += iSize;
306         regeneratedSize += decodedSize;
307         op += decodedSize;
308         remainingCapacity -= decodedSize;
309     }
310
311     return regeneratedSize;
312 }
313 #endif
314
315
316 /*_*******************************************************
317 *  Bench functions
318 *********************************************************/
319 static size_t benchMem(unsigned benchNb,
320                        const void* src, size_t srcSize,
321                        int cLevel, ZSTD_compressionParameters cparams)
322 {
323     size_t dstBuffSize = ZSTD_compressBound(srcSize);
324     BYTE*  dstBuff;
325     void*  dstBuff2;
326     void*  buff2;
327     const char* benchName;
328     BMK_benchFn_t benchFunction;
329     int errorcode = 0;
330
331     /* Selection */
332     switch(benchNb)
333     {
334     case 1:
335         benchFunction = local_ZSTD_compress; benchName = "compress";
336         break;
337     case 2:
338         benchFunction = local_ZSTD_decompress; benchName = "decompress";
339         break;
340 #ifndef ZSTD_DLL_IMPORT
341     case 11:
342         benchFunction = local_ZSTD_compressContinue; benchName = "compressContinue";
343         break;
344     case 12:
345         benchFunction = local_ZSTD_compressContinue_extDict; benchName = "compressContinue_extDict";
346         break;
347     case 13:
348         benchFunction = local_ZSTD_decompressContinue; benchName = "decompressContinue";
349         break;
350     case 31:
351         benchFunction = local_ZSTD_decodeLiteralsBlock; benchName = "decodeLiteralsBlock";
352         break;
353     case 32:
354         benchFunction = local_ZSTD_decodeSeqHeaders; benchName = "decodeSeqHeaders";
355         break;
356 #endif
357     case 41:
358         benchFunction = local_ZSTD_compressStream; benchName = "compressStream";
359         break;
360     case 42:
361         benchFunction = local_ZSTD_decompressStream; benchName = "decompressStream";
362         break;
363     case 51:
364         benchFunction = local_ZSTD_compress_generic_continue; benchName = "compress_generic, continue";
365         break;
366     case 52:
367         benchFunction = local_ZSTD_compress_generic_end; benchName = "compress_generic, end";
368         break;
369     case 61:
370         benchFunction = local_ZSTD_compress_generic_T2_continue; benchName = "compress_generic, -T2, continue";
371         break;
372     case 62:
373         benchFunction = local_ZSTD_compress_generic_T2_end; benchName = "compress_generic, -T2, end";
374         break;
375     default :
376         return 0;
377     }
378
379     /* Allocation */
380     dstBuff = (BYTE*)malloc(dstBuffSize);
381     dstBuff2 = malloc(dstBuffSize);
382     if ((!dstBuff) || (!dstBuff2)) {
383         DISPLAY("\nError: not enough memory!\n");
384         free(dstBuff); free(dstBuff2);
385         return 12;
386     }
387     buff2 = dstBuff2;
388     if (g_zcc==NULL) g_zcc = ZSTD_createCCtx();
389     if (g_zdc==NULL) g_zdc = ZSTD_createDCtx();
390     if (g_cstream==NULL) g_cstream = ZSTD_createCStream();
391     if (g_dstream==NULL) g_dstream = ZSTD_createDStream();
392
393     /* DISPLAY("params: cLevel %d, wlog %d hlog %d clog %d slog %d mml %d tlen %d strat %d \n",
394           cLevel, cparams->windowLog, cparams->hashLog, cparams->chainLog, cparams->searchLog,
395           cparams->minMatch, cparams->targetLength, cparams->strategy); */
396
397     ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_compressionLevel, cLevel);
398     ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_windowLog, cparams.windowLog);
399     ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_hashLog, cparams.hashLog);
400     ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_chainLog, cparams.chainLog);
401     ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_searchLog, cparams.searchLog);
402     ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_minMatch, cparams.minMatch);
403     ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_targetLength, cparams.targetLength);
404     ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_strategy, cparams.strategy);
405
406
407     ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_compressionLevel, cLevel);
408     ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_windowLog, cparams.windowLog);
409     ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_hashLog, cparams.hashLog);
410     ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_chainLog, cparams.chainLog);
411     ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_searchLog, cparams.searchLog);
412     ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_minMatch, cparams.minMatch);
413     ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_targetLength, cparams.targetLength);
414     ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_strategy, cparams.strategy);
415
416     /* Preparation */
417     switch(benchNb)
418     {
419     case 1:
420         buff2 = &cparams;
421         break;
422     case 2:
423         g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, cLevel);
424         break;
425 #ifndef ZSTD_DLL_IMPORT
426     case 11:
427         buff2 = &cparams;
428         break;
429     case 12:
430         buff2 = &cparams;
431         break;
432     case 13 :
433         g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, cLevel);
434         break;
435     case 31:  /* ZSTD_decodeLiteralsBlock */
436         {   blockProperties_t bp;
437             ZSTD_frameHeader zfp;
438             size_t frameHeaderSize, skippedSize;
439             g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, cLevel);
440             frameHeaderSize = ZSTD_getFrameHeader(&zfp, dstBuff, ZSTD_FRAMEHEADERSIZE_MIN);
441             if (frameHeaderSize==0) frameHeaderSize = ZSTD_FRAMEHEADERSIZE_MIN;
442             ZSTD_getcBlockSize(dstBuff+frameHeaderSize, dstBuffSize, &bp);  /* Get 1st block type */
443             if (bp.blockType != bt_compressed) {
444                 DISPLAY("ZSTD_decodeLiteralsBlock : impossible to test on this sample (not compressible)\n");
445                 goto _cleanOut;
446             }
447             skippedSize = frameHeaderSize + ZSTD_blockHeaderSize;
448             memcpy(buff2, dstBuff+skippedSize, g_cSize-skippedSize);
449             srcSize = srcSize > 128 KB ? 128 KB : srcSize;    /* speed relative to block */
450             ZSTD_decompressBegin(g_zdc);
451             break;
452         }
453     case 32:   /* ZSTD_decodeSeqHeaders */
454         {   blockProperties_t bp;
455             ZSTD_frameHeader zfp;
456             const BYTE* ip = dstBuff;
457             const BYTE* iend;
458             size_t frameHeaderSize, cBlockSize;
459             ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, cLevel);   /* it would be better to use direct block compression here */
460             g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, cLevel);
461             frameHeaderSize = ZSTD_getFrameHeader(&zfp, dstBuff, ZSTD_FRAMEHEADERSIZE_MIN);
462             if (frameHeaderSize==0) frameHeaderSize = ZSTD_FRAMEHEADERSIZE_MIN;
463             ip += frameHeaderSize;   /* Skip frame Header */
464             cBlockSize = ZSTD_getcBlockSize(ip, dstBuffSize, &bp);   /* Get 1st block type */
465             if (bp.blockType != bt_compressed) {
466                 DISPLAY("ZSTD_decodeSeqHeaders : impossible to test on this sample (not compressible)\n");
467                 goto _cleanOut;
468             }
469             iend = ip + ZSTD_blockHeaderSize + cBlockSize;   /* End of first block */
470             ip += ZSTD_blockHeaderSize;                      /* skip block header */
471             ZSTD_decompressBegin(g_zdc);
472             ip += ZSTD_decodeLiteralsBlock(g_zdc, ip, iend-ip);   /* skip literal segment */
473             g_cSize = iend-ip;
474             memcpy(buff2, ip, g_cSize);   /* copy rest of block (it starts by SeqHeader) */
475             srcSize = srcSize > 128 KB ? 128 KB : srcSize;   /* speed relative to block */
476             break;
477         }
478 #else
479     case 31:
480         goto _cleanOut;
481 #endif
482     case 41 :
483         buff2 = &cparams;
484         break;
485     case 42 :
486         g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, cLevel);
487         break;
488
489     /* test functions */
490     /* convention: test functions have ID > 100 */
491
492     default : ;
493     }
494
495      /* warming up dstBuff */
496     { size_t i; for (i=0; i<dstBuffSize; i++) dstBuff[i]=(BYTE)i; }
497
498     /* benchmark loop */
499     {   BMK_timedFnState_t* const tfs = BMK_createTimedFnState(g_nbIterations * 1000, 1000);
500         void* const avoidStrictAliasingPtr = &dstBuff;
501         BMK_benchParams_t bp;
502         BMK_runTime_t bestResult;
503         bestResult.sumOfReturn = 0;
504         bestResult.nanoSecPerRun = (unsigned long long)(-1LL);
505         assert(tfs != NULL);
506
507         bp.benchFn = benchFunction;
508         bp.benchPayload = buff2;
509         bp.initFn = NULL;
510         bp.initPayload = NULL;
511         bp.errorFn = ZSTD_isError;
512         bp.blockCount = 1;
513         bp.srcBuffers = &src;
514         bp.srcSizes = &srcSize;
515         bp.dstBuffers = (void* const*) avoidStrictAliasingPtr;  /* circumvent strict aliasing warning on gcc-8,
516                                                                  * because gcc considers that `void* const *`  and `void**` are 2 different types */
517         bp.dstCapacities = &dstBuffSize;
518         bp.blockResults = NULL;
519
520         for (;;) {
521             BMK_runOutcome_t const bOutcome = BMK_benchTimedFn(tfs, bp);
522
523             if (!BMK_isSuccessful_runOutcome(bOutcome)) {
524                 DISPLAY("ERROR benchmarking function ! ! \n");
525                 errorcode = 1;
526                 goto _cleanOut;
527             }
528
529             {   BMK_runTime_t const newResult = BMK_extract_runTime(bOutcome);
530                 if (newResult.nanoSecPerRun < bestResult.nanoSecPerRun )
531                     bestResult.nanoSecPerRun = newResult.nanoSecPerRun;
532                 DISPLAY("\r%2u#%-29.29s:%8.1f MB/s  (%8u) ",
533                         benchNb, benchName,
534                         (double)srcSize * TIMELOOP_NANOSEC / bestResult.nanoSecPerRun / MB_UNIT,
535                         (unsigned)newResult.sumOfReturn );
536             }
537
538             if ( BMK_isCompleted_TimedFn(tfs) ) break;
539         }
540         BMK_freeTimedFnState(tfs);
541     }
542     DISPLAY("\n");
543
544 _cleanOut:
545     free(dstBuff);
546     free(dstBuff2);
547     ZSTD_freeCCtx(g_zcc); g_zcc=NULL;
548     ZSTD_freeDCtx(g_zdc); g_zdc=NULL;
549     ZSTD_freeCStream(g_cstream); g_cstream=NULL;
550     ZSTD_freeDStream(g_dstream); g_dstream=NULL;
551     return errorcode;
552 }
553
554
555 static int benchSample(U32 benchNb,
556                        int cLevel, ZSTD_compressionParameters cparams)
557 {
558     size_t const benchedSize = g_sampleSize;
559     const char* const name = "Sample 10MiB";
560
561     /* Allocation */
562     void* const origBuff = malloc(benchedSize);
563     if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); return 12; }
564
565     /* Fill buffer */
566     RDG_genBuffer(origBuff, benchedSize, g_compressibility, 0.0, 0);
567
568     /* bench */
569     DISPLAY("\r%70s\r", "");
570     DISPLAY(" %s : \n", name);
571     if (benchNb) {
572         benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
573     } else {  /* 0 == run all tests */
574         for (benchNb=0; benchNb<100; benchNb++) {
575             benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
576     }   }
577
578     free(origBuff);
579     return 0;
580 }
581
582
583 static int benchFiles(U32 benchNb,
584                       const char** fileNamesTable, const int nbFiles,
585                       int cLevel, ZSTD_compressionParameters cparams)
586 {
587     /* Loop for each file */
588     int fileIdx;
589     for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
590         const char* const inFileName = fileNamesTable[fileIdx];
591         FILE* const inFile = fopen( inFileName, "rb" );
592         size_t benchedSize;
593
594         /* Check file existence */
595         if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
596
597         /* Memory allocation & restrictions */
598         {   U64 const inFileSize = UTIL_getFileSize(inFileName);
599             if (inFileSize == UTIL_FILESIZE_UNKNOWN) {
600                 DISPLAY( "Cannot measure size of %s\n", inFileName);
601                 fclose(inFile);
602                 return 11;
603             }
604             benchedSize = BMK_findMaxMem(inFileSize*3) / 3;
605             if ((U64)benchedSize > inFileSize)
606                 benchedSize = (size_t)inFileSize;
607             if ((U64)benchedSize < inFileSize) {
608                 DISPLAY("Not enough memory for '%s' full size; testing %u MB only... \n",
609                         inFileName, (unsigned)(benchedSize>>20));
610         }   }
611
612         /* Alloc */
613         {   void* const origBuff = malloc(benchedSize);
614             if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); fclose(inFile); return 12; }
615
616             /* Fill input buffer */
617             DISPLAY("Loading %s...       \r", inFileName);
618             {   size_t const readSize = fread(origBuff, 1, benchedSize, inFile);
619                 fclose(inFile);
620                 if (readSize != benchedSize) {
621                     DISPLAY("\nError: problem reading file '%s' !!    \n", inFileName);
622                     free(origBuff);
623                     return 13;
624             }   }
625
626             /* bench */
627             DISPLAY("\r%70s\r", "");   /* blank line */
628             DISPLAY(" %s : \n", inFileName);
629             if (benchNb) {
630                 benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
631             } else {
632                 for (benchNb=0; benchNb<100; benchNb++) {
633                     benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
634             }   }
635
636             free(origBuff);
637     }   }
638
639     return 0;
640 }
641
642
643
644 /*_*******************************************************
645 *  Argument Parsing
646 *********************************************************/
647
648 #define ERROR_OUT(msg) { DISPLAY("%s \n", msg); exit(1); }
649
650 static unsigned readU32FromChar(const char** stringPtr)
651 {
652     const char errorMsg[] = "error: numeric value too large";
653     unsigned result = 0;
654     while ((**stringPtr >='0') && (**stringPtr <='9')) {
655         unsigned const max = (((unsigned)(-1)) / 10) - 1;
656         if (result > max) ERROR_OUT(errorMsg);
657         result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
658     }
659     if ((**stringPtr=='K') || (**stringPtr=='M')) {
660         unsigned const maxK = ((unsigned)(-1)) >> 10;
661         if (result > maxK) ERROR_OUT(errorMsg);
662         result <<= 10;
663         if (**stringPtr=='M') {
664             if (result > maxK) ERROR_OUT(errorMsg);
665             result <<= 10;
666         }
667         (*stringPtr)++;  /* skip `K` or `M` */
668         if (**stringPtr=='i') (*stringPtr)++;
669         if (**stringPtr=='B') (*stringPtr)++;
670     }
671     return result;
672 }
673
674 static unsigned longCommandWArg(const char** stringPtr, const char* longCommand)
675 {
676     size_t const comSize = strlen(longCommand);
677     int const result = !strncmp(*stringPtr, longCommand, comSize);
678     if (result) *stringPtr += comSize;
679     return result;
680 }
681
682
683 /*_*******************************************************
684 *  Command line
685 *********************************************************/
686
687 static int usage(const char* exename)
688 {
689     DISPLAY( "Usage :\n");
690     DISPLAY( "      %s [arg] file1 file2 ... fileX\n", exename);
691     DISPLAY( "Arguments :\n");
692     DISPLAY( " -H/-h  : Help (this text + advanced options)\n");
693     return 0;
694 }
695
696 static int usage_advanced(const char* exename)
697 {
698     usage(exename);
699     DISPLAY( "\nAdvanced options :\n");
700     DISPLAY( " -b#    : test only function # \n");
701     DISPLAY( " -i#    : iteration loops [1-9](default : %i)\n", NBLOOPS);
702     DISPLAY( " -P#    : sample compressibility (default : %.1f%%)\n", COMPRESSIBILITY_DEFAULT * 100);
703     DISPLAY( " -l#    : benchmark functions at that compression level (default : %i)\n", DEFAULT_CLEVEL);
704     DISPLAY( " --zstd : custom parameter selection. Format same as zstdcli \n");
705     return 0;
706 }
707
708 static int badusage(const char* exename)
709 {
710     DISPLAY("Wrong parameters\n");
711     usage(exename);
712     return 1;
713 }
714
715 int main(int argc, const char** argv)
716 {
717     int argNb, filenamesStart=0, result;
718     const char* const exename = argv[0];
719     const char* input_filename = NULL;
720     U32 benchNb = 0, main_pause = 0;
721     int cLevel = DEFAULT_CLEVEL;
722     ZSTD_compressionParameters cparams = ZSTD_getCParams(cLevel, 0, 0);
723
724     DISPLAY(WELCOME_MESSAGE);
725     if (argc<1) return badusage(exename);
726
727     for (argNb=1; argNb<argc; argNb++) {
728         const char* argument = argv[argNb];
729         assert(argument != NULL);
730
731         if (longCommandWArg(&argument, "--zstd=")) {
732             for ( ; ;) {
733                 if (longCommandWArg(&argument, "windowLog=") || longCommandWArg(&argument, "wlog=")) { cparams.windowLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
734                 if (longCommandWArg(&argument, "chainLog=") || longCommandWArg(&argument, "clog=")) { cparams.chainLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
735                 if (longCommandWArg(&argument, "hashLog=") || longCommandWArg(&argument, "hlog=")) { cparams.hashLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
736                 if (longCommandWArg(&argument, "searchLog=") || longCommandWArg(&argument, "slog=")) { cparams.searchLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
737                 if (longCommandWArg(&argument, "minMatch=") || longCommandWArg(&argument, "mml=")) { cparams.minMatch = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
738                 if (longCommandWArg(&argument, "targetLength=") || longCommandWArg(&argument, "tlen=")) { cparams.targetLength = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
739                 if (longCommandWArg(&argument, "strategy=") || longCommandWArg(&argument, "strat=")) { cparams.strategy = (ZSTD_strategy)(readU32FromChar(&argument)); if (argument[0]==',') { argument++; continue; } else break; }
740                 if (longCommandWArg(&argument, "level=") || longCommandWArg(&argument, "lvl=")) { cLevel = (int)readU32FromChar(&argument); cparams = ZSTD_getCParams(cLevel, 0, 0); if (argument[0]==',') { argument++; continue; } else break; }
741                 DISPLAY("invalid compression parameter \n");
742                 return 1;
743             }
744
745             /* check end of string */
746             if (argument[0] != 0) {
747                 DISPLAY("invalid --zstd= format \n");
748                 return 1;
749             } else {
750                 continue;
751             }
752
753         } else if (argument[0]=='-') { /* Commands (note : aggregated commands are allowed) */
754             argument++;
755             while (argument[0]!=0) {
756
757                 switch(argument[0])
758                 {
759                     /* Display help on usage */
760                 case 'h':
761                 case 'H': return usage_advanced(exename);
762
763                     /* Pause at the end (hidden option) */
764                 case 'p': main_pause = 1; break;
765
766                     /* Select specific algorithm to bench */
767                 case 'b':
768                     argument++;
769                     benchNb = readU32FromChar(&argument);
770                     break;
771
772                     /* Modify Nb Iterations */
773                 case 'i':
774                     argument++;
775                     BMK_SetNbIterations((int)readU32FromChar(&argument));
776                     break;
777
778                     /* Select compressibility of synthetic sample */
779                 case 'P':
780                     argument++;
781                     g_compressibility = (double)readU32FromChar(&argument) / 100.;
782                     break;
783                 case 'l':
784                     argument++;
785                     cLevel = readU32FromChar(&argument);
786                     cparams = ZSTD_getCParams(cLevel, 0, 0);
787                     break;
788
789                     /* Unknown command */
790                 default : return badusage(exename);
791                 }
792             }
793             continue;
794         }
795
796         /* first provided filename is input */
797         if (!input_filename) { input_filename=argument; filenamesStart=argNb; continue; }
798     }
799
800
801
802     if (filenamesStart==0)   /* no input file */
803         result = benchSample(benchNb, cLevel, cparams);
804     else
805         result = benchFiles(benchNb, argv+filenamesStart, argc-filenamesStart, cLevel, cparams);
806
807     if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; }
808
809     return result;
810 }