benchzstd.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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. * Tuning parameters
  12. ****************************************/
  13. #ifndef BMK_TIMETEST_DEFAULT_S /* default minimum time per test */
  14. #define BMK_TIMETEST_DEFAULT_S 3
  15. #endif
  16. /* *************************************
  17. * Includes
  18. ***************************************/
  19. #include "platform.h" /* Large Files support */
  20. #include "util.h" /* UTIL_getFileSize, UTIL_sleep */
  21. #include <stdlib.h> /* malloc, free */
  22. #include <string.h> /* memset, strerror */
  23. #include <stdio.h> /* fprintf, fopen */
  24. #include <errno.h>
  25. #include <assert.h> /* assert */
  26. #include "timefn.h" /* UTIL_time_t */
  27. #include "benchfn.h"
  28. #include "../lib/common/mem.h"
  29. #ifndef ZSTD_STATIC_LINKING_ONLY
  30. #define ZSTD_STATIC_LINKING_ONLY
  31. #endif
  32. #include "../lib/zstd.h"
  33. #include "datagen.h" /* RDG_genBuffer */
  34. #ifndef XXH_INLINE_ALL
  35. #define XXH_INLINE_ALL
  36. #endif
  37. #include "../lib/common/xxhash.h"
  38. #include "benchzstd.h"
  39. #include "../lib/zstd_errors.h"
  40. /* *************************************
  41. * Constants
  42. ***************************************/
  43. #ifndef ZSTD_GIT_COMMIT
  44. # define ZSTD_GIT_COMMIT_STRING ""
  45. #else
  46. # define ZSTD_GIT_COMMIT_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_GIT_COMMIT)
  47. #endif
  48. #define TIMELOOP_MICROSEC (1*1000000ULL) /* 1 second */
  49. #define TIMELOOP_NANOSEC (1*1000000000ULL) /* 1 second */
  50. #define ACTIVEPERIOD_MICROSEC (70*TIMELOOP_MICROSEC) /* 70 seconds */
  51. #define COOLPERIOD_SEC 10
  52. #define KB *(1 <<10)
  53. #define MB *(1 <<20)
  54. #define GB *(1U<<30)
  55. #define BMK_RUNTEST_DEFAULT_MS 1000
  56. static const size_t maxMemory = (sizeof(size_t)==4) ?
  57. /* 32-bit */ (2 GB - 64 MB) :
  58. /* 64-bit */ (size_t)(1ULL << ((sizeof(size_t)*8)-31));
  59. /* *************************************
  60. * console display
  61. ***************************************/
  62. #define DISPLAY(...) { fprintf(stderr, __VA_ARGS__); fflush(NULL); }
  63. #define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); }
  64. /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
  65. #define OUTPUT(...) { fprintf(stdout, __VA_ARGS__); fflush(NULL); }
  66. #define OUTPUTLEVEL(l, ...) if (displayLevel>=l) { OUTPUT(__VA_ARGS__); }
  67. /* *************************************
  68. * Exceptions
  69. ***************************************/
  70. #ifndef DEBUG
  71. # define DEBUG 0
  72. #endif
  73. #define DEBUGOUTPUT(...) { if (DEBUG) DISPLAY(__VA_ARGS__); }
  74. #define RETURN_ERROR_INT(errorNum, ...) { \
  75. DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
  76. DISPLAYLEVEL(1, "Error %i : ", errorNum); \
  77. DISPLAYLEVEL(1, __VA_ARGS__); \
  78. DISPLAYLEVEL(1, " \n"); \
  79. return errorNum; \
  80. }
  81. #define CHECK_Z(zf) { \
  82. size_t const zerr = zf; \
  83. if (ZSTD_isError(zerr)) { \
  84. DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
  85. DISPLAY("Error : "); \
  86. DISPLAY("%s failed : %s", \
  87. #zf, ZSTD_getErrorName(zerr)); \
  88. DISPLAY(" \n"); \
  89. exit(1); \
  90. } \
  91. }
  92. #define RETURN_ERROR(errorNum, retType, ...) { \
  93. retType r; \
  94. memset(&r, 0, sizeof(retType)); \
  95. DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
  96. DISPLAYLEVEL(1, "Error %i : ", errorNum); \
  97. DISPLAYLEVEL(1, __VA_ARGS__); \
  98. DISPLAYLEVEL(1, " \n"); \
  99. r.tag = errorNum; \
  100. return r; \
  101. }
  102. /* *************************************
  103. * Benchmark Parameters
  104. ***************************************/
  105. BMK_advancedParams_t BMK_initAdvancedParams(void) {
  106. BMK_advancedParams_t const res = {
  107. BMK_both, /* mode */
  108. BMK_TIMETEST_DEFAULT_S, /* nbSeconds */
  109. 0, /* blockSize */
  110. 0, /* nbWorkers */
  111. 0, /* realTime */
  112. 0, /* additionalParam */
  113. 0, /* ldmFlag */
  114. 0, /* ldmMinMatch */
  115. 0, /* ldmHashLog */
  116. 0, /* ldmBuckSizeLog */
  117. 0, /* ldmHashRateLog */
  118. ZSTD_ps_auto, /* literalCompressionMode */
  119. 0 /* useRowMatchFinder */
  120. };
  121. return res;
  122. }
  123. /* ********************************************************
  124. * Bench functions
  125. **********************************************************/
  126. typedef struct {
  127. const void* srcPtr;
  128. size_t srcSize;
  129. void* cPtr;
  130. size_t cRoom;
  131. size_t cSize;
  132. void* resPtr;
  133. size_t resSize;
  134. } blockParam_t;
  135. #undef MIN
  136. #undef MAX
  137. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  138. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  139. static void
  140. BMK_initCCtx(ZSTD_CCtx* ctx,
  141. const void* dictBuffer, size_t dictBufferSize,
  142. int cLevel,
  143. const ZSTD_compressionParameters* comprParams,
  144. const BMK_advancedParams_t* adv)
  145. {
  146. ZSTD_CCtx_reset(ctx, ZSTD_reset_session_and_parameters);
  147. if (adv->nbWorkers==1) {
  148. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_nbWorkers, 0));
  149. } else {
  150. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_nbWorkers, adv->nbWorkers));
  151. }
  152. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, cLevel));
  153. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_useRowMatchFinder, adv->useRowMatchFinder));
  154. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_enableLongDistanceMatching, adv->ldmFlag));
  155. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmMinMatch, adv->ldmMinMatch));
  156. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmHashLog, adv->ldmHashLog));
  157. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmBucketSizeLog, adv->ldmBucketSizeLog));
  158. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmHashRateLog, adv->ldmHashRateLog));
  159. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_windowLog, (int)comprParams->windowLog));
  160. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_hashLog, (int)comprParams->hashLog));
  161. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_chainLog, (int)comprParams->chainLog));
  162. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_searchLog, (int)comprParams->searchLog));
  163. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_minMatch, (int)comprParams->minMatch));
  164. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_targetLength, (int)comprParams->targetLength));
  165. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_literalCompressionMode, (int)adv->literalCompressionMode));
  166. CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_strategy, (int)comprParams->strategy));
  167. CHECK_Z(ZSTD_CCtx_loadDictionary(ctx, dictBuffer, dictBufferSize));
  168. }
  169. static void BMK_initDCtx(ZSTD_DCtx* dctx,
  170. const void* dictBuffer, size_t dictBufferSize) {
  171. CHECK_Z(ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters));
  172. CHECK_Z(ZSTD_DCtx_loadDictionary(dctx, dictBuffer, dictBufferSize));
  173. }
  174. typedef struct {
  175. ZSTD_CCtx* cctx;
  176. const void* dictBuffer;
  177. size_t dictBufferSize;
  178. int cLevel;
  179. const ZSTD_compressionParameters* comprParams;
  180. const BMK_advancedParams_t* adv;
  181. } BMK_initCCtxArgs;
  182. static size_t local_initCCtx(void* payload) {
  183. BMK_initCCtxArgs* ag = (BMK_initCCtxArgs*)payload;
  184. BMK_initCCtx(ag->cctx, ag->dictBuffer, ag->dictBufferSize, ag->cLevel, ag->comprParams, ag->adv);
  185. return 0;
  186. }
  187. typedef struct {
  188. ZSTD_DCtx* dctx;
  189. const void* dictBuffer;
  190. size_t dictBufferSize;
  191. } BMK_initDCtxArgs;
  192. static size_t local_initDCtx(void* payload) {
  193. BMK_initDCtxArgs* ag = (BMK_initDCtxArgs*)payload;
  194. BMK_initDCtx(ag->dctx, ag->dictBuffer, ag->dictBufferSize);
  195. return 0;
  196. }
  197. /* `addArgs` is the context */
  198. static size_t local_defaultCompress(
  199. const void* srcBuffer, size_t srcSize,
  200. void* dstBuffer, size_t dstSize,
  201. void* addArgs)
  202. {
  203. ZSTD_CCtx* const cctx = (ZSTD_CCtx*)addArgs;
  204. return ZSTD_compress2(cctx, dstBuffer, dstSize, srcBuffer, srcSize);
  205. }
  206. /* `addArgs` is the context */
  207. static size_t local_defaultDecompress(
  208. const void* srcBuffer, size_t srcSize,
  209. void* dstBuffer, size_t dstCapacity,
  210. void* addArgs)
  211. {
  212. size_t moreToFlush = 1;
  213. ZSTD_DCtx* const dctx = (ZSTD_DCtx*)addArgs;
  214. ZSTD_inBuffer in;
  215. ZSTD_outBuffer out;
  216. in.src = srcBuffer; in.size = srcSize; in.pos = 0;
  217. out.dst = dstBuffer; out.size = dstCapacity; out.pos = 0;
  218. while (moreToFlush) {
  219. if(out.pos == out.size) {
  220. return (size_t)-ZSTD_error_dstSize_tooSmall;
  221. }
  222. moreToFlush = ZSTD_decompressStream(dctx, &out, &in);
  223. if (ZSTD_isError(moreToFlush)) {
  224. return moreToFlush;
  225. }
  226. }
  227. return out.pos;
  228. }
  229. /* ================================================================= */
  230. /* Benchmark Zstandard, mem-to-mem scenarios */
  231. /* ================================================================= */
  232. int BMK_isSuccessful_benchOutcome(BMK_benchOutcome_t outcome)
  233. {
  234. return outcome.tag == 0;
  235. }
  236. BMK_benchResult_t BMK_extract_benchResult(BMK_benchOutcome_t outcome)
  237. {
  238. assert(outcome.tag == 0);
  239. return outcome.internal_never_use_directly;
  240. }
  241. static BMK_benchOutcome_t BMK_benchOutcome_error(void)
  242. {
  243. BMK_benchOutcome_t b;
  244. memset(&b, 0, sizeof(b));
  245. b.tag = 1;
  246. return b;
  247. }
  248. static BMK_benchOutcome_t BMK_benchOutcome_setValidResult(BMK_benchResult_t result)
  249. {
  250. BMK_benchOutcome_t b;
  251. b.tag = 0;
  252. b.internal_never_use_directly = result;
  253. return b;
  254. }
  255. /* benchMem with no allocation */
  256. static BMK_benchOutcome_t
  257. BMK_benchMemAdvancedNoAlloc(
  258. const void** srcPtrs, size_t* srcSizes,
  259. void** cPtrs, size_t* cCapacities, size_t* cSizes,
  260. void** resPtrs, size_t* resSizes,
  261. void** resultBufferPtr, void* compressedBuffer,
  262. size_t maxCompressedSize,
  263. BMK_timedFnState_t* timeStateCompress,
  264. BMK_timedFnState_t* timeStateDecompress,
  265. const void* srcBuffer, size_t srcSize,
  266. const size_t* fileSizes, unsigned nbFiles,
  267. const int cLevel,
  268. const ZSTD_compressionParameters* comprParams,
  269. const void* dictBuffer, size_t dictBufferSize,
  270. ZSTD_CCtx* cctx, ZSTD_DCtx* dctx,
  271. int displayLevel, const char* displayName,
  272. const BMK_advancedParams_t* adv)
  273. {
  274. size_t const blockSize = ((adv->blockSize>=32 && (adv->mode != BMK_decodeOnly)) ? adv->blockSize : srcSize) + (!srcSize); /* avoid div by 0 */
  275. BMK_benchResult_t benchResult;
  276. size_t const loadedCompressedSize = srcSize;
  277. size_t cSize = 0;
  278. double ratio = 0.;
  279. U32 nbBlocks;
  280. assert(cctx != NULL); assert(dctx != NULL);
  281. /* init */
  282. memset(&benchResult, 0, sizeof(benchResult));
  283. if (strlen(displayName)>17) displayName += strlen(displayName) - 17; /* display last 17 characters */
  284. if (adv->mode == BMK_decodeOnly) { /* benchmark only decompression : source must be already compressed */
  285. const char* srcPtr = (const char*)srcBuffer;
  286. U64 totalDSize64 = 0;
  287. U32 fileNb;
  288. for (fileNb=0; fileNb<nbFiles; fileNb++) {
  289. U64 const fSize64 = ZSTD_findDecompressedSize(srcPtr, fileSizes[fileNb]);
  290. if (fSize64==0) RETURN_ERROR(32, BMK_benchOutcome_t, "Impossible to determine original size ");
  291. totalDSize64 += fSize64;
  292. srcPtr += fileSizes[fileNb];
  293. }
  294. { size_t const decodedSize = (size_t)totalDSize64;
  295. assert((U64)decodedSize == totalDSize64); /* check overflow */
  296. free(*resultBufferPtr);
  297. *resultBufferPtr = malloc(decodedSize);
  298. if (!(*resultBufferPtr)) {
  299. RETURN_ERROR(33, BMK_benchOutcome_t, "not enough memory");
  300. }
  301. if (totalDSize64 > decodedSize) { /* size_t overflow */
  302. free(*resultBufferPtr);
  303. RETURN_ERROR(32, BMK_benchOutcome_t, "original size is too large");
  304. }
  305. cSize = srcSize;
  306. srcSize = decodedSize;
  307. ratio = (double)srcSize / (double)cSize;
  308. }
  309. }
  310. /* Init data blocks */
  311. { const char* srcPtr = (const char*)srcBuffer;
  312. char* cPtr = (char*)compressedBuffer;
  313. char* resPtr = (char*)(*resultBufferPtr);
  314. U32 fileNb;
  315. for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
  316. size_t remaining = fileSizes[fileNb];
  317. U32 const nbBlocksforThisFile = (adv->mode == BMK_decodeOnly) ? 1 : (U32)((remaining + (blockSize-1)) / blockSize);
  318. U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
  319. for ( ; nbBlocks<blockEnd; nbBlocks++) {
  320. size_t const thisBlockSize = MIN(remaining, blockSize);
  321. srcPtrs[nbBlocks] = srcPtr;
  322. srcSizes[nbBlocks] = thisBlockSize;
  323. cPtrs[nbBlocks] = cPtr;
  324. cCapacities[nbBlocks] = (adv->mode == BMK_decodeOnly) ? thisBlockSize : ZSTD_compressBound(thisBlockSize);
  325. resPtrs[nbBlocks] = resPtr;
  326. resSizes[nbBlocks] = (adv->mode == BMK_decodeOnly) ? (size_t) ZSTD_findDecompressedSize(srcPtr, thisBlockSize) : thisBlockSize;
  327. srcPtr += thisBlockSize;
  328. cPtr += cCapacities[nbBlocks];
  329. resPtr += thisBlockSize;
  330. remaining -= thisBlockSize;
  331. if (adv->mode == BMK_decodeOnly) {
  332. cSizes[nbBlocks] = thisBlockSize;
  333. benchResult.cSize = thisBlockSize;
  334. } } } }
  335. /* warming up `compressedBuffer` */
  336. if (adv->mode == BMK_decodeOnly) {
  337. memcpy(compressedBuffer, srcBuffer, loadedCompressedSize);
  338. } else {
  339. RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
  340. }
  341. /* Bench */
  342. { U64 const crcOrig = (adv->mode == BMK_decodeOnly) ? 0 : XXH64(srcBuffer, srcSize, 0);
  343. # define NB_MARKS 4
  344. const char* marks[NB_MARKS] = { " |", " /", " =", " \\" };
  345. U32 markNb = 0;
  346. int compressionCompleted = (adv->mode == BMK_decodeOnly);
  347. int decompressionCompleted = (adv->mode == BMK_compressOnly);
  348. BMK_benchParams_t cbp, dbp;
  349. BMK_initCCtxArgs cctxprep;
  350. BMK_initDCtxArgs dctxprep;
  351. cbp.benchFn = local_defaultCompress; /* ZSTD_compress2 */
  352. cbp.benchPayload = cctx;
  353. cbp.initFn = local_initCCtx; /* BMK_initCCtx */
  354. cbp.initPayload = &cctxprep;
  355. cbp.errorFn = ZSTD_isError;
  356. cbp.blockCount = nbBlocks;
  357. cbp.srcBuffers = srcPtrs;
  358. cbp.srcSizes = srcSizes;
  359. cbp.dstBuffers = cPtrs;
  360. cbp.dstCapacities = cCapacities;
  361. cbp.blockResults = cSizes;
  362. cctxprep.cctx = cctx;
  363. cctxprep.dictBuffer = dictBuffer;
  364. cctxprep.dictBufferSize = dictBufferSize;
  365. cctxprep.cLevel = cLevel;
  366. cctxprep.comprParams = comprParams;
  367. cctxprep.adv = adv;
  368. dbp.benchFn = local_defaultDecompress;
  369. dbp.benchPayload = dctx;
  370. dbp.initFn = local_initDCtx;
  371. dbp.initPayload = &dctxprep;
  372. dbp.errorFn = ZSTD_isError;
  373. dbp.blockCount = nbBlocks;
  374. dbp.srcBuffers = (const void* const *) cPtrs;
  375. dbp.srcSizes = cSizes;
  376. dbp.dstBuffers = resPtrs;
  377. dbp.dstCapacities = resSizes;
  378. dbp.blockResults = NULL;
  379. dctxprep.dctx = dctx;
  380. dctxprep.dictBuffer = dictBuffer;
  381. dctxprep.dictBufferSize = dictBufferSize;
  382. OUTPUTLEVEL(2, "\r%70s\r", ""); /* blank line */
  383. assert(srcSize < UINT_MAX);
  384. OUTPUTLEVEL(2, "%2s-%-17.17s :%10u -> \r", marks[markNb], displayName, (unsigned)srcSize);
  385. while (!(compressionCompleted && decompressionCompleted)) {
  386. if (!compressionCompleted) {
  387. BMK_runOutcome_t const cOutcome = BMK_benchTimedFn( timeStateCompress, cbp);
  388. if (!BMK_isSuccessful_runOutcome(cOutcome)) {
  389. return BMK_benchOutcome_error();
  390. }
  391. { BMK_runTime_t const cResult = BMK_extract_runTime(cOutcome);
  392. cSize = cResult.sumOfReturn;
  393. ratio = (double)srcSize / (double)cSize;
  394. { BMK_benchResult_t newResult;
  395. newResult.cSpeed = (U64)((double)srcSize * TIMELOOP_NANOSEC / cResult.nanoSecPerRun);
  396. benchResult.cSize = cSize;
  397. if (newResult.cSpeed > benchResult.cSpeed)
  398. benchResult.cSpeed = newResult.cSpeed;
  399. } }
  400. { int const ratioAccuracy = (ratio < 10.) ? 3 : 2;
  401. assert(cSize < UINT_MAX);
  402. OUTPUTLEVEL(2, "%2s-%-17.17s :%10u ->%10u (x%5.*f), %6.*f MB/s \r",
  403. marks[markNb], displayName,
  404. (unsigned)srcSize, (unsigned)cSize,
  405. ratioAccuracy, ratio,
  406. benchResult.cSpeed < (10 * MB_UNIT) ? 2 : 1, (double)benchResult.cSpeed / MB_UNIT);
  407. }
  408. compressionCompleted = BMK_isCompleted_TimedFn(timeStateCompress);
  409. }
  410. if(!decompressionCompleted) {
  411. BMK_runOutcome_t const dOutcome = BMK_benchTimedFn(timeStateDecompress, dbp);
  412. if(!BMK_isSuccessful_runOutcome(dOutcome)) {
  413. return BMK_benchOutcome_error();
  414. }
  415. { BMK_runTime_t const dResult = BMK_extract_runTime(dOutcome);
  416. U64 const newDSpeed = (U64)((double)srcSize * TIMELOOP_NANOSEC / dResult.nanoSecPerRun);
  417. if (newDSpeed > benchResult.dSpeed)
  418. benchResult.dSpeed = newDSpeed;
  419. }
  420. { int const ratioAccuracy = (ratio < 10.) ? 3 : 2;
  421. OUTPUTLEVEL(2, "%2s-%-17.17s :%10u ->%10u (x%5.*f), %6.*f MB/s, %6.1f MB/s\r",
  422. marks[markNb], displayName,
  423. (unsigned)srcSize, (unsigned)cSize,
  424. ratioAccuracy, ratio,
  425. benchResult.cSpeed < (10 * MB_UNIT) ? 2 : 1, (double)benchResult.cSpeed / MB_UNIT,
  426. (double)benchResult.dSpeed / MB_UNIT);
  427. }
  428. decompressionCompleted = BMK_isCompleted_TimedFn(timeStateDecompress);
  429. }
  430. markNb = (markNb+1) % NB_MARKS;
  431. } /* while (!(compressionCompleted && decompressionCompleted)) */
  432. /* CRC Checking */
  433. { const BYTE* resultBuffer = (const BYTE*)(*resultBufferPtr);
  434. U64 const crcCheck = XXH64(resultBuffer, srcSize, 0);
  435. if ((adv->mode == BMK_both) && (crcOrig!=crcCheck)) {
  436. size_t u;
  437. DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n",
  438. displayName, (unsigned)crcOrig, (unsigned)crcCheck);
  439. for (u=0; u<srcSize; u++) {
  440. if (((const BYTE*)srcBuffer)[u] != resultBuffer[u]) {
  441. unsigned segNb, bNb, pos;
  442. size_t bacc = 0;
  443. DISPLAY("Decoding error at pos %u ", (unsigned)u);
  444. for (segNb = 0; segNb < nbBlocks; segNb++) {
  445. if (bacc + srcSizes[segNb] > u) break;
  446. bacc += srcSizes[segNb];
  447. }
  448. pos = (U32)(u - bacc);
  449. bNb = pos / (128 KB);
  450. DISPLAY("(sample %u, block %u, pos %u) \n", segNb, bNb, pos);
  451. { size_t const lowest = (u>5) ? 5 : u;
  452. size_t n;
  453. DISPLAY("origin: ");
  454. for (n=lowest; n>0; n--)
  455. DISPLAY("%02X ", ((const BYTE*)srcBuffer)[u-n]);
  456. DISPLAY(" :%02X: ", ((const BYTE*)srcBuffer)[u]);
  457. for (n=1; n<3; n++)
  458. DISPLAY("%02X ", ((const BYTE*)srcBuffer)[u+n]);
  459. DISPLAY(" \n");
  460. DISPLAY("decode: ");
  461. for (n=lowest; n>0; n--)
  462. DISPLAY("%02X ", resultBuffer[u-n]);
  463. DISPLAY(" :%02X: ", resultBuffer[u]);
  464. for (n=1; n<3; n++)
  465. DISPLAY("%02X ", resultBuffer[u+n]);
  466. DISPLAY(" \n");
  467. }
  468. break;
  469. }
  470. if (u==srcSize-1) { /* should never happen */
  471. DISPLAY("no difference detected\n");
  472. }
  473. } /* for (u=0; u<srcSize; u++) */
  474. } /* if ((adv->mode == BMK_both) && (crcOrig!=crcCheck)) */
  475. } /* CRC Checking */
  476. if (displayLevel == 1) { /* hidden display mode -q, used by python speed benchmark */
  477. double const cSpeed = (double)benchResult.cSpeed / MB_UNIT;
  478. double const dSpeed = (double)benchResult.dSpeed / MB_UNIT;
  479. if (adv->additionalParam) {
  480. OUTPUT("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s (param=%d)\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName, adv->additionalParam);
  481. } else {
  482. OUTPUT("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName);
  483. }
  484. }
  485. OUTPUTLEVEL(2, "%2i#\n", cLevel);
  486. } /* Bench */
  487. benchResult.cMem = (1ULL << (comprParams->windowLog)) + ZSTD_sizeof_CCtx(cctx);
  488. return BMK_benchOutcome_setValidResult(benchResult);
  489. }
  490. BMK_benchOutcome_t BMK_benchMemAdvanced(const void* srcBuffer, size_t srcSize,
  491. void* dstBuffer, size_t dstCapacity,
  492. const size_t* fileSizes, unsigned nbFiles,
  493. int cLevel, const ZSTD_compressionParameters* comprParams,
  494. const void* dictBuffer, size_t dictBufferSize,
  495. int displayLevel, const char* displayName, const BMK_advancedParams_t* adv)
  496. {
  497. int const dstParamsError = !dstBuffer ^ !dstCapacity; /* must be both NULL or none */
  498. size_t const blockSize = ((adv->blockSize>=32 && (adv->mode != BMK_decodeOnly)) ? adv->blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ;
  499. U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
  500. /* these are the blockTable parameters, just split up */
  501. const void ** const srcPtrs = (const void**)malloc(maxNbBlocks * sizeof(void*));
  502. size_t* const srcSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
  503. void ** const cPtrs = (void**)malloc(maxNbBlocks * sizeof(void*));
  504. size_t* const cSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
  505. size_t* const cCapacities = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
  506. void ** const resPtrs = (void**)malloc(maxNbBlocks * sizeof(void*));
  507. size_t* const resSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
  508. BMK_timedFnState_t* timeStateCompress = BMK_createTimedFnState(adv->nbSeconds * 1000, BMK_RUNTEST_DEFAULT_MS);
  509. BMK_timedFnState_t* timeStateDecompress = BMK_createTimedFnState(adv->nbSeconds * 1000, BMK_RUNTEST_DEFAULT_MS);
  510. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  511. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  512. const size_t maxCompressedSize = dstCapacity ? dstCapacity : ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024);
  513. void* const internalDstBuffer = dstBuffer ? NULL : malloc(maxCompressedSize);
  514. void* const compressedBuffer = dstBuffer ? dstBuffer : internalDstBuffer;
  515. BMK_benchOutcome_t outcome = BMK_benchOutcome_error(); /* error by default */
  516. void* resultBuffer = srcSize ? malloc(srcSize) : NULL;
  517. int allocationincomplete = !srcPtrs || !srcSizes || !cPtrs ||
  518. !cSizes || !cCapacities || !resPtrs || !resSizes ||
  519. !timeStateCompress || !timeStateDecompress ||
  520. !cctx || !dctx ||
  521. !compressedBuffer || !resultBuffer;
  522. if (!allocationincomplete && !dstParamsError) {
  523. outcome = BMK_benchMemAdvancedNoAlloc(srcPtrs, srcSizes,
  524. cPtrs, cCapacities, cSizes,
  525. resPtrs, resSizes,
  526. &resultBuffer,
  527. compressedBuffer, maxCompressedSize,
  528. timeStateCompress, timeStateDecompress,
  529. srcBuffer, srcSize,
  530. fileSizes, nbFiles,
  531. cLevel, comprParams,
  532. dictBuffer, dictBufferSize,
  533. cctx, dctx,
  534. displayLevel, displayName, adv);
  535. }
  536. /* clean up */
  537. BMK_freeTimedFnState(timeStateCompress);
  538. BMK_freeTimedFnState(timeStateDecompress);
  539. ZSTD_freeCCtx(cctx);
  540. ZSTD_freeDCtx(dctx);
  541. free(internalDstBuffer);
  542. free(resultBuffer);
  543. free((void*)srcPtrs);
  544. free(srcSizes);
  545. free(cPtrs);
  546. free(cSizes);
  547. free(cCapacities);
  548. free(resPtrs);
  549. free(resSizes);
  550. if(allocationincomplete) {
  551. RETURN_ERROR(31, BMK_benchOutcome_t, "allocation error : not enough memory");
  552. }
  553. if(dstParamsError) {
  554. RETURN_ERROR(32, BMK_benchOutcome_t, "Dst parameters not coherent");
  555. }
  556. return outcome;
  557. }
  558. BMK_benchOutcome_t BMK_benchMem(const void* srcBuffer, size_t srcSize,
  559. const size_t* fileSizes, unsigned nbFiles,
  560. int cLevel, const ZSTD_compressionParameters* comprParams,
  561. const void* dictBuffer, size_t dictBufferSize,
  562. int displayLevel, const char* displayName) {
  563. BMK_advancedParams_t const adv = BMK_initAdvancedParams();
  564. return BMK_benchMemAdvanced(srcBuffer, srcSize,
  565. NULL, 0,
  566. fileSizes, nbFiles,
  567. cLevel, comprParams,
  568. dictBuffer, dictBufferSize,
  569. displayLevel, displayName, &adv);
  570. }
  571. static BMK_benchOutcome_t BMK_benchCLevel(const void* srcBuffer, size_t benchedSize,
  572. const size_t* fileSizes, unsigned nbFiles,
  573. int cLevel, const ZSTD_compressionParameters* comprParams,
  574. const void* dictBuffer, size_t dictBufferSize,
  575. int displayLevel, const char* displayName,
  576. BMK_advancedParams_t const * const adv)
  577. {
  578. const char* pch = strrchr(displayName, '\\'); /* Windows */
  579. if (!pch) pch = strrchr(displayName, '/'); /* Linux */
  580. if (pch) displayName = pch+1;
  581. if (adv->realTime) {
  582. DISPLAYLEVEL(2, "Note : switching to real-time priority \n");
  583. SET_REALTIME_PRIORITY;
  584. }
  585. if (displayLevel == 1 && !adv->additionalParam) /* --quiet mode */
  586. OUTPUT("bench %s %s: input %u bytes, %u seconds, %u KB blocks\n",
  587. ZSTD_VERSION_STRING, ZSTD_GIT_COMMIT_STRING,
  588. (unsigned)benchedSize, adv->nbSeconds, (unsigned)(adv->blockSize>>10));
  589. return BMK_benchMemAdvanced(srcBuffer, benchedSize,
  590. NULL, 0,
  591. fileSizes, nbFiles,
  592. cLevel, comprParams,
  593. dictBuffer, dictBufferSize,
  594. displayLevel, displayName, adv);
  595. }
  596. BMK_benchOutcome_t BMK_syntheticTest(int cLevel, double compressibility,
  597. const ZSTD_compressionParameters* compressionParams,
  598. int displayLevel, const BMK_advancedParams_t* adv)
  599. {
  600. char name[20] = {0};
  601. size_t const benchedSize = 10000000;
  602. void* srcBuffer;
  603. BMK_benchOutcome_t res;
  604. if (cLevel > ZSTD_maxCLevel()) {
  605. RETURN_ERROR(15, BMK_benchOutcome_t, "Invalid Compression Level");
  606. }
  607. /* Memory allocation */
  608. srcBuffer = malloc(benchedSize);
  609. if (!srcBuffer) RETURN_ERROR(21, BMK_benchOutcome_t, "not enough memory");
  610. /* Fill input buffer */
  611. RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
  612. /* Bench */
  613. snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
  614. res = BMK_benchCLevel(srcBuffer, benchedSize,
  615. &benchedSize /* ? */, 1 /* ? */,
  616. cLevel, compressionParams,
  617. NULL, 0, /* dictionary */
  618. displayLevel, name, adv);
  619. /* clean up */
  620. free(srcBuffer);
  621. return res;
  622. }
  623. static size_t BMK_findMaxMem(U64 requiredMem)
  624. {
  625. size_t const step = 64 MB;
  626. BYTE* testmem = NULL;
  627. requiredMem = (((requiredMem >> 26) + 1) << 26);
  628. requiredMem += step;
  629. if (requiredMem > maxMemory) requiredMem = maxMemory;
  630. do {
  631. testmem = (BYTE*)malloc((size_t)requiredMem);
  632. requiredMem -= step;
  633. } while (!testmem && requiredMem > 0);
  634. free(testmem);
  635. return (size_t)(requiredMem);
  636. }
  637. /*! BMK_loadFiles() :
  638. * Loads `buffer` with content of files listed within `fileNamesTable`.
  639. * At most, fills `buffer` entirely. */
  640. static int BMK_loadFiles(void* buffer, size_t bufferSize,
  641. size_t* fileSizes,
  642. const char* const * fileNamesTable, unsigned nbFiles,
  643. int displayLevel)
  644. {
  645. size_t pos = 0, totalSize = 0;
  646. unsigned n;
  647. for (n=0; n<nbFiles; n++) {
  648. U64 fileSize = UTIL_getFileSize(fileNamesTable[n]); /* last file may be shortened */
  649. if (UTIL_isDirectory(fileNamesTable[n])) {
  650. DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]);
  651. fileSizes[n] = 0;
  652. continue;
  653. }
  654. if (fileSize == UTIL_FILESIZE_UNKNOWN) {
  655. DISPLAYLEVEL(2, "Cannot evaluate size of %s, ignoring ... \n", fileNamesTable[n]);
  656. fileSizes[n] = 0;
  657. continue;
  658. }
  659. { FILE* const f = fopen(fileNamesTable[n], "rb");
  660. if (f==NULL) RETURN_ERROR_INT(10, "impossible to open file %s", fileNamesTable[n]);
  661. OUTPUTLEVEL(2, "Loading %s... \r", fileNamesTable[n]);
  662. if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */
  663. { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
  664. if (readSize != (size_t)fileSize) RETURN_ERROR_INT(11, "could not read %s", fileNamesTable[n]);
  665. pos += readSize;
  666. }
  667. fileSizes[n] = (size_t)fileSize;
  668. totalSize += (size_t)fileSize;
  669. fclose(f);
  670. } }
  671. if (totalSize == 0) RETURN_ERROR_INT(12, "no data to bench");
  672. return 0;
  673. }
  674. BMK_benchOutcome_t BMK_benchFilesAdvanced(
  675. const char* const * fileNamesTable, unsigned nbFiles,
  676. const char* dictFileName, int cLevel,
  677. const ZSTD_compressionParameters* compressionParams,
  678. int displayLevel, const BMK_advancedParams_t* adv)
  679. {
  680. void* srcBuffer = NULL;
  681. size_t benchedSize;
  682. void* dictBuffer = NULL;
  683. size_t dictBufferSize = 0;
  684. size_t* fileSizes = NULL;
  685. BMK_benchOutcome_t res;
  686. U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
  687. if (!nbFiles) {
  688. RETURN_ERROR(14, BMK_benchOutcome_t, "No Files to Benchmark");
  689. }
  690. if (cLevel > ZSTD_maxCLevel()) {
  691. RETURN_ERROR(15, BMK_benchOutcome_t, "Invalid Compression Level");
  692. }
  693. if (totalSizeToLoad == UTIL_FILESIZE_UNKNOWN) {
  694. RETURN_ERROR(9, BMK_benchOutcome_t, "Error loading files");
  695. }
  696. fileSizes = (size_t*)calloc(nbFiles, sizeof(size_t));
  697. if (!fileSizes) RETURN_ERROR(12, BMK_benchOutcome_t, "not enough memory for fileSizes");
  698. /* Load dictionary */
  699. if (dictFileName != NULL) {
  700. U64 const dictFileSize = UTIL_getFileSize(dictFileName);
  701. if (dictFileSize == UTIL_FILESIZE_UNKNOWN) {
  702. DISPLAYLEVEL(1, "error loading %s : %s \n", dictFileName, strerror(errno));
  703. free(fileSizes);
  704. RETURN_ERROR(9, BMK_benchOutcome_t, "benchmark aborted");
  705. }
  706. if (dictFileSize > 64 MB) {
  707. free(fileSizes);
  708. RETURN_ERROR(10, BMK_benchOutcome_t, "dictionary file %s too large", dictFileName);
  709. }
  710. dictBufferSize = (size_t)dictFileSize;
  711. dictBuffer = malloc(dictBufferSize);
  712. if (dictBuffer==NULL) {
  713. free(fileSizes);
  714. RETURN_ERROR(11, BMK_benchOutcome_t, "not enough memory for dictionary (%u bytes)",
  715. (unsigned)dictBufferSize);
  716. }
  717. { int const errorCode = BMK_loadFiles(dictBuffer, dictBufferSize,
  718. fileSizes, &dictFileName /*?*/,
  719. 1 /*?*/, displayLevel);
  720. if (errorCode) {
  721. res = BMK_benchOutcome_error();
  722. goto _cleanUp;
  723. } }
  724. }
  725. /* Memory allocation & restrictions */
  726. benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
  727. if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
  728. if (benchedSize < totalSizeToLoad)
  729. DISPLAY("Not enough memory; testing %u MB only...\n", (unsigned)(benchedSize >> 20));
  730. srcBuffer = benchedSize ? malloc(benchedSize) : NULL;
  731. if (!srcBuffer) {
  732. free(dictBuffer);
  733. free(fileSizes);
  734. RETURN_ERROR(12, BMK_benchOutcome_t, "not enough memory");
  735. }
  736. /* Load input buffer */
  737. { int const errorCode = BMK_loadFiles(srcBuffer, benchedSize,
  738. fileSizes, fileNamesTable, nbFiles,
  739. displayLevel);
  740. if (errorCode) {
  741. res = BMK_benchOutcome_error();
  742. goto _cleanUp;
  743. } }
  744. /* Bench */
  745. { char mfName[20] = {0};
  746. snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
  747. { const char* const displayName = (nbFiles > 1) ? mfName : fileNamesTable[0];
  748. res = BMK_benchCLevel(srcBuffer, benchedSize,
  749. fileSizes, nbFiles,
  750. cLevel, compressionParams,
  751. dictBuffer, dictBufferSize,
  752. displayLevel, displayName,
  753. adv);
  754. } }
  755. _cleanUp:
  756. free(srcBuffer);
  757. free(dictBuffer);
  758. free(fileSizes);
  759. return res;
  760. }
  761. BMK_benchOutcome_t BMK_benchFiles(
  762. const char* const * fileNamesTable, unsigned nbFiles,
  763. const char* dictFileName,
  764. int cLevel, const ZSTD_compressionParameters* compressionParams,
  765. int displayLevel)
  766. {
  767. BMK_advancedParams_t const adv = BMK_initAdvancedParams();
  768. return BMK_benchFilesAdvanced(fileNamesTable, nbFiles, dictFileName, cLevel, compressionParams, displayLevel, &adv);
  769. }