blob: 617a0a8f42fe2fa3f3939310ecc5c28c0998c4b6 [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 Collet2e45bad2018-08-23 14:21:18 -070066static const size_t maxMemory = (sizeof(size_t)==4) ?
67 /* 32-bit */ (2 GB - 64 MB) :
68 /* 64-bit */ (size_t)(1ULL << ((sizeof(size_t)*8)-31));
69
Yann Collet4856a002015-01-24 01:58:16 +010070
Yann Colletf3eca252015-10-22 15:31:46 +010071/* *************************************
Yann Colleted699e62015-12-16 02:37:24 +010072* console display
Yann Colletf3eca252015-10-22 15:31:46 +010073***************************************/
Yann Colleted699e62015-12-16 02:37:24 +010074#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
George Lu01d940b2018-06-11 10:59:05 -040075#define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); }
76/* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
Yann Colleted699e62015-12-16 02:37:24 +010077
Nick Terrell9a2f6f42017-11-29 19:11:12 -080078static const U64 g_refreshRate = SEC_TO_MICRO / 6;
79static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
80
George Lu01d940b2018-06-11 10:59:05 -040081#define DISPLAYUPDATE(l, ...) { if (displayLevel>=l) { \
82 if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (displayLevel>=4)) \
Nick Terrell9a2f6f42017-11-29 19:11:12 -080083 { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \
George Lu01d940b2018-06-11 10:59:05 -040084 if (displayLevel>=4) fflush(stderr); } } }
Yann Colletbf2bc112016-08-02 23:48:13 +020085
Yann Colleted699e62015-12-16 02:37:24 +010086
87/* *************************************
88* Exceptions
89***************************************/
90#ifndef DEBUG
George Lu3adc2172018-07-12 17:30:39 -070091# define DEBUG 0
Yann Colleted699e62015-12-16 02:37:24 +010092#endif
Yann Colletfe234bf2017-06-19 15:23:19 -070093#define DEBUGOUTPUT(...) { if (DEBUG) DISPLAY(__VA_ARGS__); }
George Lu20f4f322018-06-12 15:54:43 -040094
95#define EXM_THROW_INT(errorNum, ...) { \
Yann Colletfe234bf2017-06-19 15:23:19 -070096 DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
George Lu20f4f322018-06-12 15:54:43 -040097 DISPLAYLEVEL(1, "Error %i : ", errorNum); \
George Lu01d940b2018-06-11 10:59:05 -040098 DISPLAYLEVEL(1, __VA_ARGS__); \
99 DISPLAYLEVEL(1, " \n"); \
George Lu20f4f322018-06-12 15:54:43 -0400100 return errorNum; \
Yann Colleted699e62015-12-16 02:37:24 +0100101}
Yann Collet4856a002015-01-24 01:58:16 +0100102
Yann Collet2e45bad2018-08-23 14:21:18 -0700103#define RETURN_ERROR(errorNum, retType, ...) { \
George Lu20f4f322018-06-12 15:54:43 -0400104 retType r; \
105 memset(&r, 0, sizeof(retType)); \
106 DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
107 DISPLAYLEVEL(1, "Error %i : ", errorNum); \
108 DISPLAYLEVEL(1, __VA_ARGS__); \
109 DISPLAYLEVEL(1, " \n"); \
Yann Collet2e45bad2018-08-23 14:21:18 -0700110 r.tag = errorNum; \
George Lu20f4f322018-06-12 15:54:43 -0400111 return r; \
112}
Yann Collet4856a002015-01-24 01:58:16 +0100113
George Lu0d1ee222018-06-15 16:21:08 -0400114/* error without displaying */
Yann Collet2e45bad2018-08-23 14:21:18 -0700115#define RETURN_QUIET_ERROR(errorNum, retType, ...) { \
George Lu0d1ee222018-06-15 16:21:08 -0400116 retType r; \
117 memset(&r, 0, sizeof(retType)); \
118 DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
George Lud6121ad2018-06-22 17:25:16 -0700119 DEBUGOUTPUT("Error %i : ", errorNum); \
120 DEBUGOUTPUT(__VA_ARGS__); \
121 DEBUGOUTPUT(" \n"); \
Yann Collet2e45bad2018-08-23 14:21:18 -0700122 r.tag = errorNum; \
George Lu0d1ee222018-06-15 16:21:08 -0400123 return r; \
124}
125
Yann Colletf3eca252015-10-22 15:31:46 +0100126/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100127* Benchmark Parameters
Yann Colletf3eca252015-10-22 15:31:46 +0100128***************************************/
Stella Laua1f04d52017-09-01 14:52:51 -0700129
Yann Collet77e805e2018-08-21 18:19:27 -0700130BMK_advancedParams_t BMK_initAdvancedParams(void) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700131 BMK_advancedParams_t const res = {
George Lu85223462018-06-14 14:46:17 -0400132 BMK_both, /* mode */
George Lu20f4f322018-06-12 15:54:43 -0400133 BMK_TIMETEST_DEFAULT_S, /* nbSeconds */
134 0, /* blockSize */
135 0, /* nbWorkers */
136 0, /* realTime */
George Lu20f4f322018-06-12 15:54:43 -0400137 0, /* additionalParam */
Yann Collet77e805e2018-08-21 18:19:27 -0700138 0, /* ldmFlag */
George Lu20f4f322018-06-12 15:54:43 -0400139 0, /* ldmMinMatch */
140 0, /* ldmHashLog */
George Lud6121ad2018-06-22 17:25:16 -0700141 0, /* ldmBuckSizeLog */
142 0 /* ldmHashEveryLog */
George Lu20f4f322018-06-12 15:54:43 -0400143 };
144 return res;
Stella Lau67d4a612017-09-02 21:10:36 -0700145}
146
Yann Collet4856a002015-01-24 01:58:16 +0100147
Yann Colletf3eca252015-10-22 15:31:46 +0100148/* ********************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100149* Bench functions
Yann Colletf3eca252015-10-22 15:31:46 +0100150**********************************************************/
Yann Colletd9465012016-12-06 16:49:23 -0800151typedef struct {
152 const void* srcPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100153 size_t srcSize;
Yann Colletd9465012016-12-06 16:49:23 -0800154 void* cPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100155 size_t cRoom;
156 size_t cSize;
Yann Colletd9465012016-12-06 16:49:23 -0800157 void* resPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100158 size_t resSize;
159} blockParam_t;
160
Sean Purcell42bac7f2017-04-13 15:35:05 -0700161#undef MIN
162#undef MAX
163#define MIN(a,b) ((a) < (b) ? (a) : (b))
164#define MAX(a,b) ((a) > (b) ? (a) : (b))
Yann Collet1c00dc32015-10-21 08:22:25 +0100165
Yann Collet77e805e2018-08-21 18:19:27 -0700166static void BMK_initCCtx(ZSTD_CCtx* ctx,
167 const void* dictBuffer, size_t dictBufferSize, int cLevel,
George Lu20f4f322018-06-12 15:54:43 -0400168 const ZSTD_compressionParameters* comprParams, const BMK_advancedParams_t* adv) {
George Lue148db32018-07-20 14:35:09 -0700169 ZSTD_CCtx_reset(ctx);
170 ZSTD_CCtx_resetParameters(ctx);
George Lu20f4f322018-06-12 15:54:43 -0400171 if (adv->nbWorkers==1) {
172 ZSTD_CCtx_setParameter(ctx, ZSTD_p_nbWorkers, 0);
173 } else {
174 ZSTD_CCtx_setParameter(ctx, ZSTD_p_nbWorkers, adv->nbWorkers);
175 }
176 ZSTD_CCtx_setParameter(ctx, ZSTD_p_compressionLevel, cLevel);
177 ZSTD_CCtx_setParameter(ctx, ZSTD_p_enableLongDistanceMatching, adv->ldmFlag);
178 ZSTD_CCtx_setParameter(ctx, ZSTD_p_ldmMinMatch, adv->ldmMinMatch);
179 ZSTD_CCtx_setParameter(ctx, ZSTD_p_ldmHashLog, adv->ldmHashLog);
George Lud6121ad2018-06-22 17:25:16 -0700180 ZSTD_CCtx_setParameter(ctx, ZSTD_p_ldmBucketSizeLog, adv->ldmBucketSizeLog);
181 ZSTD_CCtx_setParameter(ctx, ZSTD_p_ldmHashEveryLog, adv->ldmHashEveryLog);
George Lu20f4f322018-06-12 15:54:43 -0400182 ZSTD_CCtx_setParameter(ctx, ZSTD_p_windowLog, comprParams->windowLog);
183 ZSTD_CCtx_setParameter(ctx, ZSTD_p_hashLog, comprParams->hashLog);
184 ZSTD_CCtx_setParameter(ctx, ZSTD_p_chainLog, comprParams->chainLog);
185 ZSTD_CCtx_setParameter(ctx, ZSTD_p_searchLog, comprParams->searchLog);
186 ZSTD_CCtx_setParameter(ctx, ZSTD_p_minMatch, comprParams->searchLength);
187 ZSTD_CCtx_setParameter(ctx, ZSTD_p_targetLength, comprParams->targetLength);
George Lu7b5b3d72018-07-16 16:16:31 -0700188 ZSTD_CCtx_setParameter(ctx, ZSTD_p_compressionStrategy, comprParams->strategy);
George Lu20f4f322018-06-12 15:54:43 -0400189 ZSTD_CCtx_loadDictionary(ctx, dictBuffer, dictBufferSize);
190}
191
Yann Collet77e805e2018-08-21 18:19:27 -0700192static void BMK_initDCtx(ZSTD_DCtx* dctx,
George Lu20f4f322018-06-12 15:54:43 -0400193 const void* dictBuffer, size_t dictBufferSize) {
George Lue148db32018-07-20 14:35:09 -0700194 ZSTD_DCtx_reset(dctx);
George Lu20f4f322018-06-12 15:54:43 -0400195 ZSTD_DCtx_loadDictionary(dctx, dictBuffer, dictBufferSize);
196}
197
Yann Collet2e45bad2018-08-23 14:21:18 -0700198
George Lu20f4f322018-06-12 15:54:43 -0400199typedef struct {
Yann Collet2e45bad2018-08-23 14:21:18 -0700200 ZSTD_CCtx* cctx;
George Lu20f4f322018-06-12 15:54:43 -0400201 const void* dictBuffer;
202 size_t dictBufferSize;
203 int cLevel;
204 const ZSTD_compressionParameters* comprParams;
205 const BMK_advancedParams_t* adv;
206} BMK_initCCtxArgs;
207
208static size_t local_initCCtx(void* payload) {
209 BMK_initCCtxArgs* ag = (BMK_initCCtxArgs*)payload;
Yann Collet2e45bad2018-08-23 14:21:18 -0700210 BMK_initCCtx(ag->cctx, ag->dictBuffer, ag->dictBufferSize, ag->cLevel, ag->comprParams, ag->adv);
George Lu20f4f322018-06-12 15:54:43 -0400211 return 0;
212}
213
214typedef struct {
215 ZSTD_DCtx* dctx;
216 const void* dictBuffer;
217 size_t dictBufferSize;
218} BMK_initDCtxArgs;
219
220static size_t local_initDCtx(void* payload) {
221 BMK_initDCtxArgs* ag = (BMK_initDCtxArgs*)payload;
222 BMK_initDCtx(ag->dctx, ag->dictBuffer, ag->dictBufferSize);
223 return 0;
224}
225
Yann Collet2e45bad2018-08-23 14:21:18 -0700226
227/* `addArgs` is the context */
George Lu20f4f322018-06-12 15:54:43 -0400228static size_t local_defaultCompress(
Yann Collet2e45bad2018-08-23 14:21:18 -0700229 const void* srcBuffer, size_t srcSize,
230 void* dstBuffer, size_t dstSize,
231 void* addArgs)
232{
George Lu20f4f322018-06-12 15:54:43 -0400233 size_t moreToFlush = 1;
Yann Collet2e45bad2018-08-23 14:21:18 -0700234 ZSTD_CCtx* const cctx = (ZSTD_CCtx*)addArgs;
George Lu20f4f322018-06-12 15:54:43 -0400235 ZSTD_inBuffer in;
236 ZSTD_outBuffer out;
Yann Collet2e45bad2018-08-23 14:21:18 -0700237 in.src = srcBuffer; in.size = srcSize; in.pos = 0;
238 out.dst = dstBuffer; out.size = dstSize; out.pos = 0;
George Lu20f4f322018-06-12 15:54:43 -0400239 while (moreToFlush) {
George Lud6121ad2018-06-22 17:25:16 -0700240 if(out.pos == out.size) {
241 return (size_t)-ZSTD_error_dstSize_tooSmall;
242 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700243 moreToFlush = ZSTD_compress_generic(cctx, &out, &in, ZSTD_e_end);
George Lu20f4f322018-06-12 15:54:43 -0400244 if (ZSTD_isError(moreToFlush)) {
245 return moreToFlush;
246 }
247 }
248 return out.pos;
249}
250
Yann Collet2e45bad2018-08-23 14:21:18 -0700251/* `addArgs` is the context */
George Lu20f4f322018-06-12 15:54:43 -0400252static size_t local_defaultDecompress(
Yann Collet2e45bad2018-08-23 14:21:18 -0700253 const void* srcBuffer, size_t srcSize,
254 void* dstBuffer, size_t dstSize,
255 void* addArgs)
256{
George Lu20f4f322018-06-12 15:54:43 -0400257 size_t moreToFlush = 1;
Yann Collet2e45bad2018-08-23 14:21:18 -0700258 ZSTD_DCtx* const dctx = (ZSTD_DCtx*)addArgs;
George Lu20f4f322018-06-12 15:54:43 -0400259 ZSTD_inBuffer in;
260 ZSTD_outBuffer out;
Yann Collet2e45bad2018-08-23 14:21:18 -0700261 in.src = srcBuffer; in.size = srcSize; in.pos = 0;
262 out.dst = dstBuffer; out.size = dstSize; out.pos = 0;
George Lu20f4f322018-06-12 15:54:43 -0400263 while (moreToFlush) {
George Lud6121ad2018-06-22 17:25:16 -0700264 if(out.pos == out.size) {
265 return (size_t)-ZSTD_error_dstSize_tooSmall;
266 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700267 moreToFlush = ZSTD_decompress_generic(dctx, &out, &in);
George Lu20f4f322018-06-12 15:54:43 -0400268 if (ZSTD_isError(moreToFlush)) {
269 return moreToFlush;
270 }
271 }
272 return out.pos;
273
274}
275
Yann Collet2e45bad2018-08-23 14:21:18 -0700276
277/*=== Benchmarking an arbitrary function ===*/
278
279int BMK_isSuccessful_runOutcome(BMK_runOutcome_t outcome)
280{
281 return outcome.tag == 0;
282}
283
284/* warning : this function will stop program execution if outcome is invalid !
285 * check outcome validity first, using BMK_isValid_runResult() */
286BMK_runTime_t BMK_extract_runTime(BMK_runOutcome_t outcome)
287{
288 assert(outcome.tag == 0);
289 return outcome.internal_never_use_directly;
290}
291
292static BMK_runOutcome_t BMK_setValid_runTime(BMK_runTime_t runTime)
293{
294 BMK_runOutcome_t outcome;
295 outcome.tag = 0;
296 outcome.internal_never_use_directly = runTime;
297 return outcome;
298}
299
300
301/* initFn will be measured once, benchFn will be measured `nbLoops` times */
302/* initFn is optional, provide NULL if none */
303/* benchFn must return size_t field compliant with ZSTD_isError for error valuee */
George Lu20f4f322018-06-12 15:54:43 -0400304/* takes # of blocks and list of size & stuff for each. */
Yann Collet2e45bad2018-08-23 14:21:18 -0700305/* can report result of benchFn for each block into blockResult. */
306/* blockResult is optional, provide NULL if this information is not required */
307/* note : time per loop could be zero if run time < timer resolution */
308BMK_runOutcome_t BMK_benchFunction(
309 BMK_benchFn_t benchFn, void* benchPayload,
310 BMK_initFn_t initFn, void* initPayload,
311 size_t blockCount,
312 const void* const * srcBlockBuffers, const size_t* srcBlockSizes,
313 void* const * dstBlockBuffers, const size_t* dstBlockCapacities,
Yann Colletd39a25c2018-08-23 14:28:51 -0700314 size_t* blockResults,
Yann Collet2e45bad2018-08-23 14:21:18 -0700315 unsigned nbLoops)
316{
George Lue148db32018-07-20 14:35:09 -0700317 size_t dstSize = 0;
George Lu20f4f322018-06-12 15:54:43 -0400318
George Lud6121ad2018-06-22 17:25:16 -0700319 if(!nbLoops) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700320 RETURN_QUIET_ERROR(1, BMK_runOutcome_t, "nbLoops must be nonzero ");
George Lu20f4f322018-06-12 15:54:43 -0400321 }
322
Yann Collet2e45bad2018-08-23 14:21:18 -0700323 /* init */
324 { size_t i;
George Lu5f490342018-06-18 11:59:45 -0700325 for(i = 0; i < blockCount; i++) {
George Lue148db32018-07-20 14:35:09 -0700326 memset(dstBlockBuffers[i], 0xE5, dstBlockCapacities[i]); /* warm up and erase result buffer */
George Lu5f490342018-06-18 11:59:45 -0700327 }
George Lue148db32018-07-20 14:35:09 -0700328#if 0
Yann Collet77e805e2018-08-21 18:19:27 -0700329 /* based on testing these seem to lower accuracy of multiple calls of 1 nbLoops vs 1 call of multiple nbLoops
330 * (Makes former slower)
George Lue148db32018-07-20 14:35:09 -0700331 */
332 UTIL_sleepMilli(5); /* give processor time to other processes */
333 UTIL_waitForNextTick();
334#endif
George Lu5f490342018-06-18 11:59:45 -0700335 }
336
Yann Collet4da5bdf2018-08-23 18:04:50 -0700337 /* benchmark */
Yann Collet2e45bad2018-08-23 14:21:18 -0700338 { UTIL_time_t const clockStart = UTIL_getTime();
339 unsigned loopNb, blockNb;
340 if (initFn != NULL) initFn(initPayload);
341 for (loopNb = 0; loopNb < nbLoops; loopNb++) {
342 for (blockNb = 0; blockNb < blockCount; blockNb++) {
343 size_t const res = benchFn(srcBlockBuffers[blockNb], srcBlockSizes[blockNb],
344 dstBlockBuffers[blockNb], dstBlockCapacities[blockNb],
345 benchPayload);
George Lua8eea992018-06-19 10:58:22 -0700346 if(ZSTD_isError(res)) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700347 RETURN_QUIET_ERROR(2, BMK_runOutcome_t,
348 "Function benchmark failed on block %u of size %u : %s",
349 blockNb, (U32)dstBlockCapacities[blockNb], ZSTD_getErrorName(res));
350 } else if (loopNb == 0) {
George Lua8eea992018-06-19 10:58:22 -0700351 dstSize += res;
Yann Colletd39a25c2018-08-23 14:28:51 -0700352 if (blockResults != NULL) blockResults[blockNb] = res;
Yann Collet2e45bad2018-08-23 14:21:18 -0700353 } }
354 } /* for (loopNb = 0; loopNb < nbLoops; loopNb++) */
George Lua8eea992018-06-19 10:58:22 -0700355
Yann Collet4da5bdf2018-08-23 18:04:50 -0700356 { U64 const totalTime = UTIL_clockSpanNano(clockStart);
357 BMK_runTime_t rt;
358 rt.nanoSecPerRun = totalTime / nbLoops;
359 rt.sumOfReturn = dstSize;
360 return BMK_setValid_runTime(rt);
361 } }
Yann Collet77e805e2018-08-21 18:19:27 -0700362}
George Luab26f242018-06-21 11:16:53 -0700363
George Lud6121ad2018-06-22 17:25:16 -0700364
Yann Collet2e45bad2018-08-23 14:21:18 -0700365/* ==== Benchmarking any function, providing intermediate results ==== */
366
367struct BMK_timedFnState_s {
368 U64 timeSpent_ns;
369 U64 timeBudget_ns;
370 BMK_runTime_t fastestRun;
371 unsigned nbLoops;
372 UTIL_time_t coolTime;
373}; /* typedef'd to BMK_timedFnState_t within bench.h */
George Luab26f242018-06-21 11:16:53 -0700374
Yann Collet77e805e2018-08-21 18:19:27 -0700375BMK_timedFnState_t* BMK_createTimedFnState(unsigned nbSeconds) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700376 BMK_timedFnState_t* const r = (BMK_timedFnState_t*)malloc(sizeof(*r));
377 if (r == NULL) return NULL; /* malloc() error */
Yann Collet77e805e2018-08-21 18:19:27 -0700378 BMK_resetTimedFnState(r, nbSeconds);
George Lud6121ad2018-06-22 17:25:16 -0700379 return r;
380}
George Luab26f242018-06-21 11:16:53 -0700381
Yann Collet2e45bad2018-08-23 14:21:18 -0700382void BMK_resetTimedFnState(BMK_timedFnState_t* r, unsigned nbSeconds) {
383 r->timeSpent_ns = 0;
384 r->timeBudget_ns = (U64)nbSeconds * TIMELOOP_NANOSEC;
385 r->fastestRun.nanoSecPerRun = (U64)(-1LL);
386 r->fastestRun.sumOfReturn = (size_t)(-1LL);
387 r->nbLoops = 1;
388 r->coolTime = UTIL_getTime();
389}
390
Yann Collet77e805e2018-08-21 18:19:27 -0700391void BMK_freeTimedFnState(BMK_timedFnState_t* state) {
George Lud6121ad2018-06-22 17:25:16 -0700392 free(state);
393}
394
George Luab26f242018-06-21 11:16:53 -0700395
Yann Collet2e45bad2018-08-23 14:21:18 -0700396/* check first if the return structure represents an error or a valid result */
397int BMK_isSuccessful_timedFnOutcome(BMK_timedFnOutcome_t outcome)
398{
399 return (outcome.tag < 2);
400}
401
402/* extract intermediate results from variant type.
403 * note : this function will abort() program execution if result is not valid.
404 * check result validity first, by using BMK_isSuccessful_timedFnOutcome() */
405BMK_runTime_t BMK_extract_timedFnResult(BMK_timedFnOutcome_t outcome)
406{
407 assert(outcome.tag < 2);
408 return outcome.internal_never_use_directly;
409}
410
411/* Tells if nb of seconds set in timedFnState for all runs is spent.
412 * note : this function will return 1 if BMK_benchFunctionTimed() has actually errored. */
413int BMK_isCompleted_timedFnOutcome(BMK_timedFnOutcome_t outcome)
414{
415 return (outcome.tag >= 1);
416}
417
418
419#define MINUSABLETIME (TIMELOOP_NANOSEC / 2) /* 0.5 seconds */
420
421BMK_timedFnOutcome_t BMK_benchFunctionTimed(
422 BMK_timedFnState_t* cont,
423 BMK_benchFn_t benchFn, void* benchPayload,
424 BMK_initFn_t initFn, void* initPayload,
425 size_t blockCount,
426 const void* const* srcBlockBuffers, const size_t* srcBlockSizes,
427 void * const * dstBlockBuffers, const size_t * dstBlockCapacities,
428 size_t* blockResults)
429{
430 int completed = 0;
431 BMK_timedFnOutcome_t r;
432 BMK_runTime_t bestRunTime = cont->fastestRun;
433
434 r.tag = 2; /* error by default */
435
436 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 */
456 r.tag = 2;
George Lud6121ad2018-06-22 17:25:16 -0700457 return r;
George Luab26f242018-06-21 11:16:53 -0700458 }
459
Yann Collet2e45bad2018-08-23 14:21:18 -0700460 { BMK_runTime_t const newRunTime = BMK_extract_runTime(runResult);
461 U64 const loopDuration_ns = newRunTime.nanoSecPerRun * cont->nbLoops;
462
463 cont->timeSpent_ns += loopDuration_ns;
464
465 /* estimate nbLoops for next run to last approximately 1 second */
466 if (loopDuration_ns > (TIMELOOP_NANOSEC / 50)) {
467 U64 const fastestRun_ns = MIN(bestRunTime.nanoSecPerRun, newRunTime.nanoSecPerRun);
468 cont->nbLoops = (U32)(TIMELOOP_NANOSEC / fastestRun_ns) + 1;
George Luab26f242018-06-21 11:16:53 -0700469 } else {
Yann Collet2e45bad2018-08-23 14:21:18 -0700470 /* previous run was too short : blindly increase workload by x multiplier */
471 const unsigned multiplier = 10;
George Lud6121ad2018-06-22 17:25:16 -0700472 assert(cont->nbLoops < ((unsigned)-1) / multiplier); /* avoid overflow */
473 cont->nbLoops *= multiplier;
George Luab26f242018-06-21 11:16:53 -0700474 }
George Luab26f242018-06-21 11:16:53 -0700475
Yann Collet2e45bad2018-08-23 14:21:18 -0700476 if(loopDuration_ns < MINUSABLETIME) {
477 /* don't report results for which benchmark run time was too small : increased risks of rounding errors */
478 assert(completed == 0);
479 continue;
480 } else {
481 if(newRunTime.nanoSecPerRun < bestRunTime.nanoSecPerRun) {
482 bestRunTime = newRunTime;
483 }
484 completed = 1;
485 }
George Luab26f242018-06-21 11:16:53 -0700486 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700487 } /* while (!completed) */
488
489 r.tag = (cont->timeSpent_ns >= cont->timeBudget_ns); /* report if time budget is spent */
490 r.internal_never_use_directly = bestRunTime;
George Lud6121ad2018-06-22 17:25:16 -0700491 return r;
George Lu20f4f322018-06-12 15:54:43 -0400492}
493
George Lu01d940b2018-06-11 10:59:05 -0400494
Yann Collet2e45bad2018-08-23 14:21:18 -0700495/* ================================================================= */
496/* Benchmark Zstandard, mem-to-mem scenarios */
497/* ================================================================= */
498
499int BMK_isSuccessful_benchOutcome(BMK_benchOutcome_t outcome)
500{
501 return outcome.tag == 0;
502}
503
504BMK_benchResult_t BMK_extract_benchResult(BMK_benchOutcome_t outcome)
505{
506 assert(outcome.tag == 0);
507 return outcome.internal_never_use_directly;
508}
509
Yann Collet6ce7b082018-08-24 15:59:57 -0700510static BMK_benchOutcome_t BMK_benchOutcome_error(void)
Yann Collet2e45bad2018-08-23 14:21:18 -0700511{
512 BMK_benchOutcome_t b;
513 memset(&b, 0, sizeof(b));
514 b.tag = 1;
515 return b;
516}
517
518static BMK_benchOutcome_t BMK_benchOutcome_setValidResult(BMK_benchResult_t result)
519{
520 BMK_benchOutcome_t b;
521 b.tag = 0;
522 b.internal_never_use_directly = result;
523 return b;
524}
525
526
527/* benchMem with no allocation */
528static BMK_benchOutcome_t BMK_benchMemAdvancedNoAlloc(
529 const void** srcPtrs, size_t* srcSizes,
530 void** cPtrs, size_t* cCapacities, size_t* cSizes,
531 void** resPtrs, size_t* resSizes,
532 void** resultBufferPtr, void* compressedBuffer,
533 size_t maxCompressedSize,
534 BMK_timedFnState_t* timeStateCompress,
535 BMK_timedFnState_t* timeStateDecompress,
536
537 const void* srcBuffer, size_t srcSize,
538 const size_t* fileSizes, unsigned nbFiles,
539 const int cLevel, const ZSTD_compressionParameters* comprParams,
540 const void* dictBuffer, size_t dictBufferSize,
541 ZSTD_CCtx* cctx, ZSTD_DCtx* dctx,
542 int displayLevel, const char* displayName,
543 const BMK_advancedParams_t* adv)
Yann Collet1c00dc32015-10-21 08:22:25 +0100544{
Yann Collet4da5bdf2018-08-23 18:04:50 -0700545 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 -0700546 BMK_benchResult_t benchResult;
Yann Collete63c6312016-12-06 17:46:49 -0800547 size_t const loadedCompressedSize = srcSize;
548 size_t cSize = 0;
549 double ratio = 0.;
Yann Colletde406ee2016-03-20 15:46:10 +0100550 U32 nbBlocks;
551
Yann Collet2e45bad2018-08-23 14:21:18 -0700552 assert(cctx != NULL); assert(dctx != NULL);
George Lu01d940b2018-06-11 10:59:05 -0400553
Yann Colletc776c462015-10-29 19:10:54 +0100554 /* init */
Yann Collet4da5bdf2018-08-23 18:04:50 -0700555 memset(&benchResult, 0, sizeof(benchResult));
Yann Collet2e45bad2018-08-23 14:21:18 -0700556 if (strlen(displayName)>17) displayName += strlen(displayName) - 17; /* display last 17 characters */
George Lu85223462018-06-14 14:46:17 -0400557 if (adv->mode == BMK_decodeOnly) { /* benchmark only decompression : source must be already compressed */
Yann Colletc2007382017-04-04 15:35:06 -0700558 const char* srcPtr = (const char*)srcBuffer;
559 U64 totalDSize64 = 0;
Yann Collete63c6312016-12-06 17:46:49 -0800560 U32 fileNb;
561 for (fileNb=0; fileNb<nbFiles; fileNb++) {
Sean Purcell4e709712017-02-07 13:50:09 -0800562 U64 const fSize64 = ZSTD_findDecompressedSize(srcPtr, fileSizes[fileNb]);
Yann Collet2e45bad2018-08-23 14:21:18 -0700563 if (fSize64==0) RETURN_ERROR(32, BMK_benchOutcome_t, "Impossible to determine original size ");
Yann Colletc2007382017-04-04 15:35:06 -0700564 totalDSize64 += fSize64;
Yann Collete63c6312016-12-06 17:46:49 -0800565 srcPtr += fileSizes[fileNb];
566 }
Yann Colletc2007382017-04-04 15:35:06 -0700567 { size_t const decodedSize = (size_t)totalDSize64;
Yann Collet2e45bad2018-08-23 14:21:18 -0700568 assert((U64)decodedSize == totalDSize64); /* check overflow */
George Lu5f490342018-06-18 11:59:45 -0700569 free(*resultBufferPtr);
George Lu5f490342018-06-18 11:59:45 -0700570 *resultBufferPtr = malloc(decodedSize);
571 if (!(*resultBufferPtr)) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700572 RETURN_ERROR(33, BMK_benchOutcome_t, "not enough memory");
George Lua8eea992018-06-19 10:58:22 -0700573 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700574 if (totalDSize64 > decodedSize) { /* size_t overflow */
Yann Collet77e805e2018-08-21 18:19:27 -0700575 free(*resultBufferPtr);
Yann Collet2e45bad2018-08-23 14:21:18 -0700576 RETURN_ERROR(32, BMK_benchOutcome_t, "original size is too large");
George Lua8eea992018-06-19 10:58:22 -0700577 }
Yann Collete63c6312016-12-06 17:46:49 -0800578 cSize = srcSize;
579 srcSize = decodedSize;
580 ratio = (double)srcSize / (double)cSize;
Yann Collet77e805e2018-08-21 18:19:27 -0700581 }
George Lu20f4f322018-06-12 15:54:43 -0400582 }
Yann Collete63c6312016-12-06 17:46:49 -0800583
George Lu20f4f322018-06-12 15:54:43 -0400584 /* Init data blocks */
Yann Collete63c6312016-12-06 17:46:49 -0800585 { const char* srcPtr = (const char*)srcBuffer;
Yann Collet1c00dc32015-10-21 08:22:25 +0100586 char* cPtr = (char*)compressedBuffer;
George Lu5f490342018-06-18 11:59:45 -0700587 char* resPtr = (char*)(*resultBufferPtr);
Yann Collet699b14d2016-03-17 19:37:33 +0100588 U32 fileNb;
Yann Colletde406ee2016-03-20 15:46:10 +0100589 for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
Yann Collet70611352015-12-16 03:01:03 +0100590 size_t remaining = fileSizes[fileNb];
George Lu85223462018-06-14 14:46:17 -0400591 U32 const nbBlocksforThisFile = (adv->mode == BMK_decodeOnly) ? 1 : (U32)((remaining + (blockSize-1)) / blockSize);
Yann Collet699b14d2016-03-17 19:37:33 +0100592 U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
Yann Colletfd416f12016-01-30 03:14:15 +0100593 for ( ; nbBlocks<blockEnd; nbBlocks++) {
Yann Colletde406ee2016-03-20 15:46:10 +0100594 size_t const thisBlockSize = MIN(remaining, blockSize);
Yann Collet2e45bad2018-08-23 14:21:18 -0700595 srcPtrs[nbBlocks] = srcPtr;
George Lu20f4f322018-06-12 15:54:43 -0400596 srcSizes[nbBlocks] = thisBlockSize;
Yann Collet2e45bad2018-08-23 14:21:18 -0700597 cPtrs[nbBlocks] = cPtr;
George Lue148db32018-07-20 14:35:09 -0700598 cCapacities[nbBlocks] = (adv->mode == BMK_decodeOnly) ? thisBlockSize : ZSTD_compressBound(thisBlockSize);
Yann Collet2e45bad2018-08-23 14:21:18 -0700599 resPtrs[nbBlocks] = resPtr;
George Lu85223462018-06-14 14:46:17 -0400600 resSizes[nbBlocks] = (adv->mode == BMK_decodeOnly) ? (size_t) ZSTD_findDecompressedSize(srcPtr, thisBlockSize) : thisBlockSize;
Yann Colleted699e62015-12-16 02:37:24 +0100601 srcPtr += thisBlockSize;
George Lue148db32018-07-20 14:35:09 -0700602 cPtr += cCapacities[nbBlocks];
Yann Colleted699e62015-12-16 02:37:24 +0100603 resPtr += thisBlockSize;
604 remaining -= 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 Collet77e805e2018-08-21 18:19:27 -0700639
Yann Collet2e45bad2018-08-23 14:21:18 -0700640 if (!compressionCompleted) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700641 BMK_timedFnOutcome_t const cOutcome =
Yann Collet4da5bdf2018-08-23 18:04:50 -0700642 BMK_benchFunctionTimed( timeStateCompress,
643 &local_defaultCompress, cctx,
644 &local_initCCtx, &cctxprep,
645 nbBlocks,
646 srcPtrs, srcSizes,
647 cPtrs, cCapacities,
648 cSizes);
Yann Collet2e45bad2018-08-23 14:21:18 -0700649
650 if (!BMK_isSuccessful_timedFnOutcome(cOutcome)) {
651 return BMK_benchOutcome_error();
George Lu50d612f2018-06-25 15:01:03 -0700652 }
653
Yann Collet4da5bdf2018-08-23 18:04:50 -0700654 { BMK_runTime_t const cResult = BMK_extract_timedFnResult(cOutcome);
655 cSize = cResult.sumOfReturn;
656 ratio = (double)srcSize / cSize;
657 { BMK_benchResult_t newResult;
658 newResult.cSpeed = ((U64)srcSize * TIMELOOP_NANOSEC / cResult.nanoSecPerRun);
659 benchResult.cSize = cSize;
660 if (newResult.cSpeed > benchResult.cSpeed)
661 benchResult.cSpeed = newResult.cSpeed;
662 } }
George Lu5f490342018-06-18 11:59:45 -0700663
Yann Collet2e45bad2018-08-23 14:21:18 -0700664 { int const ratioAccuracy = (ratio < 10.) ? 3 : 2;
Yann Collet2e45bad2018-08-23 14:21:18 -0700665 markNb = (markNb+1) % NB_MARKS;
666 DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.*f),%6.*f MB/s\r",
Yann Collet4da5bdf2018-08-23 18:04:50 -0700667 marks[markNb], displayName,
668 (U32)srcSize, (U32)cSize,
Yann Collet2e45bad2018-08-23 14:21:18 -0700669 ratioAccuracy, ratio,
Yann Collet1f9ec132018-08-23 16:03:30 -0700670 benchResult.cSpeed < (10 MB) ? 2 : 1, (double)benchResult.cSpeed / MB_UNIT);
George Lua8eea992018-06-19 10:58:22 -0700671 }
Yann Collet4da5bdf2018-08-23 18:04:50 -0700672 compressionCompleted = BMK_isCompleted_timedFnOutcome(cOutcome);
Yann Collet2e45bad2018-08-23 14:21:18 -0700673 }
George Lue89f1fb2018-08-14 14:44:47 -0700674
Yann Collet2e45bad2018-08-23 14:21:18 -0700675 if(!decompressionCompleted) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700676 BMK_timedFnOutcome_t const dOutcome =
Yann Collet4da5bdf2018-08-23 18:04:50 -0700677 BMK_benchFunctionTimed(timeStateDecompress,
678 &local_defaultDecompress, dctx,
679 &local_initDCtx, &dctxprep,
680 nbBlocks,
681 (const void *const *)cPtrs, cSizes,
682 resPtrs, resSizes,
683 NULL);
Yann Collet2e45bad2018-08-23 14:21:18 -0700684
685 if(!BMK_isSuccessful_timedFnOutcome(dOutcome)) {
686 return BMK_benchOutcome_error();
687 }
688
Yann Collet4da5bdf2018-08-23 18:04:50 -0700689 { BMK_runTime_t const dResult = BMK_extract_timedFnResult(dOutcome);
690 U64 const newDSpeed = (srcSize * TIMELOOP_NANOSEC / dResult.nanoSecPerRun);
691 if (newDSpeed > benchResult.dSpeed)
692 benchResult.dSpeed = newDSpeed;
693 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700694
695 { int const ratioAccuracy = (ratio < 10.) ? 3 : 2;
Yann Collet2e45bad2018-08-23 14:21:18 -0700696 markNb = (markNb+1) % NB_MARKS;
697 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 -0700698 marks[markNb], displayName,
699 (U32)srcSize, (U32)benchResult.cSize,
Yann Collet2e45bad2018-08-23 14:21:18 -0700700 ratioAccuracy, ratio,
Yann Collet1f9ec132018-08-23 16:03:30 -0700701 benchResult.cSpeed < (10 MB) ? 2 : 1, (double)benchResult.cSpeed / MB_UNIT,
Yann Collet4da5bdf2018-08-23 18:04:50 -0700702 (double)benchResult.dSpeed / MB_UNIT);
George Lua8eea992018-06-19 10:58:22 -0700703 }
Yann Collet4da5bdf2018-08-23 18:04:50 -0700704 decompressionCompleted = BMK_isCompleted_timedFnOutcome(dOutcome);
Yann Colletdaebc7f2017-11-18 15:54:32 -0800705 }
Yann Collet4da5bdf2018-08-23 18:04:50 -0700706 } /* while (!(compressionCompleted && decompressionCompleted)) */
George Lu20f4f322018-06-12 15:54:43 -0400707
708 /* CRC Checking */
Yann Collet2e45bad2018-08-23 14:21:18 -0700709 { const BYTE* resultBuffer = (const BYTE*)(*resultBufferPtr);
George Lu5f490342018-06-18 11:59:45 -0700710 U64 const crcCheck = XXH64(resultBuffer, srcSize, 0);
George Lu85223462018-06-14 14:46:17 -0400711 if ((adv->mode == BMK_both) && (crcOrig!=crcCheck)) {
George Lu20f4f322018-06-12 15:54:43 -0400712 size_t u;
713 DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck);
714 for (u=0; u<srcSize; u++) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700715 if (((const BYTE*)srcBuffer)[u] != resultBuffer[u]) {
George Lu20f4f322018-06-12 15:54:43 -0400716 U32 segNb, bNb, pos;
717 size_t bacc = 0;
718 DISPLAY("Decoding error at pos %u ", (U32)u);
719 for (segNb = 0; segNb < nbBlocks; segNb++) {
720 if (bacc + srcSizes[segNb] > u) break;
721 bacc += srcSizes[segNb];
722 }
723 pos = (U32)(u - bacc);
724 bNb = pos / (128 KB);
725 DISPLAY("(sample %u, block %u, pos %u) \n", segNb, bNb, pos);
726 if (u>5) {
727 int n;
728 DISPLAY("origin: ");
729 for (n=-5; n<0; n++) DISPLAY("%02X ", ((const BYTE*)srcBuffer)[u+n]);
730 DISPLAY(" :%02X: ", ((const BYTE*)srcBuffer)[u]);
731 for (n=1; n<3; n++) DISPLAY("%02X ", ((const BYTE*)srcBuffer)[u+n]);
732 DISPLAY(" \n");
733 DISPLAY("decode: ");
Yann Collet2e45bad2018-08-23 14:21:18 -0700734 for (n=-5; n<0; n++) DISPLAY("%02X ", resultBuffer[u+n]);
735 DISPLAY(" :%02X: ", resultBuffer[u]);
736 for (n=1; n<3; n++) DISPLAY("%02X ", resultBuffer[u+n]);
George Lu20f4f322018-06-12 15:54:43 -0400737 DISPLAY(" \n");
738 }
739 break;
740 }
741 if (u==srcSize-1) { /* should never happen */
742 DISPLAY("no difference detected\n");
Yann Collet77e805e2018-08-21 18:19:27 -0700743 }
George Lu20f4f322018-06-12 15:54:43 -0400744 }
Yann Collet77e805e2018-08-21 18:19:27 -0700745 }
George Lu20f4f322018-06-12 15:54:43 -0400746 } /* CRC Checking */
747
Yann Collet2e45bad2018-08-23 14:21:18 -0700748 if (displayLevel == 1) { /* hidden display mode -q, used by python speed benchmark */
Yann Collet1f9ec132018-08-23 16:03:30 -0700749 double const cSpeed = (double)benchResult.cSpeed / MB_UNIT;
750 double const dSpeed = (double)benchResult.dSpeed / MB_UNIT;
Yann Collet2e45bad2018-08-23 14:21:18 -0700751 if (adv->additionalParam) {
752 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);
753 } else {
754 DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName);
755 }
George Luab26f242018-06-21 11:16:53 -0700756 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700757
758 DISPLAYLEVEL(2, "%2i#\n", cLevel);
George Lu85223462018-06-14 14:46:17 -0400759 } /* Bench */
Yann Collet2e45bad2018-08-23 14:21:18 -0700760
761 benchResult.cMem = (1ULL << (comprParams->windowLog)) + ZSTD_sizeof_CCtx(cctx);
762 return BMK_benchOutcome_setValidResult(benchResult);
George Lua8eea992018-06-19 10:58:22 -0700763}
Yann Collet4856a002015-01-24 01:58:16 +0100764
Yann Collet2e45bad2018-08-23 14:21:18 -0700765BMK_benchOutcome_t BMK_benchMemAdvanced(const void* srcBuffer, size_t srcSize,
Yann Collet77e805e2018-08-21 18:19:27 -0700766 void* dstBuffer, size_t dstCapacity,
George Lua8eea992018-06-19 10:58:22 -0700767 const size_t* fileSizes, unsigned nbFiles,
Yann Collet6ce7b082018-08-24 15:59:57 -0700768 int cLevel, const ZSTD_compressionParameters* comprParams,
George Lua8eea992018-06-19 10:58:22 -0700769 const void* dictBuffer, size_t dictBufferSize,
George Lua8eea992018-06-19 10:58:22 -0700770 int displayLevel, const char* displayName, const BMK_advancedParams_t* adv)
771
772{
Yann Collet2e45bad2018-08-23 14:21:18 -0700773 int const dstParamsError = !dstBuffer ^ !dstCapacity; /* must be both NULL or none */
774
George Lua8eea992018-06-19 10:58:22 -0700775 size_t const blockSize = ((adv->blockSize>=32 && (adv->mode != BMK_decodeOnly)) ? adv->blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ;
776 U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
777
778 /* these are the blockTable parameters, just split up */
George Ludd270b22018-07-27 08:49:25 -0700779 const void ** const srcPtrs = (const void**)malloc(maxNbBlocks * sizeof(void*));
780 size_t* const srcSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
George Lua8eea992018-06-19 10:58:22 -0700781
George Lue148db32018-07-20 14:35:09 -0700782
George Ludd270b22018-07-27 08:49:25 -0700783 void ** const cPtrs = (void**)malloc(maxNbBlocks * sizeof(void*));
784 size_t* const cSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
785 size_t* const cCapacities = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
George Lua8eea992018-06-19 10:58:22 -0700786
George Ludd270b22018-07-27 08:49:25 -0700787 void ** const resPtrs = (void**)malloc(maxNbBlocks * sizeof(void*));
788 size_t* const resSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
George Lua8eea992018-06-19 10:58:22 -0700789
Yann Collet77e805e2018-08-21 18:19:27 -0700790 BMK_timedFnState_t* timeStateCompress = BMK_createTimedFnState(adv->nbSeconds);
791 BMK_timedFnState_t* timeStateDecompress = BMK_createTimedFnState(adv->nbSeconds);
George Lud6121ad2018-06-22 17:25:16 -0700792
Yann Collet2e45bad2018-08-23 14:21:18 -0700793 ZSTD_CCtx* const cctx = ZSTD_createCCtx();
794 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
George Lubfe83922018-08-09 12:07:57 -0700795
George Lu5f490342018-06-18 11:59:45 -0700796 const size_t maxCompressedSize = dstCapacity ? dstCapacity : ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024);
George Ludd270b22018-07-27 08:49:25 -0700797
798 void* const internalDstBuffer = dstBuffer ? NULL : malloc(maxCompressedSize);
799 void* const compressedBuffer = dstBuffer ? dstBuffer : internalDstBuffer;
800
Yann Collet2e45bad2018-08-23 14:21:18 -0700801 BMK_benchOutcome_t outcome = BMK_benchOutcome_error(); /* error by default */
George Lue89f1fb2018-08-14 14:44:47 -0700802
803 void* resultBuffer = srcSize ? malloc(srcSize) : NULL;
804
Yann Collet77e805e2018-08-21 18:19:27 -0700805 int allocationincomplete = !srcPtrs || !srcSizes || !cPtrs ||
806 !cSizes || !cCapacities || !resPtrs || !resSizes ||
Yann Collet2e45bad2018-08-23 14:21:18 -0700807 !timeStateCompress || !timeStateDecompress ||
808 !cctx || !dctx ||
809 !compressedBuffer || !resultBuffer;
George Lu5f490342018-06-18 11:59:45 -0700810
George Lu5f490342018-06-18 11:59:45 -0700811
Yann Collet2e45bad2018-08-23 14:21:18 -0700812 if (!allocationincomplete && !dstParamsError) {
813 outcome = BMK_benchMemAdvancedNoAlloc(srcPtrs, srcSizes,
814 cPtrs, cCapacities, cSizes,
815 resPtrs, resSizes,
816 &resultBuffer,
817 compressedBuffer, maxCompressedSize,
818 timeStateCompress, timeStateDecompress,
819 srcBuffer, srcSize,
820 fileSizes, nbFiles,
821 cLevel, comprParams,
822 dictBuffer, dictBufferSize,
823 cctx, dctx,
824 displayLevel, displayName, adv);
George Lua8eea992018-06-19 10:58:22 -0700825 }
Yann Collet77e805e2018-08-21 18:19:27 -0700826
Yann Colleted699e62015-12-16 02:37:24 +0100827 /* clean up */
Yann Collet77e805e2018-08-21 18:19:27 -0700828 BMK_freeTimedFnState(timeStateCompress);
829 BMK_freeTimedFnState(timeStateDecompress);
830
Yann Collet2e45bad2018-08-23 14:21:18 -0700831 ZSTD_freeCCtx(cctx);
George Lubfe83922018-08-09 12:07:57 -0700832 ZSTD_freeDCtx(dctx);
833
George Ludd270b22018-07-27 08:49:25 -0700834 free(internalDstBuffer);
Yann Collet4856a002015-01-24 01:58:16 +0100835 free(resultBuffer);
George Lu20f4f322018-06-12 15:54:43 -0400836
Yann Collet77e805e2018-08-21 18:19:27 -0700837 free((void*)srcPtrs);
838 free(srcSizes);
839 free(cPtrs);
George Lu20f4f322018-06-12 15:54:43 -0400840 free(cSizes);
George Lue148db32018-07-20 14:35:09 -0700841 free(cCapacities);
George Lu20f4f322018-06-12 15:54:43 -0400842 free(resPtrs);
843 free(resSizes);
844
George Lua8eea992018-06-19 10:58:22 -0700845 if(allocationincomplete) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700846 RETURN_ERROR(31, BMK_benchOutcome_t, "allocation error : not enough memory");
George Lua8eea992018-06-19 10:58:22 -0700847 }
Yann Collet77e805e2018-08-21 18:19:27 -0700848
Yann Collet2e45bad2018-08-23 14:21:18 -0700849 if(dstParamsError) {
850 RETURN_ERROR(32, BMK_benchOutcome_t, "Dst parameters not coherent");
George Ludd270b22018-07-27 08:49:25 -0700851 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700852 return outcome;
Yann Collet4856a002015-01-24 01:58:16 +0100853}
854
Yann Collet2e45bad2018-08-23 14:21:18 -0700855BMK_benchOutcome_t BMK_benchMem(const void* srcBuffer, size_t srcSize,
George Lu20f4f322018-06-12 15:54:43 -0400856 const size_t* fileSizes, unsigned nbFiles,
Yann Collet2e45bad2018-08-23 14:21:18 -0700857 int cLevel, const ZSTD_compressionParameters* comprParams,
George Lu20f4f322018-06-12 15:54:43 -0400858 const void* dictBuffer, size_t dictBufferSize,
George Lu20f4f322018-06-12 15:54:43 -0400859 int displayLevel, const char* displayName) {
860
Yann Collet2e45bad2018-08-23 14:21:18 -0700861 BMK_advancedParams_t const adv = BMK_initAdvancedParams();
George Lu20f4f322018-06-12 15:54:43 -0400862 return BMK_benchMemAdvanced(srcBuffer, srcSize,
George Lu5f490342018-06-18 11:59:45 -0700863 NULL, 0,
George Lu20f4f322018-06-12 15:54:43 -0400864 fileSizes, nbFiles,
865 cLevel, comprParams,
866 dictBuffer, dictBufferSize,
George Lu20f4f322018-06-12 15:54:43 -0400867 displayLevel, displayName, &adv);
868}
869
Yann Collet2e45bad2018-08-23 14:21:18 -0700870static BMK_benchOutcome_t BMK_benchCLevel(const void* srcBuffer, size_t benchedSize,
871 const size_t* fileSizes, unsigned nbFiles,
872 int cLevel, const ZSTD_compressionParameters* comprParams,
873 const void* dictBuffer, size_t dictBufferSize,
874 int displayLevel, const char* displayName,
875 BMK_advancedParams_t const * const adv)
876{
877 const char* pch = strrchr(displayName, '\\'); /* Windows */
878 if (!pch) pch = strrchr(displayName, '/'); /* Linux */
879 if (pch) displayName = pch+1;
880
881 if (adv->realTime) {
882 DISPLAYLEVEL(2, "Note : switching to real-time priority \n");
883 SET_REALTIME_PRIORITY;
884 }
885
886 if (displayLevel == 1 && !adv->additionalParam) /* --quiet mode */
887 DISPLAY("bench %s %s: input %u bytes, %u seconds, %u KB blocks\n",
888 ZSTD_VERSION_STRING, ZSTD_GIT_COMMIT_STRING,
889 (U32)benchedSize, adv->nbSeconds, (U32)(adv->blockSize>>10));
890
891 return BMK_benchMemAdvanced(srcBuffer, benchedSize,
892 NULL, 0,
893 fileSizes, nbFiles,
894 cLevel, comprParams,
895 dictBuffer, dictBufferSize,
896 displayLevel, displayName, adv);
897}
898
899BMK_benchOutcome_t BMK_syntheticTest(int cLevel, double compressibility,
900 const ZSTD_compressionParameters* compressionParams,
901 int displayLevel, const BMK_advancedParams_t* adv)
902{
903 char name[20] = {0};
904 size_t const benchedSize = 10000000;
905 void* srcBuffer;
906 BMK_benchOutcome_t res;
907
908 if (cLevel > ZSTD_maxCLevel()) {
909 RETURN_ERROR(15, BMK_benchOutcome_t, "Invalid Compression Level");
910 }
911
912 /* Memory allocation */
913 srcBuffer = malloc(benchedSize);
914 if (!srcBuffer) RETURN_ERROR(21, BMK_benchOutcome_t, "not enough memory");
915
916 /* Fill input buffer */
917 RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
918
919 /* Bench */
920 snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
921 res = BMK_benchCLevel(srcBuffer, benchedSize,
922 &benchedSize /* ? */, 1 /* ? */,
923 cLevel, compressionParams,
924 NULL, 0, /* dictionary */
925 displayLevel, name, adv);
926
927 /* clean up */
928 free(srcBuffer);
929
930 return res;
931}
932
933
934
Yann Collet4856a002015-01-24 01:58:16 +0100935static size_t BMK_findMaxMem(U64 requiredMem)
936{
Yann Colletde406ee2016-03-20 15:46:10 +0100937 size_t const step = 64 MB;
Yann Collet4856a002015-01-24 01:58:16 +0100938 BYTE* testmem = NULL;
939
940 requiredMem = (((requiredMem >> 26) + 1) << 26);
Yann Colletde406ee2016-03-20 15:46:10 +0100941 requiredMem += step;
Yann Collet050efba2015-11-03 09:49:30 +0100942 if (requiredMem > maxMemory) requiredMem = maxMemory;
Yann Collet4856a002015-01-24 01:58:16 +0100943
Yann Colletde406ee2016-03-20 15:46:10 +0100944 do {
Yann Collet4856a002015-01-24 01:58:16 +0100945 testmem = (BYTE*)malloc((size_t)requiredMem);
Yann Colletde406ee2016-03-20 15:46:10 +0100946 requiredMem -= step;
George Lue89f1fb2018-08-14 14:44:47 -0700947 } while (!testmem && requiredMem > 0);
Yann Colletde406ee2016-03-20 15:46:10 +0100948
Yann Collet4856a002015-01-24 01:58:16 +0100949 free(testmem);
Yann Colletde406ee2016-03-20 15:46:10 +0100950 return (size_t)(requiredMem);
Yann Collet4856a002015-01-24 01:58:16 +0100951}
952
Yann Colleta5b66e32016-03-26 01:48:27 +0100953/*! BMK_loadFiles() :
Yann Collet6a9b41b2018-03-11 19:56:48 -0700954 * Loads `buffer` with content of files listed within `fileNamesTable`.
955 * At most, fills `buffer` entirely. */
George Lu20f4f322018-06-12 15:54:43 -0400956static int BMK_loadFiles(void* buffer, size_t bufferSize,
Yann Collet77e805e2018-08-21 18:19:27 -0700957 size_t* fileSizes, const char* const * const fileNamesTable,
George Lu01d940b2018-06-11 10:59:05 -0400958 unsigned nbFiles, 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 */
979 { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
George Lu20f4f322018-06-12 15:54:43 -0400980 if (readSize != (size_t)fileSize) EXM_THROW_INT(11, "could not read %s", fileNamesTable[n]);
Yann Colleta5b66e32016-03-26 01:48:27 +0100981 pos += readSize; }
Yann Colleta52c98d2015-12-16 03:12:31 +0100982 fileSizes[n] = (size_t)fileSize;
inikepc0d5f4e2016-04-13 10:48:04 +0200983 totalSize += (size_t)fileSize;
Yann Colleted699e62015-12-16 02:37:24 +0100984 fclose(f);
985 }
Yann Collet6f9c0562016-05-01 10:26:30 +0200986
George Lu20f4f322018-06-12 15:54:43 -0400987 if (totalSize == 0) EXM_THROW_INT(12, "no data to bench");
988 return 0;
Yann Colleted699e62015-12-16 02:37:24 +0100989}
990
Yann Collet2e45bad2018-08-23 14:21:18 -0700991BMK_benchOutcome_t BMK_benchFilesAdvanced(
992 const char* const * fileNamesTable, unsigned nbFiles,
993 const char* dictFileName, int cLevel,
994 const ZSTD_compressionParameters* compressionParams,
995 int displayLevel, const BMK_advancedParams_t* adv)
Yann Colleted699e62015-12-16 02:37:24 +0100996{
George Lud6121ad2018-06-22 17:25:16 -0700997 void* srcBuffer = NULL;
Yann Colleted699e62015-12-16 02:37:24 +0100998 size_t benchedSize;
Yann Collet31683c02015-12-18 01:26:48 +0100999 void* dictBuffer = NULL;
1000 size_t dictBufferSize = 0;
George Lud6121ad2018-06-22 17:25:16 -07001001 size_t* fileSizes = NULL;
Yann Collet2e45bad2018-08-23 14:21:18 -07001002 BMK_benchOutcome_t res = BMK_benchOutcome_error(); /* error by default */
Yann Collete162ace2016-05-20 11:24:35 +02001003 U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +01001004
Yann Collet2e45bad2018-08-23 14:21:18 -07001005 if (!nbFiles) {
1006 RETURN_ERROR(14, BMK_benchOutcome_t, "No Files to Benchmark");
George Lua8eea992018-06-19 10:58:22 -07001007 }
Yann Collet31683c02015-12-18 01:26:48 +01001008
George Lua8eea992018-06-19 10:58:22 -07001009 if (cLevel > ZSTD_maxCLevel()) {
Yann Collet2e45bad2018-08-23 14:21:18 -07001010 RETURN_ERROR(15, BMK_benchOutcome_t, "Invalid Compression Level");
George Lua8eea992018-06-19 10:58:22 -07001011 }
1012
1013 fileSizes = (size_t*)calloc(nbFiles, sizeof(size_t));
Yann Collet2e45bad2018-08-23 14:21:18 -07001014 if (!fileSizes) RETURN_ERROR(12, BMK_benchOutcome_t, "not enough memory for fileSizes");
1015
Yann Collet31683c02015-12-18 01:26:48 +01001016 /* Load dictionary */
Yann Colletfd416f12016-01-30 03:14:15 +01001017 if (dictFileName != NULL) {
Yann Collet18b79532017-10-17 16:14:25 -07001018 U64 const dictFileSize = UTIL_getFileSize(dictFileName);
George Lua8eea992018-06-19 10:58:22 -07001019 if (dictFileSize > 64 MB) {
1020 free(fileSizes);
Yann Collet2e45bad2018-08-23 14:21:18 -07001021 RETURN_ERROR(10, BMK_benchOutcome_t, "dictionary file %s too large", dictFileName);
George Lua8eea992018-06-19 10:58:22 -07001022 }
Yann Collet31683c02015-12-18 01:26:48 +01001023 dictBufferSize = (size_t)dictFileSize;
1024 dictBuffer = malloc(dictBufferSize);
George Lua8eea992018-06-19 10:58:22 -07001025 if (dictBuffer==NULL) {
1026 free(fileSizes);
Yann Collet2e45bad2018-08-23 14:21:18 -07001027 RETURN_ERROR(11, BMK_benchOutcome_t, "not enough memory for dictionary (%u bytes)",
Yann Collet18b79532017-10-17 16:14:25 -07001028 (U32)dictBufferSize);
George Lua8eea992018-06-19 10:58:22 -07001029 }
Yann Collet2e45bad2018-08-23 14:21:18 -07001030
1031 { int const errorCode = BMK_loadFiles(dictBuffer, dictBufferSize,
1032 fileSizes, &dictFileName /*?*/,
1033 1 /*?*/, displayLevel);
1034 if (errorCode) {
1035 res = BMK_benchOutcome_error();
George Lud6121ad2018-06-22 17:25:16 -07001036 goto _cleanUp;
Yann Collet2e45bad2018-08-23 14:21:18 -07001037 } }
Yann Collet31683c02015-12-18 01:26:48 +01001038 }
1039
Yann Colleted699e62015-12-16 02:37:24 +01001040 /* Memory allocation & restrictions */
1041 benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
1042 if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
1043 if (benchedSize < totalSizeToLoad)
1044 DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20));
George Lue89f1fb2018-08-14 14:44:47 -07001045
1046 srcBuffer = benchedSize ? malloc(benchedSize) : NULL;
George Lua8eea992018-06-19 10:58:22 -07001047 if (!srcBuffer) {
1048 free(dictBuffer);
1049 free(fileSizes);
Yann Collet2e45bad2018-08-23 14:21:18 -07001050 RETURN_ERROR(12, BMK_benchOutcome_t, "not enough memory");
George Lua8eea992018-06-19 10:58:22 -07001051 }
Yann Colleted699e62015-12-16 02:37:24 +01001052
1053 /* Load input buffer */
Yann Collet2e45bad2018-08-23 14:21:18 -07001054 { int const errorCode = BMK_loadFiles(srcBuffer, benchedSize,
1055 fileSizes, fileNamesTable, nbFiles,
1056 displayLevel);
1057 if (errorCode) {
1058 res = BMK_benchOutcome_error();
George Lud6121ad2018-06-22 17:25:16 -07001059 goto _cleanUp;
Yann Collet2e45bad2018-08-23 14:21:18 -07001060 } }
1061
Yann Colleted699e62015-12-16 02:37:24 +01001062 /* Bench */
Yann Collet2e45bad2018-08-23 14:21:18 -07001063 { char mfName[20] = {0};
Yann Colletd898fb72017-11-17 00:22:55 -08001064 snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
Yann Collet2e45bad2018-08-23 14:21:18 -07001065 { const char* const displayName = (nbFiles > 1) ? mfName : fileNamesTable[0];
Yann Collet77e805e2018-08-21 18:19:27 -07001066 res = BMK_benchCLevel(srcBuffer, benchedSize,
Yann Collet2e45bad2018-08-23 14:21:18 -07001067 fileSizes, nbFiles,
1068 cLevel, compressionParams,
1069 dictBuffer, dictBufferSize,
1070 displayLevel, displayName,
1071 adv);
Yann Colletd898fb72017-11-17 00:22:55 -08001072 } }
Yann Collet4856a002015-01-24 01:58:16 +01001073
George Lud6121ad2018-06-22 17:25:16 -07001074_cleanUp:
Yann Collet4856a002015-01-24 01:58:16 +01001075 free(srcBuffer);
Yann Collet31683c02015-12-18 01:26:48 +01001076 free(dictBuffer);
Yann Collet70611352015-12-16 03:01:03 +01001077 free(fileSizes);
George Lu20f4f322018-06-12 15:54:43 -04001078 return res;
Yann Collet4856a002015-01-24 01:58:16 +01001079}
1080
1081
Yann Collet2e45bad2018-08-23 14:21:18 -07001082BMK_benchOutcome_t BMK_benchFiles(
1083 const char* const * fileNamesTable, unsigned nbFiles,
1084 const char* dictFileName,
1085 int cLevel, const ZSTD_compressionParameters* compressionParams,
1086 int displayLevel)
Yann Collet4856a002015-01-24 01:58:16 +01001087{
Yann Collet2e45bad2018-08-23 14:21:18 -07001088 BMK_advancedParams_t const adv = BMK_initAdvancedParams();
George Lu0d1ee222018-06-15 16:21:08 -04001089 return BMK_benchFilesAdvanced(fileNamesTable, nbFiles, dictFileName, cLevel, compressionParams, displayLevel, &adv);
Yann Collet4856a002015-01-24 01:58:16 +01001090}