blob: a7a4c579cd1590d7dc1ab6fcf97675f376ca76fd [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* Compiler Options
Yann Colletf3eca252015-10-22 15:31:46 +010028****************************************/
Yann Collet4856a002015-01-24 01:58:16 +010029/* Disable some Visual warning messages */
Yann Collet6c8b9252015-12-16 02:44:56 +010030#ifdef _MSC_VER
31# define _CRT_SECURE_NO_WARNINGS /* fopen */
32# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
33#endif
Yann Collet4856a002015-01-24 01:58:16 +010034
Yann Colletf3eca252015-10-22 15:31:46 +010035/* Unix Large Files support (>4GB) */
Yann Collet4856a002015-01-24 01:58:16 +010036#define _FILE_OFFSET_BITS 64
Yann Colletf3eca252015-10-22 15:31:46 +010037#if (defined(__sun__) && (!defined(__LP64__))) /* Sun Solaris 32-bits requires specific definitions */
Yann Collet4856a002015-01-24 01:58:16 +010038# define _LARGEFILE_SOURCE
Yann Colletf3eca252015-10-22 15:31:46 +010039#elif ! defined(__LP64__) /* No point defining Large file for 64 bit */
Yann Collet4856a002015-01-24 01:58:16 +010040# define _LARGEFILE64_SOURCE
41#endif
42
Yann Collet4856a002015-01-24 01:58:16 +010043
Yann Colletf3eca252015-10-22 15:31:46 +010044/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010045* Includes
Yann Colletf3eca252015-10-22 15:31:46 +010046***************************************/
Yann Collet99909862016-04-09 16:17:18 +020047#define _POSIX_C_SOURCE 199309L /* before <time.h> - needed for nanosleep() */
Yann Collet4856a002015-01-24 01:58:16 +010048#include <stdlib.h> /* malloc, free */
49#include <string.h> /* memset */
Yann Colleteeb8ba12015-10-22 16:55:40 +010050#include <stdio.h> /* fprintf, fopen, ftello64 */
51#include <sys/types.h> /* stat64 */
52#include <sys/stat.h> /* stat64 */
inikep4c12f232016-03-29 14:52:13 +020053
Yann Colletf3eca252015-10-22 15:31:46 +010054#include "mem.h"
Yann Collet31683c02015-12-18 01:26:48 +010055#include "zstd_static.h"
inikep69fcd7c2016-04-28 12:23:33 +020056#include "datagen.h" /* RDG_genBuffer */
Yann Collet4856a002015-01-24 01:58:16 +010057#include "xxhash.h"
inikep69fcd7c2016-04-28 12:23:33 +020058#include "util.h" /* UTIL_GetFileSize */
Yann Collet4856a002015-01-24 01:58:16 +010059
60
Yann Colletf3eca252015-10-22 15:31:46 +010061/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010062* Compiler specifics
Yann Colletf3eca252015-10-22 15:31:46 +010063***************************************/
Yann Collet99909862016-04-09 16:17:18 +020064#if defined(_MSC_VER)
65# define snprintf sprintf_s
66#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L))
67 /* part of <stdio.h> */
68#else
69 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 +010070#endif
71
Yann Collet4856a002015-01-24 01:58:16 +010072
Yann Colletf3eca252015-10-22 15:31:46 +010073/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010074* Constants
Yann Colletf3eca252015-10-22 15:31:46 +010075***************************************/
inikep44af12d2016-03-14 15:59:04 +010076#ifndef ZSTD_VERSION
77# define ZSTD_VERSION ""
78#endif
79
inikep83c76b42016-04-28 13:16:01 +020080#define NBLOOPS 3
81#define TIMELOOP_MICROSEC 1*1000000ULL /* 1 second */
82#define ACTIVEPERIOD_MICROSEC 70*1000000ULL /* 70 seconds */
83#define COOLPERIOD_SEC 10
Yann Collet4856a002015-01-24 01:58:16 +010084
85#define KB *(1 <<10)
86#define MB *(1 <<20)
87#define GB *(1U<<30)
88
Yann Colletd062f132015-12-01 01:31:17 +010089static 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 +010090
91static U32 g_compressibilityDefault = 50;
Yann Collet4856a002015-01-24 01:58:16 +010092
93
Yann Colletf3eca252015-10-22 15:31:46 +010094/* *************************************
Yann Colleted699e62015-12-16 02:37:24 +010095* console display
Yann Colletf3eca252015-10-22 15:31:46 +010096***************************************/
Yann Colleted699e62015-12-16 02:37:24 +010097#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
98#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
99static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
100
101
102/* *************************************
103* Exceptions
104***************************************/
105#ifndef DEBUG
106# define DEBUG 0
107#endif
108#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
109#define EXM_THROW(error, ...) \
110{ \
111 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
112 DISPLAYLEVEL(1, "Error %i : ", error); \
113 DISPLAYLEVEL(1, __VA_ARGS__); \
114 DISPLAYLEVEL(1, "\n"); \
115 exit(error); \
116}
Yann Collet4856a002015-01-24 01:58:16 +0100117
118
Yann Colletf3eca252015-10-22 15:31:46 +0100119/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100120* Benchmark Parameters
Yann Colletf3eca252015-10-22 15:31:46 +0100121***************************************/
Yann Collet1d1ae402016-03-17 19:51:02 +0100122static U32 g_nbIterations = NBLOOPS;
Yann Collet1c00dc32015-10-21 08:22:25 +0100123static size_t g_blockSize = 0;
inikepeaba91a2016-03-23 20:30:26 +0100124int g_additionalParam = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100125
inikep4e26bb62016-03-14 12:48:51 +0100126void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; }
127
inikepeaba91a2016-03-23 20:30:26 +0100128void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; }
129
Yann Collet1d1ae402016-03-17 19:51:02 +0100130void BMK_SetNbIterations(unsigned nbLoops)
Yann Collet4856a002015-01-24 01:58:16 +0100131{
Yann Collet1d1ae402016-03-17 19:51:02 +0100132 g_nbIterations = nbLoops;
inikep2872b6f2016-03-22 14:38:34 +0100133 DISPLAYLEVEL(2, "- %i iterations -\n", g_nbIterations);
Yann Collet4856a002015-01-24 01:58:16 +0100134}
135
Yann Collet1c00dc32015-10-21 08:22:25 +0100136void BMK_SetBlockSize(size_t blockSize)
137{
138 g_blockSize = blockSize;
inikep4e26bb62016-03-14 12:48:51 +0100139 DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10));
Yann Collet1c00dc32015-10-21 08:22:25 +0100140}
141
Yann Collet4856a002015-01-24 01:58:16 +0100142
Yann Colletf3eca252015-10-22 15:31:46 +0100143/* ********************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100144* Bench functions
Yann Colletf3eca252015-10-22 15:31:46 +0100145**********************************************************/
Yann Collet1c00dc32015-10-21 08:22:25 +0100146typedef struct
Yann Collet4856a002015-01-24 01:58:16 +0100147{
Yann Colleted699e62015-12-16 02:37:24 +0100148 const char* srcPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100149 size_t srcSize;
150 char* cPtr;
151 size_t cRoom;
152 size_t cSize;
153 char* resPtr;
154 size_t resSize;
155} blockParam_t;
156
inikep4e26bb62016-03-14 12:48:51 +0100157typedef struct
158{
inikepc034b732016-03-14 13:13:42 +0100159 double ratio;
inikep4e26bb62016-03-14 12:48:51 +0100160 size_t cSize;
inikepc034b732016-03-14 13:13:42 +0100161 double cSpeed;
162 double dSpeed;
inikep4e26bb62016-03-14 12:48:51 +0100163} benchResult_t;
164
Yann Collet99909862016-04-09 16:17:18 +0200165
Yann Colletbe2010e2015-10-31 12:57:14 +0100166#define MIN(a,b) ((a)<(b) ? (a) : (b))
Yann Collet2ce49232016-02-02 14:36:49 +0100167#define MAX(a,b) ((a)>(b) ? (a) : (b))
Yann Collet1c00dc32015-10-21 08:22:25 +0100168
Yann Colleted699e62015-12-16 02:37:24 +0100169static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
inikep472638c2016-03-23 12:28:28 +0100170 const char* displayName, int cLevel,
Yann Collet31683c02015-12-18 01:26:48 +0100171 const size_t* fileSizes, U32 nbFiles,
inikep4e26bb62016-03-14 12:48:51 +0100172 const void* dictBuffer, size_t dictBufferSize, benchResult_t *result)
Yann Collet1c00dc32015-10-21 08:22:25 +0100173{
inikep38654982016-04-21 12:18:47 +0200174 size_t const blockSize = (g_blockSize>=32 ? g_blockSize : srcSize) + (!srcSize); /* avoid div by 0 */
Yann Colletd64f4352016-03-21 00:07:42 +0100175 U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
Yann Colleted699e62015-12-16 02:37:24 +0100176 blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t));
Yann Colletb9151402016-03-26 17:18:11 +0100177 size_t const maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */
Yann Collet1c00dc32015-10-21 08:22:25 +0100178 void* const compressedBuffer = malloc(maxCompressedSize);
179 void* const resultBuffer = malloc(srcSize);
Yann Collet2630a5e2016-01-14 19:13:22 +0100180 ZSTD_CCtx* refCtx = ZSTD_createCCtx();
Yann Collet31683c02015-12-18 01:26:48 +0100181 ZSTD_CCtx* ctx = ZSTD_createCCtx();
Yann Collet7b51a292016-01-26 15:58:49 +0100182 ZSTD_DCtx* refDCtx = ZSTD_createDCtx();
Yann Collet31683c02015-12-18 01:26:48 +0100183 ZSTD_DCtx* dctx = ZSTD_createDCtx();
Yann Colletde406ee2016-03-20 15:46:10 +0100184 U32 nbBlocks;
inikep83c76b42016-04-28 13:16:01 +0200185 UTIL_time_t ticksPerSecond;
Yann Colletde406ee2016-03-20 15:46:10 +0100186
187 /* checks */
Yann Collet7b51a292016-01-26 15:58:49 +0100188 if (!compressedBuffer || !resultBuffer || !blockTable || !refCtx || !ctx || !refDCtx || !dctx)
Yann Colleted699e62015-12-16 02:37:24 +0100189 EXM_THROW(31, "not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100190
Yann Collet4856a002015-01-24 01:58:16 +0100191 /* init */
192 if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */
inikep83c76b42016-04-28 13:16:01 +0200193 UTIL_initTimer(ticksPerSecond);
inikep4c12f232016-03-29 14:52:13 +0200194
Yann Collet1c00dc32015-10-21 08:22:25 +0100195 /* Init blockTable data */
Yann Colletde406ee2016-03-20 15:46:10 +0100196 { const char* srcPtr = (const char*)srcBuffer;
Yann Collet1c00dc32015-10-21 08:22:25 +0100197 char* cPtr = (char*)compressedBuffer;
198 char* resPtr = (char*)resultBuffer;
Yann Collet699b14d2016-03-17 19:37:33 +0100199 U32 fileNb;
Yann Colletde406ee2016-03-20 15:46:10 +0100200 for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
Yann Collet70611352015-12-16 03:01:03 +0100201 size_t remaining = fileSizes[fileNb];
Yann Collet699b14d2016-03-17 19:37:33 +0100202 U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize);
203 U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
Yann Colletfd416f12016-01-30 03:14:15 +0100204 for ( ; nbBlocks<blockEnd; nbBlocks++) {
Yann Colletde406ee2016-03-20 15:46:10 +0100205 size_t const thisBlockSize = MIN(remaining, blockSize);
Yann Colleted699e62015-12-16 02:37:24 +0100206 blockTable[nbBlocks].srcPtr = srcPtr;
207 blockTable[nbBlocks].cPtr = cPtr;
208 blockTable[nbBlocks].resPtr = resPtr;
209 blockTable[nbBlocks].srcSize = thisBlockSize;
210 blockTable[nbBlocks].cRoom = ZSTD_compressBound(thisBlockSize);
211 srcPtr += thisBlockSize;
212 cPtr += blockTable[nbBlocks].cRoom;
213 resPtr += thisBlockSize;
214 remaining -= thisBlockSize;
Yann Colletfd416f12016-01-30 03:14:15 +0100215 } } }
Yann Collet1c00dc32015-10-21 08:22:25 +0100216
Yann Collet4856a002015-01-24 01:58:16 +0100217 /* warmimg up memory */
Yann Colletd062f132015-12-01 01:31:17 +0100218 RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
Yann Collet4856a002015-01-24 01:58:16 +0100219
220 /* Bench */
inikep6d157f12016-04-15 16:54:11 +0200221 { U64 fastestC = (U64)(-1LL), fastestD = (U64)(-1LL);
Yann Colletb9151402016-03-26 17:18:11 +0100222 U64 const crcOrig = XXH64(srcBuffer, srcSize, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100223 U64 crcCheck = 0;
inikep83c76b42016-04-28 13:16:01 +0200224 UTIL_time_t coolTime;
Yann Colletde406ee2016-03-20 15:46:10 +0100225 U32 testNb;
inikep19bd48f2016-04-04 12:10:00 +0200226 size_t cSize = 0;
227 double ratio = 0.;
Yann Collet4856a002015-01-24 01:58:16 +0100228
inikep83c76b42016-04-28 13:16:01 +0200229 UTIL_getTime(coolTime);
inikep4e26bb62016-03-14 12:48:51 +0100230 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Colletde406ee2016-03-20 15:46:10 +0100231 for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) {
inikep83c76b42016-04-28 13:16:01 +0200232 UTIL_time_t clockStart;
233 U64 clockLoop = g_nbIterations ? TIMELOOP_MICROSEC : 1;
Yann Collet4856a002015-01-24 01:58:16 +0100234
Yann Collet27d3dad2016-03-11 13:41:20 +0100235 /* overheat protection */
inikep83c76b42016-04-28 13:16:01 +0200236 if (UTIL_clockSpanMicro(coolTime, ticksPerSecond) > ACTIVEPERIOD_MICROSEC) {
Yann Collet27d3dad2016-03-11 13:41:20 +0100237 DISPLAY("\rcooling down ... \r");
inikep83c76b42016-04-28 13:16:01 +0200238 UTIL_sleep(COOLPERIOD_SEC);
239 UTIL_getTime(coolTime);
Yann Collet27d3dad2016-03-11 13:41:20 +0100240 }
Yann Collet4856a002015-01-24 01:58:16 +0100241
242 /* Compression */
inikep2872b6f2016-03-22 14:38:34 +0100243 DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->\r", testNb, displayName, (U32)srcSize);
Yann Collet699b14d2016-03-17 19:37:33 +0100244 memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100245
inikep83c76b42016-04-28 13:16:01 +0200246 UTIL_sleepMilli(1); /* give processor time to other processes */
247 UTIL_waitForNextTick(ticksPerSecond);
248 UTIL_getTime(clockStart);
Yann Colletde406ee2016-03-20 15:46:10 +0100249
inikepc5e1d292016-04-19 09:37:59 +0200250 { U32 nbLoops = 0;
251 do {
Yann Colleta5b66e32016-03-26 01:48:27 +0100252 U32 blockNb;
inikep4b3c5ee2016-04-14 13:43:51 +0200253 { ZSTD_parameters params;
254 params.cParams = ZSTD_getCParams(cLevel, blockSize, dictBufferSize);
255 params.fParams.contentSizeFlag = 1;
256 ZSTD_adjustCParams(&params.cParams, blockSize, dictBufferSize);
257 { size_t const initResult = ZSTD_compressBegin_advanced(refCtx, dictBuffer, dictBufferSize, params, blockSize);
258 if (ZSTD_isError(initResult)) break;
259 } }
Yann Colleta5b66e32016-03-26 01:48:27 +0100260 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
261 size_t const rSize = ZSTD_compress_usingPreparedCCtx(ctx, refCtx,
262 blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
263 blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize);
264 if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingPreparedCCtx() failed : %s", ZSTD_getErrorName(rSize));
265 blockTable[blockNb].cSize = rSize;
inikepc5e1d292016-04-19 09:37:59 +0200266 }
267 nbLoops++;
inikep83c76b42016-04-28 13:16:01 +0200268 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
269 { U64 const clockSpan = UTIL_clockSpanMicro(clockStart, ticksPerSecond);
inikep6d157f12016-04-15 16:54:11 +0200270 if (clockSpan < fastestC*nbLoops) fastestC = clockSpan / nbLoops;
Yann Colletde406ee2016-03-20 15:46:10 +0100271 } }
Yann Collet4856a002015-01-24 01:58:16 +0100272
Yann Collet1c00dc32015-10-21 08:22:25 +0100273 cSize = 0;
Yann Colletde406ee2016-03-20 15:46:10 +0100274 { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; }
Yann Collet2acb5d32015-10-29 16:49:43 +0100275 ratio = (double)srcSize / (double)cSize;
inikep2872b6f2016-03-22 14:38:34 +0100276 DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r",
Yann Colletde406ee2016-03-20 15:46:10 +0100277 testNb, displayName, (U32)srcSize, (U32)cSize, ratio,
inikep06f793a2016-03-29 11:17:58 +0200278 (double)srcSize / fastestC );
Yann Collet4856a002015-01-24 01:58:16 +0100279
Yann Colleta5b66e32016-03-26 01:48:27 +0100280 (void)fastestD; (void)crcOrig; /* unused when decompression disabled */
Yann Collete93d6ce2016-01-31 00:58:06 +0100281#if 1
Yann Collet4856a002015-01-24 01:58:16 +0100282 /* Decompression */
Yann Collet7b51a292016-01-26 15:58:49 +0100283 memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100284
inikep83c76b42016-04-28 13:16:01 +0200285 UTIL_sleepMilli(1); /* give processor time to other processes */
286 UTIL_waitForNextTick(ticksPerSecond);
287 UTIL_getTime(clockStart);
Yann Collet7b51a292016-01-26 15:58:49 +0100288
inikepc5e1d292016-04-19 09:37:59 +0200289 { U32 nbLoops = 0;
290 do {
Yann Colleta5b66e32016-03-26 01:48:27 +0100291 U32 blockNb;
292 ZSTD_decompressBegin_usingDict(refDCtx, dictBuffer, dictBufferSize);
293 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
294 size_t const regenSize = ZSTD_decompress_usingPreparedDCtx(dctx, refDCtx,
295 blockTable[blockNb].resPtr, blockTable[blockNb].srcSize,
296 blockTable[blockNb].cPtr, blockTable[blockNb].cSize);
297 if (ZSTD_isError(regenSize)) {
298 DISPLAY("ZSTD_decompress_usingPreparedDCtx() failed on block %u : %s \n",
299 blockNb, ZSTD_getErrorName(regenSize));
inikep19bd48f2016-04-04 12:10:00 +0200300 clockLoop = 0; /* force immediate test end */
Yann Colleta5b66e32016-03-26 01:48:27 +0100301 break;
302 }
303 blockTable[blockNb].resSize = regenSize;
inikepc5e1d292016-04-19 09:37:59 +0200304 }
305 nbLoops++;
inikep83c76b42016-04-28 13:16:01 +0200306 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
307 { U64 const clockSpan = UTIL_clockSpanMicro(clockStart, ticksPerSecond);
inikep6d157f12016-04-15 16:54:11 +0200308 if (clockSpan < fastestD*nbLoops) fastestD = clockSpan / nbLoops;
Yann Collet7b51a292016-01-26 15:58:49 +0100309 } }
310
inikep2872b6f2016-03-22 14:38:34 +0100311 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 +0100312 testNb, displayName, (U32)srcSize, (U32)cSize, ratio,
inikep06f793a2016-03-29 11:17:58 +0200313 (double)srcSize / fastestC,
314 (double)srcSize / fastestD );
Yann Collet4856a002015-01-24 01:58:16 +0100315
316 /* CRC Checking */
inikep19bd48f2016-04-04 12:10:00 +0200317 { crcCheck = XXH64(resultBuffer, srcSize, 0);
Yann Colleta5b66e32016-03-26 01:48:27 +0100318 if (crcOrig!=crcCheck) {
319 size_t u;
320 DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck);
321 for (u=0; u<srcSize; u++) {
322 if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) {
323 U32 segNb, bNb, pos;
324 size_t bacc = 0;
325 DISPLAY("Decoding error at pos %u ", (U32)u);
326 for (segNb = 0; segNb < nbBlocks; segNb++) {
327 if (bacc + blockTable[segNb].srcSize > u) break;
328 bacc += blockTable[segNb].srcSize;
329 }
330 pos = (U32)(u - bacc);
331 bNb = pos / (128 KB);
332 DISPLAY("(block %u, sub %u, pos %u) \n", segNb, bNb, pos);
333 break;
Yann Collet03a6dab2016-01-21 02:21:17 +0100334 }
Yann Colleta5b66e32016-03-26 01:48:27 +0100335 if (u==srcSize-1) { /* should never happen */
336 DISPLAY("no difference detected\n");
337 } }
338 break;
339 } } /* CRC Checking */
Yann Collete8c6bb12015-07-26 00:23:57 +0100340#endif
Yann Colletde406ee2016-03-20 15:46:10 +0100341 } /* for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) */
Yann Collet4856a002015-01-24 01:58:16 +0100342
inikep2872b6f2016-03-22 14:38:34 +0100343 if (crcOrig == crcCheck) {
inikep4e26bb62016-03-14 12:48:51 +0100344 result->ratio = ratio;
345 result->cSize = cSize;
Yann Collet99909862016-04-09 16:17:18 +0200346 result->cSpeed = (double)srcSize / fastestC;
inikep06f793a2016-03-29 11:17:58 +0200347 result->dSpeed = (double)srcSize / fastestD;
inikep4e26bb62016-03-14 12:48:51 +0100348 }
inikep2872b6f2016-03-22 14:38:34 +0100349 DISPLAYLEVEL(2, "%2i#\n", cLevel);
Yann Colletde406ee2016-03-20 15:46:10 +0100350 } /* Bench */
Yann Collet4856a002015-01-24 01:58:16 +0100351
Yann Colleted699e62015-12-16 02:37:24 +0100352 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100353 free(compressedBuffer);
354 free(resultBuffer);
Yann Collet2630a5e2016-01-14 19:13:22 +0100355 ZSTD_freeCCtx(refCtx);
Yann Collet31683c02015-12-18 01:26:48 +0100356 ZSTD_freeCCtx(ctx);
Yann Collet7b51a292016-01-26 15:58:49 +0100357 ZSTD_freeDCtx(refDCtx);
Yann Collet31683c02015-12-18 01:26:48 +0100358 ZSTD_freeDCtx(dctx);
Yann Collet4856a002015-01-24 01:58:16 +0100359 return 0;
360}
361
362
Yann Collet4856a002015-01-24 01:58:16 +0100363static size_t BMK_findMaxMem(U64 requiredMem)
364{
Yann Colletde406ee2016-03-20 15:46:10 +0100365 size_t const step = 64 MB;
Yann Collet4856a002015-01-24 01:58:16 +0100366 BYTE* testmem = NULL;
367
368 requiredMem = (((requiredMem >> 26) + 1) << 26);
Yann Colletde406ee2016-03-20 15:46:10 +0100369 requiredMem += step;
Yann Collet050efba2015-11-03 09:49:30 +0100370 if (requiredMem > maxMemory) requiredMem = maxMemory;
Yann Collet4856a002015-01-24 01:58:16 +0100371
Yann Colletde406ee2016-03-20 15:46:10 +0100372 do {
Yann Collet4856a002015-01-24 01:58:16 +0100373 testmem = (BYTE*)malloc((size_t)requiredMem);
Yann Colletde406ee2016-03-20 15:46:10 +0100374 requiredMem -= step;
375 } while (!testmem);
376
Yann Collet4856a002015-01-24 01:58:16 +0100377 free(testmem);
Yann Colletde406ee2016-03-20 15:46:10 +0100378 return (size_t)(requiredMem);
Yann Collet4856a002015-01-24 01:58:16 +0100379}
380
Yann Colleted699e62015-12-16 02:37:24 +0100381static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize,
inikepeaba91a2016-03-23 20:30:26 +0100382 const char* displayName, int cLevel, int cLevelLast,
Yann Collet31683c02015-12-18 01:26:48 +0100383 const size_t* fileSizes, unsigned nbFiles,
384 const void* dictBuffer, size_t dictBufferSize)
Yann Collet4856a002015-01-24 01:58:16 +0100385{
inikep4e26bb62016-03-14 12:48:51 +0100386 benchResult_t result, total;
inikepe9554b72016-03-14 18:10:30 +0100387 int l;
inikep4e26bb62016-03-14 12:48:51 +0100388
inikepe9554b72016-03-14 18:10:30 +0100389 const char* pch = strrchr(displayName, '\\'); /* Windows */
390 if (!pch) pch = strrchr(displayName, '/'); /* Linux */
391 if (pch) displayName = pch+1;
inikep4e26bb62016-03-14 12:48:51 +0100392
inikepea4ee3e2016-04-25 13:09:06 +0200393 SET_HIGH_PRIORITY;
394
inikepe9554b72016-03-14 18:10:30 +0100395 memset(&result, 0, sizeof(result));
396 memset(&total, 0, sizeof(total));
inikep5fdd0b42016-03-14 19:51:11 +0100397
inikepeaba91a2016-03-23 20:30:26 +0100398 if (g_displayLevel == 1 && !g_additionalParam)
inikep2872b6f2016-03-22 14:38:34 +0100399 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 +0100400
401 if (cLevelLast < cLevel) cLevelLast = cLevel;
402
Yann Collet99909862016-04-09 16:17:18 +0200403 for (l=cLevel; l <= cLevelLast; l++) {
inikepe9554b72016-03-14 18:10:30 +0100404 BMK_benchMem(srcBuffer, benchedSize,
inikep472638c2016-03-23 12:28:28 +0100405 displayName, l,
inikepe9554b72016-03-14 18:10:30 +0100406 fileSizes, nbFiles,
407 dictBuffer, dictBufferSize, &result);
408 if (g_displayLevel == 1) {
inikepeaba91a2016-03-23 20:30:26 +0100409 if (g_additionalParam)
inikep38654982016-04-21 12:18:47 +0200410 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 +0100411 else
inikep38654982016-04-21 12:18:47 +0200412 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 +0100413 total.cSize += result.cSize;
414 total.cSpeed += result.cSpeed;
415 total.dSpeed += result.dSpeed;
416 total.ratio += result.ratio;
Yann Collet99909862016-04-09 16:17:18 +0200417 } }
418 if (g_displayLevel == 1 && cLevelLast > cLevel) {
inikepe9554b72016-03-14 18:10:30 +0100419 total.cSize /= 1+cLevelLast-cLevel;
420 total.cSpeed /= 1+cLevelLast-cLevel;
421 total.dSpeed /= 1+cLevelLast-cLevel;
422 total.ratio /= 1+cLevelLast-cLevel;
inikep38654982016-04-21 12:18:47 +0200423 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 +0100424 }
Yann Colleted699e62015-12-16 02:37:24 +0100425}
426
427static U64 BMK_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
428{
429 U64 total = 0;
430 unsigned n;
431 for (n=0; n<nbFiles; n++)
inikep69fcd7c2016-04-28 12:23:33 +0200432 total += UTIL_getFileSize(fileNamesTable[n]);
Yann Colleted699e62015-12-16 02:37:24 +0100433 return total;
434}
435
Yann Colleta5b66e32016-03-26 01:48:27 +0100436/*! BMK_loadFiles() :
437 Loads `buffer` with content of files listed within `fileNamesTable`.
438 At most, fills `buffer` entirely */
Yann Collet70611352015-12-16 03:01:03 +0100439static void BMK_loadFiles(void* buffer, size_t bufferSize,
440 size_t* fileSizes,
Yann Colleta5b66e32016-03-26 01:48:27 +0100441 const char** fileNamesTable, unsigned nbFiles)
Yann Colleted699e62015-12-16 02:37:24 +0100442{
inikepc0d5f4e2016-04-13 10:48:04 +0200443 size_t pos = 0, totalSize = 0;
inikepea4ee3e2016-04-25 13:09:06 +0200444 FILE* f;
Yann Collet699b14d2016-03-17 19:37:33 +0100445 unsigned n;
Yann Colletfd416f12016-01-30 03:14:15 +0100446 for (n=0; n<nbFiles; n++) {
inikep69fcd7c2016-04-28 12:23:33 +0200447 U64 fileSize = UTIL_getFileSize(fileNamesTable[n]);
448 if (UTIL_isDirectory(fileNamesTable[n])) {
inikepc0d5f4e2016-04-13 10:48:04 +0200449 DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]);
450 continue;
451 }
inikepea4ee3e2016-04-25 13:09:06 +0200452 f = fopen(fileNamesTable[n], "rb");
Yann Colleted699e62015-12-16 02:37:24 +0100453 if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]);
454 DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[n]);
Yann Colleta5b66e32016-03-26 01:48:27 +0100455 if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */
456 { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
457 if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]);
458 pos += readSize; }
Yann Colleta52c98d2015-12-16 03:12:31 +0100459 fileSizes[n] = (size_t)fileSize;
inikepc0d5f4e2016-04-13 10:48:04 +0200460 totalSize += (size_t)fileSize;
Yann Colleted699e62015-12-16 02:37:24 +0100461 fclose(f);
462 }
inikepc0d5f4e2016-04-13 10:48:04 +0200463
464 if (totalSize == 0) EXM_THROW(12, "no data to bench");
Yann Colleted699e62015-12-16 02:37:24 +0100465}
466
Yann Collet31683c02015-12-18 01:26:48 +0100467static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles,
inikepeaba91a2016-03-23 20:30:26 +0100468 const char* dictFileName, int cLevel, int cLevelLast)
Yann Colleted699e62015-12-16 02:37:24 +0100469{
470 void* srcBuffer;
471 size_t benchedSize;
Yann Collet31683c02015-12-18 01:26:48 +0100472 void* dictBuffer = NULL;
473 size_t dictBufferSize = 0;
474 size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t));
Yann Colleted699e62015-12-16 02:37:24 +0100475 U64 totalSizeToLoad = BMK_getTotalFileSize(fileNamesTable, nbFiles);
476 char mfName[20] = {0};
477 const char* displayName = NULL;
478
Yann Collet31683c02015-12-18 01:26:48 +0100479 if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes");
480
481 /* Load dictionary */
Yann Colletfd416f12016-01-30 03:14:15 +0100482 if (dictFileName != NULL) {
inikep69fcd7c2016-04-28 12:23:33 +0200483 U64 dictFileSize = UTIL_getFileSize(dictFileName);
Yann Collet31683c02015-12-18 01:26:48 +0100484 if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName);
485 dictBufferSize = (size_t)dictFileSize;
486 dictBuffer = malloc(dictBufferSize);
487 if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize);
488 BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1);
489 }
490
Yann Colleted699e62015-12-16 02:37:24 +0100491 /* Memory allocation & restrictions */
492 benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
493 if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
494 if (benchedSize < totalSizeToLoad)
495 DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20));
496 srcBuffer = malloc(benchedSize);
497 if (!srcBuffer) EXM_THROW(12, "not enough memory");
498
499 /* Load input buffer */
Yann Collet70611352015-12-16 03:01:03 +0100500 BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +0100501
502 /* Bench */
503 snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
504 if (nbFiles > 1) displayName = mfName;
505 else displayName = fileNamesTable[0];
506
Yann Collet31683c02015-12-18 01:26:48 +0100507 BMK_benchCLevel(srcBuffer, benchedSize,
inikepeaba91a2016-03-23 20:30:26 +0100508 displayName, cLevel, cLevelLast,
Yann Collet31683c02015-12-18 01:26:48 +0100509 fileSizes, nbFiles,
510 dictBuffer, dictBufferSize);
Yann Collet4856a002015-01-24 01:58:16 +0100511
Yann Colleteeb8ba12015-10-22 16:55:40 +0100512 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100513 free(srcBuffer);
Yann Collet31683c02015-12-18 01:26:48 +0100514 free(dictBuffer);
Yann Collet70611352015-12-16 03:01:03 +0100515 free(fileSizes);
Yann Collet4856a002015-01-24 01:58:16 +0100516}
517
518
inikepeaba91a2016-03-23 20:30:26 +0100519static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility)
Yann Collet4856a002015-01-24 01:58:16 +0100520{
Yann Colleted699e62015-12-16 02:37:24 +0100521 char name[20] = {0};
Yann Collet4856a002015-01-24 01:58:16 +0100522 size_t benchedSize = 10000000;
523 void* srcBuffer = malloc(benchedSize);
Yann Collet4856a002015-01-24 01:58:16 +0100524
Yann Collet4856a002015-01-24 01:58:16 +0100525 /* Memory allocation */
Yann Colleted699e62015-12-16 02:37:24 +0100526 if (!srcBuffer) EXM_THROW(21, "not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100527
528 /* Fill input buffer */
Yann Colletd062f132015-12-01 01:31:17 +0100529 RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100530
531 /* Bench */
Yann Colleted699e62015-12-16 02:37:24 +0100532 snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
inikepeaba91a2016-03-23 20:30:26 +0100533 BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100534
Yann Colleted699e62015-12-16 02:37:24 +0100535 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100536 free(srcBuffer);
Yann Collet4856a002015-01-24 01:58:16 +0100537}
538
539
Yann Collet31683c02015-12-18 01:26:48 +0100540int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
inikepeaba91a2016-03-23 20:30:26 +0100541 const char* dictFileName, int cLevel, int cLevelLast)
Yann Collet4856a002015-01-24 01:58:16 +0100542{
Yann Collet699b14d2016-03-17 19:37:33 +0100543 double const compressibility = (double)g_compressibilityDefault / 100;
Yann Collet4856a002015-01-24 01:58:16 +0100544
545 if (nbFiles == 0)
inikepeaba91a2016-03-23 20:30:26 +0100546 BMK_syntheticTest(cLevel, cLevelLast, compressibility);
Yann Collet4856a002015-01-24 01:58:16 +0100547 else
inikepeaba91a2016-03-23 20:30:26 +0100548 BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast);
Yann Collet4856a002015-01-24 01:58:16 +0100549 return 0;
550}
551