blob: f9493e3b0707d851abc6c88e0d6f1d2fb6407f74 [file] [log] [blame]
Yann Collet4ded9e52016-08-30 10:04:33 -07001/**
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 */
Yann Collet4856a002015-01-24 01:58:16 +01009
Yann Collet4856a002015-01-24 01:58:16 +010010
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010011
12/* **************************************
Yann Collet500014a2017-01-19 16:59:56 -080013* Tuning parameters
14****************************************/
15#ifndef BMK_TIMETEST_DEFAULT_S /* default minimum time per test */
16#define BMK_TIMETEST_DEFAULT_S 3
17#endif
18
19
20/* **************************************
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010021* Compiler Warnings
22****************************************/
23#ifdef _MSC_VER
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010024# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
25#endif
26
27
Yann Colletf3eca252015-10-22 15:31:46 +010028/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010029* Includes
Yann Colletf3eca252015-10-22 15:31:46 +010030***************************************/
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +010031#include "platform.h" /* Large Files support */
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010032#include "util.h" /* UTIL_getFileSize, UTIL_sleep */
Yann Collet4856a002015-01-24 01:58:16 +010033#include <stdlib.h> /* malloc, free */
34#include <string.h> /* memset */
Yann Colletc6a64172016-12-31 03:31:26 +010035#include <stdio.h> /* fprintf, fopen */
Yann Collet7ae67bb2016-09-06 06:28:05 +020036#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
inikep4c12f232016-03-29 14:52:13 +020037
Yann Colletf3eca252015-10-22 15:31:46 +010038#include "mem.h"
Yann Colletd3b7f8d2016-06-04 19:47:02 +020039#define ZSTD_STATIC_LINKING_ONLY
40#include "zstd.h"
inikep69fcd7c2016-04-28 12:23:33 +020041#include "datagen.h" /* RDG_genBuffer */
Yann Collet4856a002015-01-24 01:58:16 +010042#include "xxhash.h"
Nick Terrell83c387e2017-01-26 15:25:32 -080043#include "zstdmt_compress.h"
Yann Collet4856a002015-01-24 01:58:16 +010044
45
Yann Colletf3eca252015-10-22 15:31:46 +010046/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010047* Constants
Yann Colletf3eca252015-10-22 15:31:46 +010048***************************************/
inikepf2f59d72016-06-22 15:42:26 +020049#ifndef ZSTD_GIT_COMMIT
inikepd7d251c2016-06-22 16:13:25 +020050# define ZSTD_GIT_COMMIT_STRING ""
51#else
52# define ZSTD_GIT_COMMIT_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_GIT_COMMIT)
inikepf2f59d72016-06-22 15:42:26 +020053#endif
54
inikep83c76b42016-04-28 13:16:01 +020055#define TIMELOOP_MICROSEC 1*1000000ULL /* 1 second */
56#define ACTIVEPERIOD_MICROSEC 70*1000000ULL /* 70 seconds */
57#define COOLPERIOD_SEC 10
Yann Collet4856a002015-01-24 01:58:16 +010058
59#define KB *(1 <<10)
60#define MB *(1 <<20)
61#define GB *(1U<<30)
62
Yann Collet87c18b22016-08-26 01:43:47 +020063static 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 +010064
65static U32 g_compressibilityDefault = 50;
Yann Collet4856a002015-01-24 01:58:16 +010066
67
Yann Colletf3eca252015-10-22 15:31:46 +010068/* *************************************
Yann Colleted699e62015-12-16 02:37:24 +010069* console display
Yann Colletf3eca252015-10-22 15:31:46 +010070***************************************/
Yann Colleted699e62015-12-16 02:37:24 +010071#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
72#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
Sean Purcell042ba122017-03-23 11:13:52 -070073static int g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
Yann Colleted699e62015-12-16 02:37:24 +010074
Yann Colletbf2bc112016-08-02 23:48:13 +020075#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
76 if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \
77 { g_time = clock(); DISPLAY(__VA_ARGS__); \
Sean Purcell042ba122017-03-23 11:13:52 -070078 if (g_displayLevel>=4) fflush(stderr); } }
Yann Colletbf2bc112016-08-02 23:48:13 +020079static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100;
80static clock_t g_time = 0;
81
Yann Colleted699e62015-12-16 02:37:24 +010082
83/* *************************************
84* Exceptions
85***************************************/
86#ifndef DEBUG
87# define DEBUG 0
88#endif
Yann Colletfe234bf2017-06-19 15:23:19 -070089#define DEBUGOUTPUT(...) { if (DEBUG) DISPLAY(__VA_ARGS__); }
90#define EXM_THROW(error, ...) { \
91 DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
92 DISPLAYLEVEL(1, "Error %i : ", error); \
93 DISPLAYLEVEL(1, __VA_ARGS__); \
94 DISPLAYLEVEL(1, " \n"); \
95 exit(error); \
Yann Colleted699e62015-12-16 02:37:24 +010096}
Yann Collet4856a002015-01-24 01:58:16 +010097
98
Yann Colletf3eca252015-10-22 15:31:46 +010099/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100100* Benchmark Parameters
Yann Colletf3eca252015-10-22 15:31:46 +0100101***************************************/
Yann Colletd9465012016-12-06 16:49:23 -0800102static int g_additionalParam = 0;
103static U32 g_decodeOnly = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100104
inikep4e26bb62016-03-14 12:48:51 +0100105void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; }
106
inikepeaba91a2016-03-23 20:30:26 +0100107void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; }
108
Yann Collet500014a2017-01-19 16:59:56 -0800109static U32 g_nbSeconds = BMK_TIMETEST_DEFAULT_S;
110void BMK_setNbSeconds(unsigned nbSeconds)
Yann Collet4856a002015-01-24 01:58:16 +0100111{
Przemyslaw Skibinski26306fc2016-11-03 11:38:01 +0100112 g_nbSeconds = nbSeconds;
Yann Collet500014a2017-01-19 16:59:56 -0800113 DISPLAYLEVEL(3, "- test >= %u seconds per compression / decompression - \n", g_nbSeconds);
Yann Collet4856a002015-01-24 01:58:16 +0100114}
115
Yann Collet500014a2017-01-19 16:59:56 -0800116static size_t g_blockSize = 0;
117void BMK_setBlockSize(size_t blockSize)
Yann Collet1c00dc32015-10-21 08:22:25 +0100118{
119 g_blockSize = blockSize;
Yann Collet500014a2017-01-19 16:59:56 -0800120 if (g_blockSize) DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10));
Yann Collet1c00dc32015-10-21 08:22:25 +0100121}
122
Yann Colletab7a5792016-12-28 16:11:09 +0100123void BMK_setDecodeOnlyMode(unsigned decodeFlag) { g_decodeOnly = (decodeFlag>0); }
124
Yann Collet500014a2017-01-19 16:59:56 -0800125static U32 g_nbThreads = 1;
126void BMK_setNbThreads(unsigned nbThreads) {
127#ifndef ZSTD_MULTITHREAD
128 if (nbThreads > 1) DISPLAYLEVEL(2, "Note : multi-threading is disabled \n");
129#endif
130 g_nbThreads = nbThreads;
131}
Yann Collet4856a002015-01-24 01:58:16 +0100132
Yann Collet3d93f2f2016-12-27 07:19:36 +0100133
Yann Colletf3eca252015-10-22 15:31:46 +0100134/* ********************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100135* Bench functions
Yann Colletf3eca252015-10-22 15:31:46 +0100136**********************************************************/
Yann Colletd9465012016-12-06 16:49:23 -0800137typedef struct {
138 const void* srcPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100139 size_t srcSize;
Yann Colletd9465012016-12-06 16:49:23 -0800140 void* cPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100141 size_t cRoom;
142 size_t cSize;
Yann Colletd9465012016-12-06 16:49:23 -0800143 void* resPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100144 size_t resSize;
145} blockParam_t;
146
Yann Collet99909862016-04-09 16:17:18 +0200147
Sean Purcellf876f122017-04-13 12:33:45 -0700148
Sean Purcell42bac7f2017-04-13 15:35:05 -0700149#undef MIN
150#undef MAX
151#define MIN(a,b) ((a) < (b) ? (a) : (b))
152#define MAX(a,b) ((a) > (b) ? (a) : (b))
Yann Collet1c00dc32015-10-21 08:22:25 +0100153
Yann Colleted699e62015-12-16 02:37:24 +0100154static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
inikep472638c2016-03-23 12:28:28 +0100155 const char* displayName, int cLevel,
Yann Collet31683c02015-12-18 01:26:48 +0100156 const size_t* fileSizes, U32 nbFiles,
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100157 const void* dictBuffer, size_t dictBufferSize,
Yann Colletc2007382017-04-04 15:35:06 -0700158 const ZSTD_compressionParameters* comprParams)
Yann Collet1c00dc32015-10-21 08:22:25 +0100159{
Yann Collete63c6312016-12-06 17:46:49 -0800160 size_t const blockSize = ((g_blockSize>=32 && !g_decodeOnly) ? g_blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ;
Yann Colletd64f4352016-03-21 00:07:42 +0100161 U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
Yann Colleted699e62015-12-16 02:37:24 +0100162 blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t));
Yann Colletb9151402016-03-26 17:18:11 +0100163 size_t const maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */
Yann Collet1c00dc32015-10-21 08:22:25 +0100164 void* const compressedBuffer = malloc(maxCompressedSize);
Yann Colletd9465012016-12-06 16:49:23 -0800165 void* resultBuffer = malloc(srcSize);
Yann Collet458c8a92017-01-19 17:44:15 -0800166 ZSTDMT_CCtx* const mtctx = ZSTDMT_createCCtx(g_nbThreads);
Yann Collet3c242e72016-07-13 14:56:24 +0200167 ZSTD_CCtx* const ctx = ZSTD_createCCtx();
168 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
Yann Collete63c6312016-12-06 17:46:49 -0800169 size_t const loadedCompressedSize = srcSize;
170 size_t cSize = 0;
171 double ratio = 0.;
Yann Colletde406ee2016-03-20 15:46:10 +0100172 U32 nbBlocks;
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100173 UTIL_freq_t ticksPerSecond;
Yann Colletde406ee2016-03-20 15:46:10 +0100174
175 /* checks */
Yann Colletec224d22016-06-27 13:39:30 +0200176 if (!compressedBuffer || !resultBuffer || !blockTable || !ctx || !dctx)
Yann Collet3d2cd7f2016-06-27 15:12:26 +0200177 EXM_THROW(31, "allocation error : not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100178
Yann Collet4856a002015-01-24 01:58:16 +0100179 /* init */
Yann Colletc2007382017-04-04 15:35:06 -0700180 if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* display last 17 characters */
inikepaaaf9232016-05-09 16:19:25 +0200181 UTIL_initTimer(&ticksPerSecond);
inikep4c12f232016-03-29 14:52:13 +0200182
Yann Collet5bde4be2017-03-29 12:10:38 -0700183 if (g_decodeOnly) { /* benchmark only decompression : source must be already compressed */
Yann Colletc2007382017-04-04 15:35:06 -0700184 const char* srcPtr = (const char*)srcBuffer;
185 U64 totalDSize64 = 0;
Yann Collete63c6312016-12-06 17:46:49 -0800186 U32 fileNb;
187 for (fileNb=0; fileNb<nbFiles; fileNb++) {
Sean Purcell4e709712017-02-07 13:50:09 -0800188 U64 const fSize64 = ZSTD_findDecompressedSize(srcPtr, fileSizes[fileNb]);
Yann Collete63c6312016-12-06 17:46:49 -0800189 if (fSize64==0) EXM_THROW(32, "Impossible to determine original size ");
Yann Colletc2007382017-04-04 15:35:06 -0700190 totalDSize64 += fSize64;
Yann Collete63c6312016-12-06 17:46:49 -0800191 srcPtr += fileSizes[fileNb];
192 }
Yann Colletc2007382017-04-04 15:35:06 -0700193 { size_t const decodedSize = (size_t)totalDSize64;
194 if (totalDSize64 > decodedSize) EXM_THROW(32, "original size is too large"); /* size_t overflow */
Yann Collete63c6312016-12-06 17:46:49 -0800195 free(resultBuffer);
196 resultBuffer = malloc(decodedSize);
197 if (!resultBuffer) EXM_THROW(33, "not enough memory");
198 cSize = srcSize;
199 srcSize = decodedSize;
200 ratio = (double)srcSize / (double)cSize;
201 } }
202
Yann Collet1c00dc32015-10-21 08:22:25 +0100203 /* Init blockTable data */
Yann Collete63c6312016-12-06 17:46:49 -0800204 { const char* srcPtr = (const char*)srcBuffer;
Yann Collet1c00dc32015-10-21 08:22:25 +0100205 char* cPtr = (char*)compressedBuffer;
206 char* resPtr = (char*)resultBuffer;
Yann Collet699b14d2016-03-17 19:37:33 +0100207 U32 fileNb;
Yann Colletde406ee2016-03-20 15:46:10 +0100208 for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
Yann Collet70611352015-12-16 03:01:03 +0100209 size_t remaining = fileSizes[fileNb];
Yann Collete63c6312016-12-06 17:46:49 -0800210 U32 const nbBlocksforThisFile = g_decodeOnly ? 1 : (U32)((remaining + (blockSize-1)) / blockSize);
Yann Collet699b14d2016-03-17 19:37:33 +0100211 U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
Yann Colletfd416f12016-01-30 03:14:15 +0100212 for ( ; nbBlocks<blockEnd; nbBlocks++) {
Yann Colletde406ee2016-03-20 15:46:10 +0100213 size_t const thisBlockSize = MIN(remaining, blockSize);
Yann Colletd9465012016-12-06 16:49:23 -0800214 blockTable[nbBlocks].srcPtr = (const void*)srcPtr;
Yann Colleted699e62015-12-16 02:37:24 +0100215 blockTable[nbBlocks].srcSize = thisBlockSize;
Yann Colletd9465012016-12-06 16:49:23 -0800216 blockTable[nbBlocks].cPtr = (void*)cPtr;
Yann Collete63c6312016-12-06 17:46:49 -0800217 blockTable[nbBlocks].cRoom = g_decodeOnly ? thisBlockSize : ZSTD_compressBound(thisBlockSize);
218 blockTable[nbBlocks].cSize = blockTable[nbBlocks].cRoom;
Yann Colletd9465012016-12-06 16:49:23 -0800219 blockTable[nbBlocks].resPtr = (void*)resPtr;
Sean Purcell4e709712017-02-07 13:50:09 -0800220 blockTable[nbBlocks].resSize = g_decodeOnly ? (size_t) ZSTD_findDecompressedSize(srcPtr, thisBlockSize) : thisBlockSize;
Yann Colleted699e62015-12-16 02:37:24 +0100221 srcPtr += thisBlockSize;
222 cPtr += blockTable[nbBlocks].cRoom;
223 resPtr += thisBlockSize;
224 remaining -= thisBlockSize;
Yann Collete63c6312016-12-06 17:46:49 -0800225 } } }
Yann Collet1c00dc32015-10-21 08:22:25 +0100226
Yann Collet4856a002015-01-24 01:58:16 +0100227 /* warmimg up memory */
Yann Colletd062f132015-12-01 01:31:17 +0100228 RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
Yann Collet4856a002015-01-24 01:58:16 +0100229
230 /* Bench */
inikep6d157f12016-04-15 16:54:11 +0200231 { U64 fastestC = (U64)(-1LL), fastestD = (U64)(-1LL);
Yann Colletd9465012016-12-06 16:49:23 -0800232 U64 const crcOrig = g_decodeOnly ? 0 : XXH64(srcBuffer, srcSize, 0);
Przemyslaw Skibinski74dcd8d2017-02-21 12:22:05 +0100233 UTIL_time_t coolTime;
Yann Collet4f5350f2016-11-29 13:12:24 -0800234 U64 const maxTime = (g_nbSeconds * TIMELOOP_MICROSEC) + 1;
Yann Colleta9febe82016-08-01 13:37:17 +0200235 U64 totalCTime=0, totalDTime=0;
Yann Colletd9465012016-12-06 16:49:23 -0800236 U32 cCompleted=g_decodeOnly, dCompleted=0;
Yann Colleta9febe82016-08-01 13:37:17 +0200237# define NB_MARKS 4
238 const char* const marks[NB_MARKS] = { " |", " /", " =", "\\" };
239 U32 markNb = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100240
Yann Colletf8804d12017-01-20 17:23:19 -0800241 UTIL_getTime(&coolTime);
inikep4e26bb62016-03-14 12:48:51 +0100242 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Colletd9465012016-12-06 16:49:23 -0800243 while (!cCompleted || !dCompleted) {
Yann Collet4856a002015-01-24 01:58:16 +0100244
Yann Collet27d3dad2016-03-11 13:41:20 +0100245 /* overheat protection */
Przemyslaw Skibinski74dcd8d2017-02-21 12:22:05 +0100246 if (UTIL_clockSpanMicro(coolTime, ticksPerSecond) > ACTIVEPERIOD_MICROSEC) {
Yann Collet235911e2016-07-31 01:32:48 +0200247 DISPLAYLEVEL(2, "\rcooling down ... \r");
inikep83c76b42016-04-28 13:16:01 +0200248 UTIL_sleep(COOLPERIOD_SEC);
cyan49735fba09f2017-01-20 12:23:30 -0800249 UTIL_getTime(&coolTime);
Yann Collet27d3dad2016-03-11 13:41:20 +0100250 }
Yann Collet4856a002015-01-24 01:58:16 +0100251
Yann Colletd9465012016-12-06 16:49:23 -0800252 if (!g_decodeOnly) {
Przemyslaw Skibinski74dcd8d2017-02-21 12:22:05 +0100253 UTIL_time_t clockStart;
Yann Colletd9465012016-12-06 16:49:23 -0800254 /* Compression */
255 DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (U32)srcSize);
256 if (!cCompleted) memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100257
Yann Colletd9465012016-12-06 16:49:23 -0800258 UTIL_sleepMilli(1); /* give processor time to other processes */
259 UTIL_waitForNextTick(ticksPerSecond);
Yann Colletf8804d12017-01-20 17:23:19 -0800260 UTIL_getTime(&clockStart);
Yann Colletde406ee2016-03-20 15:46:10 +0100261
Yann Colletd9465012016-12-06 16:49:23 -0800262 if (!cCompleted) { /* still some time to do compression tests */
Yann Colletc2007382017-04-04 15:35:06 -0700263 U64 const clockLoop = g_nbSeconds ? TIMELOOP_MICROSEC : 1;
Yann Colletd9465012016-12-06 16:49:23 -0800264 U32 nbLoops = 0;
Yann Colletc08e6492017-06-19 18:25:35 -0700265 ZSTD_CDict* cdict = NULL;
266#ifdef ZSTD_NEWAPI
267 ZSTD_CCtx_setParameter(ctx, ZSTD_p_nbThreads, g_nbThreads);
268 ZSTD_CCtx_setParameter(ctx, ZSTD_p_compressionLevel, cLevel);
269 ZSTD_CCtx_setParameter(ctx, ZSTD_p_windowLog, comprParams->windowLog);
270 ZSTD_CCtx_setParameter(ctx, ZSTD_p_chainLog, comprParams->chainLog);
271 ZSTD_CCtx_setParameter(ctx, ZSTD_p_searchLog, comprParams->searchLog);
272 ZSTD_CCtx_setParameter(ctx, ZSTD_p_minMatch, comprParams->searchLength);
273 ZSTD_CCtx_setParameter(ctx, ZSTD_p_targetLength, comprParams->targetLength);
274 ZSTD_CCtx_setParameter(ctx, ZSTD_p_compressionStrategy, comprParams->strategy);
275 ZSTD_CCtx_loadDictionary(ctx, dictBuffer, dictBufferSize);
276#else
277 size_t const avgSize = MIN(blockSize, (srcSize / nbFiles));
Yann Colletc2007382017-04-04 15:35:06 -0700278 ZSTD_parameters zparams = ZSTD_getParams(cLevel, avgSize, dictBufferSize);
Yann Colletc08e6492017-06-19 18:25:35 -0700279 ZSTD_customMem const cmem = { NULL, NULL, NULL };
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100280 if (comprParams->windowLog) zparams.cParams.windowLog = comprParams->windowLog;
281 if (comprParams->chainLog) zparams.cParams.chainLog = comprParams->chainLog;
282 if (comprParams->hashLog) zparams.cParams.hashLog = comprParams->hashLog;
283 if (comprParams->searchLog) zparams.cParams.searchLog = comprParams->searchLog;
284 if (comprParams->searchLength) zparams.cParams.searchLength = comprParams->searchLength;
285 if (comprParams->targetLength) zparams.cParams.targetLength = comprParams->targetLength;
Yann Colletc08e6492017-06-19 18:25:35 -0700286 if (comprParams->strategy) zparams.cParams.strategy = comprParams->strategy;
Yann Collet7bd1a292017-06-21 11:50:33 -0700287 cdict = ZSTD_createCDict_advanced(dictBuffer, dictBufferSize, 1 /*byRef*/, ZSTD_dm_auto, zparams.cParams, cmem);
Yann Colletc2007382017-04-04 15:35:06 -0700288 if (cdict==NULL) EXM_THROW(1, "ZSTD_createCDict_advanced() allocation failure");
Yann Colletc08e6492017-06-19 18:25:35 -0700289#endif
Yann Colletd9465012016-12-06 16:49:23 -0800290 do {
291 U32 blockNb;
Yann Colletd9465012016-12-06 16:49:23 -0800292 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
Yann Colletc08e6492017-06-19 18:25:35 -0700293 size_t rSize;
294#ifdef ZSTD_NEWAPI
295 ZSTD_outBuffer out = { blockTable[blockNb].cPtr, blockTable[blockNb].cRoom, 0 };
296 ZSTD_inBuffer in = { blockTable[blockNb].srcPtr, blockTable[blockNb].srcSize, 0 };
297 size_t cError = 1;
298 while (cError) {
299 cError = ZSTD_compress_generic(ctx,
300 &out, &in, ZSTD_e_end);
301 if (ZSTD_isError(cError))
302 EXM_THROW(1, "ZSTD_compress_generic() error : %s",
303 ZSTD_getErrorName(cError));
304 }
305 rSize = out.pos;
306#else /* ! ZSTD_NEWAPI */
Yann Colletd9465012016-12-06 16:49:23 -0800307 if (dictBufferSize) {
308 rSize = ZSTD_compress_usingCDict(ctx,
309 blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
310 blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize,
311 cdict);
Yann Collet458c8a92017-01-19 17:44:15 -0800312 } else {
Yann Colletc08e6492017-06-19 18:25:35 -0700313# ifdef ZSTD_MULTITHREAD /* note : limitation : MT single-pass does not support compression with dictionary */
Yann Collet458c8a92017-01-19 17:44:15 -0800314 rSize = ZSTDMT_compressCCtx(mtctx,
Yann Collet3d93f2f2016-12-27 07:19:36 +0100315 blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
316 blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize,
317 cLevel);
Yann Colletc08e6492017-06-19 18:25:35 -0700318# else
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100319 rSize = ZSTD_compress_advanced (ctx,
Yann Colletd9465012016-12-06 16:49:23 -0800320 blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
Yann Colletc08e6492017-06-19 18:25:35 -0700321 blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize,
322 NULL, 0, zparams);
323# endif
Yann Colletd9465012016-12-06 16:49:23 -0800324 }
Yann Colletc08e6492017-06-19 18:25:35 -0700325 if (ZSTD_isError(rSize))
326 EXM_THROW(1, "ZSTD_compress_usingCDict() failed : %s",
327 ZSTD_getErrorName(rSize));
328#endif /* ZSTD_NEWAPI */
Yann Colletd9465012016-12-06 16:49:23 -0800329 blockTable[blockNb].cSize = rSize;
Przemyslaw Skibinski2558b4c2016-11-18 11:46:30 +0100330 }
Yann Colletd9465012016-12-06 16:49:23 -0800331 nbLoops++;
Przemyslaw Skibinski74dcd8d2017-02-21 12:22:05 +0100332 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
Yann Colletd9465012016-12-06 16:49:23 -0800333 ZSTD_freeCDict(cdict);
Przemyslaw Skibinski74dcd8d2017-02-21 12:22:05 +0100334 { U64 const clockSpanMicro = UTIL_clockSpanMicro(clockStart, ticksPerSecond);
Yann Colletc6a64172016-12-31 03:31:26 +0100335 if (clockSpanMicro < fastestC*nbLoops) fastestC = clockSpanMicro / nbLoops;
336 totalCTime += clockSpanMicro;
Yann Colletd9465012016-12-06 16:49:23 -0800337 cCompleted = (totalCTime >= maxTime);
338 } }
Yann Collet4856a002015-01-24 01:58:16 +0100339
Yann Colletd9465012016-12-06 16:49:23 -0800340 cSize = 0;
341 { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; }
342 ratio = (double)srcSize / (double)cSize;
343 markNb = (markNb+1) % NB_MARKS;
344 DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r",
345 marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratio,
346 (double)srcSize / fastestC );
347 } else { /* g_decodeOnly */
Yann Collete63c6312016-12-06 17:46:49 -0800348 memcpy(compressedBuffer, srcBuffer, loadedCompressedSize);
Yann Colletd9465012016-12-06 16:49:23 -0800349 }
Yann Collet4856a002015-01-24 01:58:16 +0100350
Yann Collet5eb749e2017-01-11 18:21:25 +0100351#if 0 /* disable decompression test */
Yann Collet3d93f2f2016-12-27 07:19:36 +0100352 dCompleted=1;
353 (void)totalDTime; (void)fastestD; (void)crcOrig; /* unused when decompression disabled */
354#else
Yann Collet4856a002015-01-24 01:58:16 +0100355 /* Decompression */
Yann Colletd1733f72016-08-21 01:04:46 +0200356 if (!dCompleted) memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100357
inikep83c76b42016-04-28 13:16:01 +0200358 UTIL_sleepMilli(1); /* give processor time to other processes */
359 UTIL_waitForNextTick(ticksPerSecond);
Yann Collet7b51a292016-01-26 15:58:49 +0100360
Yann Colleta9febe82016-08-01 13:37:17 +0200361 if (!dCompleted) {
Yann Colletd9465012016-12-06 16:49:23 -0800362 U64 clockLoop = g_nbSeconds ? TIMELOOP_MICROSEC : 1;
Yann Colleta9febe82016-08-01 13:37:17 +0200363 U32 nbLoops = 0;
Przemyslaw Skibinski74dcd8d2017-02-21 12:22:05 +0100364 UTIL_time_t clockStart;
Yann Colletd9465012016-12-06 16:49:23 -0800365 ZSTD_DDict* const ddict = ZSTD_createDDict(dictBuffer, dictBufferSize);
Yann Colletee1a0842016-06-07 01:40:49 +0200366 if (!ddict) EXM_THROW(2, "ZSTD_createDDict() allocation failure");
Yann Colletf8804d12017-01-20 17:23:19 -0800367 UTIL_getTime(&clockStart);
inikepc5e1d292016-04-19 09:37:59 +0200368 do {
Yann Colleta5b66e32016-03-26 01:48:27 +0100369 U32 blockNb;
Yann Colleta5b66e32016-03-26 01:48:27 +0100370 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
Yann Colletee1a0842016-06-07 01:40:49 +0200371 size_t const regenSize = ZSTD_decompress_usingDDict(dctx,
Yann Collete63c6312016-12-06 17:46:49 -0800372 blockTable[blockNb].resPtr, blockTable[blockNb].resSize,
Yann Colletee1a0842016-06-07 01:40:49 +0200373 blockTable[blockNb].cPtr, blockTable[blockNb].cSize,
374 ddict);
Yann Colleta5b66e32016-03-26 01:48:27 +0100375 if (ZSTD_isError(regenSize)) {
Yann Collet5eb749e2017-01-11 18:21:25 +0100376 DISPLAY("ZSTD_decompress_usingDDict() failed on block %u of size %u : %s \n",
377 blockNb, (U32)blockTable[blockNb].cSize, ZSTD_getErrorName(regenSize));
inikep19bd48f2016-04-04 12:10:00 +0200378 clockLoop = 0; /* force immediate test end */
Yann Colleta5b66e32016-03-26 01:48:27 +0100379 break;
380 }
381 blockTable[blockNb].resSize = regenSize;
inikepc5e1d292016-04-19 09:37:59 +0200382 }
383 nbLoops++;
Przemyslaw Skibinski74dcd8d2017-02-21 12:22:05 +0100384 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
Yann Colletee1a0842016-06-07 01:40:49 +0200385 ZSTD_freeDDict(ddict);
Przemyslaw Skibinski74dcd8d2017-02-21 12:22:05 +0100386 { U64 const clockSpanMicro = UTIL_clockSpanMicro(clockStart, ticksPerSecond);
Yann Collet5eb749e2017-01-11 18:21:25 +0100387 if (clockSpanMicro < fastestD*nbLoops) fastestD = clockSpanMicro / nbLoops;
388 totalDTime += clockSpanMicro;
Yann Collet4f5350f2016-11-29 13:12:24 -0800389 dCompleted = (totalDTime >= maxTime);
Yann Collet7b51a292016-01-26 15:58:49 +0100390 } }
391
Yann Colleta9febe82016-08-01 13:37:17 +0200392 markNb = (markNb+1) % NB_MARKS;
393 DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r",
394 marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratio,
inikep06f793a2016-03-29 11:17:58 +0200395 (double)srcSize / fastestC,
396 (double)srcSize / fastestD );
Yann Collet4856a002015-01-24 01:58:16 +0100397
398 /* CRC Checking */
Yann Collete162ace2016-05-20 11:24:35 +0200399 { U64 const crcCheck = XXH64(resultBuffer, srcSize, 0);
Yann Colletd9465012016-12-06 16:49:23 -0800400 if (!g_decodeOnly && (crcOrig!=crcCheck)) {
Yann Colleta5b66e32016-03-26 01:48:27 +0100401 size_t u;
402 DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck);
403 for (u=0; u<srcSize; u++) {
404 if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) {
405 U32 segNb, bNb, pos;
406 size_t bacc = 0;
407 DISPLAY("Decoding error at pos %u ", (U32)u);
408 for (segNb = 0; segNb < nbBlocks; segNb++) {
409 if (bacc + blockTable[segNb].srcSize > u) break;
410 bacc += blockTable[segNb].srcSize;
411 }
412 pos = (U32)(u - bacc);
413 bNb = pos / (128 KB);
Yann Colletc08e6492017-06-19 18:25:35 -0700414 DISPLAY("(sample %u, block %u, pos %u) \n", segNb, bNb, pos);
Yann Collet736788f2017-01-19 12:12:50 -0800415 if (u>5) {
416 int n;
417 for (n=-5; n<0; n++) DISPLAY("%02X ", ((const BYTE*)srcBuffer)[u+n]);
418 DISPLAY(" :%02X: ", ((const BYTE*)srcBuffer)[u]);
419 for (n=1; n<3; n++) DISPLAY("%02X ", ((const BYTE*)srcBuffer)[u+n]);
420 DISPLAY(" \n");
421 for (n=-5; n<0; n++) DISPLAY("%02X ", ((const BYTE*)resultBuffer)[u+n]);
422 DISPLAY(" :%02X: ", ((const BYTE*)resultBuffer)[u]);
423 for (n=1; n<3; n++) DISPLAY("%02X ", ((const BYTE*)resultBuffer)[u+n]);
424 DISPLAY(" \n");
425 }
Yann Colleta5b66e32016-03-26 01:48:27 +0100426 break;
Yann Collet03a6dab2016-01-21 02:21:17 +0100427 }
Yann Colleta5b66e32016-03-26 01:48:27 +0100428 if (u==srcSize-1) { /* should never happen */
429 DISPLAY("no difference detected\n");
430 } }
431 break;
432 } } /* CRC Checking */
Yann Collete8c6bb12015-07-26 00:23:57 +0100433#endif
Przemyslaw Skibinski26306fc2016-11-03 11:38:01 +0100434 } /* for (testNb = 1; testNb <= (g_nbSeconds + !g_nbSeconds); testNb++) */
Yann Collet4856a002015-01-24 01:58:16 +0100435
inikep7132fb12016-08-10 14:59:18 +0200436 if (g_displayLevel == 1) {
437 double cSpeed = (double)srcSize / fastestC;
438 double dSpeed = (double)srcSize / fastestD;
439 if (g_additionalParam)
440 DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s (param=%d)\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName, g_additionalParam);
441 else
442 DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName);
443 }
inikep2872b6f2016-03-22 14:38:34 +0100444 DISPLAYLEVEL(2, "%2i#\n", cLevel);
Yann Colletde406ee2016-03-20 15:46:10 +0100445 } /* Bench */
Yann Collet4856a002015-01-24 01:58:16 +0100446
Yann Colleted699e62015-12-16 02:37:24 +0100447 /* clean up */
inikep0bd0fae2016-05-05 13:10:57 +0200448 free(blockTable);
Yann Collet4856a002015-01-24 01:58:16 +0100449 free(compressedBuffer);
450 free(resultBuffer);
Yann Collet458c8a92017-01-19 17:44:15 -0800451 ZSTDMT_freeCCtx(mtctx);
Yann Collet31683c02015-12-18 01:26:48 +0100452 ZSTD_freeCCtx(ctx);
453 ZSTD_freeDCtx(dctx);
Yann Collet4856a002015-01-24 01:58:16 +0100454 return 0;
455}
456
457
Yann Collet4856a002015-01-24 01:58:16 +0100458static size_t BMK_findMaxMem(U64 requiredMem)
459{
Yann Colletde406ee2016-03-20 15:46:10 +0100460 size_t const step = 64 MB;
Yann Collet4856a002015-01-24 01:58:16 +0100461 BYTE* testmem = NULL;
462
463 requiredMem = (((requiredMem >> 26) + 1) << 26);
Yann Colletde406ee2016-03-20 15:46:10 +0100464 requiredMem += step;
Yann Collet050efba2015-11-03 09:49:30 +0100465 if (requiredMem > maxMemory) requiredMem = maxMemory;
Yann Collet4856a002015-01-24 01:58:16 +0100466
Yann Colletde406ee2016-03-20 15:46:10 +0100467 do {
Yann Collet4856a002015-01-24 01:58:16 +0100468 testmem = (BYTE*)malloc((size_t)requiredMem);
Yann Colletde406ee2016-03-20 15:46:10 +0100469 requiredMem -= step;
470 } while (!testmem);
471
Yann Collet4856a002015-01-24 01:58:16 +0100472 free(testmem);
Yann Colletde406ee2016-03-20 15:46:10 +0100473 return (size_t)(requiredMem);
Yann Collet4856a002015-01-24 01:58:16 +0100474}
475
Yann Colleted699e62015-12-16 02:37:24 +0100476static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize,
inikepeaba91a2016-03-23 20:30:26 +0100477 const char* displayName, int cLevel, int cLevelLast,
Yann Collet31683c02015-12-18 01:26:48 +0100478 const size_t* fileSizes, unsigned nbFiles,
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100479 const void* dictBuffer, size_t dictBufferSize,
Przemyslaw Skibinskid05014c2017-02-07 16:48:01 +0100480 ZSTD_compressionParameters *compressionParams, int setRealTimePrio)
Yann Collet4856a002015-01-24 01:58:16 +0100481{
inikepe9554b72016-03-14 18:10:30 +0100482 int l;
inikep4e26bb62016-03-14 12:48:51 +0100483
inikepe9554b72016-03-14 18:10:30 +0100484 const char* pch = strrchr(displayName, '\\'); /* Windows */
485 if (!pch) pch = strrchr(displayName, '/'); /* Linux */
486 if (pch) displayName = pch+1;
inikep4e26bb62016-03-14 12:48:51 +0100487
Przemyslaw Skibinskid05014c2017-02-07 16:48:01 +0100488 if (setRealTimePrio) {
489 DISPLAYLEVEL(2, "Note : switching to a real-time priority \n");
490 SET_REALTIME_PRIORITY;
491 }
inikepea4ee3e2016-04-25 13:09:06 +0200492
inikepeaba91a2016-03-23 20:30:26 +0100493 if (g_displayLevel == 1 && !g_additionalParam)
Przemyslaw Skibinski26306fc2016-11-03 11:38:01 +0100494 DISPLAY("bench %s %s: input %u bytes, %u seconds, %u KB blocks\n", ZSTD_VERSION_STRING, ZSTD_GIT_COMMIT_STRING, (U32)benchedSize, g_nbSeconds, (U32)(g_blockSize>>10));
inikepe9554b72016-03-14 18:10:30 +0100495
496 if (cLevelLast < cLevel) cLevelLast = cLevel;
497
Yann Collet99909862016-04-09 16:17:18 +0200498 for (l=cLevel; l <= cLevelLast; l++) {
inikepe9554b72016-03-14 18:10:30 +0100499 BMK_benchMem(srcBuffer, benchedSize,
inikep472638c2016-03-23 12:28:28 +0100500 displayName, l,
inikepe9554b72016-03-14 18:10:30 +0100501 fileSizes, nbFiles,
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +0100502 dictBuffer, dictBufferSize, compressionParams);
inikep7132fb12016-08-10 14:59:18 +0200503 }
Yann Colleted699e62015-12-16 02:37:24 +0100504}
505
Yann Colleted699e62015-12-16 02:37:24 +0100506
Yann Colleta5b66e32016-03-26 01:48:27 +0100507/*! BMK_loadFiles() :
508 Loads `buffer` with content of files listed within `fileNamesTable`.
509 At most, fills `buffer` entirely */
Yann Collet70611352015-12-16 03:01:03 +0100510static void BMK_loadFiles(void* buffer, size_t bufferSize,
511 size_t* fileSizes,
Yann Colleta5b66e32016-03-26 01:48:27 +0100512 const char** fileNamesTable, unsigned nbFiles)
Yann Colleted699e62015-12-16 02:37:24 +0100513{
inikepc0d5f4e2016-04-13 10:48:04 +0200514 size_t pos = 0, totalSize = 0;
Yann Collet699b14d2016-03-17 19:37:33 +0100515 unsigned n;
Yann Colletfd416f12016-01-30 03:14:15 +0100516 for (n=0; n<nbFiles; n++) {
Yann Collete162ace2016-05-20 11:24:35 +0200517 FILE* f;
inikep69fcd7c2016-04-28 12:23:33 +0200518 U64 fileSize = UTIL_getFileSize(fileNamesTable[n]);
519 if (UTIL_isDirectory(fileNamesTable[n])) {
inikepc0d5f4e2016-04-13 10:48:04 +0200520 DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]);
inikepbab43172016-04-29 15:19:40 +0200521 fileSizes[n] = 0;
inikepc0d5f4e2016-04-13 10:48:04 +0200522 continue;
523 }
inikepea4ee3e2016-04-25 13:09:06 +0200524 f = fopen(fileNamesTable[n], "rb");
Yann Colleted699e62015-12-16 02:37:24 +0100525 if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]);
Yann Colletbf2bc112016-08-02 23:48:13 +0200526 DISPLAYUPDATE(2, "Loading %s... \r", fileNamesTable[n]);
Yann Colleta5b66e32016-03-26 01:48:27 +0100527 if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */
528 { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
529 if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]);
530 pos += readSize; }
Yann Colleta52c98d2015-12-16 03:12:31 +0100531 fileSizes[n] = (size_t)fileSize;
inikepc0d5f4e2016-04-13 10:48:04 +0200532 totalSize += (size_t)fileSize;
Yann Colleted699e62015-12-16 02:37:24 +0100533 fclose(f);
534 }
Yann Collet6f9c0562016-05-01 10:26:30 +0200535
inikepc0d5f4e2016-04-13 10:48:04 +0200536 if (totalSize == 0) EXM_THROW(12, "no data to bench");
Yann Colleted699e62015-12-16 02:37:24 +0100537}
538
Przemyslaw Skibinskid05014c2017-02-07 16:48:01 +0100539static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles, const char* dictFileName, int cLevel,
540 int cLevelLast, ZSTD_compressionParameters *compressionParams, int setRealTimePrio)
Yann Colleted699e62015-12-16 02:37:24 +0100541{
542 void* srcBuffer;
543 size_t benchedSize;
Yann Collet31683c02015-12-18 01:26:48 +0100544 void* dictBuffer = NULL;
545 size_t dictBufferSize = 0;
546 size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t));
Yann Collete162ace2016-05-20 11:24:35 +0200547 U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +0100548 char mfName[20] = {0};
Yann Colleted699e62015-12-16 02:37:24 +0100549
Yann Collet31683c02015-12-18 01:26:48 +0100550 if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes");
551
552 /* Load dictionary */
Yann Colletfd416f12016-01-30 03:14:15 +0100553 if (dictFileName != NULL) {
inikep69fcd7c2016-04-28 12:23:33 +0200554 U64 dictFileSize = UTIL_getFileSize(dictFileName);
Yann Collet31683c02015-12-18 01:26:48 +0100555 if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName);
556 dictBufferSize = (size_t)dictFileSize;
557 dictBuffer = malloc(dictBufferSize);
558 if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize);
559 BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1);
560 }
561
Yann Colleted699e62015-12-16 02:37:24 +0100562 /* Memory allocation & restrictions */
563 benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
564 if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
565 if (benchedSize < totalSizeToLoad)
566 DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20));
567 srcBuffer = malloc(benchedSize);
568 if (!srcBuffer) EXM_THROW(12, "not enough memory");
569
570 /* Load input buffer */
Yann Collet70611352015-12-16 03:01:03 +0100571 BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +0100572
573 /* Bench */
574 snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
Yann Collete162ace2016-05-20 11:24:35 +0200575 { const char* displayName = (nbFiles > 1) ? mfName : fileNamesTable[0];
576 BMK_benchCLevel(srcBuffer, benchedSize,
577 displayName, cLevel, cLevelLast,
578 fileSizes, nbFiles,
Przemyslaw Skibinskid05014c2017-02-07 16:48:01 +0100579 dictBuffer, dictBufferSize, compressionParams, setRealTimePrio);
Yann Collete162ace2016-05-20 11:24:35 +0200580 }
Yann Collet4856a002015-01-24 01:58:16 +0100581
Yann Colleteeb8ba12015-10-22 16:55:40 +0100582 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100583 free(srcBuffer);
Yann Collet31683c02015-12-18 01:26:48 +0100584 free(dictBuffer);
Yann Collet70611352015-12-16 03:01:03 +0100585 free(fileSizes);
Yann Collet4856a002015-01-24 01:58:16 +0100586}
587
588
Przemyslaw Skibinskid05014c2017-02-07 16:48:01 +0100589static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility, ZSTD_compressionParameters* compressionParams, int setRealTimePrio)
Yann Collet4856a002015-01-24 01:58:16 +0100590{
Yann Colleted699e62015-12-16 02:37:24 +0100591 char name[20] = {0};
Yann Collet4856a002015-01-24 01:58:16 +0100592 size_t benchedSize = 10000000;
Yann Collete162ace2016-05-20 11:24:35 +0200593 void* const srcBuffer = malloc(benchedSize);
Yann Collet4856a002015-01-24 01:58:16 +0100594
Yann Collet4856a002015-01-24 01:58:16 +0100595 /* Memory allocation */
Yann Colleted699e62015-12-16 02:37:24 +0100596 if (!srcBuffer) EXM_THROW(21, "not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100597
598 /* Fill input buffer */
Yann Colletd062f132015-12-01 01:31:17 +0100599 RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100600
601 /* Bench */
Yann Colleted699e62015-12-16 02:37:24 +0100602 snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
Przemyslaw Skibinskid05014c2017-02-07 16:48:01 +0100603 BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0, compressionParams, setRealTimePrio);
Yann Collet4856a002015-01-24 01:58:16 +0100604
Yann Colleted699e62015-12-16 02:37:24 +0100605 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100606 free(srcBuffer);
Yann Collet4856a002015-01-24 01:58:16 +0100607}
608
609
Yann Collet1f57c2e2016-12-21 16:20:11 +0100610int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, const char* dictFileName,
Przemyslaw Skibinskid05014c2017-02-07 16:48:01 +0100611 int cLevel, int cLevelLast, ZSTD_compressionParameters* compressionParams, int setRealTimePrio)
Yann Collet4856a002015-01-24 01:58:16 +0100612{
Yann Collet699b14d2016-03-17 19:37:33 +0100613 double const compressibility = (double)g_compressibilityDefault / 100;
Yann Collet4856a002015-01-24 01:58:16 +0100614
Yann Colletc1c040e2017-03-01 16:49:20 -0800615 if (cLevel < 1) cLevel = 1; /* minimum compression level */
Przemyslaw Skibinskifd0ac932016-11-23 21:45:29 +0100616 if (cLevel > ZSTD_maxCLevel()) cLevel = ZSTD_maxCLevel();
617 if (cLevelLast > ZSTD_maxCLevel()) cLevelLast = ZSTD_maxCLevel();
Przemyslaw Skibinski5ddcd9d2016-11-21 16:37:56 +0100618 if (cLevelLast < cLevel) cLevelLast = cLevel;
Yann Colletd9465012016-12-06 16:49:23 -0800619 if (cLevelLast > cLevel) DISPLAYLEVEL(2, "Benchmarking levels from %d to %d\n", cLevel, cLevelLast);
Przemyslaw Skibinski5ddcd9d2016-11-21 16:37:56 +0100620
Yann Collet4856a002015-01-24 01:58:16 +0100621 if (nbFiles == 0)
Przemyslaw Skibinskid05014c2017-02-07 16:48:01 +0100622 BMK_syntheticTest(cLevel, cLevelLast, compressibility, compressionParams, setRealTimePrio);
Yann Collet4856a002015-01-24 01:58:16 +0100623 else
Przemyslaw Skibinskid05014c2017-02-07 16:48:01 +0100624 BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast, compressionParams, setRealTimePrio);
Yann Collet4856a002015-01-24 01:58:16 +0100625 return 0;
626}