blob: 1fc2117f69a70534f14a7356bf848c29e731c963 [file] [log] [blame]
inikepdfef5dd2016-09-22 10:23:26 +02001/**
2 * Copyright (c) 2016-present, Yann Collet, Przemyslaw Skibinski, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 */
9
10
11/* *************************************
12* Includes
13***************************************/
14#include "util.h" /* Compiler options, UTIL_GetFileSize, UTIL_sleep */
15#include <stdlib.h> /* malloc, free */
16#include <string.h> /* memset */
17#include <stdio.h> /* fprintf, fopen, ftello64 */
18#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
inikepb88accf2016-09-23 13:38:02 +020019#include <ctype.h> /* toupper */
inikepdfef5dd2016-09-22 10:23:26 +020020
21#include "mem.h"
22#define ZSTD_STATIC_LINKING_ONLY
23#include "zstd.h"
24#include "datagen.h" /* RDG_genBuffer */
25#include "xxhash.h"
26
inikep8e8b0462016-09-22 14:42:32 +020027#include "zstd_zlibwrapper.h"
inikep54320ce2016-09-22 11:52:53 +020028
inikepdfef5dd2016-09-22 10:23:26 +020029
30
31/*-************************************
32* Tuning parameters
33**************************************/
34#ifndef ZSTDCLI_CLEVEL_DEFAULT
35# define ZSTDCLI_CLEVEL_DEFAULT 3
36#endif
37
38
39
40/*-************************************
41* Constants
42**************************************/
inikep54320ce2016-09-22 11:52:53 +020043#define COMPRESSOR_NAME "Zstandard wrapper for zlib command line interface"
inikepdfef5dd2016-09-22 10:23:26 +020044#ifndef ZSTD_VERSION
45# define ZSTD_VERSION "v" ZSTD_VERSION_STRING
46#endif
47#define AUTHOR "Yann Collet"
48#define WELCOME_MESSAGE "*** %s %i-bits %s, by %s ***\n", COMPRESSOR_NAME, (int)(sizeof(size_t)*8), ZSTD_VERSION, AUTHOR
49
50#ifndef ZSTD_GIT_COMMIT
51# define ZSTD_GIT_COMMIT_STRING ""
52#else
53# define ZSTD_GIT_COMMIT_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_GIT_COMMIT)
54#endif
55
56#define NBLOOPS 3
57#define TIMELOOP_MICROSEC 1*1000000ULL /* 1 second */
58#define ACTIVEPERIOD_MICROSEC 70*1000000ULL /* 70 seconds */
59#define COOLPERIOD_SEC 10
60
61#define KB *(1 <<10)
62#define MB *(1 <<20)
63#define GB *(1U<<30)
64
65static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31));
66
67static U32 g_compressibilityDefault = 50;
68
69
70/* *************************************
71* console display
72***************************************/
73#define DEFAULT_DISPLAY_LEVEL 2
74#define DISPLAY(...) fprintf(displayOut, __VA_ARGS__)
75#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
Sean Purcell042ba122017-03-23 11:13:52 -070076static int g_displayLevel = DEFAULT_DISPLAY_LEVEL; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
inikepdfef5dd2016-09-22 10:23:26 +020077static FILE* displayOut;
78
79#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
80 if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \
81 { g_time = clock(); DISPLAY(__VA_ARGS__); \
Sean Purcell042ba122017-03-23 11:13:52 -070082 if (g_displayLevel>=4) fflush(displayOut); } }
inikepdfef5dd2016-09-22 10:23:26 +020083static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100;
84static clock_t g_time = 0;
85
86
87/* *************************************
88* Exceptions
89***************************************/
90#ifndef DEBUG
91# define DEBUG 0
92#endif
Yann Collet7bd1a292017-06-21 11:50:33 -070093#define DEBUGOUTPUT(...) { if (DEBUG) DISPLAY(__VA_ARGS__); }
inikepdfef5dd2016-09-22 10:23:26 +020094#define EXM_THROW(error, ...) \
95{ \
96 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
97 DISPLAYLEVEL(1, "Error %i : ", error); \
98 DISPLAYLEVEL(1, __VA_ARGS__); \
99 DISPLAYLEVEL(1, "\n"); \
100 exit(error); \
101}
102
103
104/* *************************************
105* Benchmark Parameters
106***************************************/
107static U32 g_nbIterations = NBLOOPS;
108static size_t g_blockSize = 0;
109int g_additionalParam = 0;
110
111void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; }
112
113void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; }
114
115void BMK_SetNbIterations(unsigned nbLoops)
116{
117 g_nbIterations = nbLoops;
118 DISPLAYLEVEL(3, "- test >= %u seconds per compression / decompression -\n", g_nbIterations);
119}
120
121void BMK_SetBlockSize(size_t blockSize)
122{
123 g_blockSize = blockSize;
124 DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10));
125}
126
127
128/* ********************************************************
129* Bench functions
130**********************************************************/
Yann Collet4f818182017-04-17 17:57:35 -0700131#undef MIN
132#undef MAX
133#define MIN(a,b) ((a)<(b) ? (a) : (b))
134#define MAX(a,b) ((a)>(b) ? (a) : (b))
135
inikepdfef5dd2016-09-22 10:23:26 +0200136typedef struct
137{
Przemyslaw Skibinski6cecb352016-11-04 17:49:17 +0100138 z_const char* srcPtr;
inikepdfef5dd2016-09-22 10:23:26 +0200139 size_t srcSize;
140 char* cPtr;
141 size_t cRoom;
142 size_t cSize;
143 char* resPtr;
144 size_t resSize;
145} blockParam_t;
146
inikepf7ab3ad2016-09-22 17:59:10 +0200147typedef enum { BMK_ZSTD, BMK_ZSTD_STREAM, BMK_ZLIB, BMK_ZWRAP_ZLIB, BMK_ZWRAP_ZSTD, BMK_ZLIB_REUSE, BMK_ZWRAP_ZLIB_REUSE, BMK_ZWRAP_ZSTD_REUSE } BMK_compressor;
inikep54320ce2016-09-22 11:52:53 +0200148
inikepdfef5dd2016-09-22 10:23:26 +0200149
Przemyslaw Skibinski6cecb352016-11-04 17:49:17 +0100150static int BMK_benchMem(z_const void* srcBuffer, size_t srcSize,
inikepdfef5dd2016-09-22 10:23:26 +0200151 const char* displayName, int cLevel,
152 const size_t* fileSizes, U32 nbFiles,
inikep54320ce2016-09-22 11:52:53 +0200153 const void* dictBuffer, size_t dictBufferSize, BMK_compressor compressor)
inikepdfef5dd2016-09-22 10:23:26 +0200154{
155 size_t const blockSize = (g_blockSize>=32 ? g_blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ;
156 size_t const avgSize = MIN(g_blockSize, (srcSize / nbFiles));
157 U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
158 blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t));
159 size_t const maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */
160 void* const compressedBuffer = malloc(maxCompressedSize);
161 void* const resultBuffer = malloc(srcSize);
162 ZSTD_CCtx* const ctx = ZSTD_createCCtx();
163 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
164 U32 nbBlocks;
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100165 UTIL_freq_t ticksPerSecond;
inikepdfef5dd2016-09-22 10:23:26 +0200166
167 /* checks */
168 if (!compressedBuffer || !resultBuffer || !blockTable || !ctx || !dctx)
169 EXM_THROW(31, "allocation error : not enough memory");
170
171 /* init */
172 if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */
173 UTIL_initTimer(&ticksPerSecond);
174
175 /* Init blockTable data */
Przemyslaw Skibinski6cecb352016-11-04 17:49:17 +0100176 { z_const char* srcPtr = (z_const char*)srcBuffer;
inikepdfef5dd2016-09-22 10:23:26 +0200177 char* cPtr = (char*)compressedBuffer;
178 char* resPtr = (char*)resultBuffer;
179 U32 fileNb;
180 for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
181 size_t remaining = fileSizes[fileNb];
182 U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize);
183 U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
184 for ( ; nbBlocks<blockEnd; nbBlocks++) {
185 size_t const thisBlockSize = MIN(remaining, blockSize);
186 blockTable[nbBlocks].srcPtr = srcPtr;
187 blockTable[nbBlocks].cPtr = cPtr;
188 blockTable[nbBlocks].resPtr = resPtr;
189 blockTable[nbBlocks].srcSize = thisBlockSize;
190 blockTable[nbBlocks].cRoom = ZSTD_compressBound(thisBlockSize);
191 srcPtr += thisBlockSize;
192 cPtr += blockTable[nbBlocks].cRoom;
193 resPtr += thisBlockSize;
194 remaining -= thisBlockSize;
195 } } }
196
197 /* warmimg up memory */
198 RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
199
200 /* Bench */
201 { U64 fastestC = (U64)(-1LL), fastestD = (U64)(-1LL);
202 U64 const crcOrig = XXH64(srcBuffer, srcSize, 0);
203 UTIL_time_t coolTime;
204 U64 const maxTime = (g_nbIterations * TIMELOOP_MICROSEC) + 100;
205 U64 totalCTime=0, totalDTime=0;
206 U32 cCompleted=0, dCompleted=0;
207# define NB_MARKS 4
208 const char* const marks[NB_MARKS] = { " |", " /", " =", "\\" };
209 U32 markNb = 0;
210 size_t cSize = 0;
211 double ratio = 0.;
212
213 UTIL_getTime(&coolTime);
214 DISPLAYLEVEL(2, "\r%79s\r", "");
215 while (!cCompleted | !dCompleted) {
216 UTIL_time_t clockStart;
217 U64 clockLoop = g_nbIterations ? TIMELOOP_MICROSEC : 1;
218
219 /* overheat protection */
220 if (UTIL_clockSpanMicro(coolTime, ticksPerSecond) > ACTIVEPERIOD_MICROSEC) {
221 DISPLAYLEVEL(2, "\rcooling down ... \r");
222 UTIL_sleep(COOLPERIOD_SEC);
223 UTIL_getTime(&coolTime);
224 }
225
226 /* Compression */
227 DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (U32)srcSize);
228 if (!cCompleted) memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */
229
230 UTIL_sleepMilli(1); /* give processor time to other processes */
231 UTIL_waitForNextTick(ticksPerSecond);
232 UTIL_getTime(&clockStart);
233
234 if (!cCompleted) { /* still some time to do compression tests */
inikepdfef5dd2016-09-22 10:23:26 +0200235 U32 nbLoops = 0;
inikep54320ce2016-09-22 11:52:53 +0200236 if (compressor == BMK_ZSTD) {
237 ZSTD_parameters const zparams = ZSTD_getParams(cLevel, avgSize, dictBufferSize);
238 ZSTD_customMem const cmem = { NULL, NULL, NULL };
Yann Collet7bd1a292017-06-21 11:50:33 -0700239 ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictBufferSize, 1 /*byRef*/, ZSTD_dm_auto, zparams.cParams, cmem);
inikep54320ce2016-09-22 11:52:53 +0200240 if (cdict==NULL) EXM_THROW(1, "ZSTD_createCDict_advanced() allocation failure");
inikep8e8b0462016-09-22 14:42:32 +0200241
inikep54320ce2016-09-22 11:52:53 +0200242 do {
243 U32 blockNb;
Przemyslaw Skibinski96fca2b2016-11-25 14:36:27 +0100244 size_t rSize;
inikep54320ce2016-09-22 11:52:53 +0200245 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
Przemyslaw Skibinski96fca2b2016-11-25 14:36:27 +0100246 if (dictBufferSize) {
247 rSize = ZSTD_compress_usingCDict(ctx,
inikep54320ce2016-09-22 11:52:53 +0200248 blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
249 blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize,
250 cdict);
Przemyslaw Skibinski96fca2b2016-11-25 14:36:27 +0100251 } else {
252 rSize = ZSTD_compressCCtx (ctx,
253 blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
254 blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize, cLevel);
255 }
inikep54320ce2016-09-22 11:52:53 +0200256 if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingCDict() failed : %s", ZSTD_getErrorName(rSize));
257 blockTable[blockNb].cSize = rSize;
258 }
259 nbLoops++;
260 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
261 ZSTD_freeCDict(cdict);
inikepf7ab3ad2016-09-22 17:59:10 +0200262 } else if (compressor == BMK_ZSTD_STREAM) {
inikep8e8b0462016-09-22 14:42:32 +0200263 ZSTD_parameters const zparams = ZSTD_getParams(cLevel, avgSize, dictBufferSize);
264 ZSTD_inBuffer inBuffer;
265 ZSTD_outBuffer outBuffer;
266 ZSTD_CStream* zbc = ZSTD_createCStream();
inikepf7ab3ad2016-09-22 17:59:10 +0200267 size_t rSize;
inikep8e8b0462016-09-22 14:42:32 +0200268 if (zbc == NULL) EXM_THROW(1, "ZSTD_createCStream() allocation failure");
inikepa03b7a72016-09-26 22:11:55 +0200269 rSize = ZSTD_initCStream_advanced(zbc, dictBuffer, dictBufferSize, zparams, avgSize);
inikepf7ab3ad2016-09-22 17:59:10 +0200270 if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_initCStream_advanced() failed : %s", ZSTD_getErrorName(rSize));
inikep8e8b0462016-09-22 14:42:32 +0200271 do {
272 U32 blockNb;
273 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
inikepcf3ec082016-09-23 10:30:26 +0200274 rSize = ZSTD_resetCStream(zbc, blockTable[blockNb].srcSize);
inikepf7ab3ad2016-09-22 17:59:10 +0200275 if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_resetCStream() failed : %s", ZSTD_getErrorName(rSize));
inikep8e8b0462016-09-22 14:42:32 +0200276 inBuffer.src = blockTable[blockNb].srcPtr;
277 inBuffer.size = blockTable[blockNb].srcSize;
278 inBuffer.pos = 0;
279 outBuffer.dst = blockTable[blockNb].cPtr;
280 outBuffer.size = blockTable[blockNb].cRoom;
281 outBuffer.pos = 0;
282 rSize = ZSTD_compressStream(zbc, &outBuffer, &inBuffer);
283 if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compressStream() failed : %s", ZSTD_getErrorName(rSize));
284 rSize = ZSTD_endStream(zbc, &outBuffer);
285 if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_endStream() failed : %s", ZSTD_getErrorName(rSize));
286 blockTable[blockNb].cSize = outBuffer.pos;
287 }
288 nbLoops++;
289 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
290 ZSTD_freeCStream(zbc);
inikepf7ab3ad2016-09-22 17:59:10 +0200291 } else if (compressor == BMK_ZWRAP_ZLIB_REUSE || compressor == BMK_ZWRAP_ZSTD_REUSE || compressor == BMK_ZLIB_REUSE) {
292 z_stream def;
293 int ret;
inikep706876f2016-09-27 16:56:07 +0200294 int useSetDict = (dictBuffer != NULL);
inikep252c20d2016-09-23 09:08:40 +0200295 if (compressor == BMK_ZLIB_REUSE || compressor == BMK_ZWRAP_ZLIB_REUSE) ZWRAP_useZSTDcompression(0);
296 else ZWRAP_useZSTDcompression(1);
inikepf7ab3ad2016-09-22 17:59:10 +0200297 def.zalloc = Z_NULL;
298 def.zfree = Z_NULL;
299 def.opaque = Z_NULL;
300 ret = deflateInit(&def, cLevel);
301 if (ret != Z_OK) EXM_THROW(1, "deflateInit failure");
inikepcf3ec082016-09-23 10:30:26 +0200302 /* if (ZWRAP_isUsingZSTDcompression()) {
inikep252c20d2016-09-23 09:08:40 +0200303 ret = ZWRAP_setPledgedSrcSize(&def, avgSize);
304 if (ret != Z_OK) EXM_THROW(1, "ZWRAP_setPledgedSrcSize failure");
inikep2bb83e82016-09-23 18:59:53 +0200305 } */
inikepf7ab3ad2016-09-22 17:59:10 +0200306 do {
307 U32 blockNb;
308 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
inikep706876f2016-09-27 16:56:07 +0200309 if (ZWRAP_isUsingZSTDcompression())
inikep20859af2016-09-27 17:27:43 +0200310 ret = ZWRAP_deflateReset_keepDict(&def); /* reuse dictionary to make compression faster */
inikep706876f2016-09-27 16:56:07 +0200311 else
312 ret = deflateReset(&def);
inikepf7ab3ad2016-09-22 17:59:10 +0200313 if (ret != Z_OK) EXM_THROW(1, "deflateReset failure");
inikep706876f2016-09-27 16:56:07 +0200314 if (useSetDict) {
inikepa03b7a72016-09-26 22:11:55 +0200315 ret = deflateSetDictionary(&def, dictBuffer, dictBufferSize);
316 if (ret != Z_OK) EXM_THROW(1, "deflateSetDictionary failure");
inikep20859af2016-09-27 17:27:43 +0200317 if (ZWRAP_isUsingZSTDcompression()) useSetDict = 0; /* zstd doesn't require deflateSetDictionary after ZWRAP_deflateReset_keepDict */
inikepa03b7a72016-09-26 22:11:55 +0200318 }
Przemyslaw Skibinski6cecb352016-11-04 17:49:17 +0100319 def.next_in = (z_const void*) blockTable[blockNb].srcPtr;
Yann Collet9ceb49e2016-12-22 15:26:33 +0100320 def.avail_in = (uInt)blockTable[blockNb].srcSize;
inikepf7ab3ad2016-09-22 17:59:10 +0200321 def.total_in = 0;
322 def.next_out = (void*) blockTable[blockNb].cPtr;
Yann Collet9ceb49e2016-12-22 15:26:33 +0100323 def.avail_out = (uInt)blockTable[blockNb].cRoom;
inikepf7ab3ad2016-09-22 17:59:10 +0200324 def.total_out = 0;
325 ret = deflate(&def, Z_FINISH);
inikep2bb83e82016-09-23 18:59:53 +0200326 if (ret != Z_STREAM_END) EXM_THROW(1, "deflate failure ret=%d srcSize=%d" , ret, (int)blockTable[blockNb].srcSize);
inikepf7ab3ad2016-09-22 17:59:10 +0200327 blockTable[blockNb].cSize = def.total_out;
328 }
329 nbLoops++;
330 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
331 ret = deflateEnd(&def);
332 if (ret != Z_OK) EXM_THROW(1, "deflateEnd failure");
inikep54320ce2016-09-22 11:52:53 +0200333 } else {
inikepf7ab3ad2016-09-22 17:59:10 +0200334 z_stream def;
inikep252c20d2016-09-23 09:08:40 +0200335 if (compressor == BMK_ZLIB || compressor == BMK_ZWRAP_ZLIB) ZWRAP_useZSTDcompression(0);
336 else ZWRAP_useZSTDcompression(1);
inikep54320ce2016-09-22 11:52:53 +0200337 do {
338 U32 blockNb;
339 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
inikep54320ce2016-09-22 11:52:53 +0200340 int ret;
341 def.zalloc = Z_NULL;
342 def.zfree = Z_NULL;
343 def.opaque = Z_NULL;
344 ret = deflateInit(&def, cLevel);
345 if (ret != Z_OK) EXM_THROW(1, "deflateInit failure");
inikepa03b7a72016-09-26 22:11:55 +0200346 if (dictBuffer) {
347 ret = deflateSetDictionary(&def, dictBuffer, dictBufferSize);
348 if (ret != Z_OK) EXM_THROW(1, "deflateSetDictionary failure");
349 }
Przemyslaw Skibinski6cecb352016-11-04 17:49:17 +0100350 def.next_in = (z_const void*) blockTable[blockNb].srcPtr;
Yann Collet9ceb49e2016-12-22 15:26:33 +0100351 def.avail_in = (uInt)blockTable[blockNb].srcSize;
inikep54320ce2016-09-22 11:52:53 +0200352 def.total_in = 0;
353 def.next_out = (void*) blockTable[blockNb].cPtr;
Yann Collet9ceb49e2016-12-22 15:26:33 +0100354 def.avail_out = (uInt)blockTable[blockNb].cRoom;
inikep54320ce2016-09-22 11:52:53 +0200355 def.total_out = 0;
356 ret = deflate(&def, Z_FINISH);
357 if (ret != Z_STREAM_END) EXM_THROW(1, "deflate failure");
358 ret = deflateEnd(&def);
359 if (ret != Z_OK) EXM_THROW(1, "deflateEnd failure");
360 blockTable[blockNb].cSize = def.total_out;
361 }
362 nbLoops++;
363 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
364 }
inikepdfef5dd2016-09-22 10:23:26 +0200365 { U64 const clockSpan = UTIL_clockSpanMicro(clockStart, ticksPerSecond);
366 if (clockSpan < fastestC*nbLoops) fastestC = clockSpan / nbLoops;
367 totalCTime += clockSpan;
368 cCompleted = totalCTime>maxTime;
369 } }
370
371 cSize = 0;
372 { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; }
373 ratio = (double)srcSize / (double)cSize;
374 markNb = (markNb+1) % NB_MARKS;
375 DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r",
376 marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratio,
377 (double)srcSize / fastestC );
378
379 (void)fastestD; (void)crcOrig; /* unused when decompression disabled */
380#if 1
381 /* Decompression */
382 if (!dCompleted) memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */
383
384 UTIL_sleepMilli(1); /* give processor time to other processes */
385 UTIL_waitForNextTick(ticksPerSecond);
386 UTIL_getTime(&clockStart);
387
388 if (!dCompleted) {
389 U32 nbLoops = 0;
inikepf71828f2016-09-22 15:55:01 +0200390 if (compressor == BMK_ZSTD) {
inikep54320ce2016-09-22 11:52:53 +0200391 ZSTD_DDict* ddict = ZSTD_createDDict(dictBuffer, dictBufferSize);
392 if (!ddict) EXM_THROW(2, "ZSTD_createDDict() allocation failure");
393 do {
394 U32 blockNb;
395 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
396 size_t const regenSize = ZSTD_decompress_usingDDict(dctx,
397 blockTable[blockNb].resPtr, blockTable[blockNb].srcSize,
398 blockTable[blockNb].cPtr, blockTable[blockNb].cSize,
399 ddict);
400 if (ZSTD_isError(regenSize)) {
401 DISPLAY("ZSTD_decompress_usingDDict() failed on block %u : %s \n",
402 blockNb, ZSTD_getErrorName(regenSize));
403 clockLoop = 0; /* force immediate test end */
404 break;
405 }
406 blockTable[blockNb].resSize = regenSize;
inikepdfef5dd2016-09-22 10:23:26 +0200407 }
inikep54320ce2016-09-22 11:52:53 +0200408 nbLoops++;
409 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
410 ZSTD_freeDDict(ddict);
inikepf7ab3ad2016-09-22 17:59:10 +0200411 } else if (compressor == BMK_ZSTD_STREAM) {
inikepf71828f2016-09-22 15:55:01 +0200412 ZSTD_inBuffer inBuffer;
413 ZSTD_outBuffer outBuffer;
414 ZSTD_DStream* zbd = ZSTD_createDStream();
inikepf7ab3ad2016-09-22 17:59:10 +0200415 size_t rSize;
inikepf71828f2016-09-22 15:55:01 +0200416 if (zbd == NULL) EXM_THROW(1, "ZSTD_createDStream() allocation failure");
inikepa03b7a72016-09-26 22:11:55 +0200417 rSize = ZSTD_initDStream_usingDict(zbd, dictBuffer, dictBufferSize);
inikepf7ab3ad2016-09-22 17:59:10 +0200418 if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_initDStream() failed : %s", ZSTD_getErrorName(rSize));
inikepf71828f2016-09-22 15:55:01 +0200419 do {
420 U32 blockNb;
421 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
inikepf7ab3ad2016-09-22 17:59:10 +0200422 rSize = ZSTD_resetDStream(zbd);
423 if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_resetDStream() failed : %s", ZSTD_getErrorName(rSize));
inikepf71828f2016-09-22 15:55:01 +0200424 inBuffer.src = blockTable[blockNb].cPtr;
425 inBuffer.size = blockTable[blockNb].cSize;
426 inBuffer.pos = 0;
427 outBuffer.dst = blockTable[blockNb].resPtr;
428 outBuffer.size = blockTable[blockNb].srcSize;
429 outBuffer.pos = 0;
430 rSize = ZSTD_decompressStream(zbd, &outBuffer, &inBuffer);
431 if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_decompressStream() failed : %s", ZSTD_getErrorName(rSize));
432 blockTable[blockNb].resSize = outBuffer.pos;
433 }
434 nbLoops++;
435 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
436 ZSTD_freeDStream(zbd);
inikepf7ab3ad2016-09-22 17:59:10 +0200437 } else if (compressor == BMK_ZWRAP_ZLIB_REUSE || compressor == BMK_ZWRAP_ZSTD_REUSE || compressor == BMK_ZLIB_REUSE) {
438 z_stream inf;
439 int ret;
inikep252c20d2016-09-23 09:08:40 +0200440 if (compressor == BMK_ZLIB_REUSE) ZWRAP_setDecompressionType(ZWRAP_FORCE_ZLIB);
441 else ZWRAP_setDecompressionType(ZWRAP_AUTO);
inikepf7ab3ad2016-09-22 17:59:10 +0200442 inf.zalloc = Z_NULL;
443 inf.zfree = Z_NULL;
444 inf.opaque = Z_NULL;
445 ret = inflateInit(&inf);
446 if (ret != Z_OK) EXM_THROW(1, "inflateInit failure");
447 do {
448 U32 blockNb;
449 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
inikep706876f2016-09-27 16:56:07 +0200450 if (ZWRAP_isUsingZSTDdecompression(&inf))
inikep20859af2016-09-27 17:27:43 +0200451 ret = ZWRAP_inflateReset_keepDict(&inf); /* reuse dictionary to make decompression faster; inflate will return Z_NEED_DICT only for the first time */
inikep706876f2016-09-27 16:56:07 +0200452 else
453 ret = inflateReset(&inf);
inikepf7ab3ad2016-09-22 17:59:10 +0200454 if (ret != Z_OK) EXM_THROW(1, "inflateReset failure");
Przemyslaw Skibinski6cecb352016-11-04 17:49:17 +0100455 inf.next_in = (z_const void*) blockTable[blockNb].cPtr;
Yann Collet9ceb49e2016-12-22 15:26:33 +0100456 inf.avail_in = (uInt)blockTable[blockNb].cSize;
inikepf7ab3ad2016-09-22 17:59:10 +0200457 inf.total_in = 0;
458 inf.next_out = (void*) blockTable[blockNb].resPtr;
Yann Collet9ceb49e2016-12-22 15:26:33 +0100459 inf.avail_out = (uInt)blockTable[blockNb].srcSize;
inikepf7ab3ad2016-09-22 17:59:10 +0200460 inf.total_out = 0;
461 ret = inflate(&inf, Z_FINISH);
inikepa03b7a72016-09-26 22:11:55 +0200462 if (ret == Z_NEED_DICT) {
463 ret = inflateSetDictionary(&inf, dictBuffer, dictBufferSize);
464 if (ret != Z_OK) EXM_THROW(1, "inflateSetDictionary failure");
465 ret = inflate(&inf, Z_FINISH);
466 }
inikepf7ab3ad2016-09-22 17:59:10 +0200467 if (ret != Z_STREAM_END) EXM_THROW(1, "inflate failure");
468 blockTable[blockNb].resSize = inf.total_out;
469 }
470 nbLoops++;
471 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
472 ret = inflateEnd(&inf);
473 if (ret != Z_OK) EXM_THROW(1, "inflateEnd failure");
inikep54320ce2016-09-22 11:52:53 +0200474 } else {
inikepf7ab3ad2016-09-22 17:59:10 +0200475 z_stream inf;
inikep252c20d2016-09-23 09:08:40 +0200476 if (compressor == BMK_ZLIB) ZWRAP_setDecompressionType(ZWRAP_FORCE_ZLIB);
477 else ZWRAP_setDecompressionType(ZWRAP_AUTO);
inikep54320ce2016-09-22 11:52:53 +0200478 do {
479 U32 blockNb;
480 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
inikep54320ce2016-09-22 11:52:53 +0200481 int ret;
482 inf.zalloc = Z_NULL;
483 inf.zfree = Z_NULL;
484 inf.opaque = Z_NULL;
485 ret = inflateInit(&inf);
486 if (ret != Z_OK) EXM_THROW(1, "inflateInit failure");
Przemyslaw Skibinski6cecb352016-11-04 17:49:17 +0100487 inf.next_in = (z_const void*) blockTable[blockNb].cPtr;
Yann Collet9ceb49e2016-12-22 15:26:33 +0100488 inf.avail_in = (uInt)blockTable[blockNb].cSize;
inikep54320ce2016-09-22 11:52:53 +0200489 inf.total_in = 0;
490 inf.next_out = (void*) blockTable[blockNb].resPtr;
Yann Collet9ceb49e2016-12-22 15:26:33 +0100491 inf.avail_out = (uInt)blockTable[blockNb].srcSize;
inikep54320ce2016-09-22 11:52:53 +0200492 inf.total_out = 0;
493 ret = inflate(&inf, Z_FINISH);
inikepa03b7a72016-09-26 22:11:55 +0200494 if (ret == Z_NEED_DICT) {
495 ret = inflateSetDictionary(&inf, dictBuffer, dictBufferSize);
496 if (ret != Z_OK) EXM_THROW(1, "inflateSetDictionary failure");
497 ret = inflate(&inf, Z_FINISH);
498 }
inikep54320ce2016-09-22 11:52:53 +0200499 if (ret != Z_STREAM_END) EXM_THROW(1, "inflate failure");
500 ret = inflateEnd(&inf);
501 if (ret != Z_OK) EXM_THROW(1, "inflateEnd failure");
502 blockTable[blockNb].resSize = inf.total_out;
503 }
504 nbLoops++;
505 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
506 }
inikepdfef5dd2016-09-22 10:23:26 +0200507 { U64 const clockSpan = UTIL_clockSpanMicro(clockStart, ticksPerSecond);
508 if (clockSpan < fastestD*nbLoops) fastestD = clockSpan / nbLoops;
509 totalDTime += clockSpan;
510 dCompleted = totalDTime>maxTime;
511 } }
512
513 markNb = (markNb+1) % NB_MARKS;
514 DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r",
515 marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratio,
516 (double)srcSize / fastestC,
517 (double)srcSize / fastestD );
518
519 /* CRC Checking */
520 { U64 const crcCheck = XXH64(resultBuffer, srcSize, 0);
521 if (crcOrig!=crcCheck) {
522 size_t u;
523 DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck);
524 for (u=0; u<srcSize; u++) {
525 if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) {
526 U32 segNb, bNb, pos;
527 size_t bacc = 0;
528 DISPLAY("Decoding error at pos %u ", (U32)u);
529 for (segNb = 0; segNb < nbBlocks; segNb++) {
530 if (bacc + blockTable[segNb].srcSize > u) break;
531 bacc += blockTable[segNb].srcSize;
532 }
533 pos = (U32)(u - bacc);
534 bNb = pos / (128 KB);
535 DISPLAY("(block %u, sub %u, pos %u) \n", segNb, bNb, pos);
536 break;
537 }
538 if (u==srcSize-1) { /* should never happen */
539 DISPLAY("no difference detected\n");
540 } }
541 break;
542 } } /* CRC Checking */
543#endif
544 } /* for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) */
545
546 if (g_displayLevel == 1) {
547 double cSpeed = (double)srcSize / fastestC;
548 double dSpeed = (double)srcSize / fastestD;
549 if (g_additionalParam)
550 DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s (param=%d)\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName, g_additionalParam);
551 else
552 DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName);
553 }
554 DISPLAYLEVEL(2, "%2i#\n", cLevel);
555 } /* Bench */
556
557 /* clean up */
558 free(blockTable);
559 free(compressedBuffer);
560 free(resultBuffer);
561 ZSTD_freeCCtx(ctx);
562 ZSTD_freeDCtx(dctx);
563 return 0;
564}
565
566
567static size_t BMK_findMaxMem(U64 requiredMem)
568{
569 size_t const step = 64 MB;
570 BYTE* testmem = NULL;
571
572 requiredMem = (((requiredMem >> 26) + 1) << 26);
573 requiredMem += step;
574 if (requiredMem > maxMemory) requiredMem = maxMemory;
575
576 do {
577 testmem = (BYTE*)malloc((size_t)requiredMem);
578 requiredMem -= step;
579 } while (!testmem);
580
581 free(testmem);
582 return (size_t)(requiredMem);
583}
584
585static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize,
586 const char* displayName, int cLevel, int cLevelLast,
587 const size_t* fileSizes, unsigned nbFiles,
588 const void* dictBuffer, size_t dictBufferSize)
589{
590 int l;
591
592 const char* pch = strrchr(displayName, '\\'); /* Windows */
593 if (!pch) pch = strrchr(displayName, '/'); /* Linux */
594 if (pch) displayName = pch+1;
595
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +0100596 SET_REALTIME_PRIORITY;
inikepdfef5dd2016-09-22 10:23:26 +0200597
598 if (g_displayLevel == 1 && !g_additionalParam)
inikep68cd4762016-09-23 12:42:21 +0200599 DISPLAY("bench %s %s: input %u bytes, %u seconds, %u KB blocks\n", ZSTD_VERSION_STRING, ZSTD_GIT_COMMIT_STRING, (U32)benchedSize, g_nbIterations, (U32)(g_blockSize>>10));
inikepdfef5dd2016-09-22 10:23:26 +0200600
601 if (cLevelLast < cLevel) cLevelLast = cLevel;
602
inikepf7ab3ad2016-09-22 17:59:10 +0200603 DISPLAY("benchmarking zstd %s (using ZSTD_CStream)\n", ZSTD_VERSION_STRING);
inikep8e8b0462016-09-22 14:42:32 +0200604 for (l=cLevel; l <= cLevelLast; l++) {
605 BMK_benchMem(srcBuffer, benchedSize,
606 displayName, l,
607 fileSizes, nbFiles,
inikepf7ab3ad2016-09-22 17:59:10 +0200608 dictBuffer, dictBufferSize, BMK_ZSTD_STREAM);
inikep8e8b0462016-09-22 14:42:32 +0200609 }
610
Przemyslaw Skibinski96fca2b2016-11-25 14:36:27 +0100611 DISPLAY("benchmarking zstd %s (using ZSTD_CCtx)\n", ZSTD_VERSION_STRING);
612 for (l=cLevel; l <= cLevelLast; l++) {
613 BMK_benchMem(srcBuffer, benchedSize,
614 displayName, l,
615 fileSizes, nbFiles,
616 dictBuffer, dictBufferSize, BMK_ZSTD);
617 }
618
inikepf7ab3ad2016-09-22 17:59:10 +0200619 DISPLAY("benchmarking zstd %s (using zlibWrapper)\n", ZSTD_VERSION_STRING);
inikep8e8b0462016-09-22 14:42:32 +0200620 for (l=cLevel; l <= cLevelLast; l++) {
621 BMK_benchMem(srcBuffer, benchedSize,
622 displayName, l,
623 fileSizes, nbFiles,
inikep252c20d2016-09-23 09:08:40 +0200624 dictBuffer, dictBufferSize, BMK_ZWRAP_ZSTD_REUSE);
inikep8e8b0462016-09-22 14:42:32 +0200625 }
626
inikep252c20d2016-09-23 09:08:40 +0200627 DISPLAY("benchmarking zstd %s (zlibWrapper not reusing a context)\n", ZSTD_VERSION_STRING);
inikepf7ab3ad2016-09-22 17:59:10 +0200628 for (l=cLevel; l <= cLevelLast; l++) {
629 BMK_benchMem(srcBuffer, benchedSize,
630 displayName, l,
631 fileSizes, nbFiles,
inikep252c20d2016-09-23 09:08:40 +0200632 dictBuffer, dictBufferSize, BMK_ZWRAP_ZSTD);
inikepf7ab3ad2016-09-22 17:59:10 +0200633 }
634
inikep8e8b0462016-09-22 14:42:32 +0200635
636 if (cLevelLast > Z_BEST_COMPRESSION) cLevelLast = Z_BEST_COMPRESSION;
637
inikep252c20d2016-09-23 09:08:40 +0200638 DISPLAY("\n");
inikep54320ce2016-09-22 11:52:53 +0200639 DISPLAY("benchmarking zlib %s\n", ZLIB_VERSION);
inikepdfef5dd2016-09-22 10:23:26 +0200640 for (l=cLevel; l <= cLevelLast; l++) {
641 BMK_benchMem(srcBuffer, benchedSize,
642 displayName, l,
643 fileSizes, nbFiles,
inikepf7ab3ad2016-09-22 17:59:10 +0200644 dictBuffer, dictBufferSize, BMK_ZLIB_REUSE);
645 }
646
inikep252c20d2016-09-23 09:08:40 +0200647 DISPLAY("benchmarking zlib %s (zlib not reusing a context)\n", ZLIB_VERSION);
648 for (l=cLevel; l <= cLevelLast; l++) {
649 BMK_benchMem(srcBuffer, benchedSize,
650 displayName, l,
651 fileSizes, nbFiles,
652 dictBuffer, dictBufferSize, BMK_ZLIB);
653 }
654
inikepf7ab3ad2016-09-22 17:59:10 +0200655 DISPLAY("benchmarking zlib %s (using zlibWrapper)\n", ZLIB_VERSION);
inikep54320ce2016-09-22 11:52:53 +0200656 for (l=cLevel; l <= cLevelLast; l++) {
657 BMK_benchMem(srcBuffer, benchedSize,
658 displayName, l,
659 fileSizes, nbFiles,
inikep252c20d2016-09-23 09:08:40 +0200660 dictBuffer, dictBufferSize, BMK_ZWRAP_ZLIB_REUSE);
inikepdfef5dd2016-09-22 10:23:26 +0200661 }
inikepf7ab3ad2016-09-22 17:59:10 +0200662
inikep252c20d2016-09-23 09:08:40 +0200663 DISPLAY("benchmarking zlib %s (zlibWrapper not reusing a context)\n", ZLIB_VERSION);
inikepf7ab3ad2016-09-22 17:59:10 +0200664 for (l=cLevel; l <= cLevelLast; l++) {
665 BMK_benchMem(srcBuffer, benchedSize,
666 displayName, l,
667 fileSizes, nbFiles,
inikep252c20d2016-09-23 09:08:40 +0200668 dictBuffer, dictBufferSize, BMK_ZWRAP_ZLIB);
inikepf7ab3ad2016-09-22 17:59:10 +0200669 }
inikepdfef5dd2016-09-22 10:23:26 +0200670}
671
672
673/*! BMK_loadFiles() :
674 Loads `buffer` with content of files listed within `fileNamesTable`.
675 At most, fills `buffer` entirely */
676static void BMK_loadFiles(void* buffer, size_t bufferSize,
677 size_t* fileSizes,
678 const char** fileNamesTable, unsigned nbFiles)
679{
680 size_t pos = 0, totalSize = 0;
681 unsigned n;
682 for (n=0; n<nbFiles; n++) {
683 FILE* f;
684 U64 fileSize = UTIL_getFileSize(fileNamesTable[n]);
685 if (UTIL_isDirectory(fileNamesTable[n])) {
686 DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]);
687 fileSizes[n] = 0;
688 continue;
689 }
690 f = fopen(fileNamesTable[n], "rb");
691 if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]);
692 DISPLAYUPDATE(2, "Loading %s... \r", fileNamesTable[n]);
693 if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */
694 { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
695 if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]);
696 pos += readSize; }
697 fileSizes[n] = (size_t)fileSize;
698 totalSize += (size_t)fileSize;
699 fclose(f);
700 }
701
702 if (totalSize == 0) EXM_THROW(12, "no data to bench");
703}
704
705static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles,
706 const char* dictFileName, int cLevel, int cLevelLast)
707{
708 void* srcBuffer;
709 size_t benchedSize;
710 void* dictBuffer = NULL;
711 size_t dictBufferSize = 0;
712 size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t));
713 U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
714 char mfName[20] = {0};
715
716 if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes");
717
718 /* Load dictionary */
719 if (dictFileName != NULL) {
720 U64 dictFileSize = UTIL_getFileSize(dictFileName);
721 if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName);
722 dictBufferSize = (size_t)dictFileSize;
723 dictBuffer = malloc(dictBufferSize);
724 if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize);
725 BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1);
726 }
727
728 /* Memory allocation & restrictions */
729 benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
730 if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
731 if (benchedSize < totalSizeToLoad)
732 DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20));
733 srcBuffer = malloc(benchedSize);
734 if (!srcBuffer) EXM_THROW(12, "not enough memory");
735
736 /* Load input buffer */
737 BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles);
738
739 /* Bench */
740 snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
741 { const char* displayName = (nbFiles > 1) ? mfName : fileNamesTable[0];
742 BMK_benchCLevel(srcBuffer, benchedSize,
743 displayName, cLevel, cLevelLast,
744 fileSizes, nbFiles,
745 dictBuffer, dictBufferSize);
746 }
747
748 /* clean up */
749 free(srcBuffer);
750 free(dictBuffer);
751 free(fileSizes);
752}
753
754
755static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility)
756{
757 char name[20] = {0};
758 size_t benchedSize = 10000000;
759 void* const srcBuffer = malloc(benchedSize);
760
761 /* Memory allocation */
762 if (!srcBuffer) EXM_THROW(21, "not enough memory");
763
764 /* Fill input buffer */
765 RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
766
767 /* Bench */
768 snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
769 BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0);
770
771 /* clean up */
772 free(srcBuffer);
773}
774
775
776int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
777 const char* dictFileName, int cLevel, int cLevelLast)
778{
779 double const compressibility = (double)g_compressibilityDefault / 100;
780
781 if (nbFiles == 0)
782 BMK_syntheticTest(cLevel, cLevelLast, compressibility);
783 else
784 BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast);
785 return 0;
786}
787
788
789
790
791/*-************************************
792* Command Line
793**************************************/
794static int usage(const char* programName)
795{
796 DISPLAY(WELCOME_MESSAGE);
797 DISPLAY( "Usage :\n");
798 DISPLAY( " %s [args] [FILE(s)] [-o file]\n", programName);
799 DISPLAY( "\n");
800 DISPLAY( "FILE : a filename\n");
801 DISPLAY( " with no FILE, or when FILE is - , read standard input\n");
802 DISPLAY( "Arguments :\n");
803 DISPLAY( " -D file: use `file` as Dictionary \n");
804 DISPLAY( " -h/-H : display help/long help and exit\n");
805 DISPLAY( " -V : display Version number and exit\n");
806 DISPLAY( " -v : verbose mode; specify multiple times to increase log level (default:%d)\n", DEFAULT_DISPLAY_LEVEL);
807 DISPLAY( " -q : suppress warnings; specify twice to suppress errors too\n");
808#ifdef UTIL_HAS_CREATEFILELIST
809 DISPLAY( " -r : operate recursively on directories\n");
810#endif
811 DISPLAY( "\n");
812 DISPLAY( "Benchmark arguments :\n");
inikep8e8b0462016-09-22 14:42:32 +0200813 DISPLAY( " -b# : benchmark file(s), using # compression level (default : %d) \n", ZSTDCLI_CLEVEL_DEFAULT);
814 DISPLAY( " -e# : test all compression levels from -bX to # (default: %d)\n", ZSTDCLI_CLEVEL_DEFAULT);
inikepdfef5dd2016-09-22 10:23:26 +0200815 DISPLAY( " -i# : minimum evaluation time in seconds (default : 3s)\n");
816 DISPLAY( " -B# : cut file into independent blocks of size # (default: no block)\n");
817 return 0;
818}
819
820static int badusage(const char* programName)
821{
822 DISPLAYLEVEL(1, "Incorrect parameters\n");
823 if (g_displayLevel >= 1) usage(programName);
824 return 1;
825}
826
827static void waitEnter(void)
828{
829 int unused;
830 DISPLAY("Press enter to continue...\n");
831 unused = getchar();
832 (void)unused;
833}
834
835/*! readU32FromChar() :
836 @return : unsigned integer value reach from input in `char` format
837 Will also modify `*stringPtr`, advancing it to position where it stopped reading.
838 Note : this function can overflow if digit string > MAX_UINT */
839static unsigned readU32FromChar(const char** stringPtr)
840{
841 unsigned result = 0;
842 while ((**stringPtr >='0') && (**stringPtr <='9'))
843 result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
844 return result;
845}
846
847
848#define CLEAN_RETURN(i) { operationResult = (i); goto _end; }
849
850int main(int argCount, char** argv)
851{
852 int argNb,
853 main_pause=0,
854 nextEntryIsDictionary=0,
855 operationResult=0,
856 nextArgumentIsFile=0;
857 int cLevel = ZSTDCLI_CLEVEL_DEFAULT;
858 int cLevelLast = 1;
859 unsigned recursive = 0;
860 const char** filenameTable = (const char**)malloc(argCount * sizeof(const char*)); /* argCount >= 1 */
861 unsigned filenameIdx = 0;
862 const char* programName = argv[0];
863 const char* dictFileName = NULL;
864 char* dynNameSpace = NULL;
865#ifdef UTIL_HAS_CREATEFILELIST
866 const char** fileNamesTable = NULL;
867 char* fileNamesBuf = NULL;
868 unsigned fileNamesNb;
869#endif
870
871 /* init */
872 if (filenameTable==NULL) { DISPLAY("zstd: %s \n", strerror(errno)); exit(1); }
873 displayOut = stderr;
874
875 /* Pick out program name from path. Don't rely on stdlib because of conflicting behavior */
876 { size_t pos;
877 for (pos = (int)strlen(programName); pos > 0; pos--) { if (programName[pos] == '/') { pos++; break; } }
878 programName += pos;
879 }
880
881 /* command switches */
882 for(argNb=1; argNb<argCount; argNb++) {
883 const char* argument = argv[argNb];
884 if(!argument) continue; /* Protection if argument empty */
885
886 if (nextArgumentIsFile==0) {
887
888 /* long commands (--long-word) */
889 if (!strcmp(argument, "--")) { nextArgumentIsFile=1; continue; }
890 if (!strcmp(argument, "--version")) { displayOut=stdout; DISPLAY(WELCOME_MESSAGE); CLEAN_RETURN(0); }
891 if (!strcmp(argument, "--help")) { displayOut=stdout; CLEAN_RETURN(usage(programName)); }
892 if (!strcmp(argument, "--verbose")) { g_displayLevel++; continue; }
893 if (!strcmp(argument, "--quiet")) { g_displayLevel--; continue; }
894
895 /* Decode commands (note : aggregated commands are allowed) */
896 if (argument[0]=='-') {
897 argument++;
898
899 while (argument[0]!=0) {
900 switch(argument[0])
901 {
902 /* Display help */
903 case 'V': displayOut=stdout; DISPLAY(WELCOME_MESSAGE); CLEAN_RETURN(0); /* Version Only */
904 case 'H':
905 case 'h': displayOut=stdout; CLEAN_RETURN(usage(programName));
906
907 /* Use file content as dictionary */
908 case 'D': nextEntryIsDictionary = 1; argument++; break;
909
910 /* Verbose mode */
911 case 'v': g_displayLevel++; argument++; break;
912
913 /* Quiet mode */
914 case 'q': g_displayLevel--; argument++; break;
915
916#ifdef UTIL_HAS_CREATEFILELIST
917 /* recursive */
918 case 'r': recursive=1; argument++; break;
919#endif
920
921 /* Benchmark */
922 case 'b':
923 /* first compression Level */
924 argument++;
925 cLevel = readU32FromChar(&argument);
926 break;
927
928 /* range bench (benchmark only) */
929 case 'e':
930 /* last compression Level */
931 argument++;
932 cLevelLast = readU32FromChar(&argument);
933 break;
934
935 /* Modify Nb Iterations (benchmark only) */
936 case 'i':
937 argument++;
938 { U32 const iters = readU32FromChar(&argument);
939 BMK_setNotificationLevel(g_displayLevel);
940 BMK_SetNbIterations(iters);
941 }
942 break;
943
944 /* cut input into blocks (benchmark only) */
945 case 'B':
946 argument++;
947 { size_t bSize = readU32FromChar(&argument);
948 if (toupper(*argument)=='K') bSize<<=10, argument++; /* allows using KB notation */
949 if (toupper(*argument)=='M') bSize<<=20, argument++;
950 if (toupper(*argument)=='B') argument++;
951 BMK_setNotificationLevel(g_displayLevel);
952 BMK_SetBlockSize(bSize);
953 }
954 break;
955
956 /* Pause at the end (-p) or set an additional param (-p#) (hidden option) */
957 case 'p': argument++;
958 if ((*argument>='0') && (*argument<='9')) {
959 BMK_setAdditionalParam(readU32FromChar(&argument));
960 } else
961 main_pause=1;
962 break;
963 /* unknown command */
964 default : CLEAN_RETURN(badusage(programName));
965 }
966 }
967 continue;
968 } /* if (argument[0]=='-') */
969
970 } /* if (nextArgumentIsAFile==0) */
971
972 if (nextEntryIsDictionary) {
973 nextEntryIsDictionary = 0;
974 dictFileName = argument;
975 continue;
976 }
977
978 /* add filename to list */
979 filenameTable[filenameIdx++] = argument;
980 }
981
982 /* Welcome message (if verbose) */
983 DISPLAYLEVEL(3, WELCOME_MESSAGE);
984
985#ifdef UTIL_HAS_CREATEFILELIST
986 if (recursive) {
Sean Purcell680e4e02017-03-23 11:52:09 -0700987 fileNamesTable = UTIL_createFileList(filenameTable, filenameIdx, &fileNamesBuf, &fileNamesNb, 1);
inikepdfef5dd2016-09-22 10:23:26 +0200988 if (fileNamesTable) {
989 unsigned u;
990 for (u=0; u<fileNamesNb; u++) DISPLAYLEVEL(4, "%u %s\n", u, fileNamesTable[u]);
991 free((void*)filenameTable);
992 filenameTable = fileNamesTable;
993 filenameIdx = fileNamesNb;
994 }
995 }
996#endif
997
998 BMK_setNotificationLevel(g_displayLevel);
999 BMK_benchFiles(filenameTable, filenameIdx, dictFileName, cLevel, cLevelLast);
1000
1001_end:
1002 if (main_pause) waitEnter();
1003 free(dynNameSpace);
1004#ifdef UTIL_HAS_CREATEFILELIST
1005 if (fileNamesTable)
1006 UTIL_freeFileList(fileNamesTable, fileNamesBuf);
1007 else
1008#endif
1009 free((void*)filenameTable);
1010 return operationResult;
1011}