blob: 8470ed3245267167a09708bf1b243d1bbe0a6b62 [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 */
Yann Collet99909862016-04-09 16:17:18 +020053#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)
inikep4c12f232016-03-29 14:52:13 +020060# define mili_sleep(mili) { struct timespec t; t.tv_sec=0; t.tv_nsec=mili*1000000ULL; nanosleep(&t, NULL); }
inikep19bd48f2016-04-04 12:10:00 +020061# define SET_HIGH_PRIORITY 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)
inikep19bd48f2016-04-04 12:10:00 +020066# define SET_HIGH_PRIORITY 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 */
inikep19bd48f2016-04-04 12:10:00 +020070# define SET_HIGH_PRIORITY /* disabled */
Yann Collet4856a002015-01-24 01:58:16 +010071#endif
72
inikep1eeddde2016-04-08 16:55:17 +020073#if !defined(_WIN32)
inikep4c12f232016-03-29 14:52:13 +020074 typedef clock_t BMK_time_t;
inikep1c556a32016-03-30 10:59:48 +020075# define BMK_initTimer(ticksPerSecond) ticksPerSecond=0
inikep4c12f232016-03-29 14:52:13 +020076# define BMK_getTime(x) x = clock()
77# define BMK_getSpanTimeMicro(ticksPerSecond, clockStart, clockEnd) (1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC)
78# define BMK_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) (1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC)
inikep1eeddde2016-04-08 16:55:17 +020079#else
inikep4c12f232016-03-29 14:52:13 +020080 typedef LARGE_INTEGER BMK_time_t;
inikep4c12f232016-03-29 14:52:13 +020081# define BMK_initTimer(x) if (!QueryPerformanceFrequency(&x)) { fprintf(stderr, "ERROR: QueryPerformance not present\n"); }
82# define BMK_getTime(x) QueryPerformanceCounter(&x)
83# define BMK_getSpanTimeMicro(ticksPerSecond, clockStart, clockEnd) (1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart)
84# define BMK_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) (1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart)
inikep4c12f232016-03-29 14:52:13 +020085#endif
86
Yann Colletf3eca252015-10-22 15:31:46 +010087#include "mem.h"
Yann Collet31683c02015-12-18 01:26:48 +010088#include "zstd_static.h"
inikepeaba91a2016-03-23 20:30:26 +010089#include "datagen.h" /* RDG_genBuffer */
Yann Collet4856a002015-01-24 01:58:16 +010090#include "xxhash.h"
91
92
Yann Colletf3eca252015-10-22 15:31:46 +010093/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010094* Compiler specifics
Yann Colletf3eca252015-10-22 15:31:46 +010095***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010096#if !defined(S_ISREG)
97# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
98#endif
99
Yann Collet99909862016-04-09 16:17:18 +0200100#if defined(_MSC_VER)
101# define snprintf sprintf_s
102#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L))
103 /* part of <stdio.h> */
104#else
105 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 +0100106#endif
107
Yann Collet4856a002015-01-24 01:58:16 +0100108
Yann Colletf3eca252015-10-22 15:31:46 +0100109/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100110* Constants
Yann Colletf3eca252015-10-22 15:31:46 +0100111***************************************/
inikep44af12d2016-03-14 15:59:04 +0100112#ifndef ZSTD_VERSION
113# define ZSTD_VERSION ""
114#endif
115
Yann Collet27d3dad2016-03-11 13:41:20 +0100116#define NBLOOPS 3
Yann Collet699b14d2016-03-17 19:37:33 +0100117#define TIMELOOP_S 1
Yann Collet27d3dad2016-03-11 13:41:20 +0100118#define ACTIVEPERIOD_S 70
119#define COOLPERIOD_S 10
Yann Collet4856a002015-01-24 01:58:16 +0100120
121#define KB *(1 <<10)
122#define MB *(1 <<20)
123#define GB *(1U<<30)
124
Yann Colletd062f132015-12-01 01:31:17 +0100125static 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 +0100126
127static U32 g_compressibilityDefault = 50;
Yann Collet4856a002015-01-24 01:58:16 +0100128
129
Yann Colletf3eca252015-10-22 15:31:46 +0100130/* *************************************
Yann Colleted699e62015-12-16 02:37:24 +0100131* console display
Yann Colletf3eca252015-10-22 15:31:46 +0100132***************************************/
Yann Colleted699e62015-12-16 02:37:24 +0100133#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
134#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
135static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
136
137
138/* *************************************
139* Exceptions
140***************************************/
141#ifndef DEBUG
142# define DEBUG 0
143#endif
144#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
145#define EXM_THROW(error, ...) \
146{ \
147 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
148 DISPLAYLEVEL(1, "Error %i : ", error); \
149 DISPLAYLEVEL(1, __VA_ARGS__); \
150 DISPLAYLEVEL(1, "\n"); \
151 exit(error); \
152}
Yann Collet4856a002015-01-24 01:58:16 +0100153
154
Yann Colletf3eca252015-10-22 15:31:46 +0100155/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100156* Benchmark Parameters
Yann Colletf3eca252015-10-22 15:31:46 +0100157***************************************/
Yann Collet1d1ae402016-03-17 19:51:02 +0100158static U32 g_nbIterations = NBLOOPS;
Yann Collet1c00dc32015-10-21 08:22:25 +0100159static size_t g_blockSize = 0;
inikepeaba91a2016-03-23 20:30:26 +0100160int g_additionalParam = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100161
inikep4e26bb62016-03-14 12:48:51 +0100162void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; }
163
inikepeaba91a2016-03-23 20:30:26 +0100164void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; }
165
Yann Collet1d1ae402016-03-17 19:51:02 +0100166void BMK_SetNbIterations(unsigned nbLoops)
Yann Collet4856a002015-01-24 01:58:16 +0100167{
Yann Collet1d1ae402016-03-17 19:51:02 +0100168 g_nbIterations = nbLoops;
inikep2872b6f2016-03-22 14:38:34 +0100169 DISPLAYLEVEL(2, "- %i iterations -\n", g_nbIterations);
Yann Collet4856a002015-01-24 01:58:16 +0100170}
171
Yann Collet1c00dc32015-10-21 08:22:25 +0100172void BMK_SetBlockSize(size_t blockSize)
173{
174 g_blockSize = blockSize;
inikep4e26bb62016-03-14 12:48:51 +0100175 DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10));
Yann Collet1c00dc32015-10-21 08:22:25 +0100176}
177
Yann Collet4856a002015-01-24 01:58:16 +0100178
Yann Colletf3eca252015-10-22 15:31:46 +0100179/* ********************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100180* Private functions
Yann Colletf3eca252015-10-22 15:31:46 +0100181**********************************************************/
inikep4c12f232016-03-29 14:52:13 +0200182/* returns time span in microseconds */
183static U64 BMK_clockSpan( BMK_time_t clockStart, BMK_time_t ticksPerSecond )
Yann Collet4856a002015-01-24 01:58:16 +0100184{
inikep4c12f232016-03-29 14:52:13 +0200185 BMK_time_t clockEnd;
Yann Collet99909862016-04-09 16:17:18 +0200186
inikep4c12f232016-03-29 14:52:13 +0200187 (void)ticksPerSecond;
188 BMK_getTime(clockEnd);
189 return BMK_getSpanTimeMicro(ticksPerSecond, clockStart, clockEnd);
inikep06f793a2016-03-29 11:17:58 +0200190}
191
Yann Colleted699e62015-12-16 02:37:24 +0100192static U64 BMK_getFileSize(const char* infilename)
193{
194 int r;
195#if defined(_MSC_VER)
196 struct _stat64 statbuf;
197 r = _stat64(infilename, &statbuf);
198#else
199 struct stat statbuf;
200 r = stat(infilename, &statbuf);
201#endif
202 if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
203 return (U64)statbuf.st_size;
204}
205
inikep4b3c5ee2016-04-14 13:43:51 +0200206static U32 BMK_isDirectory(const char* infilename)
207{
208 int r;
209#if defined(_MSC_VER)
210 struct _stat64 statbuf;
211 r = _stat64(infilename, &statbuf);
212#else
213 struct stat statbuf;
214 r = stat(infilename, &statbuf);
215#endif
216 if (!r && S_ISDIR(statbuf.st_mode)) return 1;
217 return 0;
218}
Yann Collet4856a002015-01-24 01:58:16 +0100219
Yann Colletf3eca252015-10-22 15:31:46 +0100220/* ********************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100221* Bench functions
Yann Colletf3eca252015-10-22 15:31:46 +0100222**********************************************************/
Yann Collet1c00dc32015-10-21 08:22:25 +0100223typedef struct
Yann Collet4856a002015-01-24 01:58:16 +0100224{
Yann Colleted699e62015-12-16 02:37:24 +0100225 const char* srcPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100226 size_t srcSize;
227 char* cPtr;
228 size_t cRoom;
229 size_t cSize;
230 char* resPtr;
231 size_t resSize;
232} blockParam_t;
233
inikep4e26bb62016-03-14 12:48:51 +0100234typedef struct
235{
inikepc034b732016-03-14 13:13:42 +0100236 double ratio;
inikep4e26bb62016-03-14 12:48:51 +0100237 size_t cSize;
inikepc034b732016-03-14 13:13:42 +0100238 double cSpeed;
239 double dSpeed;
inikep4e26bb62016-03-14 12:48:51 +0100240} benchResult_t;
241
Yann Collet99909862016-04-09 16:17:18 +0200242
Yann Colletbe2010e2015-10-31 12:57:14 +0100243#define MIN(a,b) ((a)<(b) ? (a) : (b))
Yann Collet2ce49232016-02-02 14:36:49 +0100244#define MAX(a,b) ((a)>(b) ? (a) : (b))
Yann Collet1c00dc32015-10-21 08:22:25 +0100245
Yann Colleted699e62015-12-16 02:37:24 +0100246static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
inikep472638c2016-03-23 12:28:28 +0100247 const char* displayName, int cLevel,
Yann Collet31683c02015-12-18 01:26:48 +0100248 const size_t* fileSizes, U32 nbFiles,
inikep4e26bb62016-03-14 12:48:51 +0100249 const void* dictBuffer, size_t dictBufferSize, benchResult_t *result)
Yann Collet1c00dc32015-10-21 08:22:25 +0100250{
inikep38654982016-04-21 12:18:47 +0200251 size_t const blockSize = (g_blockSize>=32 ? g_blockSize : srcSize) + (!srcSize); /* avoid div by 0 */
Yann Colletd64f4352016-03-21 00:07:42 +0100252 U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
Yann Colleted699e62015-12-16 02:37:24 +0100253 blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t));
Yann Colletb9151402016-03-26 17:18:11 +0100254 size_t const maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */
Yann Collet1c00dc32015-10-21 08:22:25 +0100255 void* const compressedBuffer = malloc(maxCompressedSize);
256 void* const resultBuffer = malloc(srcSize);
Yann Collet2630a5e2016-01-14 19:13:22 +0100257 ZSTD_CCtx* refCtx = ZSTD_createCCtx();
Yann Collet31683c02015-12-18 01:26:48 +0100258 ZSTD_CCtx* ctx = ZSTD_createCCtx();
Yann Collet7b51a292016-01-26 15:58:49 +0100259 ZSTD_DCtx* refDCtx = ZSTD_createDCtx();
Yann Collet31683c02015-12-18 01:26:48 +0100260 ZSTD_DCtx* dctx = ZSTD_createDCtx();
Yann Colletde406ee2016-03-20 15:46:10 +0100261 U32 nbBlocks;
inikep4c12f232016-03-29 14:52:13 +0200262 BMK_time_t ticksPerSecond;
Yann Colletde406ee2016-03-20 15:46:10 +0100263
264 /* checks */
Yann Collet7b51a292016-01-26 15:58:49 +0100265 if (!compressedBuffer || !resultBuffer || !blockTable || !refCtx || !ctx || !refDCtx || !dctx)
Yann Colleted699e62015-12-16 02:37:24 +0100266 EXM_THROW(31, "not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100267
Yann Collet4856a002015-01-24 01:58:16 +0100268 /* init */
269 if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */
inikep4c12f232016-03-29 14:52:13 +0200270 BMK_initTimer(ticksPerSecond);
271
Yann Collet1c00dc32015-10-21 08:22:25 +0100272 /* Init blockTable data */
Yann Colletde406ee2016-03-20 15:46:10 +0100273 { const char* srcPtr = (const char*)srcBuffer;
Yann Collet1c00dc32015-10-21 08:22:25 +0100274 char* cPtr = (char*)compressedBuffer;
275 char* resPtr = (char*)resultBuffer;
Yann Collet699b14d2016-03-17 19:37:33 +0100276 U32 fileNb;
Yann Colletde406ee2016-03-20 15:46:10 +0100277 for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
Yann Collet70611352015-12-16 03:01:03 +0100278 size_t remaining = fileSizes[fileNb];
Yann Collet699b14d2016-03-17 19:37:33 +0100279 U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize);
280 U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
Yann Colletfd416f12016-01-30 03:14:15 +0100281 for ( ; nbBlocks<blockEnd; nbBlocks++) {
Yann Colletde406ee2016-03-20 15:46:10 +0100282 size_t const thisBlockSize = MIN(remaining, blockSize);
Yann Colleted699e62015-12-16 02:37:24 +0100283 blockTable[nbBlocks].srcPtr = srcPtr;
284 blockTable[nbBlocks].cPtr = cPtr;
285 blockTable[nbBlocks].resPtr = resPtr;
286 blockTable[nbBlocks].srcSize = thisBlockSize;
287 blockTable[nbBlocks].cRoom = ZSTD_compressBound(thisBlockSize);
288 srcPtr += thisBlockSize;
289 cPtr += blockTable[nbBlocks].cRoom;
290 resPtr += thisBlockSize;
291 remaining -= thisBlockSize;
Yann Colletfd416f12016-01-30 03:14:15 +0100292 } } }
Yann Collet1c00dc32015-10-21 08:22:25 +0100293
Yann Collet4856a002015-01-24 01:58:16 +0100294 /* warmimg up memory */
Yann Colletd062f132015-12-01 01:31:17 +0100295 RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
Yann Collet4856a002015-01-24 01:58:16 +0100296
297 /* Bench */
inikep6d157f12016-04-15 16:54:11 +0200298 { U64 fastestC = (U64)(-1LL), fastestD = (U64)(-1LL);
Yann Colletb9151402016-03-26 17:18:11 +0100299 U64 const crcOrig = XXH64(srcBuffer, srcSize, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100300 U64 crcCheck = 0;
inikep4c12f232016-03-29 14:52:13 +0200301 BMK_time_t coolTime;
Yann Colletde406ee2016-03-20 15:46:10 +0100302 U32 testNb;
inikep19bd48f2016-04-04 12:10:00 +0200303 size_t cSize = 0;
304 double ratio = 0.;
Yann Collet4856a002015-01-24 01:58:16 +0100305
inikep4c12f232016-03-29 14:52:13 +0200306 BMK_getTime(coolTime);
inikep4e26bb62016-03-14 12:48:51 +0100307 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Colletde406ee2016-03-20 15:46:10 +0100308 for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) {
inikep4c12f232016-03-29 14:52:13 +0200309 BMK_time_t clockStart, clockEnd;
inikepc5e1d292016-04-19 09:37:59 +0200310 U64 clockLoop = g_nbIterations ? TIMELOOP_S*1000000ULL : 1;
Yann Collet4856a002015-01-24 01:58:16 +0100311
Yann Collet27d3dad2016-03-11 13:41:20 +0100312 /* overheat protection */
inikep4c12f232016-03-29 14:52:13 +0200313 if (BMK_clockSpan(coolTime, ticksPerSecond) > ACTIVEPERIOD_S*1000000ULL) {
Yann Collet27d3dad2016-03-11 13:41:20 +0100314 DISPLAY("\rcooling down ... \r");
315 BMK_sleep(COOLPERIOD_S);
inikep4c12f232016-03-29 14:52:13 +0200316 BMK_getTime(coolTime);
Yann Collet27d3dad2016-03-11 13:41:20 +0100317 }
Yann Collet4856a002015-01-24 01:58:16 +0100318
319 /* Compression */
inikep2872b6f2016-03-22 14:38:34 +0100320 DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->\r", testNb, displayName, (U32)srcSize);
Yann Collet699b14d2016-03-17 19:37:33 +0100321 memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100322
inikep472638c2016-03-23 12:28:28 +0100323 mili_sleep(1); /* give processor time to other processes */
inikep4c12f232016-03-29 14:52:13 +0200324 BMK_getTime(clockStart);
325 do { BMK_getTime(clockEnd); }
326 while (BMK_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) == 0);
327 BMK_getTime(clockStart);
Yann Colletde406ee2016-03-20 15:46:10 +0100328
inikepc5e1d292016-04-19 09:37:59 +0200329 { U32 nbLoops = 0;
330 do {
Yann Colleta5b66e32016-03-26 01:48:27 +0100331 U32 blockNb;
inikep4b3c5ee2016-04-14 13:43:51 +0200332 { ZSTD_parameters params;
333 params.cParams = ZSTD_getCParams(cLevel, blockSize, dictBufferSize);
334 params.fParams.contentSizeFlag = 1;
335 ZSTD_adjustCParams(&params.cParams, blockSize, dictBufferSize);
336 { size_t const initResult = ZSTD_compressBegin_advanced(refCtx, dictBuffer, dictBufferSize, params, blockSize);
337 if (ZSTD_isError(initResult)) break;
338 } }
Yann Colleta5b66e32016-03-26 01:48:27 +0100339 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
340 size_t const rSize = ZSTD_compress_usingPreparedCCtx(ctx, refCtx,
341 blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
342 blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize);
343 if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingPreparedCCtx() failed : %s", ZSTD_getErrorName(rSize));
344 blockTable[blockNb].cSize = rSize;
inikepc5e1d292016-04-19 09:37:59 +0200345 }
346 nbLoops++;
347 } while (BMK_clockSpan(clockStart, ticksPerSecond) < clockLoop);
inikep19bd48f2016-04-04 12:10:00 +0200348 { U64 const clockSpan = BMK_clockSpan(clockStart, ticksPerSecond);
inikep6d157f12016-04-15 16:54:11 +0200349 if (clockSpan < fastestC*nbLoops) fastestC = clockSpan / nbLoops;
Yann Colletde406ee2016-03-20 15:46:10 +0100350 } }
Yann Collet4856a002015-01-24 01:58:16 +0100351
Yann Collet1c00dc32015-10-21 08:22:25 +0100352 cSize = 0;
Yann Colletde406ee2016-03-20 15:46:10 +0100353 { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; }
Yann Collet2acb5d32015-10-29 16:49:43 +0100354 ratio = (double)srcSize / (double)cSize;
inikep2872b6f2016-03-22 14:38:34 +0100355 DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r",
Yann Colletde406ee2016-03-20 15:46:10 +0100356 testNb, displayName, (U32)srcSize, (U32)cSize, ratio,
inikep06f793a2016-03-29 11:17:58 +0200357 (double)srcSize / fastestC );
Yann Collet4856a002015-01-24 01:58:16 +0100358
Yann Colleta5b66e32016-03-26 01:48:27 +0100359 (void)fastestD; (void)crcOrig; /* unused when decompression disabled */
Yann Collete93d6ce2016-01-31 00:58:06 +0100360#if 1
Yann Collet4856a002015-01-24 01:58:16 +0100361 /* Decompression */
Yann Collet7b51a292016-01-26 15:58:49 +0100362 memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100363
inikep472638c2016-03-23 12:28:28 +0100364 mili_sleep(1); /* give processor time to other processes */
inikep4c12f232016-03-29 14:52:13 +0200365 BMK_getTime(clockStart);
366 do { BMK_getTime(clockEnd); }
367 while (BMK_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) == 0);
368 BMK_getTime(clockStart);
Yann Collet7b51a292016-01-26 15:58:49 +0100369
inikepc5e1d292016-04-19 09:37:59 +0200370 { U32 nbLoops = 0;
371 do {
Yann Colleta5b66e32016-03-26 01:48:27 +0100372 U32 blockNb;
373 ZSTD_decompressBegin_usingDict(refDCtx, dictBuffer, dictBufferSize);
374 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
375 size_t const regenSize = ZSTD_decompress_usingPreparedDCtx(dctx, refDCtx,
376 blockTable[blockNb].resPtr, blockTable[blockNb].srcSize,
377 blockTable[blockNb].cPtr, blockTable[blockNb].cSize);
378 if (ZSTD_isError(regenSize)) {
379 DISPLAY("ZSTD_decompress_usingPreparedDCtx() failed on block %u : %s \n",
380 blockNb, ZSTD_getErrorName(regenSize));
inikep19bd48f2016-04-04 12:10:00 +0200381 clockLoop = 0; /* force immediate test end */
Yann Colleta5b66e32016-03-26 01:48:27 +0100382 break;
383 }
384 blockTable[blockNb].resSize = regenSize;
inikepc5e1d292016-04-19 09:37:59 +0200385 }
386 nbLoops++;
387 } while (BMK_clockSpan(clockStart, ticksPerSecond) < clockLoop);
inikep19bd48f2016-04-04 12:10:00 +0200388 { U64 const clockSpan = BMK_clockSpan(clockStart, ticksPerSecond);
inikep6d157f12016-04-15 16:54:11 +0200389 if (clockSpan < fastestD*nbLoops) fastestD = clockSpan / nbLoops;
Yann Collet7b51a292016-01-26 15:58:49 +0100390 } }
391
inikep2872b6f2016-03-22 14:38:34 +0100392 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 +0100393 testNb, displayName, (U32)srcSize, (U32)cSize, ratio,
inikep06f793a2016-03-29 11:17:58 +0200394 (double)srcSize / fastestC,
395 (double)srcSize / fastestD );
Yann Collet4856a002015-01-24 01:58:16 +0100396
397 /* CRC Checking */
inikep19bd48f2016-04-04 12:10:00 +0200398 { crcCheck = XXH64(resultBuffer, srcSize, 0);
Yann Colleta5b66e32016-03-26 01:48:27 +0100399 if (crcOrig!=crcCheck) {
400 size_t u;
401 DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck);
402 for (u=0; u<srcSize; u++) {
403 if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) {
404 U32 segNb, bNb, pos;
405 size_t bacc = 0;
406 DISPLAY("Decoding error at pos %u ", (U32)u);
407 for (segNb = 0; segNb < nbBlocks; segNb++) {
408 if (bacc + blockTable[segNb].srcSize > u) break;
409 bacc += blockTable[segNb].srcSize;
410 }
411 pos = (U32)(u - bacc);
412 bNb = pos / (128 KB);
413 DISPLAY("(block %u, sub %u, pos %u) \n", segNb, bNb, pos);
414 break;
Yann Collet03a6dab2016-01-21 02:21:17 +0100415 }
Yann Colleta5b66e32016-03-26 01:48:27 +0100416 if (u==srcSize-1) { /* should never happen */
417 DISPLAY("no difference detected\n");
418 } }
419 break;
420 } } /* CRC Checking */
Yann Collete8c6bb12015-07-26 00:23:57 +0100421#endif
Yann Colletde406ee2016-03-20 15:46:10 +0100422 } /* for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) */
Yann Collet4856a002015-01-24 01:58:16 +0100423
inikep2872b6f2016-03-22 14:38:34 +0100424 if (crcOrig == crcCheck) {
inikep4e26bb62016-03-14 12:48:51 +0100425 result->ratio = ratio;
426 result->cSize = cSize;
Yann Collet99909862016-04-09 16:17:18 +0200427 result->cSpeed = (double)srcSize / fastestC;
inikep06f793a2016-03-29 11:17:58 +0200428 result->dSpeed = (double)srcSize / fastestD;
inikep4e26bb62016-03-14 12:48:51 +0100429 }
inikep2872b6f2016-03-22 14:38:34 +0100430 DISPLAYLEVEL(2, "%2i#\n", cLevel);
Yann Colletde406ee2016-03-20 15:46:10 +0100431 } /* Bench */
Yann Collet4856a002015-01-24 01:58:16 +0100432
Yann Colleted699e62015-12-16 02:37:24 +0100433 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100434 free(compressedBuffer);
435 free(resultBuffer);
Yann Collet2630a5e2016-01-14 19:13:22 +0100436 ZSTD_freeCCtx(refCtx);
Yann Collet31683c02015-12-18 01:26:48 +0100437 ZSTD_freeCCtx(ctx);
Yann Collet7b51a292016-01-26 15:58:49 +0100438 ZSTD_freeDCtx(refDCtx);
Yann Collet31683c02015-12-18 01:26:48 +0100439 ZSTD_freeDCtx(dctx);
Yann Collet4856a002015-01-24 01:58:16 +0100440 return 0;
441}
442
443
Yann Collet4856a002015-01-24 01:58:16 +0100444static size_t BMK_findMaxMem(U64 requiredMem)
445{
Yann Colletde406ee2016-03-20 15:46:10 +0100446 size_t const step = 64 MB;
Yann Collet4856a002015-01-24 01:58:16 +0100447 BYTE* testmem = NULL;
448
449 requiredMem = (((requiredMem >> 26) + 1) << 26);
Yann Colletde406ee2016-03-20 15:46:10 +0100450 requiredMem += step;
Yann Collet050efba2015-11-03 09:49:30 +0100451 if (requiredMem > maxMemory) requiredMem = maxMemory;
Yann Collet4856a002015-01-24 01:58:16 +0100452
Yann Colletde406ee2016-03-20 15:46:10 +0100453 do {
Yann Collet4856a002015-01-24 01:58:16 +0100454 testmem = (BYTE*)malloc((size_t)requiredMem);
Yann Colletde406ee2016-03-20 15:46:10 +0100455 requiredMem -= step;
456 } while (!testmem);
457
Yann Collet4856a002015-01-24 01:58:16 +0100458 free(testmem);
Yann Colletde406ee2016-03-20 15:46:10 +0100459 return (size_t)(requiredMem);
Yann Collet4856a002015-01-24 01:58:16 +0100460}
461
Yann Colleted699e62015-12-16 02:37:24 +0100462static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize,
inikepeaba91a2016-03-23 20:30:26 +0100463 const char* displayName, int cLevel, int cLevelLast,
Yann Collet31683c02015-12-18 01:26:48 +0100464 const size_t* fileSizes, unsigned nbFiles,
465 const void* dictBuffer, size_t dictBufferSize)
Yann Collet4856a002015-01-24 01:58:16 +0100466{
inikep4e26bb62016-03-14 12:48:51 +0100467 benchResult_t result, total;
inikepe9554b72016-03-14 18:10:30 +0100468 int l;
inikep4e26bb62016-03-14 12:48:51 +0100469
inikep19bd48f2016-04-04 12:10:00 +0200470 SET_HIGH_PRIORITY;
inikep472638c2016-03-23 12:28:28 +0100471
inikepe9554b72016-03-14 18:10:30 +0100472 const char* pch = strrchr(displayName, '\\'); /* Windows */
473 if (!pch) pch = strrchr(displayName, '/'); /* Linux */
474 if (pch) displayName = pch+1;
inikep4e26bb62016-03-14 12:48:51 +0100475
inikepe9554b72016-03-14 18:10:30 +0100476 memset(&result, 0, sizeof(result));
477 memset(&total, 0, sizeof(total));
inikep5fdd0b42016-03-14 19:51:11 +0100478
inikepeaba91a2016-03-23 20:30:26 +0100479 if (g_displayLevel == 1 && !g_additionalParam)
inikep2872b6f2016-03-22 14:38:34 +0100480 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 +0100481
482 if (cLevelLast < cLevel) cLevelLast = cLevel;
483
Yann Collet99909862016-04-09 16:17:18 +0200484 for (l=cLevel; l <= cLevelLast; l++) {
inikepe9554b72016-03-14 18:10:30 +0100485 BMK_benchMem(srcBuffer, benchedSize,
inikep472638c2016-03-23 12:28:28 +0100486 displayName, l,
inikepe9554b72016-03-14 18:10:30 +0100487 fileSizes, nbFiles,
488 dictBuffer, dictBufferSize, &result);
489 if (g_displayLevel == 1) {
inikepeaba91a2016-03-23 20:30:26 +0100490 if (g_additionalParam)
inikep38654982016-04-21 12:18:47 +0200491 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 +0100492 else
inikep38654982016-04-21 12:18:47 +0200493 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 +0100494 total.cSize += result.cSize;
495 total.cSpeed += result.cSpeed;
496 total.dSpeed += result.dSpeed;
497 total.ratio += result.ratio;
Yann Collet99909862016-04-09 16:17:18 +0200498 } }
499 if (g_displayLevel == 1 && cLevelLast > cLevel) {
inikepe9554b72016-03-14 18:10:30 +0100500 total.cSize /= 1+cLevelLast-cLevel;
501 total.cSpeed /= 1+cLevelLast-cLevel;
502 total.dSpeed /= 1+cLevelLast-cLevel;
503 total.ratio /= 1+cLevelLast-cLevel;
inikep38654982016-04-21 12:18:47 +0200504 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 +0100505 }
Yann Colleted699e62015-12-16 02:37:24 +0100506}
507
508static U64 BMK_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
509{
510 U64 total = 0;
511 unsigned n;
512 for (n=0; n<nbFiles; n++)
513 total += BMK_getFileSize(fileNamesTable[n]);
514 return total;
515}
516
Yann Colleta5b66e32016-03-26 01:48:27 +0100517/*! BMK_loadFiles() :
518 Loads `buffer` with content of files listed within `fileNamesTable`.
519 At most, fills `buffer` entirely */
Yann Collet70611352015-12-16 03:01:03 +0100520static void BMK_loadFiles(void* buffer, size_t bufferSize,
521 size_t* fileSizes,
Yann Colleta5b66e32016-03-26 01:48:27 +0100522 const char** fileNamesTable, unsigned nbFiles)
Yann Colleted699e62015-12-16 02:37:24 +0100523{
inikepc0d5f4e2016-04-13 10:48:04 +0200524 size_t pos = 0, totalSize = 0;
Yann Colleted699e62015-12-16 02:37:24 +0100525
Yann Collet699b14d2016-03-17 19:37:33 +0100526 unsigned n;
Yann Colletfd416f12016-01-30 03:14:15 +0100527 for (n=0; n<nbFiles; n++) {
inikep4b3c5ee2016-04-14 13:43:51 +0200528 if (BMK_isDirectory(fileNamesTable[n])) {
inikepc0d5f4e2016-04-13 10:48:04 +0200529 DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]);
530 continue;
531 }
Yann Colleted699e62015-12-16 02:37:24 +0100532 U64 fileSize = BMK_getFileSize(fileNamesTable[n]);
Yann Colleta5b66e32016-03-26 01:48:27 +0100533 FILE* const f = fopen(fileNamesTable[n], "rb");
Yann Colleted699e62015-12-16 02:37:24 +0100534 if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]);
535 DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[n]);
Yann Colleta5b66e32016-03-26 01:48:27 +0100536 if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */
537 { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
538 if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]);
539 pos += readSize; }
Yann Colleta52c98d2015-12-16 03:12:31 +0100540 fileSizes[n] = (size_t)fileSize;
inikepc0d5f4e2016-04-13 10:48:04 +0200541 totalSize += (size_t)fileSize;
Yann Colleted699e62015-12-16 02:37:24 +0100542 fclose(f);
543 }
inikepc0d5f4e2016-04-13 10:48:04 +0200544
545 if (totalSize == 0) EXM_THROW(12, "no data to bench");
Yann Colleted699e62015-12-16 02:37:24 +0100546}
547
Yann Collet31683c02015-12-18 01:26:48 +0100548static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles,
inikepeaba91a2016-03-23 20:30:26 +0100549 const char* dictFileName, int cLevel, int cLevelLast)
Yann Colleted699e62015-12-16 02:37:24 +0100550{
551 void* srcBuffer;
552 size_t benchedSize;
Yann Collet31683c02015-12-18 01:26:48 +0100553 void* dictBuffer = NULL;
554 size_t dictBufferSize = 0;
555 size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t));
Yann Colleted699e62015-12-16 02:37:24 +0100556 U64 totalSizeToLoad = BMK_getTotalFileSize(fileNamesTable, nbFiles);
557 char mfName[20] = {0};
558 const char* displayName = NULL;
559
Yann Collet31683c02015-12-18 01:26:48 +0100560 if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes");
561
562 /* Load dictionary */
Yann Colletfd416f12016-01-30 03:14:15 +0100563 if (dictFileName != NULL) {
Yann Collet31683c02015-12-18 01:26:48 +0100564 U64 dictFileSize = BMK_getFileSize(dictFileName);
565 if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName);
566 dictBufferSize = (size_t)dictFileSize;
567 dictBuffer = malloc(dictBufferSize);
568 if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize);
569 BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1);
570 }
571
Yann Colleted699e62015-12-16 02:37:24 +0100572 /* Memory allocation & restrictions */
573 benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
574 if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
575 if (benchedSize < totalSizeToLoad)
576 DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20));
577 srcBuffer = malloc(benchedSize);
578 if (!srcBuffer) EXM_THROW(12, "not enough memory");
579
580 /* Load input buffer */
Yann Collet70611352015-12-16 03:01:03 +0100581 BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +0100582
583 /* Bench */
584 snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
585 if (nbFiles > 1) displayName = mfName;
586 else displayName = fileNamesTable[0];
587
Yann Collet31683c02015-12-18 01:26:48 +0100588 BMK_benchCLevel(srcBuffer, benchedSize,
inikepeaba91a2016-03-23 20:30:26 +0100589 displayName, cLevel, cLevelLast,
Yann Collet31683c02015-12-18 01:26:48 +0100590 fileSizes, nbFiles,
591 dictBuffer, dictBufferSize);
Yann Collet4856a002015-01-24 01:58:16 +0100592
Yann Colleteeb8ba12015-10-22 16:55:40 +0100593 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100594 free(srcBuffer);
Yann Collet31683c02015-12-18 01:26:48 +0100595 free(dictBuffer);
Yann Collet70611352015-12-16 03:01:03 +0100596 free(fileSizes);
Yann Collet4856a002015-01-24 01:58:16 +0100597}
598
599
inikepeaba91a2016-03-23 20:30:26 +0100600static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility)
Yann Collet4856a002015-01-24 01:58:16 +0100601{
Yann Colleted699e62015-12-16 02:37:24 +0100602 char name[20] = {0};
Yann Collet4856a002015-01-24 01:58:16 +0100603 size_t benchedSize = 10000000;
604 void* srcBuffer = malloc(benchedSize);
Yann Collet4856a002015-01-24 01:58:16 +0100605
Yann Collet4856a002015-01-24 01:58:16 +0100606 /* Memory allocation */
Yann Colleted699e62015-12-16 02:37:24 +0100607 if (!srcBuffer) EXM_THROW(21, "not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100608
609 /* Fill input buffer */
Yann Colletd062f132015-12-01 01:31:17 +0100610 RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100611
612 /* Bench */
Yann Colleted699e62015-12-16 02:37:24 +0100613 snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
inikepeaba91a2016-03-23 20:30:26 +0100614 BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100615
Yann Colleted699e62015-12-16 02:37:24 +0100616 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100617 free(srcBuffer);
Yann Collet4856a002015-01-24 01:58:16 +0100618}
619
620
Yann Collet31683c02015-12-18 01:26:48 +0100621int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
inikepeaba91a2016-03-23 20:30:26 +0100622 const char* dictFileName, int cLevel, int cLevelLast)
Yann Collet4856a002015-01-24 01:58:16 +0100623{
Yann Collet699b14d2016-03-17 19:37:33 +0100624 double const compressibility = (double)g_compressibilityDefault / 100;
Yann Collet4856a002015-01-24 01:58:16 +0100625
626 if (nbFiles == 0)
inikepeaba91a2016-03-23 20:30:26 +0100627 BMK_syntheticTest(cLevel, cLevelLast, compressibility);
Yann Collet4856a002015-01-24 01:58:16 +0100628 else
inikepeaba91a2016-03-23 20:30:26 +0100629 BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast);
Yann Collet4856a002015-01-24 01:58:16 +0100630 return 0;
631}
632