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