zwrapbench.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /*
  2. * Copyright (c) 2016-present, Przemyslaw Skibinski, 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. */
  9. /* *************************************
  10. * Includes
  11. ***************************************/
  12. #include "util.h" /* Compiler options, UTIL_GetFileSize, UTIL_sleep */
  13. #include <stdlib.h> /* malloc, free */
  14. #include <string.h> /* memset */
  15. #include <stdio.h> /* fprintf, fopen, ftello64 */
  16. #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
  17. #include <ctype.h> /* toupper */
  18. #include <errno.h> /* errno */
  19. #include "timefn.h" /* UTIL_time_t, UTIL_getTime, UTIL_clockSpanMicro, UTIL_waitForNextTick */
  20. #include "mem.h"
  21. #define ZSTD_STATIC_LINKING_ONLY
  22. #include "zstd.h"
  23. #include "datagen.h" /* RDG_genBuffer */
  24. #include "xxhash.h"
  25. #include "zstd_zlibwrapper.h"
  26. /*-************************************
  27. * Tuning parameters
  28. **************************************/
  29. #ifndef ZSTDCLI_CLEVEL_DEFAULT
  30. # define ZSTDCLI_CLEVEL_DEFAULT 3
  31. #endif
  32. /*-************************************
  33. * Constants
  34. **************************************/
  35. #define COMPRESSOR_NAME "Zstandard wrapper for zlib command line interface"
  36. #ifndef ZSTD_VERSION
  37. # define ZSTD_VERSION "v" ZSTD_VERSION_STRING
  38. #endif
  39. #define AUTHOR "Yann Collet"
  40. #define WELCOME_MESSAGE "*** %s %i-bits %s, by %s ***\n", COMPRESSOR_NAME, (int)(sizeof(size_t)*8), ZSTD_VERSION, AUTHOR
  41. #ifndef ZSTD_GIT_COMMIT
  42. # define ZSTD_GIT_COMMIT_STRING ""
  43. #else
  44. # define ZSTD_GIT_COMMIT_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_GIT_COMMIT)
  45. #endif
  46. #define NBLOOPS 3
  47. #define TIMELOOP_MICROSEC 1*1000000ULL /* 1 second */
  48. #define ACTIVEPERIOD_MICROSEC 70*1000000ULL /* 70 seconds */
  49. #define COOLPERIOD_SEC 10
  50. #define KB *(1 <<10)
  51. #define MB *(1 <<20)
  52. #define GB *(1U<<30)
  53. static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31));
  54. static U32 g_compressibilityDefault = 50;
  55. /* *************************************
  56. * console display
  57. ***************************************/
  58. #define DEFAULT_DISPLAY_LEVEL 2
  59. #define DISPLAY(...) fprintf(displayOut, __VA_ARGS__)
  60. #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
  61. static unsigned g_displayLevel = DEFAULT_DISPLAY_LEVEL; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
  62. static FILE* displayOut;
  63. #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
  64. if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \
  65. { g_time = clock(); DISPLAY(__VA_ARGS__); \
  66. if (g_displayLevel>=4) fflush(displayOut); } }
  67. static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100;
  68. static clock_t g_time = 0;
  69. /* *************************************
  70. * Exceptions
  71. ***************************************/
  72. #ifndef DEBUG
  73. # define DEBUG 0
  74. #endif
  75. #define DEBUGOUTPUT(...) { if (DEBUG) DISPLAY(__VA_ARGS__); }
  76. #define EXM_THROW(error, ...) \
  77. { \
  78. DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
  79. DISPLAYLEVEL(1, "Error %i : ", error); \
  80. DISPLAYLEVEL(1, __VA_ARGS__); \
  81. DISPLAYLEVEL(1, "\n"); \
  82. exit(error); \
  83. }
  84. /* *************************************
  85. * Benchmark Parameters
  86. ***************************************/
  87. static unsigned g_nbIterations = NBLOOPS;
  88. static size_t g_blockSize = 0;
  89. int g_additionalParam = 0;
  90. void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; }
  91. void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; }
  92. void BMK_SetNbIterations(unsigned nbLoops)
  93. {
  94. g_nbIterations = nbLoops;
  95. DISPLAYLEVEL(3, "- test >= %u seconds per compression / decompression -\n", g_nbIterations);
  96. }
  97. void BMK_SetBlockSize(size_t blockSize)
  98. {
  99. g_blockSize = blockSize;
  100. DISPLAYLEVEL(2, "using blocks of size %u KB \n", (unsigned)(blockSize>>10));
  101. }
  102. /* ********************************************************
  103. * Bench functions
  104. **********************************************************/
  105. #undef MIN
  106. #undef MAX
  107. #define MIN(a,b) ((a)<(b) ? (a) : (b))
  108. #define MAX(a,b) ((a)>(b) ? (a) : (b))
  109. typedef struct
  110. {
  111. z_const char* srcPtr;
  112. size_t srcSize;
  113. char* cPtr;
  114. size_t cRoom;
  115. size_t cSize;
  116. char* resPtr;
  117. size_t resSize;
  118. } blockParam_t;
  119. typedef 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;
  120. static int BMK_benchMem(z_const void* srcBuffer, size_t srcSize,
  121. const char* displayName, int cLevel,
  122. const size_t* fileSizes, U32 nbFiles,
  123. const void* dictBuffer, size_t dictBufferSize, BMK_compressor compressor)
  124. {
  125. size_t const blockSize = (g_blockSize>=32 ? g_blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ;
  126. size_t const avgSize = MIN(g_blockSize, (srcSize / nbFiles));
  127. U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
  128. blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t));
  129. size_t const maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */
  130. void* const compressedBuffer = malloc(maxCompressedSize);
  131. void* const resultBuffer = malloc(srcSize);
  132. ZSTD_CCtx* const ctx = ZSTD_createCCtx();
  133. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  134. U32 nbBlocks;
  135. /* checks */
  136. if (!compressedBuffer || !resultBuffer || !blockTable || !ctx || !dctx)
  137. EXM_THROW(31, "allocation error : not enough memory");
  138. /* init */
  139. if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */
  140. /* Init blockTable data */
  141. { z_const char* srcPtr = (z_const char*)srcBuffer;
  142. char* cPtr = (char*)compressedBuffer;
  143. char* resPtr = (char*)resultBuffer;
  144. U32 fileNb;
  145. for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
  146. size_t remaining = fileSizes[fileNb];
  147. U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize);
  148. U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
  149. for ( ; nbBlocks<blockEnd; nbBlocks++) {
  150. size_t const thisBlockSize = MIN(remaining, blockSize);
  151. blockTable[nbBlocks].srcPtr = srcPtr;
  152. blockTable[nbBlocks].cPtr = cPtr;
  153. blockTable[nbBlocks].resPtr = resPtr;
  154. blockTable[nbBlocks].srcSize = thisBlockSize;
  155. blockTable[nbBlocks].cRoom = ZSTD_compressBound(thisBlockSize);
  156. srcPtr += thisBlockSize;
  157. cPtr += blockTable[nbBlocks].cRoom;
  158. resPtr += thisBlockSize;
  159. remaining -= thisBlockSize;
  160. } } }
  161. /* warming up memory */
  162. RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
  163. /* Bench */
  164. { U64 fastestC = (U64)(-1LL), fastestD = (U64)(-1LL);
  165. U64 const crcOrig = XXH64(srcBuffer, srcSize, 0);
  166. UTIL_time_t coolTime;
  167. U64 const maxTime = (g_nbIterations * TIMELOOP_MICROSEC) + 100;
  168. U64 totalCTime=0, totalDTime=0;
  169. U32 cCompleted=0, dCompleted=0;
  170. # define NB_MARKS 4
  171. const char* const marks[NB_MARKS] = { " |", " /", " =", "\\" };
  172. U32 markNb = 0;
  173. size_t cSize = 0;
  174. double ratio = 0.;
  175. coolTime = UTIL_getTime();
  176. DISPLAYLEVEL(2, "\r%79s\r", "");
  177. while (!cCompleted | !dCompleted) {
  178. UTIL_time_t clockStart;
  179. U64 clockLoop = g_nbIterations ? TIMELOOP_MICROSEC : 1;
  180. /* overheat protection */
  181. if (UTIL_clockSpanMicro(coolTime) > ACTIVEPERIOD_MICROSEC) {
  182. DISPLAYLEVEL(2, "\rcooling down ... \r");
  183. UTIL_sleep(COOLPERIOD_SEC);
  184. coolTime = UTIL_getTime();
  185. }
  186. /* Compression */
  187. DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (unsigned)srcSize);
  188. if (!cCompleted) memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */
  189. UTIL_sleepMilli(1); /* give processor time to other processes */
  190. UTIL_waitForNextTick();
  191. clockStart = UTIL_getTime();
  192. if (!cCompleted) { /* still some time to do compression tests */
  193. U32 nbLoops = 0;
  194. if (compressor == BMK_ZSTD) {
  195. ZSTD_parameters const zparams = ZSTD_getParams(cLevel, avgSize, dictBufferSize);
  196. ZSTD_customMem const cmem = { NULL, NULL, NULL };
  197. ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictBuffer, dictBufferSize, ZSTD_dlm_byRef, ZSTD_dct_auto, zparams.cParams, cmem);
  198. if (cdict==NULL) EXM_THROW(1, "ZSTD_createCDict_advanced() allocation failure");
  199. do {
  200. U32 blockNb;
  201. size_t rSize;
  202. for (blockNb=0; blockNb<nbBlocks; blockNb++) {
  203. if (dictBufferSize) {
  204. rSize = ZSTD_compress_usingCDict(ctx,
  205. blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
  206. blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize,
  207. cdict);
  208. } else {
  209. rSize = ZSTD_compressCCtx (ctx,
  210. blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
  211. blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize, cLevel);
  212. }
  213. if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingCDict() failed : %s", ZSTD_getErrorName(rSize));
  214. blockTable[blockNb].cSize = rSize;
  215. }
  216. nbLoops++;
  217. } while (UTIL_clockSpanMicro(clockStart) < clockLoop);
  218. ZSTD_freeCDict(cdict);
  219. } else if (compressor == BMK_ZSTD_STREAM) {
  220. ZSTD_parameters const zparams = ZSTD_getParams(cLevel, avgSize, dictBufferSize);
  221. ZSTD_inBuffer inBuffer;
  222. ZSTD_outBuffer outBuffer;
  223. ZSTD_CStream* zbc = ZSTD_createCStream();
  224. size_t rSize;
  225. ZSTD_CCtx_params* cctxParams = ZSTD_createCCtxParams();
  226. if (!cctxParams) EXM_THROW(1, "ZSTD_createCCtxParams() allocation failure");
  227. if (zbc == NULL) EXM_THROW(1, "ZSTD_createCStream() allocation failure");
  228. { int initErr = 0;
  229. initErr |= ZSTD_isError(ZSTD_CCtx_reset(zbc, ZSTD_reset_session_only));
  230. initErr |= ZSTD_isError(ZSTD_CCtxParams_init_advanced(cctxParams, zparams));
  231. initErr |= ZSTD_isError(ZSTD_CCtx_setParametersUsingCCtxParams(zbc, cctxParams));
  232. initErr |= ZSTD_isError(ZSTD_CCtx_setPledgedSrcSize(zbc, avgSize));
  233. initErr |= ZSTD_isError(ZSTD_CCtx_loadDictionary(zbc, dictBuffer, dictBufferSize));
  234. ZSTD_freeCCtxParams(cctxParams);
  235. if (initErr) EXM_THROW(1, "CCtx init failed!");
  236. }
  237. do {
  238. U32 blockNb;
  239. for (blockNb=0; blockNb<nbBlocks; blockNb++) {
  240. rSize = ZSTD_CCtx_reset(zbc, ZSTD_reset_session_only);
  241. if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_CCtx_reset() failed : %s", ZSTD_getErrorName(rSize));
  242. rSize = ZSTD_CCtx_setPledgedSrcSize(zbc, blockTable[blockNb].srcSize);
  243. if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_CCtx_setPledgedSrcSize() failed : %s", ZSTD_getErrorName(rSize));
  244. inBuffer.src = blockTable[blockNb].srcPtr;
  245. inBuffer.size = blockTable[blockNb].srcSize;
  246. inBuffer.pos = 0;
  247. outBuffer.dst = blockTable[blockNb].cPtr;
  248. outBuffer.size = blockTable[blockNb].cRoom;
  249. outBuffer.pos = 0;
  250. rSize = ZSTD_compressStream(zbc, &outBuffer, &inBuffer);
  251. if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compressStream() failed : %s", ZSTD_getErrorName(rSize));
  252. rSize = ZSTD_endStream(zbc, &outBuffer);
  253. if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_endStream() failed : %s", ZSTD_getErrorName(rSize));
  254. blockTable[blockNb].cSize = outBuffer.pos;
  255. }
  256. nbLoops++;
  257. } while (UTIL_clockSpanMicro(clockStart) < clockLoop);
  258. ZSTD_freeCStream(zbc);
  259. } else if (compressor == BMK_ZWRAP_ZLIB_REUSE || compressor == BMK_ZWRAP_ZSTD_REUSE || compressor == BMK_ZLIB_REUSE) {
  260. z_stream def;
  261. int ret;
  262. int useSetDict = (dictBuffer != NULL);
  263. if (compressor == BMK_ZLIB_REUSE || compressor == BMK_ZWRAP_ZLIB_REUSE) ZWRAP_useZSTDcompression(0);
  264. else ZWRAP_useZSTDcompression(1);
  265. def.zalloc = Z_NULL;
  266. def.zfree = Z_NULL;
  267. def.opaque = Z_NULL;
  268. ret = deflateInit(&def, cLevel);
  269. if (ret != Z_OK) EXM_THROW(1, "deflateInit failure");
  270. /* if (ZWRAP_isUsingZSTDcompression()) {
  271. ret = ZWRAP_setPledgedSrcSize(&def, avgSize);
  272. if (ret != Z_OK) EXM_THROW(1, "ZWRAP_setPledgedSrcSize failure");
  273. } */
  274. do {
  275. U32 blockNb;
  276. for (blockNb=0; blockNb<nbBlocks; blockNb++) {
  277. if (ZWRAP_isUsingZSTDcompression())
  278. ret = ZWRAP_deflateReset_keepDict(&def); /* reuse dictionary to make compression faster */
  279. else
  280. ret = deflateReset(&def);
  281. if (ret != Z_OK) EXM_THROW(1, "deflateReset failure");
  282. if (useSetDict) {
  283. ret = deflateSetDictionary(&def, (const z_Bytef*)dictBuffer, dictBufferSize);
  284. if (ret != Z_OK) EXM_THROW(1, "deflateSetDictionary failure");
  285. if (ZWRAP_isUsingZSTDcompression()) useSetDict = 0; /* zstd doesn't require deflateSetDictionary after ZWRAP_deflateReset_keepDict */
  286. }
  287. def.next_in = (z_const z_Bytef*) blockTable[blockNb].srcPtr;
  288. def.avail_in = (uInt)blockTable[blockNb].srcSize;
  289. def.total_in = 0;
  290. def.next_out = (z_Bytef*) blockTable[blockNb].cPtr;
  291. def.avail_out = (uInt)blockTable[blockNb].cRoom;
  292. def.total_out = 0;
  293. ret = deflate(&def, Z_FINISH);
  294. if (ret != Z_STREAM_END) EXM_THROW(1, "deflate failure ret=%d srcSize=%d" , ret, (int)blockTable[blockNb].srcSize);
  295. blockTable[blockNb].cSize = def.total_out;
  296. }
  297. nbLoops++;
  298. } while (UTIL_clockSpanMicro(clockStart) < clockLoop);
  299. ret = deflateEnd(&def);
  300. if (ret != Z_OK) EXM_THROW(1, "deflateEnd failure");
  301. } else {
  302. z_stream def;
  303. if (compressor == BMK_ZLIB || compressor == BMK_ZWRAP_ZLIB) ZWRAP_useZSTDcompression(0);
  304. else ZWRAP_useZSTDcompression(1);
  305. do {
  306. U32 blockNb;
  307. for (blockNb=0; blockNb<nbBlocks; blockNb++) {
  308. int ret;
  309. def.zalloc = Z_NULL;
  310. def.zfree = Z_NULL;
  311. def.opaque = Z_NULL;
  312. ret = deflateInit(&def, cLevel);
  313. if (ret != Z_OK) EXM_THROW(1, "deflateInit failure");
  314. if (dictBuffer) {
  315. ret = deflateSetDictionary(&def, (const z_Bytef*)dictBuffer, dictBufferSize);
  316. if (ret != Z_OK) EXM_THROW(1, "deflateSetDictionary failure");
  317. }
  318. def.next_in = (z_const z_Bytef*) blockTable[blockNb].srcPtr;
  319. def.avail_in = (uInt)blockTable[blockNb].srcSize;
  320. def.total_in = 0;
  321. def.next_out = (z_Bytef*) blockTable[blockNb].cPtr;
  322. def.avail_out = (uInt)blockTable[blockNb].cRoom;
  323. def.total_out = 0;
  324. ret = deflate(&def, Z_FINISH);
  325. if (ret != Z_STREAM_END) EXM_THROW(1, "deflate failure");
  326. ret = deflateEnd(&def);
  327. if (ret != Z_OK) EXM_THROW(1, "deflateEnd failure");
  328. blockTable[blockNb].cSize = def.total_out;
  329. }
  330. nbLoops++;
  331. } while (UTIL_clockSpanMicro(clockStart) < clockLoop);
  332. }
  333. { U64 const clockSpan = UTIL_clockSpanMicro(clockStart);
  334. if (clockSpan < fastestC*nbLoops) fastestC = clockSpan / nbLoops;
  335. totalCTime += clockSpan;
  336. cCompleted = totalCTime>maxTime;
  337. } }
  338. cSize = 0;
  339. { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; }
  340. ratio = (double)srcSize / (double)cSize;
  341. markNb = (markNb+1) % NB_MARKS;
  342. DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r",
  343. marks[markNb], displayName, (unsigned)srcSize, (unsigned)cSize, ratio,
  344. (double)srcSize / fastestC );
  345. (void)fastestD; (void)crcOrig; /* unused when decompression disabled */
  346. #if 1
  347. /* Decompression */
  348. if (!dCompleted) memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */
  349. UTIL_sleepMilli(1); /* give processor time to other processes */
  350. UTIL_waitForNextTick();
  351. clockStart = UTIL_getTime();
  352. if (!dCompleted) {
  353. U32 nbLoops = 0;
  354. if (compressor == BMK_ZSTD) {
  355. ZSTD_DDict* ddict = ZSTD_createDDict(dictBuffer, dictBufferSize);
  356. if (!ddict) EXM_THROW(2, "ZSTD_createDDict() allocation failure");
  357. do {
  358. unsigned blockNb;
  359. for (blockNb=0; blockNb<nbBlocks; blockNb++) {
  360. size_t const regenSize = ZSTD_decompress_usingDDict(dctx,
  361. blockTable[blockNb].resPtr, blockTable[blockNb].srcSize,
  362. blockTable[blockNb].cPtr, blockTable[blockNb].cSize,
  363. ddict);
  364. if (ZSTD_isError(regenSize)) {
  365. DISPLAY("ZSTD_decompress_usingDDict() failed on block %u : %s \n",
  366. blockNb, ZSTD_getErrorName(regenSize));
  367. clockLoop = 0; /* force immediate test end */
  368. break;
  369. }
  370. blockTable[blockNb].resSize = regenSize;
  371. }
  372. nbLoops++;
  373. } while (UTIL_clockSpanMicro(clockStart) < clockLoop);
  374. ZSTD_freeDDict(ddict);
  375. } else if (compressor == BMK_ZSTD_STREAM) {
  376. ZSTD_inBuffer inBuffer;
  377. ZSTD_outBuffer outBuffer;
  378. ZSTD_DStream* zbd = ZSTD_createDStream();
  379. size_t rSize;
  380. if (zbd == NULL) EXM_THROW(1, "ZSTD_createDStream() allocation failure");
  381. rSize = ZSTD_initDStream_usingDict(zbd, dictBuffer, dictBufferSize);
  382. if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_initDStream() failed : %s", ZSTD_getErrorName(rSize));
  383. do {
  384. U32 blockNb;
  385. for (blockNb=0; blockNb<nbBlocks; blockNb++) {
  386. rSize = ZSTD_DCtx_reset(zbd, ZSTD_reset_session_only);
  387. if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_DCtx_reset() failed : %s", ZSTD_getErrorName(rSize));
  388. inBuffer.src = blockTable[blockNb].cPtr;
  389. inBuffer.size = blockTable[blockNb].cSize;
  390. inBuffer.pos = 0;
  391. outBuffer.dst = blockTable[blockNb].resPtr;
  392. outBuffer.size = blockTable[blockNb].srcSize;
  393. outBuffer.pos = 0;
  394. rSize = ZSTD_decompressStream(zbd, &outBuffer, &inBuffer);
  395. if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_decompressStream() failed : %s", ZSTD_getErrorName(rSize));
  396. blockTable[blockNb].resSize = outBuffer.pos;
  397. }
  398. nbLoops++;
  399. } while (UTIL_clockSpanMicro(clockStart) < clockLoop);
  400. ZSTD_freeDStream(zbd);
  401. } else if (compressor == BMK_ZWRAP_ZLIB_REUSE || compressor == BMK_ZWRAP_ZSTD_REUSE || compressor == BMK_ZLIB_REUSE) {
  402. z_stream inf;
  403. int ret;
  404. if (compressor == BMK_ZLIB_REUSE) ZWRAP_setDecompressionType(ZWRAP_FORCE_ZLIB);
  405. else ZWRAP_setDecompressionType(ZWRAP_AUTO);
  406. inf.zalloc = Z_NULL;
  407. inf.zfree = Z_NULL;
  408. inf.opaque = Z_NULL;
  409. ret = inflateInit(&inf);
  410. if (ret != Z_OK) EXM_THROW(1, "inflateInit failure");
  411. do {
  412. U32 blockNb;
  413. for (blockNb=0; blockNb<nbBlocks; blockNb++) {
  414. if (ZWRAP_isUsingZSTDdecompression(&inf))
  415. ret = ZWRAP_inflateReset_keepDict(&inf); /* reuse dictionary to make decompression faster; inflate will return Z_NEED_DICT only for the first time */
  416. else
  417. ret = inflateReset(&inf);
  418. if (ret != Z_OK) EXM_THROW(1, "inflateReset failure");
  419. inf.next_in = (z_const z_Bytef*) blockTable[blockNb].cPtr;
  420. inf.avail_in = (uInt)blockTable[blockNb].cSize;
  421. inf.total_in = 0;
  422. inf.next_out = (z_Bytef*) blockTable[blockNb].resPtr;
  423. inf.avail_out = (uInt)blockTable[blockNb].srcSize;
  424. inf.total_out = 0;
  425. ret = inflate(&inf, Z_FINISH);
  426. if (ret == Z_NEED_DICT) {
  427. ret = inflateSetDictionary(&inf, (const z_Bytef*)dictBuffer, dictBufferSize);
  428. if (ret != Z_OK) EXM_THROW(1, "inflateSetDictionary failure");
  429. ret = inflate(&inf, Z_FINISH);
  430. }
  431. if (ret != Z_STREAM_END) EXM_THROW(1, "inflate failure");
  432. blockTable[blockNb].resSize = inf.total_out;
  433. }
  434. nbLoops++;
  435. } while (UTIL_clockSpanMicro(clockStart) < clockLoop);
  436. ret = inflateEnd(&inf);
  437. if (ret != Z_OK) EXM_THROW(1, "inflateEnd failure");
  438. } else {
  439. z_stream inf;
  440. if (compressor == BMK_ZLIB) ZWRAP_setDecompressionType(ZWRAP_FORCE_ZLIB);
  441. else ZWRAP_setDecompressionType(ZWRAP_AUTO);
  442. do {
  443. U32 blockNb;
  444. for (blockNb=0; blockNb<nbBlocks; blockNb++) {
  445. int ret;
  446. inf.zalloc = Z_NULL;
  447. inf.zfree = Z_NULL;
  448. inf.opaque = Z_NULL;
  449. ret = inflateInit(&inf);
  450. if (ret != Z_OK) EXM_THROW(1, "inflateInit failure");
  451. inf.next_in = (z_const z_Bytef*) blockTable[blockNb].cPtr;
  452. inf.avail_in = (uInt)blockTable[blockNb].cSize;
  453. inf.total_in = 0;
  454. inf.next_out = (z_Bytef*) blockTable[blockNb].resPtr;
  455. inf.avail_out = (uInt)blockTable[blockNb].srcSize;
  456. inf.total_out = 0;
  457. ret = inflate(&inf, Z_FINISH);
  458. if (ret == Z_NEED_DICT) {
  459. ret = inflateSetDictionary(&inf, (const z_Bytef*) dictBuffer, dictBufferSize);
  460. if (ret != Z_OK) EXM_THROW(1, "inflateSetDictionary failure");
  461. ret = inflate(&inf, Z_FINISH);
  462. }
  463. if (ret != Z_STREAM_END) EXM_THROW(1, "inflate failure");
  464. ret = inflateEnd(&inf);
  465. if (ret != Z_OK) EXM_THROW(1, "inflateEnd failure");
  466. blockTable[blockNb].resSize = inf.total_out;
  467. }
  468. nbLoops++;
  469. } while (UTIL_clockSpanMicro(clockStart) < clockLoop);
  470. }
  471. { U64 const clockSpan = UTIL_clockSpanMicro(clockStart);
  472. if (clockSpan < fastestD*nbLoops) fastestD = clockSpan / nbLoops;
  473. totalDTime += clockSpan;
  474. dCompleted = totalDTime>maxTime;
  475. } }
  476. markNb = (markNb+1) % NB_MARKS;
  477. DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r",
  478. marks[markNb], displayName, (unsigned)srcSize, (unsigned)cSize, ratio,
  479. (double)srcSize / fastestC,
  480. (double)srcSize / fastestD );
  481. /* CRC Checking */
  482. { U64 const crcCheck = XXH64(resultBuffer, srcSize, 0);
  483. if (crcOrig!=crcCheck) {
  484. size_t u;
  485. DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck);
  486. for (u=0; u<srcSize; u++) {
  487. if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) {
  488. unsigned segNb, bNb, pos;
  489. size_t bacc = 0;
  490. DISPLAY("Decoding error at pos %u ", (unsigned)u);
  491. for (segNb = 0; segNb < nbBlocks; segNb++) {
  492. if (bacc + blockTable[segNb].srcSize > u) break;
  493. bacc += blockTable[segNb].srcSize;
  494. }
  495. pos = (U32)(u - bacc);
  496. bNb = pos / (128 KB);
  497. DISPLAY("(block %u, sub %u, pos %u) \n", segNb, bNb, pos);
  498. break;
  499. }
  500. if (u==srcSize-1) { /* should never happen */
  501. DISPLAY("no difference detected\n");
  502. } }
  503. break;
  504. } } /* CRC Checking */
  505. #endif
  506. } /* for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) */
  507. if (g_displayLevel == 1) {
  508. double cSpeed = (double)srcSize / fastestC;
  509. double dSpeed = (double)srcSize / fastestD;
  510. if (g_additionalParam)
  511. 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);
  512. else
  513. DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName);
  514. }
  515. DISPLAYLEVEL(2, "%2i#\n", cLevel);
  516. } /* Bench */
  517. /* clean up */
  518. free(blockTable);
  519. free(compressedBuffer);
  520. free(resultBuffer);
  521. ZSTD_freeCCtx(ctx);
  522. ZSTD_freeDCtx(dctx);
  523. return 0;
  524. }
  525. static size_t BMK_findMaxMem(U64 requiredMem)
  526. {
  527. size_t const step = 64 MB;
  528. BYTE* testmem = NULL;
  529. requiredMem = (((requiredMem >> 26) + 1) << 26);
  530. requiredMem += step;
  531. if (requiredMem > maxMemory) requiredMem = maxMemory;
  532. do {
  533. testmem = (BYTE*)malloc((size_t)requiredMem);
  534. requiredMem -= step;
  535. } while (!testmem && requiredMem); /* do not allocate zero bytes */
  536. free(testmem);
  537. return (size_t)(requiredMem+1); /* avoid zero */
  538. }
  539. static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize,
  540. const char* displayName, int cLevel, int cLevelLast,
  541. const size_t* fileSizes, unsigned nbFiles,
  542. const void* dictBuffer, size_t dictBufferSize)
  543. {
  544. int l;
  545. const char* pch = strrchr(displayName, '\\'); /* Windows */
  546. if (!pch) pch = strrchr(displayName, '/'); /* Linux */
  547. if (pch) displayName = pch+1;
  548. SET_REALTIME_PRIORITY;
  549. if (g_displayLevel == 1 && !g_additionalParam)
  550. DISPLAY("bench %s %s: input %u bytes, %u seconds, %u KB blocks\n",
  551. ZSTD_VERSION_STRING, ZSTD_GIT_COMMIT_STRING,
  552. (unsigned)benchedSize, g_nbIterations, (unsigned)(g_blockSize>>10));
  553. if (cLevelLast < cLevel) cLevelLast = cLevel;
  554. DISPLAY("benchmarking zstd %s (using ZSTD_CStream)\n", ZSTD_VERSION_STRING);
  555. for (l=cLevel; l <= cLevelLast; l++) {
  556. BMK_benchMem(srcBuffer, benchedSize,
  557. displayName, l,
  558. fileSizes, nbFiles,
  559. dictBuffer, dictBufferSize, BMK_ZSTD_STREAM);
  560. }
  561. DISPLAY("benchmarking zstd %s (using ZSTD_CCtx)\n", ZSTD_VERSION_STRING);
  562. for (l=cLevel; l <= cLevelLast; l++) {
  563. BMK_benchMem(srcBuffer, benchedSize,
  564. displayName, l,
  565. fileSizes, nbFiles,
  566. dictBuffer, dictBufferSize, BMK_ZSTD);
  567. }
  568. DISPLAY("benchmarking zstd %s (using zlibWrapper)\n", ZSTD_VERSION_STRING);
  569. for (l=cLevel; l <= cLevelLast; l++) {
  570. BMK_benchMem(srcBuffer, benchedSize,
  571. displayName, l,
  572. fileSizes, nbFiles,
  573. dictBuffer, dictBufferSize, BMK_ZWRAP_ZSTD_REUSE);
  574. }
  575. DISPLAY("benchmarking zstd %s (zlibWrapper not reusing a context)\n", ZSTD_VERSION_STRING);
  576. for (l=cLevel; l <= cLevelLast; l++) {
  577. BMK_benchMem(srcBuffer, benchedSize,
  578. displayName, l,
  579. fileSizes, nbFiles,
  580. dictBuffer, dictBufferSize, BMK_ZWRAP_ZSTD);
  581. }
  582. if (cLevelLast > Z_BEST_COMPRESSION) cLevelLast = Z_BEST_COMPRESSION;
  583. DISPLAY("\n");
  584. DISPLAY("benchmarking zlib %s\n", ZLIB_VERSION);
  585. for (l=cLevel; l <= cLevelLast; l++) {
  586. BMK_benchMem(srcBuffer, benchedSize,
  587. displayName, l,
  588. fileSizes, nbFiles,
  589. dictBuffer, dictBufferSize, BMK_ZLIB_REUSE);
  590. }
  591. DISPLAY("benchmarking zlib %s (zlib not reusing a context)\n", ZLIB_VERSION);
  592. for (l=cLevel; l <= cLevelLast; l++) {
  593. BMK_benchMem(srcBuffer, benchedSize,
  594. displayName, l,
  595. fileSizes, nbFiles,
  596. dictBuffer, dictBufferSize, BMK_ZLIB);
  597. }
  598. DISPLAY("benchmarking zlib %s (using zlibWrapper)\n", ZLIB_VERSION);
  599. for (l=cLevel; l <= cLevelLast; l++) {
  600. BMK_benchMem(srcBuffer, benchedSize,
  601. displayName, l,
  602. fileSizes, nbFiles,
  603. dictBuffer, dictBufferSize, BMK_ZWRAP_ZLIB_REUSE);
  604. }
  605. DISPLAY("benchmarking zlib %s (zlibWrapper not reusing a context)\n", ZLIB_VERSION);
  606. for (l=cLevel; l <= cLevelLast; l++) {
  607. BMK_benchMem(srcBuffer, benchedSize,
  608. displayName, l,
  609. fileSizes, nbFiles,
  610. dictBuffer, dictBufferSize, BMK_ZWRAP_ZLIB);
  611. }
  612. }
  613. /*! BMK_loadFiles() :
  614. Loads `buffer` with content of files listed within `fileNamesTable`.
  615. At most, fills `buffer` entirely */
  616. static void BMK_loadFiles(void* buffer, size_t bufferSize,
  617. size_t* fileSizes,
  618. const char** fileNamesTable, unsigned nbFiles)
  619. {
  620. size_t pos = 0, totalSize = 0;
  621. unsigned n;
  622. for (n=0; n<nbFiles; n++) {
  623. FILE* f;
  624. U64 fileSize = UTIL_getFileSize(fileNamesTable[n]);
  625. if (UTIL_isDirectory(fileNamesTable[n])) {
  626. DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]);
  627. fileSizes[n] = 0;
  628. continue;
  629. }
  630. if (fileSize == UTIL_FILESIZE_UNKNOWN) {
  631. DISPLAYLEVEL(2, "Cannot determine size of %s ... \n", fileNamesTable[n]);
  632. fileSizes[n] = 0;
  633. continue;
  634. }
  635. f = fopen(fileNamesTable[n], "rb");
  636. if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]);
  637. DISPLAYUPDATE(2, "Loading %s... \r", fileNamesTable[n]);
  638. if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */
  639. { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
  640. if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]);
  641. pos += readSize; }
  642. fileSizes[n] = (size_t)fileSize;
  643. totalSize += (size_t)fileSize;
  644. fclose(f);
  645. }
  646. if (totalSize == 0) EXM_THROW(12, "no data to bench");
  647. }
  648. static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles,
  649. const char* dictFileName, int cLevel, int cLevelLast)
  650. {
  651. void* srcBuffer;
  652. size_t benchedSize;
  653. void* dictBuffer = NULL;
  654. size_t dictBufferSize = 0;
  655. size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t));
  656. U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
  657. char mfName[20] = {0};
  658. if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes");
  659. /* Load dictionary */
  660. if (dictFileName != NULL) {
  661. U64 const dictFileSize = UTIL_getFileSize(dictFileName);
  662. if (dictFileSize > 64 MB)
  663. EXM_THROW(10, "dictionary file %s too large", dictFileName);
  664. dictBufferSize = (size_t)dictFileSize;
  665. dictBuffer = malloc(dictBufferSize);
  666. if (dictBuffer==NULL)
  667. EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (unsigned)dictBufferSize);
  668. BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1);
  669. }
  670. /* Memory allocation & restrictions */
  671. benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
  672. if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
  673. if (benchedSize < totalSizeToLoad)
  674. DISPLAY("Not enough memory; testing %u MB only...\n", (unsigned)(benchedSize >> 20));
  675. srcBuffer = malloc(benchedSize + !benchedSize);
  676. if (!srcBuffer) EXM_THROW(12, "not enough memory");
  677. /* Load input buffer */
  678. BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles);
  679. /* Bench */
  680. snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
  681. { const char* displayName = (nbFiles > 1) ? mfName : fileNamesTable[0];
  682. BMK_benchCLevel(srcBuffer, benchedSize,
  683. displayName, cLevel, cLevelLast,
  684. fileSizes, nbFiles,
  685. dictBuffer, dictBufferSize);
  686. }
  687. /* clean up */
  688. free(srcBuffer);
  689. free(dictBuffer);
  690. free(fileSizes);
  691. }
  692. static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility)
  693. {
  694. char name[20] = {0};
  695. size_t benchedSize = 10000000;
  696. void* const srcBuffer = malloc(benchedSize);
  697. /* Memory allocation */
  698. if (!srcBuffer) EXM_THROW(21, "not enough memory");
  699. /* Fill input buffer */
  700. RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
  701. /* Bench */
  702. snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
  703. BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0);
  704. /* clean up */
  705. free(srcBuffer);
  706. }
  707. int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
  708. const char* dictFileName, int cLevel, int cLevelLast)
  709. {
  710. double const compressibility = (double)g_compressibilityDefault / 100;
  711. if (nbFiles == 0)
  712. BMK_syntheticTest(cLevel, cLevelLast, compressibility);
  713. else
  714. BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast);
  715. return 0;
  716. }
  717. /*-************************************
  718. * Command Line
  719. **************************************/
  720. static int usage(const char* programName)
  721. {
  722. DISPLAY(WELCOME_MESSAGE);
  723. DISPLAY( "Usage :\n");
  724. DISPLAY( " %s [args] [FILE(s)] [-o file]\n", programName);
  725. DISPLAY( "\n");
  726. DISPLAY( "FILE : a filename\n");
  727. DISPLAY( " with no FILE, or when FILE is - , read standard input\n");
  728. DISPLAY( "Arguments :\n");
  729. DISPLAY( " -D file: use `file` as Dictionary \n");
  730. DISPLAY( " -h/-H : display help/long help and exit\n");
  731. DISPLAY( " -V : display Version number and exit\n");
  732. DISPLAY( " -v : verbose mode; specify multiple times to increase log level (default:%d)\n", DEFAULT_DISPLAY_LEVEL);
  733. DISPLAY( " -q : suppress warnings; specify twice to suppress errors too\n");
  734. #ifdef UTIL_HAS_CREATEFILELIST
  735. DISPLAY( " -r : operate recursively on directories\n");
  736. #endif
  737. DISPLAY( "\n");
  738. DISPLAY( "Benchmark arguments :\n");
  739. DISPLAY( " -b# : benchmark file(s), using # compression level (default : %d) \n", ZSTDCLI_CLEVEL_DEFAULT);
  740. DISPLAY( " -e# : test all compression levels from -bX to # (default: %d)\n", ZSTDCLI_CLEVEL_DEFAULT);
  741. DISPLAY( " -i# : minimum evaluation time in seconds (default : 3s)\n");
  742. DISPLAY( " -B# : cut file into independent blocks of size # (default: no block)\n");
  743. return 0;
  744. }
  745. static int badusage(const char* programName)
  746. {
  747. DISPLAYLEVEL(1, "Incorrect parameters\n");
  748. if (g_displayLevel >= 1) usage(programName);
  749. return 1;
  750. }
  751. static void waitEnter(void)
  752. {
  753. int unused;
  754. DISPLAY("Press enter to continue...\n");
  755. unused = getchar();
  756. (void)unused;
  757. }
  758. /*! readU32FromChar() :
  759. @return : unsigned integer value reach from input in `char` format
  760. Will also modify `*stringPtr`, advancing it to position where it stopped reading.
  761. Note : this function can overflow if digit string > MAX_UINT */
  762. static unsigned readU32FromChar(const char** stringPtr)
  763. {
  764. unsigned result = 0;
  765. while ((**stringPtr >='0') && (**stringPtr <='9'))
  766. result *= 10, result += (unsigned)(**stringPtr - '0'), (*stringPtr)++ ;
  767. return result;
  768. }
  769. #define CLEAN_RETURN(i) { operationResult = (i); goto _end; }
  770. int main(int argCount, char** argv)
  771. {
  772. int argNb,
  773. main_pause=0,
  774. nextEntryIsDictionary=0,
  775. operationResult=0,
  776. nextArgumentIsFile=0;
  777. int cLevel = ZSTDCLI_CLEVEL_DEFAULT;
  778. int cLevelLast = 1;
  779. unsigned recursive = 0;
  780. FileNamesTable* filenames = UTIL_allocateFileNamesTable((size_t)argCount);
  781. const char* programName = argv[0];
  782. const char* dictFileName = NULL;
  783. char* dynNameSpace = NULL;
  784. /* init */
  785. if (filenames==NULL) { DISPLAY("zstd: %s \n", strerror(errno)); exit(1); }
  786. displayOut = stderr;
  787. /* Pick out program name from path. Don't rely on stdlib because of conflicting behavior */
  788. { size_t pos;
  789. for (pos = strlen(programName); pos > 0; pos--) { if (programName[pos] == '/') { pos++; break; } }
  790. programName += pos;
  791. }
  792. /* command switches */
  793. for(argNb=1; argNb<argCount; argNb++) {
  794. const char* argument = argv[argNb];
  795. if(!argument) continue; /* Protection if argument empty */
  796. if (nextArgumentIsFile==0) {
  797. /* long commands (--long-word) */
  798. if (!strcmp(argument, "--")) { nextArgumentIsFile=1; continue; }
  799. if (!strcmp(argument, "--version")) { displayOut=stdout; DISPLAY(WELCOME_MESSAGE); CLEAN_RETURN(0); }
  800. if (!strcmp(argument, "--help")) { displayOut=stdout; CLEAN_RETURN(usage(programName)); }
  801. if (!strcmp(argument, "--verbose")) { g_displayLevel++; continue; }
  802. if (!strcmp(argument, "--quiet")) { g_displayLevel--; continue; }
  803. /* Decode commands (note : aggregated commands are allowed) */
  804. if (argument[0]=='-') {
  805. argument++;
  806. while (argument[0]!=0) {
  807. switch(argument[0])
  808. {
  809. /* Display help */
  810. case 'V': displayOut=stdout; DISPLAY(WELCOME_MESSAGE); CLEAN_RETURN(0); /* Version Only */
  811. case 'H':
  812. case 'h': displayOut=stdout; CLEAN_RETURN(usage(programName));
  813. /* Use file content as dictionary */
  814. case 'D': nextEntryIsDictionary = 1; argument++; break;
  815. /* Verbose mode */
  816. case 'v': g_displayLevel++; argument++; break;
  817. /* Quiet mode */
  818. case 'q': g_displayLevel--; argument++; break;
  819. #ifdef UTIL_HAS_CREATEFILELIST
  820. /* recursive */
  821. case 'r': recursive=1; argument++; break;
  822. #endif
  823. /* Benchmark */
  824. case 'b':
  825. /* first compression Level */
  826. argument++;
  827. cLevel = (int)readU32FromChar(&argument);
  828. break;
  829. /* range bench (benchmark only) */
  830. case 'e':
  831. /* last compression Level */
  832. argument++;
  833. cLevelLast = (int)readU32FromChar(&argument);
  834. break;
  835. /* Modify Nb Iterations (benchmark only) */
  836. case 'i':
  837. argument++;
  838. { U32 const iters = readU32FromChar(&argument);
  839. BMK_setNotificationLevel(g_displayLevel);
  840. BMK_SetNbIterations(iters);
  841. }
  842. break;
  843. /* cut input into blocks (benchmark only) */
  844. case 'B':
  845. argument++;
  846. { size_t bSize = readU32FromChar(&argument);
  847. if (toupper(*argument)=='K') bSize<<=10, argument++; /* allows using KB notation */
  848. if (toupper(*argument)=='M') bSize<<=20, argument++;
  849. if (toupper(*argument)=='B') argument++;
  850. BMK_setNotificationLevel(g_displayLevel);
  851. BMK_SetBlockSize(bSize);
  852. }
  853. break;
  854. /* Pause at the end (-p) or set an additional param (-p#) (hidden option) */
  855. case 'p': argument++;
  856. if ((*argument>='0') && (*argument<='9')) {
  857. BMK_setAdditionalParam((int)readU32FromChar(&argument));
  858. } else
  859. main_pause=1;
  860. break;
  861. /* unknown command */
  862. default : CLEAN_RETURN(badusage(programName));
  863. }
  864. }
  865. continue;
  866. } /* if (argument[0]=='-') */
  867. } /* if (nextArgumentIsAFile==0) */
  868. if (nextEntryIsDictionary) {
  869. nextEntryIsDictionary = 0;
  870. dictFileName = argument;
  871. continue;
  872. }
  873. /* add filename to list */
  874. UTIL_refFilename(filenames, argument);
  875. }
  876. /* Welcome message (if verbose) */
  877. DISPLAYLEVEL(3, WELCOME_MESSAGE);
  878. #ifdef UTIL_HAS_CREATEFILELIST
  879. if (recursive) {
  880. UTIL_expandFNT(&filenames, 1);
  881. }
  882. #endif
  883. BMK_setNotificationLevel(g_displayLevel);
  884. BMK_benchFiles(filenames->fileNames, (unsigned)filenames->tableSize, dictFileName, cLevel, cLevelLast);
  885. _end:
  886. if (main_pause) waitEnter();
  887. free(dynNameSpace);
  888. UTIL_freeFileNamesTable(filenames);
  889. return operationResult;
  890. }