blob: 77056203d55eafbf29b93f5dea2db2622890bb28 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Nick Terrellac58c8d2020-03-26 15:19:05 -07002 * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
Yann Collet4ded9e52016-08-30 10:04:33 -07003 * 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/* **************************************
Yann Collet500014a2017-01-19 16:59:56 -080013* Tuning parameters
14****************************************/
15#ifndef BMK_TIMETEST_DEFAULT_S /* default minimum time per test */
16#define BMK_TIMETEST_DEFAULT_S 3
17#endif
18
19
Yann Colletf3eca252015-10-22 15:31:46 +010020/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010021* Includes
Yann Colletf3eca252015-10-22 15:31:46 +010022***************************************/
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +010023#include "platform.h" /* Large Files support */
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010024#include "util.h" /* UTIL_getFileSize, UTIL_sleep */
Yann Collet4856a002015-01-24 01:58:16 +010025#include <stdlib.h> /* malloc, free */
Yann Collet18434d72018-12-20 17:27:08 -080026#include <string.h> /* memset, strerror */
Yann Colletc6a64172016-12-31 03:31:26 +010027#include <stdio.h> /* fprintf, fopen */
Yann Colleted2fb6b2018-12-20 17:20:07 -080028#include <errno.h>
Yann Colletd3364aa2018-02-20 14:48:09 -080029#include <assert.h> /* assert */
inikep4c12f232016-03-29 14:52:13 +020030
Yann Collet59a71162019-04-10 12:37:03 -070031#include "timefn.h" /* UTIL_time_t */
Yann Colletd38063f2018-11-13 11:01:59 -080032#include "benchfn.h"
W. Felix Handte7dcca6b2020-05-01 16:20:40 -040033#include "../lib/common/mem.h"
Yann Colletd3b7f8d2016-06-04 19:47:02 +020034#define ZSTD_STATIC_LINKING_ONLY
W. Felix Handte7dcca6b2020-05-01 16:20:40 -040035#include "../lib/zstd.h"
inikep69fcd7c2016-04-28 12:23:33 +020036#include "datagen.h" /* RDG_genBuffer */
W. Felix Handte7dcca6b2020-05-01 16:20:40 -040037#include "../lib/common/xxhash.h"
Yann Colletd38063f2018-11-13 11:01:59 -080038#include "benchzstd.h"
W. Felix Handte7dcca6b2020-05-01 16:20:40 -040039#include "../lib/common/zstd_errors.h"
Yann Collet4856a002015-01-24 01:58:16 +010040
41
Yann Colletf3eca252015-10-22 15:31:46 +010042/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010043* Constants
Yann Colletf3eca252015-10-22 15:31:46 +010044***************************************/
inikepf2f59d72016-06-22 15:42:26 +020045#ifndef ZSTD_GIT_COMMIT
inikepd7d251c2016-06-22 16:13:25 +020046# define ZSTD_GIT_COMMIT_STRING ""
47#else
48# define ZSTD_GIT_COMMIT_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_GIT_COMMIT)
inikepf2f59d72016-06-22 15:42:26 +020049#endif
50
Yann Colletd3364aa2018-02-20 14:48:09 -080051#define TIMELOOP_MICROSEC (1*1000000ULL) /* 1 second */
52#define TIMELOOP_NANOSEC (1*1000000000ULL) /* 1 second */
53#define ACTIVEPERIOD_MICROSEC (70*TIMELOOP_MICROSEC) /* 70 seconds */
inikep83c76b42016-04-28 13:16:01 +020054#define COOLPERIOD_SEC 10
Yann Collet4856a002015-01-24 01:58:16 +010055
56#define KB *(1 <<10)
57#define MB *(1 <<20)
58#define GB *(1U<<30)
59
Yann Collet55affc02018-08-28 11:21:09 -070060#define BMK_RUNTEST_DEFAULT_MS 1000
61
Yann Collet2e45bad2018-08-23 14:21:18 -070062static const size_t maxMemory = (sizeof(size_t)==4) ?
63 /* 32-bit */ (2 GB - 64 MB) :
64 /* 64-bit */ (size_t)(1ULL << ((sizeof(size_t)*8)-31));
65
Yann Collet4856a002015-01-24 01:58:16 +010066
Yann Colletf3eca252015-10-22 15:31:46 +010067/* *************************************
Yann Colleted699e62015-12-16 02:37:24 +010068* console display
Yann Colletf3eca252015-10-22 15:31:46 +010069***************************************/
Yann Colleted699e62015-12-16 02:37:24 +010070#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
George Lu01d940b2018-06-11 10:59:05 -040071#define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); }
72/* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
Yann Colleted699e62015-12-16 02:37:24 +010073
Nick Terrell9a2f6f42017-11-29 19:11:12 -080074static const U64 g_refreshRate = SEC_TO_MICRO / 6;
75static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
76
George Lu01d940b2018-06-11 10:59:05 -040077#define DISPLAYUPDATE(l, ...) { if (displayLevel>=l) { \
78 if ((UTIL_clockSpanMicro(g_displayClock) > g_refreshRate) || (displayLevel>=4)) \
Nick Terrell9a2f6f42017-11-29 19:11:12 -080079 { g_displayClock = UTIL_getTime(); DISPLAY(__VA_ARGS__); \
George Lu01d940b2018-06-11 10:59:05 -040080 if (displayLevel>=4) fflush(stderr); } } }
Yann Colletbf2bc112016-08-02 23:48:13 +020081
Yann Colleted699e62015-12-16 02:37:24 +010082
83/* *************************************
84* Exceptions
85***************************************/
86#ifndef DEBUG
George Lu3adc2172018-07-12 17:30:39 -070087# define DEBUG 0
Yann Colleted699e62015-12-16 02:37:24 +010088#endif
Yann Colletfe234bf2017-06-19 15:23:19 -070089#define DEBUGOUTPUT(...) { if (DEBUG) DISPLAY(__VA_ARGS__); }
George Lu20f4f322018-06-12 15:54:43 -040090
Yann Collet6309be62019-10-15 16:09:18 -070091#define RETURN_ERROR_INT(errorNum, ...) { \
Yann Colletfe234bf2017-06-19 15:23:19 -070092 DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
George Lu20f4f322018-06-12 15:54:43 -040093 DISPLAYLEVEL(1, "Error %i : ", errorNum); \
George Lu01d940b2018-06-11 10:59:05 -040094 DISPLAYLEVEL(1, __VA_ARGS__); \
95 DISPLAYLEVEL(1, " \n"); \
George Lu20f4f322018-06-12 15:54:43 -040096 return errorNum; \
Yann Colleted699e62015-12-16 02:37:24 +010097}
Yann Collet4856a002015-01-24 01:58:16 +010098
Yann Colletd613fd92018-12-06 19:27:37 -080099#define CHECK_Z(zf) { \
100 size_t const zerr = zf; \
101 if (ZSTD_isError(zerr)) { \
102 DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
103 DISPLAY("Error : "); \
104 DISPLAY("%s failed : %s", \
105 #zf, ZSTD_getErrorName(zerr)); \
106 DISPLAY(" \n"); \
107 exit(1); \
108 } \
109}
110
Yann Collet2e45bad2018-08-23 14:21:18 -0700111#define RETURN_ERROR(errorNum, retType, ...) { \
George Lu20f4f322018-06-12 15:54:43 -0400112 retType r; \
113 memset(&r, 0, sizeof(retType)); \
114 DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
115 DISPLAYLEVEL(1, "Error %i : ", errorNum); \
116 DISPLAYLEVEL(1, __VA_ARGS__); \
117 DISPLAYLEVEL(1, " \n"); \
Yann Collet2e45bad2018-08-23 14:21:18 -0700118 r.tag = errorNum; \
George Lu20f4f322018-06-12 15:54:43 -0400119 return r; \
120}
Yann Collet4856a002015-01-24 01:58:16 +0100121
George Lu0d1ee222018-06-15 16:21:08 -0400122
Yann Colletf3eca252015-10-22 15:31:46 +0100123/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100124* Benchmark Parameters
Yann Colletf3eca252015-10-22 15:31:46 +0100125***************************************/
Stella Laua1f04d52017-09-01 14:52:51 -0700126
Yann Collet77e805e2018-08-21 18:19:27 -0700127BMK_advancedParams_t BMK_initAdvancedParams(void) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700128 BMK_advancedParams_t const res = {
George Lu85223462018-06-14 14:46:17 -0400129 BMK_both, /* mode */
George Lu20f4f322018-06-12 15:54:43 -0400130 BMK_TIMETEST_DEFAULT_S, /* nbSeconds */
131 0, /* blockSize */
132 0, /* nbWorkers */
133 0, /* realTime */
George Lu20f4f322018-06-12 15:54:43 -0400134 0, /* additionalParam */
Yann Collet77e805e2018-08-21 18:19:27 -0700135 0, /* ldmFlag */
George Lu20f4f322018-06-12 15:54:43 -0400136 0, /* ldmMinMatch */
137 0, /* ldmHashLog */
George Lud6121ad2018-06-22 17:25:16 -0700138 0, /* ldmBuckSizeLog */
Nick Terrell19ca3fb2019-02-15 15:24:55 -0800139 0, /* ldmHashRateLog */
140 ZSTD_lcm_auto /* literalCompressionMode */
George Lu20f4f322018-06-12 15:54:43 -0400141 };
142 return res;
Stella Lau67d4a612017-09-02 21:10:36 -0700143}
144
Yann Collet4856a002015-01-24 01:58:16 +0100145
Yann Colletf3eca252015-10-22 15:31:46 +0100146/* ********************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100147* Bench functions
Yann Colletf3eca252015-10-22 15:31:46 +0100148**********************************************************/
Yann Colletd9465012016-12-06 16:49:23 -0800149typedef struct {
150 const void* srcPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100151 size_t srcSize;
Yann Colletd9465012016-12-06 16:49:23 -0800152 void* cPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100153 size_t cRoom;
154 size_t cSize;
Yann Colletd9465012016-12-06 16:49:23 -0800155 void* resPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100156 size_t resSize;
157} blockParam_t;
158
Sean Purcell42bac7f2017-04-13 15:35:05 -0700159#undef MIN
160#undef MAX
161#define MIN(a,b) ((a) < (b) ? (a) : (b))
162#define MAX(a,b) ((a) > (b) ? (a) : (b))
Yann Collet1c00dc32015-10-21 08:22:25 +0100163
Yann Colletb8701102019-01-25 15:11:50 -0800164static void
165BMK_initCCtx(ZSTD_CCtx* ctx,
166 const void* dictBuffer, size_t dictBufferSize,
167 int cLevel,
168 const ZSTD_compressionParameters* comprParams,
169 const BMK_advancedParams_t* adv)
170{
Yann Collet5c686392018-11-15 16:12:39 -0800171 ZSTD_CCtx_reset(ctx, ZSTD_reset_session_and_parameters);
George Lu20f4f322018-06-12 15:54:43 -0400172 if (adv->nbWorkers==1) {
Yann Colletd613fd92018-12-06 19:27:37 -0800173 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_nbWorkers, 0));
George Lu20f4f322018-06-12 15:54:43 -0400174 } else {
Yann Colletd613fd92018-12-06 19:27:37 -0800175 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_nbWorkers, adv->nbWorkers));
George Lu20f4f322018-06-12 15:54:43 -0400176 }
Yann Colletd613fd92018-12-06 19:27:37 -0800177 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, cLevel));
178 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_enableLongDistanceMatching, adv->ldmFlag));
179 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmMinMatch, adv->ldmMinMatch));
180 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmHashLog, adv->ldmHashLog));
181 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmBucketSizeLog, adv->ldmBucketSizeLog));
182 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmHashRateLog, adv->ldmHashRateLog));
Yann Colletb8701102019-01-25 15:11:50 -0800183 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_windowLog, (int)comprParams->windowLog));
184 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_hashLog, (int)comprParams->hashLog));
185 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_chainLog, (int)comprParams->chainLog));
186 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_searchLog, (int)comprParams->searchLog));
187 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_minMatch, (int)comprParams->minMatch));
188 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_targetLength, (int)comprParams->targetLength));
Nick Terrell19ca3fb2019-02-15 15:24:55 -0800189 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_literalCompressionMode, (int)adv->literalCompressionMode));
Yann Colletd613fd92018-12-06 19:27:37 -0800190 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_strategy, comprParams->strategy));
191 CHECK_Z(ZSTD_CCtx_loadDictionary(ctx, dictBuffer, dictBufferSize));
George Lu20f4f322018-06-12 15:54:43 -0400192}
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) {
Yann Colletd613fd92018-12-06 19:27:37 -0800196 CHECK_Z(ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters));
197 CHECK_Z(ZSTD_DCtx_loadDictionary(dctx, dictBuffer, dictBufferSize));
George Lu20f4f322018-06-12 15:54:43 -0400198}
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{
Yann Collet2e45bad2018-08-23 14:21:18 -0700235 ZSTD_CCtx* const cctx = (ZSTD_CCtx*)addArgs;
Yann Colletd8e215c2018-11-30 11:16:26 -0800236 return ZSTD_compress2(cctx, dstBuffer, dstSize, srcBuffer, srcSize);
George Lu20f4f322018-06-12 15:54:43 -0400237}
238
Yann Collet2e45bad2018-08-23 14:21:18 -0700239/* `addArgs` is the context */
George Lu20f4f322018-06-12 15:54:43 -0400240static size_t local_defaultDecompress(
Yann Collet2e45bad2018-08-23 14:21:18 -0700241 const void* srcBuffer, size_t srcSize,
Yann Collet0c66a442018-08-28 15:47:07 -0700242 void* dstBuffer, size_t dstCapacity,
Yann Collet2e45bad2018-08-23 14:21:18 -0700243 void* addArgs)
244{
George Lu20f4f322018-06-12 15:54:43 -0400245 size_t moreToFlush = 1;
Yann Collet2e45bad2018-08-23 14:21:18 -0700246 ZSTD_DCtx* const dctx = (ZSTD_DCtx*)addArgs;
George Lu20f4f322018-06-12 15:54:43 -0400247 ZSTD_inBuffer in;
248 ZSTD_outBuffer out;
Yann Collet2e45bad2018-08-23 14:21:18 -0700249 in.src = srcBuffer; in.size = srcSize; in.pos = 0;
Yann Collet0c66a442018-08-28 15:47:07 -0700250 out.dst = dstBuffer; out.size = dstCapacity; out.pos = 0;
George Lu20f4f322018-06-12 15:54:43 -0400251 while (moreToFlush) {
George Lud6121ad2018-06-22 17:25:16 -0700252 if(out.pos == out.size) {
253 return (size_t)-ZSTD_error_dstSize_tooSmall;
254 }
Yann Collet34e146f2018-12-04 10:28:36 -0800255 moreToFlush = ZSTD_decompressStream(dctx, &out, &in);
George Lu20f4f322018-06-12 15:54:43 -0400256 if (ZSTD_isError(moreToFlush)) {
257 return moreToFlush;
258 }
259 }
260 return out.pos;
261
262}
263
Yann Collet2e45bad2018-08-23 14:21:18 -0700264
Yann Collet2e45bad2018-08-23 14:21:18 -0700265/* ================================================================= */
266/* Benchmark Zstandard, mem-to-mem scenarios */
267/* ================================================================= */
268
269int BMK_isSuccessful_benchOutcome(BMK_benchOutcome_t outcome)
270{
271 return outcome.tag == 0;
272}
273
274BMK_benchResult_t BMK_extract_benchResult(BMK_benchOutcome_t outcome)
275{
276 assert(outcome.tag == 0);
277 return outcome.internal_never_use_directly;
278}
279
Yann Collet6ce7b082018-08-24 15:59:57 -0700280static BMK_benchOutcome_t BMK_benchOutcome_error(void)
Yann Collet2e45bad2018-08-23 14:21:18 -0700281{
282 BMK_benchOutcome_t b;
283 memset(&b, 0, sizeof(b));
284 b.tag = 1;
285 return b;
286}
287
288static BMK_benchOutcome_t BMK_benchOutcome_setValidResult(BMK_benchResult_t result)
289{
290 BMK_benchOutcome_t b;
291 b.tag = 0;
292 b.internal_never_use_directly = result;
293 return b;
294}
295
296
297/* benchMem with no allocation */
Yann Collet8bed4012018-11-08 12:36:39 -0800298static BMK_benchOutcome_t
299BMK_benchMemAdvancedNoAlloc(
300 const void** srcPtrs, size_t* srcSizes,
301 void** cPtrs, size_t* cCapacities, size_t* cSizes,
302 void** resPtrs, size_t* resSizes,
303 void** resultBufferPtr, void* compressedBuffer,
304 size_t maxCompressedSize,
305 BMK_timedFnState_t* timeStateCompress,
306 BMK_timedFnState_t* timeStateDecompress,
Yann Collet2e45bad2018-08-23 14:21:18 -0700307
Yann Collet8bed4012018-11-08 12:36:39 -0800308 const void* srcBuffer, size_t srcSize,
309 const size_t* fileSizes, unsigned nbFiles,
310 const int cLevel,
311 const ZSTD_compressionParameters* comprParams,
312 const void* dictBuffer, size_t dictBufferSize,
313 ZSTD_CCtx* cctx, ZSTD_DCtx* dctx,
314 int displayLevel, const char* displayName,
315 const BMK_advancedParams_t* adv)
Yann Collet1c00dc32015-10-21 08:22:25 +0100316{
Yann Collet4da5bdf2018-08-23 18:04:50 -0700317 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 -0700318 BMK_benchResult_t benchResult;
Yann Collete63c6312016-12-06 17:46:49 -0800319 size_t const loadedCompressedSize = srcSize;
320 size_t cSize = 0;
321 double ratio = 0.;
Yann Colletde406ee2016-03-20 15:46:10 +0100322 U32 nbBlocks;
323
Yann Collet2e45bad2018-08-23 14:21:18 -0700324 assert(cctx != NULL); assert(dctx != NULL);
George Lu01d940b2018-06-11 10:59:05 -0400325
Yann Colletc776c462015-10-29 19:10:54 +0100326 /* init */
Yann Collet4da5bdf2018-08-23 18:04:50 -0700327 memset(&benchResult, 0, sizeof(benchResult));
Yann Collet2e45bad2018-08-23 14:21:18 -0700328 if (strlen(displayName)>17) displayName += strlen(displayName) - 17; /* display last 17 characters */
George Lu85223462018-06-14 14:46:17 -0400329 if (adv->mode == BMK_decodeOnly) { /* benchmark only decompression : source must be already compressed */
Yann Colletc2007382017-04-04 15:35:06 -0700330 const char* srcPtr = (const char*)srcBuffer;
331 U64 totalDSize64 = 0;
Yann Collete63c6312016-12-06 17:46:49 -0800332 U32 fileNb;
333 for (fileNb=0; fileNb<nbFiles; fileNb++) {
Sean Purcell4e709712017-02-07 13:50:09 -0800334 U64 const fSize64 = ZSTD_findDecompressedSize(srcPtr, fileSizes[fileNb]);
Yann Collet2e45bad2018-08-23 14:21:18 -0700335 if (fSize64==0) RETURN_ERROR(32, BMK_benchOutcome_t, "Impossible to determine original size ");
Yann Colletc2007382017-04-04 15:35:06 -0700336 totalDSize64 += fSize64;
Yann Collete63c6312016-12-06 17:46:49 -0800337 srcPtr += fileSizes[fileNb];
338 }
Yann Colletc2007382017-04-04 15:35:06 -0700339 { size_t const decodedSize = (size_t)totalDSize64;
Yann Collet2e45bad2018-08-23 14:21:18 -0700340 assert((U64)decodedSize == totalDSize64); /* check overflow */
George Lu5f490342018-06-18 11:59:45 -0700341 free(*resultBufferPtr);
George Lu5f490342018-06-18 11:59:45 -0700342 *resultBufferPtr = malloc(decodedSize);
343 if (!(*resultBufferPtr)) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700344 RETURN_ERROR(33, BMK_benchOutcome_t, "not enough memory");
George Lua8eea992018-06-19 10:58:22 -0700345 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700346 if (totalDSize64 > decodedSize) { /* size_t overflow */
Yann Collet77e805e2018-08-21 18:19:27 -0700347 free(*resultBufferPtr);
Yann Collet2e45bad2018-08-23 14:21:18 -0700348 RETURN_ERROR(32, BMK_benchOutcome_t, "original size is too large");
George Lua8eea992018-06-19 10:58:22 -0700349 }
Yann Collete63c6312016-12-06 17:46:49 -0800350 cSize = srcSize;
351 srcSize = decodedSize;
352 ratio = (double)srcSize / (double)cSize;
Yann Collet77e805e2018-08-21 18:19:27 -0700353 }
George Lu20f4f322018-06-12 15:54:43 -0400354 }
Yann Collete63c6312016-12-06 17:46:49 -0800355
George Lu20f4f322018-06-12 15:54:43 -0400356 /* Init data blocks */
Yann Collete63c6312016-12-06 17:46:49 -0800357 { const char* srcPtr = (const char*)srcBuffer;
Yann Collet1c00dc32015-10-21 08:22:25 +0100358 char* cPtr = (char*)compressedBuffer;
George Lu5f490342018-06-18 11:59:45 -0700359 char* resPtr = (char*)(*resultBufferPtr);
Yann Collet699b14d2016-03-17 19:37:33 +0100360 U32 fileNb;
Yann Colletde406ee2016-03-20 15:46:10 +0100361 for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
Yann Collet70611352015-12-16 03:01:03 +0100362 size_t remaining = fileSizes[fileNb];
George Lu85223462018-06-14 14:46:17 -0400363 U32 const nbBlocksforThisFile = (adv->mode == BMK_decodeOnly) ? 1 : (U32)((remaining + (blockSize-1)) / blockSize);
Yann Collet699b14d2016-03-17 19:37:33 +0100364 U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
Yann Colletfd416f12016-01-30 03:14:15 +0100365 for ( ; nbBlocks<blockEnd; nbBlocks++) {
Yann Colletde406ee2016-03-20 15:46:10 +0100366 size_t const thisBlockSize = MIN(remaining, blockSize);
Yann Collet2e45bad2018-08-23 14:21:18 -0700367 srcPtrs[nbBlocks] = srcPtr;
George Lu20f4f322018-06-12 15:54:43 -0400368 srcSizes[nbBlocks] = thisBlockSize;
Yann Collet2e45bad2018-08-23 14:21:18 -0700369 cPtrs[nbBlocks] = cPtr;
George Lue148db32018-07-20 14:35:09 -0700370 cCapacities[nbBlocks] = (adv->mode == BMK_decodeOnly) ? thisBlockSize : ZSTD_compressBound(thisBlockSize);
Yann Collet2e45bad2018-08-23 14:21:18 -0700371 resPtrs[nbBlocks] = resPtr;
George Lu85223462018-06-14 14:46:17 -0400372 resSizes[nbBlocks] = (adv->mode == BMK_decodeOnly) ? (size_t) ZSTD_findDecompressedSize(srcPtr, thisBlockSize) : thisBlockSize;
Yann Colleted699e62015-12-16 02:37:24 +0100373 srcPtr += thisBlockSize;
George Lue148db32018-07-20 14:35:09 -0700374 cPtr += cCapacities[nbBlocks];
Yann Colleted699e62015-12-16 02:37:24 +0100375 resPtr += thisBlockSize;
376 remaining -= thisBlockSize;
Yann Collet3ba0d6d2018-11-13 14:15:12 -0800377 if (adv->mode == BMK_decodeOnly) {
Yann Collet9126da52018-11-08 12:47:46 -0800378 cSizes[nbBlocks] = thisBlockSize;
379 benchResult.cSize = thisBlockSize;
380 }
Yann Collet77e805e2018-08-21 18:19:27 -0700381 }
382 }
George Lu20f4f322018-06-12 15:54:43 -0400383 }
Yann Collet1c00dc32015-10-21 08:22:25 +0100384
Josh Sorefa880ca22019-04-12 14:18:11 -0400385 /* warming up `compressedBuffer` */
George Lu85223462018-06-14 14:46:17 -0400386 if (adv->mode == BMK_decodeOnly) {
Yann Collet03e7e142018-03-05 13:50:07 -0800387 memcpy(compressedBuffer, srcBuffer, loadedCompressedSize);
388 } else {
389 RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
390 }
Yann Collet4856a002015-01-24 01:58:16 +0100391
392 /* Bench */
Yann Collet2e45bad2018-08-23 14:21:18 -0700393 { U64 const crcOrig = (adv->mode == BMK_decodeOnly) ? 0 : XXH64(srcBuffer, srcSize, 0);
Yann Colleta9febe82016-08-01 13:37:17 +0200394# define NB_MARKS 4
Yann Collet2e45bad2018-08-23 14:21:18 -0700395 const char* marks[NB_MARKS] = { " |", " /", " =", " \\" };
Yann Colleta9febe82016-08-01 13:37:17 +0200396 U32 markNb = 0;
Yann Collet2e45bad2018-08-23 14:21:18 -0700397 int compressionCompleted = (adv->mode == BMK_decodeOnly);
398 int decompressionCompleted = (adv->mode == BMK_compressOnly);
Yann Colletb830ccc2018-11-13 13:05:39 -0800399 BMK_benchParams_t cbp, dbp;
Yann Collet2e45bad2018-08-23 14:21:18 -0700400 BMK_initCCtxArgs cctxprep;
401 BMK_initDCtxArgs dctxprep;
Yann Colletb830ccc2018-11-13 13:05:39 -0800402
Yann Collet6309be62019-10-15 16:09:18 -0700403 cbp.benchFn = local_defaultCompress; /* ZSTD_compress2 */
Yann Colletb830ccc2018-11-13 13:05:39 -0800404 cbp.benchPayload = cctx;
Yann Collet6309be62019-10-15 16:09:18 -0700405 cbp.initFn = local_initCCtx; /* BMK_initCCtx */
Yann Colletb830ccc2018-11-13 13:05:39 -0800406 cbp.initPayload = &cctxprep;
407 cbp.errorFn = ZSTD_isError;
408 cbp.blockCount = nbBlocks;
409 cbp.srcBuffers = srcPtrs;
410 cbp.srcSizes = srcSizes;
411 cbp.dstBuffers = cPtrs;
412 cbp.dstCapacities = cCapacities;
413 cbp.blockResults = cSizes;
414
Yann Collet2e45bad2018-08-23 14:21:18 -0700415 cctxprep.cctx = cctx;
416 cctxprep.dictBuffer = dictBuffer;
417 cctxprep.dictBufferSize = dictBufferSize;
418 cctxprep.cLevel = cLevel;
419 cctxprep.comprParams = comprParams;
420 cctxprep.adv = adv;
Yann Colletb830ccc2018-11-13 13:05:39 -0800421
422 dbp.benchFn = local_defaultDecompress;
423 dbp.benchPayload = dctx;
424 dbp.initFn = local_initDCtx;
425 dbp.initPayload = &dctxprep;
426 dbp.errorFn = ZSTD_isError;
427 dbp.blockCount = nbBlocks;
428 dbp.srcBuffers = (const void* const *) cPtrs;
429 dbp.srcSizes = cSizes;
430 dbp.dstBuffers = resPtrs;
431 dbp.dstCapacities = resSizes;
432 dbp.blockResults = NULL;
433
Yann Collet2e45bad2018-08-23 14:21:18 -0700434 dctxprep.dctx = dctx;
435 dctxprep.dictBuffer = dictBuffer;
436 dctxprep.dictBufferSize = dictBufferSize;
Yann Collet4856a002015-01-24 01:58:16 +0100437
Yann Collet2e45bad2018-08-23 14:21:18 -0700438 DISPLAYLEVEL(2, "\r%70s\r", ""); /* blank line */
Yann Colletededcfc2018-12-21 16:19:44 -0800439 DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (unsigned)srcSize);
George Lu50d612f2018-06-25 15:01:03 -0700440
Yann Collet2e45bad2018-08-23 14:21:18 -0700441 while (!(compressionCompleted && decompressionCompleted)) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700442 if (!compressionCompleted) {
Yann Colletb830ccc2018-11-13 13:05:39 -0800443 BMK_runOutcome_t const cOutcome = BMK_benchTimedFn( timeStateCompress, cbp);
Yann Collet2e45bad2018-08-23 14:21:18 -0700444
Yann Collet2279f3d2018-08-24 17:28:38 -0700445 if (!BMK_isSuccessful_runOutcome(cOutcome)) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700446 return BMK_benchOutcome_error();
George Lu50d612f2018-06-25 15:01:03 -0700447 }
448
Yann Collet2279f3d2018-08-24 17:28:38 -0700449 { BMK_runTime_t const cResult = BMK_extract_runTime(cOutcome);
Yann Collet4da5bdf2018-08-23 18:04:50 -0700450 cSize = cResult.sumOfReturn;
451 ratio = (double)srcSize / cSize;
452 { BMK_benchResult_t newResult;
Yann Colletb8701102019-01-25 15:11:50 -0800453 newResult.cSpeed = (U64)((double)srcSize * TIMELOOP_NANOSEC / cResult.nanoSecPerRun);
Yann Collet4da5bdf2018-08-23 18:04:50 -0700454 benchResult.cSize = cSize;
455 if (newResult.cSpeed > benchResult.cSpeed)
456 benchResult.cSpeed = newResult.cSpeed;
457 } }
George Lu5f490342018-06-18 11:59:45 -0700458
Yann Collet2e45bad2018-08-23 14:21:18 -0700459 { int const ratioAccuracy = (ratio < 10.) ? 3 : 2;
Yann Collet2e45bad2018-08-23 14:21:18 -0700460 DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.*f),%6.*f MB/s\r",
Yann Collet4da5bdf2018-08-23 18:04:50 -0700461 marks[markNb], displayName,
Yann Colletededcfc2018-12-21 16:19:44 -0800462 (unsigned)srcSize, (unsigned)cSize,
Yann Collet2e45bad2018-08-23 14:21:18 -0700463 ratioAccuracy, ratio,
Yann Collet1f9ec132018-08-23 16:03:30 -0700464 benchResult.cSpeed < (10 MB) ? 2 : 1, (double)benchResult.cSpeed / MB_UNIT);
George Lua8eea992018-06-19 10:58:22 -0700465 }
Yann Collet01dcd0f2018-08-26 21:30:18 -0700466 compressionCompleted = BMK_isCompleted_TimedFn(timeStateCompress);
Yann Collet2e45bad2018-08-23 14:21:18 -0700467 }
George Lue89f1fb2018-08-14 14:44:47 -0700468
Yann Collet2e45bad2018-08-23 14:21:18 -0700469 if(!decompressionCompleted) {
Yann Colletb830ccc2018-11-13 13:05:39 -0800470 BMK_runOutcome_t const dOutcome = BMK_benchTimedFn(timeStateDecompress, dbp);
Yann Collet2e45bad2018-08-23 14:21:18 -0700471
Yann Collet2279f3d2018-08-24 17:28:38 -0700472 if(!BMK_isSuccessful_runOutcome(dOutcome)) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700473 return BMK_benchOutcome_error();
474 }
475
Yann Collet2279f3d2018-08-24 17:28:38 -0700476 { BMK_runTime_t const dResult = BMK_extract_runTime(dOutcome);
Yann Colletb8701102019-01-25 15:11:50 -0800477 U64 const newDSpeed = (U64)((double)srcSize * TIMELOOP_NANOSEC / dResult.nanoSecPerRun);
Yann Collet4da5bdf2018-08-23 18:04:50 -0700478 if (newDSpeed > benchResult.dSpeed)
479 benchResult.dSpeed = newDSpeed;
480 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700481
482 { int const ratioAccuracy = (ratio < 10.) ? 3 : 2;
Yann Collet2e45bad2018-08-23 14:21:18 -0700483 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 -0700484 marks[markNb], displayName,
Bimba Shresthaf33baa22020-01-31 11:54:14 -0800485 (unsigned)srcSize, (unsigned)cSize,
Yann Collet2e45bad2018-08-23 14:21:18 -0700486 ratioAccuracy, ratio,
Yann Collet1f9ec132018-08-23 16:03:30 -0700487 benchResult.cSpeed < (10 MB) ? 2 : 1, (double)benchResult.cSpeed / MB_UNIT,
Yann Collet4da5bdf2018-08-23 18:04:50 -0700488 (double)benchResult.dSpeed / MB_UNIT);
George Lua8eea992018-06-19 10:58:22 -0700489 }
Yann Collet01dcd0f2018-08-26 21:30:18 -0700490 decompressionCompleted = BMK_isCompleted_TimedFn(timeStateDecompress);
Yann Colletdaebc7f2017-11-18 15:54:32 -0800491 }
Yann Collet8bed4012018-11-08 12:36:39 -0800492 markNb = (markNb+1) % NB_MARKS;
Yann Collet4da5bdf2018-08-23 18:04:50 -0700493 } /* while (!(compressionCompleted && decompressionCompleted)) */
George Lu20f4f322018-06-12 15:54:43 -0400494
495 /* CRC Checking */
Yann Collet2e45bad2018-08-23 14:21:18 -0700496 { const BYTE* resultBuffer = (const BYTE*)(*resultBufferPtr);
George Lu5f490342018-06-18 11:59:45 -0700497 U64 const crcCheck = XXH64(resultBuffer, srcSize, 0);
George Lu85223462018-06-14 14:46:17 -0400498 if ((adv->mode == BMK_both) && (crcOrig!=crcCheck)) {
George Lu20f4f322018-06-12 15:54:43 -0400499 size_t u;
Yann Collet8bed4012018-11-08 12:36:39 -0800500 DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n",
501 displayName, (unsigned)crcOrig, (unsigned)crcCheck);
George Lu20f4f322018-06-12 15:54:43 -0400502 for (u=0; u<srcSize; u++) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700503 if (((const BYTE*)srcBuffer)[u] != resultBuffer[u]) {
Yann Colletededcfc2018-12-21 16:19:44 -0800504 unsigned segNb, bNb, pos;
George Lu20f4f322018-06-12 15:54:43 -0400505 size_t bacc = 0;
Yann Colletededcfc2018-12-21 16:19:44 -0800506 DISPLAY("Decoding error at pos %u ", (unsigned)u);
George Lu20f4f322018-06-12 15:54:43 -0400507 for (segNb = 0; segNb < nbBlocks; segNb++) {
508 if (bacc + srcSizes[segNb] > u) break;
509 bacc += srcSizes[segNb];
510 }
511 pos = (U32)(u - bacc);
512 bNb = pos / (128 KB);
513 DISPLAY("(sample %u, block %u, pos %u) \n", segNb, bNb, pos);
Yann Colletb8701102019-01-25 15:11:50 -0800514 { size_t const lowest = (u>5) ? 5 : u;
515 size_t n;
George Lu20f4f322018-06-12 15:54:43 -0400516 DISPLAY("origin: ");
Yann Colletb8701102019-01-25 15:11:50 -0800517 for (n=lowest; n>0; n--)
518 DISPLAY("%02X ", ((const BYTE*)srcBuffer)[u-n]);
George Lu20f4f322018-06-12 15:54:43 -0400519 DISPLAY(" :%02X: ", ((const BYTE*)srcBuffer)[u]);
Yann Colletb8701102019-01-25 15:11:50 -0800520 for (n=1; n<3; n++)
521 DISPLAY("%02X ", ((const BYTE*)srcBuffer)[u+n]);
George Lu20f4f322018-06-12 15:54:43 -0400522 DISPLAY(" \n");
523 DISPLAY("decode: ");
Yann Colletb8701102019-01-25 15:11:50 -0800524 for (n=lowest; n>0; n++)
525 DISPLAY("%02X ", resultBuffer[u-n]);
Yann Collet2e45bad2018-08-23 14:21:18 -0700526 DISPLAY(" :%02X: ", resultBuffer[u]);
Yann Colletb8701102019-01-25 15:11:50 -0800527 for (n=1; n<3; n++)
528 DISPLAY("%02X ", resultBuffer[u+n]);
George Lu20f4f322018-06-12 15:54:43 -0400529 DISPLAY(" \n");
530 }
531 break;
532 }
533 if (u==srcSize-1) { /* should never happen */
534 DISPLAY("no difference detected\n");
Yann Collet77e805e2018-08-21 18:19:27 -0700535 }
Yann Collet6309be62019-10-15 16:09:18 -0700536 } /* for (u=0; u<srcSize; u++) */
537 } /* if ((adv->mode == BMK_both) && (crcOrig!=crcCheck)) */
George Lu20f4f322018-06-12 15:54:43 -0400538 } /* CRC Checking */
539
Yann Collet2e45bad2018-08-23 14:21:18 -0700540 if (displayLevel == 1) { /* hidden display mode -q, used by python speed benchmark */
Yann Collet1f9ec132018-08-23 16:03:30 -0700541 double const cSpeed = (double)benchResult.cSpeed / MB_UNIT;
542 double const dSpeed = (double)benchResult.dSpeed / MB_UNIT;
Yann Collet2e45bad2018-08-23 14:21:18 -0700543 if (adv->additionalParam) {
544 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);
545 } else {
546 DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName);
547 }
George Luab26f242018-06-21 11:16:53 -0700548 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700549
550 DISPLAYLEVEL(2, "%2i#\n", cLevel);
George Lu85223462018-06-14 14:46:17 -0400551 } /* Bench */
Yann Collet2e45bad2018-08-23 14:21:18 -0700552
553 benchResult.cMem = (1ULL << (comprParams->windowLog)) + ZSTD_sizeof_CCtx(cctx);
554 return BMK_benchOutcome_setValidResult(benchResult);
George Lua8eea992018-06-19 10:58:22 -0700555}
Yann Collet4856a002015-01-24 01:58:16 +0100556
Yann Collet2e45bad2018-08-23 14:21:18 -0700557BMK_benchOutcome_t BMK_benchMemAdvanced(const void* srcBuffer, size_t srcSize,
Yann Collet77e805e2018-08-21 18:19:27 -0700558 void* dstBuffer, size_t dstCapacity,
George Lua8eea992018-06-19 10:58:22 -0700559 const size_t* fileSizes, unsigned nbFiles,
Yann Collet6ce7b082018-08-24 15:59:57 -0700560 int cLevel, const ZSTD_compressionParameters* comprParams,
George Lua8eea992018-06-19 10:58:22 -0700561 const void* dictBuffer, size_t dictBufferSize,
George Lua8eea992018-06-19 10:58:22 -0700562 int displayLevel, const char* displayName, const BMK_advancedParams_t* adv)
563
564{
Yann Collet2e45bad2018-08-23 14:21:18 -0700565 int const dstParamsError = !dstBuffer ^ !dstCapacity; /* must be both NULL or none */
566
George Lua8eea992018-06-19 10:58:22 -0700567 size_t const blockSize = ((adv->blockSize>=32 && (adv->mode != BMK_decodeOnly)) ? adv->blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ;
568 U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
569
570 /* these are the blockTable parameters, just split up */
George Ludd270b22018-07-27 08:49:25 -0700571 const void ** const srcPtrs = (const void**)malloc(maxNbBlocks * sizeof(void*));
572 size_t* const srcSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
George Lua8eea992018-06-19 10:58:22 -0700573
George Lue148db32018-07-20 14:35:09 -0700574
George Ludd270b22018-07-27 08:49:25 -0700575 void ** const cPtrs = (void**)malloc(maxNbBlocks * sizeof(void*));
576 size_t* const cSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
577 size_t* const cCapacities = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
George Lua8eea992018-06-19 10:58:22 -0700578
George Ludd270b22018-07-27 08:49:25 -0700579 void ** const resPtrs = (void**)malloc(maxNbBlocks * sizeof(void*));
580 size_t* const resSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
George Lua8eea992018-06-19 10:58:22 -0700581
Yann Collet55affc02018-08-28 11:21:09 -0700582 BMK_timedFnState_t* timeStateCompress = BMK_createTimedFnState(adv->nbSeconds * 1000, BMK_RUNTEST_DEFAULT_MS);
583 BMK_timedFnState_t* timeStateDecompress = BMK_createTimedFnState(adv->nbSeconds * 1000, BMK_RUNTEST_DEFAULT_MS);
George Lud6121ad2018-06-22 17:25:16 -0700584
Yann Collet2e45bad2018-08-23 14:21:18 -0700585 ZSTD_CCtx* const cctx = ZSTD_createCCtx();
586 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
George Lubfe83922018-08-09 12:07:57 -0700587
George Lu5f490342018-06-18 11:59:45 -0700588 const size_t maxCompressedSize = dstCapacity ? dstCapacity : ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024);
George Ludd270b22018-07-27 08:49:25 -0700589
590 void* const internalDstBuffer = dstBuffer ? NULL : malloc(maxCompressedSize);
591 void* const compressedBuffer = dstBuffer ? dstBuffer : internalDstBuffer;
592
Yann Collet2e45bad2018-08-23 14:21:18 -0700593 BMK_benchOutcome_t outcome = BMK_benchOutcome_error(); /* error by default */
George Lue89f1fb2018-08-14 14:44:47 -0700594
595 void* resultBuffer = srcSize ? malloc(srcSize) : NULL;
596
Yann Collet77e805e2018-08-21 18:19:27 -0700597 int allocationincomplete = !srcPtrs || !srcSizes || !cPtrs ||
598 !cSizes || !cCapacities || !resPtrs || !resSizes ||
Yann Collet2e45bad2018-08-23 14:21:18 -0700599 !timeStateCompress || !timeStateDecompress ||
600 !cctx || !dctx ||
601 !compressedBuffer || !resultBuffer;
George Lu5f490342018-06-18 11:59:45 -0700602
George Lu5f490342018-06-18 11:59:45 -0700603
Yann Collet2e45bad2018-08-23 14:21:18 -0700604 if (!allocationincomplete && !dstParamsError) {
605 outcome = BMK_benchMemAdvancedNoAlloc(srcPtrs, srcSizes,
606 cPtrs, cCapacities, cSizes,
607 resPtrs, resSizes,
608 &resultBuffer,
609 compressedBuffer, maxCompressedSize,
610 timeStateCompress, timeStateDecompress,
611 srcBuffer, srcSize,
612 fileSizes, nbFiles,
613 cLevel, comprParams,
614 dictBuffer, dictBufferSize,
615 cctx, dctx,
616 displayLevel, displayName, adv);
George Lua8eea992018-06-19 10:58:22 -0700617 }
Yann Collet77e805e2018-08-21 18:19:27 -0700618
Yann Colleted699e62015-12-16 02:37:24 +0100619 /* clean up */
Yann Collet77e805e2018-08-21 18:19:27 -0700620 BMK_freeTimedFnState(timeStateCompress);
621 BMK_freeTimedFnState(timeStateDecompress);
622
Yann Collet2e45bad2018-08-23 14:21:18 -0700623 ZSTD_freeCCtx(cctx);
George Lubfe83922018-08-09 12:07:57 -0700624 ZSTD_freeDCtx(dctx);
625
George Ludd270b22018-07-27 08:49:25 -0700626 free(internalDstBuffer);
Yann Collet4856a002015-01-24 01:58:16 +0100627 free(resultBuffer);
George Lu20f4f322018-06-12 15:54:43 -0400628
Yann Collet77e805e2018-08-21 18:19:27 -0700629 free((void*)srcPtrs);
630 free(srcSizes);
631 free(cPtrs);
George Lu20f4f322018-06-12 15:54:43 -0400632 free(cSizes);
George Lue148db32018-07-20 14:35:09 -0700633 free(cCapacities);
George Lu20f4f322018-06-12 15:54:43 -0400634 free(resPtrs);
635 free(resSizes);
636
George Lua8eea992018-06-19 10:58:22 -0700637 if(allocationincomplete) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700638 RETURN_ERROR(31, BMK_benchOutcome_t, "allocation error : not enough memory");
George Lua8eea992018-06-19 10:58:22 -0700639 }
Yann Collet77e805e2018-08-21 18:19:27 -0700640
Yann Collet2e45bad2018-08-23 14:21:18 -0700641 if(dstParamsError) {
642 RETURN_ERROR(32, BMK_benchOutcome_t, "Dst parameters not coherent");
George Ludd270b22018-07-27 08:49:25 -0700643 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700644 return outcome;
Yann Collet4856a002015-01-24 01:58:16 +0100645}
646
Yann Collet2e45bad2018-08-23 14:21:18 -0700647BMK_benchOutcome_t BMK_benchMem(const void* srcBuffer, size_t srcSize,
George Lu20f4f322018-06-12 15:54:43 -0400648 const size_t* fileSizes, unsigned nbFiles,
Yann Collet2e45bad2018-08-23 14:21:18 -0700649 int cLevel, const ZSTD_compressionParameters* comprParams,
George Lu20f4f322018-06-12 15:54:43 -0400650 const void* dictBuffer, size_t dictBufferSize,
George Lu20f4f322018-06-12 15:54:43 -0400651 int displayLevel, const char* displayName) {
652
Yann Collet2e45bad2018-08-23 14:21:18 -0700653 BMK_advancedParams_t const adv = BMK_initAdvancedParams();
George Lu20f4f322018-06-12 15:54:43 -0400654 return BMK_benchMemAdvanced(srcBuffer, srcSize,
George Lu5f490342018-06-18 11:59:45 -0700655 NULL, 0,
George Lu20f4f322018-06-12 15:54:43 -0400656 fileSizes, nbFiles,
657 cLevel, comprParams,
658 dictBuffer, dictBufferSize,
George Lu20f4f322018-06-12 15:54:43 -0400659 displayLevel, displayName, &adv);
660}
661
Yann Collet2e45bad2018-08-23 14:21:18 -0700662static BMK_benchOutcome_t BMK_benchCLevel(const void* srcBuffer, size_t benchedSize,
663 const size_t* fileSizes, unsigned nbFiles,
664 int cLevel, const ZSTD_compressionParameters* comprParams,
665 const void* dictBuffer, size_t dictBufferSize,
666 int displayLevel, const char* displayName,
667 BMK_advancedParams_t const * const adv)
668{
669 const char* pch = strrchr(displayName, '\\'); /* Windows */
670 if (!pch) pch = strrchr(displayName, '/'); /* Linux */
671 if (pch) displayName = pch+1;
672
673 if (adv->realTime) {
674 DISPLAYLEVEL(2, "Note : switching to real-time priority \n");
675 SET_REALTIME_PRIORITY;
676 }
677
678 if (displayLevel == 1 && !adv->additionalParam) /* --quiet mode */
679 DISPLAY("bench %s %s: input %u bytes, %u seconds, %u KB blocks\n",
680 ZSTD_VERSION_STRING, ZSTD_GIT_COMMIT_STRING,
Yann Colletededcfc2018-12-21 16:19:44 -0800681 (unsigned)benchedSize, adv->nbSeconds, (unsigned)(adv->blockSize>>10));
Yann Collet2e45bad2018-08-23 14:21:18 -0700682
683 return BMK_benchMemAdvanced(srcBuffer, benchedSize,
684 NULL, 0,
685 fileSizes, nbFiles,
686 cLevel, comprParams,
687 dictBuffer, dictBufferSize,
688 displayLevel, displayName, adv);
689}
690
691BMK_benchOutcome_t BMK_syntheticTest(int cLevel, double compressibility,
692 const ZSTD_compressionParameters* compressionParams,
693 int displayLevel, const BMK_advancedParams_t* adv)
694{
695 char name[20] = {0};
696 size_t const benchedSize = 10000000;
697 void* srcBuffer;
698 BMK_benchOutcome_t res;
699
700 if (cLevel > ZSTD_maxCLevel()) {
701 RETURN_ERROR(15, BMK_benchOutcome_t, "Invalid Compression Level");
702 }
703
704 /* Memory allocation */
705 srcBuffer = malloc(benchedSize);
706 if (!srcBuffer) RETURN_ERROR(21, BMK_benchOutcome_t, "not enough memory");
707
708 /* Fill input buffer */
709 RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
710
711 /* Bench */
712 snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
713 res = BMK_benchCLevel(srcBuffer, benchedSize,
714 &benchedSize /* ? */, 1 /* ? */,
715 cLevel, compressionParams,
716 NULL, 0, /* dictionary */
717 displayLevel, name, adv);
718
719 /* clean up */
720 free(srcBuffer);
721
722 return res;
723}
724
725
726
Yann Collet4856a002015-01-24 01:58:16 +0100727static size_t BMK_findMaxMem(U64 requiredMem)
728{
Yann Colletde406ee2016-03-20 15:46:10 +0100729 size_t const step = 64 MB;
Yann Collet4856a002015-01-24 01:58:16 +0100730 BYTE* testmem = NULL;
731
732 requiredMem = (((requiredMem >> 26) + 1) << 26);
Yann Colletde406ee2016-03-20 15:46:10 +0100733 requiredMem += step;
Yann Collet050efba2015-11-03 09:49:30 +0100734 if (requiredMem > maxMemory) requiredMem = maxMemory;
Yann Collet4856a002015-01-24 01:58:16 +0100735
Yann Colletde406ee2016-03-20 15:46:10 +0100736 do {
Yann Collet4856a002015-01-24 01:58:16 +0100737 testmem = (BYTE*)malloc((size_t)requiredMem);
Yann Colletde406ee2016-03-20 15:46:10 +0100738 requiredMem -= step;
George Lue89f1fb2018-08-14 14:44:47 -0700739 } while (!testmem && requiredMem > 0);
Yann Colletde406ee2016-03-20 15:46:10 +0100740
Yann Collet4856a002015-01-24 01:58:16 +0100741 free(testmem);
Yann Colletde406ee2016-03-20 15:46:10 +0100742 return (size_t)(requiredMem);
Yann Collet4856a002015-01-24 01:58:16 +0100743}
744
Yann Colleta5b66e32016-03-26 01:48:27 +0100745/*! BMK_loadFiles() :
Yann Collet6a9b41b2018-03-11 19:56:48 -0700746 * Loads `buffer` with content of files listed within `fileNamesTable`.
747 * At most, fills `buffer` entirely. */
George Lu20f4f322018-06-12 15:54:43 -0400748static int BMK_loadFiles(void* buffer, size_t bufferSize,
Yann Collet4086b282018-08-30 11:02:08 -0700749 size_t* fileSizes,
750 const char* const * fileNamesTable, unsigned nbFiles,
751 int displayLevel)
Yann Colleted699e62015-12-16 02:37:24 +0100752{
inikepc0d5f4e2016-04-13 10:48:04 +0200753 size_t pos = 0, totalSize = 0;
Yann Collet699b14d2016-03-17 19:37:33 +0100754 unsigned n;
Yann Colletfd416f12016-01-30 03:14:15 +0100755 for (n=0; n<nbFiles; n++) {
Yann Collet6309be62019-10-15 16:09:18 -0700756 U64 fileSize = UTIL_getFileSize(fileNamesTable[n]); /* last file may be shortened */
inikep69fcd7c2016-04-28 12:23:33 +0200757 if (UTIL_isDirectory(fileNamesTable[n])) {
George Lu01d940b2018-06-11 10:59:05 -0400758 DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]);
inikepbab43172016-04-29 15:19:40 +0200759 fileSizes[n] = 0;
inikepc0d5f4e2016-04-13 10:48:04 +0200760 continue;
761 }
Yann Collet18b79532017-10-17 16:14:25 -0700762 if (fileSize == UTIL_FILESIZE_UNKNOWN) {
George Lu01d940b2018-06-11 10:59:05 -0400763 DISPLAYLEVEL(2, "Cannot evaluate size of %s, ignoring ... \n", fileNamesTable[n]);
Yann Collet18b79532017-10-17 16:14:25 -0700764 fileSizes[n] = 0;
765 continue;
766 }
Yann Collet6309be62019-10-15 16:09:18 -0700767 { FILE* const f = fopen(fileNamesTable[n], "rb");
768 if (f==NULL) RETURN_ERROR_INT(10, "impossible to open file %s", fileNamesTable[n]);
769 DISPLAYUPDATE(2, "Loading %s... \r", fileNamesTable[n]);
770 if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */
771 { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
772 if (readSize != (size_t)fileSize) RETURN_ERROR_INT(11, "could not read %s", fileNamesTable[n]);
773 pos += readSize;
774 }
775 fileSizes[n] = (size_t)fileSize;
776 totalSize += (size_t)fileSize;
777 fclose(f);
778 } }
Yann Collet6f9c0562016-05-01 10:26:30 +0200779
Yann Collet6309be62019-10-15 16:09:18 -0700780 if (totalSize == 0) RETURN_ERROR_INT(12, "no data to bench");
George Lu20f4f322018-06-12 15:54:43 -0400781 return 0;
Yann Colleted699e62015-12-16 02:37:24 +0100782}
783
Yann Collet2e45bad2018-08-23 14:21:18 -0700784BMK_benchOutcome_t BMK_benchFilesAdvanced(
785 const char* const * fileNamesTable, unsigned nbFiles,
786 const char* dictFileName, int cLevel,
787 const ZSTD_compressionParameters* compressionParams,
788 int displayLevel, const BMK_advancedParams_t* adv)
Yann Colleted699e62015-12-16 02:37:24 +0100789{
George Lud6121ad2018-06-22 17:25:16 -0700790 void* srcBuffer = NULL;
Yann Colleted699e62015-12-16 02:37:24 +0100791 size_t benchedSize;
Yann Collet31683c02015-12-18 01:26:48 +0100792 void* dictBuffer = NULL;
793 size_t dictBufferSize = 0;
George Lud6121ad2018-06-22 17:25:16 -0700794 size_t* fileSizes = NULL;
Yann Colletc3a4baa2018-08-24 21:38:09 -0700795 BMK_benchOutcome_t res;
Yann Collete162ace2016-05-20 11:24:35 +0200796 U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +0100797
Yann Collet2e45bad2018-08-23 14:21:18 -0700798 if (!nbFiles) {
799 RETURN_ERROR(14, BMK_benchOutcome_t, "No Files to Benchmark");
George Lua8eea992018-06-19 10:58:22 -0700800 }
Yann Collet31683c02015-12-18 01:26:48 +0100801
George Lua8eea992018-06-19 10:58:22 -0700802 if (cLevel > ZSTD_maxCLevel()) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700803 RETURN_ERROR(15, BMK_benchOutcome_t, "Invalid Compression Level");
George Lua8eea992018-06-19 10:58:22 -0700804 }
805
806 fileSizes = (size_t*)calloc(nbFiles, sizeof(size_t));
Yann Collet2e45bad2018-08-23 14:21:18 -0700807 if (!fileSizes) RETURN_ERROR(12, BMK_benchOutcome_t, "not enough memory for fileSizes");
808
Yann Collet31683c02015-12-18 01:26:48 +0100809 /* Load dictionary */
Yann Colletfd416f12016-01-30 03:14:15 +0100810 if (dictFileName != NULL) {
Yann Collet18b79532017-10-17 16:14:25 -0700811 U64 const dictFileSize = UTIL_getFileSize(dictFileName);
Yann Colleted2fb6b2018-12-20 17:20:07 -0800812 if (dictFileSize == UTIL_FILESIZE_UNKNOWN) {
813 DISPLAYLEVEL(1, "error loading %s : %s \n", dictFileName, strerror(errno));
814 free(fileSizes);
815 RETURN_ERROR(9, BMK_benchOutcome_t, "benchmark aborted");
816 }
George Lua8eea992018-06-19 10:58:22 -0700817 if (dictFileSize > 64 MB) {
818 free(fileSizes);
Yann Collet2e45bad2018-08-23 14:21:18 -0700819 RETURN_ERROR(10, BMK_benchOutcome_t, "dictionary file %s too large", dictFileName);
George Lua8eea992018-06-19 10:58:22 -0700820 }
Yann Collet31683c02015-12-18 01:26:48 +0100821 dictBufferSize = (size_t)dictFileSize;
822 dictBuffer = malloc(dictBufferSize);
George Lua8eea992018-06-19 10:58:22 -0700823 if (dictBuffer==NULL) {
824 free(fileSizes);
Yann Collet2e45bad2018-08-23 14:21:18 -0700825 RETURN_ERROR(11, BMK_benchOutcome_t, "not enough memory for dictionary (%u bytes)",
Yann Colletededcfc2018-12-21 16:19:44 -0800826 (unsigned)dictBufferSize);
George Lua8eea992018-06-19 10:58:22 -0700827 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700828
829 { int const errorCode = BMK_loadFiles(dictBuffer, dictBufferSize,
830 fileSizes, &dictFileName /*?*/,
831 1 /*?*/, displayLevel);
832 if (errorCode) {
833 res = BMK_benchOutcome_error();
George Lud6121ad2018-06-22 17:25:16 -0700834 goto _cleanUp;
Yann Collet2e45bad2018-08-23 14:21:18 -0700835 } }
Yann Collet31683c02015-12-18 01:26:48 +0100836 }
837
Yann Colleted699e62015-12-16 02:37:24 +0100838 /* Memory allocation & restrictions */
839 benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
840 if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
841 if (benchedSize < totalSizeToLoad)
Yann Colletededcfc2018-12-21 16:19:44 -0800842 DISPLAY("Not enough memory; testing %u MB only...\n", (unsigned)(benchedSize >> 20));
George Lue89f1fb2018-08-14 14:44:47 -0700843
844 srcBuffer = benchedSize ? malloc(benchedSize) : NULL;
George Lua8eea992018-06-19 10:58:22 -0700845 if (!srcBuffer) {
846 free(dictBuffer);
847 free(fileSizes);
Yann Collet2e45bad2018-08-23 14:21:18 -0700848 RETURN_ERROR(12, BMK_benchOutcome_t, "not enough memory");
George Lua8eea992018-06-19 10:58:22 -0700849 }
Yann Colleted699e62015-12-16 02:37:24 +0100850
851 /* Load input buffer */
Yann Collet2e45bad2018-08-23 14:21:18 -0700852 { int const errorCode = BMK_loadFiles(srcBuffer, benchedSize,
853 fileSizes, fileNamesTable, nbFiles,
854 displayLevel);
855 if (errorCode) {
856 res = BMK_benchOutcome_error();
George Lud6121ad2018-06-22 17:25:16 -0700857 goto _cleanUp;
Yann Collet2e45bad2018-08-23 14:21:18 -0700858 } }
859
Yann Colleted699e62015-12-16 02:37:24 +0100860 /* Bench */
Yann Collet2e45bad2018-08-23 14:21:18 -0700861 { char mfName[20] = {0};
Yann Colletd898fb72017-11-17 00:22:55 -0800862 snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
Yann Collet2e45bad2018-08-23 14:21:18 -0700863 { const char* const displayName = (nbFiles > 1) ? mfName : fileNamesTable[0];
Yann Collet77e805e2018-08-21 18:19:27 -0700864 res = BMK_benchCLevel(srcBuffer, benchedSize,
Yann Collet2e45bad2018-08-23 14:21:18 -0700865 fileSizes, nbFiles,
866 cLevel, compressionParams,
867 dictBuffer, dictBufferSize,
868 displayLevel, displayName,
869 adv);
Yann Colletd898fb72017-11-17 00:22:55 -0800870 } }
Yann Collet4856a002015-01-24 01:58:16 +0100871
George Lud6121ad2018-06-22 17:25:16 -0700872_cleanUp:
Yann Collet4856a002015-01-24 01:58:16 +0100873 free(srcBuffer);
Yann Collet31683c02015-12-18 01:26:48 +0100874 free(dictBuffer);
Yann Collet70611352015-12-16 03:01:03 +0100875 free(fileSizes);
George Lu20f4f322018-06-12 15:54:43 -0400876 return res;
Yann Collet4856a002015-01-24 01:58:16 +0100877}
878
879
Yann Collet2e45bad2018-08-23 14:21:18 -0700880BMK_benchOutcome_t BMK_benchFiles(
881 const char* const * fileNamesTable, unsigned nbFiles,
882 const char* dictFileName,
883 int cLevel, const ZSTD_compressionParameters* compressionParams,
884 int displayLevel)
Yann Collet4856a002015-01-24 01:58:16 +0100885{
Yann Collet2e45bad2018-08-23 14:21:18 -0700886 BMK_advancedParams_t const adv = BMK_initAdvancedParams();
George Lu0d1ee222018-06-15 16:21:08 -0400887 return BMK_benchFilesAdvanced(fileNamesTable, nbFiles, dictFileName, cLevel, compressionParams, displayLevel, &adv);
Yann Collet4856a002015-01-24 01:58:16 +0100888}