blob: 0007ef43fbef87f52803b324482d48ac7c0647fc [file] [log] [blame]
Yann Collet4856a002015-01-24 01:58:16 +01001/*
Yann Collet99909862016-04-09 16:17:18 +02002 bench.c - open-source compression benchmark module
3 Copyright (C) Yann Collet 2012-2016
Yann Collet4856a002015-01-24 01:58:16 +01004
5 GPL v2 License
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 You can contact the author at :
Yann Collet99909862016-04-09 16:17:18 +020022 - zstd homepage : http://www.zstd.net
Yann Collet4856a002015-01-24 01:58:16 +010023 - zstd source repository : https://github.com/Cyan4973/zstd
Yann Collet4856a002015-01-24 01:58:16 +010024*/
25
Yann Colletf3eca252015-10-22 15:31:46 +010026/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010027* Includes
Yann Colletf3eca252015-10-22 15:31:46 +010028***************************************/
inikep55d047a2016-04-28 16:50:13 +020029#include "util.h" /* UTIL_GetFileSize */
Yann Collet4856a002015-01-24 01:58:16 +010030#include <stdlib.h> /* malloc, free */
31#include <string.h> /* memset */
Yann Colleteeb8ba12015-10-22 16:55:40 +010032#include <stdio.h> /* fprintf, fopen, ftello64 */
inikep4c12f232016-03-29 14:52:13 +020033
Yann Colletf3eca252015-10-22 15:31:46 +010034#include "mem.h"
Yann Collet31683c02015-12-18 01:26:48 +010035#include "zstd_static.h"
inikep69fcd7c2016-04-28 12:23:33 +020036#include "datagen.h" /* RDG_genBuffer */
Yann Collet4856a002015-01-24 01:58:16 +010037#include "xxhash.h"
38
39
Yann Colletf3eca252015-10-22 15:31:46 +010040/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010041* Compiler specifics
Yann Colletf3eca252015-10-22 15:31:46 +010042***************************************/
Yann Collet99909862016-04-09 16:17:18 +020043#if defined(_MSC_VER)
inikep9c22e572016-05-05 11:53:42 +020044
Yann Collet99909862016-04-09 16:17:18 +020045#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L))
46 /* part of <stdio.h> */
47#else
48 extern int snprintf (char* s, size_t maxlen, const char* format, ...); /* not declared in <stdio.h> when C version < c99 */
Yann Colleted699e62015-12-16 02:37:24 +010049#endif
50
Yann Collet4856a002015-01-24 01:58:16 +010051
Yann Colletf3eca252015-10-22 15:31:46 +010052/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010053* Constants
Yann Colletf3eca252015-10-22 15:31:46 +010054***************************************/
inikep44af12d2016-03-14 15:59:04 +010055#ifndef ZSTD_VERSION
56# define ZSTD_VERSION ""
57#endif
58
inikep83c76b42016-04-28 13:16:01 +020059#define NBLOOPS 3
60#define TIMELOOP_MICROSEC 1*1000000ULL /* 1 second */
61#define ACTIVEPERIOD_MICROSEC 70*1000000ULL /* 70 seconds */
62#define COOLPERIOD_SEC 10
inikep9c22e572016-05-05 11:53:42 +020063#define MAX_LIST_SIZE (64*1024)
Yann Collet4856a002015-01-24 01:58:16 +010064
65#define KB *(1 <<10)
66#define MB *(1 <<20)
67#define GB *(1U<<30)
68
Yann Colletd062f132015-12-01 01:31:17 +010069static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31));
Yann Collet4856a002015-01-24 01:58:16 +010070
71static U32 g_compressibilityDefault = 50;
Yann Collet4856a002015-01-24 01:58:16 +010072
73
Yann Colletf3eca252015-10-22 15:31:46 +010074/* *************************************
Yann Colleted699e62015-12-16 02:37:24 +010075* console display
Yann Colletf3eca252015-10-22 15:31:46 +010076***************************************/
Yann Colleted699e62015-12-16 02:37:24 +010077#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
78#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
79static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
80
81
82/* *************************************
83* Exceptions
84***************************************/
85#ifndef DEBUG
86# define DEBUG 0
87#endif
88#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
89#define EXM_THROW(error, ...) \
90{ \
91 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
92 DISPLAYLEVEL(1, "Error %i : ", error); \
93 DISPLAYLEVEL(1, __VA_ARGS__); \
94 DISPLAYLEVEL(1, "\n"); \
95 exit(error); \
96}
Yann Collet4856a002015-01-24 01:58:16 +010097
98
Yann Colletf3eca252015-10-22 15:31:46 +010099/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100100* Benchmark Parameters
Yann Colletf3eca252015-10-22 15:31:46 +0100101***************************************/
Yann Collet1d1ae402016-03-17 19:51:02 +0100102static U32 g_nbIterations = NBLOOPS;
Yann Collet1c00dc32015-10-21 08:22:25 +0100103static size_t g_blockSize = 0;
inikepeaba91a2016-03-23 20:30:26 +0100104int g_additionalParam = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100105
inikep4e26bb62016-03-14 12:48:51 +0100106void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; }
107
inikepeaba91a2016-03-23 20:30:26 +0100108void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; }
109
Yann Collet1d1ae402016-03-17 19:51:02 +0100110void BMK_SetNbIterations(unsigned nbLoops)
Yann Collet4856a002015-01-24 01:58:16 +0100111{
Yann Collet1d1ae402016-03-17 19:51:02 +0100112 g_nbIterations = nbLoops;
inikep2872b6f2016-03-22 14:38:34 +0100113 DISPLAYLEVEL(2, "- %i iterations -\n", g_nbIterations);
Yann Collet4856a002015-01-24 01:58:16 +0100114}
115
Yann Collet1c00dc32015-10-21 08:22:25 +0100116void BMK_SetBlockSize(size_t blockSize)
117{
118 g_blockSize = blockSize;
inikep4e26bb62016-03-14 12:48:51 +0100119 DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10));
Yann Collet1c00dc32015-10-21 08:22:25 +0100120}
121
Yann Collet4856a002015-01-24 01:58:16 +0100122
Yann Colletf3eca252015-10-22 15:31:46 +0100123/* ********************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100124* Bench functions
Yann Colletf3eca252015-10-22 15:31:46 +0100125**********************************************************/
Yann Collet1c00dc32015-10-21 08:22:25 +0100126typedef struct
Yann Collet4856a002015-01-24 01:58:16 +0100127{
Yann Colleted699e62015-12-16 02:37:24 +0100128 const char* srcPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100129 size_t srcSize;
130 char* cPtr;
131 size_t cRoom;
132 size_t cSize;
133 char* resPtr;
134 size_t resSize;
135} blockParam_t;
136
inikep4e26bb62016-03-14 12:48:51 +0100137typedef struct
138{
inikepc034b732016-03-14 13:13:42 +0100139 double ratio;
inikep4e26bb62016-03-14 12:48:51 +0100140 size_t cSize;
inikepc034b732016-03-14 13:13:42 +0100141 double cSpeed;
142 double dSpeed;
inikep4e26bb62016-03-14 12:48:51 +0100143} benchResult_t;
144
Yann Collet99909862016-04-09 16:17:18 +0200145
Yann Colletbe2010e2015-10-31 12:57:14 +0100146#define MIN(a,b) ((a)<(b) ? (a) : (b))
Yann Collet2ce49232016-02-02 14:36:49 +0100147#define MAX(a,b) ((a)>(b) ? (a) : (b))
Yann Collet1c00dc32015-10-21 08:22:25 +0100148
Yann Colleted699e62015-12-16 02:37:24 +0100149static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
inikep472638c2016-03-23 12:28:28 +0100150 const char* displayName, int cLevel,
Yann Collet31683c02015-12-18 01:26:48 +0100151 const size_t* fileSizes, U32 nbFiles,
inikep4e26bb62016-03-14 12:48:51 +0100152 const void* dictBuffer, size_t dictBufferSize, benchResult_t *result)
Yann Collet1c00dc32015-10-21 08:22:25 +0100153{
inikep38654982016-04-21 12:18:47 +0200154 size_t const blockSize = (g_blockSize>=32 ? g_blockSize : srcSize) + (!srcSize); /* avoid div by 0 */
Yann Colletd64f4352016-03-21 00:07:42 +0100155 U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
Yann Colleted699e62015-12-16 02:37:24 +0100156 blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t));
Yann Colletb9151402016-03-26 17:18:11 +0100157 size_t const maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */
Yann Collet1c00dc32015-10-21 08:22:25 +0100158 void* const compressedBuffer = malloc(maxCompressedSize);
159 void* const resultBuffer = malloc(srcSize);
Yann Collet2630a5e2016-01-14 19:13:22 +0100160 ZSTD_CCtx* refCtx = ZSTD_createCCtx();
Yann Collet31683c02015-12-18 01:26:48 +0100161 ZSTD_CCtx* ctx = ZSTD_createCCtx();
Yann Collet7b51a292016-01-26 15:58:49 +0100162 ZSTD_DCtx* refDCtx = ZSTD_createDCtx();
Yann Collet31683c02015-12-18 01:26:48 +0100163 ZSTD_DCtx* dctx = ZSTD_createDCtx();
Yann Colletde406ee2016-03-20 15:46:10 +0100164 U32 nbBlocks;
inikep83c76b42016-04-28 13:16:01 +0200165 UTIL_time_t ticksPerSecond;
Yann Colletde406ee2016-03-20 15:46:10 +0100166
167 /* checks */
Yann Collet7b51a292016-01-26 15:58:49 +0100168 if (!compressedBuffer || !resultBuffer || !blockTable || !refCtx || !ctx || !refDCtx || !dctx)
Yann Colleted699e62015-12-16 02:37:24 +0100169 EXM_THROW(31, "not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100170
Yann Collet4856a002015-01-24 01:58:16 +0100171 /* init */
172 if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */
inikep83c76b42016-04-28 13:16:01 +0200173 UTIL_initTimer(ticksPerSecond);
inikep4c12f232016-03-29 14:52:13 +0200174
Yann Collet1c00dc32015-10-21 08:22:25 +0100175 /* Init blockTable data */
Yann Colletde406ee2016-03-20 15:46:10 +0100176 { const char* srcPtr = (const char*)srcBuffer;
Yann Collet1c00dc32015-10-21 08:22:25 +0100177 char* cPtr = (char*)compressedBuffer;
178 char* resPtr = (char*)resultBuffer;
Yann Collet699b14d2016-03-17 19:37:33 +0100179 U32 fileNb;
Yann Colletde406ee2016-03-20 15:46:10 +0100180 for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
Yann Collet70611352015-12-16 03:01:03 +0100181 size_t remaining = fileSizes[fileNb];
Yann Collet699b14d2016-03-17 19:37:33 +0100182 U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize);
183 U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
Yann Colletfd416f12016-01-30 03:14:15 +0100184 for ( ; nbBlocks<blockEnd; nbBlocks++) {
Yann Colletde406ee2016-03-20 15:46:10 +0100185 size_t const thisBlockSize = MIN(remaining, blockSize);
Yann Colleted699e62015-12-16 02:37:24 +0100186 blockTable[nbBlocks].srcPtr = srcPtr;
187 blockTable[nbBlocks].cPtr = cPtr;
188 blockTable[nbBlocks].resPtr = resPtr;
189 blockTable[nbBlocks].srcSize = thisBlockSize;
190 blockTable[nbBlocks].cRoom = ZSTD_compressBound(thisBlockSize);
191 srcPtr += thisBlockSize;
192 cPtr += blockTable[nbBlocks].cRoom;
193 resPtr += thisBlockSize;
194 remaining -= thisBlockSize;
Yann Colletfd416f12016-01-30 03:14:15 +0100195 } } }
Yann Collet1c00dc32015-10-21 08:22:25 +0100196
Yann Collet4856a002015-01-24 01:58:16 +0100197 /* warmimg up memory */
Yann Colletd062f132015-12-01 01:31:17 +0100198 RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
Yann Collet4856a002015-01-24 01:58:16 +0100199
200 /* Bench */
inikep6d157f12016-04-15 16:54:11 +0200201 { U64 fastestC = (U64)(-1LL), fastestD = (U64)(-1LL);
Yann Colletb9151402016-03-26 17:18:11 +0100202 U64 const crcOrig = XXH64(srcBuffer, srcSize, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100203 U64 crcCheck = 0;
inikep83c76b42016-04-28 13:16:01 +0200204 UTIL_time_t coolTime;
Yann Colletde406ee2016-03-20 15:46:10 +0100205 U32 testNb;
inikep19bd48f2016-04-04 12:10:00 +0200206 size_t cSize = 0;
207 double ratio = 0.;
Yann Collet4856a002015-01-24 01:58:16 +0100208
inikep83c76b42016-04-28 13:16:01 +0200209 UTIL_getTime(coolTime);
inikep4e26bb62016-03-14 12:48:51 +0100210 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Colletde406ee2016-03-20 15:46:10 +0100211 for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) {
inikep83c76b42016-04-28 13:16:01 +0200212 UTIL_time_t clockStart;
213 U64 clockLoop = g_nbIterations ? TIMELOOP_MICROSEC : 1;
Yann Collet4856a002015-01-24 01:58:16 +0100214
Yann Collet27d3dad2016-03-11 13:41:20 +0100215 /* overheat protection */
inikep83c76b42016-04-28 13:16:01 +0200216 if (UTIL_clockSpanMicro(coolTime, ticksPerSecond) > ACTIVEPERIOD_MICROSEC) {
Yann Collet27d3dad2016-03-11 13:41:20 +0100217 DISPLAY("\rcooling down ... \r");
inikep83c76b42016-04-28 13:16:01 +0200218 UTIL_sleep(COOLPERIOD_SEC);
219 UTIL_getTime(coolTime);
Yann Collet27d3dad2016-03-11 13:41:20 +0100220 }
Yann Collet4856a002015-01-24 01:58:16 +0100221
222 /* Compression */
inikep2872b6f2016-03-22 14:38:34 +0100223 DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->\r", testNb, displayName, (U32)srcSize);
Yann Collet699b14d2016-03-17 19:37:33 +0100224 memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100225
inikep83c76b42016-04-28 13:16:01 +0200226 UTIL_sleepMilli(1); /* give processor time to other processes */
227 UTIL_waitForNextTick(ticksPerSecond);
228 UTIL_getTime(clockStart);
Yann Colletde406ee2016-03-20 15:46:10 +0100229
inikepc5e1d292016-04-19 09:37:59 +0200230 { U32 nbLoops = 0;
231 do {
Yann Colleta5b66e32016-03-26 01:48:27 +0100232 U32 blockNb;
inikep4b3c5ee2016-04-14 13:43:51 +0200233 { ZSTD_parameters params;
234 params.cParams = ZSTD_getCParams(cLevel, blockSize, dictBufferSize);
235 params.fParams.contentSizeFlag = 1;
236 ZSTD_adjustCParams(&params.cParams, blockSize, dictBufferSize);
237 { size_t const initResult = ZSTD_compressBegin_advanced(refCtx, dictBuffer, dictBufferSize, params, blockSize);
238 if (ZSTD_isError(initResult)) break;
239 } }
Yann Colleta5b66e32016-03-26 01:48:27 +0100240 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
241 size_t const rSize = ZSTD_compress_usingPreparedCCtx(ctx, refCtx,
242 blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
243 blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize);
244 if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingPreparedCCtx() failed : %s", ZSTD_getErrorName(rSize));
245 blockTable[blockNb].cSize = rSize;
inikepc5e1d292016-04-19 09:37:59 +0200246 }
247 nbLoops++;
inikep83c76b42016-04-28 13:16:01 +0200248 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
249 { U64 const clockSpan = UTIL_clockSpanMicro(clockStart, ticksPerSecond);
inikep6d157f12016-04-15 16:54:11 +0200250 if (clockSpan < fastestC*nbLoops) fastestC = clockSpan / nbLoops;
Yann Colletde406ee2016-03-20 15:46:10 +0100251 } }
Yann Collet4856a002015-01-24 01:58:16 +0100252
Yann Collet1c00dc32015-10-21 08:22:25 +0100253 cSize = 0;
Yann Colletde406ee2016-03-20 15:46:10 +0100254 { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; }
Yann Collet2acb5d32015-10-29 16:49:43 +0100255 ratio = (double)srcSize / (double)cSize;
inikep2872b6f2016-03-22 14:38:34 +0100256 DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r",
Yann Colletde406ee2016-03-20 15:46:10 +0100257 testNb, displayName, (U32)srcSize, (U32)cSize, ratio,
inikep06f793a2016-03-29 11:17:58 +0200258 (double)srcSize / fastestC );
Yann Collet4856a002015-01-24 01:58:16 +0100259
Yann Colleta5b66e32016-03-26 01:48:27 +0100260 (void)fastestD; (void)crcOrig; /* unused when decompression disabled */
Yann Collete93d6ce2016-01-31 00:58:06 +0100261#if 1
Yann Collet4856a002015-01-24 01:58:16 +0100262 /* Decompression */
Yann Collet7b51a292016-01-26 15:58:49 +0100263 memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100264
inikep83c76b42016-04-28 13:16:01 +0200265 UTIL_sleepMilli(1); /* give processor time to other processes */
266 UTIL_waitForNextTick(ticksPerSecond);
267 UTIL_getTime(clockStart);
Yann Collet7b51a292016-01-26 15:58:49 +0100268
inikepc5e1d292016-04-19 09:37:59 +0200269 { U32 nbLoops = 0;
270 do {
Yann Colleta5b66e32016-03-26 01:48:27 +0100271 U32 blockNb;
272 ZSTD_decompressBegin_usingDict(refDCtx, dictBuffer, dictBufferSize);
273 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
274 size_t const regenSize = ZSTD_decompress_usingPreparedDCtx(dctx, refDCtx,
275 blockTable[blockNb].resPtr, blockTable[blockNb].srcSize,
276 blockTable[blockNb].cPtr, blockTable[blockNb].cSize);
277 if (ZSTD_isError(regenSize)) {
278 DISPLAY("ZSTD_decompress_usingPreparedDCtx() failed on block %u : %s \n",
279 blockNb, ZSTD_getErrorName(regenSize));
inikep19bd48f2016-04-04 12:10:00 +0200280 clockLoop = 0; /* force immediate test end */
Yann Colleta5b66e32016-03-26 01:48:27 +0100281 break;
282 }
283 blockTable[blockNb].resSize = regenSize;
inikepc5e1d292016-04-19 09:37:59 +0200284 }
285 nbLoops++;
inikep83c76b42016-04-28 13:16:01 +0200286 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
287 { U64 const clockSpan = UTIL_clockSpanMicro(clockStart, ticksPerSecond);
inikep6d157f12016-04-15 16:54:11 +0200288 if (clockSpan < fastestD*nbLoops) fastestD = clockSpan / nbLoops;
Yann Collet7b51a292016-01-26 15:58:49 +0100289 } }
290
inikep2872b6f2016-03-22 14:38:34 +0100291 DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r",
Yann Colletde406ee2016-03-20 15:46:10 +0100292 testNb, displayName, (U32)srcSize, (U32)cSize, ratio,
inikep06f793a2016-03-29 11:17:58 +0200293 (double)srcSize / fastestC,
294 (double)srcSize / fastestD );
Yann Collet4856a002015-01-24 01:58:16 +0100295
296 /* CRC Checking */
inikep19bd48f2016-04-04 12:10:00 +0200297 { crcCheck = XXH64(resultBuffer, srcSize, 0);
Yann Colleta5b66e32016-03-26 01:48:27 +0100298 if (crcOrig!=crcCheck) {
299 size_t u;
300 DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck);
301 for (u=0; u<srcSize; u++) {
302 if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) {
303 U32 segNb, bNb, pos;
304 size_t bacc = 0;
305 DISPLAY("Decoding error at pos %u ", (U32)u);
306 for (segNb = 0; segNb < nbBlocks; segNb++) {
307 if (bacc + blockTable[segNb].srcSize > u) break;
308 bacc += blockTable[segNb].srcSize;
309 }
310 pos = (U32)(u - bacc);
311 bNb = pos / (128 KB);
312 DISPLAY("(block %u, sub %u, pos %u) \n", segNb, bNb, pos);
313 break;
Yann Collet03a6dab2016-01-21 02:21:17 +0100314 }
Yann Colleta5b66e32016-03-26 01:48:27 +0100315 if (u==srcSize-1) { /* should never happen */
316 DISPLAY("no difference detected\n");
317 } }
318 break;
319 } } /* CRC Checking */
Yann Collete8c6bb12015-07-26 00:23:57 +0100320#endif
Yann Colletde406ee2016-03-20 15:46:10 +0100321 } /* for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) */
Yann Collet4856a002015-01-24 01:58:16 +0100322
inikep2872b6f2016-03-22 14:38:34 +0100323 if (crcOrig == crcCheck) {
inikep4e26bb62016-03-14 12:48:51 +0100324 result->ratio = ratio;
325 result->cSize = cSize;
Yann Collet99909862016-04-09 16:17:18 +0200326 result->cSpeed = (double)srcSize / fastestC;
inikep06f793a2016-03-29 11:17:58 +0200327 result->dSpeed = (double)srcSize / fastestD;
inikep4e26bb62016-03-14 12:48:51 +0100328 }
inikep2872b6f2016-03-22 14:38:34 +0100329 DISPLAYLEVEL(2, "%2i#\n", cLevel);
Yann Colletde406ee2016-03-20 15:46:10 +0100330 } /* Bench */
Yann Collet4856a002015-01-24 01:58:16 +0100331
Yann Colleted699e62015-12-16 02:37:24 +0100332 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100333 free(compressedBuffer);
334 free(resultBuffer);
Yann Collet2630a5e2016-01-14 19:13:22 +0100335 ZSTD_freeCCtx(refCtx);
Yann Collet31683c02015-12-18 01:26:48 +0100336 ZSTD_freeCCtx(ctx);
Yann Collet7b51a292016-01-26 15:58:49 +0100337 ZSTD_freeDCtx(refDCtx);
Yann Collet31683c02015-12-18 01:26:48 +0100338 ZSTD_freeDCtx(dctx);
Yann Collet4856a002015-01-24 01:58:16 +0100339 return 0;
340}
341
342
Yann Collet4856a002015-01-24 01:58:16 +0100343static size_t BMK_findMaxMem(U64 requiredMem)
344{
Yann Colletde406ee2016-03-20 15:46:10 +0100345 size_t const step = 64 MB;
Yann Collet4856a002015-01-24 01:58:16 +0100346 BYTE* testmem = NULL;
347
348 requiredMem = (((requiredMem >> 26) + 1) << 26);
Yann Colletde406ee2016-03-20 15:46:10 +0100349 requiredMem += step;
Yann Collet050efba2015-11-03 09:49:30 +0100350 if (requiredMem > maxMemory) requiredMem = maxMemory;
Yann Collet4856a002015-01-24 01:58:16 +0100351
Yann Colletde406ee2016-03-20 15:46:10 +0100352 do {
Yann Collet4856a002015-01-24 01:58:16 +0100353 testmem = (BYTE*)malloc((size_t)requiredMem);
Yann Colletde406ee2016-03-20 15:46:10 +0100354 requiredMem -= step;
355 } while (!testmem);
356
Yann Collet4856a002015-01-24 01:58:16 +0100357 free(testmem);
Yann Colletde406ee2016-03-20 15:46:10 +0100358 return (size_t)(requiredMem);
Yann Collet4856a002015-01-24 01:58:16 +0100359}
360
Yann Colleted699e62015-12-16 02:37:24 +0100361static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize,
inikepeaba91a2016-03-23 20:30:26 +0100362 const char* displayName, int cLevel, int cLevelLast,
Yann Collet31683c02015-12-18 01:26:48 +0100363 const size_t* fileSizes, unsigned nbFiles,
364 const void* dictBuffer, size_t dictBufferSize)
Yann Collet4856a002015-01-24 01:58:16 +0100365{
inikep4e26bb62016-03-14 12:48:51 +0100366 benchResult_t result, total;
inikepe9554b72016-03-14 18:10:30 +0100367 int l;
inikep4e26bb62016-03-14 12:48:51 +0100368
inikepe9554b72016-03-14 18:10:30 +0100369 const char* pch = strrchr(displayName, '\\'); /* Windows */
370 if (!pch) pch = strrchr(displayName, '/'); /* Linux */
371 if (pch) displayName = pch+1;
inikep4e26bb62016-03-14 12:48:51 +0100372
inikepea4ee3e2016-04-25 13:09:06 +0200373 SET_HIGH_PRIORITY;
374
inikepe9554b72016-03-14 18:10:30 +0100375 memset(&result, 0, sizeof(result));
376 memset(&total, 0, sizeof(total));
inikep5fdd0b42016-03-14 19:51:11 +0100377
inikepeaba91a2016-03-23 20:30:26 +0100378 if (g_displayLevel == 1 && !g_additionalParam)
inikep2872b6f2016-03-22 14:38:34 +0100379 DISPLAY("bench %s: input %u bytes, %i iterations, %u KB blocks\n", ZSTD_VERSION, (U32)benchedSize, g_nbIterations, (U32)(g_blockSize>>10));
inikepe9554b72016-03-14 18:10:30 +0100380
381 if (cLevelLast < cLevel) cLevelLast = cLevel;
382
Yann Collet99909862016-04-09 16:17:18 +0200383 for (l=cLevel; l <= cLevelLast; l++) {
inikepe9554b72016-03-14 18:10:30 +0100384 BMK_benchMem(srcBuffer, benchedSize,
inikep472638c2016-03-23 12:28:28 +0100385 displayName, l,
inikepe9554b72016-03-14 18:10:30 +0100386 fileSizes, nbFiles,
387 dictBuffer, dictBufferSize, &result);
388 if (g_displayLevel == 1) {
inikepeaba91a2016-03-23 20:30:26 +0100389 if (g_additionalParam)
inikep38654982016-04-21 12:18:47 +0200390 DISPLAY("%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s (param=%d)\n", -l, (int)result.cSize, result.ratio, result.cSpeed, result.dSpeed, displayName, g_additionalParam);
inikepd700a1a2016-03-15 12:18:44 +0100391 else
inikep38654982016-04-21 12:18:47 +0200392 DISPLAY("%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", -l, (int)result.cSize, result.ratio, result.cSpeed, result.dSpeed, displayName);
inikepe9554b72016-03-14 18:10:30 +0100393 total.cSize += result.cSize;
394 total.cSpeed += result.cSpeed;
395 total.dSpeed += result.dSpeed;
396 total.ratio += result.ratio;
Yann Collet99909862016-04-09 16:17:18 +0200397 } }
398 if (g_displayLevel == 1 && cLevelLast > cLevel) {
inikepe9554b72016-03-14 18:10:30 +0100399 total.cSize /= 1+cLevelLast-cLevel;
400 total.cSpeed /= 1+cLevelLast-cLevel;
401 total.dSpeed /= 1+cLevelLast-cLevel;
402 total.ratio /= 1+cLevelLast-cLevel;
inikep38654982016-04-21 12:18:47 +0200403 DISPLAY("avg%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", (int)total.cSize, total.ratio, total.cSpeed, total.dSpeed, displayName);
inikepe9554b72016-03-14 18:10:30 +0100404 }
Yann Colleted699e62015-12-16 02:37:24 +0100405}
406
Yann Colleted699e62015-12-16 02:37:24 +0100407
Yann Colleta5b66e32016-03-26 01:48:27 +0100408/*! BMK_loadFiles() :
409 Loads `buffer` with content of files listed within `fileNamesTable`.
410 At most, fills `buffer` entirely */
Yann Collet70611352015-12-16 03:01:03 +0100411static void BMK_loadFiles(void* buffer, size_t bufferSize,
412 size_t* fileSizes,
Yann Colleta5b66e32016-03-26 01:48:27 +0100413 const char** fileNamesTable, unsigned nbFiles)
Yann Colleted699e62015-12-16 02:37:24 +0100414{
inikepc0d5f4e2016-04-13 10:48:04 +0200415 size_t pos = 0, totalSize = 0;
inikepea4ee3e2016-04-25 13:09:06 +0200416 FILE* f;
Yann Collet699b14d2016-03-17 19:37:33 +0100417 unsigned n;
Yann Colletfd416f12016-01-30 03:14:15 +0100418 for (n=0; n<nbFiles; n++) {
inikep69fcd7c2016-04-28 12:23:33 +0200419 U64 fileSize = UTIL_getFileSize(fileNamesTable[n]);
420 if (UTIL_isDirectory(fileNamesTable[n])) {
inikepc0d5f4e2016-04-13 10:48:04 +0200421 DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]);
inikepbab43172016-04-29 15:19:40 +0200422 fileSizes[n] = 0;
inikepc0d5f4e2016-04-13 10:48:04 +0200423 continue;
424 }
inikepea4ee3e2016-04-25 13:09:06 +0200425 f = fopen(fileNamesTable[n], "rb");
Yann Colleted699e62015-12-16 02:37:24 +0100426 if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]);
427 DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[n]);
Yann Colleta5b66e32016-03-26 01:48:27 +0100428 if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */
429 { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
430 if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]);
431 pos += readSize; }
Yann Colleta52c98d2015-12-16 03:12:31 +0100432 fileSizes[n] = (size_t)fileSize;
inikepc0d5f4e2016-04-13 10:48:04 +0200433 totalSize += (size_t)fileSize;
Yann Colleted699e62015-12-16 02:37:24 +0100434 fclose(f);
435 }
inikepc0d5f4e2016-04-13 10:48:04 +0200436
437 if (totalSize == 0) EXM_THROW(12, "no data to bench");
Yann Colleted699e62015-12-16 02:37:24 +0100438}
439
Yann Collet31683c02015-12-18 01:26:48 +0100440static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles,
inikepeaba91a2016-03-23 20:30:26 +0100441 const char* dictFileName, int cLevel, int cLevelLast)
Yann Colleted699e62015-12-16 02:37:24 +0100442{
443 void* srcBuffer;
444 size_t benchedSize;
Yann Collet31683c02015-12-18 01:26:48 +0100445 void* dictBuffer = NULL;
446 size_t dictBufferSize = 0;
447 size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t));
inikep55d047a2016-04-28 16:50:13 +0200448 U64 totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +0100449 char mfName[20] = {0};
450 const char* displayName = NULL;
451
Yann Collet31683c02015-12-18 01:26:48 +0100452 if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes");
453
454 /* Load dictionary */
Yann Colletfd416f12016-01-30 03:14:15 +0100455 if (dictFileName != NULL) {
inikep69fcd7c2016-04-28 12:23:33 +0200456 U64 dictFileSize = UTIL_getFileSize(dictFileName);
Yann Collet31683c02015-12-18 01:26:48 +0100457 if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName);
458 dictBufferSize = (size_t)dictFileSize;
459 dictBuffer = malloc(dictBufferSize);
460 if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize);
461 BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1);
462 }
463
Yann Colleted699e62015-12-16 02:37:24 +0100464 /* Memory allocation & restrictions */
465 benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
466 if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
467 if (benchedSize < totalSizeToLoad)
468 DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20));
469 srcBuffer = malloc(benchedSize);
470 if (!srcBuffer) EXM_THROW(12, "not enough memory");
471
472 /* Load input buffer */
Yann Collet70611352015-12-16 03:01:03 +0100473 BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +0100474
475 /* Bench */
476 snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
477 if (nbFiles > 1) displayName = mfName;
478 else displayName = fileNamesTable[0];
479
Yann Collet31683c02015-12-18 01:26:48 +0100480 BMK_benchCLevel(srcBuffer, benchedSize,
inikepeaba91a2016-03-23 20:30:26 +0100481 displayName, cLevel, cLevelLast,
Yann Collet31683c02015-12-18 01:26:48 +0100482 fileSizes, nbFiles,
483 dictBuffer, dictBufferSize);
Yann Collet4856a002015-01-24 01:58:16 +0100484
Yann Colleteeb8ba12015-10-22 16:55:40 +0100485 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100486 free(srcBuffer);
Yann Collet31683c02015-12-18 01:26:48 +0100487 free(dictBuffer);
Yann Collet70611352015-12-16 03:01:03 +0100488 free(fileSizes);
Yann Collet4856a002015-01-24 01:58:16 +0100489}
490
491
inikepeaba91a2016-03-23 20:30:26 +0100492static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility)
Yann Collet4856a002015-01-24 01:58:16 +0100493{
Yann Colleted699e62015-12-16 02:37:24 +0100494 char name[20] = {0};
Yann Collet4856a002015-01-24 01:58:16 +0100495 size_t benchedSize = 10000000;
496 void* srcBuffer = malloc(benchedSize);
Yann Collet4856a002015-01-24 01:58:16 +0100497
Yann Collet4856a002015-01-24 01:58:16 +0100498 /* Memory allocation */
Yann Colleted699e62015-12-16 02:37:24 +0100499 if (!srcBuffer) EXM_THROW(21, "not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100500
501 /* Fill input buffer */
Yann Colletd062f132015-12-01 01:31:17 +0100502 RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100503
504 /* Bench */
Yann Colleted699e62015-12-16 02:37:24 +0100505 snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
inikepeaba91a2016-03-23 20:30:26 +0100506 BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100507
Yann Colleted699e62015-12-16 02:37:24 +0100508 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100509 free(srcBuffer);
Yann Collet4856a002015-01-24 01:58:16 +0100510}
511
512
Yann Collet31683c02015-12-18 01:26:48 +0100513int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
inikepeaba91a2016-03-23 20:30:26 +0100514 const char* dictFileName, int cLevel, int cLevelLast)
Yann Collet4856a002015-01-24 01:58:16 +0100515{
Yann Collet699b14d2016-03-17 19:37:33 +0100516 double const compressibility = (double)g_compressibilityDefault / 100;
Yann Collet4856a002015-01-24 01:58:16 +0100517
518 if (nbFiles == 0)
inikepeaba91a2016-03-23 20:30:26 +0100519 BMK_syntheticTest(cLevel, cLevelLast, compressibility);
Yann Collet4856a002015-01-24 01:58:16 +0100520 else
inikep9c22e572016-05-05 11:53:42 +0200521 {
522#ifdef UTIL_HAS_CREATEFILELIST
523 char* buf;
524 const char** filenameTable;
525 unsigned i;
526 nbFiles = UTIL_createFileList(fileNamesTable, nbFiles, MAX_LIST_SIZE, &filenameTable, &buf);
527 if (filenameTable) {
528 for (i=0; i<nbFiles; i++) printf ("%d %s\n", i, filenameTable[i]);
529 BMK_benchFileTable(filenameTable, nbFiles, dictFileName, cLevel, cLevelLast);
530 UTIL_freeFileList(filenameTable, buf);
531 }
532#else
inikepeaba91a2016-03-23 20:30:26 +0100533 BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast);
inikep9c22e572016-05-05 11:53:42 +0200534#endif
535 }
Yann Collet4856a002015-01-24 01:58:16 +0100536 return 0;
537}
538