blob: d1341fe1c475ebd488ea7da08e5005e48e60d494 [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***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010047#include <stdlib.h> /* malloc, free */
48#include <string.h> /* memset */
Yann Colleteeb8ba12015-10-22 16:55:40 +010049#include <stdio.h> /* fprintf, fopen, ftello64 */
50#include <sys/types.h> /* stat64 */
51#include <sys/stat.h> /* stat64 */
Yann Collet699b14d2016-03-17 19:37:33 +010052#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
Yann Collet4856a002015-01-24 01:58:16 +010053
Yann Collet27d3dad2016-03-11 13:41:20 +010054/* sleep : posix - windows - others */
55#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
56# include <unistd.h>
57# define BMK_sleep(s) sleep(s)
58#elif defined(_WIN32)
59# include <windows.h>
60# define BMK_sleep(s) Sleep(1000*s)
61#else
62# define BMK_sleep(s) /* disabled */
63#endif
64
Yann Colletf3eca252015-10-22 15:31:46 +010065#include "mem.h"
Yann Collet31683c02015-12-18 01:26:48 +010066#include "zstd_static.h"
Yann Collet4856a002015-01-24 01:58:16 +010067#include "xxhash.h"
Yann Colletd062f132015-12-01 01:31:17 +010068#include "datagen.h" /* RDG_genBuffer */
Yann Collet4856a002015-01-24 01:58:16 +010069
70
Yann Colletf3eca252015-10-22 15:31:46 +010071/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010072* Compiler specifics
Yann Colletf3eca252015-10-22 15:31:46 +010073***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010074#if !defined(S_ISREG)
75# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
76#endif
77
Yann Colleted699e62015-12-16 02:37:24 +010078#ifdef _MSC_VER
79#define snprintf sprintf_s
80#endif
81
Yann Collet4856a002015-01-24 01:58:16 +010082
Yann Colletf3eca252015-10-22 15:31:46 +010083/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010084* Constants
Yann Colletf3eca252015-10-22 15:31:46 +010085***************************************/
Yann Collet27d3dad2016-03-11 13:41:20 +010086#define NBLOOPS 3
Yann Collet699b14d2016-03-17 19:37:33 +010087#define TIMELOOP_S 1
Yann Collet27d3dad2016-03-11 13:41:20 +010088#define ACTIVEPERIOD_S 70
89#define COOLPERIOD_S 10
Yann Collet4856a002015-01-24 01:58:16 +010090
91#define KB *(1 <<10)
92#define MB *(1 <<20)
93#define GB *(1U<<30)
94
Yann Colletd062f132015-12-01 01:31:17 +010095static 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 +010096#define DEFAULT_CHUNKSIZE (4 MB)
97
98static U32 g_compressibilityDefault = 50;
Yann Collet4856a002015-01-24 01:58:16 +010099
100
Yann Colletf3eca252015-10-22 15:31:46 +0100101/* *************************************
Yann Colleted699e62015-12-16 02:37:24 +0100102* console display
Yann Colletf3eca252015-10-22 15:31:46 +0100103***************************************/
Yann Colleted699e62015-12-16 02:37:24 +0100104#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
105#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
106static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
107
108
109/* *************************************
110* Exceptions
111***************************************/
112#ifndef DEBUG
113# define DEBUG 0
114#endif
115#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
116#define EXM_THROW(error, ...) \
117{ \
118 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
119 DISPLAYLEVEL(1, "Error %i : ", error); \
120 DISPLAYLEVEL(1, __VA_ARGS__); \
121 DISPLAYLEVEL(1, "\n"); \
122 exit(error); \
123}
Yann Collet4856a002015-01-24 01:58:16 +0100124
125
Yann Colletf3eca252015-10-22 15:31:46 +0100126/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100127* Benchmark Parameters
Yann Colletf3eca252015-10-22 15:31:46 +0100128***************************************/
Yann Collet1d1ae402016-03-17 19:51:02 +0100129static U32 g_nbIterations = NBLOOPS;
Yann Collet1c00dc32015-10-21 08:22:25 +0100130static size_t g_blockSize = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100131
Yann Collet1d1ae402016-03-17 19:51:02 +0100132void BMK_SetNbIterations(unsigned nbLoops)
Yann Collet4856a002015-01-24 01:58:16 +0100133{
Yann Collet1d1ae402016-03-17 19:51:02 +0100134 g_nbIterations = nbLoops;
135 DISPLAY("- %i iterations -\n", g_nbIterations);
Yann Collet4856a002015-01-24 01:58:16 +0100136}
137
Yann Collet1c00dc32015-10-21 08:22:25 +0100138void BMK_SetBlockSize(size_t blockSize)
139{
140 g_blockSize = blockSize;
141 DISPLAY("using blocks of size %u KB \n", (U32)(blockSize>>10));
142}
143
Yann Collet4856a002015-01-24 01:58:16 +0100144
Yann Colletf3eca252015-10-22 15:31:46 +0100145/* ********************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100146* Private functions
Yann Colletf3eca252015-10-22 15:31:46 +0100147**********************************************************/
Yann Collet699b14d2016-03-17 19:37:33 +0100148static clock_t BMK_clockSpan( clock_t clockStart )
Yann Collet4856a002015-01-24 01:58:16 +0100149{
Yann Collet699b14d2016-03-17 19:37:33 +0100150 return clock() - clockStart; /* works even if overflow, span limited to <= ~30mn */
Yann Collet4856a002015-01-24 01:58:16 +0100151}
152
Yann Collet4856a002015-01-24 01:58:16 +0100153
Yann Colleted699e62015-12-16 02:37:24 +0100154static U64 BMK_getFileSize(const char* infilename)
155{
156 int r;
157#if defined(_MSC_VER)
158 struct _stat64 statbuf;
159 r = _stat64(infilename, &statbuf);
160#else
161 struct stat statbuf;
162 r = stat(infilename, &statbuf);
163#endif
164 if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
165 return (U64)statbuf.st_size;
166}
167
Yann Collet4856a002015-01-24 01:58:16 +0100168
Yann Colletf3eca252015-10-22 15:31:46 +0100169/* ********************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100170* Bench functions
Yann Colletf3eca252015-10-22 15:31:46 +0100171**********************************************************/
Yann Collet1c00dc32015-10-21 08:22:25 +0100172typedef struct
Yann Collet4856a002015-01-24 01:58:16 +0100173{
Yann Colleted699e62015-12-16 02:37:24 +0100174 const char* srcPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100175 size_t srcSize;
176 char* cPtr;
177 size_t cRoom;
178 size_t cSize;
179 char* resPtr;
180 size_t resSize;
181} blockParam_t;
182
Yann Colletbe2010e2015-10-31 12:57:14 +0100183#define MIN(a,b) ((a)<(b) ? (a) : (b))
Yann Collet2ce49232016-02-02 14:36:49 +0100184#define MAX(a,b) ((a)>(b) ? (a) : (b))
Yann Collet1c00dc32015-10-21 08:22:25 +0100185
Yann Colleted699e62015-12-16 02:37:24 +0100186static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
187 const char* displayName, int cLevel,
Yann Collet31683c02015-12-18 01:26:48 +0100188 const size_t* fileSizes, U32 nbFiles,
189 const void* dictBuffer, size_t dictBufferSize)
Yann Collet1c00dc32015-10-21 08:22:25 +0100190{
Yann Collet2c7ac7c2015-11-04 17:52:18 +0100191 const size_t blockSize = (g_blockSize ? g_blockSize : srcSize) + (!srcSize); /* avoid div by 0 */
Yann Colleted699e62015-12-16 02:37:24 +0100192 const U32 maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
Yann Collet1840d6a2016-01-20 15:39:06 +0100193 size_t largestBlockSize = 0;
Yann Colleted699e62015-12-16 02:37:24 +0100194 blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t));
Yann Collet367060b2015-12-17 00:07:10 +0100195 const size_t maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */
Yann Collet1c00dc32015-10-21 08:22:25 +0100196 void* const compressedBuffer = malloc(maxCompressedSize);
197 void* const resultBuffer = malloc(srcSize);
Yann Collet2630a5e2016-01-14 19:13:22 +0100198 ZSTD_CCtx* refCtx = ZSTD_createCCtx();
Yann Collet31683c02015-12-18 01:26:48 +0100199 ZSTD_CCtx* ctx = ZSTD_createCCtx();
Yann Collet7b51a292016-01-26 15:58:49 +0100200 ZSTD_DCtx* refDCtx = ZSTD_createDCtx();
Yann Collet31683c02015-12-18 01:26:48 +0100201 ZSTD_DCtx* dctx = ZSTD_createDCtx();
Yann Colleted699e62015-12-16 02:37:24 +0100202 U64 crcOrig = XXH64(srcBuffer, srcSize, 0);
203 U32 nbBlocks = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100204
Yann Colletc776c462015-10-29 19:10:54 +0100205 /* init */
Yann Colleted699e62015-12-16 02:37:24 +0100206 if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */
Yann Colletc776c462015-10-29 19:10:54 +0100207
Yann Collet4856a002015-01-24 01:58:16 +0100208 /* Memory allocation & restrictions */
Yann Collet7b51a292016-01-26 15:58:49 +0100209 if (!compressedBuffer || !resultBuffer || !blockTable || !refCtx || !ctx || !refDCtx || !dctx)
Yann Colleted699e62015-12-16 02:37:24 +0100210 EXM_THROW(31, "not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100211
Yann Collet1c00dc32015-10-21 08:22:25 +0100212 /* Init blockTable data */
213 {
Yann Colleted699e62015-12-16 02:37:24 +0100214 const char* srcPtr = (const char*)srcBuffer;
Yann Collet1c00dc32015-10-21 08:22:25 +0100215 char* cPtr = (char*)compressedBuffer;
216 char* resPtr = (char*)resultBuffer;
Yann Collet699b14d2016-03-17 19:37:33 +0100217 U32 fileNb;
Yann Colletfd416f12016-01-30 03:14:15 +0100218 for (fileNb=0; fileNb<nbFiles; fileNb++) {
Yann Collet70611352015-12-16 03:01:03 +0100219 size_t remaining = fileSizes[fileNb];
Yann Collet699b14d2016-03-17 19:37:33 +0100220 U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize);
221 U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
Yann Colletfd416f12016-01-30 03:14:15 +0100222 for ( ; nbBlocks<blockEnd; nbBlocks++) {
Yann Colleted699e62015-12-16 02:37:24 +0100223 size_t thisBlockSize = MIN(remaining, blockSize);
224 blockTable[nbBlocks].srcPtr = srcPtr;
225 blockTable[nbBlocks].cPtr = cPtr;
226 blockTable[nbBlocks].resPtr = resPtr;
227 blockTable[nbBlocks].srcSize = thisBlockSize;
228 blockTable[nbBlocks].cRoom = ZSTD_compressBound(thisBlockSize);
229 srcPtr += thisBlockSize;
230 cPtr += blockTable[nbBlocks].cRoom;
231 resPtr += thisBlockSize;
232 remaining -= thisBlockSize;
Yann Collet1840d6a2016-01-20 15:39:06 +0100233 if (thisBlockSize > largestBlockSize) largestBlockSize = thisBlockSize;
Yann Colletfd416f12016-01-30 03:14:15 +0100234 } } }
Yann Collet1c00dc32015-10-21 08:22:25 +0100235
Yann Collet4856a002015-01-24 01:58:16 +0100236 /* warmimg up memory */
Yann Colletd062f132015-12-01 01:31:17 +0100237 RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
Yann Collet4856a002015-01-24 01:58:16 +0100238
239 /* Bench */
240 {
Yann Collet1d1ae402016-03-17 19:51:02 +0100241 U32 loopNb;
Yann Collet4856a002015-01-24 01:58:16 +0100242 size_t cSize = 0;
243 double fastestC = 100000000., fastestD = 100000000.;
244 double ratio = 0.;
245 U64 crcCheck = 0;
Yann Collet699b14d2016-03-17 19:37:33 +0100246 clock_t coolTime = clock();
Yann Collet4856a002015-01-24 01:58:16 +0100247
248 DISPLAY("\r%79s\r", "");
Yann Collet1d1ae402016-03-17 19:51:02 +0100249 for (loopNb = 1; loopNb <= (g_nbIterations + !g_nbIterations); loopNb++) {
Yann Collet4856a002015-01-24 01:58:16 +0100250 int nbLoops;
Yann Collet1c00dc32015-10-21 08:22:25 +0100251 U32 blockNb;
Yann Collet699b14d2016-03-17 19:37:33 +0100252 clock_t clockStart, clockSpan;
Yann Collet1d1ae402016-03-17 19:51:02 +0100253 clock_t const clockLoop = g_nbIterations ? TIMELOOP_S * CLOCKS_PER_SEC : 10;
Yann Collet4856a002015-01-24 01:58:16 +0100254
Yann Collet27d3dad2016-03-11 13:41:20 +0100255 /* overheat protection */
Yann Collet699b14d2016-03-17 19:37:33 +0100256 if (BMK_clockSpan(coolTime) > ACTIVEPERIOD_S * CLOCKS_PER_SEC) {
Yann Collet27d3dad2016-03-11 13:41:20 +0100257 DISPLAY("\rcooling down ... \r");
258 BMK_sleep(COOLPERIOD_S);
Yann Collet699b14d2016-03-17 19:37:33 +0100259 coolTime = clock();
Yann Collet27d3dad2016-03-11 13:41:20 +0100260 }
261
Yann Collet4856a002015-01-24 01:58:16 +0100262 /* Compression */
Yann Colleted699e62015-12-16 02:37:24 +0100263 DISPLAY("%2i-%-17.17s :%10u ->\r", loopNb, displayName, (U32)srcSize);
Yann Collet699b14d2016-03-17 19:37:33 +0100264 memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100265
266 nbLoops = 0;
Yann Collet699b14d2016-03-17 19:37:33 +0100267 clockStart = clock();
268 while (clock() == clockStart);
269 clockStart = clock();
Yann Collet1d1ae402016-03-17 19:51:02 +0100270 while (BMK_clockSpan(clockStart) < clockLoop) {
Yann Collet2ce49232016-02-02 14:36:49 +0100271 ZSTD_compressBegin_advanced(refCtx, dictBuffer, dictBufferSize, ZSTD_getParams(cLevel, MAX(dictBufferSize, largestBlockSize)));
Yann Colletfb810d62016-01-28 00:18:06 +0100272 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
Yann Colletfd416f12016-01-30 03:14:15 +0100273 size_t rSize = ZSTD_compress_usingPreparedCCtx(ctx, refCtx,
274 blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
275 blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize);
276 if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingPreparedCCtx() failed : %s", ZSTD_getErrorName(rSize));
Yann Collet2630a5e2016-01-14 19:13:22 +0100277 blockTable[blockNb].cSize = rSize;
Yann Collet2630a5e2016-01-14 19:13:22 +0100278 }
Yann Collet4856a002015-01-24 01:58:16 +0100279 nbLoops++;
280 }
Yann Collet699b14d2016-03-17 19:37:33 +0100281 clockSpan = BMK_clockSpan(clockStart);
Yann Collet4856a002015-01-24 01:58:16 +0100282
Yann Collet1c00dc32015-10-21 08:22:25 +0100283 cSize = 0;
284 for (blockNb=0; blockNb<nbBlocks; blockNb++)
285 cSize += blockTable[blockNb].cSize;
Yann Collet699b14d2016-03-17 19:37:33 +0100286 if ((double)clockSpan < fastestC*nbLoops) fastestC = (double)clockSpan / nbLoops;
Yann Collet2acb5d32015-10-29 16:49:43 +0100287 ratio = (double)srcSize / (double)cSize;
Yann Collet699b14d2016-03-17 19:37:33 +0100288 DISPLAY("%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r",
289 loopNb, displayName, (U32)srcSize, (U32)cSize, ratio,
290 (double)srcSize / 1000000. / (fastestC / CLOCKS_PER_SEC) );
Yann Collet4856a002015-01-24 01:58:16 +0100291
Yann Collete93d6ce2016-01-31 00:58:06 +0100292#if 1
Yann Collet4856a002015-01-24 01:58:16 +0100293 /* Decompression */
Yann Collet7b51a292016-01-26 15:58:49 +0100294 memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100295
296 nbLoops = 0;
Yann Collet699b14d2016-03-17 19:37:33 +0100297 clockStart = clock();
298 while (clock() == clockStart);
299 clockStart = clock();
Yann Collet7b51a292016-01-26 15:58:49 +0100300
Yann Collet1d1ae402016-03-17 19:51:02 +0100301 for ( ; BMK_clockSpan(clockStart) < clockLoop; nbLoops++) {
Yann Collete93d6ce2016-01-31 00:58:06 +0100302 ZSTD_decompressBegin_usingDict(refDCtx, dictBuffer, dictBufferSize);
Yann Colletb923f652016-01-26 03:14:20 +0100303 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
Yann Collete93d6ce2016-01-31 00:58:06 +0100304 size_t regenSize = ZSTD_decompress_usingPreparedDCtx(dctx, refDCtx,
Yann Collet7b51a292016-01-26 15:58:49 +0100305 blockTable[blockNb].resPtr, blockTable[blockNb].srcSize,
306 blockTable[blockNb].cPtr, blockTable[blockNb].cSize);
Yann Collete93d6ce2016-01-31 00:58:06 +0100307 if (ZSTD_isError(regenSize)) {
308 DISPLAY("ZSTD_decompress_usingPreparedDCtx() failed on block %u : %s",
Yann Colletfb810d62016-01-28 00:18:06 +0100309 blockNb, ZSTD_getErrorName(regenSize));
Yann Collete93d6ce2016-01-31 00:58:06 +0100310 goto _findError;
311 }
Yann Collet7b51a292016-01-26 15:58:49 +0100312 blockTable[blockNb].resSize = regenSize;
313 } }
314
Yann Collet699b14d2016-03-17 19:37:33 +0100315 clockSpan = BMK_clockSpan(clockStart);
316 if ((double)clockSpan < fastestD*nbLoops) fastestD = (double)clockSpan / nbLoops;
317 DISPLAY("%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r",
318 loopNb, displayName, (U32)srcSize, (U32)cSize, ratio,
319 (double)srcSize / 1000000. / (fastestC / CLOCKS_PER_SEC),
320 (double)srcSize / 1000000. / (fastestD / CLOCKS_PER_SEC) );
Yann Collet4856a002015-01-24 01:58:16 +0100321
322 /* CRC Checking */
Yann Collete93d6ce2016-01-31 00:58:06 +0100323_findError:
Yann Collet4856a002015-01-24 01:58:16 +0100324 crcCheck = XXH64(resultBuffer, srcSize, 0);
Yann Collet7b51a292016-01-26 15:58:49 +0100325 if (crcOrig!=crcCheck) {
Yann Collet03a6dab2016-01-21 02:21:17 +0100326 size_t u;
Yann Colleted699e62015-12-16 02:37:24 +0100327 DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n", displayName, (unsigned)crcOrig, (unsigned)crcCheck);
Yann Collet7b51a292016-01-26 15:58:49 +0100328 for (u=0; u<srcSize; u++) {
329 if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) {
Yann Collet59d1f792016-01-23 19:28:41 +0100330 U32 segNb, bNb, pos;
Yann Collet03a6dab2016-01-21 02:21:17 +0100331 size_t bacc = 0;
332 printf("Decoding error at pos %u ", (U32)u);
Yann Collet7b51a292016-01-26 15:58:49 +0100333 for (segNb = 0; segNb < nbBlocks; segNb++) {
Yann Collet59d1f792016-01-23 19:28:41 +0100334 if (bacc + blockTable[segNb].srcSize > u) break;
335 bacc += blockTable[segNb].srcSize;
Yann Collet03a6dab2016-01-21 02:21:17 +0100336 }
Yann Collet59d1f792016-01-23 19:28:41 +0100337 pos = (U32)(u - bacc);
338 bNb = pos / (128 KB);
Yann Colletfb810d62016-01-28 00:18:06 +0100339 printf("(block %u, sub %u, pos %u) \n", segNb, bNb, pos);
Yann Collet4856a002015-01-24 01:58:16 +0100340 break;
Yann Colletfb810d62016-01-28 00:18:06 +0100341 }
342 if (u==srcSize-1) { /* should never happen */
343 printf("no difference detected\n");
Yann Collet7b51a292016-01-26 15:58:49 +0100344 } }
Yann Collet4856a002015-01-24 01:58:16 +0100345 break;
346 }
Yann Collete8c6bb12015-07-26 00:23:57 +0100347#endif
Yann Collet4856a002015-01-24 01:58:16 +0100348 }
349
Yann Collet699b14d2016-03-17 19:37:33 +0100350 DISPLAY("%2i-\n", cLevel);
Yann Collet4856a002015-01-24 01:58:16 +0100351 }
352
Yann Colleted699e62015-12-16 02:37:24 +0100353 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100354 free(compressedBuffer);
355 free(resultBuffer);
Yann Collet2630a5e2016-01-14 19:13:22 +0100356 ZSTD_freeCCtx(refCtx);
Yann Collet31683c02015-12-18 01:26:48 +0100357 ZSTD_freeCCtx(ctx);
Yann Collet7b51a292016-01-26 15:58:49 +0100358 ZSTD_freeDCtx(refDCtx);
Yann Collet31683c02015-12-18 01:26:48 +0100359 ZSTD_freeDCtx(dctx);
Yann Collet4856a002015-01-24 01:58:16 +0100360 return 0;
361}
362
363
Yann Collet4856a002015-01-24 01:58:16 +0100364static size_t BMK_findMaxMem(U64 requiredMem)
365{
366 size_t step = 64 MB;
367 BYTE* testmem = NULL;
368
369 requiredMem = (((requiredMem >> 26) + 1) << 26);
370 requiredMem += 2 * step;
Yann Collet050efba2015-11-03 09:49:30 +0100371 if (requiredMem > maxMemory) requiredMem = maxMemory;
Yann Collet4856a002015-01-24 01:58:16 +0100372
Yann Colletfd416f12016-01-30 03:14:15 +0100373 while (!testmem) {
Yann Collet4856a002015-01-24 01:58:16 +0100374 requiredMem -= step;
375 testmem = (BYTE*)malloc((size_t)requiredMem);
376 }
Yann Collet4856a002015-01-24 01:58:16 +0100377 free(testmem);
378 return (size_t)(requiredMem - step);
379}
380
Yann Colleted699e62015-12-16 02:37:24 +0100381static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize,
382 const char* displayName, int cLevel,
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{
Yann Collet699b14d2016-03-17 19:37:33 +0100386 if (cLevel < 0) { /* range mode : test all levels from 1 to l */
Yann Colletc776c462015-10-29 19:10:54 +0100387 int l;
Yann Collet27d3dad2016-03-11 13:41:20 +0100388 for (l=1; l <= -cLevel; l++) {
Yann Collet31683c02015-12-18 01:26:48 +0100389 BMK_benchMem(srcBuffer, benchedSize,
390 displayName, l,
391 fileSizes, nbFiles,
392 dictBuffer, dictBufferSize);
Yann Collet27d3dad2016-03-11 13:41:20 +0100393 }
Yann Colleted699e62015-12-16 02:37:24 +0100394 return;
Yann Colletc776c462015-10-29 19:10:54 +0100395 }
Yann Collet31683c02015-12-18 01:26:48 +0100396 BMK_benchMem(srcBuffer, benchedSize,
397 displayName, cLevel,
398 fileSizes, nbFiles,
399 dictBuffer, dictBufferSize);
Yann Colleted699e62015-12-16 02:37:24 +0100400}
401
402static U64 BMK_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
403{
404 U64 total = 0;
405 unsigned n;
406 for (n=0; n<nbFiles; n++)
407 total += BMK_getFileSize(fileNamesTable[n]);
408 return total;
409}
410
Yann Collet70611352015-12-16 03:01:03 +0100411static void BMK_loadFiles(void* buffer, size_t bufferSize,
412 size_t* fileSizes,
Yann Collet699b14d2016-03-17 19:37:33 +0100413 const char** fileNamesTable, unsigned const nbFiles)
Yann Colleted699e62015-12-16 02:37:24 +0100414{
Yann Colleted699e62015-12-16 02:37:24 +0100415 size_t pos = 0;
Yann Colleted699e62015-12-16 02:37:24 +0100416
Yann Collet699b14d2016-03-17 19:37:33 +0100417 unsigned n;
Yann Colletfd416f12016-01-30 03:14:15 +0100418 for (n=0; n<nbFiles; n++) {
Yann Colleted699e62015-12-16 02:37:24 +0100419 size_t readSize;
420 U64 fileSize = BMK_getFileSize(fileNamesTable[n]);
421 FILE* f = fopen(fileNamesTable[n], "rb");
422 if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]);
423 DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[n]);
424 if (fileSize > bufferSize-pos) fileSize = bufferSize-pos;
Yann Collet699b14d2016-03-17 19:37:33 +0100425 readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
Yann Colleted699e62015-12-16 02:37:24 +0100426 if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]);
427 pos += readSize;
Yann Colleta52c98d2015-12-16 03:12:31 +0100428 fileSizes[n] = (size_t)fileSize;
Yann Colleted699e62015-12-16 02:37:24 +0100429 fclose(f);
430 }
431}
432
Yann Collet31683c02015-12-18 01:26:48 +0100433static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles,
434 const char* dictFileName, int cLevel)
Yann Colleted699e62015-12-16 02:37:24 +0100435{
436 void* srcBuffer;
437 size_t benchedSize;
Yann Collet31683c02015-12-18 01:26:48 +0100438 void* dictBuffer = NULL;
439 size_t dictBufferSize = 0;
440 size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t));
Yann Colleted699e62015-12-16 02:37:24 +0100441 U64 totalSizeToLoad = BMK_getTotalFileSize(fileNamesTable, nbFiles);
442 char mfName[20] = {0};
443 const char* displayName = NULL;
444
Yann Collet31683c02015-12-18 01:26:48 +0100445 if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes");
446
447 /* Load dictionary */
Yann Colletfd416f12016-01-30 03:14:15 +0100448 if (dictFileName != NULL) {
Yann Collet31683c02015-12-18 01:26:48 +0100449 U64 dictFileSize = BMK_getFileSize(dictFileName);
450 if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName);
451 dictBufferSize = (size_t)dictFileSize;
452 dictBuffer = malloc(dictBufferSize);
453 if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize);
454 BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1);
455 }
456
Yann Colleted699e62015-12-16 02:37:24 +0100457 /* Memory allocation & restrictions */
458 benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
459 if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
460 if (benchedSize < totalSizeToLoad)
461 DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20));
462 srcBuffer = malloc(benchedSize);
463 if (!srcBuffer) EXM_THROW(12, "not enough memory");
464
465 /* Load input buffer */
Yann Collet70611352015-12-16 03:01:03 +0100466 BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +0100467
468 /* Bench */
469 snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
470 if (nbFiles > 1) displayName = mfName;
471 else displayName = fileNamesTable[0];
472
Yann Collet31683c02015-12-18 01:26:48 +0100473 BMK_benchCLevel(srcBuffer, benchedSize,
474 displayName, cLevel,
475 fileSizes, nbFiles,
476 dictBuffer, dictBufferSize);
Yann Collet4856a002015-01-24 01:58:16 +0100477
Yann Colleteeb8ba12015-10-22 16:55:40 +0100478 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100479 free(srcBuffer);
Yann Collet31683c02015-12-18 01:26:48 +0100480 free(dictBuffer);
Yann Collet70611352015-12-16 03:01:03 +0100481 free(fileSizes);
Yann Collet4856a002015-01-24 01:58:16 +0100482}
483
484
Yann Colleted699e62015-12-16 02:37:24 +0100485static void BMK_syntheticTest(int cLevel, double compressibility)
Yann Collet4856a002015-01-24 01:58:16 +0100486{
Yann Colleted699e62015-12-16 02:37:24 +0100487 char name[20] = {0};
Yann Collet4856a002015-01-24 01:58:16 +0100488 size_t benchedSize = 10000000;
489 void* srcBuffer = malloc(benchedSize);
Yann Collet4856a002015-01-24 01:58:16 +0100490
Yann Collet4856a002015-01-24 01:58:16 +0100491 /* Memory allocation */
Yann Colleted699e62015-12-16 02:37:24 +0100492 if (!srcBuffer) EXM_THROW(21, "not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100493
494 /* Fill input buffer */
Yann Colletd062f132015-12-01 01:31:17 +0100495 RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100496
497 /* Bench */
Yann Colleted699e62015-12-16 02:37:24 +0100498 snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
Yann Collet31683c02015-12-18 01:26:48 +0100499 BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, &benchedSize, 1, NULL, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100500
Yann Colleted699e62015-12-16 02:37:24 +0100501 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100502 free(srcBuffer);
Yann Collet4856a002015-01-24 01:58:16 +0100503}
504
505
Yann Collet31683c02015-12-18 01:26:48 +0100506int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
507 const char* dictFileName, int cLevel)
Yann Collet4856a002015-01-24 01:58:16 +0100508{
Yann Collet699b14d2016-03-17 19:37:33 +0100509 double const compressibility = (double)g_compressibilityDefault / 100;
Yann Collet4856a002015-01-24 01:58:16 +0100510
511 if (nbFiles == 0)
Yann Collet4856a002015-01-24 01:58:16 +0100512 BMK_syntheticTest(cLevel, compressibility);
Yann Collet4856a002015-01-24 01:58:16 +0100513 else
Yann Collet31683c02015-12-18 01:26:48 +0100514 BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel);
Yann Collet4856a002015-01-24 01:58:16 +0100515 return 0;
516}
517