blob: 2366cf2d7c0d9ca7ad80b13a76262da47b9ca280 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Nick Terrella4943082021-03-29 14:23:36 -07002 * Copyright (c) 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"
Nick Terrell09149be2021-04-30 15:02:12 -070039#include "../lib/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 Collet9750f3c2021-05-05 16:52:21 -070070#define DISPLAY(...) { fprintf(stderr, __VA_ARGS__); fflush(NULL); }
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 Collet08ceda32021-09-04 00:52:44 -070073#define OUTPUT(...) { fprintf(stdout, __VA_ARGS__); fflush(NULL); }
74#define OUTPUTLEVEL(l, ...) if (displayLevel>=l) { OUTPUT(__VA_ARGS__); }
Yann Colleted699e62015-12-16 02:37:24 +010075
76
77/* *************************************
78* Exceptions
79***************************************/
80#ifndef DEBUG
George Lu3adc2172018-07-12 17:30:39 -070081# define DEBUG 0
Yann Colleted699e62015-12-16 02:37:24 +010082#endif
Yann Colletfe234bf2017-06-19 15:23:19 -070083#define DEBUGOUTPUT(...) { if (DEBUG) DISPLAY(__VA_ARGS__); }
George Lu20f4f322018-06-12 15:54:43 -040084
Yann Collet6309be62019-10-15 16:09:18 -070085#define RETURN_ERROR_INT(errorNum, ...) { \
Yann Colletfe234bf2017-06-19 15:23:19 -070086 DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
George Lu20f4f322018-06-12 15:54:43 -040087 DISPLAYLEVEL(1, "Error %i : ", errorNum); \
George Lu01d940b2018-06-11 10:59:05 -040088 DISPLAYLEVEL(1, __VA_ARGS__); \
89 DISPLAYLEVEL(1, " \n"); \
George Lu20f4f322018-06-12 15:54:43 -040090 return errorNum; \
Yann Colleted699e62015-12-16 02:37:24 +010091}
Yann Collet4856a002015-01-24 01:58:16 +010092
Yann Colletd613fd92018-12-06 19:27:37 -080093#define CHECK_Z(zf) { \
94 size_t const zerr = zf; \
95 if (ZSTD_isError(zerr)) { \
96 DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
97 DISPLAY("Error : "); \
98 DISPLAY("%s failed : %s", \
99 #zf, ZSTD_getErrorName(zerr)); \
100 DISPLAY(" \n"); \
101 exit(1); \
102 } \
103}
104
Yann Collet2e45bad2018-08-23 14:21:18 -0700105#define RETURN_ERROR(errorNum, retType, ...) { \
George Lu20f4f322018-06-12 15:54:43 -0400106 retType r; \
107 memset(&r, 0, sizeof(retType)); \
108 DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \
109 DISPLAYLEVEL(1, "Error %i : ", errorNum); \
110 DISPLAYLEVEL(1, __VA_ARGS__); \
111 DISPLAYLEVEL(1, " \n"); \
Yann Collet2e45bad2018-08-23 14:21:18 -0700112 r.tag = errorNum; \
George Lu20f4f322018-06-12 15:54:43 -0400113 return r; \
114}
Yann Collet4856a002015-01-24 01:58:16 +0100115
George Lu0d1ee222018-06-15 16:21:08 -0400116
Yann Colletf3eca252015-10-22 15:31:46 +0100117/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +0100118* Benchmark Parameters
Yann Colletf3eca252015-10-22 15:31:46 +0100119***************************************/
Stella Laua1f04d52017-09-01 14:52:51 -0700120
Yann Collet77e805e2018-08-21 18:19:27 -0700121BMK_advancedParams_t BMK_initAdvancedParams(void) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700122 BMK_advancedParams_t const res = {
George Lu85223462018-06-14 14:46:17 -0400123 BMK_both, /* mode */
George Lu20f4f322018-06-12 15:54:43 -0400124 BMK_TIMETEST_DEFAULT_S, /* nbSeconds */
125 0, /* blockSize */
126 0, /* nbWorkers */
127 0, /* realTime */
George Lu20f4f322018-06-12 15:54:43 -0400128 0, /* additionalParam */
Yann Collet77e805e2018-08-21 18:19:27 -0700129 0, /* ldmFlag */
George Lu20f4f322018-06-12 15:54:43 -0400130 0, /* ldmMinMatch */
131 0, /* ldmHashLog */
George Lud6121ad2018-06-22 17:25:16 -0700132 0, /* ldmBuckSizeLog */
Nick Terrell19ca3fb2019-02-15 15:24:55 -0800133 0, /* ldmHashRateLog */
Nick Terrell46944232020-11-02 17:52:29 -0800134 ZSTD_lcm_auto, /* literalCompressionMode */
135 0 /* useRowMatchFinder */
George Lu20f4f322018-06-12 15:54:43 -0400136 };
137 return res;
Stella Lau67d4a612017-09-02 21:10:36 -0700138}
139
Yann Collet4856a002015-01-24 01:58:16 +0100140
Yann Colletf3eca252015-10-22 15:31:46 +0100141/* ********************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100142* Bench functions
Yann Colletf3eca252015-10-22 15:31:46 +0100143**********************************************************/
Yann Colletd9465012016-12-06 16:49:23 -0800144typedef struct {
145 const void* srcPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100146 size_t srcSize;
Yann Colletd9465012016-12-06 16:49:23 -0800147 void* cPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100148 size_t cRoom;
149 size_t cSize;
Yann Colletd9465012016-12-06 16:49:23 -0800150 void* resPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100151 size_t resSize;
152} blockParam_t;
153
Sean Purcell42bac7f2017-04-13 15:35:05 -0700154#undef MIN
155#undef MAX
156#define MIN(a,b) ((a) < (b) ? (a) : (b))
157#define MAX(a,b) ((a) > (b) ? (a) : (b))
Yann Collet1c00dc32015-10-21 08:22:25 +0100158
Yann Colletb8701102019-01-25 15:11:50 -0800159static void
160BMK_initCCtx(ZSTD_CCtx* ctx,
161 const void* dictBuffer, size_t dictBufferSize,
162 int cLevel,
163 const ZSTD_compressionParameters* comprParams,
164 const BMK_advancedParams_t* adv)
165{
Yann Collet5c686392018-11-15 16:12:39 -0800166 ZSTD_CCtx_reset(ctx, ZSTD_reset_session_and_parameters);
George Lu20f4f322018-06-12 15:54:43 -0400167 if (adv->nbWorkers==1) {
Yann Colletd613fd92018-12-06 19:27:37 -0800168 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_nbWorkers, 0));
George Lu20f4f322018-06-12 15:54:43 -0400169 } else {
Yann Colletd613fd92018-12-06 19:27:37 -0800170 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_nbWorkers, adv->nbWorkers));
George Lu20f4f322018-06-12 15:54:43 -0400171 }
Yann Colletd613fd92018-12-06 19:27:37 -0800172 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, cLevel));
Nick Terrell46944232020-11-02 17:52:29 -0800173 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_useRowMatchFinder, adv->useRowMatchFinder));
Yann Colletd613fd92018-12-06 19:27:37 -0800174 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_enableLongDistanceMatching, adv->ldmFlag));
175 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmMinMatch, adv->ldmMinMatch));
176 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmHashLog, adv->ldmHashLog));
177 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmBucketSizeLog, adv->ldmBucketSizeLog));
178 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_ldmHashRateLog, adv->ldmHashRateLog));
Yann Colletb8701102019-01-25 15:11:50 -0800179 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_windowLog, (int)comprParams->windowLog));
180 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_hashLog, (int)comprParams->hashLog));
181 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_chainLog, (int)comprParams->chainLog));
182 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_searchLog, (int)comprParams->searchLog));
183 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_minMatch, (int)comprParams->minMatch));
184 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_targetLength, (int)comprParams->targetLength));
Nick Terrell19ca3fb2019-02-15 15:24:55 -0800185 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_literalCompressionMode, (int)adv->literalCompressionMode));
Yann Colleteab69222021-09-03 12:51:02 -0700186 CHECK_Z(ZSTD_CCtx_setParameter(ctx, ZSTD_c_strategy, (int)comprParams->strategy));
Yann Colletd613fd92018-12-06 19:27:37 -0800187 CHECK_Z(ZSTD_CCtx_loadDictionary(ctx, dictBuffer, dictBufferSize));
George Lu20f4f322018-06-12 15:54:43 -0400188}
189
Yann Collet77e805e2018-08-21 18:19:27 -0700190static void BMK_initDCtx(ZSTD_DCtx* dctx,
George Lu20f4f322018-06-12 15:54:43 -0400191 const void* dictBuffer, size_t dictBufferSize) {
Yann Colletd613fd92018-12-06 19:27:37 -0800192 CHECK_Z(ZSTD_DCtx_reset(dctx, ZSTD_reset_session_and_parameters));
193 CHECK_Z(ZSTD_DCtx_loadDictionary(dctx, dictBuffer, dictBufferSize));
George Lu20f4f322018-06-12 15:54:43 -0400194}
195
Yann Collet2e45bad2018-08-23 14:21:18 -0700196
George Lu20f4f322018-06-12 15:54:43 -0400197typedef struct {
Yann Collet2e45bad2018-08-23 14:21:18 -0700198 ZSTD_CCtx* cctx;
George Lu20f4f322018-06-12 15:54:43 -0400199 const void* dictBuffer;
200 size_t dictBufferSize;
201 int cLevel;
202 const ZSTD_compressionParameters* comprParams;
203 const BMK_advancedParams_t* adv;
204} BMK_initCCtxArgs;
205
206static size_t local_initCCtx(void* payload) {
207 BMK_initCCtxArgs* ag = (BMK_initCCtxArgs*)payload;
Yann Collet2e45bad2018-08-23 14:21:18 -0700208 BMK_initCCtx(ag->cctx, ag->dictBuffer, ag->dictBufferSize, ag->cLevel, ag->comprParams, ag->adv);
George Lu20f4f322018-06-12 15:54:43 -0400209 return 0;
210}
211
212typedef struct {
213 ZSTD_DCtx* dctx;
214 const void* dictBuffer;
215 size_t dictBufferSize;
216} BMK_initDCtxArgs;
217
218static size_t local_initDCtx(void* payload) {
219 BMK_initDCtxArgs* ag = (BMK_initDCtxArgs*)payload;
220 BMK_initDCtx(ag->dctx, ag->dictBuffer, ag->dictBufferSize);
221 return 0;
222}
223
Yann Collet2e45bad2018-08-23 14:21:18 -0700224
225/* `addArgs` is the context */
George Lu20f4f322018-06-12 15:54:43 -0400226static size_t local_defaultCompress(
Yann Collet2e45bad2018-08-23 14:21:18 -0700227 const void* srcBuffer, size_t srcSize,
228 void* dstBuffer, size_t dstSize,
229 void* addArgs)
230{
Yann Collet2e45bad2018-08-23 14:21:18 -0700231 ZSTD_CCtx* const cctx = (ZSTD_CCtx*)addArgs;
Yann Colletd8e215c2018-11-30 11:16:26 -0800232 return ZSTD_compress2(cctx, dstBuffer, dstSize, srcBuffer, srcSize);
George Lu20f4f322018-06-12 15:54:43 -0400233}
234
Yann Collet2e45bad2018-08-23 14:21:18 -0700235/* `addArgs` is the context */
George Lu20f4f322018-06-12 15:54:43 -0400236static size_t local_defaultDecompress(
Yann Collet2e45bad2018-08-23 14:21:18 -0700237 const void* srcBuffer, size_t srcSize,
Yann Collet0c66a442018-08-28 15:47:07 -0700238 void* dstBuffer, size_t dstCapacity,
Yann Collet2e45bad2018-08-23 14:21:18 -0700239 void* addArgs)
240{
George Lu20f4f322018-06-12 15:54:43 -0400241 size_t moreToFlush = 1;
Yann Collet2e45bad2018-08-23 14:21:18 -0700242 ZSTD_DCtx* const dctx = (ZSTD_DCtx*)addArgs;
George Lu20f4f322018-06-12 15:54:43 -0400243 ZSTD_inBuffer in;
244 ZSTD_outBuffer out;
Yann Collet2e45bad2018-08-23 14:21:18 -0700245 in.src = srcBuffer; in.size = srcSize; in.pos = 0;
Yann Collet0c66a442018-08-28 15:47:07 -0700246 out.dst = dstBuffer; out.size = dstCapacity; out.pos = 0;
George Lu20f4f322018-06-12 15:54:43 -0400247 while (moreToFlush) {
George Lud6121ad2018-06-22 17:25:16 -0700248 if(out.pos == out.size) {
249 return (size_t)-ZSTD_error_dstSize_tooSmall;
250 }
Yann Collet34e146f2018-12-04 10:28:36 -0800251 moreToFlush = ZSTD_decompressStream(dctx, &out, &in);
George Lu20f4f322018-06-12 15:54:43 -0400252 if (ZSTD_isError(moreToFlush)) {
253 return moreToFlush;
254 }
255 }
256 return out.pos;
257
258}
259
Yann Collet2e45bad2018-08-23 14:21:18 -0700260
Yann Collet2e45bad2018-08-23 14:21:18 -0700261/* ================================================================= */
262/* Benchmark Zstandard, mem-to-mem scenarios */
263/* ================================================================= */
264
265int BMK_isSuccessful_benchOutcome(BMK_benchOutcome_t outcome)
266{
267 return outcome.tag == 0;
268}
269
270BMK_benchResult_t BMK_extract_benchResult(BMK_benchOutcome_t outcome)
271{
272 assert(outcome.tag == 0);
273 return outcome.internal_never_use_directly;
274}
275
Yann Collet6ce7b082018-08-24 15:59:57 -0700276static BMK_benchOutcome_t BMK_benchOutcome_error(void)
Yann Collet2e45bad2018-08-23 14:21:18 -0700277{
278 BMK_benchOutcome_t b;
279 memset(&b, 0, sizeof(b));
280 b.tag = 1;
281 return b;
282}
283
284static BMK_benchOutcome_t BMK_benchOutcome_setValidResult(BMK_benchResult_t result)
285{
286 BMK_benchOutcome_t b;
287 b.tag = 0;
288 b.internal_never_use_directly = result;
289 return b;
290}
291
292
293/* benchMem with no allocation */
Yann Collet8bed4012018-11-08 12:36:39 -0800294static BMK_benchOutcome_t
295BMK_benchMemAdvancedNoAlloc(
296 const void** srcPtrs, size_t* srcSizes,
297 void** cPtrs, size_t* cCapacities, size_t* cSizes,
298 void** resPtrs, size_t* resSizes,
299 void** resultBufferPtr, void* compressedBuffer,
300 size_t maxCompressedSize,
301 BMK_timedFnState_t* timeStateCompress,
302 BMK_timedFnState_t* timeStateDecompress,
Yann Collet2e45bad2018-08-23 14:21:18 -0700303
Yann Collet8bed4012018-11-08 12:36:39 -0800304 const void* srcBuffer, size_t srcSize,
305 const size_t* fileSizes, unsigned nbFiles,
306 const int cLevel,
307 const ZSTD_compressionParameters* comprParams,
308 const void* dictBuffer, size_t dictBufferSize,
309 ZSTD_CCtx* cctx, ZSTD_DCtx* dctx,
310 int displayLevel, const char* displayName,
311 const BMK_advancedParams_t* adv)
Yann Collet1c00dc32015-10-21 08:22:25 +0100312{
Yann Collet4da5bdf2018-08-23 18:04:50 -0700313 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 -0700314 BMK_benchResult_t benchResult;
Yann Collete63c6312016-12-06 17:46:49 -0800315 size_t const loadedCompressedSize = srcSize;
316 size_t cSize = 0;
317 double ratio = 0.;
Yann Colletde406ee2016-03-20 15:46:10 +0100318 U32 nbBlocks;
319
Yann Collet2e45bad2018-08-23 14:21:18 -0700320 assert(cctx != NULL); assert(dctx != NULL);
George Lu01d940b2018-06-11 10:59:05 -0400321
Yann Colletc776c462015-10-29 19:10:54 +0100322 /* init */
Yann Collet4da5bdf2018-08-23 18:04:50 -0700323 memset(&benchResult, 0, sizeof(benchResult));
Yann Collet2e45bad2018-08-23 14:21:18 -0700324 if (strlen(displayName)>17) displayName += strlen(displayName) - 17; /* display last 17 characters */
George Lu85223462018-06-14 14:46:17 -0400325 if (adv->mode == BMK_decodeOnly) { /* benchmark only decompression : source must be already compressed */
Yann Colletc2007382017-04-04 15:35:06 -0700326 const char* srcPtr = (const char*)srcBuffer;
327 U64 totalDSize64 = 0;
Yann Collete63c6312016-12-06 17:46:49 -0800328 U32 fileNb;
329 for (fileNb=0; fileNb<nbFiles; fileNb++) {
Sean Purcell4e709712017-02-07 13:50:09 -0800330 U64 const fSize64 = ZSTD_findDecompressedSize(srcPtr, fileSizes[fileNb]);
Yann Collet2e45bad2018-08-23 14:21:18 -0700331 if (fSize64==0) RETURN_ERROR(32, BMK_benchOutcome_t, "Impossible to determine original size ");
Yann Colletc2007382017-04-04 15:35:06 -0700332 totalDSize64 += fSize64;
Yann Collete63c6312016-12-06 17:46:49 -0800333 srcPtr += fileSizes[fileNb];
334 }
Yann Colletc2007382017-04-04 15:35:06 -0700335 { size_t const decodedSize = (size_t)totalDSize64;
Yann Collet2e45bad2018-08-23 14:21:18 -0700336 assert((U64)decodedSize == totalDSize64); /* check overflow */
George Lu5f490342018-06-18 11:59:45 -0700337 free(*resultBufferPtr);
George Lu5f490342018-06-18 11:59:45 -0700338 *resultBufferPtr = malloc(decodedSize);
339 if (!(*resultBufferPtr)) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700340 RETURN_ERROR(33, BMK_benchOutcome_t, "not enough memory");
George Lua8eea992018-06-19 10:58:22 -0700341 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700342 if (totalDSize64 > decodedSize) { /* size_t overflow */
Yann Collet77e805e2018-08-21 18:19:27 -0700343 free(*resultBufferPtr);
Yann Collet2e45bad2018-08-23 14:21:18 -0700344 RETURN_ERROR(32, BMK_benchOutcome_t, "original size is too large");
George Lua8eea992018-06-19 10:58:22 -0700345 }
Yann Collete63c6312016-12-06 17:46:49 -0800346 cSize = srcSize;
347 srcSize = decodedSize;
348 ratio = (double)srcSize / (double)cSize;
Yann Collet77e805e2018-08-21 18:19:27 -0700349 }
George Lu20f4f322018-06-12 15:54:43 -0400350 }
Yann Collete63c6312016-12-06 17:46:49 -0800351
George Lu20f4f322018-06-12 15:54:43 -0400352 /* Init data blocks */
Yann Collete63c6312016-12-06 17:46:49 -0800353 { const char* srcPtr = (const char*)srcBuffer;
Yann Collet1c00dc32015-10-21 08:22:25 +0100354 char* cPtr = (char*)compressedBuffer;
George Lu5f490342018-06-18 11:59:45 -0700355 char* resPtr = (char*)(*resultBufferPtr);
Yann Collet699b14d2016-03-17 19:37:33 +0100356 U32 fileNb;
Yann Colletde406ee2016-03-20 15:46:10 +0100357 for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
Yann Collet70611352015-12-16 03:01:03 +0100358 size_t remaining = fileSizes[fileNb];
George Lu85223462018-06-14 14:46:17 -0400359 U32 const nbBlocksforThisFile = (adv->mode == BMK_decodeOnly) ? 1 : (U32)((remaining + (blockSize-1)) / blockSize);
Yann Collet699b14d2016-03-17 19:37:33 +0100360 U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
Yann Colletfd416f12016-01-30 03:14:15 +0100361 for ( ; nbBlocks<blockEnd; nbBlocks++) {
Yann Colletde406ee2016-03-20 15:46:10 +0100362 size_t const thisBlockSize = MIN(remaining, blockSize);
Yann Collet2e45bad2018-08-23 14:21:18 -0700363 srcPtrs[nbBlocks] = srcPtr;
George Lu20f4f322018-06-12 15:54:43 -0400364 srcSizes[nbBlocks] = thisBlockSize;
Yann Collet2e45bad2018-08-23 14:21:18 -0700365 cPtrs[nbBlocks] = cPtr;
George Lue148db32018-07-20 14:35:09 -0700366 cCapacities[nbBlocks] = (adv->mode == BMK_decodeOnly) ? thisBlockSize : ZSTD_compressBound(thisBlockSize);
Yann Collet2e45bad2018-08-23 14:21:18 -0700367 resPtrs[nbBlocks] = resPtr;
George Lu85223462018-06-14 14:46:17 -0400368 resSizes[nbBlocks] = (adv->mode == BMK_decodeOnly) ? (size_t) ZSTD_findDecompressedSize(srcPtr, thisBlockSize) : thisBlockSize;
Yann Colleted699e62015-12-16 02:37:24 +0100369 srcPtr += thisBlockSize;
George Lue148db32018-07-20 14:35:09 -0700370 cPtr += cCapacities[nbBlocks];
Yann Colleted699e62015-12-16 02:37:24 +0100371 resPtr += thisBlockSize;
372 remaining -= thisBlockSize;
Yann Collet3ba0d6d2018-11-13 14:15:12 -0800373 if (adv->mode == BMK_decodeOnly) {
Yann Collet9126da52018-11-08 12:47:46 -0800374 cSizes[nbBlocks] = thisBlockSize;
375 benchResult.cSize = thisBlockSize;
376 }
Yann Collet77e805e2018-08-21 18:19:27 -0700377 }
378 }
George Lu20f4f322018-06-12 15:54:43 -0400379 }
Yann Collet1c00dc32015-10-21 08:22:25 +0100380
Josh Sorefa880ca22019-04-12 14:18:11 -0400381 /* warming up `compressedBuffer` */
George Lu85223462018-06-14 14:46:17 -0400382 if (adv->mode == BMK_decodeOnly) {
Yann Collet03e7e142018-03-05 13:50:07 -0800383 memcpy(compressedBuffer, srcBuffer, loadedCompressedSize);
384 } else {
385 RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
386 }
Yann Collet4856a002015-01-24 01:58:16 +0100387
388 /* Bench */
Yann Collet2e45bad2018-08-23 14:21:18 -0700389 { U64 const crcOrig = (adv->mode == BMK_decodeOnly) ? 0 : XXH64(srcBuffer, srcSize, 0);
Yann Colleta9febe82016-08-01 13:37:17 +0200390# define NB_MARKS 4
Yann Collet2e45bad2018-08-23 14:21:18 -0700391 const char* marks[NB_MARKS] = { " |", " /", " =", " \\" };
Yann Colleta9febe82016-08-01 13:37:17 +0200392 U32 markNb = 0;
Yann Collet2e45bad2018-08-23 14:21:18 -0700393 int compressionCompleted = (adv->mode == BMK_decodeOnly);
394 int decompressionCompleted = (adv->mode == BMK_compressOnly);
Yann Colletb830ccc2018-11-13 13:05:39 -0800395 BMK_benchParams_t cbp, dbp;
Yann Collet2e45bad2018-08-23 14:21:18 -0700396 BMK_initCCtxArgs cctxprep;
397 BMK_initDCtxArgs dctxprep;
Yann Colletb830ccc2018-11-13 13:05:39 -0800398
Yann Collet6309be62019-10-15 16:09:18 -0700399 cbp.benchFn = local_defaultCompress; /* ZSTD_compress2 */
Yann Colletb830ccc2018-11-13 13:05:39 -0800400 cbp.benchPayload = cctx;
Yann Collet6309be62019-10-15 16:09:18 -0700401 cbp.initFn = local_initCCtx; /* BMK_initCCtx */
Yann Colletb830ccc2018-11-13 13:05:39 -0800402 cbp.initPayload = &cctxprep;
403 cbp.errorFn = ZSTD_isError;
404 cbp.blockCount = nbBlocks;
405 cbp.srcBuffers = srcPtrs;
406 cbp.srcSizes = srcSizes;
407 cbp.dstBuffers = cPtrs;
408 cbp.dstCapacities = cCapacities;
409 cbp.blockResults = cSizes;
410
Yann Collet2e45bad2018-08-23 14:21:18 -0700411 cctxprep.cctx = cctx;
412 cctxprep.dictBuffer = dictBuffer;
413 cctxprep.dictBufferSize = dictBufferSize;
414 cctxprep.cLevel = cLevel;
415 cctxprep.comprParams = comprParams;
416 cctxprep.adv = adv;
Yann Colletb830ccc2018-11-13 13:05:39 -0800417
418 dbp.benchFn = local_defaultDecompress;
419 dbp.benchPayload = dctx;
420 dbp.initFn = local_initDCtx;
421 dbp.initPayload = &dctxprep;
422 dbp.errorFn = ZSTD_isError;
423 dbp.blockCount = nbBlocks;
424 dbp.srcBuffers = (const void* const *) cPtrs;
425 dbp.srcSizes = cSizes;
426 dbp.dstBuffers = resPtrs;
427 dbp.dstCapacities = resSizes;
428 dbp.blockResults = NULL;
429
Yann Collet2e45bad2018-08-23 14:21:18 -0700430 dctxprep.dctx = dctx;
431 dctxprep.dictBuffer = dictBuffer;
432 dctxprep.dictBufferSize = dictBufferSize;
Yann Collet4856a002015-01-24 01:58:16 +0100433
Yann Collet08ceda32021-09-04 00:52:44 -0700434 OUTPUTLEVEL(2, "\r%70s\r", ""); /* blank line */
Yann Colleteab69222021-09-03 12:51:02 -0700435 assert(srcSize < UINT_MAX);
Yann Collet08ceda32021-09-04 00:52:44 -0700436 OUTPUTLEVEL(2, "%2s-%-17.17s :%10u -> \r", marks[markNb], displayName, (unsigned)srcSize);
George Lu50d612f2018-06-25 15:01:03 -0700437
Yann Collet2e45bad2018-08-23 14:21:18 -0700438 while (!(compressionCompleted && decompressionCompleted)) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700439 if (!compressionCompleted) {
Yann Colletb830ccc2018-11-13 13:05:39 -0800440 BMK_runOutcome_t const cOutcome = BMK_benchTimedFn( timeStateCompress, cbp);
Yann Collet2e45bad2018-08-23 14:21:18 -0700441
Yann Collet2279f3d2018-08-24 17:28:38 -0700442 if (!BMK_isSuccessful_runOutcome(cOutcome)) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700443 return BMK_benchOutcome_error();
George Lu50d612f2018-06-25 15:01:03 -0700444 }
445
Yann Collet2279f3d2018-08-24 17:28:38 -0700446 { BMK_runTime_t const cResult = BMK_extract_runTime(cOutcome);
Yann Collet4da5bdf2018-08-23 18:04:50 -0700447 cSize = cResult.sumOfReturn;
Yann Colleteab69222021-09-03 12:51:02 -0700448 ratio = (double)srcSize / (double)cSize;
Yann Collet4da5bdf2018-08-23 18:04:50 -0700449 { BMK_benchResult_t newResult;
Yann Colletb8701102019-01-25 15:11:50 -0800450 newResult.cSpeed = (U64)((double)srcSize * TIMELOOP_NANOSEC / cResult.nanoSecPerRun);
Yann Collet4da5bdf2018-08-23 18:04:50 -0700451 benchResult.cSize = cSize;
452 if (newResult.cSpeed > benchResult.cSpeed)
453 benchResult.cSpeed = newResult.cSpeed;
454 } }
George Lu5f490342018-06-18 11:59:45 -0700455
Yann Collet2e45bad2018-08-23 14:21:18 -0700456 { int const ratioAccuracy = (ratio < 10.) ? 3 : 2;
Yann Colleteab69222021-09-03 12:51:02 -0700457 assert(cSize < UINT_MAX);
Yann Collet08ceda32021-09-04 00:52:44 -0700458 OUTPUTLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.*f), %6.*f MB/s\r",
Yann Collet4da5bdf2018-08-23 18:04:50 -0700459 marks[markNb], displayName,
Yann Colleteab69222021-09-03 12:51:02 -0700460 (unsigned)srcSize, (unsigned)cSize,
Yann Collet2e45bad2018-08-23 14:21:18 -0700461 ratioAccuracy, ratio,
Yann Collet1f9ec132018-08-23 16:03:30 -0700462 benchResult.cSpeed < (10 MB) ? 2 : 1, (double)benchResult.cSpeed / MB_UNIT);
George Lua8eea992018-06-19 10:58:22 -0700463 }
Yann Collet01dcd0f2018-08-26 21:30:18 -0700464 compressionCompleted = BMK_isCompleted_TimedFn(timeStateCompress);
Yann Collet2e45bad2018-08-23 14:21:18 -0700465 }
George Lue89f1fb2018-08-14 14:44:47 -0700466
Yann Collet2e45bad2018-08-23 14:21:18 -0700467 if(!decompressionCompleted) {
Yann Colletb830ccc2018-11-13 13:05:39 -0800468 BMK_runOutcome_t const dOutcome = BMK_benchTimedFn(timeStateDecompress, dbp);
Yann Collet2e45bad2018-08-23 14:21:18 -0700469
Yann Collet2279f3d2018-08-24 17:28:38 -0700470 if(!BMK_isSuccessful_runOutcome(dOutcome)) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700471 return BMK_benchOutcome_error();
472 }
473
Yann Collet2279f3d2018-08-24 17:28:38 -0700474 { BMK_runTime_t const dResult = BMK_extract_runTime(dOutcome);
Yann Colletb8701102019-01-25 15:11:50 -0800475 U64 const newDSpeed = (U64)((double)srcSize * TIMELOOP_NANOSEC / dResult.nanoSecPerRun);
Yann Collet4da5bdf2018-08-23 18:04:50 -0700476 if (newDSpeed > benchResult.dSpeed)
477 benchResult.dSpeed = newDSpeed;
478 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700479
480 { int const ratioAccuracy = (ratio < 10.) ? 3 : 2;
Yann Collet08ceda32021-09-04 00:52:44 -0700481 OUTPUTLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.*f), %6.*f MB/s, %6.1f MB/s \r",
Yann Collet4da5bdf2018-08-23 18:04:50 -0700482 marks[markNb], displayName,
Yann Colleteab69222021-09-03 12:51:02 -0700483 (unsigned)srcSize, (unsigned)cSize,
Yann Collet2e45bad2018-08-23 14:21:18 -0700484 ratioAccuracy, ratio,
Yann Collet1f9ec132018-08-23 16:03:30 -0700485 benchResult.cSpeed < (10 MB) ? 2 : 1, (double)benchResult.cSpeed / MB_UNIT,
Yann Collet4da5bdf2018-08-23 18:04:50 -0700486 (double)benchResult.dSpeed / MB_UNIT);
George Lua8eea992018-06-19 10:58:22 -0700487 }
Yann Collet01dcd0f2018-08-26 21:30:18 -0700488 decompressionCompleted = BMK_isCompleted_TimedFn(timeStateDecompress);
Yann Colletdaebc7f2017-11-18 15:54:32 -0800489 }
Yann Collet8bed4012018-11-08 12:36:39 -0800490 markNb = (markNb+1) % NB_MARKS;
Yann Collet4da5bdf2018-08-23 18:04:50 -0700491 } /* while (!(compressionCompleted && decompressionCompleted)) */
George Lu20f4f322018-06-12 15:54:43 -0400492
493 /* CRC Checking */
Yann Collet2e45bad2018-08-23 14:21:18 -0700494 { const BYTE* resultBuffer = (const BYTE*)(*resultBufferPtr);
George Lu5f490342018-06-18 11:59:45 -0700495 U64 const crcCheck = XXH64(resultBuffer, srcSize, 0);
George Lu85223462018-06-14 14:46:17 -0400496 if ((adv->mode == BMK_both) && (crcOrig!=crcCheck)) {
George Lu20f4f322018-06-12 15:54:43 -0400497 size_t u;
Yann Collet8bed4012018-11-08 12:36:39 -0800498 DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n",
499 displayName, (unsigned)crcOrig, (unsigned)crcCheck);
George Lu20f4f322018-06-12 15:54:43 -0400500 for (u=0; u<srcSize; u++) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700501 if (((const BYTE*)srcBuffer)[u] != resultBuffer[u]) {
Yann Colletededcfc2018-12-21 16:19:44 -0800502 unsigned segNb, bNb, pos;
George Lu20f4f322018-06-12 15:54:43 -0400503 size_t bacc = 0;
Yann Colletededcfc2018-12-21 16:19:44 -0800504 DISPLAY("Decoding error at pos %u ", (unsigned)u);
George Lu20f4f322018-06-12 15:54:43 -0400505 for (segNb = 0; segNb < nbBlocks; segNb++) {
506 if (bacc + srcSizes[segNb] > u) break;
507 bacc += srcSizes[segNb];
508 }
509 pos = (U32)(u - bacc);
510 bNb = pos / (128 KB);
511 DISPLAY("(sample %u, block %u, pos %u) \n", segNb, bNb, pos);
Yann Colletb8701102019-01-25 15:11:50 -0800512 { size_t const lowest = (u>5) ? 5 : u;
513 size_t n;
George Lu20f4f322018-06-12 15:54:43 -0400514 DISPLAY("origin: ");
Yann Colletb8701102019-01-25 15:11:50 -0800515 for (n=lowest; n>0; n--)
516 DISPLAY("%02X ", ((const BYTE*)srcBuffer)[u-n]);
George Lu20f4f322018-06-12 15:54:43 -0400517 DISPLAY(" :%02X: ", ((const BYTE*)srcBuffer)[u]);
Yann Colletb8701102019-01-25 15:11:50 -0800518 for (n=1; n<3; n++)
519 DISPLAY("%02X ", ((const BYTE*)srcBuffer)[u+n]);
George Lu20f4f322018-06-12 15:54:43 -0400520 DISPLAY(" \n");
521 DISPLAY("decode: ");
Yann Colletb8701102019-01-25 15:11:50 -0800522 for (n=lowest; n>0; n++)
523 DISPLAY("%02X ", resultBuffer[u-n]);
Yann Collet2e45bad2018-08-23 14:21:18 -0700524 DISPLAY(" :%02X: ", resultBuffer[u]);
Yann Colletb8701102019-01-25 15:11:50 -0800525 for (n=1; n<3; n++)
526 DISPLAY("%02X ", resultBuffer[u+n]);
George Lu20f4f322018-06-12 15:54:43 -0400527 DISPLAY(" \n");
528 }
529 break;
530 }
531 if (u==srcSize-1) { /* should never happen */
532 DISPLAY("no difference detected\n");
Yann Collet77e805e2018-08-21 18:19:27 -0700533 }
Yann Collet6309be62019-10-15 16:09:18 -0700534 } /* for (u=0; u<srcSize; u++) */
535 } /* if ((adv->mode == BMK_both) && (crcOrig!=crcCheck)) */
George Lu20f4f322018-06-12 15:54:43 -0400536 } /* CRC Checking */
537
Yann Collet2e45bad2018-08-23 14:21:18 -0700538 if (displayLevel == 1) { /* hidden display mode -q, used by python speed benchmark */
Yann Collet1f9ec132018-08-23 16:03:30 -0700539 double const cSpeed = (double)benchResult.cSpeed / MB_UNIT;
540 double const dSpeed = (double)benchResult.dSpeed / MB_UNIT;
Yann Collet2e45bad2018-08-23 14:21:18 -0700541 if (adv->additionalParam) {
Yann Collet08ceda32021-09-04 00:52:44 -0700542 OUTPUT("-%-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);
Yann Collet2e45bad2018-08-23 14:21:18 -0700543 } else {
Yann Collet08ceda32021-09-04 00:52:44 -0700544 OUTPUT("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName);
Yann Collet2e45bad2018-08-23 14:21:18 -0700545 }
George Luab26f242018-06-21 11:16:53 -0700546 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700547
Yann Collet08ceda32021-09-04 00:52:44 -0700548 OUTPUTLEVEL(2, "%2i#\n", cLevel);
George Lu85223462018-06-14 14:46:17 -0400549 } /* Bench */
Yann Collet2e45bad2018-08-23 14:21:18 -0700550
551 benchResult.cMem = (1ULL << (comprParams->windowLog)) + ZSTD_sizeof_CCtx(cctx);
552 return BMK_benchOutcome_setValidResult(benchResult);
George Lua8eea992018-06-19 10:58:22 -0700553}
Yann Collet4856a002015-01-24 01:58:16 +0100554
Yann Collet2e45bad2018-08-23 14:21:18 -0700555BMK_benchOutcome_t BMK_benchMemAdvanced(const void* srcBuffer, size_t srcSize,
Yann Collet77e805e2018-08-21 18:19:27 -0700556 void* dstBuffer, size_t dstCapacity,
George Lua8eea992018-06-19 10:58:22 -0700557 const size_t* fileSizes, unsigned nbFiles,
Yann Collet6ce7b082018-08-24 15:59:57 -0700558 int cLevel, const ZSTD_compressionParameters* comprParams,
George Lua8eea992018-06-19 10:58:22 -0700559 const void* dictBuffer, size_t dictBufferSize,
George Lua8eea992018-06-19 10:58:22 -0700560 int displayLevel, const char* displayName, const BMK_advancedParams_t* adv)
561
562{
Yann Collet2e45bad2018-08-23 14:21:18 -0700563 int const dstParamsError = !dstBuffer ^ !dstCapacity; /* must be both NULL or none */
564
George Lua8eea992018-06-19 10:58:22 -0700565 size_t const blockSize = ((adv->blockSize>=32 && (adv->mode != BMK_decodeOnly)) ? adv->blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ;
566 U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
567
568 /* these are the blockTable parameters, just split up */
George Ludd270b22018-07-27 08:49:25 -0700569 const void ** const srcPtrs = (const void**)malloc(maxNbBlocks * sizeof(void*));
570 size_t* const srcSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
George Lua8eea992018-06-19 10:58:22 -0700571
George Lue148db32018-07-20 14:35:09 -0700572
George Ludd270b22018-07-27 08:49:25 -0700573 void ** const cPtrs = (void**)malloc(maxNbBlocks * sizeof(void*));
574 size_t* const cSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
575 size_t* const cCapacities = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
George Lua8eea992018-06-19 10:58:22 -0700576
George Ludd270b22018-07-27 08:49:25 -0700577 void ** const resPtrs = (void**)malloc(maxNbBlocks * sizeof(void*));
578 size_t* const resSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
George Lua8eea992018-06-19 10:58:22 -0700579
Yann Collet55affc02018-08-28 11:21:09 -0700580 BMK_timedFnState_t* timeStateCompress = BMK_createTimedFnState(adv->nbSeconds * 1000, BMK_RUNTEST_DEFAULT_MS);
581 BMK_timedFnState_t* timeStateDecompress = BMK_createTimedFnState(adv->nbSeconds * 1000, BMK_RUNTEST_DEFAULT_MS);
George Lud6121ad2018-06-22 17:25:16 -0700582
Yann Collet2e45bad2018-08-23 14:21:18 -0700583 ZSTD_CCtx* const cctx = ZSTD_createCCtx();
584 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
George Lubfe83922018-08-09 12:07:57 -0700585
George Lu5f490342018-06-18 11:59:45 -0700586 const size_t maxCompressedSize = dstCapacity ? dstCapacity : ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024);
George Ludd270b22018-07-27 08:49:25 -0700587
588 void* const internalDstBuffer = dstBuffer ? NULL : malloc(maxCompressedSize);
589 void* const compressedBuffer = dstBuffer ? dstBuffer : internalDstBuffer;
590
Yann Collet2e45bad2018-08-23 14:21:18 -0700591 BMK_benchOutcome_t outcome = BMK_benchOutcome_error(); /* error by default */
George Lue89f1fb2018-08-14 14:44:47 -0700592
593 void* resultBuffer = srcSize ? malloc(srcSize) : NULL;
594
Yann Collet77e805e2018-08-21 18:19:27 -0700595 int allocationincomplete = !srcPtrs || !srcSizes || !cPtrs ||
596 !cSizes || !cCapacities || !resPtrs || !resSizes ||
Yann Collet2e45bad2018-08-23 14:21:18 -0700597 !timeStateCompress || !timeStateDecompress ||
598 !cctx || !dctx ||
599 !compressedBuffer || !resultBuffer;
George Lu5f490342018-06-18 11:59:45 -0700600
George Lu5f490342018-06-18 11:59:45 -0700601
Yann Collet2e45bad2018-08-23 14:21:18 -0700602 if (!allocationincomplete && !dstParamsError) {
603 outcome = BMK_benchMemAdvancedNoAlloc(srcPtrs, srcSizes,
604 cPtrs, cCapacities, cSizes,
605 resPtrs, resSizes,
606 &resultBuffer,
607 compressedBuffer, maxCompressedSize,
608 timeStateCompress, timeStateDecompress,
609 srcBuffer, srcSize,
610 fileSizes, nbFiles,
611 cLevel, comprParams,
612 dictBuffer, dictBufferSize,
613 cctx, dctx,
614 displayLevel, displayName, adv);
George Lua8eea992018-06-19 10:58:22 -0700615 }
Yann Collet77e805e2018-08-21 18:19:27 -0700616
Yann Colleted699e62015-12-16 02:37:24 +0100617 /* clean up */
Yann Collet77e805e2018-08-21 18:19:27 -0700618 BMK_freeTimedFnState(timeStateCompress);
619 BMK_freeTimedFnState(timeStateDecompress);
620
Yann Collet2e45bad2018-08-23 14:21:18 -0700621 ZSTD_freeCCtx(cctx);
George Lubfe83922018-08-09 12:07:57 -0700622 ZSTD_freeDCtx(dctx);
623
George Ludd270b22018-07-27 08:49:25 -0700624 free(internalDstBuffer);
Yann Collet4856a002015-01-24 01:58:16 +0100625 free(resultBuffer);
George Lu20f4f322018-06-12 15:54:43 -0400626
Yann Collet77e805e2018-08-21 18:19:27 -0700627 free((void*)srcPtrs);
628 free(srcSizes);
629 free(cPtrs);
George Lu20f4f322018-06-12 15:54:43 -0400630 free(cSizes);
George Lue148db32018-07-20 14:35:09 -0700631 free(cCapacities);
George Lu20f4f322018-06-12 15:54:43 -0400632 free(resPtrs);
633 free(resSizes);
634
George Lua8eea992018-06-19 10:58:22 -0700635 if(allocationincomplete) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700636 RETURN_ERROR(31, BMK_benchOutcome_t, "allocation error : not enough memory");
George Lua8eea992018-06-19 10:58:22 -0700637 }
Yann Collet77e805e2018-08-21 18:19:27 -0700638
Yann Collet2e45bad2018-08-23 14:21:18 -0700639 if(dstParamsError) {
640 RETURN_ERROR(32, BMK_benchOutcome_t, "Dst parameters not coherent");
George Ludd270b22018-07-27 08:49:25 -0700641 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700642 return outcome;
Yann Collet4856a002015-01-24 01:58:16 +0100643}
644
Yann Collet2e45bad2018-08-23 14:21:18 -0700645BMK_benchOutcome_t BMK_benchMem(const void* srcBuffer, size_t srcSize,
George Lu20f4f322018-06-12 15:54:43 -0400646 const size_t* fileSizes, unsigned nbFiles,
Yann Collet2e45bad2018-08-23 14:21:18 -0700647 int cLevel, const ZSTD_compressionParameters* comprParams,
George Lu20f4f322018-06-12 15:54:43 -0400648 const void* dictBuffer, size_t dictBufferSize,
George Lu20f4f322018-06-12 15:54:43 -0400649 int displayLevel, const char* displayName) {
650
Yann Collet2e45bad2018-08-23 14:21:18 -0700651 BMK_advancedParams_t const adv = BMK_initAdvancedParams();
George Lu20f4f322018-06-12 15:54:43 -0400652 return BMK_benchMemAdvanced(srcBuffer, srcSize,
George Lu5f490342018-06-18 11:59:45 -0700653 NULL, 0,
George Lu20f4f322018-06-12 15:54:43 -0400654 fileSizes, nbFiles,
655 cLevel, comprParams,
656 dictBuffer, dictBufferSize,
George Lu20f4f322018-06-12 15:54:43 -0400657 displayLevel, displayName, &adv);
658}
659
Yann Collet2e45bad2018-08-23 14:21:18 -0700660static BMK_benchOutcome_t BMK_benchCLevel(const void* srcBuffer, size_t benchedSize,
661 const size_t* fileSizes, unsigned nbFiles,
662 int cLevel, const ZSTD_compressionParameters* comprParams,
663 const void* dictBuffer, size_t dictBufferSize,
664 int displayLevel, const char* displayName,
665 BMK_advancedParams_t const * const adv)
666{
667 const char* pch = strrchr(displayName, '\\'); /* Windows */
668 if (!pch) pch = strrchr(displayName, '/'); /* Linux */
669 if (pch) displayName = pch+1;
670
671 if (adv->realTime) {
672 DISPLAYLEVEL(2, "Note : switching to real-time priority \n");
673 SET_REALTIME_PRIORITY;
674 }
675
676 if (displayLevel == 1 && !adv->additionalParam) /* --quiet mode */
Yann Collet08ceda32021-09-04 00:52:44 -0700677 OUTPUT("bench %s %s: input %u bytes, %u seconds, %u KB blocks\n",
Yann Collet2e45bad2018-08-23 14:21:18 -0700678 ZSTD_VERSION_STRING, ZSTD_GIT_COMMIT_STRING,
Yann Colletededcfc2018-12-21 16:19:44 -0800679 (unsigned)benchedSize, adv->nbSeconds, (unsigned)(adv->blockSize>>10));
Yann Collet2e45bad2018-08-23 14:21:18 -0700680
681 return BMK_benchMemAdvanced(srcBuffer, benchedSize,
682 NULL, 0,
683 fileSizes, nbFiles,
684 cLevel, comprParams,
685 dictBuffer, dictBufferSize,
686 displayLevel, displayName, adv);
687}
688
689BMK_benchOutcome_t BMK_syntheticTest(int cLevel, double compressibility,
690 const ZSTD_compressionParameters* compressionParams,
691 int displayLevel, const BMK_advancedParams_t* adv)
692{
693 char name[20] = {0};
694 size_t const benchedSize = 10000000;
695 void* srcBuffer;
696 BMK_benchOutcome_t res;
697
698 if (cLevel > ZSTD_maxCLevel()) {
699 RETURN_ERROR(15, BMK_benchOutcome_t, "Invalid Compression Level");
700 }
701
702 /* Memory allocation */
703 srcBuffer = malloc(benchedSize);
704 if (!srcBuffer) RETURN_ERROR(21, BMK_benchOutcome_t, "not enough memory");
705
706 /* Fill input buffer */
707 RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
708
709 /* Bench */
710 snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
711 res = BMK_benchCLevel(srcBuffer, benchedSize,
712 &benchedSize /* ? */, 1 /* ? */,
713 cLevel, compressionParams,
714 NULL, 0, /* dictionary */
715 displayLevel, name, adv);
716
717 /* clean up */
718 free(srcBuffer);
719
720 return res;
721}
722
723
724
Yann Collet4856a002015-01-24 01:58:16 +0100725static size_t BMK_findMaxMem(U64 requiredMem)
726{
Yann Colletde406ee2016-03-20 15:46:10 +0100727 size_t const step = 64 MB;
Yann Collet4856a002015-01-24 01:58:16 +0100728 BYTE* testmem = NULL;
729
730 requiredMem = (((requiredMem >> 26) + 1) << 26);
Yann Colletde406ee2016-03-20 15:46:10 +0100731 requiredMem += step;
Yann Collet050efba2015-11-03 09:49:30 +0100732 if (requiredMem > maxMemory) requiredMem = maxMemory;
Yann Collet4856a002015-01-24 01:58:16 +0100733
Yann Colletde406ee2016-03-20 15:46:10 +0100734 do {
Yann Collet4856a002015-01-24 01:58:16 +0100735 testmem = (BYTE*)malloc((size_t)requiredMem);
Yann Colletde406ee2016-03-20 15:46:10 +0100736 requiredMem -= step;
George Lue89f1fb2018-08-14 14:44:47 -0700737 } while (!testmem && requiredMem > 0);
Yann Colletde406ee2016-03-20 15:46:10 +0100738
Yann Collet4856a002015-01-24 01:58:16 +0100739 free(testmem);
Yann Colletde406ee2016-03-20 15:46:10 +0100740 return (size_t)(requiredMem);
Yann Collet4856a002015-01-24 01:58:16 +0100741}
742
Yann Colleta5b66e32016-03-26 01:48:27 +0100743/*! BMK_loadFiles() :
Yann Collet6a9b41b2018-03-11 19:56:48 -0700744 * Loads `buffer` with content of files listed within `fileNamesTable`.
745 * At most, fills `buffer` entirely. */
George Lu20f4f322018-06-12 15:54:43 -0400746static int BMK_loadFiles(void* buffer, size_t bufferSize,
Yann Collet4086b282018-08-30 11:02:08 -0700747 size_t* fileSizes,
748 const char* const * fileNamesTable, unsigned nbFiles,
749 int displayLevel)
Yann Colleted699e62015-12-16 02:37:24 +0100750{
inikepc0d5f4e2016-04-13 10:48:04 +0200751 size_t pos = 0, totalSize = 0;
Yann Collet699b14d2016-03-17 19:37:33 +0100752 unsigned n;
Yann Colletfd416f12016-01-30 03:14:15 +0100753 for (n=0; n<nbFiles; n++) {
Yann Collet6309be62019-10-15 16:09:18 -0700754 U64 fileSize = UTIL_getFileSize(fileNamesTable[n]); /* last file may be shortened */
inikep69fcd7c2016-04-28 12:23:33 +0200755 if (UTIL_isDirectory(fileNamesTable[n])) {
George Lu01d940b2018-06-11 10:59:05 -0400756 DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]);
inikepbab43172016-04-29 15:19:40 +0200757 fileSizes[n] = 0;
inikepc0d5f4e2016-04-13 10:48:04 +0200758 continue;
759 }
Yann Collet18b79532017-10-17 16:14:25 -0700760 if (fileSize == UTIL_FILESIZE_UNKNOWN) {
George Lu01d940b2018-06-11 10:59:05 -0400761 DISPLAYLEVEL(2, "Cannot evaluate size of %s, ignoring ... \n", fileNamesTable[n]);
Yann Collet18b79532017-10-17 16:14:25 -0700762 fileSizes[n] = 0;
763 continue;
764 }
Yann Collet6309be62019-10-15 16:09:18 -0700765 { FILE* const f = fopen(fileNamesTable[n], "rb");
766 if (f==NULL) RETURN_ERROR_INT(10, "impossible to open file %s", fileNamesTable[n]);
Yann Collet08ceda32021-09-04 00:52:44 -0700767 OUTPUTLEVEL(2, "Loading %s... \r", fileNamesTable[n]);
Yann Collet6309be62019-10-15 16:09:18 -0700768 if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */
769 { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
770 if (readSize != (size_t)fileSize) RETURN_ERROR_INT(11, "could not read %s", fileNamesTable[n]);
771 pos += readSize;
772 }
773 fileSizes[n] = (size_t)fileSize;
774 totalSize += (size_t)fileSize;
775 fclose(f);
776 } }
Yann Collet6f9c0562016-05-01 10:26:30 +0200777
Yann Collet6309be62019-10-15 16:09:18 -0700778 if (totalSize == 0) RETURN_ERROR_INT(12, "no data to bench");
George Lu20f4f322018-06-12 15:54:43 -0400779 return 0;
Yann Colleted699e62015-12-16 02:37:24 +0100780}
781
Yann Collet2e45bad2018-08-23 14:21:18 -0700782BMK_benchOutcome_t BMK_benchFilesAdvanced(
783 const char* const * fileNamesTable, unsigned nbFiles,
784 const char* dictFileName, int cLevel,
785 const ZSTD_compressionParameters* compressionParams,
786 int displayLevel, const BMK_advancedParams_t* adv)
Yann Colleted699e62015-12-16 02:37:24 +0100787{
George Lud6121ad2018-06-22 17:25:16 -0700788 void* srcBuffer = NULL;
Yann Colleted699e62015-12-16 02:37:24 +0100789 size_t benchedSize;
Yann Collet31683c02015-12-18 01:26:48 +0100790 void* dictBuffer = NULL;
791 size_t dictBufferSize = 0;
George Lud6121ad2018-06-22 17:25:16 -0700792 size_t* fileSizes = NULL;
Yann Colletc3a4baa2018-08-24 21:38:09 -0700793 BMK_benchOutcome_t res;
Yann Collete162ace2016-05-20 11:24:35 +0200794 U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +0100795
Yann Collet2e45bad2018-08-23 14:21:18 -0700796 if (!nbFiles) {
797 RETURN_ERROR(14, BMK_benchOutcome_t, "No Files to Benchmark");
George Lua8eea992018-06-19 10:58:22 -0700798 }
Yann Collet31683c02015-12-18 01:26:48 +0100799
George Lua8eea992018-06-19 10:58:22 -0700800 if (cLevel > ZSTD_maxCLevel()) {
Yann Collet2e45bad2018-08-23 14:21:18 -0700801 RETURN_ERROR(15, BMK_benchOutcome_t, "Invalid Compression Level");
George Lua8eea992018-06-19 10:58:22 -0700802 }
803
senhuang42dce48f52021-08-23 19:10:16 -0400804 if (totalSizeToLoad == UTIL_FILESIZE_UNKNOWN) {
805 RETURN_ERROR(9, BMK_benchOutcome_t, "Error loading files");
806 }
807
George Lua8eea992018-06-19 10:58:22 -0700808 fileSizes = (size_t*)calloc(nbFiles, sizeof(size_t));
Yann Collet2e45bad2018-08-23 14:21:18 -0700809 if (!fileSizes) RETURN_ERROR(12, BMK_benchOutcome_t, "not enough memory for fileSizes");
810
Yann Collet31683c02015-12-18 01:26:48 +0100811 /* Load dictionary */
Yann Colletfd416f12016-01-30 03:14:15 +0100812 if (dictFileName != NULL) {
Yann Collet18b79532017-10-17 16:14:25 -0700813 U64 const dictFileSize = UTIL_getFileSize(dictFileName);
Yann Colleted2fb6b2018-12-20 17:20:07 -0800814 if (dictFileSize == UTIL_FILESIZE_UNKNOWN) {
815 DISPLAYLEVEL(1, "error loading %s : %s \n", dictFileName, strerror(errno));
816 free(fileSizes);
817 RETURN_ERROR(9, BMK_benchOutcome_t, "benchmark aborted");
818 }
George Lua8eea992018-06-19 10:58:22 -0700819 if (dictFileSize > 64 MB) {
820 free(fileSizes);
Yann Collet2e45bad2018-08-23 14:21:18 -0700821 RETURN_ERROR(10, BMK_benchOutcome_t, "dictionary file %s too large", dictFileName);
George Lua8eea992018-06-19 10:58:22 -0700822 }
Yann Collet31683c02015-12-18 01:26:48 +0100823 dictBufferSize = (size_t)dictFileSize;
824 dictBuffer = malloc(dictBufferSize);
George Lua8eea992018-06-19 10:58:22 -0700825 if (dictBuffer==NULL) {
826 free(fileSizes);
Yann Collet2e45bad2018-08-23 14:21:18 -0700827 RETURN_ERROR(11, BMK_benchOutcome_t, "not enough memory for dictionary (%u bytes)",
Yann Colletededcfc2018-12-21 16:19:44 -0800828 (unsigned)dictBufferSize);
George Lua8eea992018-06-19 10:58:22 -0700829 }
Yann Collet2e45bad2018-08-23 14:21:18 -0700830
831 { int const errorCode = BMK_loadFiles(dictBuffer, dictBufferSize,
832 fileSizes, &dictFileName /*?*/,
833 1 /*?*/, displayLevel);
834 if (errorCode) {
835 res = BMK_benchOutcome_error();
George Lud6121ad2018-06-22 17:25:16 -0700836 goto _cleanUp;
Yann Collet2e45bad2018-08-23 14:21:18 -0700837 } }
Yann Collet31683c02015-12-18 01:26:48 +0100838 }
839
Yann Colleted699e62015-12-16 02:37:24 +0100840 /* Memory allocation & restrictions */
841 benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
842 if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
843 if (benchedSize < totalSizeToLoad)
Yann Colletededcfc2018-12-21 16:19:44 -0800844 DISPLAY("Not enough memory; testing %u MB only...\n", (unsigned)(benchedSize >> 20));
George Lue89f1fb2018-08-14 14:44:47 -0700845
846 srcBuffer = benchedSize ? malloc(benchedSize) : NULL;
George Lua8eea992018-06-19 10:58:22 -0700847 if (!srcBuffer) {
848 free(dictBuffer);
849 free(fileSizes);
Yann Collet2e45bad2018-08-23 14:21:18 -0700850 RETURN_ERROR(12, BMK_benchOutcome_t, "not enough memory");
George Lua8eea992018-06-19 10:58:22 -0700851 }
Yann Colleted699e62015-12-16 02:37:24 +0100852
853 /* Load input buffer */
Yann Collet2e45bad2018-08-23 14:21:18 -0700854 { int const errorCode = BMK_loadFiles(srcBuffer, benchedSize,
855 fileSizes, fileNamesTable, nbFiles,
856 displayLevel);
857 if (errorCode) {
858 res = BMK_benchOutcome_error();
George Lud6121ad2018-06-22 17:25:16 -0700859 goto _cleanUp;
Yann Collet2e45bad2018-08-23 14:21:18 -0700860 } }
861
Yann Colleted699e62015-12-16 02:37:24 +0100862 /* Bench */
Yann Collet2e45bad2018-08-23 14:21:18 -0700863 { char mfName[20] = {0};
Yann Colletd898fb72017-11-17 00:22:55 -0800864 snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
Yann Collet2e45bad2018-08-23 14:21:18 -0700865 { const char* const displayName = (nbFiles > 1) ? mfName : fileNamesTable[0];
Yann Collet77e805e2018-08-21 18:19:27 -0700866 res = BMK_benchCLevel(srcBuffer, benchedSize,
Yann Collet2e45bad2018-08-23 14:21:18 -0700867 fileSizes, nbFiles,
868 cLevel, compressionParams,
869 dictBuffer, dictBufferSize,
870 displayLevel, displayName,
871 adv);
Yann Colletd898fb72017-11-17 00:22:55 -0800872 } }
Yann Collet4856a002015-01-24 01:58:16 +0100873
George Lud6121ad2018-06-22 17:25:16 -0700874_cleanUp:
Yann Collet4856a002015-01-24 01:58:16 +0100875 free(srcBuffer);
Yann Collet31683c02015-12-18 01:26:48 +0100876 free(dictBuffer);
Yann Collet70611352015-12-16 03:01:03 +0100877 free(fileSizes);
George Lu20f4f322018-06-12 15:54:43 -0400878 return res;
Yann Collet4856a002015-01-24 01:58:16 +0100879}
880
881
Yann Collet2e45bad2018-08-23 14:21:18 -0700882BMK_benchOutcome_t BMK_benchFiles(
883 const char* const * fileNamesTable, unsigned nbFiles,
884 const char* dictFileName,
885 int cLevel, const ZSTD_compressionParameters* compressionParams,
886 int displayLevel)
Yann Collet4856a002015-01-24 01:58:16 +0100887{
Yann Collet2e45bad2018-08-23 14:21:18 -0700888 BMK_advancedParams_t const adv = BMK_initAdvancedParams();
George Lu0d1ee222018-06-15 16:21:08 -0400889 return BMK_benchFilesAdvanced(fileNamesTable, nbFiles, dictFileName, cLevel, compressionParams, displayLevel, &adv);
Yann Collet4856a002015-01-24 01:58:16 +0100890}