]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/zstd/tests/fullbench.c
MFV r349134:
[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 "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 */
24 #else
25     #define KB *(1 <<10)
26     #define MB *(1 <<20)
27     #define GB *(1U<<30)
28     typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
29 #endif
30 #define ZSTD_STATIC_LINKING_ONLY  /* ZSTD_compressBegin, ZSTD_compressContinue, etc. */
31 #include "zstd.h"        /* ZSTD_versionString */
32 #include "util.h"        /* time functions */
33 #include "datagen.h"
34 #include "benchfn.h"       /* CustomBench*/
35 #include "benchzstd.h"     /* MB_UNIT */
36
37
38 /*_************************************
39 *  Constants
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__
44
45 #define NBLOOPS    6
46 #define TIMELOOP_S 2
47
48 #define KNUTH      2654435761U
49 #define MAX_MEM    (1984 MB)
50
51 #define DEFAULT_CLEVEL 1
52
53 #define COMPRESSIBILITY_DEFAULT 0.50
54 static const size_t g_sampleSize = 10000000;
55
56 #define TIMELOOP_NANOSEC      (1*1000000000ULL) /* 1 second */
57
58
59 /*_************************************
60 *  Macros
61 **************************************/
62 #define DISPLAY(...)  fprintf(stderr, __VA_ARGS__)
63
64
65 /*_************************************
66 *  Benchmark Parameters
67 **************************************/
68 static unsigned g_nbIterations = NBLOOPS;
69 static double g_compressibility = COMPRESSIBILITY_DEFAULT;
70
71
72 /*_*******************************************************
73 *  Private functions
74 *********************************************************/
75 static size_t BMK_findMaxMem(U64 requiredMem)
76 {
77     size_t const step = 64 MB;
78     void* testmem = NULL;
79
80     requiredMem = (((requiredMem >> 26) + 1) << 26);
81     if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
82
83     requiredMem += step;
84     do {
85         testmem = malloc ((size_t)requiredMem);
86         requiredMem -= step;
87     } while (!testmem);
88
89     free (testmem);
90     return (size_t) requiredMem;
91 }
92
93
94 /*_*******************************************************
95 *  Benchmark wrappers
96 *********************************************************/
97
98 static ZSTD_CCtx* g_zcc = NULL;
99
100 static size_t
101 local_ZSTD_compress(const void* src, size_t srcSize,
102                     void* dst, size_t dstSize,
103                     void* buff2)
104 {
105     ZSTD_parameters p;
106     ZSTD_frameParameters f = { 1 /* contentSizeHeader*/, 0, 0 };
107     p.fParams = f;
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);
111 }
112
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,
116                                     void* buff2)
117 {
118     (void)src; (void)srcSize;
119     return ZSTD_decompress(dst, dstSize, buff2, g_cSize);
120 }
121
122 static ZSTD_DCtx* g_zdc = NULL;
123
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)
127 {
128     (void)src; (void)srcSize; (void)dst; (void)dstSize;
129     return ZSTD_decodeLiteralsBlock((ZSTD_DCtx*)g_zdc, buff2, g_cSize);
130 }
131
132 static size_t local_ZSTD_decodeSeqHeaders(const void* src, size_t srcSize, void* dst, size_t dstSize, void* buff2)
133 {
134     int nbSeq;
135     (void)src; (void)srcSize; (void)dst; (void)dstSize;
136     return ZSTD_decodeSeqHeaders(g_zdc, &nbSeq, buff2, g_cSize);
137 }
138 #endif
139
140 static ZSTD_CStream* g_cstream= NULL;
141 static size_t
142 local_ZSTD_compressStream(const void* src, size_t srcSize,
143                           void* dst, size_t dstCapacity,
144                           void* buff2)
145 {
146     ZSTD_outBuffer buffOut;
147     ZSTD_inBuffer buffIn;
148     ZSTD_parameters p;
149     ZSTD_frameParameters f = {1 /* contentSizeHeader*/, 0, 0};
150     p.fParams = f;
151     p.cParams = *(ZSTD_compressionParameters*)buff2;
152     ZSTD_initCStream_advanced(g_cstream, NULL, 0, p, ZSTD_CONTENTSIZE_UNKNOWN);
153     buffOut.dst = dst;
154     buffOut.size = dstCapacity;
155     buffOut.pos = 0;
156     buffIn.src = src;
157     buffIn.size = srcSize;
158     buffIn.pos = 0;
159     ZSTD_compressStream(g_cstream, &buffOut, &buffIn);
160     ZSTD_endStream(g_cstream, &buffOut);
161     return buffOut.pos;
162 }
163
164 static size_t
165 local_ZSTD_compress_generic_end(const void* src, size_t srcSize,
166                                 void* dst, size_t dstCapacity,
167                                 void* buff2)
168 {
169     (void)buff2;
170     return ZSTD_compress2(g_cstream, dst, dstCapacity, src, srcSize);
171 }
172
173 static size_t
174 local_ZSTD_compress_generic_continue(const void* src, size_t srcSize,
175                                      void* dst, size_t dstCapacity,
176                                      void* buff2)
177 {
178     ZSTD_outBuffer buffOut;
179     ZSTD_inBuffer buffIn;
180     (void)buff2;
181     buffOut.dst = dst;
182     buffOut.size = dstCapacity;
183     buffOut.pos = 0;
184     buffIn.src = src;
185     buffIn.size = srcSize;
186     buffIn.pos = 0;
187     ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
188     ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_end);
189     return buffOut.pos;
190 }
191
192 static size_t
193 local_ZSTD_compress_generic_T2_end(const void* src, size_t srcSize,
194                                    void* dst, size_t dstCapacity,
195                                    void* buff2)
196 {
197     (void)buff2;
198     ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_nbWorkers, 2);
199     return ZSTD_compress2(g_cstream, dst, dstCapacity, src, srcSize);
200 }
201
202 static size_t
203 local_ZSTD_compress_generic_T2_continue(const void* src, size_t srcSize,
204                                         void* dst, size_t dstCapacity,
205                                         void* buff2)
206 {
207     ZSTD_outBuffer buffOut;
208     ZSTD_inBuffer buffIn;
209     (void)buff2;
210     ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_nbWorkers, 2);
211     buffOut.dst = dst;
212     buffOut.size = dstCapacity;
213     buffOut.pos = 0;
214     buffIn.src = src;
215     buffIn.size = srcSize;
216     buffIn.pos = 0;
217     ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
218     while(ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_end)) {}
219     return buffOut.pos;
220 }
221
222 static ZSTD_DStream* g_dstream= NULL;
223 static size_t
224 local_ZSTD_decompressStream(const void* src, size_t srcSize,
225                             void* dst, size_t dstCapacity,
226                             void* buff2)
227 {
228     ZSTD_outBuffer buffOut;
229     ZSTD_inBuffer buffIn;
230     (void)src; (void)srcSize;
231     ZSTD_initDStream(g_dstream);
232     buffOut.dst = dst;
233     buffOut.size = dstCapacity;
234     buffOut.pos = 0;
235     buffIn.src = buff2;
236     buffIn.size = g_cSize;
237     buffIn.pos = 0;
238     ZSTD_decompressStream(g_dstream, &buffOut, &buffIn);
239     return buffOut.pos;
240 }
241
242 #ifndef ZSTD_DLL_IMPORT
243 static size_t local_ZSTD_compressContinue(const void* src, size_t srcSize,
244                                           void* dst, size_t dstCapacity,
245                                           void* buff2)
246 {
247     ZSTD_parameters p;
248     ZSTD_frameParameters f = { 1 /* contentSizeHeader*/, 0, 0 };
249     p.fParams = f;
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);
253 }
254
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,
258                                                   void* buff2)
259 {
260     BYTE firstBlockBuf[FIRST_BLOCK_SIZE];
261
262     ZSTD_parameters p;
263     ZSTD_frameParameters f = { 1, 0, 0 };
264     p.fParams = f;
265     p.cParams = *(ZSTD_compressionParameters*)buff2;
266     ZSTD_compressBegin_advanced(g_zcc, NULL, 0, p, srcSize);
267     memcpy(firstBlockBuf, src, FIRST_BLOCK_SIZE);
268
269     {   size_t const compressResult = ZSTD_compressContinue(g_zcc,
270                                             dst, dstCapacity,
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;
276         }
277         dst = (BYTE*)dst + compressResult;
278         dstCapacity -= compressResult;
279     }
280     return ZSTD_compressEnd(g_zcc, dst, dstCapacity,
281                             (const BYTE*)src + FIRST_BLOCK_SIZE,
282                             srcSize - FIRST_BLOCK_SIZE);
283 }
284
285 static size_t local_ZSTD_decompressContinue(const void* src, size_t srcSize,
286                                             void* dst, size_t dstCapacity,
287                                             void* buff2)
288 {
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;
294
295     (void)src; (void)srcSize;  /* unused */
296     ZSTD_decompressBegin(g_zdc);
297     while (ip < iend) {
298         size_t const iSize = ZSTD_nextSrcSizeToDecompress(g_zdc);
299         size_t const decodedSize = ZSTD_decompressContinue(g_zdc, op, remainingCapacity, ip, iSize);
300         ip += iSize;
301         regeneratedSize += decodedSize;
302         op += decodedSize;
303         remainingCapacity -= decodedSize;
304     }
305
306     return regeneratedSize;
307 }
308 #endif
309
310
311 /*_*******************************************************
312 *  Bench functions
313 *********************************************************/
314 static int benchMem(unsigned benchNb,
315                     const void* src, size_t srcSize,
316                     int cLevel, ZSTD_compressionParameters cparams)
317 {
318     size_t dstBuffSize = ZSTD_compressBound(srcSize);
319     BYTE*  dstBuff;
320     void*  dstBuff2;
321     void*  buff2;
322     const char* benchName;
323     BMK_benchFn_t benchFunction;
324     int errorcode = 0;
325
326     /* Selection */
327     switch(benchNb)
328     {
329     case 1:
330         benchFunction = local_ZSTD_compress; benchName = "compress";
331         break;
332     case 2:
333         benchFunction = local_ZSTD_decompress; benchName = "decompress";
334         break;
335 #ifndef ZSTD_DLL_IMPORT
336     case 11:
337         benchFunction = local_ZSTD_compressContinue; benchName = "compressContinue";
338         break;
339     case 12:
340         benchFunction = local_ZSTD_compressContinue_extDict; benchName = "compressContinue_extDict";
341         break;
342     case 13:
343         benchFunction = local_ZSTD_decompressContinue; benchName = "decompressContinue";
344         break;
345     case 31:
346         benchFunction = local_ZSTD_decodeLiteralsBlock; benchName = "decodeLiteralsBlock";
347         break;
348     case 32:
349         benchFunction = local_ZSTD_decodeSeqHeaders; benchName = "decodeSeqHeaders";
350         break;
351 #endif
352     case 41:
353         benchFunction = local_ZSTD_compressStream; benchName = "compressStream";
354         break;
355     case 42:
356         benchFunction = local_ZSTD_decompressStream; benchName = "decompressStream";
357         break;
358     case 51:
359         benchFunction = local_ZSTD_compress_generic_continue; benchName = "compress_generic, continue";
360         break;
361     case 52:
362         benchFunction = local_ZSTD_compress_generic_end; benchName = "compress_generic, end";
363         break;
364     case 61:
365         benchFunction = local_ZSTD_compress_generic_T2_continue; benchName = "compress_generic, -T2, continue";
366         break;
367     case 62:
368         benchFunction = local_ZSTD_compress_generic_T2_end; benchName = "compress_generic, -T2, end";
369         break;
370     default :
371         return 0;
372     }
373
374     /* Allocation */
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);
380         return 12;
381     }
382     buff2 = 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();
387
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); */
391
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);
400
401
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);
410
411     /* Preparation */
412     switch(benchNb)
413     {
414     case 1:
415         buff2 = &cparams;
416         break;
417     case 2:
418         g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, cLevel);
419         break;
420 #ifndef ZSTD_DLL_IMPORT
421     case 11:
422         buff2 = &cparams;
423         break;
424     case 12:
425         buff2 = &cparams;
426         break;
427     case 13 :
428         g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, cLevel);
429         break;
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");
440                 goto _cleanOut;
441             }
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);
446             break;
447         }
448     case 32:   /* ZSTD_decodeSeqHeaders */
449         {   blockProperties_t bp;
450             ZSTD_frameHeader zfp;
451             const BYTE* ip = dstBuff;
452             const BYTE* iend;
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");
462                 goto _cleanOut;
463             }
464             iend = ip + ZSTD_blockHeaderSize + cBlockSize;   /* End of first block */
465             ip += ZSTD_blockHeaderSize;                      /* skip block header */
466             ZSTD_decompressBegin(g_zdc);
467             assert(iend > ip);
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 */
472             break;
473         }
474 #else
475     case 31:
476         goto _cleanOut;
477 #endif
478     case 41 :
479         buff2 = &cparams;
480         break;
481     case 42 :
482         g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, cLevel);
483         break;
484
485     /* test functions */
486     /* convention: test functions have ID > 100 */
487
488     default : ;
489     }
490
491      /* warming up dstBuff */
492     { size_t i; for (i=0; i<dstBuffSize; i++) dstBuff[i]=(BYTE)i; }
493
494     /* benchmark loop */
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 */
501         assert(tfs != NULL);
502
503         bp.benchFn = benchFunction;
504         bp.benchPayload = buff2;
505         bp.initFn = NULL;
506         bp.initPayload = NULL;
507         bp.errorFn = ZSTD_isError;
508         bp.blockCount = 1;
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;
515
516         for (;;) {
517             BMK_runOutcome_t const bOutcome = BMK_benchTimedFn(tfs, bp);
518
519             if (!BMK_isSuccessful_runOutcome(bOutcome)) {
520                 DISPLAY("ERROR benchmarking function ! ! \n");
521                 errorcode = 1;
522                 goto _cleanOut;
523             }
524
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) ",
529                         benchNb, benchName,
530                         (double)srcSize * TIMELOOP_NANOSEC / bestResult.nanoSecPerRun / MB_UNIT,
531                         (unsigned)newResult.sumOfReturn );
532             }
533
534             if ( BMK_isCompleted_TimedFn(tfs) ) break;
535         }
536         BMK_freeTimedFnState(tfs);
537     }
538     DISPLAY("\n");
539
540 _cleanOut:
541     free(dstBuff);
542     free(dstBuff2);
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;
547     return errorcode;
548 }
549
550
551 static int benchSample(U32 benchNb,
552                        int cLevel, ZSTD_compressionParameters cparams)
553 {
554     size_t const benchedSize = g_sampleSize;
555     const char* const name = "Sample 10MiB";
556
557     /* Allocation */
558     void* const origBuff = malloc(benchedSize);
559     if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); return 12; }
560
561     /* Fill buffer */
562     RDG_genBuffer(origBuff, benchedSize, g_compressibility, 0.0, 0);
563
564     /* bench */
565     DISPLAY("\r%70s\r", "");
566     DISPLAY(" %s : \n", name);
567     if (benchNb) {
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);
572     }   }
573
574     free(origBuff);
575     return 0;
576 }
577
578
579 static int benchFiles(U32 benchNb,
580                       const char** fileNamesTable, const int nbFiles,
581                       int cLevel, ZSTD_compressionParameters cparams)
582 {
583     /* Loop for each file */
584     int fileIdx;
585     for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
586         const char* const inFileName = fileNamesTable[fileIdx];
587         FILE* const inFile = fopen( inFileName, "rb" );
588         size_t benchedSize;
589
590         /* Check file existence */
591         if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
592
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);
597                 fclose(inFile);
598                 return 11;
599             }
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));
606         }   }
607
608         /* Alloc */
609         {   void* const origBuff = malloc(benchedSize);
610             if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); fclose(inFile); return 12; }
611
612             /* Fill input buffer */
613             DISPLAY("Loading %s...       \r", inFileName);
614             {   size_t const readSize = fread(origBuff, 1, benchedSize, inFile);
615                 fclose(inFile);
616                 if (readSize != benchedSize) {
617                     DISPLAY("\nError: problem reading file '%s' !!    \n", inFileName);
618                     free(origBuff);
619                     return 13;
620             }   }
621
622             /* bench */
623             DISPLAY("\r%70s\r", "");   /* blank line */
624             DISPLAY(" %s : \n", inFileName);
625             if (benchNb) {
626                 benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
627             } else {
628                 for (benchNb=0; benchNb<100; benchNb++) {
629                     benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
630             }   }
631
632             free(origBuff);
633     }   }
634
635     return 0;
636 }
637
638
639
640 /*_*******************************************************
641 *  Argument Parsing
642 *********************************************************/
643
644 #define ERROR_OUT(msg) { DISPLAY("%s \n", msg); exit(1); }
645
646 static unsigned readU32FromChar(const char** stringPtr)
647 {
648     const char errorMsg[] = "error: numeric value too large";
649     unsigned result = 0;
650     while ((**stringPtr >='0') && (**stringPtr <='9')) {
651         unsigned const max = (((unsigned)(-1)) / 10) - 1;
652         if (result > max) ERROR_OUT(errorMsg);
653         result *= 10;
654         result += (unsigned)(**stringPtr - '0');
655         (*stringPtr)++ ;
656     }
657     if ((**stringPtr=='K') || (**stringPtr=='M')) {
658         unsigned const maxK = ((unsigned)(-1)) >> 10;
659         if (result > maxK) ERROR_OUT(errorMsg);
660         result <<= 10;
661         if (**stringPtr=='M') {
662             if (result > maxK) ERROR_OUT(errorMsg);
663             result <<= 10;
664         }
665         (*stringPtr)++;  /* skip `K` or `M` */
666         if (**stringPtr=='i') (*stringPtr)++;
667         if (**stringPtr=='B') (*stringPtr)++;
668     }
669     return result;
670 }
671
672 static int longCommandWArg(const char** stringPtr, const char* longCommand)
673 {
674     size_t const comSize = strlen(longCommand);
675     int const result = !strncmp(*stringPtr, longCommand, comSize);
676     if (result) *stringPtr += comSize;
677     return result;
678 }
679
680
681 /*_*******************************************************
682 *  Command line
683 *********************************************************/
684
685 static int usage(const char* exename)
686 {
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");
691     return 0;
692 }
693
694 static int usage_advanced(const char* exename)
695 {
696     usage(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");
703     return 0;
704 }
705
706 static int badusage(const char* exename)
707 {
708     DISPLAY("Wrong parameters\n");
709     usage(exename);
710     return 1;
711 }
712
713 int main(int argc, const char** argv)
714 {
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);
721
722     DISPLAY(WELCOME_MESSAGE);
723     if (argc<1) return badusage(exename);
724
725     for (argNb=1; argNb<argc; argNb++) {
726         const char* argument = argv[argNb];
727         assert(argument != NULL);
728
729         if (longCommandWArg(&argument, "--zstd=")) {
730             for ( ; ;) {
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");
740                 return 1;
741             }
742
743             /* check end of string */
744             if (argument[0] != 0) {
745                 DISPLAY("invalid --zstd= format \n");
746                 return 1;
747             } else {
748                 continue;
749             }
750
751         } else if (argument[0]=='-') { /* Commands (note : aggregated commands are allowed) */
752             argument++;
753             while (argument[0]!=0) {
754
755                 switch(argument[0])
756                 {
757                     /* Display help on usage */
758                 case 'h':
759                 case 'H': return usage_advanced(exename);
760
761                     /* Pause at the end (hidden option) */
762                 case 'p': main_pause = 1; break;
763
764                     /* Select specific algorithm to bench */
765                 case 'b':
766                     argument++;
767                     benchNb = readU32FromChar(&argument);
768                     break;
769
770                     /* Modify Nb Iterations */
771                 case 'i':
772                     argument++;
773                     g_nbIterations = readU32FromChar(&argument);
774                     break;
775
776                     /* Select compressibility of synthetic sample */
777                 case 'P':
778                     argument++;
779                     g_compressibility = (double)readU32FromChar(&argument) / 100.;
780                     break;
781                 case 'l':
782                     argument++;
783                     cLevel = (int)readU32FromChar(&argument);
784                     cparams = ZSTD_getCParams(cLevel, 0, 0);
785                     break;
786
787                     /* Unknown command */
788                 default : return badusage(exename);
789                 }
790             }
791             continue;
792         }
793
794         /* first provided filename is input */
795         if (!input_filename) { input_filename=argument; filenamesStart=argNb; continue; }
796     }
797
798
799
800     if (filenamesStart==0)   /* no input file */
801         result = benchSample(benchNb, cLevel, cparams);
802     else
803         result = benchFiles(benchNb, argv+filenamesStart, argc-filenamesStart, cLevel, cparams);
804
805     if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; }
806
807     return result;
808 }