blob: caa803572e552a89cbd062aff6f1d3ab8d09bce5 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Yann Collet4ded9e52016-08-30 10:04:33 -07002 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
Yann Collet32fb4072017-08-18 16:52:05 -07005 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet4ded9e52016-08-30 10:04:33 -07009 */
Yann Collet4856a002015-01-24 01:58:16 +010010
Yann Collet4856a002015-01-24 01:58:16 +010011
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010012
13/* **************************************
Yann Collet500014a2017-01-19 16:59:56 -080014* Tuning parameters
15****************************************/
16#ifndef BMK_TIMETEST_DEFAULT_S /* default minimum time per test */
17#define BMK_TIMETEST_DEFAULT_S 3
18#endif
19
20
21/* **************************************
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010022* Compiler Warnings
23****************************************/
24#ifdef _MSC_VER
Yann Colletd3364aa2018-02-20 14:48:09 -080025# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010026#endif
27
28
Yann Colletf3eca252015-10-22 15:31:46 +010029/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010030* Includes
Yann Colletf3eca252015-10-22 15:31:46 +010031***************************************/
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +010032#include "platform.h" /* Large Files support */
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010033#include "util.h" /* UTIL_getFileSize, UTIL_sleep */
Yann Collet4856a002015-01-24 01:58:16 +010034#include <stdlib.h> /* malloc, free */
35#include <string.h> /* memset */
Yann Colletc6a64172016-12-31 03:31:26 +010036#include <stdio.h> /* fprintf, fopen */
Yann Colletd3364aa2018-02-20 14:48:09 -080037#include <assert.h> /* assert */
inikep4c12f232016-03-29 14:52:13 +020038
Yann Colletf3eca252015-10-22 15:31:46 +010039#include "mem.h"
Yann Colletd3b7f8d2016-06-04 19:47:02 +020040#define ZSTD_STATIC_LINKING_ONLY
41#include "zstd.h"
inikep69fcd7c2016-04-28 12:23:33 +020042#include "datagen.h" /* RDG_genBuffer */
Yann Collet4856a002015-01-24 01:58:16 +010043#include "xxhash.h"
George Lu0e808d62018-06-04 16:32:37 -070044#include "bench.h"
George Lud6121ad2018-06-22 17:25:16 -070045#include "zstd_errors.h"
Yann Collet4856a002015-01-24 01:58:16 +010046
47
Yann Colletf3eca252015-10-22 15:31:46 +010048/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010049* Constants
Yann Colletf3eca252015-10-22 15:31:46 +010050***************************************/
inikepf2f59d72016-06-22 15:42:26 +020051#ifndef ZSTD_GIT_COMMIT
inikepd7d251c2016-06-22 16:13:25 +020052# define ZSTD_GIT_COMMIT_STRING ""
53#else
54# define ZSTD_GIT_COMMIT_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_GIT_COMMIT)
inikepf2f59d72016-06-22 15:42:26 +020055#endif
56
Yann Colletd3364aa2018-02-20 14:48:09 -080057#define TIMELOOP_MICROSEC (1*1000000ULL) /* 1 second */
58#define TIMELOOP_NANOSEC (1*1000000000ULL) /* 1 second */
59#define ACTIVEPERIOD_MICROSEC (70*TIMELOOP_MICROSEC) /* 70 seconds */
inikep83c76b42016-04-28 13:16:01 +020060#define COOLPERIOD_SEC 10
Yann Collet4856a002015-01-24 01:58:16 +010061
62#define KB *(1 <<10)
63#define MB *(1 <<20)
64#define GB *(1U<<30)
65
Yann Collet55affc02018-08-28 11:21:09 -070066#define BMK_RUNTEST_DEFAULT_MS 1000
67
Yann Collet2e45bad2018-08-23 14:21:18 -070068static const size_t maxMemory = (sizeof(size_t)==4) ?
69 /* 32-bit */ (2 GB - 64 MB) :
70 /* 64-bit */ (size_t)(1ULL << ((sizeof(size_t)*8)-31));
71
Yann Collet4856a002015-01-24 01:58:16 +010072
Yann Colletf3eca252015-10-22 15:31:46 +010073/* *************************************
Yann Colleted699e62015-12-16 02:37:24 +010074* console display
Yann Colletf3eca252015-10-22 15:31:46 +010075***************************************/
Yann Colleted699e62015-12-16 02:37:24 +010076#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
George Lu01d940b2018-06-11 10:59:05 -040077#define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); }
78/* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
Yann Colleted699e62015-12-16 02:37:24 +010079
Nick Terrell9a2f6f42017-11-29 19:11:12 -080080static const U64 g_refreshRate = SEC_TO_MICRO / 6;
81static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
82
George Lu01d940b2018-06-11 10:59:05 -040083#define DISPLAYUPDATE(l, ...) { if (displayLevel>=l) { \
84 if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (displayLevel>=4)) \
Nick Terrell9a2f6f42017-11-29 19:11:12 -080085 { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \
George Lu01d940b2018-06-11 10:59:05 -040086 if (displayLevel>=4) fflush(stderr); } } }
Yann Colletbf2bc112016-08-02 23:48:13 +020087
Yann Colleted699e62015-12-16 02:37:24 +010088
89/* *************************************
90* Exceptions
91***************************************/
92#ifndef DEBUG
George Lu3adc2172018-07-12 17:30:39 -070093# define DEBUG 0
Yann Colleted699e62015-12-16 02:37:24 +010094#endif
Yann Colletfe234bf2017-06-19 15:23:19 -070095#define DEBUGOUTPUT(...) { if (DEBUG) DISPLAY(__VA_ARGS__); }
George Lu20f4f322018-06-12 15:54:43 -040096
97#define EXM_THROW_INT(errorNum, ...) { \
Yann Colletfe234bf2017-06-19 15:23:19 -070098 DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
George Lu20f4f322018-06-12 15:54:43 -040099 DISPLAYLEVEL(1, "Error %i : ", errorNum); \
George Lu01d940b2018-06-11 10:59:05 -0400100 DISPLAYLEVEL(1, __VA_ARGS__); \
101 DISPLAYLEVEL(1, " \n"); \
George Lu20f4f322018-06-12 15:54:43 -0400102 return errorNum; \
Yann Colleted699e62015-12-16 02:37:24 +0100103}
Yann Collet4856a002015-01-24 01:58:16 +0100104
Yann Collet2e45bad2018-08-23 14:21:18 -0700105#define RETURN_ERROR(errorNum, retType, ...) { \
George Lu20f4f322018-06-12 15:54:43 -0400106 retType r; \
107 memset(&r, 0, sizeof(retType)); \
108 DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
109 DISPLAYLEVEL(1, "Error %i : ", errorNum); \
110 DISPLAYLEVEL(1, __VA_ARGS__); \
111 DISPLAYLEVEL(1, " \n"); \
Yann Collet2e45bad2018-08-23 14:21:18 -0700112 r.tag = errorNum; \
George Lu20f4f322018-06-12 15:54:43 -0400113 return r; \
114}
Yann Collet4856a002015-01-24 01:58:16 +0100115
George Lu0d1ee222018-06-15 16:21:08 -0400116/* error without displaying */
Yann Collet2e45bad2018-08-23 14:21:18 -0700117#define RETURN_QUIET_ERROR(errorNum, retType, ...) { \
George Lu0d1ee222018-06-15 16:21:08 -0400118 retType r; \
119 memset(&r, 0, sizeof(retType)); \
120 DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
George Lud6121ad2018-06-22 17:25:16 -0700121 DEBUGOUTPUT("Error %i : ", errorNum); \
122 DEBUGOUTPUT(__VA_ARGS__); \
123 DEBUGOUTPUT(" \n"); \
Yann Collet2e45bad2018-08-23 14:21:18 -0700124 r.tag = errorNum; \
George Lu0d1ee222018-06-15 16:21:08 -0400125 return r; \
126}
127
Yann Colletf3eca252015-10-22 15:31:46 +0100128/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100129* Benchmark Parameters
Yann Colletf3eca252015-10-22 15:31:46 +0100130***************************************/
Stella Laua1f04d52017-09-01 14:52:51 -0700131
Yann Collet77e805e2018-08-21 18:19:27 -0700132BMK_advancedParams_t BMK_initAdvancedParams(void) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700133 BMK_advancedParams_t const res = {
George Lu85223462018-06-14 14:46:17 -0400134 BMK_both, /* mode */
George Lu20f4f322018-06-12 15:54:43 -0400135 BMK_TIMETEST_DEFAULT_S, /* nbSeconds */
136 0, /* blockSize */
137 0, /* nbWorkers */
138 0, /* realTime */
George Lu20f4f322018-06-12 15:54:43 -0400139 0, /* additionalParam */
Yann Collet77e805e2018-08-21 18:19:27 -0700140 0, /* ldmFlag */
George Lu20f4f322018-06-12 15:54:43 -0400141 0, /* ldmMinMatch */
142 0, /* ldmHashLog */
George Lud6121ad2018-06-22 17:25:16 -0700143 0, /* ldmBuckSizeLog */
144 0 /* ldmHashEveryLog */
George Lu20f4f322018-06-12 15:54:43 -0400145 };
146 return res;
Stella Lau67d4a612017-09-02 21:10:36 -0700147}
148
Yann Collet4856a002015-01-24 01:58:16 +0100149
Yann Colletf3eca252015-10-22 15:31:46 +0100150/* ********************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100151* Bench functions
Yann Colletf3eca252015-10-22 15:31:46 +0100152**********************************************************/
Yann Colletd9465012016-12-06 16:49:23 -0800153typedef struct {
154 const void* srcPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100155 size_t srcSize;
Yann Colletd9465012016-12-06 16:49:23 -0800156 void* cPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100157 size_t cRoom;
158 size_t cSize;
Yann Colletd9465012016-12-06 16:49:23 -0800159 void* resPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100160 size_t resSize;
161} blockParam_t;
162
Sean Purcell42bac7f2017-04-13 15:35:05 -0700163#undef MIN
164#undef MAX
165#define MIN(a,b) ((a) < (b) ? (a) : (b))
166#define MAX(a,b) ((a) > (b) ? (a) : (b))
Yann Collet1c00dc32015-10-21 08:22:25 +0100167
Yann Collet77e805e2018-08-21 18:19:27 -0700168static void BMK_initCCtx(ZSTD_CCtx* ctx,
169 const void* dictBuffer, size_t dictBufferSize, int cLevel,
George Lu20f4f322018-06-12 15:54:43 -0400170 const ZSTD_compressionParameters* comprParams, const BMK_advancedParams_t* adv) {
George Lue148db32018-07-20 14:35:09 -0700171 ZSTD_CCtx_reset(ctx);
172 ZSTD_CCtx_resetParameters(ctx);
George Lu20f4f322018-06-12 15:54:43 -0400173 if (adv->nbWorkers==1) {
174 ZSTD_CCtx_setParameter(ctx, ZSTD_p_nbWorkers, 0);
175 } else {
176 ZSTD_CCtx_setParameter(ctx, ZSTD_p_nbWorkers, adv->nbWorkers);
177 }
178 ZSTD_CCtx_setParameter(ctx, ZSTD_p_compressionLevel, cLevel);
179 ZSTD_CCtx_setParameter(ctx, ZSTD_p_enableLongDistanceMatching, adv->ldmFlag);
180 ZSTD_CCtx_setParameter(ctx, ZSTD_p_ldmMinMatch, adv->ldmMinMatch);
181 ZSTD_CCtx_setParameter(ctx, ZSTD_p_ldmHashLog, adv->ldmHashLog);
George Lud6121ad2018-06-22 17:25:16 -0700182 ZSTD_CCtx_setParameter(ctx, ZSTD_p_ldmBucketSizeLog, adv->ldmBucketSizeLog);
183 ZSTD_CCtx_setParameter(ctx, ZSTD_p_ldmHashEveryLog, adv->ldmHashEveryLog);
George Lu20f4f322018-06-12 15:54:43 -0400184 ZSTD_CCtx_setParameter(ctx, ZSTD_p_windowLog, comprParams->windowLog);
185 ZSTD_CCtx_setParameter(ctx, ZSTD_p_hashLog, comprParams->hashLog);
186 ZSTD_CCtx_setParameter(ctx, ZSTD_p_chainLog, comprParams->chainLog);
187 ZSTD_CCtx_setParameter(ctx, ZSTD_p_searchLog, comprParams->searchLog);
188 ZSTD_CCtx_setParameter(ctx, ZSTD_p_minMatch, comprParams->searchLength);
189 ZSTD_CCtx_setParameter(ctx, ZSTD_p_targetLength, comprParams->targetLength);
George Lu7b5b3d72018-07-16 16:16:31 -0700190 ZSTD_CCtx_setParameter(ctx, ZSTD_p_compressionStrategy, comprParams->strategy);
George Lu20f4f322018-06-12 15:54:43 -0400191 ZSTD_CCtx_loadDictionary(ctx, dictBuffer, dictBufferSize);
192}
193
Yann Collet77e805e2018-08-21 18:19:27 -0700194static void BMK_initDCtx(ZSTD_DCtx* dctx,
George Lu20f4f322018-06-12 15:54:43 -0400195 const void* dictBuffer, size_t dictBufferSize) {
George Lue148db32018-07-20 14:35:09 -0700196 ZSTD_DCtx_reset(dctx);
George Lu20f4f322018-06-12 15:54:43 -0400197 ZSTD_DCtx_loadDictionary(dctx, dictBuffer, dictBufferSize);
198}
199
Yann Collet2e45bad2018-08-23 14:21:18 -0700200
George Lu20f4f322018-06-12 15:54:43 -0400201typedef struct {
Yann Collet2e45bad2018-08-23 14:21:18 -0700202 ZSTD_CCtx* cctx;
George Lu20f4f322018-06-12 15:54:43 -0400203 const void* dictBuffer;
204 size_t dictBufferSize;
205 int cLevel;
206 const ZSTD_compressionParameters* comprParams;
207 const BMK_advancedParams_t* adv;
208} BMK_initCCtxArgs;
209
210static size_t local_initCCtx(void* payload) {
211 BMK_initCCtxArgs* ag = (BMK_initCCtxArgs*)payload;
Yann Collet2e45bad2018-08-23 14:21:18 -0700212 BMK_initCCtx(ag->cctx, ag->dictBuffer, ag->dictBufferSize, ag->cLevel, ag->comprParams, ag->adv);
George Lu20f4f322018-06-12 15:54:43 -0400213 return 0;
214}
215
216typedef struct {
217 ZSTD_DCtx* dctx;
218 const void* dictBuffer;
219 size_t dictBufferSize;
220} BMK_initDCtxArgs;
221
222static size_t local_initDCtx(void* payload) {
223 BMK_initDCtxArgs* ag = (BMK_initDCtxArgs*)payload;
224 BMK_initDCtx(ag->dctx, ag->dictBuffer, ag->dictBufferSize);
225 return 0;
226}
227
Yann Collet2e45bad2018-08-23 14:21:18 -0700228
229/* `addArgs` is the context */
George Lu20f4f322018-06-12 15:54:43 -0400230static size_t local_defaultCompress(
Yann Collet2e45bad2018-08-23 14:21:18 -0700231 const void* srcBuffer, size_t srcSize,
232 void* dstBuffer, size_t dstSize,
233 void* addArgs)
234{
George Lu20f4f322018-06-12 15:54:43 -0400235 size_t moreToFlush = 1;
Yann Collet2e45bad2018-08-23 14:21:18 -0700236 ZSTD_CCtx* const cctx = (ZSTD_CCtx*)addArgs;
George Lu20f4f322018-06-12 15:54:43 -0400237 ZSTD_inBuffer in;
238 ZSTD_outBuffer out;
Yann Collet2e45bad2018-08-23 14:21:18 -0700239 in.src = srcBuffer; in.size = srcSize; in.pos = 0;
240 out.dst = dstBuffer; out.size = dstSize; out.pos = 0;
George Lu20f4f322018-06-12 15:54:43 -0400241 while (moreToFlush) {
George Lud6121ad2018-06-22 17:25:16 -0700242 if(out.pos == out.size) {
243 return (size_t)-ZSTD_error_dstSize_tooSmall;
244 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700245 moreToFlush = ZSTD_compress_generic(cctx, &out, &in, ZSTD_e_end);
George Lu20f4f322018-06-12 15:54:43 -0400246 if (ZSTD_isError(moreToFlush)) {
247 return moreToFlush;
248 }
249 }
250 return out.pos;
251}
252
Yann Collet2e45bad2018-08-23 14:21:18 -0700253/* `addArgs` is the context */
George Lu20f4f322018-06-12 15:54:43 -0400254static size_t local_defaultDecompress(
Yann Collet2e45bad2018-08-23 14:21:18 -0700255 const void* srcBuffer, size_t srcSize,
Yann Collet0c66a442018-08-28 15:47:07 -0700256 void* dstBuffer, size_t dstCapacity,
Yann Collet2e45bad2018-08-23 14:21:18 -0700257 void* addArgs)
258{
George Lu20f4f322018-06-12 15:54:43 -0400259 size_t moreToFlush = 1;
Yann Collet2e45bad2018-08-23 14:21:18 -0700260 ZSTD_DCtx* const dctx = (ZSTD_DCtx*)addArgs;
George Lu20f4f322018-06-12 15:54:43 -0400261 ZSTD_inBuffer in;
262 ZSTD_outBuffer out;
Yann Collet2e45bad2018-08-23 14:21:18 -0700263 in.src = srcBuffer; in.size = srcSize; in.pos = 0;
Yann Collet0c66a442018-08-28 15:47:07 -0700264 out.dst = dstBuffer; out.size = dstCapacity; out.pos = 0;
George Lu20f4f322018-06-12 15:54:43 -0400265 while (moreToFlush) {
George Lud6121ad2018-06-22 17:25:16 -0700266 if(out.pos == out.size) {
267 return (size_t)-ZSTD_error_dstSize_tooSmall;
268 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700269 moreToFlush = ZSTD_decompress_generic(dctx, &out, &in);
George Lu20f4f322018-06-12 15:54:43 -0400270 if (ZSTD_isError(moreToFlush)) {
271 return moreToFlush;
272 }
273 }
274 return out.pos;
275
276}
277
Yann Collet2e45bad2018-08-23 14:21:18 -0700278
279/*=== Benchmarking an arbitrary function ===*/
280
281int BMK_isSuccessful_runOutcome(BMK_runOutcome_t outcome)
282{
Yann Collet01dcd0f2018-08-26 21:30:18 -0700283 return outcome.tag == 0;
Yann Collet2e45bad2018-08-23 14:21:18 -0700284}
285
286/* warning : this function will stop program execution if outcome is invalid !
287 * check outcome validity first, using BMK_isValid_runResult() */
288BMK_runTime_t BMK_extract_runTime(BMK_runOutcome_t outcome)
289{
Yann Collet01dcd0f2018-08-26 21:30:18 -0700290 assert(outcome.tag == 0);
Yann Collet2e45bad2018-08-23 14:21:18 -0700291 return outcome.internal_never_use_directly;
292}
293
Yann Collet01dcd0f2018-08-26 21:30:18 -0700294static BMK_runOutcome_t BMK_runOutcome_error(void)
295{
296 BMK_runOutcome_t b;
297 memset(&b, 0, sizeof(b));
298 b.tag = 1;
299 return b;
300}
301
Yann Collet2e45bad2018-08-23 14:21:18 -0700302static BMK_runOutcome_t BMK_setValid_runTime(BMK_runTime_t runTime)
303{
304 BMK_runOutcome_t outcome;
305 outcome.tag = 0;
306 outcome.internal_never_use_directly = runTime;
307 return outcome;
308}
309
310
311/* initFn will be measured once, benchFn will be measured `nbLoops` times */
312/* initFn is optional, provide NULL if none */
313/* benchFn must return size_t field compliant with ZSTD_isError for error valuee */
George Lu20f4f322018-06-12 15:54:43 -0400314/* takes # of blocks and list of size & stuff for each. */
Yann Collet2e45bad2018-08-23 14:21:18 -0700315/* can report result of benchFn for each block into blockResult. */
316/* blockResult is optional, provide NULL if this information is not required */
317/* note : time per loop could be zero if run time < timer resolution */
318BMK_runOutcome_t BMK_benchFunction(
319 BMK_benchFn_t benchFn, void* benchPayload,
320 BMK_initFn_t initFn, void* initPayload,
321 size_t blockCount,
322 const void* const * srcBlockBuffers, const size_t* srcBlockSizes,
323 void* const * dstBlockBuffers, const size_t* dstBlockCapacities,
Yann Colletd39a25c2018-08-23 14:28:51 -0700324 size_t* blockResults,
Yann Collet2e45bad2018-08-23 14:21:18 -0700325 unsigned nbLoops)
326{
George Lue148db32018-07-20 14:35:09 -0700327 size_t dstSize = 0;
George Lu20f4f322018-06-12 15:54:43 -0400328
George Lud6121ad2018-06-22 17:25:16 -0700329 if(!nbLoops) {
Yann Collet2279f3d2018-08-24 17:28:38 -0700330 RETURN_QUIET_ERROR(2, BMK_runOutcome_t, "nbLoops must be nonzero ");
George Lu20f4f322018-06-12 15:54:43 -0400331 }
332
Yann Collet2e45bad2018-08-23 14:21:18 -0700333 /* init */
334 { size_t i;
George Lu5f490342018-06-18 11:59:45 -0700335 for(i = 0; i < blockCount; i++) {
George Lue148db32018-07-20 14:35:09 -0700336 memset(dstBlockBuffers[i], 0xE5, dstBlockCapacities[i]); /* warm up and erase result buffer */
George Lu5f490342018-06-18 11:59:45 -0700337 }
George Lue148db32018-07-20 14:35:09 -0700338#if 0
Yann Collet77e805e2018-08-21 18:19:27 -0700339 /* based on testing these seem to lower accuracy of multiple calls of 1 nbLoops vs 1 call of multiple nbLoops
340 * (Makes former slower)
George Lue148db32018-07-20 14:35:09 -0700341 */
342 UTIL_sleepMilli(5); /* give processor time to other processes */
343 UTIL_waitForNextTick();
344#endif
George Lu5f490342018-06-18 11:59:45 -0700345 }
346
Yann Collet4da5bdf2018-08-23 18:04:50 -0700347 /* benchmark */
Yann Collet2e45bad2018-08-23 14:21:18 -0700348 { UTIL_time_t const clockStart = UTIL_getTime();
349 unsigned loopNb, blockNb;
350 if (initFn != NULL) initFn(initPayload);
351 for (loopNb = 0; loopNb < nbLoops; loopNb++) {
352 for (blockNb = 0; blockNb < blockCount; blockNb++) {
353 size_t const res = benchFn(srcBlockBuffers[blockNb], srcBlockSizes[blockNb],
354 dstBlockBuffers[blockNb], dstBlockCapacities[blockNb],
355 benchPayload);
George Lua8eea992018-06-19 10:58:22 -0700356 if(ZSTD_isError(res)) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700357 RETURN_QUIET_ERROR(2, BMK_runOutcome_t,
358 "Function benchmark failed on block %u of size %u : %s",
359 blockNb, (U32)dstBlockCapacities[blockNb], ZSTD_getErrorName(res));
360 } else if (loopNb == 0) {
George Lua8eea992018-06-19 10:58:22 -0700361 dstSize += res;
Yann Colletd39a25c2018-08-23 14:28:51 -0700362 if (blockResults != NULL) blockResults[blockNb] = res;
Yann Collet2e45bad2018-08-23 14:21:18 -0700363 } }
364 } /* for (loopNb = 0; loopNb < nbLoops; loopNb++) */
George Lua8eea992018-06-19 10:58:22 -0700365
Yann Collet4da5bdf2018-08-23 18:04:50 -0700366 { U64 const totalTime = UTIL_clockSpanNano(clockStart);
367 BMK_runTime_t rt;
368 rt.nanoSecPerRun = totalTime / nbLoops;
369 rt.sumOfReturn = dstSize;
370 return BMK_setValid_runTime(rt);
371 } }
Yann Collet77e805e2018-08-21 18:19:27 -0700372}
George Luab26f242018-06-21 11:16:53 -0700373
George Lud6121ad2018-06-22 17:25:16 -0700374
Yann Collet2e45bad2018-08-23 14:21:18 -0700375/* ==== Benchmarking any function, providing intermediate results ==== */
376
377struct BMK_timedFnState_s {
378 U64 timeSpent_ns;
379 U64 timeBudget_ns;
Yann Collet55affc02018-08-28 11:21:09 -0700380 U64 runBudget_ns;
Yann Collet2e45bad2018-08-23 14:21:18 -0700381 BMK_runTime_t fastestRun;
382 unsigned nbLoops;
383 UTIL_time_t coolTime;
384}; /* typedef'd to BMK_timedFnState_t within bench.h */
George Luab26f242018-06-21 11:16:53 -0700385
Yann Collet55affc02018-08-28 11:21:09 -0700386BMK_timedFnState_t* BMK_createTimedFnState(unsigned total_ms, unsigned run_ms)
387{
Yann Collet2e45bad2018-08-23 14:21:18 -0700388 BMK_timedFnState_t* const r = (BMK_timedFnState_t*)malloc(sizeof(*r));
389 if (r == NULL) return NULL; /* malloc() error */
Yann Collet55affc02018-08-28 11:21:09 -0700390 BMK_resetTimedFnState(r, total_ms, run_ms);
George Lud6121ad2018-06-22 17:25:16 -0700391 return r;
392}
George Luab26f242018-06-21 11:16:53 -0700393
Yann Collet77e805e2018-08-21 18:19:27 -0700394void BMK_freeTimedFnState(BMK_timedFnState_t* state) {
George Lud6121ad2018-06-22 17:25:16 -0700395 free(state);
396}
397
Yann Collet55affc02018-08-28 11:21:09 -0700398void BMK_resetTimedFnState(BMK_timedFnState_t* timedFnState, unsigned total_ms, unsigned run_ms)
399{
400 if (!total_ms) total_ms = 1 ;
401 if (!run_ms) run_ms = 1;
402 if (run_ms > total_ms) run_ms = total_ms;
403 timedFnState->timeSpent_ns = 0;
404 timedFnState->timeBudget_ns = (U64)total_ms * TIMELOOP_NANOSEC / 1000;
405 timedFnState->runBudget_ns = (U64)run_ms * TIMELOOP_NANOSEC / 1000;
406 timedFnState->fastestRun.nanoSecPerRun = (U64)(-1LL);
407 timedFnState->fastestRun.sumOfReturn = (size_t)(-1LL);
408 timedFnState->nbLoops = 1;
409 timedFnState->coolTime = UTIL_getTime();
410}
George Luab26f242018-06-21 11:16:53 -0700411
Yann Collet2e45bad2018-08-23 14:21:18 -0700412/* Tells if nb of seconds set in timedFnState for all runs is spent.
413 * note : this function will return 1 if BMK_benchFunctionTimed() has actually errored. */
Yann Collet01dcd0f2018-08-26 21:30:18 -0700414int BMK_isCompleted_TimedFn(const BMK_timedFnState_t* timedFnState)
Yann Collet2e45bad2018-08-23 14:21:18 -0700415{
Yann Collet01dcd0f2018-08-26 21:30:18 -0700416 return (timedFnState->timeSpent_ns >= timedFnState->timeBudget_ns);
Yann Collet2e45bad2018-08-23 14:21:18 -0700417}
418
419
420#define MINUSABLETIME (TIMELOOP_NANOSEC / 2) /* 0.5 seconds */
421
Yann Collet01dcd0f2018-08-26 21:30:18 -0700422BMK_runOutcome_t BMK_benchTimedFn(
Yann Collet2e45bad2018-08-23 14:21:18 -0700423 BMK_timedFnState_t* cont,
424 BMK_benchFn_t benchFn, void* benchPayload,
425 BMK_initFn_t initFn, void* initPayload,
426 size_t blockCount,
427 const void* const* srcBlockBuffers, const size_t* srcBlockSizes,
428 void * const * dstBlockBuffers, const size_t * dstBlockCapacities,
429 size_t* blockResults)
430{
Yann Collet55affc02018-08-28 11:21:09 -0700431 U64 const runBudget_ns = cont->runBudget_ns;
432 U64 const runTimeMin_ns = runBudget_ns / 2;
Yann Collet2e45bad2018-08-23 14:21:18 -0700433 int completed = 0;
Yann Collet2e45bad2018-08-23 14:21:18 -0700434 BMK_runTime_t bestRunTime = cont->fastestRun;
435
Yann Collet2e45bad2018-08-23 14:21:18 -0700436 while (!completed) {
437 BMK_runOutcome_t runResult;
438
George Luab26f242018-06-21 11:16:53 -0700439 /* Overheat protection */
George Lud6121ad2018-06-22 17:25:16 -0700440 if (UTIL_clockSpanMicro(cont->coolTime) > ACTIVEPERIOD_MICROSEC) {
George Luab26f242018-06-21 11:16:53 -0700441 DEBUGOUTPUT("\rcooling down ... \r");
442 UTIL_sleep(COOLPERIOD_SEC);
George Lud6121ad2018-06-22 17:25:16 -0700443 cont->coolTime = UTIL_getTime();
George Luab26f242018-06-21 11:16:53 -0700444 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700445
George Lue148db32018-07-20 14:35:09 -0700446 /* reinitialize capacity */
Yann Collet2e45bad2018-08-23 14:21:18 -0700447 runResult = BMK_benchFunction(benchFn, benchPayload,
448 initFn, initPayload,
449 blockCount,
450 srcBlockBuffers, srcBlockSizes,
451 dstBlockBuffers, dstBlockCapacities,
452 blockResults,
453 cont->nbLoops);
454
455 if(!BMK_isSuccessful_runOutcome(runResult)) { /* error : move out */
Yann Collet01dcd0f2018-08-26 21:30:18 -0700456 return BMK_runOutcome_error();
George Luab26f242018-06-21 11:16:53 -0700457 }
458
Yann Collet2e45bad2018-08-23 14:21:18 -0700459 { BMK_runTime_t const newRunTime = BMK_extract_runTime(runResult);
460 U64 const loopDuration_ns = newRunTime.nanoSecPerRun * cont->nbLoops;
461
462 cont->timeSpent_ns += loopDuration_ns;
463
464 /* estimate nbLoops for next run to last approximately 1 second */
Yann Collet55affc02018-08-28 11:21:09 -0700465 if (loopDuration_ns > (runBudget_ns / 50)) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700466 U64 const fastestRun_ns = MIN(bestRunTime.nanoSecPerRun, newRunTime.nanoSecPerRun);
Yann Collet55affc02018-08-28 11:21:09 -0700467 cont->nbLoops = (U32)(runBudget_ns / fastestRun_ns) + 1;
George Luab26f242018-06-21 11:16:53 -0700468 } else {
Yann Collet2e45bad2018-08-23 14:21:18 -0700469 /* previous run was too short : blindly increase workload by x multiplier */
470 const unsigned multiplier = 10;
George Lud6121ad2018-06-22 17:25:16 -0700471 assert(cont->nbLoops < ((unsigned)-1) / multiplier); /* avoid overflow */
472 cont->nbLoops *= multiplier;
George Luab26f242018-06-21 11:16:53 -0700473 }
George Luab26f242018-06-21 11:16:53 -0700474
Yann Collet55affc02018-08-28 11:21:09 -0700475 if(loopDuration_ns < runTimeMin_ns) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700476 /* don't report results for which benchmark run time was too small : increased risks of rounding errors */
477 assert(completed == 0);
478 continue;
479 } else {
480 if(newRunTime.nanoSecPerRun < bestRunTime.nanoSecPerRun) {
481 bestRunTime = newRunTime;
482 }
483 completed = 1;
484 }
George Luab26f242018-06-21 11:16:53 -0700485 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700486 } /* while (!completed) */
487
Yann Collet01dcd0f2018-08-26 21:30:18 -0700488 return BMK_setValid_runTime(bestRunTime);
George Lu20f4f322018-06-12 15:54:43 -0400489}
490
George Lu01d940b2018-06-11 10:59:05 -0400491
Yann Collet2e45bad2018-08-23 14:21:18 -0700492/* ================================================================= */
493/* Benchmark Zstandard, mem-to-mem scenarios */
494/* ================================================================= */
495
496int BMK_isSuccessful_benchOutcome(BMK_benchOutcome_t outcome)
497{
498 return outcome.tag == 0;
499}
500
501BMK_benchResult_t BMK_extract_benchResult(BMK_benchOutcome_t outcome)
502{
503 assert(outcome.tag == 0);
504 return outcome.internal_never_use_directly;
505}
506
Yann Collet6ce7b082018-08-24 15:59:57 -0700507static BMK_benchOutcome_t BMK_benchOutcome_error(void)
Yann Collet2e45bad2018-08-23 14:21:18 -0700508{
509 BMK_benchOutcome_t b;
510 memset(&b, 0, sizeof(b));
511 b.tag = 1;
512 return b;
513}
514
515static BMK_benchOutcome_t BMK_benchOutcome_setValidResult(BMK_benchResult_t result)
516{
517 BMK_benchOutcome_t b;
518 b.tag = 0;
519 b.internal_never_use_directly = result;
520 return b;
521}
522
523
524/* benchMem with no allocation */
Yann Collet8bed4012018-11-08 12:36:39 -0800525static BMK_benchOutcome_t
526BMK_benchMemAdvancedNoAlloc(
527 const void** srcPtrs, size_t* srcSizes,
528 void** cPtrs, size_t* cCapacities, size_t* cSizes,
529 void** resPtrs, size_t* resSizes,
530 void** resultBufferPtr, void* compressedBuffer,
531 size_t maxCompressedSize,
532 BMK_timedFnState_t* timeStateCompress,
533 BMK_timedFnState_t* timeStateDecompress,
Yann Collet2e45bad2018-08-23 14:21:18 -0700534
Yann Collet8bed4012018-11-08 12:36:39 -0800535 const void* srcBuffer, size_t srcSize,
536 const size_t* fileSizes, unsigned nbFiles,
537 const int cLevel,
538 const ZSTD_compressionParameters* comprParams,
539 const void* dictBuffer, size_t dictBufferSize,
540 ZSTD_CCtx* cctx, ZSTD_DCtx* dctx,
541 int displayLevel, const char* displayName,
542 const BMK_advancedParams_t* adv)
Yann Collet1c00dc32015-10-21 08:22:25 +0100543{
Yann Collet4da5bdf2018-08-23 18:04:50 -0700544 size_t const blockSize = ((adv->blockSize>=32 && (adv->mode != BMK_decodeOnly)) ? adv->blockSize : srcSize) + (!srcSize); /* avoid div by 0 */
Yann Collet2e45bad2018-08-23 14:21:18 -0700545 BMK_benchResult_t benchResult;
Yann Collete63c6312016-12-06 17:46:49 -0800546 size_t const loadedCompressedSize = srcSize;
547 size_t cSize = 0;
548 double ratio = 0.;
Yann Colletde406ee2016-03-20 15:46:10 +0100549 U32 nbBlocks;
550
Yann Collet2e45bad2018-08-23 14:21:18 -0700551 assert(cctx != NULL); assert(dctx != NULL);
George Lu01d940b2018-06-11 10:59:05 -0400552
Yann Colletc776c462015-10-29 19:10:54 +0100553 /* init */
Yann Collet4da5bdf2018-08-23 18:04:50 -0700554 memset(&benchResult, 0, sizeof(benchResult));
Yann Collet2e45bad2018-08-23 14:21:18 -0700555 if (strlen(displayName)>17) displayName += strlen(displayName) - 17; /* display last 17 characters */
George Lu85223462018-06-14 14:46:17 -0400556 if (adv->mode == BMK_decodeOnly) { /* benchmark only decompression : source must be already compressed */
Yann Colletc2007382017-04-04 15:35:06 -0700557 const char* srcPtr = (const char*)srcBuffer;
558 U64 totalDSize64 = 0;
Yann Collete63c6312016-12-06 17:46:49 -0800559 U32 fileNb;
560 for (fileNb=0; fileNb<nbFiles; fileNb++) {
Sean Purcell4e709712017-02-07 13:50:09 -0800561 U64 const fSize64 = ZSTD_findDecompressedSize(srcPtr, fileSizes[fileNb]);
Yann Collet2e45bad2018-08-23 14:21:18 -0700562 if (fSize64==0) RETURN_ERROR(32, BMK_benchOutcome_t, "Impossible to determine original size ");
Yann Colletc2007382017-04-04 15:35:06 -0700563 totalDSize64 += fSize64;
Yann Collete63c6312016-12-06 17:46:49 -0800564 srcPtr += fileSizes[fileNb];
565 }
Yann Colletc2007382017-04-04 15:35:06 -0700566 { size_t const decodedSize = (size_t)totalDSize64;
Yann Collet2e45bad2018-08-23 14:21:18 -0700567 assert((U64)decodedSize == totalDSize64); /* check overflow */
George Lu5f490342018-06-18 11:59:45 -0700568 free(*resultBufferPtr);
George Lu5f490342018-06-18 11:59:45 -0700569 *resultBufferPtr = malloc(decodedSize);
570 if (!(*resultBufferPtr)) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700571 RETURN_ERROR(33, BMK_benchOutcome_t, "not enough memory");
George Lua8eea992018-06-19 10:58:22 -0700572 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700573 if (totalDSize64 > decodedSize) { /* size_t overflow */
Yann Collet77e805e2018-08-21 18:19:27 -0700574 free(*resultBufferPtr);
Yann Collet2e45bad2018-08-23 14:21:18 -0700575 RETURN_ERROR(32, BMK_benchOutcome_t, "original size is too large");
George Lua8eea992018-06-19 10:58:22 -0700576 }
Yann Collete63c6312016-12-06 17:46:49 -0800577 cSize = srcSize;
578 srcSize = decodedSize;
579 ratio = (double)srcSize / (double)cSize;
Yann Collet77e805e2018-08-21 18:19:27 -0700580 }
George Lu20f4f322018-06-12 15:54:43 -0400581 }
Yann Collete63c6312016-12-06 17:46:49 -0800582
George Lu20f4f322018-06-12 15:54:43 -0400583 /* Init data blocks */
Yann Collete63c6312016-12-06 17:46:49 -0800584 { const char* srcPtr = (const char*)srcBuffer;
Yann Collet1c00dc32015-10-21 08:22:25 +0100585 char* cPtr = (char*)compressedBuffer;
George Lu5f490342018-06-18 11:59:45 -0700586 char* resPtr = (char*)(*resultBufferPtr);
Yann Collet699b14d2016-03-17 19:37:33 +0100587 U32 fileNb;
Yann Colletde406ee2016-03-20 15:46:10 +0100588 for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
Yann Collet70611352015-12-16 03:01:03 +0100589 size_t remaining = fileSizes[fileNb];
George Lu85223462018-06-14 14:46:17 -0400590 U32 const nbBlocksforThisFile = (adv->mode == BMK_decodeOnly) ? 1 : (U32)((remaining + (blockSize-1)) / blockSize);
Yann Collet699b14d2016-03-17 19:37:33 +0100591 U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
Yann Colletfd416f12016-01-30 03:14:15 +0100592 for ( ; nbBlocks<blockEnd; nbBlocks++) {
Yann Colletde406ee2016-03-20 15:46:10 +0100593 size_t const thisBlockSize = MIN(remaining, blockSize);
Yann Collet2e45bad2018-08-23 14:21:18 -0700594 srcPtrs[nbBlocks] = srcPtr;
George Lu20f4f322018-06-12 15:54:43 -0400595 srcSizes[nbBlocks] = thisBlockSize;
Yann Collet2e45bad2018-08-23 14:21:18 -0700596 cPtrs[nbBlocks] = cPtr;
George Lue148db32018-07-20 14:35:09 -0700597 cCapacities[nbBlocks] = (adv->mode == BMK_decodeOnly) ? thisBlockSize : ZSTD_compressBound(thisBlockSize);
Yann Collet2e45bad2018-08-23 14:21:18 -0700598 resPtrs[nbBlocks] = resPtr;
George Lu85223462018-06-14 14:46:17 -0400599 resSizes[nbBlocks] = (adv->mode == BMK_decodeOnly) ? (size_t) ZSTD_findDecompressedSize(srcPtr, thisBlockSize) : thisBlockSize;
Yann Colleted699e62015-12-16 02:37:24 +0100600 srcPtr += thisBlockSize;
George Lue148db32018-07-20 14:35:09 -0700601 cPtr += cCapacities[nbBlocks];
Yann Colleted699e62015-12-16 02:37:24 +0100602 resPtr += thisBlockSize;
603 remaining -= thisBlockSize;
Yann Collet8bed4012018-11-08 12:36:39 -0800604 if (BMK_decodeOnly) { assert(nbBlocks==0); cSizes[nbBlocks] = thisBlockSize; }
Yann Collet77e805e2018-08-21 18:19:27 -0700605 }
606 }
George Lu20f4f322018-06-12 15:54:43 -0400607 }
Yann Collet1c00dc32015-10-21 08:22:25 +0100608
Yann Collet2e45bad2018-08-23 14:21:18 -0700609 /* warmimg up `compressedBuffer` */
George Lu85223462018-06-14 14:46:17 -0400610 if (adv->mode == BMK_decodeOnly) {
Yann Collet03e7e142018-03-05 13:50:07 -0800611 memcpy(compressedBuffer, srcBuffer, loadedCompressedSize);
612 } else {
613 RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
614 }
Yann Collet4856a002015-01-24 01:58:16 +0100615
616 /* Bench */
Yann Collet2e45bad2018-08-23 14:21:18 -0700617 { U64 const crcOrig = (adv->mode == BMK_decodeOnly) ? 0 : XXH64(srcBuffer, srcSize, 0);
Yann Colleta9febe82016-08-01 13:37:17 +0200618# define NB_MARKS 4
Yann Collet2e45bad2018-08-23 14:21:18 -0700619 const char* marks[NB_MARKS] = { " |", " /", " =", " \\" };
Yann Colleta9febe82016-08-01 13:37:17 +0200620 U32 markNb = 0;
Yann Collet2e45bad2018-08-23 14:21:18 -0700621 int compressionCompleted = (adv->mode == BMK_decodeOnly);
622 int decompressionCompleted = (adv->mode == BMK_compressOnly);
623 BMK_initCCtxArgs cctxprep;
624 BMK_initDCtxArgs dctxprep;
625 cctxprep.cctx = cctx;
626 cctxprep.dictBuffer = dictBuffer;
627 cctxprep.dictBufferSize = dictBufferSize;
628 cctxprep.cLevel = cLevel;
629 cctxprep.comprParams = comprParams;
630 cctxprep.adv = adv;
631 dctxprep.dctx = dctx;
632 dctxprep.dictBuffer = dictBuffer;
633 dctxprep.dictBufferSize = dictBufferSize;
Yann Collet4856a002015-01-24 01:58:16 +0100634
Yann Collet2e45bad2018-08-23 14:21:18 -0700635 DISPLAYLEVEL(2, "\r%70s\r", ""); /* blank line */
George Lu50d612f2018-06-25 15:01:03 -0700636 DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (U32)srcSize);
George Lu50d612f2018-06-25 15:01:03 -0700637
Yann Collet2e45bad2018-08-23 14:21:18 -0700638 while (!(compressionCompleted && decompressionCompleted)) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700639 if (!compressionCompleted) {
Yann Collet2279f3d2018-08-24 17:28:38 -0700640 BMK_runOutcome_t const cOutcome =
Yann Collet01dcd0f2018-08-26 21:30:18 -0700641 BMK_benchTimedFn( timeStateCompress,
642 &local_defaultCompress, cctx,
643 &local_initCCtx, &cctxprep,
644 nbBlocks,
645 srcPtrs, srcSizes,
646 cPtrs, cCapacities,
647 cSizes);
Yann Collet2e45bad2018-08-23 14:21:18 -0700648
Yann Collet2279f3d2018-08-24 17:28:38 -0700649 if (!BMK_isSuccessful_runOutcome(cOutcome)) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700650 return BMK_benchOutcome_error();
George Lu50d612f2018-06-25 15:01:03 -0700651 }
652
Yann Collet2279f3d2018-08-24 17:28:38 -0700653 { BMK_runTime_t const cResult = BMK_extract_runTime(cOutcome);
Yann Collet4da5bdf2018-08-23 18:04:50 -0700654 cSize = cResult.sumOfReturn;
655 ratio = (double)srcSize / cSize;
656 { BMK_benchResult_t newResult;
657 newResult.cSpeed = ((U64)srcSize * TIMELOOP_NANOSEC / cResult.nanoSecPerRun);
658 benchResult.cSize = cSize;
659 if (newResult.cSpeed > benchResult.cSpeed)
660 benchResult.cSpeed = newResult.cSpeed;
661 } }
George Lu5f490342018-06-18 11:59:45 -0700662
Yann Collet2e45bad2018-08-23 14:21:18 -0700663 { int const ratioAccuracy = (ratio < 10.) ? 3 : 2;
Yann Collet2e45bad2018-08-23 14:21:18 -0700664 DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.*f),%6.*f MB/s\r",
Yann Collet4da5bdf2018-08-23 18:04:50 -0700665 marks[markNb], displayName,
666 (U32)srcSize, (U32)cSize,
Yann Collet2e45bad2018-08-23 14:21:18 -0700667 ratioAccuracy, ratio,
Yann Collet1f9ec132018-08-23 16:03:30 -0700668 benchResult.cSpeed < (10 MB) ? 2 : 1, (double)benchResult.cSpeed / MB_UNIT);
George Lua8eea992018-06-19 10:58:22 -0700669 }
Yann Collet01dcd0f2018-08-26 21:30:18 -0700670 compressionCompleted = BMK_isCompleted_TimedFn(timeStateCompress);
Yann Collet2e45bad2018-08-23 14:21:18 -0700671 }
George Lue89f1fb2018-08-14 14:44:47 -0700672
Yann Collet2e45bad2018-08-23 14:21:18 -0700673 if(!decompressionCompleted) {
Yann Collet2279f3d2018-08-24 17:28:38 -0700674 BMK_runOutcome_t const dOutcome =
Yann Collet01dcd0f2018-08-26 21:30:18 -0700675 BMK_benchTimedFn(timeStateDecompress,
676 &local_defaultDecompress, dctx,
677 &local_initDCtx, &dctxprep,
678 nbBlocks,
679 (const void *const *)cPtrs, cSizes,
680 resPtrs, resSizes,
681 NULL);
Yann Collet2e45bad2018-08-23 14:21:18 -0700682
Yann Collet2279f3d2018-08-24 17:28:38 -0700683 if(!BMK_isSuccessful_runOutcome(dOutcome)) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700684 return BMK_benchOutcome_error();
685 }
686
Yann Collet2279f3d2018-08-24 17:28:38 -0700687 { BMK_runTime_t const dResult = BMK_extract_runTime(dOutcome);
Yann Collet4da5bdf2018-08-23 18:04:50 -0700688 U64 const newDSpeed = (srcSize * TIMELOOP_NANOSEC / dResult.nanoSecPerRun);
689 if (newDSpeed > benchResult.dSpeed)
690 benchResult.dSpeed = newDSpeed;
691 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700692
693 { int const ratioAccuracy = (ratio < 10.) ? 3 : 2;
Yann Collet2e45bad2018-08-23 14:21:18 -0700694 DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.*f),%6.*f MB/s ,%6.1f MB/s \r",
Yann Collet4da5bdf2018-08-23 18:04:50 -0700695 marks[markNb], displayName,
696 (U32)srcSize, (U32)benchResult.cSize,
Yann Collet2e45bad2018-08-23 14:21:18 -0700697 ratioAccuracy, ratio,
Yann Collet1f9ec132018-08-23 16:03:30 -0700698 benchResult.cSpeed < (10 MB) ? 2 : 1, (double)benchResult.cSpeed / MB_UNIT,
Yann Collet4da5bdf2018-08-23 18:04:50 -0700699 (double)benchResult.dSpeed / MB_UNIT);
George Lua8eea992018-06-19 10:58:22 -0700700 }
Yann Collet01dcd0f2018-08-26 21:30:18 -0700701 decompressionCompleted = BMK_isCompleted_TimedFn(timeStateDecompress);
Yann Colletdaebc7f2017-11-18 15:54:32 -0800702 }
Yann Collet8bed4012018-11-08 12:36:39 -0800703 markNb = (markNb+1) % NB_MARKS;
Yann Collet4da5bdf2018-08-23 18:04:50 -0700704 } /* while (!(compressionCompleted && decompressionCompleted)) */
George Lu20f4f322018-06-12 15:54:43 -0400705
706 /* CRC Checking */
Yann Collet2e45bad2018-08-23 14:21:18 -0700707 { const BYTE* resultBuffer = (const BYTE*)(*resultBufferPtr);
George Lu5f490342018-06-18 11:59:45 -0700708 U64 const crcCheck = XXH64(resultBuffer, srcSize, 0);
George Lu85223462018-06-14 14:46:17 -0400709 if ((adv->mode == BMK_both) && (crcOrig!=crcCheck)) {
George Lu20f4f322018-06-12 15:54:43 -0400710 size_t u;
Yann Collet8bed4012018-11-08 12:36:39 -0800711 DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n",
712 displayName, (unsigned)crcOrig, (unsigned)crcCheck);
George Lu20f4f322018-06-12 15:54:43 -0400713 for (u=0; u<srcSize; u++) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700714 if (((const BYTE*)srcBuffer)[u] != resultBuffer[u]) {
George Lu20f4f322018-06-12 15:54:43 -0400715 U32 segNb, bNb, pos;
716 size_t bacc = 0;
717 DISPLAY("Decoding error at pos %u ", (U32)u);
718 for (segNb = 0; segNb < nbBlocks; segNb++) {
719 if (bacc + srcSizes[segNb] > u) break;
720 bacc += srcSizes[segNb];
721 }
722 pos = (U32)(u - bacc);
723 bNb = pos / (128 KB);
724 DISPLAY("(sample %u, block %u, pos %u) \n", segNb, bNb, pos);
725 if (u>5) {
726 int n;
727 DISPLAY("origin: ");
728 for (n=-5; n<0; n++) DISPLAY("%02X ", ((const BYTE*)srcBuffer)[u+n]);
729 DISPLAY(" :%02X: ", ((const BYTE*)srcBuffer)[u]);
730 for (n=1; n<3; n++) DISPLAY("%02X ", ((const BYTE*)srcBuffer)[u+n]);
731 DISPLAY(" \n");
732 DISPLAY("decode: ");
Yann Collet2e45bad2018-08-23 14:21:18 -0700733 for (n=-5; n<0; n++) DISPLAY("%02X ", resultBuffer[u+n]);
734 DISPLAY(" :%02X: ", resultBuffer[u]);
735 for (n=1; n<3; n++) DISPLAY("%02X ", resultBuffer[u+n]);
George Lu20f4f322018-06-12 15:54:43 -0400736 DISPLAY(" \n");
737 }
738 break;
739 }
740 if (u==srcSize-1) { /* should never happen */
741 DISPLAY("no difference detected\n");
Yann Collet77e805e2018-08-21 18:19:27 -0700742 }
George Lu20f4f322018-06-12 15:54:43 -0400743 }
Yann Collet77e805e2018-08-21 18:19:27 -0700744 }
George Lu20f4f322018-06-12 15:54:43 -0400745 } /* CRC Checking */
746
Yann Collet2e45bad2018-08-23 14:21:18 -0700747 if (displayLevel == 1) { /* hidden display mode -q, used by python speed benchmark */
Yann Collet1f9ec132018-08-23 16:03:30 -0700748 double const cSpeed = (double)benchResult.cSpeed / MB_UNIT;
749 double const dSpeed = (double)benchResult.dSpeed / MB_UNIT;
Yann Collet2e45bad2018-08-23 14:21:18 -0700750 if (adv->additionalParam) {
751 DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s (param=%d)\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName, adv->additionalParam);
752 } else {
753 DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName);
754 }
George Luab26f242018-06-21 11:16:53 -0700755 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700756
757 DISPLAYLEVEL(2, "%2i#\n", cLevel);
George Lu85223462018-06-14 14:46:17 -0400758 } /* Bench */
Yann Collet2e45bad2018-08-23 14:21:18 -0700759
760 benchResult.cMem = (1ULL << (comprParams->windowLog)) + ZSTD_sizeof_CCtx(cctx);
761 return BMK_benchOutcome_setValidResult(benchResult);
George Lua8eea992018-06-19 10:58:22 -0700762}
Yann Collet4856a002015-01-24 01:58:16 +0100763
Yann Collet2e45bad2018-08-23 14:21:18 -0700764BMK_benchOutcome_t BMK_benchMemAdvanced(const void* srcBuffer, size_t srcSize,
Yann Collet77e805e2018-08-21 18:19:27 -0700765 void* dstBuffer, size_t dstCapacity,
George Lua8eea992018-06-19 10:58:22 -0700766 const size_t* fileSizes, unsigned nbFiles,
Yann Collet6ce7b082018-08-24 15:59:57 -0700767 int cLevel, const ZSTD_compressionParameters* comprParams,
George Lua8eea992018-06-19 10:58:22 -0700768 const void* dictBuffer, size_t dictBufferSize,
George Lua8eea992018-06-19 10:58:22 -0700769 int displayLevel, const char* displayName, const BMK_advancedParams_t* adv)
770
771{
Yann Collet2e45bad2018-08-23 14:21:18 -0700772 int const dstParamsError = !dstBuffer ^ !dstCapacity; /* must be both NULL or none */
773
George Lua8eea992018-06-19 10:58:22 -0700774 size_t const blockSize = ((adv->blockSize>=32 && (adv->mode != BMK_decodeOnly)) ? adv->blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ;
775 U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
776
777 /* these are the blockTable parameters, just split up */
George Ludd270b22018-07-27 08:49:25 -0700778 const void ** const srcPtrs = (const void**)malloc(maxNbBlocks * sizeof(void*));
779 size_t* const srcSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
George Lua8eea992018-06-19 10:58:22 -0700780
George Lue148db32018-07-20 14:35:09 -0700781
George Ludd270b22018-07-27 08:49:25 -0700782 void ** const cPtrs = (void**)malloc(maxNbBlocks * sizeof(void*));
783 size_t* const cSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
784 size_t* const cCapacities = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
George Lua8eea992018-06-19 10:58:22 -0700785
George Ludd270b22018-07-27 08:49:25 -0700786 void ** const resPtrs = (void**)malloc(maxNbBlocks * sizeof(void*));
787 size_t* const resSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
George Lua8eea992018-06-19 10:58:22 -0700788
Yann Collet55affc02018-08-28 11:21:09 -0700789 BMK_timedFnState_t* timeStateCompress = BMK_createTimedFnState(adv->nbSeconds * 1000, BMK_RUNTEST_DEFAULT_MS);
790 BMK_timedFnState_t* timeStateDecompress = BMK_createTimedFnState(adv->nbSeconds * 1000, BMK_RUNTEST_DEFAULT_MS);
George Lud6121ad2018-06-22 17:25:16 -0700791
Yann Collet2e45bad2018-08-23 14:21:18 -0700792 ZSTD_CCtx* const cctx = ZSTD_createCCtx();
793 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
George Lubfe83922018-08-09 12:07:57 -0700794
George Lu5f490342018-06-18 11:59:45 -0700795 const size_t maxCompressedSize = dstCapacity ? dstCapacity : ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024);
George Ludd270b22018-07-27 08:49:25 -0700796
797 void* const internalDstBuffer = dstBuffer ? NULL : malloc(maxCompressedSize);
798 void* const compressedBuffer = dstBuffer ? dstBuffer : internalDstBuffer;
799
Yann Collet2e45bad2018-08-23 14:21:18 -0700800 BMK_benchOutcome_t outcome = BMK_benchOutcome_error(); /* error by default */
George Lue89f1fb2018-08-14 14:44:47 -0700801
802 void* resultBuffer = srcSize ? malloc(srcSize) : NULL;
803
Yann Collet77e805e2018-08-21 18:19:27 -0700804 int allocationincomplete = !srcPtrs || !srcSizes || !cPtrs ||
805 !cSizes || !cCapacities || !resPtrs || !resSizes ||
Yann Collet2e45bad2018-08-23 14:21:18 -0700806 !timeStateCompress || !timeStateDecompress ||
807 !cctx || !dctx ||
808 !compressedBuffer || !resultBuffer;
George Lu5f490342018-06-18 11:59:45 -0700809
George Lu5f490342018-06-18 11:59:45 -0700810
Yann Collet2e45bad2018-08-23 14:21:18 -0700811 if (!allocationincomplete && !dstParamsError) {
812 outcome = BMK_benchMemAdvancedNoAlloc(srcPtrs, srcSizes,
813 cPtrs, cCapacities, cSizes,
814 resPtrs, resSizes,
815 &resultBuffer,
816 compressedBuffer, maxCompressedSize,
817 timeStateCompress, timeStateDecompress,
818 srcBuffer, srcSize,
819 fileSizes, nbFiles,
820 cLevel, comprParams,
821 dictBuffer, dictBufferSize,
822 cctx, dctx,
823 displayLevel, displayName, adv);
George Lua8eea992018-06-19 10:58:22 -0700824 }
Yann Collet77e805e2018-08-21 18:19:27 -0700825
Yann Colleted699e62015-12-16 02:37:24 +0100826 /* clean up */
Yann Collet77e805e2018-08-21 18:19:27 -0700827 BMK_freeTimedFnState(timeStateCompress);
828 BMK_freeTimedFnState(timeStateDecompress);
829
Yann Collet2e45bad2018-08-23 14:21:18 -0700830 ZSTD_freeCCtx(cctx);
George Lubfe83922018-08-09 12:07:57 -0700831 ZSTD_freeDCtx(dctx);
832
George Ludd270b22018-07-27 08:49:25 -0700833 free(internalDstBuffer);
Yann Collet4856a002015-01-24 01:58:16 +0100834 free(resultBuffer);
George Lu20f4f322018-06-12 15:54:43 -0400835
Yann Collet77e805e2018-08-21 18:19:27 -0700836 free((void*)srcPtrs);
837 free(srcSizes);
838 free(cPtrs);
George Lu20f4f322018-06-12 15:54:43 -0400839 free(cSizes);
George Lue148db32018-07-20 14:35:09 -0700840 free(cCapacities);
George Lu20f4f322018-06-12 15:54:43 -0400841 free(resPtrs);
842 free(resSizes);
843
George Lua8eea992018-06-19 10:58:22 -0700844 if(allocationincomplete) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700845 RETURN_ERROR(31, BMK_benchOutcome_t, "allocation error : not enough memory");
George Lua8eea992018-06-19 10:58:22 -0700846 }
Yann Collet77e805e2018-08-21 18:19:27 -0700847
Yann Collet2e45bad2018-08-23 14:21:18 -0700848 if(dstParamsError) {
849 RETURN_ERROR(32, BMK_benchOutcome_t, "Dst parameters not coherent");
George Ludd270b22018-07-27 08:49:25 -0700850 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700851 return outcome;
Yann Collet4856a002015-01-24 01:58:16 +0100852}
853
Yann Collet2e45bad2018-08-23 14:21:18 -0700854BMK_benchOutcome_t BMK_benchMem(const void* srcBuffer, size_t srcSize,
George Lu20f4f322018-06-12 15:54:43 -0400855 const size_t* fileSizes, unsigned nbFiles,
Yann Collet2e45bad2018-08-23 14:21:18 -0700856 int cLevel, const ZSTD_compressionParameters* comprParams,
George Lu20f4f322018-06-12 15:54:43 -0400857 const void* dictBuffer, size_t dictBufferSize,
George Lu20f4f322018-06-12 15:54:43 -0400858 int displayLevel, const char* displayName) {
859
Yann Collet2e45bad2018-08-23 14:21:18 -0700860 BMK_advancedParams_t const adv = BMK_initAdvancedParams();
George Lu20f4f322018-06-12 15:54:43 -0400861 return BMK_benchMemAdvanced(srcBuffer, srcSize,
George Lu5f490342018-06-18 11:59:45 -0700862 NULL, 0,
George Lu20f4f322018-06-12 15:54:43 -0400863 fileSizes, nbFiles,
864 cLevel, comprParams,
865 dictBuffer, dictBufferSize,
George Lu20f4f322018-06-12 15:54:43 -0400866 displayLevel, displayName, &adv);
867}
868
Yann Collet2e45bad2018-08-23 14:21:18 -0700869static BMK_benchOutcome_t BMK_benchCLevel(const void* srcBuffer, size_t benchedSize,
870 const size_t* fileSizes, unsigned nbFiles,
871 int cLevel, const ZSTD_compressionParameters* comprParams,
872 const void* dictBuffer, size_t dictBufferSize,
873 int displayLevel, const char* displayName,
874 BMK_advancedParams_t const * const adv)
875{
876 const char* pch = strrchr(displayName, '\\'); /* Windows */
877 if (!pch) pch = strrchr(displayName, '/'); /* Linux */
878 if (pch) displayName = pch+1;
879
880 if (adv->realTime) {
881 DISPLAYLEVEL(2, "Note : switching to real-time priority \n");
882 SET_REALTIME_PRIORITY;
883 }
884
885 if (displayLevel == 1 && !adv->additionalParam) /* --quiet mode */
886 DISPLAY("bench %s %s: input %u bytes, %u seconds, %u KB blocks\n",
887 ZSTD_VERSION_STRING, ZSTD_GIT_COMMIT_STRING,
888 (U32)benchedSize, adv->nbSeconds, (U32)(adv->blockSize>>10));
889
890 return BMK_benchMemAdvanced(srcBuffer, benchedSize,
891 NULL, 0,
892 fileSizes, nbFiles,
893 cLevel, comprParams,
894 dictBuffer, dictBufferSize,
895 displayLevel, displayName, adv);
896}
897
898BMK_benchOutcome_t BMK_syntheticTest(int cLevel, double compressibility,
899 const ZSTD_compressionParameters* compressionParams,
900 int displayLevel, const BMK_advancedParams_t* adv)
901{
902 char name[20] = {0};
903 size_t const benchedSize = 10000000;
904 void* srcBuffer;
905 BMK_benchOutcome_t res;
906
907 if (cLevel > ZSTD_maxCLevel()) {
908 RETURN_ERROR(15, BMK_benchOutcome_t, "Invalid Compression Level");
909 }
910
911 /* Memory allocation */
912 srcBuffer = malloc(benchedSize);
913 if (!srcBuffer) RETURN_ERROR(21, BMK_benchOutcome_t, "not enough memory");
914
915 /* Fill input buffer */
916 RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
917
918 /* Bench */
919 snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
920 res = BMK_benchCLevel(srcBuffer, benchedSize,
921 &benchedSize /* ? */, 1 /* ? */,
922 cLevel, compressionParams,
923 NULL, 0, /* dictionary */
924 displayLevel, name, adv);
925
926 /* clean up */
927 free(srcBuffer);
928
929 return res;
930}
931
932
933
Yann Collet4856a002015-01-24 01:58:16 +0100934static size_t BMK_findMaxMem(U64 requiredMem)
935{
Yann Colletde406ee2016-03-20 15:46:10 +0100936 size_t const step = 64 MB;
Yann Collet4856a002015-01-24 01:58:16 +0100937 BYTE* testmem = NULL;
938
939 requiredMem = (((requiredMem >> 26) + 1) << 26);
Yann Colletde406ee2016-03-20 15:46:10 +0100940 requiredMem += step;
Yann Collet050efba2015-11-03 09:49:30 +0100941 if (requiredMem > maxMemory) requiredMem = maxMemory;
Yann Collet4856a002015-01-24 01:58:16 +0100942
Yann Colletde406ee2016-03-20 15:46:10 +0100943 do {
Yann Collet4856a002015-01-24 01:58:16 +0100944 testmem = (BYTE*)malloc((size_t)requiredMem);
Yann Colletde406ee2016-03-20 15:46:10 +0100945 requiredMem -= step;
George Lue89f1fb2018-08-14 14:44:47 -0700946 } while (!testmem && requiredMem > 0);
Yann Colletde406ee2016-03-20 15:46:10 +0100947
Yann Collet4856a002015-01-24 01:58:16 +0100948 free(testmem);
Yann Colletde406ee2016-03-20 15:46:10 +0100949 return (size_t)(requiredMem);
Yann Collet4856a002015-01-24 01:58:16 +0100950}
951
Yann Colleta5b66e32016-03-26 01:48:27 +0100952/*! BMK_loadFiles() :
Yann Collet6a9b41b2018-03-11 19:56:48 -0700953 * Loads `buffer` with content of files listed within `fileNamesTable`.
954 * At most, fills `buffer` entirely. */
George Lu20f4f322018-06-12 15:54:43 -0400955static int BMK_loadFiles(void* buffer, size_t bufferSize,
Yann Collet4086b282018-08-30 11:02:08 -0700956 size_t* fileSizes,
957 const char* const * fileNamesTable, unsigned nbFiles,
958 int displayLevel)
Yann Colleted699e62015-12-16 02:37:24 +0100959{
inikepc0d5f4e2016-04-13 10:48:04 +0200960 size_t pos = 0, totalSize = 0;
Yann Collet699b14d2016-03-17 19:37:33 +0100961 unsigned n;
Yann Colletfd416f12016-01-30 03:14:15 +0100962 for (n=0; n<nbFiles; n++) {
Yann Collete162ace2016-05-20 11:24:35 +0200963 FILE* f;
inikep69fcd7c2016-04-28 12:23:33 +0200964 U64 fileSize = UTIL_getFileSize(fileNamesTable[n]);
965 if (UTIL_isDirectory(fileNamesTable[n])) {
George Lu01d940b2018-06-11 10:59:05 -0400966 DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]);
inikepbab43172016-04-29 15:19:40 +0200967 fileSizes[n] = 0;
inikepc0d5f4e2016-04-13 10:48:04 +0200968 continue;
969 }
Yann Collet18b79532017-10-17 16:14:25 -0700970 if (fileSize == UTIL_FILESIZE_UNKNOWN) {
George Lu01d940b2018-06-11 10:59:05 -0400971 DISPLAYLEVEL(2, "Cannot evaluate size of %s, ignoring ... \n", fileNamesTable[n]);
Yann Collet18b79532017-10-17 16:14:25 -0700972 fileSizes[n] = 0;
973 continue;
974 }
inikepea4ee3e2016-04-25 13:09:06 +0200975 f = fopen(fileNamesTable[n], "rb");
George Lu20f4f322018-06-12 15:54:43 -0400976 if (f==NULL) EXM_THROW_INT(10, "impossible to open file %s", fileNamesTable[n]);
Yann Colletbf2bc112016-08-02 23:48:13 +0200977 DISPLAYUPDATE(2, "Loading %s... \r", fileNamesTable[n]);
Yann Colleta5b66e32016-03-26 01:48:27 +0100978 if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */
Yann Collet4086b282018-08-30 11:02:08 -0700979 { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
980 if (readSize != (size_t)fileSize) EXM_THROW_INT(11, "could not read %s", fileNamesTable[n]);
981 pos += readSize;
982 }
Yann Colleta52c98d2015-12-16 03:12:31 +0100983 fileSizes[n] = (size_t)fileSize;
inikepc0d5f4e2016-04-13 10:48:04 +0200984 totalSize += (size_t)fileSize;
Yann Colleted699e62015-12-16 02:37:24 +0100985 fclose(f);
986 }
Yann Collet6f9c0562016-05-01 10:26:30 +0200987
George Lu20f4f322018-06-12 15:54:43 -0400988 if (totalSize == 0) EXM_THROW_INT(12, "no data to bench");
989 return 0;
Yann Colleted699e62015-12-16 02:37:24 +0100990}
991
Yann Collet2e45bad2018-08-23 14:21:18 -0700992BMK_benchOutcome_t BMK_benchFilesAdvanced(
993 const char* const * fileNamesTable, unsigned nbFiles,
994 const char* dictFileName, int cLevel,
995 const ZSTD_compressionParameters* compressionParams,
996 int displayLevel, const BMK_advancedParams_t* adv)
Yann Colleted699e62015-12-16 02:37:24 +0100997{
George Lud6121ad2018-06-22 17:25:16 -0700998 void* srcBuffer = NULL;
Yann Colleted699e62015-12-16 02:37:24 +0100999 size_t benchedSize;
Yann Collet31683c02015-12-18 01:26:48 +01001000 void* dictBuffer = NULL;
1001 size_t dictBufferSize = 0;
George Lud6121ad2018-06-22 17:25:16 -07001002 size_t* fileSizes = NULL;
Yann Colletc3a4baa2018-08-24 21:38:09 -07001003 BMK_benchOutcome_t res;
Yann Collete162ace2016-05-20 11:24:35 +02001004 U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +01001005
Yann Collet2e45bad2018-08-23 14:21:18 -07001006 if (!nbFiles) {
1007 RETURN_ERROR(14, BMK_benchOutcome_t, "No Files to Benchmark");
George Lua8eea992018-06-19 10:58:22 -07001008 }
Yann Collet31683c02015-12-18 01:26:48 +01001009
George Lua8eea992018-06-19 10:58:22 -07001010 if (cLevel > ZSTD_maxCLevel()) {
Yann Collet2e45bad2018-08-23 14:21:18 -07001011 RETURN_ERROR(15, BMK_benchOutcome_t, "Invalid Compression Level");
George Lua8eea992018-06-19 10:58:22 -07001012 }
1013
1014 fileSizes = (size_t*)calloc(nbFiles, sizeof(size_t));
Yann Collet2e45bad2018-08-23 14:21:18 -07001015 if (!fileSizes) RETURN_ERROR(12, BMK_benchOutcome_t, "not enough memory for fileSizes");
1016
Yann Collet31683c02015-12-18 01:26:48 +01001017 /* Load dictionary */
Yann Colletfd416f12016-01-30 03:14:15 +01001018 if (dictFileName != NULL) {
Yann Collet18b79532017-10-17 16:14:25 -07001019 U64 const dictFileSize = UTIL_getFileSize(dictFileName);
George Lua8eea992018-06-19 10:58:22 -07001020 if (dictFileSize > 64 MB) {
1021 free(fileSizes);
Yann Collet2e45bad2018-08-23 14:21:18 -07001022 RETURN_ERROR(10, BMK_benchOutcome_t, "dictionary file %s too large", dictFileName);
George Lua8eea992018-06-19 10:58:22 -07001023 }
Yann Collet31683c02015-12-18 01:26:48 +01001024 dictBufferSize = (size_t)dictFileSize;
1025 dictBuffer = malloc(dictBufferSize);
George Lua8eea992018-06-19 10:58:22 -07001026 if (dictBuffer==NULL) {
1027 free(fileSizes);
Yann Collet2e45bad2018-08-23 14:21:18 -07001028 RETURN_ERROR(11, BMK_benchOutcome_t, "not enough memory for dictionary (%u bytes)",
Yann Collet18b79532017-10-17 16:14:25 -07001029 (U32)dictBufferSize);
George Lua8eea992018-06-19 10:58:22 -07001030 }
Yann Collet2e45bad2018-08-23 14:21:18 -07001031
1032 { int const errorCode = BMK_loadFiles(dictBuffer, dictBufferSize,
1033 fileSizes, &dictFileName /*?*/,
1034 1 /*?*/, displayLevel);
1035 if (errorCode) {
1036 res = BMK_benchOutcome_error();
George Lud6121ad2018-06-22 17:25:16 -07001037 goto _cleanUp;
Yann Collet2e45bad2018-08-23 14:21:18 -07001038 } }
Yann Collet31683c02015-12-18 01:26:48 +01001039 }
1040
Yann Colleted699e62015-12-16 02:37:24 +01001041 /* Memory allocation & restrictions */
1042 benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
1043 if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
1044 if (benchedSize < totalSizeToLoad)
1045 DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20));
George Lue89f1fb2018-08-14 14:44:47 -07001046
1047 srcBuffer = benchedSize ? malloc(benchedSize) : NULL;
George Lua8eea992018-06-19 10:58:22 -07001048 if (!srcBuffer) {
1049 free(dictBuffer);
1050 free(fileSizes);
Yann Collet2e45bad2018-08-23 14:21:18 -07001051 RETURN_ERROR(12, BMK_benchOutcome_t, "not enough memory");
George Lua8eea992018-06-19 10:58:22 -07001052 }
Yann Colleted699e62015-12-16 02:37:24 +01001053
1054 /* Load input buffer */
Yann Collet2e45bad2018-08-23 14:21:18 -07001055 { int const errorCode = BMK_loadFiles(srcBuffer, benchedSize,
1056 fileSizes, fileNamesTable, nbFiles,
1057 displayLevel);
1058 if (errorCode) {
1059 res = BMK_benchOutcome_error();
George Lud6121ad2018-06-22 17:25:16 -07001060 goto _cleanUp;
Yann Collet2e45bad2018-08-23 14:21:18 -07001061 } }
1062
Yann Colleted699e62015-12-16 02:37:24 +01001063 /* Bench */
Yann Collet2e45bad2018-08-23 14:21:18 -07001064 { char mfName[20] = {0};
Yann Colletd898fb72017-11-17 00:22:55 -08001065 snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
Yann Collet2e45bad2018-08-23 14:21:18 -07001066 { const char* const displayName = (nbFiles > 1) ? mfName : fileNamesTable[0];
Yann Collet77e805e2018-08-21 18:19:27 -07001067 res = BMK_benchCLevel(srcBuffer, benchedSize,
Yann Collet2e45bad2018-08-23 14:21:18 -07001068 fileSizes, nbFiles,
1069 cLevel, compressionParams,
1070 dictBuffer, dictBufferSize,
1071 displayLevel, displayName,
1072 adv);
Yann Colletd898fb72017-11-17 00:22:55 -08001073 } }
Yann Collet4856a002015-01-24 01:58:16 +01001074
George Lud6121ad2018-06-22 17:25:16 -07001075_cleanUp:
Yann Collet4856a002015-01-24 01:58:16 +01001076 free(srcBuffer);
Yann Collet31683c02015-12-18 01:26:48 +01001077 free(dictBuffer);
Yann Collet70611352015-12-16 03:01:03 +01001078 free(fileSizes);
George Lu20f4f322018-06-12 15:54:43 -04001079 return res;
Yann Collet4856a002015-01-24 01:58:16 +01001080}
1081
1082
Yann Collet2e45bad2018-08-23 14:21:18 -07001083BMK_benchOutcome_t BMK_benchFiles(
1084 const char* const * fileNamesTable, unsigned nbFiles,
1085 const char* dictFileName,
1086 int cLevel, const ZSTD_compressionParameters* compressionParams,
1087 int displayLevel)
Yann Collet4856a002015-01-24 01:58:16 +01001088{
Yann Collet2e45bad2018-08-23 14:21:18 -07001089 BMK_advancedParams_t const adv = BMK_initAdvancedParams();
George Lu0d1ee222018-06-15 16:21:08 -04001090 return BMK_benchFilesAdvanced(fileNamesTable, nbFiles, dictFileName, cLevel, compressionParams, displayLevel, &adv);
Yann Collet4856a002015-01-24 01:58:16 +01001091}