blob: 1468bcb2b29705f8971469897c5ce4c1fbefb66b [file] [log] [blame]
Yann Collet4856a002015-01-24 01:58:16 +01001/*
2 bench.c - Demo module to benchmark open-source compression algorithms
3 Copyright (C) Yann Collet 2012-2015
4
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 :
22 - zstd source repository : https://github.com/Cyan4973/zstd
23 - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c
24*/
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***************************************/
inikep472638c2016-03-23 12:28:28 +010047#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 */
inikep472638c2016-03-23 12:28:28 +010053#include <time.h> /* clock_t, nanosleep, clock, CLOCKS_PER_SEC */
Yann Collet4856a002015-01-24 01:58:16 +010054
Yann Collet27d3dad2016-03-11 13:41:20 +010055/* sleep : posix - windows - others */
56#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
57# include <unistd.h>
inikep472638c2016-03-23 12:28:28 +010058# include <sys/resource.h> /* setpriority */
Yann Collet27d3dad2016-03-11 13:41:20 +010059# define BMK_sleep(s) sleep(s)
inikep472638c2016-03-23 12:28:28 +010060# define mili_sleep(mili) { struct timespec t; t.tv_sec=0; t.tv_nsec=mili*1000000L; nanosleep(&t, NULL); }
61# define setHighPriority() setpriority(PRIO_PROCESS, 0, -20)
Yann Collet27d3dad2016-03-11 13:41:20 +010062#elif defined(_WIN32)
63# include <windows.h>
64# define BMK_sleep(s) Sleep(1000*s)
inikep472638c2016-03-23 12:28:28 +010065# define mili_sleep(mili) Sleep(mili)
66# define setHighPriority() SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
Yann Collet4856a002015-01-24 01:58:16 +010067#else
Yann Collet27d3dad2016-03-11 13:41:20 +010068# define BMK_sleep(s) /* disabled */
inikep472638c2016-03-23 12:28:28 +010069# define mili_sleep(mili) /* disabled */
70# define setHighPriority() /* disabled */
Yann Collet4856a002015-01-24 01:58:16 +010071#endif
72
Yann Colletf3eca252015-10-22 15:31:46 +010073#include "mem.h"
Yann Collet31683c02015-12-18 01:26:48 +010074#include "zstd_static.h"
inikepeaba91a2016-03-23 20:30:26 +010075#include "zstd_internal.h" /* ZSTD_compressBegin_targetSrcSize */
76#include "datagen.h" /* RDG_genBuffer */
Yann Collet4856a002015-01-24 01:58:16 +010077#include "xxhash.h"
78
79
Yann Colletf3eca252015-10-22 15:31:46 +010080/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010081* Compiler specifics
Yann Colletf3eca252015-10-22 15:31:46 +010082***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010083#if !defined(S_ISREG)
84# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
85#endif
86
Yann Colleted699e62015-12-16 02:37:24 +010087#ifdef _MSC_VER
88#define snprintf sprintf_s
89#endif
90
Yann Collet4856a002015-01-24 01:58:16 +010091
Yann Colletf3eca252015-10-22 15:31:46 +010092/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010093* Constants
Yann Colletf3eca252015-10-22 15:31:46 +010094***************************************/
inikep44af12d2016-03-14 15:59:04 +010095#ifndef ZSTD_VERSION
96# define ZSTD_VERSION ""
97#endif
98
Yann Collet27d3dad2016-03-11 13:41:20 +010099#define NBLOOPS 3
Yann Collet699b14d2016-03-17 19:37:33 +0100100#define TIMELOOP_S 1
Yann Collet27d3dad2016-03-11 13:41:20 +0100101#define ACTIVEPERIOD_S 70
102#define COOLPERIOD_S 10
Yann Collet4856a002015-01-24 01:58:16 +0100103
104#define KB *(1 <<10)
105#define MB *(1 <<20)
106#define GB *(1U<<30)
107
Yann Colletd062f132015-12-01 01:31:17 +0100108static 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 +0100109
110static U32 g_compressibilityDefault = 50;
Yann Collet4856a002015-01-24 01:58:16 +0100111
112
Yann Colletf3eca252015-10-22 15:31:46 +0100113/* *************************************
Yann Colleted699e62015-12-16 02:37:24 +0100114* console display
Yann Colletf3eca252015-10-22 15:31:46 +0100115***************************************/
Yann Colleted699e62015-12-16 02:37:24 +0100116#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
117#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
118static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
119
120
121/* *************************************
122* Exceptions
123***************************************/
124#ifndef DEBUG
125# define DEBUG 0
126#endif
127#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
128#define EXM_THROW(error, ...) \
129{ \
130 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
131 DISPLAYLEVEL(1, "Error %i : ", error); \
132 DISPLAYLEVEL(1, __VA_ARGS__); \
133 DISPLAYLEVEL(1, "\n"); \
134 exit(error); \
135}
Yann Collet4856a002015-01-24 01:58:16 +0100136
137
Yann Colletf3eca252015-10-22 15:31:46 +0100138/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100139* Benchmark Parameters
Yann Colletf3eca252015-10-22 15:31:46 +0100140***************************************/
Yann Collet1d1ae402016-03-17 19:51:02 +0100141static U32 g_nbIterations = NBLOOPS;
Yann Collet1c00dc32015-10-21 08:22:25 +0100142static size_t g_blockSize = 0;
inikepeaba91a2016-03-23 20:30:26 +0100143int g_additionalParam = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100144
inikep4e26bb62016-03-14 12:48:51 +0100145void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; }
146
inikepeaba91a2016-03-23 20:30:26 +0100147void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; }
148
Yann Collet1d1ae402016-03-17 19:51:02 +0100149void BMK_SetNbIterations(unsigned nbLoops)
Yann Collet4856a002015-01-24 01:58:16 +0100150{
Yann Collet1d1ae402016-03-17 19:51:02 +0100151 g_nbIterations = nbLoops;
inikep2872b6f2016-03-22 14:38:34 +0100152 DISPLAYLEVEL(2, "- %i iterations -\n", g_nbIterations);
Yann Collet4856a002015-01-24 01:58:16 +0100153}
154
Yann Collet1c00dc32015-10-21 08:22:25 +0100155void BMK_SetBlockSize(size_t blockSize)
156{
157 g_blockSize = blockSize;
inikep4e26bb62016-03-14 12:48:51 +0100158 DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10));
Yann Collet1c00dc32015-10-21 08:22:25 +0100159}
160
Yann Collet4856a002015-01-24 01:58:16 +0100161
Yann Colletf3eca252015-10-22 15:31:46 +0100162/* ********************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100163* Private functions
Yann Colletf3eca252015-10-22 15:31:46 +0100164**********************************************************/
Yann Collet699b14d2016-03-17 19:37:33 +0100165static clock_t BMK_clockSpan( clock_t clockStart )
Yann Collet4856a002015-01-24 01:58:16 +0100166{
Yann Collet699b14d2016-03-17 19:37:33 +0100167 return clock() - clockStart; /* works even if overflow, span limited to <= ~30mn */
Yann Collet4856a002015-01-24 01:58:16 +0100168}
169
Yann Collet4856a002015-01-24 01:58:16 +0100170
Yann Colleted699e62015-12-16 02:37:24 +0100171static U64 BMK_getFileSize(const char* infilename)
172{
173 int r;
174#if defined(_MSC_VER)
175 struct _stat64 statbuf;
176 r = _stat64(infilename, &statbuf);
177#else
178 struct stat statbuf;
179 r = stat(infilename, &statbuf);
180#endif
181 if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
182 return (U64)statbuf.st_size;
183}
184
Yann Collet4856a002015-01-24 01:58:16 +0100185
Yann Colletf3eca252015-10-22 15:31:46 +0100186/* ********************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100187* Bench functions
Yann Colletf3eca252015-10-22 15:31:46 +0100188**********************************************************/
Yann Collet1c00dc32015-10-21 08:22:25 +0100189typedef struct
Yann Collet4856a002015-01-24 01:58:16 +0100190{
Yann Colleted699e62015-12-16 02:37:24 +0100191 const char* srcPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100192 size_t srcSize;
193 char* cPtr;
194 size_t cRoom;
195 size_t cSize;
196 char* resPtr;
197 size_t resSize;
198} blockParam_t;
199
inikep4e26bb62016-03-14 12:48:51 +0100200typedef struct
201{
inikepc034b732016-03-14 13:13:42 +0100202 double ratio;
inikep4e26bb62016-03-14 12:48:51 +0100203 size_t cSize;
inikepc034b732016-03-14 13:13:42 +0100204 double cSpeed;
205 double dSpeed;
inikep4e26bb62016-03-14 12:48:51 +0100206} benchResult_t;
207
208
Yann Colletbe2010e2015-10-31 12:57:14 +0100209#define MIN(a,b) ((a)<(b) ? (a) : (b))
Yann Collet2ce49232016-02-02 14:36:49 +0100210#define MAX(a,b) ((a)>(b) ? (a) : (b))
Yann Collet1c00dc32015-10-21 08:22:25 +0100211
Yann Colleted699e62015-12-16 02:37:24 +0100212static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
inikep472638c2016-03-23 12:28:28 +0100213 const char* displayName, int cLevel,
Yann Collet31683c02015-12-18 01:26:48 +0100214 const size_t* fileSizes, U32 nbFiles,
inikep4e26bb62016-03-14 12:48:51 +0100215 const void* dictBuffer, size_t dictBufferSize, benchResult_t *result)
Yann Collet1c00dc32015-10-21 08:22:25 +0100216{
Yann Collet2c7ac7c2015-11-04 17:52:18 +0100217 const size_t blockSize = (g_blockSize ? g_blockSize : srcSize) + (!srcSize); /* avoid div by 0 */
Yann Colleted699e62015-12-16 02:37:24 +0100218 const U32 maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
219 blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t));
Yann Collet367060b2015-12-17 00:07:10 +0100220 const size_t maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */
Yann Collet1c00dc32015-10-21 08:22:25 +0100221 void* const compressedBuffer = malloc(maxCompressedSize);
222 void* const resultBuffer = malloc(srcSize);
Yann Collet2630a5e2016-01-14 19:13:22 +0100223 ZSTD_CCtx* refCtx = ZSTD_createCCtx();
Yann Collet31683c02015-12-18 01:26:48 +0100224 ZSTD_CCtx* ctx = ZSTD_createCCtx();
Yann Collet7b51a292016-01-26 15:58:49 +0100225 ZSTD_DCtx* refDCtx = ZSTD_createDCtx();
Yann Collet31683c02015-12-18 01:26:48 +0100226 ZSTD_DCtx* dctx = ZSTD_createDCtx();
Yann Colletc776c462015-10-29 19:10:54 +0100227
Yann Colletde406ee2016-03-20 15:46:10 +0100228 U64 const crcOrig = XXH64(srcBuffer, srcSize, 0);
229 U32 nbBlocks;
230
231 /* checks */
Yann Collet7b51a292016-01-26 15:58:49 +0100232 if (!compressedBuffer || !resultBuffer || !blockTable || !refCtx || !ctx || !refDCtx || !dctx)
Yann Colleted699e62015-12-16 02:37:24 +0100233 EXM_THROW(31, "not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100234
Yann Collet4856a002015-01-24 01:58:16 +0100235 /* init */
236 if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */
237
Yann Collet1c00dc32015-10-21 08:22:25 +0100238 /* Init blockTable data */
Yann Colletde406ee2016-03-20 15:46:10 +0100239 { const char* srcPtr = (const char*)srcBuffer;
Yann Collet1c00dc32015-10-21 08:22:25 +0100240 char* cPtr = (char*)compressedBuffer;
241 char* resPtr = (char*)resultBuffer;
Yann Collet699b14d2016-03-17 19:37:33 +0100242 U32 fileNb;
Yann Colletde406ee2016-03-20 15:46:10 +0100243 for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
Yann Collet70611352015-12-16 03:01:03 +0100244 size_t remaining = fileSizes[fileNb];
Yann Collet699b14d2016-03-17 19:37:33 +0100245 U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize);
246 U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
Yann Colletfd416f12016-01-30 03:14:15 +0100247 for ( ; nbBlocks<blockEnd; nbBlocks++) {
Yann Colletde406ee2016-03-20 15:46:10 +0100248 size_t const thisBlockSize = MIN(remaining, blockSize);
Yann Colleted699e62015-12-16 02:37:24 +0100249 blockTable[nbBlocks].srcPtr = srcPtr;
250 blockTable[nbBlocks].cPtr = cPtr;
251 blockTable[nbBlocks].resPtr = resPtr;
252 blockTable[nbBlocks].srcSize = thisBlockSize;
253 blockTable[nbBlocks].cRoom = ZSTD_compressBound(thisBlockSize);
254 srcPtr += thisBlockSize;
255 cPtr += blockTable[nbBlocks].cRoom;
256 resPtr += thisBlockSize;
257 remaining -= thisBlockSize;
Yann Colletfd416f12016-01-30 03:14:15 +0100258 } } }
Yann Collet1c00dc32015-10-21 08:22:25 +0100259
Yann Collet4856a002015-01-24 01:58:16 +0100260 /* warmimg up memory */
Yann Colletd062f132015-12-01 01:31:17 +0100261 RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
Yann Collet4856a002015-01-24 01:58:16 +0100262
263 /* Bench */
Yann Colletde406ee2016-03-20 15:46:10 +0100264 { size_t cSize = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100265 double fastestC = 100000000., fastestD = 100000000.;
266 double ratio = 0.;
267 U64 crcCheck = 0;
Yann Collet699b14d2016-03-17 19:37:33 +0100268 clock_t coolTime = clock();
Yann Colletde406ee2016-03-20 15:46:10 +0100269 U32 testNb;
Yann Collet4856a002015-01-24 01:58:16 +0100270
inikep4e26bb62016-03-14 12:48:51 +0100271 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Colletde406ee2016-03-20 15:46:10 +0100272 for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) {
Yann Collet4856a002015-01-24 01:58:16 +0100273 int nbLoops;
Yann Collet699b14d2016-03-17 19:37:33 +0100274 clock_t clockStart, clockSpan;
Yann Collet1d1ae402016-03-17 19:51:02 +0100275 clock_t const clockLoop = g_nbIterations ? TIMELOOP_S * CLOCKS_PER_SEC : 10;
Yann Collet4856a002015-01-24 01:58:16 +0100276
Yann Collet27d3dad2016-03-11 13:41:20 +0100277 /* overheat protection */
Yann Collet699b14d2016-03-17 19:37:33 +0100278 if (BMK_clockSpan(coolTime) > ACTIVEPERIOD_S * CLOCKS_PER_SEC) {
Yann Collet27d3dad2016-03-11 13:41:20 +0100279 DISPLAY("\rcooling down ... \r");
280 BMK_sleep(COOLPERIOD_S);
Yann Collet699b14d2016-03-17 19:37:33 +0100281 coolTime = clock();
Yann Collet27d3dad2016-03-11 13:41:20 +0100282 }
Yann Collet4856a002015-01-24 01:58:16 +0100283
284 /* Compression */
inikep2872b6f2016-03-22 14:38:34 +0100285 DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->\r", testNb, displayName, (U32)srcSize);
Yann Collet699b14d2016-03-17 19:37:33 +0100286 memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100287
inikep472638c2016-03-23 12:28:28 +0100288 mili_sleep(1); /* give processor time to other processes */
Yann Collet699b14d2016-03-17 19:37:33 +0100289 clockStart = clock();
290 while (clock() == clockStart);
291 clockStart = clock();
Yann Colletde406ee2016-03-20 15:46:10 +0100292
293 for (nbLoops = 0 ; BMK_clockSpan(clockStart) < clockLoop ; nbLoops++) {
294 U32 blockNb;
inikepeaba91a2016-03-23 20:30:26 +0100295 ZSTD_compressBegin_targetSrcSize(refCtx, dictBuffer, dictBufferSize, blockSize, cLevel);
Yann Colletfb810d62016-01-28 00:18:06 +0100296 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
Yann Colletde406ee2016-03-20 15:46:10 +0100297 size_t const rSize = ZSTD_compress_usingPreparedCCtx(ctx, refCtx,
Yann Colletfd416f12016-01-30 03:14:15 +0100298 blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
299 blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize);
300 if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingPreparedCCtx() failed : %s", ZSTD_getErrorName(rSize));
Yann Collet2630a5e2016-01-14 19:13:22 +0100301 blockTable[blockNb].cSize = rSize;
Yann Colletde406ee2016-03-20 15:46:10 +0100302 } }
Yann Collet699b14d2016-03-17 19:37:33 +0100303 clockSpan = BMK_clockSpan(clockStart);
Yann Collet4856a002015-01-24 01:58:16 +0100304
Yann Collet699b14d2016-03-17 19:37:33 +0100305 if ((double)clockSpan < fastestC*nbLoops) fastestC = (double)clockSpan / nbLoops;
Yann Collet1c00dc32015-10-21 08:22:25 +0100306 cSize = 0;
Yann Colletde406ee2016-03-20 15:46:10 +0100307 { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; }
Yann Collet2acb5d32015-10-29 16:49:43 +0100308 ratio = (double)srcSize / (double)cSize;
inikep2872b6f2016-03-22 14:38:34 +0100309 DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r",
Yann Colletde406ee2016-03-20 15:46:10 +0100310 testNb, displayName, (U32)srcSize, (U32)cSize, ratio,
Yann Collet699b14d2016-03-17 19:37:33 +0100311 (double)srcSize / 1000000. / (fastestC / CLOCKS_PER_SEC) );
Yann Collet4856a002015-01-24 01:58:16 +0100312
Yann Collete93d6ce2016-01-31 00:58:06 +0100313#if 1
Yann Collet4856a002015-01-24 01:58:16 +0100314 /* Decompression */
Yann Collet7b51a292016-01-26 15:58:49 +0100315 memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100316
inikep472638c2016-03-23 12:28:28 +0100317 mili_sleep(1); /* give processor time to other processes */
Yann Collet699b14d2016-03-17 19:37:33 +0100318 clockStart = clock();
319 while (clock() == clockStart);
320 clockStart = clock();
Yann Collet7b51a292016-01-26 15:58:49 +0100321
Yann Colletde406ee2016-03-20 15:46:10 +0100322 for (nbLoops = 0 ; BMK_clockSpan(clockStart) < clockLoop ; nbLoops++) {
323 U32 blockNb;
Yann Collete93d6ce2016-01-31 00:58:06 +0100324 ZSTD_decompressBegin_usingDict(refDCtx, dictBuffer, dictBufferSize);
Yann Colletb923f652016-01-26 03:14:20 +0100325 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
Yann Collete93d6ce2016-01-31 00:58:06 +0100326 size_t regenSize = ZSTD_decompress_usingPreparedDCtx(dctx, refDCtx,
Yann Collet7b51a292016-01-26 15:58:49 +0100327 blockTable[blockNb].resPtr, blockTable[blockNb].srcSize,
328 blockTable[blockNb].cPtr, blockTable[blockNb].cSize);
Yann Collete93d6ce2016-01-31 00:58:06 +0100329 if (ZSTD_isError(regenSize)) {
330 DISPLAY("ZSTD_decompress_usingPreparedDCtx() failed on block %u : %s",
Yann Colletfb810d62016-01-28 00:18:06 +0100331 blockNb, ZSTD_getErrorName(regenSize));
Yann Collete93d6ce2016-01-31 00:58:06 +0100332 goto _findError;
333 }
Yann Collet7b51a292016-01-26 15:58:49 +0100334 blockTable[blockNb].resSize = regenSize;
335 } }
336
Yann Collet699b14d2016-03-17 19:37:33 +0100337 clockSpan = BMK_clockSpan(clockStart);
338 if ((double)clockSpan < fastestD*nbLoops) fastestD = (double)clockSpan / nbLoops;
inikep2872b6f2016-03-22 14:38:34 +0100339 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 +0100340 testNb, displayName, (U32)srcSize, (U32)cSize, ratio,
Yann Collet699b14d2016-03-17 19:37:33 +0100341 (double)srcSize / 1000000. / (fastestC / CLOCKS_PER_SEC),
342 (double)srcSize / 1000000. / (fastestD / CLOCKS_PER_SEC) );
Yann Collet4856a002015-01-24 01:58:16 +0100343
344 /* CRC Checking */
Yann Collete93d6ce2016-01-31 00:58:06 +0100345_findError:
Yann Collet4856a002015-01-24 01:58:16 +0100346 crcCheck = XXH64(resultBuffer, srcSize, 0);
Yann Collet7b51a292016-01-26 15:58:49 +0100347 if (crcOrig!=crcCheck) {
Yann Collet03a6dab2016-01-21 02:21:17 +0100348 size_t u;
Yann Colleted699e62015-12-16 02:37:24 +0100349 DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n", displayName, (unsigned)crcOrig, (unsigned)crcCheck);
Yann Collet7b51a292016-01-26 15:58:49 +0100350 for (u=0; u<srcSize; u++) {
351 if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) {
Yann Collet59d1f792016-01-23 19:28:41 +0100352 U32 segNb, bNb, pos;
Yann Collet03a6dab2016-01-21 02:21:17 +0100353 size_t bacc = 0;
354 printf("Decoding error at pos %u ", (U32)u);
Yann Collet7b51a292016-01-26 15:58:49 +0100355 for (segNb = 0; segNb < nbBlocks; segNb++) {
Yann Collet59d1f792016-01-23 19:28:41 +0100356 if (bacc + blockTable[segNb].srcSize > u) break;
357 bacc += blockTable[segNb].srcSize;
Yann Collet03a6dab2016-01-21 02:21:17 +0100358 }
Yann Collet59d1f792016-01-23 19:28:41 +0100359 pos = (U32)(u - bacc);
360 bNb = pos / (128 KB);
Yann Colletfb810d62016-01-28 00:18:06 +0100361 printf("(block %u, sub %u, pos %u) \n", segNb, bNb, pos);
Yann Collet4856a002015-01-24 01:58:16 +0100362 break;
Yann Colletfb810d62016-01-28 00:18:06 +0100363 }
364 if (u==srcSize-1) { /* should never happen */
365 printf("no difference detected\n");
Yann Collet7b51a292016-01-26 15:58:49 +0100366 } }
Yann Collet4856a002015-01-24 01:58:16 +0100367 break;
Yann Colletde406ee2016-03-20 15:46:10 +0100368 } /* if (crcOrig!=crcCheck) */
Yann Collete8c6bb12015-07-26 00:23:57 +0100369#endif
Yann Colletde406ee2016-03-20 15:46:10 +0100370 } /* for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) */
Yann Collet4856a002015-01-24 01:58:16 +0100371
inikep2872b6f2016-03-22 14:38:34 +0100372 if (crcOrig == crcCheck) {
inikep4e26bb62016-03-14 12:48:51 +0100373 result->ratio = ratio;
374 result->cSize = cSize;
inikep2872b6f2016-03-22 14:38:34 +0100375 result->cSpeed = (double)srcSize / 1000000. / (fastestC / CLOCKS_PER_SEC);
376 result->dSpeed = (double)srcSize / 1000000. / (fastestD / CLOCKS_PER_SEC);
inikep4e26bb62016-03-14 12:48:51 +0100377 }
inikep2872b6f2016-03-22 14:38:34 +0100378 DISPLAYLEVEL(2, "%2i#\n", cLevel);
Yann Colletde406ee2016-03-20 15:46:10 +0100379 } /* Bench */
Yann Collet4856a002015-01-24 01:58:16 +0100380
Yann Colleted699e62015-12-16 02:37:24 +0100381 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100382 free(compressedBuffer);
383 free(resultBuffer);
Yann Collet2630a5e2016-01-14 19:13:22 +0100384 ZSTD_freeCCtx(refCtx);
Yann Collet31683c02015-12-18 01:26:48 +0100385 ZSTD_freeCCtx(ctx);
Yann Collet7b51a292016-01-26 15:58:49 +0100386 ZSTD_freeDCtx(refDCtx);
Yann Collet31683c02015-12-18 01:26:48 +0100387 ZSTD_freeDCtx(dctx);
Yann Collet4856a002015-01-24 01:58:16 +0100388 return 0;
389}
390
391
Yann Collet4856a002015-01-24 01:58:16 +0100392static size_t BMK_findMaxMem(U64 requiredMem)
393{
Yann Colletde406ee2016-03-20 15:46:10 +0100394 size_t const step = 64 MB;
Yann Collet4856a002015-01-24 01:58:16 +0100395 BYTE* testmem = NULL;
396
397 requiredMem = (((requiredMem >> 26) + 1) << 26);
Yann Colletde406ee2016-03-20 15:46:10 +0100398 requiredMem += step;
Yann Collet050efba2015-11-03 09:49:30 +0100399 if (requiredMem > maxMemory) requiredMem = maxMemory;
Yann Collet4856a002015-01-24 01:58:16 +0100400
Yann Colletde406ee2016-03-20 15:46:10 +0100401 do {
Yann Collet4856a002015-01-24 01:58:16 +0100402 testmem = (BYTE*)malloc((size_t)requiredMem);
Yann Colletde406ee2016-03-20 15:46:10 +0100403 requiredMem -= step;
404 } while (!testmem);
405
Yann Collet4856a002015-01-24 01:58:16 +0100406 free(testmem);
Yann Colletde406ee2016-03-20 15:46:10 +0100407 return (size_t)(requiredMem);
Yann Collet4856a002015-01-24 01:58:16 +0100408}
409
Yann Colleted699e62015-12-16 02:37:24 +0100410static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize,
inikepeaba91a2016-03-23 20:30:26 +0100411 const char* displayName, int cLevel, int cLevelLast,
Yann Collet31683c02015-12-18 01:26:48 +0100412 const size_t* fileSizes, unsigned nbFiles,
413 const void* dictBuffer, size_t dictBufferSize)
Yann Collet4856a002015-01-24 01:58:16 +0100414{
inikep4e26bb62016-03-14 12:48:51 +0100415 benchResult_t result, total;
inikepe9554b72016-03-14 18:10:30 +0100416 int l;
inikep4e26bb62016-03-14 12:48:51 +0100417
inikep472638c2016-03-23 12:28:28 +0100418 setHighPriority();
419
inikepe9554b72016-03-14 18:10:30 +0100420 const char* pch = strrchr(displayName, '\\'); /* Windows */
421 if (!pch) pch = strrchr(displayName, '/'); /* Linux */
422 if (pch) displayName = pch+1;
inikep4e26bb62016-03-14 12:48:51 +0100423
inikepe9554b72016-03-14 18:10:30 +0100424 memset(&result, 0, sizeof(result));
425 memset(&total, 0, sizeof(total));
inikep5fdd0b42016-03-14 19:51:11 +0100426
inikepeaba91a2016-03-23 20:30:26 +0100427 if (g_displayLevel == 1 && !g_additionalParam)
inikep2872b6f2016-03-22 14:38:34 +0100428 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 +0100429
430 if (cLevelLast < cLevel) cLevelLast = cLevel;
431
432 for (l=cLevel; l <= cLevelLast; l++) {
433 BMK_benchMem(srcBuffer, benchedSize,
inikep472638c2016-03-23 12:28:28 +0100434 displayName, l,
inikepe9554b72016-03-14 18:10:30 +0100435 fileSizes, nbFiles,
436 dictBuffer, dictBufferSize, &result);
437 if (g_displayLevel == 1) {
inikepeaba91a2016-03-23 20:30:26 +0100438 if (g_additionalParam)
439 DISPLAY("%-3i%11i (%5.3f) %6.1f 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 +0100440 else
441 DISPLAY("%-3i%11i (%5.3f) %6.1f 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 +0100442 total.cSize += result.cSize;
443 total.cSpeed += result.cSpeed;
444 total.dSpeed += result.dSpeed;
445 total.ratio += result.ratio;
inikep4e26bb62016-03-14 12:48:51 +0100446 }
Yann Colletc776c462015-10-29 19:10:54 +0100447 }
inikepe9554b72016-03-14 18:10:30 +0100448 if (g_displayLevel == 1 && cLevelLast > cLevel)
449 {
450 total.cSize /= 1+cLevelLast-cLevel;
451 total.cSpeed /= 1+cLevelLast-cLevel;
452 total.dSpeed /= 1+cLevelLast-cLevel;
453 total.ratio /= 1+cLevelLast-cLevel;
454 DISPLAY("avg%11i (%5.3f) %6.1f MB/s %6.1f MB/s %s\n", (int)total.cSize, total.ratio, total.cSpeed, total.dSpeed, displayName);
455 }
Yann Colleted699e62015-12-16 02:37:24 +0100456}
457
458static U64 BMK_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
459{
460 U64 total = 0;
461 unsigned n;
462 for (n=0; n<nbFiles; n++)
463 total += BMK_getFileSize(fileNamesTable[n]);
464 return total;
465}
466
Yann Collet70611352015-12-16 03:01:03 +0100467static void BMK_loadFiles(void* buffer, size_t bufferSize,
468 size_t* fileSizes,
Yann Collet699b14d2016-03-17 19:37:33 +0100469 const char** fileNamesTable, unsigned const nbFiles)
Yann Colleted699e62015-12-16 02:37:24 +0100470{
Yann Colleted699e62015-12-16 02:37:24 +0100471 size_t pos = 0;
Yann Colleted699e62015-12-16 02:37:24 +0100472
Yann Collet699b14d2016-03-17 19:37:33 +0100473 unsigned n;
Yann Colletfd416f12016-01-30 03:14:15 +0100474 for (n=0; n<nbFiles; n++) {
Yann Colleted699e62015-12-16 02:37:24 +0100475 size_t readSize;
476 U64 fileSize = BMK_getFileSize(fileNamesTable[n]);
477 FILE* f = fopen(fileNamesTable[n], "rb");
478 if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]);
479 DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[n]);
480 if (fileSize > bufferSize-pos) fileSize = bufferSize-pos;
Yann Collet699b14d2016-03-17 19:37:33 +0100481 readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
Yann Colleted699e62015-12-16 02:37:24 +0100482 if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]);
483 pos += readSize;
Yann Colleta52c98d2015-12-16 03:12:31 +0100484 fileSizes[n] = (size_t)fileSize;
Yann Colleted699e62015-12-16 02:37:24 +0100485 fclose(f);
486 }
487}
488
Yann Collet31683c02015-12-18 01:26:48 +0100489static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles,
inikepeaba91a2016-03-23 20:30:26 +0100490 const char* dictFileName, int cLevel, int cLevelLast)
Yann Colleted699e62015-12-16 02:37:24 +0100491{
492 void* srcBuffer;
493 size_t benchedSize;
Yann Collet31683c02015-12-18 01:26:48 +0100494 void* dictBuffer = NULL;
495 size_t dictBufferSize = 0;
496 size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t));
Yann Colleted699e62015-12-16 02:37:24 +0100497 U64 totalSizeToLoad = BMK_getTotalFileSize(fileNamesTable, nbFiles);
498 char mfName[20] = {0};
499 const char* displayName = NULL;
500
Yann Collet31683c02015-12-18 01:26:48 +0100501 if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes");
502
503 /* Load dictionary */
Yann Colletfd416f12016-01-30 03:14:15 +0100504 if (dictFileName != NULL) {
Yann Collet31683c02015-12-18 01:26:48 +0100505 U64 dictFileSize = BMK_getFileSize(dictFileName);
506 if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName);
507 dictBufferSize = (size_t)dictFileSize;
508 dictBuffer = malloc(dictBufferSize);
509 if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize);
510 BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1);
511 }
512
Yann Colleted699e62015-12-16 02:37:24 +0100513 /* Memory allocation & restrictions */
514 benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
515 if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
516 if (benchedSize < totalSizeToLoad)
517 DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20));
518 srcBuffer = malloc(benchedSize);
519 if (!srcBuffer) EXM_THROW(12, "not enough memory");
520
521 /* Load input buffer */
Yann Collet70611352015-12-16 03:01:03 +0100522 BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +0100523
524 /* Bench */
525 snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
526 if (nbFiles > 1) displayName = mfName;
527 else displayName = fileNamesTable[0];
528
Yann Collet31683c02015-12-18 01:26:48 +0100529 BMK_benchCLevel(srcBuffer, benchedSize,
inikepeaba91a2016-03-23 20:30:26 +0100530 displayName, cLevel, cLevelLast,
Yann Collet31683c02015-12-18 01:26:48 +0100531 fileSizes, nbFiles,
532 dictBuffer, dictBufferSize);
Yann Collet4856a002015-01-24 01:58:16 +0100533
Yann Colleteeb8ba12015-10-22 16:55:40 +0100534 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100535 free(srcBuffer);
Yann Collet31683c02015-12-18 01:26:48 +0100536 free(dictBuffer);
Yann Collet70611352015-12-16 03:01:03 +0100537 free(fileSizes);
Yann Collet4856a002015-01-24 01:58:16 +0100538}
539
540
inikepeaba91a2016-03-23 20:30:26 +0100541static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility)
Yann Collet4856a002015-01-24 01:58:16 +0100542{
Yann Colleted699e62015-12-16 02:37:24 +0100543 char name[20] = {0};
Yann Collet4856a002015-01-24 01:58:16 +0100544 size_t benchedSize = 10000000;
545 void* srcBuffer = malloc(benchedSize);
Yann Collet4856a002015-01-24 01:58:16 +0100546
Yann Collet4856a002015-01-24 01:58:16 +0100547 /* Memory allocation */
Yann Colleted699e62015-12-16 02:37:24 +0100548 if (!srcBuffer) EXM_THROW(21, "not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100549
550 /* Fill input buffer */
Yann Colletd062f132015-12-01 01:31:17 +0100551 RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100552
553 /* Bench */
Yann Colleted699e62015-12-16 02:37:24 +0100554 snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
inikepeaba91a2016-03-23 20:30:26 +0100555 BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100556
Yann Colleted699e62015-12-16 02:37:24 +0100557 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100558 free(srcBuffer);
Yann Collet4856a002015-01-24 01:58:16 +0100559}
560
561
Yann Collet31683c02015-12-18 01:26:48 +0100562int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
inikepeaba91a2016-03-23 20:30:26 +0100563 const char* dictFileName, int cLevel, int cLevelLast)
Yann Collet4856a002015-01-24 01:58:16 +0100564{
Yann Collet699b14d2016-03-17 19:37:33 +0100565 double const compressibility = (double)g_compressibilityDefault / 100;
Yann Collet4856a002015-01-24 01:58:16 +0100566
567 if (nbFiles == 0)
inikepeaba91a2016-03-23 20:30:26 +0100568 BMK_syntheticTest(cLevel, cLevelLast, compressibility);
Yann Collet4856a002015-01-24 01:58:16 +0100569 else
inikepeaba91a2016-03-23 20:30:26 +0100570 BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast);
Yann Collet4856a002015-01-24 01:58:16 +0100571 return 0;
572}
573