blob: 513562185fd5ac1a6bbf5110588a6e981c6c0f9e [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 */
inikep0bd0fae2016-05-05 13:10:57 +0200333 free(blockTable);
Yann Collet4856a002015-01-24 01:58:16 +0100334 free(compressedBuffer);
335 free(resultBuffer);
Yann Collet2630a5e2016-01-14 19:13:22 +0100336 ZSTD_freeCCtx(refCtx);
Yann Collet31683c02015-12-18 01:26:48 +0100337 ZSTD_freeCCtx(ctx);
Yann Collet7b51a292016-01-26 15:58:49 +0100338 ZSTD_freeDCtx(refDCtx);
Yann Collet31683c02015-12-18 01:26:48 +0100339 ZSTD_freeDCtx(dctx);
Yann Collet4856a002015-01-24 01:58:16 +0100340 return 0;
341}
342
343
Yann Collet4856a002015-01-24 01:58:16 +0100344static size_t BMK_findMaxMem(U64 requiredMem)
345{
Yann Colletde406ee2016-03-20 15:46:10 +0100346 size_t const step = 64 MB;
Yann Collet4856a002015-01-24 01:58:16 +0100347 BYTE* testmem = NULL;
348
349 requiredMem = (((requiredMem >> 26) + 1) << 26);
Yann Colletde406ee2016-03-20 15:46:10 +0100350 requiredMem += step;
Yann Collet050efba2015-11-03 09:49:30 +0100351 if (requiredMem > maxMemory) requiredMem = maxMemory;
Yann Collet4856a002015-01-24 01:58:16 +0100352
Yann Colletde406ee2016-03-20 15:46:10 +0100353 do {
Yann Collet4856a002015-01-24 01:58:16 +0100354 testmem = (BYTE*)malloc((size_t)requiredMem);
Yann Colletde406ee2016-03-20 15:46:10 +0100355 requiredMem -= step;
356 } while (!testmem);
357
Yann Collet4856a002015-01-24 01:58:16 +0100358 free(testmem);
Yann Colletde406ee2016-03-20 15:46:10 +0100359 return (size_t)(requiredMem);
Yann Collet4856a002015-01-24 01:58:16 +0100360}
361
Yann Colleted699e62015-12-16 02:37:24 +0100362static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize,
inikepeaba91a2016-03-23 20:30:26 +0100363 const char* displayName, int cLevel, int cLevelLast,
Yann Collet31683c02015-12-18 01:26:48 +0100364 const size_t* fileSizes, unsigned nbFiles,
365 const void* dictBuffer, size_t dictBufferSize)
Yann Collet4856a002015-01-24 01:58:16 +0100366{
inikep4e26bb62016-03-14 12:48:51 +0100367 benchResult_t result, total;
inikepe9554b72016-03-14 18:10:30 +0100368 int l;
inikep4e26bb62016-03-14 12:48:51 +0100369
inikepe9554b72016-03-14 18:10:30 +0100370 const char* pch = strrchr(displayName, '\\'); /* Windows */
371 if (!pch) pch = strrchr(displayName, '/'); /* Linux */
372 if (pch) displayName = pch+1;
inikep4e26bb62016-03-14 12:48:51 +0100373
inikepea4ee3e2016-04-25 13:09:06 +0200374 SET_HIGH_PRIORITY;
375
inikepe9554b72016-03-14 18:10:30 +0100376 memset(&result, 0, sizeof(result));
377 memset(&total, 0, sizeof(total));
inikep5fdd0b42016-03-14 19:51:11 +0100378
inikepeaba91a2016-03-23 20:30:26 +0100379 if (g_displayLevel == 1 && !g_additionalParam)
inikep2872b6f2016-03-22 14:38:34 +0100380 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 +0100381
382 if (cLevelLast < cLevel) cLevelLast = cLevel;
383
Yann Collet99909862016-04-09 16:17:18 +0200384 for (l=cLevel; l <= cLevelLast; l++) {
inikepe9554b72016-03-14 18:10:30 +0100385 BMK_benchMem(srcBuffer, benchedSize,
inikep472638c2016-03-23 12:28:28 +0100386 displayName, l,
inikepe9554b72016-03-14 18:10:30 +0100387 fileSizes, nbFiles,
388 dictBuffer, dictBufferSize, &result);
389 if (g_displayLevel == 1) {
inikepeaba91a2016-03-23 20:30:26 +0100390 if (g_additionalParam)
inikep38654982016-04-21 12:18:47 +0200391 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 +0100392 else
inikep38654982016-04-21 12:18:47 +0200393 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 +0100394 total.cSize += result.cSize;
395 total.cSpeed += result.cSpeed;
396 total.dSpeed += result.dSpeed;
397 total.ratio += result.ratio;
Yann Collet99909862016-04-09 16:17:18 +0200398 } }
399 if (g_displayLevel == 1 && cLevelLast > cLevel) {
inikepe9554b72016-03-14 18:10:30 +0100400 total.cSize /= 1+cLevelLast-cLevel;
401 total.cSpeed /= 1+cLevelLast-cLevel;
402 total.dSpeed /= 1+cLevelLast-cLevel;
403 total.ratio /= 1+cLevelLast-cLevel;
inikep38654982016-04-21 12:18:47 +0200404 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 +0100405 }
Yann Colleted699e62015-12-16 02:37:24 +0100406}
407
Yann Colleted699e62015-12-16 02:37:24 +0100408
Yann Colleta5b66e32016-03-26 01:48:27 +0100409/*! BMK_loadFiles() :
410 Loads `buffer` with content of files listed within `fileNamesTable`.
411 At most, fills `buffer` entirely */
Yann Collet70611352015-12-16 03:01:03 +0100412static void BMK_loadFiles(void* buffer, size_t bufferSize,
413 size_t* fileSizes,
Yann Colleta5b66e32016-03-26 01:48:27 +0100414 const char** fileNamesTable, unsigned nbFiles)
Yann Colleted699e62015-12-16 02:37:24 +0100415{
inikepc0d5f4e2016-04-13 10:48:04 +0200416 size_t pos = 0, totalSize = 0;
inikepea4ee3e2016-04-25 13:09:06 +0200417 FILE* f;
Yann Collet699b14d2016-03-17 19:37:33 +0100418 unsigned n;
Yann Colletfd416f12016-01-30 03:14:15 +0100419 for (n=0; n<nbFiles; n++) {
inikep69fcd7c2016-04-28 12:23:33 +0200420 U64 fileSize = UTIL_getFileSize(fileNamesTable[n]);
421 if (UTIL_isDirectory(fileNamesTable[n])) {
inikepc0d5f4e2016-04-13 10:48:04 +0200422 DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]);
inikepbab43172016-04-29 15:19:40 +0200423 fileSizes[n] = 0;
inikepc0d5f4e2016-04-13 10:48:04 +0200424 continue;
425 }
inikepea4ee3e2016-04-25 13:09:06 +0200426 f = fopen(fileNamesTable[n], "rb");
Yann Colleted699e62015-12-16 02:37:24 +0100427 if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]);
428 DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[n]);
Yann Colleta5b66e32016-03-26 01:48:27 +0100429 if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */
430 { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
431 if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]);
432 pos += readSize; }
Yann Colleta52c98d2015-12-16 03:12:31 +0100433 fileSizes[n] = (size_t)fileSize;
inikepc0d5f4e2016-04-13 10:48:04 +0200434 totalSize += (size_t)fileSize;
Yann Colleted699e62015-12-16 02:37:24 +0100435 fclose(f);
436 }
inikepc0d5f4e2016-04-13 10:48:04 +0200437
438 if (totalSize == 0) EXM_THROW(12, "no data to bench");
Yann Colleted699e62015-12-16 02:37:24 +0100439}
440
Yann Collet31683c02015-12-18 01:26:48 +0100441static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles,
inikepeaba91a2016-03-23 20:30:26 +0100442 const char* dictFileName, int cLevel, int cLevelLast)
Yann Colleted699e62015-12-16 02:37:24 +0100443{
444 void* srcBuffer;
445 size_t benchedSize;
Yann Collet31683c02015-12-18 01:26:48 +0100446 void* dictBuffer = NULL;
447 size_t dictBufferSize = 0;
448 size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t));
inikep55d047a2016-04-28 16:50:13 +0200449 U64 totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +0100450 char mfName[20] = {0};
451 const char* displayName = NULL;
452
Yann Collet31683c02015-12-18 01:26:48 +0100453 if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes");
454
455 /* Load dictionary */
Yann Colletfd416f12016-01-30 03:14:15 +0100456 if (dictFileName != NULL) {
inikep69fcd7c2016-04-28 12:23:33 +0200457 U64 dictFileSize = UTIL_getFileSize(dictFileName);
Yann Collet31683c02015-12-18 01:26:48 +0100458 if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName);
459 dictBufferSize = (size_t)dictFileSize;
460 dictBuffer = malloc(dictBufferSize);
461 if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize);
462 BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1);
463 }
464
Yann Colleted699e62015-12-16 02:37:24 +0100465 /* Memory allocation & restrictions */
466 benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
467 if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
468 if (benchedSize < totalSizeToLoad)
469 DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20));
470 srcBuffer = malloc(benchedSize);
471 if (!srcBuffer) EXM_THROW(12, "not enough memory");
472
473 /* Load input buffer */
Yann Collet70611352015-12-16 03:01:03 +0100474 BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +0100475
476 /* Bench */
477 snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
478 if (nbFiles > 1) displayName = mfName;
479 else displayName = fileNamesTable[0];
480
Yann Collet31683c02015-12-18 01:26:48 +0100481 BMK_benchCLevel(srcBuffer, benchedSize,
inikepeaba91a2016-03-23 20:30:26 +0100482 displayName, cLevel, cLevelLast,
Yann Collet31683c02015-12-18 01:26:48 +0100483 fileSizes, nbFiles,
484 dictBuffer, dictBufferSize);
Yann Collet4856a002015-01-24 01:58:16 +0100485
Yann Colleteeb8ba12015-10-22 16:55:40 +0100486 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100487 free(srcBuffer);
Yann Collet31683c02015-12-18 01:26:48 +0100488 free(dictBuffer);
Yann Collet70611352015-12-16 03:01:03 +0100489 free(fileSizes);
Yann Collet4856a002015-01-24 01:58:16 +0100490}
491
492
inikepeaba91a2016-03-23 20:30:26 +0100493static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility)
Yann Collet4856a002015-01-24 01:58:16 +0100494{
Yann Colleted699e62015-12-16 02:37:24 +0100495 char name[20] = {0};
Yann Collet4856a002015-01-24 01:58:16 +0100496 size_t benchedSize = 10000000;
497 void* srcBuffer = malloc(benchedSize);
Yann Collet4856a002015-01-24 01:58:16 +0100498
Yann Collet4856a002015-01-24 01:58:16 +0100499 /* Memory allocation */
Yann Colleted699e62015-12-16 02:37:24 +0100500 if (!srcBuffer) EXM_THROW(21, "not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100501
502 /* Fill input buffer */
Yann Colletd062f132015-12-01 01:31:17 +0100503 RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100504
505 /* Bench */
Yann Colleted699e62015-12-16 02:37:24 +0100506 snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
inikepeaba91a2016-03-23 20:30:26 +0100507 BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100508
Yann Colleted699e62015-12-16 02:37:24 +0100509 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100510 free(srcBuffer);
Yann Collet4856a002015-01-24 01:58:16 +0100511}
512
513
Yann Collet31683c02015-12-18 01:26:48 +0100514int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
inikepeaba91a2016-03-23 20:30:26 +0100515 const char* dictFileName, int cLevel, int cLevelLast)
Yann Collet4856a002015-01-24 01:58:16 +0100516{
Yann Collet699b14d2016-03-17 19:37:33 +0100517 double const compressibility = (double)g_compressibilityDefault / 100;
Yann Collet4856a002015-01-24 01:58:16 +0100518
519 if (nbFiles == 0)
inikepeaba91a2016-03-23 20:30:26 +0100520 BMK_syntheticTest(cLevel, cLevelLast, compressibility);
Yann Collet4856a002015-01-24 01:58:16 +0100521 else
inikep9c22e572016-05-05 11:53:42 +0200522 {
523#ifdef UTIL_HAS_CREATEFILELIST
524 char* buf;
525 const char** filenameTable;
526 unsigned i;
527 nbFiles = UTIL_createFileList(fileNamesTable, nbFiles, MAX_LIST_SIZE, &filenameTable, &buf);
528 if (filenameTable) {
inikep0bd0fae2016-05-05 13:10:57 +0200529 for (i=0; i<nbFiles; i++) DISPLAYLEVEL(3, "%d %s\n", i, filenameTable[i]);
inikep9c22e572016-05-05 11:53:42 +0200530 BMK_benchFileTable(filenameTable, nbFiles, dictFileName, cLevel, cLevelLast);
531 UTIL_freeFileList(filenameTable, buf);
532 }
533#else
inikepeaba91a2016-03-23 20:30:26 +0100534 BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast);
inikep9c22e572016-05-05 11:53:42 +0200535#endif
536 }
Yann Collet4856a002015-01-24 01:58:16 +0100537 return 0;
538}
539