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