blob: 41bd26d0410ddfd5ab2fcdd0b4f4249a56917ff2 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Elliott Hughes44aba642023-09-12 20:18:59 +00002 * Copyright (c) Meta Platforms, Inc. and affiliates.
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
Yann Collet34b20ec2016-03-15 20:47:23 +010012/*_************************************
Yann Collet4856a002015-01-24 01:58:16 +010013* Includes
14**************************************/
sen06718082021-05-13 19:33:48 -040015#define ZSTD_DISABLE_DEPRECATE_WARNINGS /* No deprecation warnings, we still bench some deprecated functions */
inikep13c84242016-05-05 13:58:56 +020016#include "util.h" /* Compiler options, UTIL_GetFileSize */
17#include <stdlib.h> /* malloc */
18#include <stdio.h> /* fprintf, fopen, ftello64 */
Yann Collet78056a32019-05-30 10:13:56 -070019#include <assert.h>
Yann Collet4856a002015-01-24 01:58:16 +010020
Yann Collet59a71162019-04-10 12:37:03 -070021#include "timefn.h" /* UTIL_clockSpanNano, UTIL_getTime */
Yann Collete7e5a8c2017-06-29 18:56:24 -070022#include "mem.h" /* U32 */
Przemyslaw Skibinskicc388702016-11-21 13:58:58 +010023#ifndef ZSTD_DLL_IMPORT
Yann Collet458a1a12020-04-13 10:13:29 -070024 #include "zstd_internal.h" /* ZSTD_decodeSeqHeaders, ZSTD_blockHeaderSize, ZSTD_getcBlockSize, blockType_e, KB, MB */
Nick Terrell8def0e52020-08-24 12:24:45 -070025 #include "decompress/zstd_decompress_internal.h" /* ZSTD_DCtx struct */
Przemyslaw Skibinskicc388702016-11-21 13:58:58 +010026#else
27 #define KB *(1 <<10)
28 #define MB *(1 <<20)
29 #define GB *(1U<<30)
Yann Collet952d06f2017-02-27 17:58:02 -080030 typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
Przemyslaw Skibinskicc388702016-11-21 13:58:58 +010031#endif
cyan4973b5bb7c62017-06-29 19:59:37 -070032#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressBegin, ZSTD_compressContinue, etc. */
Yann Collete7e5a8c2017-06-29 18:56:24 -070033#include "zstd.h" /* ZSTD_versionString */
34#include "util.h" /* time functions */
Yann Colletfb98fd02015-02-10 18:15:20 +010035#include "datagen.h"
Yann Collet78056a32019-05-30 10:13:56 -070036#include "benchfn.h" /* CustomBench */
37#include "benchzstd.h" /* MB_UNIT */
Yann Collet4856a002015-01-24 01:58:16 +010038
39
Yann Collet34b20ec2016-03-15 20:47:23 +010040/*_************************************
Yann Collet4856a002015-01-24 01:58:16 +010041* Constants
42**************************************/
Yann Colletfd9d6b82015-10-26 00:06:36 +010043#define PROGRAM_DESCRIPTION "Zstandard speed analyzer"
Yann Collet4856a002015-01-24 01:58:16 +010044#define AUTHOR "Yann Collet"
Yann Collete7e5a8c2017-06-29 18:56:24 -070045#define WELCOME_MESSAGE "*** %s %s %i-bits, by %s (%s) ***\n", PROGRAM_DESCRIPTION, ZSTD_versionString(), (int)(sizeof(void*)*8), AUTHOR, __DATE__
Yann Collet4856a002015-01-24 01:58:16 +010046
Yann Collet4856a002015-01-24 01:58:16 +010047#define NBLOOPS 6
Yann Colletcdabd4a2016-03-17 16:18:36 +010048#define TIMELOOP_S 2
Yann Collet4856a002015-01-24 01:58:16 +010049
Yann Collet4856a002015-01-24 01:58:16 +010050#define MAX_MEM (1984 MB)
Yann Collet4856a002015-01-24 01:58:16 +010051
George Luceb4b9e2018-06-19 17:07:34 -070052#define DEFAULT_CLEVEL 1
53
Yann Colletf66d2ba2015-02-11 08:34:50 +010054#define COMPRESSIBILITY_DEFAULT 0.50
Yann Collet78056a32019-05-30 10:13:56 -070055static const size_t kSampleSizeDefault = 10000000;
Yann Collet4856a002015-01-24 01:58:16 +010056
Yann Colletb0e1f392018-08-23 17:21:38 -070057#define TIMELOOP_NANOSEC (1*1000000000ULL) /* 1 second */
58
Yann Collet4856a002015-01-24 01:58:16 +010059
Yann Collet34b20ec2016-03-15 20:47:23 +010060/*_************************************
Yann Collet4856a002015-01-24 01:58:16 +010061* Macros
62**************************************/
63#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
64
Yann Colletab3625c2019-05-29 16:18:22 -070065#define CONTROL(c) { if (!(c)) { abort(); } } /* like assert(), but cannot be disabled */
Yann Collet4856a002015-01-24 01:58:16 +010066
Yann Collet34b20ec2016-03-15 20:47:23 +010067/*_************************************
Yann Collet4856a002015-01-24 01:58:16 +010068* Benchmark Parameters
69**************************************/
Yann Colletededcfc2018-12-21 16:19:44 -080070static unsigned g_nbIterations = NBLOOPS;
Yann Collet4856a002015-01-24 01:58:16 +010071
Yann Collet4856a002015-01-24 01:58:16 +010072
Yann Collet34b20ec2016-03-15 20:47:23 +010073/*_*******************************************************
Yann Collet4856a002015-01-24 01:58:16 +010074* Private functions
75*********************************************************/
Yann Collet4856a002015-01-24 01:58:16 +010076static size_t BMK_findMaxMem(U64 requiredMem)
77{
Yann Colletb58c6852016-03-25 20:29:35 +010078 size_t const step = 64 MB;
Yann Collet34b20ec2016-03-15 20:47:23 +010079 void* testmem = NULL;
Yann Collet4856a002015-01-24 01:58:16 +010080
81 requiredMem = (((requiredMem >> 26) + 1) << 26);
82 if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
83
Yann Collet699b14d2016-03-17 19:37:33 +010084 requiredMem += step;
85 do {
Yann Collet34b20ec2016-03-15 20:47:23 +010086 testmem = malloc ((size_t)requiredMem);
Yann Collet4856a002015-01-24 01:58:16 +010087 requiredMem -= step;
Yann Collet699b14d2016-03-17 19:37:33 +010088 } while (!testmem);
Yann Collet4856a002015-01-24 01:58:16 +010089
90 free (testmem);
Yann Collet699b14d2016-03-17 19:37:33 +010091 return (size_t) requiredMem;
Yann Collet4856a002015-01-24 01:58:16 +010092}
93
94
Yann Collet34b20ec2016-03-15 20:47:23 +010095/*_*******************************************************
Yann Collet4856a002015-01-24 01:58:16 +010096* Benchmark wrappers
97*********************************************************/
George Lu85223462018-06-14 14:46:17 -040098
George Luceb4b9e2018-06-19 17:07:34 -070099static ZSTD_CCtx* g_zcc = NULL;
100
Yann Colletb0e1f392018-08-23 17:21:38 -0700101static size_t
102local_ZSTD_compress(const void* src, size_t srcSize,
103 void* dst, size_t dstSize,
Yann Collet904d4da2019-05-29 16:08:49 -0700104 void* payload)
Yann Collet4856a002015-01-24 01:58:16 +0100105{
George Luceb4b9e2018-06-19 17:07:34 -0700106 ZSTD_parameters p;
Yann Colletb0e1f392018-08-23 17:21:38 -0700107 ZSTD_frameParameters f = { 1 /* contentSizeHeader*/, 0, 0 };
George Luceb4b9e2018-06-19 17:07:34 -0700108 p.fParams = f;
Yann Collet904d4da2019-05-29 16:08:49 -0700109 p.cParams = *(ZSTD_compressionParameters*)payload;
Yann Colletb0e1f392018-08-23 17:21:38 -0700110 return ZSTD_compress_advanced (g_zcc, dst, dstSize, src, srcSize, NULL ,0, p);
Yann Colleta0b95202022-01-02 08:52:33 -0800111}
112
113static size_t
114local_ZSTD_compress_freshCCtx(const void* src, size_t srcSize,
115 void* dst, size_t dstSize,
116 void* payload)
117{
118 ZSTD_parameters p;
119 ZSTD_frameParameters f = { 1 /* contentSizeHeader*/, 0, 0 };
120 p.fParams = f;
121 p.cParams = *(ZSTD_compressionParameters*)payload;
122 if (g_zcc != NULL) ZSTD_freeCCtx(g_zcc);
123 g_zcc = ZSTD_createCCtx();
124 assert(g_zcc != NULL);
125 { size_t const r = ZSTD_compress_advanced (g_zcc, dst, dstSize, src, srcSize, NULL ,0, p);
126 ZSTD_freeCCtx(g_zcc);
127 g_zcc = NULL;
128 return r;
129 }
Yann Collet4856a002015-01-24 01:58:16 +0100130}
131
Yann Collet31922d72016-03-16 16:05:18 +0100132static size_t g_cSize = 0;
Yann Colletb0e1f392018-08-23 17:21:38 -0700133static size_t local_ZSTD_decompress(const void* src, size_t srcSize,
134 void* dst, size_t dstSize,
135 void* buff2)
Yann Collet4856a002015-01-24 01:58:16 +0100136{
137 (void)src; (void)srcSize;
138 return ZSTD_decompress(dst, dstSize, buff2, g_cSize);
139}
140
Przemyslaw Skibinskicc388702016-11-21 13:58:58 +0100141static ZSTD_DCtx* g_zdc = NULL;
cyan4973b5bb7c62017-06-29 19:59:37 -0700142
143#ifndef ZSTD_DLL_IMPORT
binhdvo931778e2021-11-09 15:15:35 -0500144typedef enum {
145 not_streaming = 0,
146 is_streaming = 1
147} streaming_operation;
148extern size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* ctx, const void* src, size_t srcSize, void* dst, size_t dstCapacity, const streaming_operation streaming);
Nick Terrellf2d6db42018-09-27 15:13:43 -0700149static size_t local_ZSTD_decodeLiteralsBlock(const void* src, size_t srcSize, void* dst, size_t dstSize, void* buff2)
Yann Collet4856a002015-01-24 01:58:16 +0100150{
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100151 (void)src; (void)srcSize; (void)dst; (void)dstSize;
binhdvo931778e2021-11-09 15:15:35 -0500152 return ZSTD_decodeLiteralsBlock(g_zdc, buff2, g_cSize, dst, dstSize, not_streaming);
Yann Collet4856a002015-01-24 01:58:16 +0100153}
154
Nick Terrellf2d6db42018-09-27 15:13:43 -0700155static size_t local_ZSTD_decodeSeqHeaders(const void* src, size_t srcSize, void* dst, size_t dstSize, void* buff2)
Yann Collet4856a002015-01-24 01:58:16 +0100156{
Yann Colletf3cb79b2015-08-20 00:02:43 +0100157 int nbSeq;
Yann Collet4856a002015-01-24 01:58:16 +0100158 (void)src; (void)srcSize; (void)dst; (void)dstSize;
Yann Collet0be21d72016-09-13 17:33:47 +0200159 return ZSTD_decodeSeqHeaders(g_zdc, &nbSeq, buff2, g_cSize);
Yann Collet4856a002015-01-24 01:58:16 +0100160}
Nick Terrell8def0e52020-08-24 12:24:45 -0700161
162FORCE_NOINLINE size_t ZSTD_decodeLiteralsHeader(ZSTD_DCtx* dctx, void const* src, size_t srcSize)
163{
164 RETURN_ERROR_IF(srcSize < MIN_CBLOCK_SIZE, corruption_detected, "");
165 {
166 BYTE const* istart = (BYTE const*)src;
167 symbolEncodingType_e const litEncType = (symbolEncodingType_e)(istart[0] & 3);
168 if (litEncType == set_compressed) {
169 RETURN_ERROR_IF(srcSize < 5, corruption_detected, "srcSize >= MIN_CBLOCK_SIZE == 3; here we need up to 5 for case 3");
170 {
171 size_t lhSize, litSize, litCSize;
172 U32 const lhlCode = (istart[0] >> 2) & 3;
173 U32 const lhc = MEM_readLE32(istart);
Elliott Hughes44aba642023-09-12 20:18:59 +0000174 int const flags = ZSTD_DCtx_get_bmi2(dctx) ? HUF_flags_bmi2 : 0;
Nick Terrell8def0e52020-08-24 12:24:45 -0700175 switch(lhlCode)
176 {
177 case 0: case 1: default: /* note : default is impossible, since lhlCode into [0..3] */
178 /* 2 - 2 - 10 - 10 */
179 lhSize = 3;
180 litSize = (lhc >> 4) & 0x3FF;
181 litCSize = (lhc >> 14) & 0x3FF;
182 break;
183 case 2:
184 /* 2 - 2 - 14 - 14 */
185 lhSize = 4;
186 litSize = (lhc >> 4) & 0x3FFF;
187 litCSize = lhc >> 18;
188 break;
189 case 3:
190 /* 2 - 2 - 18 - 18 */
191 lhSize = 5;
192 litSize = (lhc >> 4) & 0x3FFFF;
193 litCSize = (lhc >> 22) + ((size_t)istart[4] << 10);
194 break;
195 }
196 RETURN_ERROR_IF(litSize > ZSTD_BLOCKSIZE_MAX, corruption_detected, "");
197 RETURN_ERROR_IF(litCSize + lhSize > srcSize, corruption_detected, "");
Nick Terrell52f33a12020-08-24 16:09:45 -0700198#ifndef HUF_FORCE_DECOMPRESS_X2
Elliott Hughes44aba642023-09-12 20:18:59 +0000199 return HUF_readDTableX1_wksp(
Nick Terrell8def0e52020-08-24 12:24:45 -0700200 dctx->entropy.hufTable,
201 istart+lhSize, litCSize,
202 dctx->workspace, sizeof(dctx->workspace),
Elliott Hughes44aba642023-09-12 20:18:59 +0000203 flags);
Nick Terrell52f33a12020-08-24 16:09:45 -0700204#else
205 return HUF_readDTableX2_wksp(
206 dctx->entropy.hufTable,
207 istart+lhSize, litCSize,
Elliott Hughes44aba642023-09-12 20:18:59 +0000208 dctx->workspace, sizeof(dctx->workspace), flags);
Nick Terrell52f33a12020-08-24 16:09:45 -0700209#endif
Nick Terrell8def0e52020-08-24 12:24:45 -0700210 }
211 }
212 }
213 return 0;
214}
215
216static size_t local_ZSTD_decodeLiteralsHeader(const void* src, size_t srcSize, void* dst, size_t dstSize, void* buff2)
217{
218 (void)dst, (void)dstSize, (void)src, (void)srcSize;
219 return ZSTD_decodeLiteralsHeader(g_zdc, buff2, g_cSize);
220}
Przemyslaw Skibinski179555c2016-11-15 18:05:46 +0100221#endif
Yann Collet4856a002015-01-24 01:58:16 +0100222
Yann Collet589f0112016-10-28 15:17:38 -0700223static ZSTD_CStream* g_cstream= NULL;
Yann Colletb0e1f392018-08-23 17:21:38 -0700224static size_t
225local_ZSTD_compressStream(const void* src, size_t srcSize,
226 void* dst, size_t dstCapacity,
Yann Collet904d4da2019-05-29 16:08:49 -0700227 void* payload)
Yann Collet34b20ec2016-03-15 20:47:23 +0100228{
Yann Collet589f0112016-10-28 15:17:38 -0700229 ZSTD_outBuffer buffOut;
230 ZSTD_inBuffer buffIn;
George Luceb4b9e2018-06-19 17:07:34 -0700231 ZSTD_parameters p;
232 ZSTD_frameParameters f = {1 /* contentSizeHeader*/, 0, 0};
233 p.fParams = f;
Yann Collet904d4da2019-05-29 16:08:49 -0700234 p.cParams = *(ZSTD_compressionParameters*)payload;
George Luceb4b9e2018-06-19 17:07:34 -0700235 ZSTD_initCStream_advanced(g_cstream, NULL, 0, p, ZSTD_CONTENTSIZE_UNKNOWN);
Yann Collet589f0112016-10-28 15:17:38 -0700236 buffOut.dst = dst;
237 buffOut.size = dstCapacity;
238 buffOut.pos = 0;
239 buffIn.src = src;
240 buffIn.size = srcSize;
241 buffIn.pos = 0;
242 ZSTD_compressStream(g_cstream, &buffOut, &buffIn);
243 ZSTD_endStream(g_cstream, &buffOut);
244 return buffOut.pos;
Yann Collet34b20ec2016-03-15 20:47:23 +0100245}
Yann Collet4856a002015-01-24 01:58:16 +0100246
Yann Colletb0e1f392018-08-23 17:21:38 -0700247static size_t
Yann Colleteb6b1992019-05-28 16:50:49 -0700248local_ZSTD_compressStream_freshCCtx(const void* src, size_t srcSize,
249 void* dst, size_t dstCapacity,
Yann Collet904d4da2019-05-29 16:08:49 -0700250 void* payload)
Yann Colleteb6b1992019-05-28 16:50:49 -0700251{
Yann Collet213dc612022-01-01 23:46:49 -0800252 if (g_cstream != NULL) ZSTD_freeCCtx(g_cstream);
253 g_cstream = ZSTD_createCCtx();
254 assert(g_cstream != NULL);
Yann Colleteb6b1992019-05-28 16:50:49 -0700255
Yann Collet213dc612022-01-01 23:46:49 -0800256 { size_t const r = local_ZSTD_compressStream(src, srcSize, dst, dstCapacity, payload);
257 ZSTD_freeCCtx(g_cstream);
258 g_cstream = NULL;
259 return r;
260 }
Yann Colleteb6b1992019-05-28 16:50:49 -0700261}
262
263static size_t
Yann Collet5618e002020-09-21 07:19:20 -0700264local_ZSTD_compress2(const void* src, size_t srcSize,
265 void* dst, size_t dstCapacity,
266 void* payload)
Yann Collet2e84bec2017-06-29 13:03:10 -0700267{
Yann Collet904d4da2019-05-29 16:08:49 -0700268 (void)payload;
Yann Colletd8e215c2018-11-30 11:16:26 -0800269 return ZSTD_compress2(g_cstream, dst, dstCapacity, src, srcSize);
Yann Collet2e84bec2017-06-29 13:03:10 -0700270}
271
Yann Colletb0e1f392018-08-23 17:21:38 -0700272static size_t
Yann Collet5618e002020-09-21 07:19:20 -0700273local_ZSTD_compressStream2_end(const void* src, size_t srcSize,
274 void* dst, size_t dstCapacity,
275 void* payload)
276{
277 ZSTD_outBuffer buffOut;
278 ZSTD_inBuffer buffIn;
279 (void)payload;
280 buffOut.dst = dst;
281 buffOut.size = dstCapacity;
282 buffOut.pos = 0;
283 buffIn.src = src;
284 buffIn.size = srcSize;
285 buffIn.pos = 0;
286 ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_end);
287 return buffOut.pos;
288}
289
290static size_t
291local_ZSTD_compressStream2_continue(const void* src, size_t srcSize,
292 void* dst, size_t dstCapacity,
293 void* payload)
Yann Collet2e84bec2017-06-29 13:03:10 -0700294{
295 ZSTD_outBuffer buffOut;
296 ZSTD_inBuffer buffIn;
Yann Collet904d4da2019-05-29 16:08:49 -0700297 (void)payload;
Yann Collet2e84bec2017-06-29 13:03:10 -0700298 buffOut.dst = dst;
299 buffOut.size = dstCapacity;
300 buffOut.pos = 0;
301 buffIn.src = src;
302 buffIn.size = srcSize;
303 buffIn.pos = 0;
Yann Colletd8e215c2018-11-30 11:16:26 -0800304 ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
305 ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_end);
Yann Collet2e84bec2017-06-29 13:03:10 -0700306 return buffOut.pos;
307}
308
Yann Colletb0e1f392018-08-23 17:21:38 -0700309static size_t
310local_ZSTD_compress_generic_T2_end(const void* src, size_t srcSize,
311 void* dst, size_t dstCapacity,
Yann Collet904d4da2019-05-29 16:08:49 -0700312 void* payload)
Yann Collete7e5a8c2017-06-29 18:56:24 -0700313{
Yann Collet904d4da2019-05-29 16:08:49 -0700314 (void)payload;
Yann Collet3583d192018-12-05 17:26:02 -0800315 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_nbWorkers, 2);
Yann Colletd8e215c2018-11-30 11:16:26 -0800316 return ZSTD_compress2(g_cstream, dst, dstCapacity, src, srcSize);
Yann Collete7e5a8c2017-06-29 18:56:24 -0700317}
318
Yann Colletb0e1f392018-08-23 17:21:38 -0700319static size_t
320local_ZSTD_compress_generic_T2_continue(const void* src, size_t srcSize,
321 void* dst, size_t dstCapacity,
Yann Collet904d4da2019-05-29 16:08:49 -0700322 void* payload)
Yann Collete7e5a8c2017-06-29 18:56:24 -0700323{
324 ZSTD_outBuffer buffOut;
325 ZSTD_inBuffer buffIn;
Yann Collet904d4da2019-05-29 16:08:49 -0700326 (void)payload;
Yann Collet3583d192018-12-05 17:26:02 -0800327 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_nbWorkers, 2);
Yann Collete7e5a8c2017-06-29 18:56:24 -0700328 buffOut.dst = dst;
329 buffOut.size = dstCapacity;
330 buffOut.pos = 0;
331 buffIn.src = src;
332 buffIn.size = srcSize;
333 buffIn.pos = 0;
Yann Colletd8e215c2018-11-30 11:16:26 -0800334 ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
335 while(ZSTD_compressStream2(g_cstream, &buffOut, &buffIn, ZSTD_e_end)) {}
Yann Collete7e5a8c2017-06-29 18:56:24 -0700336 return buffOut.pos;
337}
338
Yann Collet589f0112016-10-28 15:17:38 -0700339static ZSTD_DStream* g_dstream= NULL;
Yann Colletb0e1f392018-08-23 17:21:38 -0700340static size_t
341local_ZSTD_decompressStream(const void* src, size_t srcSize,
342 void* dst, size_t dstCapacity,
343 void* buff2)
Yann Collet34b20ec2016-03-15 20:47:23 +0100344{
Yann Collet589f0112016-10-28 15:17:38 -0700345 ZSTD_outBuffer buffOut;
346 ZSTD_inBuffer buffIn;
Yann Collet34b20ec2016-03-15 20:47:23 +0100347 (void)src; (void)srcSize;
Yann Collet589f0112016-10-28 15:17:38 -0700348 ZSTD_initDStream(g_dstream);
349 buffOut.dst = dst;
350 buffOut.size = dstCapacity;
351 buffOut.pos = 0;
352 buffIn.src = buff2;
353 buffIn.size = g_cSize;
354 buffIn.pos = 0;
355 ZSTD_decompressStream(g_dstream, &buffOut, &buffIn);
356 return buffOut.pos;
Yann Collet34b20ec2016-03-15 20:47:23 +0100357}
358
cyan4973b5bb7c62017-06-29 19:59:37 -0700359#ifndef ZSTD_DLL_IMPORT
Nick Terrellf2d6db42018-09-27 15:13:43 -0700360static size_t local_ZSTD_compressContinue(const void* src, size_t srcSize,
361 void* dst, size_t dstCapacity,
Yann Collet904d4da2019-05-29 16:08:49 -0700362 void* payload)
Yann Collet31922d72016-03-16 16:05:18 +0100363{
George Luceb4b9e2018-06-19 17:07:34 -0700364 ZSTD_parameters p;
Yann Colletb0e1f392018-08-23 17:21:38 -0700365 ZSTD_frameParameters f = { 1 /* contentSizeHeader*/, 0, 0 };
George Luceb4b9e2018-06-19 17:07:34 -0700366 p.fParams = f;
Yann Collet904d4da2019-05-29 16:08:49 -0700367 p.cParams = *(ZSTD_compressionParameters*)payload;
George Luceb4b9e2018-06-19 17:07:34 -0700368 ZSTD_compressBegin_advanced(g_zcc, NULL, 0, p, srcSize);
Yann Collet62470b42016-07-28 15:29:08 +0200369 return ZSTD_compressEnd(g_zcc, dst, dstCapacity, src, srcSize);
Yann Collet31922d72016-03-16 16:05:18 +0100370}
371
Przemyslaw Skibinski4beb51f2016-12-20 10:17:21 +0100372#define FIRST_BLOCK_SIZE 8
Yann Collet904d4da2019-05-29 16:08:49 -0700373static size_t
374local_ZSTD_compressContinue_extDict(const void* src, size_t srcSize,
375 void* dst, size_t dstCapacity,
376 void* payload)
Przemyslaw Skibinski4beb51f2016-12-20 10:17:21 +0100377{
378 BYTE firstBlockBuf[FIRST_BLOCK_SIZE];
Yann Colletd39a25c2018-08-23 14:28:51 -0700379
George Luceb4b9e2018-06-19 17:07:34 -0700380 ZSTD_parameters p;
Yann Collet904d4da2019-05-29 16:08:49 -0700381 ZSTD_frameParameters const f = { 1, 0, 0 };
George Luceb4b9e2018-06-19 17:07:34 -0700382 p.fParams = f;
Yann Collet904d4da2019-05-29 16:08:49 -0700383 p.cParams = *(ZSTD_compressionParameters*)payload;
George Luceb4b9e2018-06-19 17:07:34 -0700384 ZSTD_compressBegin_advanced(g_zcc, NULL, 0, p, srcSize);
Przemyslaw Skibinski4beb51f2016-12-20 10:17:21 +0100385 memcpy(firstBlockBuf, src, FIRST_BLOCK_SIZE);
Przemyslaw Skibinski4beb51f2016-12-20 10:17:21 +0100386
Yann Colletb0e1f392018-08-23 17:21:38 -0700387 { size_t const compressResult = ZSTD_compressContinue(g_zcc,
388 dst, dstCapacity,
389 firstBlockBuf, FIRST_BLOCK_SIZE);
390 if (ZSTD_isError(compressResult)) {
391 DISPLAY("local_ZSTD_compressContinue_extDict error : %s\n",
392 ZSTD_getErrorName(compressResult));
393 return compressResult;
394 }
Przemyslaw Skibinski4beb51f2016-12-20 10:17:21 +0100395 dst = (BYTE*)dst + compressResult;
396 dstCapacity -= compressResult;
397 }
Yann Colletb0e1f392018-08-23 17:21:38 -0700398 return ZSTD_compressEnd(g_zcc, dst, dstCapacity,
399 (const BYTE*)src + FIRST_BLOCK_SIZE,
400 srcSize - FIRST_BLOCK_SIZE);
Przemyslaw Skibinski4beb51f2016-12-20 10:17:21 +0100401}
402
Nick Terrellf2d6db42018-09-27 15:13:43 -0700403static size_t local_ZSTD_decompressContinue(const void* src, size_t srcSize,
404 void* dst, size_t dstCapacity,
405 void* buff2)
Yann Collet31922d72016-03-16 16:05:18 +0100406{
407 size_t regeneratedSize = 0;
408 const BYTE* ip = (const BYTE*)buff2;
409 const BYTE* const iend = ip + g_cSize;
410 BYTE* op = (BYTE*)dst;
411 size_t remainingCapacity = dstCapacity;
412
Yann Colletb0e1f392018-08-23 17:21:38 -0700413 (void)src; (void)srcSize; /* unused */
Yann Collet31922d72016-03-16 16:05:18 +0100414 ZSTD_decompressBegin(g_zdc);
415 while (ip < iend) {
416 size_t const iSize = ZSTD_nextSrcSizeToDecompress(g_zdc);
417 size_t const decodedSize = ZSTD_decompressContinue(g_zdc, op, remainingCapacity, ip, iSize);
418 ip += iSize;
419 regeneratedSize += decodedSize;
420 op += decodedSize;
421 remainingCapacity -= decodedSize;
422 }
423
424 return regeneratedSize;
425}
Przemyslaw Skibinskicc388702016-11-21 13:58:58 +0100426#endif
Yann Collet34b20ec2016-03-15 20:47:23 +0100427
428
429/*_*******************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100430* Bench functions
431*********************************************************/
Yann Colletcee31bb2019-01-25 15:38:08 -0800432static int benchMem(unsigned benchNb,
433 const void* src, size_t srcSize,
434 int cLevel, ZSTD_compressionParameters cparams)
Yann Collet4856a002015-01-24 01:58:16 +0100435{
George Lu3adc2172018-07-12 17:30:39 -0700436 size_t dstBuffSize = ZSTD_compressBound(srcSize);
Yann Colletc3a4baa2018-08-24 21:38:09 -0700437 BYTE* dstBuff;
438 void* dstBuff2;
Yann Collet904d4da2019-05-29 16:08:49 -0700439 void* payload;
Yann Collet4856a002015-01-24 01:58:16 +0100440 const char* benchName;
George Lud6121ad2018-06-22 17:25:16 -0700441 BMK_benchFn_t benchFunction;
George Lua8eea992018-06-19 10:58:22 -0700442 int errorcode = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100443
Yann Colletfb98fd02015-02-10 18:15:20 +0100444 /* Selection */
Yann Collet4856a002015-01-24 01:58:16 +0100445 switch(benchNb)
446 {
447 case 1:
George Luceb4b9e2018-06-19 17:07:34 -0700448 benchFunction = local_ZSTD_compress; benchName = "compress";
Yann Collet4856a002015-01-24 01:58:16 +0100449 break;
Yann Colletf141eaa2016-03-15 21:13:52 +0100450 case 2:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700451 benchFunction = local_ZSTD_decompress; benchName = "decompress";
Yann Collet4856a002015-01-24 01:58:16 +0100452 break;
Yann Colleta0b95202022-01-02 08:52:33 -0800453 case 3:
454 benchFunction = local_ZSTD_compress_freshCCtx; benchName = "compress_freshCCtx";
455 break;
Przemyslaw Skibinskicc388702016-11-21 13:58:58 +0100456#ifndef ZSTD_DLL_IMPORT
Yann Collet31922d72016-03-16 16:05:18 +0100457 case 11:
George Luceb4b9e2018-06-19 17:07:34 -0700458 benchFunction = local_ZSTD_compressContinue; benchName = "compressContinue";
Yann Collet31922d72016-03-16 16:05:18 +0100459 break;
460 case 12:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700461 benchFunction = local_ZSTD_compressContinue_extDict; benchName = "compressContinue_extDict";
Przemyslaw Skibinski4beb51f2016-12-20 10:17:21 +0100462 break;
463 case 13:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700464 benchFunction = local_ZSTD_decompressContinue; benchName = "decompressContinue";
Yann Collet31922d72016-03-16 16:05:18 +0100465 break;
Nick Terrell8def0e52020-08-24 12:24:45 -0700466 case 30:
467 benchFunction = local_ZSTD_decodeLiteralsHeader; benchName = "decodeLiteralsHeader";
468 break;
Nick Terrell5152fb22017-03-29 18:51:58 -0700469 case 31:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700470 benchFunction = local_ZSTD_decodeLiteralsBlock; benchName = "decodeLiteralsBlock";
Yann Collet4856a002015-01-24 01:58:16 +0100471 break;
472 case 32:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700473 benchFunction = local_ZSTD_decodeSeqHeaders; benchName = "decodeSeqHeaders";
Yann Collet4856a002015-01-24 01:58:16 +0100474 break;
Przemyslaw Skibinski179555c2016-11-15 18:05:46 +0100475#endif
Nick Terrell5152fb22017-03-29 18:51:58 -0700476 case 41:
George Luceb4b9e2018-06-19 17:07:34 -0700477 benchFunction = local_ZSTD_compressStream; benchName = "compressStream";
Yann Collet34b20ec2016-03-15 20:47:23 +0100478 break;
479 case 42:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700480 benchFunction = local_ZSTD_decompressStream; benchName = "decompressStream";
Yann Collet34b20ec2016-03-15 20:47:23 +0100481 break;
Yann Colleteb6b1992019-05-28 16:50:49 -0700482 case 43:
483 benchFunction = local_ZSTD_compressStream_freshCCtx; benchName = "compressStream_freshCCtx";
484 break;
Yann Collet5618e002020-09-21 07:19:20 -0700485 case 50:
486 benchFunction = local_ZSTD_compress2; benchName = "compress2";
487 break;
Yann Collet2e84bec2017-06-29 13:03:10 -0700488 case 51:
Yann Collet5618e002020-09-21 07:19:20 -0700489 benchFunction = local_ZSTD_compressStream2_end; benchName = "compressStream2, end";
Yann Collet2e84bec2017-06-29 13:03:10 -0700490 break;
491 case 52:
Yann Collet5618e002020-09-21 07:19:20 -0700492 benchFunction = local_ZSTD_compressStream2_end; benchName = "compressStream2, end & short";
493 break;
494 case 53:
495 benchFunction = local_ZSTD_compressStream2_continue; benchName = "compressStream2, continue";
Yann Collete7e5a8c2017-06-29 18:56:24 -0700496 break;
497 case 61:
498 benchFunction = local_ZSTD_compress_generic_T2_continue; benchName = "compress_generic, -T2, continue";
499 break;
500 case 62:
501 benchFunction = local_ZSTD_compress_generic_T2_end; benchName = "compress_generic, -T2, end";
Yann Collet2e84bec2017-06-29 13:03:10 -0700502 break;
Yann Collet4856a002015-01-24 01:58:16 +0100503 default :
504 return 0;
505 }
506
507 /* Allocation */
Yann Collet213089c2015-06-18 07:43:16 -0800508 dstBuff = (BYTE*)malloc(dstBuffSize);
Yann Colletc3a4baa2018-08-24 21:38:09 -0700509 dstBuff2 = malloc(dstBuffSize);
510 if ((!dstBuff) || (!dstBuff2)) {
Yann Collet4856a002015-01-24 01:58:16 +0100511 DISPLAY("\nError: not enough memory!\n");
Yann Colletc3a4baa2018-08-24 21:38:09 -0700512 free(dstBuff); free(dstBuff2);
Yann Collet4856a002015-01-24 01:58:16 +0100513 return 12;
514 }
Yann Collet904d4da2019-05-29 16:08:49 -0700515 payload = dstBuff2;
Yann Collet2e84bec2017-06-29 13:03:10 -0700516 if (g_zcc==NULL) g_zcc = ZSTD_createCCtx();
517 if (g_zdc==NULL) g_zdc = ZSTD_createDCtx();
518 if (g_cstream==NULL) g_cstream = ZSTD_createCStream();
519 if (g_dstream==NULL) g_dstream = ZSTD_createDStream();
Yann Collet4856a002015-01-24 01:58:16 +0100520
Yann Collete874dac2018-11-20 14:56:07 -0800521 /* DISPLAY("params: cLevel %d, wlog %d hlog %d clog %d slog %d mml %d tlen %d strat %d \n",
Yann Colletd39a25c2018-08-23 14:28:51 -0700522 cLevel, cparams->windowLog, cparams->hashLog, cparams->chainLog, cparams->searchLog,
Yann Collete874dac2018-11-20 14:56:07 -0800523 cparams->minMatch, cparams->targetLength, cparams->strategy); */
George Luceb4b9e2018-06-19 17:07:34 -0700524
Yann Collet3583d192018-12-05 17:26:02 -0800525 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_compressionLevel, cLevel);
Yann Colletcee31bb2019-01-25 15:38:08 -0800526 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_windowLog, (int)cparams.windowLog);
527 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_hashLog, (int)cparams.hashLog);
528 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_chainLog, (int)cparams.chainLog);
529 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_searchLog, (int)cparams.searchLog);
530 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_minMatch, (int)cparams.minMatch);
531 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_targetLength, (int)cparams.targetLength);
Yann Colletbe9e5612018-12-06 15:00:52 -0800532 ZSTD_CCtx_setParameter(g_zcc, ZSTD_c_strategy, cparams.strategy);
George Luceb4b9e2018-06-19 17:07:34 -0700533
Yann Collet3583d192018-12-05 17:26:02 -0800534 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_compressionLevel, cLevel);
Yann Colletcee31bb2019-01-25 15:38:08 -0800535 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_windowLog, (int)cparams.windowLog);
536 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_hashLog, (int)cparams.hashLog);
537 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_chainLog, (int)cparams.chainLog);
538 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_searchLog, (int)cparams.searchLog);
539 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_minMatch, (int)cparams.minMatch);
540 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_targetLength, (int)cparams.targetLength);
Yann Colletbe9e5612018-12-06 15:00:52 -0800541 ZSTD_CCtx_setParameter(g_cstream, ZSTD_c_strategy, cparams.strategy);
George Luceb4b9e2018-06-19 17:07:34 -0700542
Yann Collet4856a002015-01-24 01:58:16 +0100543 /* Preparation */
544 switch(benchNb)
545 {
George Luceb4b9e2018-06-19 17:07:34 -0700546 case 1:
Yann Collet904d4da2019-05-29 16:08:49 -0700547 payload = &cparams;
George Luceb4b9e2018-06-19 17:07:34 -0700548 break;
Yann Colletf141eaa2016-03-15 21:13:52 +0100549 case 2:
Yann Collet904d4da2019-05-29 16:08:49 -0700550 g_cSize = ZSTD_compress(dstBuff2, dstBuffSize, src, srcSize, cLevel);
Yann Collet4856a002015-01-24 01:58:16 +0100551 break;
Yann Colleta0b95202022-01-02 08:52:33 -0800552 case 3:
553 payload = &cparams;
554 break;
Przemyslaw Skibinskicc388702016-11-21 13:58:58 +0100555#ifndef ZSTD_DLL_IMPORT
George Luceb4b9e2018-06-19 17:07:34 -0700556 case 11:
Yann Collet904d4da2019-05-29 16:08:49 -0700557 payload = &cparams;
George Luceb4b9e2018-06-19 17:07:34 -0700558 break;
559 case 12:
Yann Collet904d4da2019-05-29 16:08:49 -0700560 payload = &cparams;
George Luceb4b9e2018-06-19 17:07:34 -0700561 break;
Przemyslaw Skibinski4beb51f2016-12-20 10:17:21 +0100562 case 13 :
Yann Collet904d4da2019-05-29 16:08:49 -0700563 g_cSize = ZSTD_compress(dstBuff2, dstBuffSize, src, srcSize, cLevel);
Yann Collet4856a002015-01-24 01:58:16 +0100564 break;
Nick Terrell8def0e52020-08-24 12:24:45 -0700565 case 30: /* ZSTD_decodeLiteralsHeader */
566 /* fall-through */
Yann Collet904d4da2019-05-29 16:08:49 -0700567 case 31: /* ZSTD_decodeLiteralsBlock : starts literals block in dstBuff2 */
568 { size_t frameHeaderSize;
George Luceb4b9e2018-06-19 17:07:34 -0700569 g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, cLevel);
Nick Terrellb1ec94e2019-10-21 19:42:14 -0700570 frameHeaderSize = ZSTD_frameHeaderSize(dstBuff, ZSTD_FRAMEHEADERSIZE_PREFIX(ZSTD_f_zstd1));
Yann Colletab3625c2019-05-29 16:18:22 -0700571 CONTROL(!ZSTD_isError(frameHeaderSize));
Yann Collet904d4da2019-05-29 16:08:49 -0700572 /* check block is compressible, hence contains a literals section */
573 { blockProperties_t bp;
574 ZSTD_getcBlockSize(dstBuff+frameHeaderSize, dstBuffSize, &bp); /* Get 1st block type */
575 if (bp.blockType != bt_compressed) {
576 DISPLAY("ZSTD_decodeLiteralsBlock : impossible to test on this sample (not compressible)\n");
577 goto _cleanOut;
578 } }
579 { size_t const skippedSize = frameHeaderSize + ZSTD_blockHeaderSize;
580 memcpy(dstBuff2, dstBuff+skippedSize, g_cSize-skippedSize);
Yann Collet759433d2015-01-24 13:31:55 +0100581 }
Yann Collet88fcd292015-11-25 14:42:45 +0100582 srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */
Yann Colletea1f50b2017-09-27 13:51:05 -0700583 ZSTD_decompressBegin(g_zdc);
Yann Collet4856a002015-01-24 01:58:16 +0100584 break;
585 }
Yann Colletfb98fd02015-02-10 18:15:20 +0100586 case 32: /* ZSTD_decodeSeqHeaders */
Yann Collet31922d72016-03-16 16:05:18 +0100587 { blockProperties_t bp;
Yann Collet4856a002015-01-24 01:58:16 +0100588 const BYTE* ip = dstBuff;
589 const BYTE* iend;
Yann Collet904d4da2019-05-29 16:08:49 -0700590 { size_t const cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, cLevel);
Nick Terrellb1ec94e2019-10-21 19:42:14 -0700591 CONTROL(cSize > ZSTD_FRAMEHEADERSIZE_PREFIX(ZSTD_f_zstd1));
Yann Collet759433d2015-01-24 13:31:55 +0100592 }
Yann Collet904d4da2019-05-29 16:08:49 -0700593 /* Skip frame Header */
Nick Terrellb1ec94e2019-10-21 19:42:14 -0700594 { size_t const frameHeaderSize = ZSTD_frameHeaderSize(dstBuff, ZSTD_FRAMEHEADERSIZE_PREFIX(ZSTD_f_zstd1));
Yann Colletab3625c2019-05-29 16:18:22 -0700595 CONTROL(!ZSTD_isError(frameHeaderSize));
Yann Collet904d4da2019-05-29 16:08:49 -0700596 ip += frameHeaderSize;
597 }
598 /* Find end of block */
599 { size_t const cBlockSize = ZSTD_getcBlockSize(ip, dstBuffSize, &bp); /* Get 1st block type */
600 if (bp.blockType != bt_compressed) {
601 DISPLAY("ZSTD_decodeSeqHeaders : impossible to test on this sample (not compressible)\n");
602 goto _cleanOut;
603 }
604 iend = ip + ZSTD_blockHeaderSize + cBlockSize; /* End of first block */
605 }
606 ip += ZSTD_blockHeaderSize; /* skip block header */
Yann Collet0be21d72016-09-13 17:33:47 +0200607 ZSTD_decompressBegin(g_zdc);
Yann Colletab3625c2019-05-29 16:18:22 -0700608 CONTROL(iend > ip);
binhdvo931778e2021-11-09 15:15:35 -0500609 ip += ZSTD_decodeLiteralsBlock(g_zdc, ip, (size_t)(iend-ip), dstBuff, dstBuffSize, not_streaming); /* skip literal segment */
Yann Colletcee31bb2019-01-25 15:38:08 -0800610 g_cSize = (size_t)(iend-ip);
Yann Collet904d4da2019-05-29 16:08:49 -0700611 memcpy(dstBuff2, ip, g_cSize); /* copy rest of block (it starts by SeqHeader) */
Yann Collet88fcd292015-11-25 14:42:45 +0100612 srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */
Yann Collet4856a002015-01-24 01:58:16 +0100613 break;
614 }
Przemyslaw Skibinski811b34d2016-11-15 19:02:39 +0100615#else
616 case 31:
617 goto _cleanOut;
Przemyslaw Skibinski179555c2016-11-15 18:05:46 +0100618#endif
Yann Colletd39a25c2018-08-23 14:28:51 -0700619 case 41 :
Yann Collet904d4da2019-05-29 16:08:49 -0700620 payload = &cparams;
George Luceb4b9e2018-06-19 17:07:34 -0700621 break;
Yann Collet34b20ec2016-03-15 20:47:23 +0100622 case 42 :
Yann Collet904d4da2019-05-29 16:08:49 -0700623 g_cSize = ZSTD_compress(payload, dstBuffSize, src, srcSize, cLevel);
Yann Collet34b20ec2016-03-15 20:47:23 +0100624 break;
Yann Colleted38b642019-05-29 15:26:06 -0700625 case 43 :
Yann Collet904d4da2019-05-29 16:08:49 -0700626 payload = &cparams;
Yann Colleted38b642019-05-29 15:26:06 -0700627 break;
Yann Collet4856a002015-01-24 01:58:16 +0100628
Yann Collet5618e002020-09-21 07:19:20 -0700629 case 52 :
630 /* compressStream2, short dstCapacity */
631 dstBuffSize--;
632 break;
633
Yann Collet4856a002015-01-24 01:58:16 +0100634 /* test functions */
Yann Collet364ce192018-02-21 07:30:26 -0800635 /* convention: test functions have ID > 100 */
Yann Collet4856a002015-01-24 01:58:16 +0100636
Yann Collet4856a002015-01-24 01:58:16 +0100637 default : ;
638 }
639
Yann Colletd39a25c2018-08-23 14:28:51 -0700640 /* warming up dstBuff */
Yann Collet364ce192018-02-21 07:30:26 -0800641 { size_t i; for (i=0; i<dstBuffSize; i++) dstBuff[i]=(BYTE)i; }
Yann Collet759433d2015-01-24 13:31:55 +0100642
Yann Collet364ce192018-02-21 07:30:26 -0800643 /* benchmark loop */
Yann Collet55affc02018-08-28 11:21:09 -0700644 { BMK_timedFnState_t* const tfs = BMK_createTimedFnState(g_nbIterations * 1000, 1000);
Yann Collet975abf82018-11-13 16:04:35 -0800645 void* const avoidStrictAliasingPtr = &dstBuff;
Yann Colletb830ccc2018-11-13 13:05:39 -0800646 BMK_benchParams_t bp;
Yann Collet7b23cc42018-08-24 12:40:10 -0700647 BMK_runTime_t bestResult;
648 bestResult.sumOfReturn = 0;
Nick Terrella48e32c2019-04-15 21:01:27 -0700649 bestResult.nanoSecPerRun = (double)TIMELOOP_NANOSEC * 2000000000; /* hopefully large enough : must be larger than any potential measurement */
Yann Colletab3625c2019-05-29 16:18:22 -0700650 CONTROL(tfs != NULL);
Yann Colletb830ccc2018-11-13 13:05:39 -0800651
652 bp.benchFn = benchFunction;
Yann Collet904d4da2019-05-29 16:08:49 -0700653 bp.benchPayload = payload;
Yann Colletb830ccc2018-11-13 13:05:39 -0800654 bp.initFn = NULL;
655 bp.initPayload = NULL;
656 bp.errorFn = ZSTD_isError;
657 bp.blockCount = 1;
658 bp.srcBuffers = &src;
659 bp.srcSizes = &srcSize;
Yann Collet975abf82018-11-13 16:04:35 -0800660 bp.dstBuffers = (void* const*) avoidStrictAliasingPtr; /* circumvent strict aliasing warning on gcc-8,
661 * because gcc considers that `void* const *` and `void**` are 2 different types */
Yann Colletb830ccc2018-11-13 13:05:39 -0800662 bp.dstCapacities = &dstBuffSize;
663 bp.blockResults = NULL;
664
Yann Collet7b23cc42018-08-24 12:40:10 -0700665 for (;;) {
Yann Collet975abf82018-11-13 16:04:35 -0800666 BMK_runOutcome_t const bOutcome = BMK_benchTimedFn(tfs, bp);
Yann Collet4856a002015-01-24 01:58:16 +0100667
Yann Collet2279f3d2018-08-24 17:28:38 -0700668 if (!BMK_isSuccessful_runOutcome(bOutcome)) {
Yann Collet7b23cc42018-08-24 12:40:10 -0700669 DISPLAY("ERROR benchmarking function ! ! \n");
670 errorcode = 1;
671 goto _cleanOut;
672 }
673
Yann Collet2279f3d2018-08-24 17:28:38 -0700674 { BMK_runTime_t const newResult = BMK_extract_runTime(bOutcome);
Yann Collet7b23cc42018-08-24 12:40:10 -0700675 if (newResult.nanoSecPerRun < bestResult.nanoSecPerRun )
676 bestResult.nanoSecPerRun = newResult.nanoSecPerRun;
677 DISPLAY("\r%2u#%-29.29s:%8.1f MB/s (%8u) ",
678 benchNb, benchName,
679 (double)srcSize * TIMELOOP_NANOSEC / bestResult.nanoSecPerRun / MB_UNIT,
680 (unsigned)newResult.sumOfReturn );
681 }
682
Yann Collet01dcd0f2018-08-26 21:30:18 -0700683 if ( BMK_isCompleted_TimedFn(tfs) ) break;
Yann Colletc3a4baa2018-08-24 21:38:09 -0700684 }
685 BMK_freeTimedFnState(tfs);
686 }
Yann Collet7b23cc42018-08-24 12:40:10 -0700687 DISPLAY("\n");
Yann Colletd39a25c2018-08-23 14:28:51 -0700688
Yann Collet93a823c2015-11-13 15:08:43 +0100689_cleanOut:
Yann Collet4856a002015-01-24 01:58:16 +0100690 free(dstBuff);
Yann Colletc3a4baa2018-08-24 21:38:09 -0700691 free(dstBuff2);
cyan49734b263062017-07-01 08:03:59 -0700692 ZSTD_freeCCtx(g_zcc); g_zcc=NULL;
693 ZSTD_freeDCtx(g_zdc); g_zdc=NULL;
694 ZSTD_freeCStream(g_cstream); g_cstream=NULL;
695 ZSTD_freeDStream(g_dstream); g_dstream=NULL;
George Lua8eea992018-06-19 10:58:22 -0700696 return errorcode;
Yann Collet4856a002015-01-24 01:58:16 +0100697}
698
699
Yann Colletd39a25c2018-08-23 14:28:51 -0700700static int benchSample(U32 benchNb,
Yann Collet4c414942019-05-28 16:37:03 -0700701 size_t benchedSize, double compressibility,
Yann Colletd39a25c2018-08-23 14:28:51 -0700702 int cLevel, ZSTD_compressionParameters cparams)
Yann Collet4856a002015-01-24 01:58:16 +0100703{
Yann Collet4856a002015-01-24 01:58:16 +0100704 /* Allocation */
Yann Colletd39a25c2018-08-23 14:28:51 -0700705 void* const origBuff = malloc(benchedSize);
Yann Collet69afc712016-03-15 21:44:39 +0100706 if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); return 12; }
Yann Collet4856a002015-01-24 01:58:16 +0100707
708 /* Fill buffer */
Yann Collet4c414942019-05-28 16:37:03 -0700709 RDG_genBuffer(origBuff, benchedSize, compressibility, 0.0, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100710
711 /* bench */
Yann Colletd39a25c2018-08-23 14:28:51 -0700712 DISPLAY("\r%70s\r", "");
Yann Collet4c414942019-05-28 16:37:03 -0700713 DISPLAY(" Sample %u bytes : \n", (unsigned)benchedSize);
Yann Colletd39a25c2018-08-23 14:28:51 -0700714 if (benchNb) {
715 benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
716 } else { /* 0 == run all tests */
717 for (benchNb=0; benchNb<100; benchNb++) {
718 benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
719 } }
Yann Collet4856a002015-01-24 01:58:16 +0100720
721 free(origBuff);
722 return 0;
723}
724
725
Yann Colletd39a25c2018-08-23 14:28:51 -0700726static int benchFiles(U32 benchNb,
727 const char** fileNamesTable, const int nbFiles,
728 int cLevel, ZSTD_compressionParameters cparams)
Yann Collet4856a002015-01-24 01:58:16 +0100729{
Yann Collet4856a002015-01-24 01:58:16 +0100730 /* Loop for each file */
Yann Collet569b81a2016-03-16 15:26:51 +0100731 int fileIdx;
732 for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
Yann Collet18b79532017-10-17 16:14:25 -0700733 const char* const inFileName = fileNamesTable[fileIdx];
734 FILE* const inFile = fopen( inFileName, "rb" );
Yann Collet4856a002015-01-24 01:58:16 +0100735 size_t benchedSize;
Yann Collet4856a002015-01-24 01:58:16 +0100736
737 /* Check file existence */
Yann Collet69afc712016-03-15 21:44:39 +0100738 if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
Yann Collet4856a002015-01-24 01:58:16 +0100739
Yann Collet695a6cb2015-12-10 15:51:38 +0100740 /* Memory allocation & restrictions */
Yann Colletd39a25c2018-08-23 14:28:51 -0700741 { U64 const inFileSize = UTIL_getFileSize(inFileName);
742 if (inFileSize == UTIL_FILESIZE_UNKNOWN) {
743 DISPLAY( "Cannot measure size of %s\n", inFileName);
744 fclose(inFile);
745 return 11;
746 }
747 benchedSize = BMK_findMaxMem(inFileSize*3) / 3;
748 if ((U64)benchedSize > inFileSize)
749 benchedSize = (size_t)inFileSize;
750 if ((U64)benchedSize < inFileSize) {
Yann Collet4da5bdf2018-08-23 18:04:50 -0700751 DISPLAY("Not enough memory for '%s' full size; testing %u MB only... \n",
Yann Colletededcfc2018-12-21 16:19:44 -0800752 inFileName, (unsigned)(benchedSize>>20));
Yann Collet569b81a2016-03-16 15:26:51 +0100753 } }
Yann Collet4856a002015-01-24 01:58:16 +0100754
Yann Colletd39a25c2018-08-23 14:28:51 -0700755 /* Alloc */
756 { void* const origBuff = malloc(benchedSize);
757 if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); fclose(inFile); return 12; }
Yann Collet569b81a2016-03-16 15:26:51 +0100758
Yann Colletd39a25c2018-08-23 14:28:51 -0700759 /* Fill input buffer */
760 DISPLAY("Loading %s... \r", inFileName);
761 { size_t const readSize = fread(origBuff, 1, benchedSize, inFile);
762 fclose(inFile);
763 if (readSize != benchedSize) {
764 DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
765 free(origBuff);
766 return 13;
767 } }
768
769 /* bench */
770 DISPLAY("\r%70s\r", ""); /* blank line */
771 DISPLAY(" %s : \n", inFileName);
772 if (benchNb) {
773 benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
774 } else {
775 for (benchNb=0; benchNb<100; benchNb++) {
776 benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
Elliott Hughes44aba642023-09-12 20:18:59 +0000777 }
778 benchNb = 0;
779 }
Yann Colletd39a25c2018-08-23 14:28:51 -0700780
781 free(origBuff);
782 } }
Yann Collet4856a002015-01-24 01:58:16 +0100783
784 return 0;
785}
786
787
Yann Colletb0e1f392018-08-23 17:21:38 -0700788
789/*_*******************************************************
790* Argument Parsing
791*********************************************************/
792
793#define ERROR_OUT(msg) { DISPLAY("%s \n", msg); exit(1); }
794
Yann Colletc3a4baa2018-08-24 21:38:09 -0700795static unsigned readU32FromChar(const char** stringPtr)
Yann Colletb0e1f392018-08-23 17:21:38 -0700796{
797 const char errorMsg[] = "error: numeric value too large";
798 unsigned result = 0;
799 while ((**stringPtr >='0') && (**stringPtr <='9')) {
800 unsigned const max = (((unsigned)(-1)) / 10) - 1;
801 if (result > max) ERROR_OUT(errorMsg);
Yann Colletcee31bb2019-01-25 15:38:08 -0800802 result *= 10;
803 result += (unsigned)(**stringPtr - '0');
804 (*stringPtr)++ ;
Yann Colletb0e1f392018-08-23 17:21:38 -0700805 }
806 if ((**stringPtr=='K') || (**stringPtr=='M')) {
807 unsigned const maxK = ((unsigned)(-1)) >> 10;
808 if (result > maxK) ERROR_OUT(errorMsg);
809 result <<= 10;
810 if (**stringPtr=='M') {
811 if (result > maxK) ERROR_OUT(errorMsg);
812 result <<= 10;
813 }
814 (*stringPtr)++; /* skip `K` or `M` */
815 if (**stringPtr=='i') (*stringPtr)++;
816 if (**stringPtr=='B') (*stringPtr)++;
817 }
818 return result;
819}
820
Yann Colletcee31bb2019-01-25 15:38:08 -0800821static int longCommandWArg(const char** stringPtr, const char* longCommand)
Yann Colletb0e1f392018-08-23 17:21:38 -0700822{
823 size_t const comSize = strlen(longCommand);
824 int const result = !strncmp(*stringPtr, longCommand, comSize);
825 if (result) *stringPtr += comSize;
826 return result;
827}
828
829
830/*_*******************************************************
831* Command line
832*********************************************************/
833
Yann Collet69afc712016-03-15 21:44:39 +0100834static int usage(const char* exename)
Yann Collet4856a002015-01-24 01:58:16 +0100835{
836 DISPLAY( "Usage :\n");
837 DISPLAY( " %s [arg] file1 file2 ... fileX\n", exename);
838 DISPLAY( "Arguments :\n");
839 DISPLAY( " -H/-h : Help (this text + advanced options)\n");
840 return 0;
841}
842
Yann Collet569b81a2016-03-16 15:26:51 +0100843static int usage_advanced(const char* exename)
Yann Collet4856a002015-01-24 01:58:16 +0100844{
Yann Collet569b81a2016-03-16 15:26:51 +0100845 usage(exename);
Yann Collet4856a002015-01-24 01:58:16 +0100846 DISPLAY( "\nAdvanced options :\n");
847 DISPLAY( " -b# : test only function # \n");
George Luceb4b9e2018-06-19 17:07:34 -0700848 DISPLAY( " -l# : benchmark functions at that compression level (default : %i)\n", DEFAULT_CLEVEL);
Yann Collet5d5c8952019-10-22 14:57:15 -0700849 DISPLAY( "--zstd= : custom parameter selection. Format same as zstdcli \n");
Yann Collet4c414942019-05-28 16:37:03 -0700850 DISPLAY( " -P# : sample compressibility (default : %.1f%%)\n", COMPRESSIBILITY_DEFAULT * 100);
Yann Collet78056a32019-05-30 10:13:56 -0700851 DISPLAY( " -B# : sample size (default : %u)\n", (unsigned)kSampleSizeDefault);
Yann Collet4c414942019-05-28 16:37:03 -0700852 DISPLAY( " -i# : iteration loops [1-9](default : %i)\n", NBLOOPS);
Yann Collet4856a002015-01-24 01:58:16 +0100853 return 0;
854}
855
Yann Collet69afc712016-03-15 21:44:39 +0100856static int badusage(const char* exename)
Yann Collet4856a002015-01-24 01:58:16 +0100857{
858 DISPLAY("Wrong parameters\n");
859 usage(exename);
Yann Collet569b81a2016-03-16 15:26:51 +0100860 return 1;
Yann Collet4856a002015-01-24 01:58:16 +0100861}
862
Yann Collet69afc712016-03-15 21:44:39 +0100863int main(int argc, const char** argv)
Yann Collet4856a002015-01-24 01:58:16 +0100864{
Yann Colletd39a25c2018-08-23 14:28:51 -0700865 int argNb, filenamesStart=0, result;
Yann Colletb0e1f392018-08-23 17:21:38 -0700866 const char* const exename = argv[0];
Yann Collet569b81a2016-03-16 15:26:51 +0100867 const char* input_filename = NULL;
Yann Collet4856a002015-01-24 01:58:16 +0100868 U32 benchNb = 0, main_pause = 0;
George Luceb4b9e2018-06-19 17:07:34 -0700869 int cLevel = DEFAULT_CLEVEL;
870 ZSTD_compressionParameters cparams = ZSTD_getCParams(cLevel, 0, 0);
Yann Collet78056a32019-05-30 10:13:56 -0700871 size_t sampleSize = kSampleSizeDefault;
Yann Collet4c414942019-05-28 16:37:03 -0700872 double compressibility = COMPRESSIBILITY_DEFAULT;
Yann Collet4856a002015-01-24 01:58:16 +0100873
Yann Collet4856a002015-01-24 01:58:16 +0100874 DISPLAY(WELCOME_MESSAGE);
Yann Collet569b81a2016-03-16 15:26:51 +0100875 if (argc<1) return badusage(exename);
Yann Collet4856a002015-01-24 01:58:16 +0100876
Yann Colletb0e1f392018-08-23 17:21:38 -0700877 for (argNb=1; argNb<argc; argNb++) {
Yann Colletd39a25c2018-08-23 14:28:51 -0700878 const char* argument = argv[argNb];
Yann Colletab3625c2019-05-29 16:18:22 -0700879 CONTROL(argument != NULL);
Yann Collet4856a002015-01-24 01:58:16 +0100880
George Luceb4b9e2018-06-19 17:07:34 -0700881 if (longCommandWArg(&argument, "--zstd=")) {
882 for ( ; ;) {
883 if (longCommandWArg(&argument, "windowLog=") || longCommandWArg(&argument, "wlog=")) { cparams.windowLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
884 if (longCommandWArg(&argument, "chainLog=") || longCommandWArg(&argument, "clog=")) { cparams.chainLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
885 if (longCommandWArg(&argument, "hashLog=") || longCommandWArg(&argument, "hlog=")) { cparams.hashLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
886 if (longCommandWArg(&argument, "searchLog=") || longCommandWArg(&argument, "slog=")) { cparams.searchLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
Yann Collete874dac2018-11-20 14:56:07 -0800887 if (longCommandWArg(&argument, "minMatch=") || longCommandWArg(&argument, "mml=")) { cparams.minMatch = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
George Luceb4b9e2018-06-19 17:07:34 -0700888 if (longCommandWArg(&argument, "targetLength=") || longCommandWArg(&argument, "tlen=")) { cparams.targetLength = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
889 if (longCommandWArg(&argument, "strategy=") || longCommandWArg(&argument, "strat=")) { cparams.strategy = (ZSTD_strategy)(readU32FromChar(&argument)); if (argument[0]==',') { argument++; continue; } else break; }
890 if (longCommandWArg(&argument, "level=") || longCommandWArg(&argument, "lvl=")) { cLevel = (int)readU32FromChar(&argument); cparams = ZSTD_getCParams(cLevel, 0, 0); if (argument[0]==',') { argument++; continue; } else break; }
891 DISPLAY("invalid compression parameter \n");
892 return 1;
893 }
Yann Collet4856a002015-01-24 01:58:16 +0100894
Yann Colletd39a25c2018-08-23 14:28:51 -0700895 /* check end of string */
George Luceb4b9e2018-06-19 17:07:34 -0700896 if (argument[0] != 0) {
Yann Colletd39a25c2018-08-23 14:28:51 -0700897 DISPLAY("invalid --zstd= format \n");
898 return 1;
George Luceb4b9e2018-06-19 17:07:34 -0700899 } else {
900 continue;
901 }
Yann Colletd39a25c2018-08-23 14:28:51 -0700902
George Luceb4b9e2018-06-19 17:07:34 -0700903 } else if (argument[0]=='-') { /* Commands (note : aggregated commands are allowed) */
904 argument++;
905 while (argument[0]!=0) {
Yann Collet4856a002015-01-24 01:58:16 +0100906
907 switch(argument[0])
908 {
Yann Colletf66d2ba2015-02-11 08:34:50 +0100909 /* Display help on usage */
Yann Collet589f0112016-10-28 15:17:38 -0700910 case 'h':
Yann Collet569b81a2016-03-16 15:26:51 +0100911 case 'H': return usage_advanced(exename);
Yann Collet4856a002015-01-24 01:58:16 +0100912
Yann Colletf66d2ba2015-02-11 08:34:50 +0100913 /* Pause at the end (hidden option) */
Yann Collet4856a002015-01-24 01:58:16 +0100914 case 'p': main_pause = 1; break;
915
Yann Colletf66d2ba2015-02-11 08:34:50 +0100916 /* Select specific algorithm to bench */
Yann Collet4856a002015-01-24 01:58:16 +0100917 case 'b':
Yann Colletd39a25c2018-08-23 14:28:51 -0700918 argument++;
919 benchNb = readU32FromChar(&argument);
920 break;
Yann Collet4856a002015-01-24 01:58:16 +0100921
Yann Collet4c414942019-05-28 16:37:03 -0700922 /* Select compression level to use */
923 case 'l':
Yann Colletd39a25c2018-08-23 14:28:51 -0700924 argument++;
Yann Collet4c414942019-05-28 16:37:03 -0700925 cLevel = (int)readU32FromChar(&argument);
926 cparams = ZSTD_getCParams(cLevel, 0, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100927 break;
928
Yann Collet569b81a2016-03-16 15:26:51 +0100929 /* Select compressibility of synthetic sample */
Yann Colletf66d2ba2015-02-11 08:34:50 +0100930 case 'P':
Yann Colletd39a25c2018-08-23 14:28:51 -0700931 argument++;
Yann Collet4c414942019-05-28 16:37:03 -0700932 compressibility = (double)readU32FromChar(&argument) / 100.;
Yann Colletf66d2ba2015-02-11 08:34:50 +0100933 break;
Yann Collet4c414942019-05-28 16:37:03 -0700934
935 /* Select size of synthetic sample */
936 case 'B':
Yann Colletd39a25c2018-08-23 14:28:51 -0700937 argument++;
Yann Collet4c414942019-05-28 16:37:03 -0700938 sampleSize = (size_t)readU32FromChar(&argument);
939 break;
940
941 /* Modify Nb Iterations */
942 case 'i':
943 argument++;
944 g_nbIterations = readU32FromChar(&argument);
George Luceb4b9e2018-06-19 17:07:34 -0700945 break;
946
Yann Colletf66d2ba2015-02-11 08:34:50 +0100947 /* Unknown command */
Yann Collet569b81a2016-03-16 15:26:51 +0100948 default : return badusage(exename);
Yann Collet4856a002015-01-24 01:58:16 +0100949 }
950 }
951 continue;
952 }
953
Yann Colletf66d2ba2015-02-11 08:34:50 +0100954 /* first provided filename is input */
Yann Colletd39a25c2018-08-23 14:28:51 -0700955 if (!input_filename) { input_filename=argument; filenamesStart=argNb; continue; }
Yann Collet4856a002015-01-24 01:58:16 +0100956 }
957
George Luceb4b9e2018-06-19 17:07:34 -0700958
959
Yann Collet569b81a2016-03-16 15:26:51 +0100960 if (filenamesStart==0) /* no input file */
Yann Collet4c414942019-05-28 16:37:03 -0700961 result = benchSample(benchNb, sampleSize, compressibility, cLevel, cparams);
Yann Collet569b81a2016-03-16 15:26:51 +0100962 else
Yann Colletd39a25c2018-08-23 14:28:51 -0700963 result = benchFiles(benchNb, argv+filenamesStart, argc-filenamesStart, cLevel, cparams);
Yann Collet4856a002015-01-24 01:58:16 +0100964
Yann Colletb5e06dc2015-07-04 23:20:56 -0800965 if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; }
Yann Collet4856a002015-01-24 01:58:16 +0100966
967 return result;
968}