blob: db00ce217b0e06aa836f03096a7b48630f752697 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Yann Collet3128e032017-09-08 00:09:23 -07002 * Copyright (c) 2015-present, 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
Yann Collet34b20ec2016-03-15 20:47:23 +010012/*_************************************
Yann Collet4856a002015-01-24 01:58:16 +010013* Includes
14**************************************/
inikep13c84242016-05-05 13:58:56 +020015#include "util.h" /* Compiler options, UTIL_GetFileSize */
16#include <stdlib.h> /* malloc */
17#include <stdio.h> /* fprintf, fopen, ftello64 */
Yann Collet4856a002015-01-24 01:58:16 +010018
Yann Collete7e5a8c2017-06-29 18:56:24 -070019#include "mem.h" /* U32 */
Przemyslaw Skibinskicc388702016-11-21 13:58:58 +010020#ifndef ZSTD_DLL_IMPORT
21 #include "zstd_internal.h" /* ZSTD_blockHeaderSize, blockType_e, KB, MB */
Przemyslaw Skibinskicc388702016-11-21 13:58:58 +010022#else
23 #define KB *(1 <<10)
24 #define MB *(1 <<20)
25 #define GB *(1U<<30)
Yann Collet952d06f2017-02-27 17:58:02 -080026 typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e;
Przemyslaw Skibinskicc388702016-11-21 13:58:58 +010027#endif
cyan4973b5bb7c62017-06-29 19:59:37 -070028#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressBegin, ZSTD_compressContinue, etc. */
Yann Collete7e5a8c2017-06-29 18:56:24 -070029#include "zstd.h" /* ZSTD_versionString */
30#include "util.h" /* time functions */
Yann Colletfb98fd02015-02-10 18:15:20 +010031#include "datagen.h"
Yann Collet4856a002015-01-24 01:58:16 +010032
33
Yann Collet34b20ec2016-03-15 20:47:23 +010034/*_************************************
Yann Collet4856a002015-01-24 01:58:16 +010035* Constants
36**************************************/
Yann Colletfd9d6b82015-10-26 00:06:36 +010037#define PROGRAM_DESCRIPTION "Zstandard speed analyzer"
Yann Collet4856a002015-01-24 01:58:16 +010038#define AUTHOR "Yann Collet"
Yann Collete7e5a8c2017-06-29 18:56:24 -070039#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 +010040
Yann Collet4856a002015-01-24 01:58:16 +010041#define NBLOOPS 6
Yann Colletcdabd4a2016-03-17 16:18:36 +010042#define TIMELOOP_S 2
Yann Collet4856a002015-01-24 01:58:16 +010043
44#define KNUTH 2654435761U
45#define MAX_MEM (1984 MB)
Yann Collet4856a002015-01-24 01:58:16 +010046
Yann Colletf66d2ba2015-02-11 08:34:50 +010047#define COMPRESSIBILITY_DEFAULT 0.50
Yann Collet569b81a2016-03-16 15:26:51 +010048static const size_t g_sampleSize = 10000000;
Yann Collet4856a002015-01-24 01:58:16 +010049
50
Yann Collet34b20ec2016-03-15 20:47:23 +010051/*_************************************
Yann Collet4856a002015-01-24 01:58:16 +010052* Macros
53**************************************/
54#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
55
56
Yann Collet34b20ec2016-03-15 20:47:23 +010057/*_************************************
Yann Collet4856a002015-01-24 01:58:16 +010058* Benchmark Parameters
59**************************************/
Yann Collet699b14d2016-03-17 19:37:33 +010060static U32 g_nbIterations = NBLOOPS;
Yann Colletf66d2ba2015-02-11 08:34:50 +010061static double g_compressibility = COMPRESSIBILITY_DEFAULT;
Yann Collet4856a002015-01-24 01:58:16 +010062
Yann Collet699b14d2016-03-17 19:37:33 +010063static void BMK_SetNbIterations(U32 nbLoops)
Yann Collet4856a002015-01-24 01:58:16 +010064{
Yann Collet699b14d2016-03-17 19:37:33 +010065 g_nbIterations = nbLoops;
66 DISPLAY("- %i iterations -\n", g_nbIterations);
Yann Collet4856a002015-01-24 01:58:16 +010067}
68
69
Yann Collet34b20ec2016-03-15 20:47:23 +010070/*_*******************************************************
Yann Collet4856a002015-01-24 01:58:16 +010071* Private functions
72*********************************************************/
Yann Collet4856a002015-01-24 01:58:16 +010073static size_t BMK_findMaxMem(U64 requiredMem)
74{
Yann Colletb58c6852016-03-25 20:29:35 +010075 size_t const step = 64 MB;
Yann Collet34b20ec2016-03-15 20:47:23 +010076 void* testmem = NULL;
Yann Collet4856a002015-01-24 01:58:16 +010077
78 requiredMem = (((requiredMem >> 26) + 1) << 26);
79 if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
80
Yann Collet699b14d2016-03-17 19:37:33 +010081 requiredMem += step;
82 do {
Yann Collet34b20ec2016-03-15 20:47:23 +010083 testmem = malloc ((size_t)requiredMem);
Yann Collet4856a002015-01-24 01:58:16 +010084 requiredMem -= step;
Yann Collet699b14d2016-03-17 19:37:33 +010085 } while (!testmem);
Yann Collet4856a002015-01-24 01:58:16 +010086
87 free (testmem);
Yann Collet699b14d2016-03-17 19:37:33 +010088 return (size_t) requiredMem;
Yann Collet4856a002015-01-24 01:58:16 +010089}
90
91
Yann Collet34b20ec2016-03-15 20:47:23 +010092/*_*******************************************************
Yann Collet4856a002015-01-24 01:58:16 +010093* Benchmark wrappers
94*********************************************************/
Yann Collet4856a002015-01-24 01:58:16 +010095size_t local_ZSTD_compress(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
96{
97 (void)buff2;
Yann Collet5be2dd22015-11-11 13:43:58 +010098 return ZSTD_compress(dst, dstSize, src, srcSize, 1);
Yann Collet4856a002015-01-24 01:58:16 +010099}
100
Yann Collet31922d72016-03-16 16:05:18 +0100101static size_t g_cSize = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100102size_t local_ZSTD_decompress(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
103{
104 (void)src; (void)srcSize;
105 return ZSTD_decompress(dst, dstSize, buff2, g_cSize);
106}
107
Przemyslaw Skibinskicc388702016-11-21 13:58:58 +0100108static ZSTD_DCtx* g_zdc = NULL;
cyan4973b5bb7c62017-06-29 19:59:37 -0700109
110#ifndef ZSTD_DLL_IMPORT
Yann Colletcfdeb342015-11-12 16:00:04 +0100111extern size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* ctx, const void* src, size_t srcSize);
Yann Collet4856a002015-01-24 01:58:16 +0100112size_t local_ZSTD_decodeLiteralsBlock(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
113{
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100114 (void)src; (void)srcSize; (void)dst; (void)dstSize;
Yann Collet31922d72016-03-16 16:05:18 +0100115 return ZSTD_decodeLiteralsBlock((ZSTD_DCtx*)g_zdc, buff2, g_cSize);
Yann Collet4856a002015-01-24 01:58:16 +0100116}
117
Yann Collet0be21d72016-09-13 17:33:47 +0200118extern size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeq, const void* src, size_t srcSize);
Yann Collet4856a002015-01-24 01:58:16 +0100119size_t local_ZSTD_decodeSeqHeaders(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
120{
Yann Colletf3cb79b2015-08-20 00:02:43 +0100121 int nbSeq;
Yann Collet4856a002015-01-24 01:58:16 +0100122 (void)src; (void)srcSize; (void)dst; (void)dstSize;
Yann Collet0be21d72016-09-13 17:33:47 +0200123 return ZSTD_decodeSeqHeaders(g_zdc, &nbSeq, buff2, g_cSize);
Yann Collet4856a002015-01-24 01:58:16 +0100124}
Przemyslaw Skibinski179555c2016-11-15 18:05:46 +0100125#endif
Yann Collet4856a002015-01-24 01:58:16 +0100126
Yann Collet589f0112016-10-28 15:17:38 -0700127static ZSTD_CStream* g_cstream= NULL;
128size_t local_ZSTD_compressStream(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
Yann Collet34b20ec2016-03-15 20:47:23 +0100129{
Yann Collet589f0112016-10-28 15:17:38 -0700130 ZSTD_outBuffer buffOut;
131 ZSTD_inBuffer buffIn;
Yann Collet34b20ec2016-03-15 20:47:23 +0100132 (void)buff2;
Yann Collet589f0112016-10-28 15:17:38 -0700133 ZSTD_initCStream(g_cstream, 1);
134 buffOut.dst = dst;
135 buffOut.size = dstCapacity;
136 buffOut.pos = 0;
137 buffIn.src = src;
138 buffIn.size = srcSize;
139 buffIn.pos = 0;
140 ZSTD_compressStream(g_cstream, &buffOut, &buffIn);
141 ZSTD_endStream(g_cstream, &buffOut);
142 return buffOut.pos;
Yann Collet34b20ec2016-03-15 20:47:23 +0100143}
Yann Collet4856a002015-01-24 01:58:16 +0100144
Yann Collet2e84bec2017-06-29 13:03:10 -0700145static size_t local_ZSTD_compress_generic_end(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
146{
147 ZSTD_outBuffer buffOut;
148 ZSTD_inBuffer buffIn;
149 (void)buff2;
150 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
151 buffOut.dst = dst;
152 buffOut.size = dstCapacity;
153 buffOut.pos = 0;
154 buffIn.src = src;
155 buffIn.size = srcSize;
156 buffIn.pos = 0;
157 ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end);
158 return buffOut.pos;
159}
160
161static size_t local_ZSTD_compress_generic_continue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
162{
163 ZSTD_outBuffer buffOut;
164 ZSTD_inBuffer buffIn;
165 (void)buff2;
166 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
167 buffOut.dst = dst;
168 buffOut.size = dstCapacity;
169 buffOut.pos = 0;
170 buffIn.src = src;
171 buffIn.size = srcSize;
172 buffIn.pos = 0;
173 ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
174 ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end);
175 return buffOut.pos;
176}
177
Yann Collete7e5a8c2017-06-29 18:56:24 -0700178static size_t local_ZSTD_compress_generic_T2_end(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
179{
180 ZSTD_outBuffer buffOut;
181 ZSTD_inBuffer buffIn;
182 (void)buff2;
183 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
184 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_nbThreads, 2);
185 buffOut.dst = dst;
186 buffOut.size = dstCapacity;
187 buffOut.pos = 0;
188 buffIn.src = src;
189 buffIn.size = srcSize;
190 buffIn.pos = 0;
191 while (ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end)) {}
192 return buffOut.pos;
193}
194
195static size_t local_ZSTD_compress_generic_T2_continue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
196{
197 ZSTD_outBuffer buffOut;
198 ZSTD_inBuffer buffIn;
199 (void)buff2;
200 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
201 ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_nbThreads, 2);
202 buffOut.dst = dst;
203 buffOut.size = dstCapacity;
204 buffOut.pos = 0;
205 buffIn.src = src;
206 buffIn.size = srcSize;
207 buffIn.pos = 0;
208 ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_continue);
209 while(ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end)) {}
210 return buffOut.pos;
211}
212
Yann Collet589f0112016-10-28 15:17:38 -0700213static ZSTD_DStream* g_dstream= NULL;
214static size_t local_ZSTD_decompressStream(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
Yann Collet34b20ec2016-03-15 20:47:23 +0100215{
Yann Collet589f0112016-10-28 15:17:38 -0700216 ZSTD_outBuffer buffOut;
217 ZSTD_inBuffer buffIn;
Yann Collet34b20ec2016-03-15 20:47:23 +0100218 (void)src; (void)srcSize;
Yann Collet589f0112016-10-28 15:17:38 -0700219 ZSTD_initDStream(g_dstream);
220 buffOut.dst = dst;
221 buffOut.size = dstCapacity;
222 buffOut.pos = 0;
223 buffIn.src = buff2;
224 buffIn.size = g_cSize;
225 buffIn.pos = 0;
226 ZSTD_decompressStream(g_dstream, &buffOut, &buffIn);
227 return buffOut.pos;
Yann Collet34b20ec2016-03-15 20:47:23 +0100228}
229
Yann Collet31922d72016-03-16 16:05:18 +0100230static ZSTD_CCtx* g_zcc = NULL;
cyan4973b5bb7c62017-06-29 19:59:37 -0700231
232#ifndef ZSTD_DLL_IMPORT
Yann Collet31922d72016-03-16 16:05:18 +0100233size_t local_ZSTD_compressContinue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
234{
Yann Collet31922d72016-03-16 16:05:18 +0100235 (void)buff2;
236 ZSTD_compressBegin(g_zcc, 1);
Yann Collet62470b42016-07-28 15:29:08 +0200237 return ZSTD_compressEnd(g_zcc, dst, dstCapacity, src, srcSize);
Yann Collet31922d72016-03-16 16:05:18 +0100238}
239
Przemyslaw Skibinski4beb51f2016-12-20 10:17:21 +0100240#define FIRST_BLOCK_SIZE 8
241size_t local_ZSTD_compressContinue_extDict(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
242{
243 BYTE firstBlockBuf[FIRST_BLOCK_SIZE];
244
245 (void)buff2;
246 memcpy(firstBlockBuf, src, FIRST_BLOCK_SIZE);
247 ZSTD_compressBegin(g_zcc, 1);
248
249 { size_t const compressResult = ZSTD_compressContinue(g_zcc, dst, dstCapacity, firstBlockBuf, FIRST_BLOCK_SIZE);
250 if (ZSTD_isError(compressResult)) { DISPLAY("local_ZSTD_compressContinue_extDict error : %s\n", ZSTD_getErrorName(compressResult)); return compressResult; }
251 dst = (BYTE*)dst + compressResult;
252 dstCapacity -= compressResult;
253 }
254 return ZSTD_compressEnd(g_zcc, dst, dstCapacity, (const BYTE*)src + FIRST_BLOCK_SIZE, srcSize - FIRST_BLOCK_SIZE);
255}
256
Yann Collet31922d72016-03-16 16:05:18 +0100257size_t local_ZSTD_decompressContinue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
258{
259 size_t regeneratedSize = 0;
260 const BYTE* ip = (const BYTE*)buff2;
261 const BYTE* const iend = ip + g_cSize;
262 BYTE* op = (BYTE*)dst;
263 size_t remainingCapacity = dstCapacity;
264
265 (void)src; (void)srcSize;
266 ZSTD_decompressBegin(g_zdc);
267 while (ip < iend) {
268 size_t const iSize = ZSTD_nextSrcSizeToDecompress(g_zdc);
269 size_t const decodedSize = ZSTD_decompressContinue(g_zdc, op, remainingCapacity, ip, iSize);
270 ip += iSize;
271 regeneratedSize += decodedSize;
272 op += decodedSize;
273 remainingCapacity -= decodedSize;
274 }
275
276 return regeneratedSize;
277}
Przemyslaw Skibinskicc388702016-11-21 13:58:58 +0100278#endif
Yann Collet34b20ec2016-03-15 20:47:23 +0100279
280
281/*_*******************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100282* Bench functions
283*********************************************************/
Yann Collet569b81a2016-03-16 15:26:51 +0100284static size_t benchMem(const void* src, size_t srcSize, U32 benchNb)
Yann Collet4856a002015-01-24 01:58:16 +0100285{
286 BYTE* dstBuff;
Yann Colletf8e7b532016-07-23 16:31:49 +0200287 size_t const dstBuffSize = ZSTD_compressBound(srcSize);
288 void* buff2;
Yann Collet4856a002015-01-24 01:58:16 +0100289 const char* benchName;
290 size_t (*benchFunction)(void* dst, size_t dstSize, void* verifBuff, const void* src, size_t srcSize);
291 double bestTime = 100000000.;
Yann Collet4856a002015-01-24 01:58:16 +0100292
Yann Colletfb98fd02015-02-10 18:15:20 +0100293 /* Selection */
Yann Collet4856a002015-01-24 01:58:16 +0100294 switch(benchNb)
295 {
296 case 1:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700297 benchFunction = local_ZSTD_compress; benchName = "compress(1)";
Yann Collet4856a002015-01-24 01:58:16 +0100298 break;
Yann Colletf141eaa2016-03-15 21:13:52 +0100299 case 2:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700300 benchFunction = local_ZSTD_decompress; benchName = "decompress";
Yann Collet4856a002015-01-24 01:58:16 +0100301 break;
Przemyslaw Skibinskicc388702016-11-21 13:58:58 +0100302#ifndef ZSTD_DLL_IMPORT
Yann Collet31922d72016-03-16 16:05:18 +0100303 case 11:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700304 benchFunction = local_ZSTD_compressContinue; benchName = "compressContinue(1)";
Yann Collet31922d72016-03-16 16:05:18 +0100305 break;
306 case 12:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700307 benchFunction = local_ZSTD_compressContinue_extDict; benchName = "compressContinue_extDict";
Przemyslaw Skibinski4beb51f2016-12-20 10:17:21 +0100308 break;
309 case 13:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700310 benchFunction = local_ZSTD_decompressContinue; benchName = "decompressContinue";
Yann Collet31922d72016-03-16 16:05:18 +0100311 break;
Nick Terrell5152fb22017-03-29 18:51:58 -0700312 case 31:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700313 benchFunction = local_ZSTD_decodeLiteralsBlock; benchName = "decodeLiteralsBlock";
Yann Collet4856a002015-01-24 01:58:16 +0100314 break;
315 case 32:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700316 benchFunction = local_ZSTD_decodeSeqHeaders; benchName = "decodeSeqHeaders";
Yann Collet4856a002015-01-24 01:58:16 +0100317 break;
Przemyslaw Skibinski179555c2016-11-15 18:05:46 +0100318#endif
Nick Terrell5152fb22017-03-29 18:51:58 -0700319 case 41:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700320 benchFunction = local_ZSTD_compressStream; benchName = "compressStream(1)";
Yann Collet34b20ec2016-03-15 20:47:23 +0100321 break;
322 case 42:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700323 benchFunction = local_ZSTD_decompressStream; benchName = "decompressStream";
Yann Collet34b20ec2016-03-15 20:47:23 +0100324 break;
Yann Collet2e84bec2017-06-29 13:03:10 -0700325 case 51:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700326 benchFunction = local_ZSTD_compress_generic_continue; benchName = "compress_generic, continue";
Yann Collet2e84bec2017-06-29 13:03:10 -0700327 break;
328 case 52:
Yann Collete7e5a8c2017-06-29 18:56:24 -0700329 benchFunction = local_ZSTD_compress_generic_end; benchName = "compress_generic, end";
330 break;
331 case 61:
332 benchFunction = local_ZSTD_compress_generic_T2_continue; benchName = "compress_generic, -T2, continue";
333 break;
334 case 62:
335 benchFunction = local_ZSTD_compress_generic_T2_end; benchName = "compress_generic, -T2, end";
Yann Collet2e84bec2017-06-29 13:03:10 -0700336 break;
Yann Collet4856a002015-01-24 01:58:16 +0100337 default :
338 return 0;
339 }
340
341 /* Allocation */
Yann Collet213089c2015-06-18 07:43:16 -0800342 dstBuff = (BYTE*)malloc(dstBuffSize);
Yann Colletf8e7b532016-07-23 16:31:49 +0200343 buff2 = malloc(dstBuffSize);
Yann Collet34b20ec2016-03-15 20:47:23 +0100344 if ((!dstBuff) || (!buff2)) {
Yann Collet4856a002015-01-24 01:58:16 +0100345 DISPLAY("\nError: not enough memory!\n");
346 free(dstBuff); free(buff2);
347 return 12;
348 }
Yann Collet2e84bec2017-06-29 13:03:10 -0700349 if (g_zcc==NULL) g_zcc = ZSTD_createCCtx();
350 if (g_zdc==NULL) g_zdc = ZSTD_createDCtx();
351 if (g_cstream==NULL) g_cstream = ZSTD_createCStream();
352 if (g_dstream==NULL) g_dstream = ZSTD_createDStream();
Yann Collet4856a002015-01-24 01:58:16 +0100353
354 /* Preparation */
355 switch(benchNb)
356 {
Yann Colletf141eaa2016-03-15 21:13:52 +0100357 case 2:
Yann Collet5be2dd22015-11-11 13:43:58 +0100358 g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
Yann Collet4856a002015-01-24 01:58:16 +0100359 break;
Przemyslaw Skibinskicc388702016-11-21 13:58:58 +0100360#ifndef ZSTD_DLL_IMPORT
Przemyslaw Skibinski4beb51f2016-12-20 10:17:21 +0100361 case 13 :
Yann Collet4856a002015-01-24 01:58:16 +0100362 g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
363 break;
Yann Colletfb98fd02015-02-10 18:15:20 +0100364 case 31: /* ZSTD_decodeLiteralsBlock */
Yann Collet31922d72016-03-16 16:05:18 +0100365 { blockProperties_t bp;
Yann Collet542c9df2017-05-09 15:46:07 -0700366 ZSTD_frameHeader zfp;
Yann Collet512220a2016-03-17 16:42:16 +0100367 size_t frameHeaderSize, skippedSize;
Yann Collet5be2dd22015-11-11 13:43:58 +0100368 g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1);
Yann Collet542c9df2017-05-09 15:46:07 -0700369 frameHeaderSize = ZSTD_getFrameHeader(&zfp, dstBuff, ZSTD_frameHeaderSize_min);
Yann Collet512220a2016-03-17 16:42:16 +0100370 if (frameHeaderSize==0) frameHeaderSize = ZSTD_frameHeaderSize_min;
371 ZSTD_getcBlockSize(dstBuff+frameHeaderSize, dstBuffSize, &bp); /* Get 1st block type */
Yann Collet34b20ec2016-03-15 20:47:23 +0100372 if (bp.blockType != bt_compressed) {
Yann Collet759433d2015-01-24 13:31:55 +0100373 DISPLAY("ZSTD_decodeLiteralsBlock : impossible to test on this sample (not compressible)\n");
Yann Collet93a823c2015-11-13 15:08:43 +0100374 goto _cleanOut;
Yann Collet759433d2015-01-24 13:31:55 +0100375 }
Yann Colletf8e7b532016-07-23 16:31:49 +0200376 skippedSize = frameHeaderSize + ZSTD_blockHeaderSize;
Yann Collet512220a2016-03-17 16:42:16 +0100377 memcpy(buff2, dstBuff+skippedSize, g_cSize-skippedSize);
Yann Collet88fcd292015-11-25 14:42:45 +0100378 srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */
Yann Colletea1f50b2017-09-27 13:51:05 -0700379 ZSTD_decompressBegin(g_zdc);
Yann Collet4856a002015-01-24 01:58:16 +0100380 break;
381 }
Yann Colletfb98fd02015-02-10 18:15:20 +0100382 case 32: /* ZSTD_decodeSeqHeaders */
Yann Collet31922d72016-03-16 16:05:18 +0100383 { blockProperties_t bp;
Yann Collet542c9df2017-05-09 15:46:07 -0700384 ZSTD_frameHeader zfp;
Yann Collet4856a002015-01-24 01:58:16 +0100385 const BYTE* ip = dstBuff;
386 const BYTE* iend;
Yann Collet512220a2016-03-17 16:42:16 +0100387 size_t frameHeaderSize, cBlockSize;
Yann Collet88fcd292015-11-25 14:42:45 +0100388 ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1); /* it would be better to use direct block compression here */
Yann Collet512220a2016-03-17 16:42:16 +0100389 g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1);
Yann Collet542c9df2017-05-09 15:46:07 -0700390 frameHeaderSize = ZSTD_getFrameHeader(&zfp, dstBuff, ZSTD_frameHeaderSize_min);
Yann Collet512220a2016-03-17 16:42:16 +0100391 if (frameHeaderSize==0) frameHeaderSize = ZSTD_frameHeaderSize_min;
392 ip += frameHeaderSize; /* Skip frame Header */
393 cBlockSize = ZSTD_getcBlockSize(ip, dstBuffSize, &bp); /* Get 1st block type */
Yann Collet69afc712016-03-15 21:44:39 +0100394 if (bp.blockType != bt_compressed) {
Yann Collet759433d2015-01-24 13:31:55 +0100395 DISPLAY("ZSTD_decodeSeqHeaders : impossible to test on this sample (not compressible)\n");
Yann Collet93a823c2015-11-13 15:08:43 +0100396 goto _cleanOut;
Yann Collet759433d2015-01-24 13:31:55 +0100397 }
Yann Colletf8e7b532016-07-23 16:31:49 +0200398 iend = ip + ZSTD_blockHeaderSize + cBlockSize; /* End of first block */
399 ip += ZSTD_blockHeaderSize; /* skip block header */
Yann Collet0be21d72016-09-13 17:33:47 +0200400 ZSTD_decompressBegin(g_zdc);
Yann Colletf8e7b532016-07-23 16:31:49 +0200401 ip += ZSTD_decodeLiteralsBlock(g_zdc, ip, iend-ip); /* skip literal segment */
Yann Collet4856a002015-01-24 01:58:16 +0100402 g_cSize = iend-ip;
Yann Collet88fcd292015-11-25 14:42:45 +0100403 memcpy(buff2, ip, g_cSize); /* copy rest of block (it starts by SeqHeader) */
404 srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */
Yann Collet4856a002015-01-24 01:58:16 +0100405 break;
406 }
Przemyslaw Skibinski811b34d2016-11-15 19:02:39 +0100407#else
408 case 31:
409 goto _cleanOut;
Przemyslaw Skibinski179555c2016-11-15 18:05:46 +0100410#endif
Yann Collet34b20ec2016-03-15 20:47:23 +0100411 case 42 :
Yann Collet34b20ec2016-03-15 20:47:23 +0100412 g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
413 break;
Yann Collet4856a002015-01-24 01:58:16 +0100414
415 /* test functions */
Yann Collet88fcd292015-11-25 14:42:45 +0100416 /* by convention, test functions can be added > 100 */
Yann Collet4856a002015-01-24 01:58:16 +0100417
Yann Collet4856a002015-01-24 01:58:16 +0100418 default : ;
419 }
420
Yann Collet759433d2015-01-24 13:31:55 +0100421 { size_t i; for (i=0; i<dstBuffSize; i++) dstBuff[i]=(BYTE)i; } /* warming up memory */
422
Yann Collet589f0112016-10-28 15:17:38 -0700423 { U32 loopNb;
Yann Collete7e5a8c2017-06-29 18:56:24 -0700424# define TIME_SEC_MICROSEC (1*1000000ULL) /* 1 second */
425 U64 const clockLoop = TIMELOOP_S * TIME_SEC_MICROSEC;
Yann Collet952d06f2017-02-27 17:58:02 -0800426 DISPLAY("%2i- %-30.30s : \r", benchNb, benchName);
Yann Collet589f0112016-10-28 15:17:38 -0700427 for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
Yann Collete7e5a8c2017-06-29 18:56:24 -0700428 UTIL_time_t clockStart;
Yann Collet589f0112016-10-28 15:17:38 -0700429 size_t benchResult=0;
Yann Collet83d0c762017-05-15 17:15:46 -0700430 U32 nbRounds;
Yann Collet4856a002015-01-24 01:58:16 +0100431
Yann Collete7e5a8c2017-06-29 18:56:24 -0700432 UTIL_sleepMilli(1); /* give processor time to other processes */
Yann Colletc95c0c92017-09-12 18:12:46 -0700433 UTIL_waitForNextTick();
434 clockStart = UTIL_getTime();
435 for (nbRounds=0; UTIL_clockSpanMicro(clockStart) < clockLoop; nbRounds++) {
Yann Collet589f0112016-10-28 15:17:38 -0700436 benchResult = benchFunction(dstBuff, dstBuffSize, buff2, src, srcSize);
437 if (ZSTD_isError(benchResult)) { DISPLAY("ERROR ! %s() => %s !! \n", benchName, ZSTD_getErrorName(benchResult)); exit(1); }
438 }
Yann Colletc95c0c92017-09-12 18:12:46 -0700439 { U64 const clockSpanMicro = UTIL_clockSpanMicro(clockStart);
Yann Collete7e5a8c2017-06-29 18:56:24 -0700440 double const averageTime = (double)clockSpanMicro / TIME_SEC_MICROSEC / nbRounds;
Yann Collet83d0c762017-05-15 17:15:46 -0700441 if (averageTime < bestTime) bestTime = averageTime;
442 DISPLAY("%2i- %-30.30s : %7.1f MB/s (%9u)\r", loopNb, benchName, (double)srcSize / (1 MB) / bestTime, (U32)benchResult);
443 } } }
Yann Colletcdabd4a2016-03-17 16:18:36 +0100444 DISPLAY("%2u\n", benchNb);
Yann Collet4856a002015-01-24 01:58:16 +0100445
Yann Collet93a823c2015-11-13 15:08:43 +0100446_cleanOut:
Yann Collet4856a002015-01-24 01:58:16 +0100447 free(dstBuff);
448 free(buff2);
cyan49734b263062017-07-01 08:03:59 -0700449 ZSTD_freeCCtx(g_zcc); g_zcc=NULL;
450 ZSTD_freeDCtx(g_zdc); g_zdc=NULL;
451 ZSTD_freeCStream(g_cstream); g_cstream=NULL;
452 ZSTD_freeDStream(g_dstream); g_dstream=NULL;
Yann Collet4856a002015-01-24 01:58:16 +0100453 return 0;
454}
455
456
Yann Collet569b81a2016-03-16 15:26:51 +0100457static int benchSample(U32 benchNb)
Yann Collet4856a002015-01-24 01:58:16 +0100458{
Yann Collet569b81a2016-03-16 15:26:51 +0100459 size_t const benchedSize = g_sampleSize;
Yann Colletf66d2ba2015-02-11 08:34:50 +0100460 const char* name = "Sample 10MiB";
Yann Collet4856a002015-01-24 01:58:16 +0100461
462 /* Allocation */
Yann Collet69afc712016-03-15 21:44:39 +0100463 void* origBuff = malloc(benchedSize);
464 if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); return 12; }
Yann Collet4856a002015-01-24 01:58:16 +0100465
466 /* Fill buffer */
Yann Colletf66d2ba2015-02-11 08:34:50 +0100467 RDG_genBuffer(origBuff, benchedSize, g_compressibility, 0.0, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100468
469 /* bench */
470 DISPLAY("\r%79s\r", "");
471 DISPLAY(" %s : \n", name);
472 if (benchNb)
473 benchMem(origBuff, benchedSize, benchNb);
474 else
475 for (benchNb=0; benchNb<100; benchNb++) benchMem(origBuff, benchedSize, benchNb);
476
477 free(origBuff);
478 return 0;
479}
480
481
Yann Collet569b81a2016-03-16 15:26:51 +0100482static int benchFiles(const char** fileNamesTable, const int nbFiles, U32 benchNb)
Yann Collet4856a002015-01-24 01:58:16 +0100483{
Yann Collet4856a002015-01-24 01:58:16 +0100484 /* Loop for each file */
Yann Collet569b81a2016-03-16 15:26:51 +0100485 int fileIdx;
486 for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
487 const char* inFileName = fileNamesTable[fileIdx];
Yann Collet69afc712016-03-15 21:44:39 +0100488 FILE* inFile = fopen( inFileName, "rb" );
Yann Collet4856a002015-01-24 01:58:16 +0100489 U64 inFileSize;
490 size_t benchedSize;
Yann Collet69afc712016-03-15 21:44:39 +0100491 void* origBuff;
Yann Collet4856a002015-01-24 01:58:16 +0100492
493 /* Check file existence */
Yann Collet69afc712016-03-15 21:44:39 +0100494 if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
Yann Collet4856a002015-01-24 01:58:16 +0100495
Yann Collet695a6cb2015-12-10 15:51:38 +0100496 /* Memory allocation & restrictions */
inikep69fcd7c2016-04-28 12:23:33 +0200497 inFileSize = UTIL_getFileSize(inFileName);
Yann Collet69afc712016-03-15 21:44:39 +0100498 benchedSize = BMK_findMaxMem(inFileSize*3) / 3;
Yann Collet4856a002015-01-24 01:58:16 +0100499 if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
500 if (benchedSize < inFileSize)
Yann Collet569b81a2016-03-16 15:26:51 +0100501 DISPLAY("Not enough memory for '%s' full size; testing %u MB only...\n", inFileName, (U32)(benchedSize>>20));
Yann Collet4856a002015-01-24 01:58:16 +0100502
Yann Collet18a68712015-01-24 12:08:52 +0100503 /* Alloc */
Yann Collet69afc712016-03-15 21:44:39 +0100504 origBuff = malloc(benchedSize);
505 if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); fclose(inFile); return 12; }
Yann Collet4856a002015-01-24 01:58:16 +0100506
Yann Collet18a68712015-01-24 12:08:52 +0100507 /* Fill input buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100508 DISPLAY("Loading %s... \r", inFileName);
Yann Collet4856a002015-01-24 01:58:16 +0100509 {
Yann Collet569b81a2016-03-16 15:26:51 +0100510 size_t readSize = fread(origBuff, 1, benchedSize, inFile);
511 fclose(inFile);
512 if (readSize != benchedSize) {
513 DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
514 free(origBuff);
515 return 13;
516 } }
Yann Collet4856a002015-01-24 01:58:16 +0100517
Yann Collet18a68712015-01-24 12:08:52 +0100518 /* bench */
Yann Collet4856a002015-01-24 01:58:16 +0100519 DISPLAY("\r%79s\r", "");
520 DISPLAY(" %s : \n", inFileName);
521 if (benchNb)
522 benchMem(origBuff, benchedSize, benchNb);
523 else
524 for (benchNb=0; benchNb<100; benchNb++) benchMem(origBuff, benchedSize, benchNb);
Yann Collet569b81a2016-03-16 15:26:51 +0100525
526 free(origBuff);
Yann Collet4856a002015-01-24 01:58:16 +0100527 }
528
529 return 0;
530}
531
532
Yann Collet69afc712016-03-15 21:44:39 +0100533static int usage(const char* exename)
Yann Collet4856a002015-01-24 01:58:16 +0100534{
535 DISPLAY( "Usage :\n");
536 DISPLAY( " %s [arg] file1 file2 ... fileX\n", exename);
537 DISPLAY( "Arguments :\n");
538 DISPLAY( " -H/-h : Help (this text + advanced options)\n");
539 return 0;
540}
541
Yann Collet569b81a2016-03-16 15:26:51 +0100542static int usage_advanced(const char* exename)
Yann Collet4856a002015-01-24 01:58:16 +0100543{
Yann Collet569b81a2016-03-16 15:26:51 +0100544 usage(exename);
Yann Collet4856a002015-01-24 01:58:16 +0100545 DISPLAY( "\nAdvanced options :\n");
546 DISPLAY( " -b# : test only function # \n");
547 DISPLAY( " -i# : iteration loops [1-9](default : %i)\n", NBLOOPS);
Yann Colletf66d2ba2015-02-11 08:34:50 +0100548 DISPLAY( " -P# : sample compressibility (default : %.1f%%)\n", COMPRESSIBILITY_DEFAULT * 100);
Yann Collet4856a002015-01-24 01:58:16 +0100549 return 0;
550}
551
Yann Collet69afc712016-03-15 21:44:39 +0100552static int badusage(const char* exename)
Yann Collet4856a002015-01-24 01:58:16 +0100553{
554 DISPLAY("Wrong parameters\n");
555 usage(exename);
Yann Collet569b81a2016-03-16 15:26:51 +0100556 return 1;
Yann Collet4856a002015-01-24 01:58:16 +0100557}
558
Yann Collet69afc712016-03-15 21:44:39 +0100559int main(int argc, const char** argv)
Yann Collet4856a002015-01-24 01:58:16 +0100560{
Yann Collet569b81a2016-03-16 15:26:51 +0100561 int i, filenamesStart=0, result;
562 const char* exename = argv[0];
563 const char* input_filename = NULL;
Yann Collet4856a002015-01-24 01:58:16 +0100564 U32 benchNb = 0, main_pause = 0;
565
Yann Collet4856a002015-01-24 01:58:16 +0100566 DISPLAY(WELCOME_MESSAGE);
Yann Collet569b81a2016-03-16 15:26:51 +0100567 if (argc<1) return badusage(exename);
Yann Collet4856a002015-01-24 01:58:16 +0100568
Yann Collet569b81a2016-03-16 15:26:51 +0100569 for(i=1; i<argc; i++) {
Yann Collet69afc712016-03-15 21:44:39 +0100570 const char* argument = argv[i];
Yann Collet569b81a2016-03-16 15:26:51 +0100571 if(!argument) continue; /* Protection if argument empty */
Yann Collet4856a002015-01-24 01:58:16 +0100572
Yann Collet569b81a2016-03-16 15:26:51 +0100573 /* Commands (note : aggregated commands are allowed) */
574 if (argument[0]=='-') {
Yann Collet4856a002015-01-24 01:58:16 +0100575
Yann Collet569b81a2016-03-16 15:26:51 +0100576 while (argument[1]!=0) {
577 argument++;
Yann Collet4856a002015-01-24 01:58:16 +0100578
579 switch(argument[0])
580 {
Yann Colletf66d2ba2015-02-11 08:34:50 +0100581 /* Display help on usage */
Yann Collet589f0112016-10-28 15:17:38 -0700582 case 'h':
Yann Collet569b81a2016-03-16 15:26:51 +0100583 case 'H': return usage_advanced(exename);
Yann Collet4856a002015-01-24 01:58:16 +0100584
Yann Colletf66d2ba2015-02-11 08:34:50 +0100585 /* Pause at the end (hidden option) */
Yann Collet4856a002015-01-24 01:58:16 +0100586 case 'p': main_pause = 1; break;
587
Yann Colletf66d2ba2015-02-11 08:34:50 +0100588 /* Select specific algorithm to bench */
Yann Collet4856a002015-01-24 01:58:16 +0100589 case 'b':
590 benchNb = 0;
Yann Collet69afc712016-03-15 21:44:39 +0100591 while ((argument[1]>= '0') && (argument[1]<= '9')) {
Yann Collet4856a002015-01-24 01:58:16 +0100592 benchNb *= 10;
593 benchNb += argument[1] - '0';
594 argument++;
595 }
596 break;
597
Yann Colletf66d2ba2015-02-11 08:34:50 +0100598 /* Modify Nb Iterations */
Yann Collet4856a002015-01-24 01:58:16 +0100599 case 'i':
Yann Collet69afc712016-03-15 21:44:39 +0100600 if ((argument[1] >='0') && (argument[1] <='9')) {
Yann Collet4856a002015-01-24 01:58:16 +0100601 int iters = argument[1] - '0';
602 BMK_SetNbIterations(iters);
603 argument++;
604 }
605 break;
606
Yann Collet569b81a2016-03-16 15:26:51 +0100607 /* Select compressibility of synthetic sample */
Yann Colletf66d2ba2015-02-11 08:34:50 +0100608 case 'P':
Yann Collet69afc712016-03-15 21:44:39 +0100609 { U32 proba32 = 0;
610 while ((argument[1]>= '0') && (argument[1]<= '9')) {
Yann Colletf66d2ba2015-02-11 08:34:50 +0100611 proba32 *= 10;
612 proba32 += argument[1] - '0';
613 argument++;
614 }
615 g_compressibility = (double)proba32 / 100.;
616 }
617 break;
618
619 /* Unknown command */
Yann Collet569b81a2016-03-16 15:26:51 +0100620 default : return badusage(exename);
Yann Collet4856a002015-01-24 01:58:16 +0100621 }
622 }
623 continue;
624 }
625
Yann Colletf66d2ba2015-02-11 08:34:50 +0100626 /* first provided filename is input */
Yann Collet4856a002015-01-24 01:58:16 +0100627 if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
628 }
629
Yann Collet569b81a2016-03-16 15:26:51 +0100630 if (filenamesStart==0) /* no input file */
Yann Collet4856a002015-01-24 01:58:16 +0100631 result = benchSample(benchNb);
Yann Collet569b81a2016-03-16 15:26:51 +0100632 else
633 result = benchFiles(argv+filenamesStart, argc-filenamesStart, benchNb);
Yann Collet4856a002015-01-24 01:58:16 +0100634
Yann Colletb5e06dc2015-07-04 23:20:56 -0800635 if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; }
Yann Collet4856a002015-01-24 01:58:16 +0100636
637 return result;
638}