bench.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. bench.c - Demo program to benchmark open-source compression algorithms
  3. Copyright (C) Yann Collet 2012-2020
  4. GPL v2 License
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. You can contact the author at :
  17. - LZ4 homepage : http://www.lz4.org
  18. - LZ4 source repository : https://github.com/lz4/lz4
  19. */
  20. /*-************************************
  21. * Compiler options
  22. **************************************/
  23. #ifdef _MSC_VER /* Visual Studio */
  24. # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  25. #endif
  26. /* *************************************
  27. * Includes
  28. ***************************************/
  29. #include "platform.h" /* Compiler options */
  30. #include "util.h" /* UTIL_GetFileSize, UTIL_sleep */
  31. #include <stdlib.h> /* malloc, free */
  32. #include <string.h> /* memset */
  33. #include <stdio.h> /* fprintf, fopen, ftello */
  34. #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
  35. #include <assert.h> /* assert */
  36. #include "datagen.h" /* RDG_genBuffer */
  37. #include "xxhash.h"
  38. #include "bench.h"
  39. #define LZ4_STATIC_LINKING_ONLY
  40. #include "lz4.h"
  41. #define LZ4_HC_STATIC_LINKING_ONLY
  42. #include "lz4hc.h"
  43. #include "lz4frame.h" /* LZ4F_decompress */
  44. /* *************************************
  45. * Constants
  46. ***************************************/
  47. #ifndef LZ4_GIT_COMMIT_STRING
  48. # define LZ4_GIT_COMMIT_STRING ""
  49. #else
  50. # define LZ4_GIT_COMMIT_STRING LZ4_EXPAND_AND_QUOTE(LZ4_GIT_COMMIT)
  51. #endif
  52. #define NBSECONDS 3
  53. #define TIMELOOP_MICROSEC 1*1000000ULL /* 1 second */
  54. #define TIMELOOP_NANOSEC 1*1000000000ULL /* 1 second */
  55. #define ACTIVEPERIOD_MICROSEC 70*1000000ULL /* 70 seconds */
  56. #define COOLPERIOD_SEC 10
  57. #define DECOMP_MULT 1 /* test decompression DECOMP_MULT times longer than compression */
  58. #define KB *(1 <<10)
  59. #define MB *(1 <<20)
  60. #define GB *(1U<<30)
  61. #define LZ4_MAX_DICT_SIZE (64 KB)
  62. static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31));
  63. static U32 g_compressibilityDefault = 50;
  64. /* *************************************
  65. * console display
  66. ***************************************/
  67. #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
  68. #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
  69. static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
  70. #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
  71. if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \
  72. { g_time = clock(); DISPLAY(__VA_ARGS__); \
  73. if (g_displayLevel>=4) fflush(stdout); } }
  74. static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100;
  75. static clock_t g_time = 0;
  76. /* *************************************
  77. * DEBUG and error conditions
  78. ***************************************/
  79. #ifndef DEBUG
  80. # define DEBUG 0
  81. #endif
  82. #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
  83. #define END_PROCESS(error, ...) \
  84. { \
  85. DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
  86. DISPLAYLEVEL(1, "Error %i : ", error); \
  87. DISPLAYLEVEL(1, __VA_ARGS__); \
  88. DISPLAYLEVEL(1, "\n"); \
  89. exit(error); \
  90. }
  91. #define LZ4_isError(errcode) (errcode==0)
  92. /* *************************************
  93. * Benchmark Parameters
  94. ***************************************/
  95. static U32 g_nbSeconds = NBSECONDS;
  96. static size_t g_blockSize = 0;
  97. int g_additionalParam = 0;
  98. int g_benchSeparately = 0;
  99. int g_decodeOnly = 0;
  100. unsigned g_skipChecksums = 0;
  101. void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; }
  102. void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; }
  103. void BMK_setNbSeconds(unsigned nbSeconds)
  104. {
  105. g_nbSeconds = nbSeconds;
  106. DISPLAYLEVEL(3, "- test >= %u seconds per compression / decompression -\n", g_nbSeconds);
  107. }
  108. void BMK_setBlockSize(size_t blockSize) { g_blockSize = blockSize; }
  109. void BMK_setBenchSeparately(int separate) { g_benchSeparately = (separate!=0); }
  110. void BMK_setDecodeOnlyMode(int set) { g_decodeOnly = (set!=0); }
  111. void BMK_skipChecksums(int skip) { g_skipChecksums = (skip!=0); }
  112. /* *************************************
  113. * Compression state management
  114. ***************************************/
  115. struct compressionParameters
  116. {
  117. int cLevel;
  118. const char* dictBuf;
  119. int dictSize;
  120. LZ4_stream_t* LZ4_stream;
  121. LZ4_stream_t* LZ4_dictStream;
  122. LZ4_streamHC_t* LZ4_streamHC;
  123. LZ4_streamHC_t* LZ4_dictStreamHC;
  124. void (*initFunction)(
  125. struct compressionParameters* pThis);
  126. void (*resetFunction)(
  127. const struct compressionParameters* pThis);
  128. int (*blockFunction)(
  129. const struct compressionParameters* pThis,
  130. const char* src, char* dst, int srcSize, int dstSize);
  131. void (*cleanupFunction)(
  132. const struct compressionParameters* pThis);
  133. };
  134. static void
  135. LZ4_compressInitNoStream(struct compressionParameters* pThis)
  136. {
  137. pThis->LZ4_stream = NULL;
  138. pThis->LZ4_dictStream = NULL;
  139. pThis->LZ4_streamHC = NULL;
  140. pThis->LZ4_dictStreamHC = NULL;
  141. }
  142. static void
  143. LZ4_compressInitStream(struct compressionParameters* pThis)
  144. {
  145. pThis->LZ4_stream = LZ4_createStream();
  146. pThis->LZ4_dictStream = LZ4_createStream();
  147. pThis->LZ4_streamHC = NULL;
  148. pThis->LZ4_dictStreamHC = NULL;
  149. LZ4_loadDict(pThis->LZ4_dictStream, pThis->dictBuf, pThis->dictSize);
  150. }
  151. static void
  152. LZ4_compressInitStreamHC(struct compressionParameters* pThis)
  153. {
  154. pThis->LZ4_stream = NULL;
  155. pThis->LZ4_dictStream = NULL;
  156. pThis->LZ4_streamHC = LZ4_createStreamHC();
  157. pThis->LZ4_dictStreamHC = LZ4_createStreamHC();
  158. LZ4_loadDictHC(pThis->LZ4_dictStreamHC, pThis->dictBuf, pThis->dictSize);
  159. }
  160. static void
  161. LZ4_compressResetNoStream(const struct compressionParameters* pThis)
  162. {
  163. (void)pThis;
  164. }
  165. static void
  166. LZ4_compressResetStream(const struct compressionParameters* pThis)
  167. {
  168. LZ4_resetStream_fast(pThis->LZ4_stream);
  169. LZ4_attach_dictionary(pThis->LZ4_stream, pThis->LZ4_dictStream);
  170. }
  171. static void
  172. LZ4_compressResetStreamHC(const struct compressionParameters* pThis)
  173. {
  174. LZ4_resetStreamHC_fast(pThis->LZ4_streamHC, pThis->cLevel);
  175. LZ4_attach_HC_dictionary(pThis->LZ4_streamHC, pThis->LZ4_dictStreamHC);
  176. }
  177. static int
  178. LZ4_compressBlockNoStream(const struct compressionParameters* pThis,
  179. const char* src, char* dst,
  180. int srcSize, int dstSize)
  181. {
  182. int const acceleration = (pThis->cLevel < 0) ? -pThis->cLevel + 1 : 1;
  183. return LZ4_compress_fast(src, dst, srcSize, dstSize, acceleration);
  184. }
  185. static int
  186. LZ4_compressBlockNoStreamHC(const struct compressionParameters* pThis,
  187. const char* src, char* dst,
  188. int srcSize, int dstSize)
  189. {
  190. return LZ4_compress_HC(src, dst, srcSize, dstSize, pThis->cLevel);
  191. }
  192. static int
  193. LZ4_compressBlockStream(const struct compressionParameters* pThis,
  194. const char* src, char* dst,
  195. int srcSize, int dstSize)
  196. {
  197. int const acceleration = (pThis->cLevel < 0) ? -pThis->cLevel + 1 : 1;
  198. return LZ4_compress_fast_continue(pThis->LZ4_stream, src, dst, srcSize, dstSize, acceleration);
  199. }
  200. static int
  201. LZ4_compressBlockStreamHC(const struct compressionParameters* pThis,
  202. const char* src, char* dst,
  203. int srcSize, int dstSize)
  204. {
  205. return LZ4_compress_HC_continue(pThis->LZ4_streamHC, src, dst, srcSize, dstSize);
  206. }
  207. static void
  208. LZ4_compressCleanupNoStream(const struct compressionParameters* pThis)
  209. {
  210. (void)pThis;
  211. }
  212. static void
  213. LZ4_compressCleanupStream(const struct compressionParameters* pThis)
  214. {
  215. LZ4_freeStream(pThis->LZ4_stream);
  216. LZ4_freeStream(pThis->LZ4_dictStream);
  217. }
  218. static void
  219. LZ4_compressCleanupStreamHC(const struct compressionParameters* pThis)
  220. {
  221. LZ4_freeStreamHC(pThis->LZ4_streamHC);
  222. LZ4_freeStreamHC(pThis->LZ4_dictStreamHC);
  223. }
  224. static void
  225. LZ4_buildCompressionParameters(struct compressionParameters* pParams,
  226. int cLevel,
  227. const char* dictBuf, int dictSize)
  228. {
  229. pParams->cLevel = cLevel;
  230. pParams->dictBuf = dictBuf;
  231. pParams->dictSize = dictSize;
  232. if (dictSize) {
  233. if (cLevel < LZ4HC_CLEVEL_MIN) {
  234. pParams->initFunction = LZ4_compressInitStream;
  235. pParams->resetFunction = LZ4_compressResetStream;
  236. pParams->blockFunction = LZ4_compressBlockStream;
  237. pParams->cleanupFunction = LZ4_compressCleanupStream;
  238. } else {
  239. pParams->initFunction = LZ4_compressInitStreamHC;
  240. pParams->resetFunction = LZ4_compressResetStreamHC;
  241. pParams->blockFunction = LZ4_compressBlockStreamHC;
  242. pParams->cleanupFunction = LZ4_compressCleanupStreamHC;
  243. }
  244. } else {
  245. pParams->initFunction = LZ4_compressInitNoStream;
  246. pParams->resetFunction = LZ4_compressResetNoStream;
  247. pParams->cleanupFunction = LZ4_compressCleanupNoStream;
  248. if (cLevel < LZ4HC_CLEVEL_MIN) {
  249. pParams->blockFunction = LZ4_compressBlockNoStream;
  250. } else {
  251. pParams->blockFunction = LZ4_compressBlockNoStreamHC;
  252. }
  253. }
  254. }
  255. typedef int (*DecFunction_f)(const char* src, char* dst,
  256. int srcSize, int dstCapacity,
  257. const char* dictStart, int dictSize);
  258. static LZ4F_dctx* g_dctx = NULL;
  259. static int
  260. LZ4F_decompress_binding(const char* src, char* dst,
  261. int srcSize, int dstCapacity,
  262. const char* dictStart, int dictSize)
  263. {
  264. size_t dstSize = (size_t)dstCapacity;
  265. size_t readSize = (size_t)srcSize;
  266. LZ4F_decompressOptions_t dOpt = { 1, 0, 0, 0 };
  267. size_t decStatus;
  268. dOpt.skipChecksums = g_skipChecksums;
  269. decStatus = LZ4F_decompress(g_dctx,
  270. dst, &dstSize,
  271. src, &readSize,
  272. &dOpt);
  273. if ( (decStatus == 0) /* decompression successful */
  274. && ((int)readSize==srcSize) /* consume all input */ )
  275. return (int)dstSize;
  276. /* else, error */
  277. return -1;
  278. (void)dictStart; (void)dictSize; /* not compatible with dictionary yet */
  279. }
  280. /* ********************************************************
  281. * Bench functions
  282. **********************************************************/
  283. typedef struct {
  284. const char* srcPtr;
  285. size_t srcSize;
  286. char* cPtr;
  287. size_t cRoom;
  288. size_t cSize;
  289. char* resPtr;
  290. size_t resSize;
  291. } blockParam_t;
  292. #define MIN(a,b) ((a)<(b) ? (a) : (b))
  293. #define MAX(a,b) ((a)>(b) ? (a) : (b))
  294. static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
  295. const char* displayName, int cLevel,
  296. const size_t* fileSizes, U32 nbFiles,
  297. const char* dictBuf, int dictSize)
  298. {
  299. size_t const blockSize = (g_blockSize>=32 && !g_decodeOnly ? g_blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ;
  300. U32 const maxNbBlocks = (U32)((srcSize + (blockSize-1)) / blockSize) + nbFiles;
  301. blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t));
  302. size_t const maxCompressedSize = (size_t)LZ4_compressBound((int)srcSize) + (maxNbBlocks * 1024); /* add some room for safety */
  303. void* const compressedBuffer = malloc(maxCompressedSize);
  304. size_t const decMultiplier = g_decodeOnly ? 255 : 1;
  305. size_t const maxInSize = (size_t)LZ4_MAX_INPUT_SIZE / decMultiplier;
  306. size_t const maxDecSize = srcSize < maxInSize ? srcSize * decMultiplier : LZ4_MAX_INPUT_SIZE;
  307. void* const resultBuffer = malloc(maxDecSize);
  308. U32 nbBlocks;
  309. struct compressionParameters compP;
  310. /* checks */
  311. if (!compressedBuffer || !resultBuffer || !blockTable)
  312. END_PROCESS(31, "allocation error : not enough memory");
  313. if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */
  314. /* init */
  315. LZ4_buildCompressionParameters(&compP, cLevel, dictBuf, dictSize);
  316. compP.initFunction(&compP);
  317. if (g_dctx==NULL) {
  318. LZ4F_createDecompressionContext(&g_dctx, LZ4F_VERSION);
  319. if (g_dctx==NULL)
  320. END_PROCESS(1, "allocation error - decompression state");
  321. }
  322. /* Init blockTable data */
  323. { const char* srcPtr = (const char*)srcBuffer;
  324. char* cPtr = (char*)compressedBuffer;
  325. char* resPtr = (char*)resultBuffer;
  326. U32 fileNb;
  327. for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
  328. size_t remaining = fileSizes[fileNb];
  329. U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize);
  330. U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
  331. for ( ; nbBlocks<blockEnd; nbBlocks++) {
  332. size_t const thisBlockSize = MIN(remaining, blockSize);
  333. size_t const resMaxSize = thisBlockSize * decMultiplier;
  334. size_t const resCapa = (thisBlockSize < maxInSize) ? resMaxSize : LZ4_MAX_INPUT_SIZE;
  335. blockTable[nbBlocks].srcPtr = srcPtr;
  336. blockTable[nbBlocks].cPtr = cPtr;
  337. blockTable[nbBlocks].resPtr = resPtr;
  338. blockTable[nbBlocks].srcSize = thisBlockSize;
  339. blockTable[nbBlocks].cRoom = (size_t)LZ4_compressBound((int)thisBlockSize);
  340. srcPtr += thisBlockSize;
  341. cPtr += blockTable[nbBlocks].cRoom;
  342. resPtr += resCapa;
  343. remaining -= thisBlockSize;
  344. } } }
  345. /* warming up memory */
  346. RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
  347. /* decode-only mode : copy input to @compressedBuffer */
  348. if (g_decodeOnly) {
  349. U32 blockNb;
  350. for (blockNb=0; blockNb < nbBlocks; blockNb++) {
  351. memcpy(blockTable[blockNb].cPtr, blockTable[blockNb].srcPtr, blockTable[blockNb].srcSize);
  352. blockTable[blockNb].cSize = blockTable[blockNb].srcSize;
  353. } }
  354. /* Bench */
  355. { U64 fastestC = (U64)(-1LL), fastestD = (U64)(-1LL);
  356. U64 const crcOrig = XXH64(srcBuffer, srcSize, 0);
  357. UTIL_time_t coolTime = UTIL_getTime();
  358. U64 const maxTime = (g_nbSeconds * TIMELOOP_NANOSEC) + 100;
  359. U32 nbCompressionLoops = (U32)((5 MB) / (srcSize+1)) + 1; /* conservative initial compression speed estimate */
  360. U32 nbDecodeLoops = (U32)((200 MB) / (srcSize+1)) + 1; /* conservative initial decode speed estimate */
  361. U64 totalCTime=0, totalDTime=0;
  362. U32 cCompleted=(g_decodeOnly==1), dCompleted=0;
  363. # define NB_MARKS 4
  364. const char* const marks[NB_MARKS] = { " |", " /", " =", "\\" };
  365. U32 markNb = 0;
  366. size_t cSize = srcSize;
  367. size_t totalRSize = srcSize;
  368. double ratio = 0.;
  369. DISPLAYLEVEL(2, "\r%79s\r", "");
  370. while (!cCompleted || !dCompleted) {
  371. /* overheat protection */
  372. if (UTIL_clockSpanMicro(coolTime) > ACTIVEPERIOD_MICROSEC) {
  373. DISPLAYLEVEL(2, "\rcooling down ... \r");
  374. UTIL_sleep(COOLPERIOD_SEC);
  375. coolTime = UTIL_getTime();
  376. }
  377. /* Compression */
  378. DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (U32)totalRSize);
  379. if (!cCompleted) memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase compressed buffer */
  380. UTIL_sleepMilli(1); /* give processor time to other processes */
  381. UTIL_waitForNextTick();
  382. if (!cCompleted) { /* still some time to do compression tests */
  383. UTIL_time_t const clockStart = UTIL_getTime();
  384. U32 nbLoops;
  385. for (nbLoops=0; nbLoops < nbCompressionLoops; nbLoops++) {
  386. U32 blockNb;
  387. compP.resetFunction(&compP);
  388. for (blockNb=0; blockNb<nbBlocks; blockNb++) {
  389. size_t const rSize = (size_t)compP.blockFunction(
  390. &compP,
  391. blockTable[blockNb].srcPtr, blockTable[blockNb].cPtr,
  392. (int)blockTable[blockNb].srcSize, (int)blockTable[blockNb].cRoom);
  393. if (LZ4_isError(rSize)) END_PROCESS(1, "LZ4 compression failed");
  394. blockTable[blockNb].cSize = rSize;
  395. } }
  396. { U64 const clockSpan = UTIL_clockSpanNano(clockStart);
  397. if (clockSpan > 0) {
  398. if (clockSpan < fastestC * nbCompressionLoops)
  399. fastestC = clockSpan / nbCompressionLoops;
  400. assert(fastestC > 0);
  401. nbCompressionLoops = (U32)(TIMELOOP_NANOSEC / fastestC) + 1; /* aim for ~1sec */
  402. } else {
  403. assert(nbCompressionLoops < 40000000); /* avoid overflow */
  404. nbCompressionLoops *= 100;
  405. }
  406. totalCTime += clockSpan;
  407. cCompleted = totalCTime>maxTime;
  408. }
  409. cSize = 0;
  410. { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; }
  411. cSize += !cSize; /* avoid div by 0 */
  412. ratio = (double)totalRSize / (double)cSize;
  413. markNb = (markNb+1) % NB_MARKS;
  414. DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r",
  415. marks[markNb], displayName,
  416. (U32)totalRSize, (U32)cSize, ratio,
  417. ((double)totalRSize / fastestC) * 1000 );
  418. }
  419. (void)fastestD; (void)crcOrig; /* unused when decompression disabled */
  420. #if 1
  421. /* Decompression */
  422. if (!dCompleted) memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */
  423. UTIL_sleepMilli(5); /* give processor time to other processes */
  424. UTIL_waitForNextTick();
  425. if (!dCompleted) {
  426. const DecFunction_f decFunction = g_decodeOnly ?
  427. LZ4F_decompress_binding : LZ4_decompress_safe_usingDict;
  428. const char* const decString = g_decodeOnly ?
  429. "LZ4F_decompress" : "LZ4_decompress_safe_usingDict";
  430. UTIL_time_t const clockStart = UTIL_getTime();
  431. U32 nbLoops;
  432. for (nbLoops=0; nbLoops < nbDecodeLoops; nbLoops++) {
  433. U32 blockNb;
  434. for (blockNb=0; blockNb<nbBlocks; blockNb++) {
  435. size_t const inMaxSize = (size_t)INT_MAX / decMultiplier;
  436. size_t const resCapa = (blockTable[blockNb].srcSize < inMaxSize) ?
  437. blockTable[blockNb].srcSize * decMultiplier :
  438. INT_MAX;
  439. int const regenSize = decFunction(
  440. blockTable[blockNb].cPtr, blockTable[blockNb].resPtr,
  441. (int)blockTable[blockNb].cSize, (int)resCapa,
  442. dictBuf, dictSize);
  443. if (regenSize < 0) {
  444. DISPLAY("%s() failed on block %u of size %u \n",
  445. decString, blockNb, (unsigned)blockTable[blockNb].srcSize);
  446. if (g_decodeOnly)
  447. DISPLAY("Is input using LZ4 Frame format ? \n");
  448. END_PROCESS(2, "error during decoding");
  449. break;
  450. }
  451. blockTable[blockNb].resSize = (size_t)regenSize;
  452. } }
  453. { U64 const clockSpan = UTIL_clockSpanNano(clockStart);
  454. if (clockSpan > 0) {
  455. if (clockSpan < fastestD * nbDecodeLoops)
  456. fastestD = clockSpan / nbDecodeLoops;
  457. assert(fastestD > 0);
  458. nbDecodeLoops = (U32)(TIMELOOP_NANOSEC / fastestD) + 1; /* aim for ~1sec */
  459. } else {
  460. assert(nbDecodeLoops < 40000000); /* avoid overflow */
  461. nbDecodeLoops *= 100;
  462. }
  463. totalDTime += clockSpan;
  464. dCompleted = totalDTime > (DECOMP_MULT*maxTime);
  465. } }
  466. if (g_decodeOnly) {
  467. unsigned u;
  468. totalRSize = 0;
  469. for (u=0; u<nbBlocks; u++) totalRSize += blockTable[u].resSize;
  470. }
  471. markNb = (markNb+1) % NB_MARKS;
  472. ratio = (double)totalRSize / (double)cSize;
  473. DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r",
  474. marks[markNb], displayName,
  475. (U32)totalRSize, (U32)cSize, ratio,
  476. ((double)totalRSize / fastestC) * 1000,
  477. ((double)totalRSize / fastestD) * 1000);
  478. /* CRC Checking (not possible in decode-only mode)*/
  479. if (!g_decodeOnly) {
  480. U64 const crcCheck = XXH64(resultBuffer, srcSize, 0);
  481. if (crcOrig!=crcCheck) {
  482. size_t u;
  483. DISPLAY("\n!!! WARNING !!! %17s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck);
  484. for (u=0; u<srcSize; u++) {
  485. if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) {
  486. U32 segNb, bNb, pos;
  487. size_t bacc = 0;
  488. DISPLAY("Decoding error at pos %u ", (U32)u);
  489. for (segNb = 0; segNb < nbBlocks; segNb++) {
  490. if (bacc + blockTable[segNb].srcSize > u) break;
  491. bacc += blockTable[segNb].srcSize;
  492. }
  493. pos = (U32)(u - bacc);
  494. bNb = pos / (128 KB);
  495. DISPLAY("(block %u, sub %u, pos %u) \n", segNb, bNb, pos);
  496. break;
  497. }
  498. if (u==srcSize-1) { /* should never happen */
  499. DISPLAY("no difference detected\n");
  500. } }
  501. break;
  502. } } /* CRC Checking */
  503. #endif
  504. } /* for (testNb = 1; testNb <= (g_nbSeconds + !g_nbSeconds); testNb++) */
  505. if (g_displayLevel == 1) {
  506. double const cSpeed = ((double)srcSize / fastestC) * 1000;
  507. double const dSpeed = ((double)srcSize / fastestD) * 1000;
  508. if (g_additionalParam)
  509. 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);
  510. else
  511. DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName);
  512. }
  513. DISPLAYLEVEL(2, "%2i#\n", cLevel);
  514. } /* Bench */
  515. /* clean up */
  516. compP.cleanupFunction(&compP);
  517. free(blockTable);
  518. free(compressedBuffer);
  519. free(resultBuffer);
  520. return 0;
  521. }
  522. static size_t BMK_findMaxMem(U64 requiredMem)
  523. {
  524. size_t step = 64 MB;
  525. BYTE* testmem=NULL;
  526. requiredMem = (((requiredMem >> 26) + 1) << 26);
  527. requiredMem += 2*step;
  528. if (requiredMem > maxMemory) requiredMem = maxMemory;
  529. while (!testmem) {
  530. if (requiredMem > step) requiredMem -= step;
  531. else requiredMem >>= 1;
  532. testmem = (BYTE*) malloc ((size_t)requiredMem);
  533. }
  534. free (testmem);
  535. /* keep some space available */
  536. if (requiredMem > step) requiredMem -= step;
  537. else requiredMem >>= 1;
  538. return (size_t)requiredMem;
  539. }
  540. static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize,
  541. const char* displayName, int cLevel, int cLevelLast,
  542. const size_t* fileSizes, unsigned nbFiles,
  543. const char* dictBuf, int dictSize)
  544. {
  545. int l;
  546. const char* pch = strrchr(displayName, '\\'); /* Windows */
  547. if (!pch) pch = strrchr(displayName, '/'); /* Linux */
  548. if (pch) displayName = pch+1;
  549. SET_REALTIME_PRIORITY;
  550. if (g_displayLevel == 1 && !g_additionalParam)
  551. DISPLAY("bench %s %s: input %u bytes, %u seconds, %u KB blocks\n", LZ4_VERSION_STRING, LZ4_GIT_COMMIT_STRING, (U32)benchedSize, g_nbSeconds, (U32)(g_blockSize>>10));
  552. if (cLevelLast < cLevel) cLevelLast = cLevel;
  553. for (l=cLevel; l <= cLevelLast; l++) {
  554. BMK_benchMem(srcBuffer, benchedSize,
  555. displayName, l,
  556. fileSizes, nbFiles,
  557. dictBuf, dictSize);
  558. }
  559. }
  560. /*! BMK_loadFiles() :
  561. Loads `buffer` with content of files listed within `fileNamesTable`.
  562. At most, fills `buffer` entirely */
  563. static void BMK_loadFiles(void* buffer, size_t bufferSize,
  564. size_t* fileSizes,
  565. const char** fileNamesTable, unsigned nbFiles)
  566. {
  567. size_t pos = 0, totalSize = 0;
  568. unsigned n;
  569. for (n=0; n<nbFiles; n++) {
  570. FILE* f;
  571. U64 fileSize = UTIL_getFileSize(fileNamesTable[n]);
  572. if (UTIL_isDirectory(fileNamesTable[n])) {
  573. DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]);
  574. fileSizes[n] = 0;
  575. continue;
  576. }
  577. f = fopen(fileNamesTable[n], "rb");
  578. if (f==NULL) END_PROCESS(10, "impossible to open file %s", fileNamesTable[n]);
  579. DISPLAYUPDATE(2, "Loading %s... \r", fileNamesTable[n]);
  580. if (fileSize > bufferSize-pos) { /* buffer too small - stop after this file */
  581. fileSize = bufferSize-pos;
  582. nbFiles=n;
  583. }
  584. { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
  585. if (readSize != (size_t)fileSize) END_PROCESS(11, "could not read %s", fileNamesTable[n]);
  586. pos += readSize; }
  587. fileSizes[n] = (size_t)fileSize;
  588. totalSize += (size_t)fileSize;
  589. fclose(f);
  590. }
  591. if (totalSize == 0) END_PROCESS(12, "no data to bench");
  592. }
  593. static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles,
  594. int cLevel, int cLevelLast,
  595. const char* dictBuf, int dictSize)
  596. {
  597. void* srcBuffer;
  598. size_t benchedSize;
  599. size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t));
  600. U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
  601. char mfName[20] = {0};
  602. if (!fileSizes) END_PROCESS(12, "not enough memory for fileSizes");
  603. /* Memory allocation & restrictions */
  604. benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
  605. if (benchedSize==0) END_PROCESS(12, "not enough memory");
  606. if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
  607. if (benchedSize > LZ4_MAX_INPUT_SIZE) {
  608. benchedSize = LZ4_MAX_INPUT_SIZE;
  609. DISPLAY("File(s) bigger than LZ4's max input size; testing %u MB only...\n", (U32)(benchedSize >> 20));
  610. } else {
  611. if (benchedSize < totalSizeToLoad)
  612. DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20));
  613. }
  614. srcBuffer = malloc(benchedSize + !benchedSize); /* avoid alloc of zero */
  615. if (!srcBuffer) END_PROCESS(12, "not enough memory");
  616. /* Load input buffer */
  617. BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles);
  618. /* Bench */
  619. snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
  620. { const char* displayName = (nbFiles > 1) ? mfName : fileNamesTable[0];
  621. BMK_benchCLevel(srcBuffer, benchedSize,
  622. displayName, cLevel, cLevelLast,
  623. fileSizes, nbFiles,
  624. dictBuf, dictSize);
  625. }
  626. /* clean up */
  627. free(srcBuffer);
  628. free(fileSizes);
  629. }
  630. static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility,
  631. const char* dictBuf, int dictSize)
  632. {
  633. char name[20] = {0};
  634. size_t benchedSize = 10000000;
  635. void* const srcBuffer = malloc(benchedSize);
  636. /* Memory allocation */
  637. if (!srcBuffer) END_PROCESS(21, "not enough memory");
  638. /* Fill input buffer */
  639. RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
  640. /* Bench */
  641. snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
  642. BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, dictBuf, dictSize);
  643. /* clean up */
  644. free(srcBuffer);
  645. }
  646. static int
  647. BMK_benchFilesSeparately(const char** fileNamesTable, unsigned nbFiles,
  648. int cLevel, int cLevelLast,
  649. const char* dictBuf, int dictSize)
  650. {
  651. unsigned fileNb;
  652. if (cLevel > LZ4HC_CLEVEL_MAX) cLevel = LZ4HC_CLEVEL_MAX;
  653. if (cLevelLast > LZ4HC_CLEVEL_MAX) cLevelLast = LZ4HC_CLEVEL_MAX;
  654. if (cLevelLast < cLevel) cLevelLast = cLevel;
  655. for (fileNb=0; fileNb<nbFiles; fileNb++)
  656. BMK_benchFileTable(fileNamesTable+fileNb, 1, cLevel, cLevelLast, dictBuf, dictSize);
  657. return 0;
  658. }
  659. int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
  660. int cLevel, int cLevelLast,
  661. const char* dictFileName)
  662. {
  663. double const compressibility = (double)g_compressibilityDefault / 100;
  664. char* dictBuf = NULL;
  665. size_t dictSize = 0;
  666. if (cLevel > LZ4HC_CLEVEL_MAX) cLevel = LZ4HC_CLEVEL_MAX;
  667. if (g_decodeOnly) {
  668. DISPLAYLEVEL(2, "Benchmark Decompression of LZ4 Frame ");
  669. if (g_skipChecksums) {
  670. DISPLAYLEVEL(2, "_without_ checksum even when present \n");
  671. } else {
  672. DISPLAYLEVEL(2, "+ Checksum when present \n");
  673. }
  674. cLevelLast = cLevel;
  675. }
  676. if (cLevelLast > LZ4HC_CLEVEL_MAX) cLevelLast = LZ4HC_CLEVEL_MAX;
  677. if (cLevelLast < cLevel) cLevelLast = cLevel;
  678. if (cLevelLast > cLevel)
  679. DISPLAYLEVEL(2, "Benchmarking levels from %d to %d\n", cLevel, cLevelLast);
  680. if (dictFileName) {
  681. FILE* dictFile = NULL;
  682. U64 const dictFileSize = UTIL_getFileSize(dictFileName);
  683. if (!dictFileSize)
  684. END_PROCESS(25, "Dictionary error : could not stat dictionary file");
  685. if (g_decodeOnly)
  686. END_PROCESS(26, "Error : LZ4 Frame decoder mode not compatible with dictionary yet");
  687. dictFile = fopen(dictFileName, "rb");
  688. if (!dictFile)
  689. END_PROCESS(25, "Dictionary error : could not open dictionary file");
  690. if (dictFileSize > LZ4_MAX_DICT_SIZE) {
  691. dictSize = LZ4_MAX_DICT_SIZE;
  692. if (UTIL_fseek(dictFile, (long)(dictFileSize - dictSize), SEEK_SET))
  693. END_PROCESS(25, "Dictionary error : could not seek dictionary file");
  694. } else {
  695. dictSize = (size_t)dictFileSize;
  696. }
  697. dictBuf = (char*)malloc(dictSize);
  698. if (!dictBuf) END_PROCESS(25, "Allocation error : not enough memory");
  699. if (fread(dictBuf, 1, dictSize, dictFile) != dictSize)
  700. END_PROCESS(25, "Dictionary error : could not read dictionary file");
  701. fclose(dictFile);
  702. }
  703. if (nbFiles == 0)
  704. BMK_syntheticTest(cLevel, cLevelLast, compressibility, dictBuf, (int)dictSize);
  705. else {
  706. if (g_benchSeparately)
  707. BMK_benchFilesSeparately(fileNamesTable, nbFiles, cLevel, cLevelLast, dictBuf, (int)dictSize);
  708. else
  709. BMK_benchFileTable(fileNamesTable, nbFiles, cLevel, cLevelLast, dictBuf, (int)dictSize);
  710. }
  711. free(dictBuf);
  712. return 0;
  713. }