blob: cc9b4b7082e5cb4fd76bc0f8a9a7144967a0519c [file] [log] [blame]
Yann Collet4856a002015-01-24 01:58:16 +01001/*
Yann Collet99909862016-04-09 16:17:18 +02002 bench.c - open-source compression benchmark module
3 Copyright (C) Yann Collet 2012-2016
Yann Collet4856a002015-01-24 01:58:16 +01004
5 GPL v2 License
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 You can contact the author at :
Yann Collet99909862016-04-09 16:17:18 +020022 - zstd homepage : http://www.zstd.net
Yann Collet4856a002015-01-24 01:58:16 +010023 - zstd source repository : https://github.com/Cyan4973/zstd
Yann Collet4856a002015-01-24 01:58:16 +010024*/
25
Yann Colletf3eca252015-10-22 15:31:46 +010026/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010027* Includes
Yann Colletf3eca252015-10-22 15:31:46 +010028***************************************/
inikep957823f2016-05-25 15:30:55 +020029#include "util.h" /* Compiler options, UTIL_GetFileSize, UTIL_sleep */
Yann Collet4856a002015-01-24 01:58:16 +010030#include <stdlib.h> /* malloc, free */
31#include <string.h> /* memset */
Yann Colleteeb8ba12015-10-22 16:55:40 +010032#include <stdio.h> /* fprintf, fopen, ftello64 */
inikep4c12f232016-03-29 14:52:13 +020033
Yann Colletf3eca252015-10-22 15:31:46 +010034#include "mem.h"
Yann Colletd3b7f8d2016-06-04 19:47:02 +020035#define ZSTD_STATIC_LINKING_ONLY
36#include "zstd.h"
inikep69fcd7c2016-04-28 12:23:33 +020037#include "datagen.h" /* RDG_genBuffer */
Yann Collet4856a002015-01-24 01:58:16 +010038#include "xxhash.h"
39
40
Yann Collet4856a002015-01-24 01:58:16 +010041
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
inikep83c76b42016-04-28 13:16:01 +020051#define NBLOOPS 3
52#define TIMELOOP_MICROSEC 1*1000000ULL /* 1 second */
53#define ACTIVEPERIOD_MICROSEC 70*1000000ULL /* 70 seconds */
54#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 Colletd062f132015-12-01 01:31:17 +010060static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31));
Yann Collet4856a002015-01-24 01:58:16 +010061
62static U32 g_compressibilityDefault = 50;
Yann Collet4856a002015-01-24 01:58:16 +010063
64
Yann Colletf3eca252015-10-22 15:31:46 +010065/* *************************************
Yann Colleted699e62015-12-16 02:37:24 +010066* console display
Yann Colletf3eca252015-10-22 15:31:46 +010067***************************************/
Yann Colleted699e62015-12-16 02:37:24 +010068#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
69#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
70static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
71
72
73/* *************************************
74* Exceptions
75***************************************/
76#ifndef DEBUG
77# define DEBUG 0
78#endif
79#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
80#define EXM_THROW(error, ...) \
81{ \
82 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
83 DISPLAYLEVEL(1, "Error %i : ", error); \
84 DISPLAYLEVEL(1, __VA_ARGS__); \
85 DISPLAYLEVEL(1, "\n"); \
86 exit(error); \
87}
Yann Collet4856a002015-01-24 01:58:16 +010088
89
Yann Colletf3eca252015-10-22 15:31:46 +010090/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010091* Benchmark Parameters
Yann Colletf3eca252015-10-22 15:31:46 +010092***************************************/
Yann Collet1d1ae402016-03-17 19:51:02 +010093static U32 g_nbIterations = NBLOOPS;
Yann Collet1c00dc32015-10-21 08:22:25 +010094static size_t g_blockSize = 0;
inikepeaba91a2016-03-23 20:30:26 +010095int g_additionalParam = 0;
Yann Collet4856a002015-01-24 01:58:16 +010096
inikep4e26bb62016-03-14 12:48:51 +010097void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; }
98
inikepeaba91a2016-03-23 20:30:26 +010099void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; }
100
Yann Collet1d1ae402016-03-17 19:51:02 +0100101void BMK_SetNbIterations(unsigned nbLoops)
Yann Collet4856a002015-01-24 01:58:16 +0100102{
Yann Collet1d1ae402016-03-17 19:51:02 +0100103 g_nbIterations = nbLoops;
inikep2872b6f2016-03-22 14:38:34 +0100104 DISPLAYLEVEL(2, "- %i iterations -\n", g_nbIterations);
Yann Collet4856a002015-01-24 01:58:16 +0100105}
106
Yann Collet1c00dc32015-10-21 08:22:25 +0100107void BMK_SetBlockSize(size_t blockSize)
108{
109 g_blockSize = blockSize;
inikep4e26bb62016-03-14 12:48:51 +0100110 DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10));
Yann Collet1c00dc32015-10-21 08:22:25 +0100111}
112
Yann Collet4856a002015-01-24 01:58:16 +0100113
Yann Colletf3eca252015-10-22 15:31:46 +0100114/* ********************************************************
Yann Collet4856a002015-01-24 01:58:16 +0100115* Bench functions
Yann Colletf3eca252015-10-22 15:31:46 +0100116**********************************************************/
Yann Collet1c00dc32015-10-21 08:22:25 +0100117typedef struct
Yann Collet4856a002015-01-24 01:58:16 +0100118{
Yann Colleted699e62015-12-16 02:37:24 +0100119 const char* srcPtr;
Yann Collet1c00dc32015-10-21 08:22:25 +0100120 size_t srcSize;
121 char* cPtr;
122 size_t cRoom;
123 size_t cSize;
124 char* resPtr;
125 size_t resSize;
126} blockParam_t;
127
inikep4e26bb62016-03-14 12:48:51 +0100128typedef struct
129{
inikepc034b732016-03-14 13:13:42 +0100130 double ratio;
inikep4e26bb62016-03-14 12:48:51 +0100131 size_t cSize;
inikepc034b732016-03-14 13:13:42 +0100132 double cSpeed;
133 double dSpeed;
inikep4e26bb62016-03-14 12:48:51 +0100134} benchResult_t;
135
Yann Collet99909862016-04-09 16:17:18 +0200136
Yann Colletbe2010e2015-10-31 12:57:14 +0100137#define MIN(a,b) ((a)<(b) ? (a) : (b))
Yann Collet2ce49232016-02-02 14:36:49 +0100138#define MAX(a,b) ((a)>(b) ? (a) : (b))
Yann Collet1c00dc32015-10-21 08:22:25 +0100139
Yann Colleted699e62015-12-16 02:37:24 +0100140static int BMK_benchMem(const void* srcBuffer, size_t srcSize,
inikep472638c2016-03-23 12:28:28 +0100141 const char* displayName, int cLevel,
Yann Collet31683c02015-12-18 01:26:48 +0100142 const size_t* fileSizes, U32 nbFiles,
inikep4e26bb62016-03-14 12:48:51 +0100143 const void* dictBuffer, size_t dictBufferSize, benchResult_t *result)
Yann Collet1c00dc32015-10-21 08:22:25 +0100144{
inikep38654982016-04-21 12:18:47 +0200145 size_t const blockSize = (g_blockSize>=32 ? g_blockSize : srcSize) + (!srcSize); /* avoid div by 0 */
Yann Colletd64f4352016-03-21 00:07:42 +0100146 U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles;
Yann Colleted699e62015-12-16 02:37:24 +0100147 blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t));
Yann Colletb9151402016-03-26 17:18:11 +0100148 size_t const maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */
Yann Collet1c00dc32015-10-21 08:22:25 +0100149 void* const compressedBuffer = malloc(maxCompressedSize);
150 void* const resultBuffer = malloc(srcSize);
Yann Collet31683c02015-12-18 01:26:48 +0100151 ZSTD_CCtx* ctx = ZSTD_createCCtx();
152 ZSTD_DCtx* dctx = ZSTD_createDCtx();
Yann Colletde406ee2016-03-20 15:46:10 +0100153 U32 nbBlocks;
inikep83c76b42016-04-28 13:16:01 +0200154 UTIL_time_t ticksPerSecond;
Yann Colletde406ee2016-03-20 15:46:10 +0100155
156 /* checks */
Yann Colletec224d22016-06-27 13:39:30 +0200157 if (!compressedBuffer || !resultBuffer || !blockTable || !ctx || !dctx)
Yann Colleted699e62015-12-16 02:37:24 +0100158 EXM_THROW(31, "not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100159
Yann Collet4856a002015-01-24 01:58:16 +0100160 /* init */
161 if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */
inikepaaaf9232016-05-09 16:19:25 +0200162 UTIL_initTimer(&ticksPerSecond);
inikep4c12f232016-03-29 14:52:13 +0200163
Yann Collet1c00dc32015-10-21 08:22:25 +0100164 /* Init blockTable data */
Yann Colletde406ee2016-03-20 15:46:10 +0100165 { const char* srcPtr = (const char*)srcBuffer;
Yann Collet1c00dc32015-10-21 08:22:25 +0100166 char* cPtr = (char*)compressedBuffer;
167 char* resPtr = (char*)resultBuffer;
Yann Collet699b14d2016-03-17 19:37:33 +0100168 U32 fileNb;
Yann Colletde406ee2016-03-20 15:46:10 +0100169 for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) {
Yann Collet70611352015-12-16 03:01:03 +0100170 size_t remaining = fileSizes[fileNb];
Yann Collet699b14d2016-03-17 19:37:33 +0100171 U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize);
172 U32 const blockEnd = nbBlocks + nbBlocksforThisFile;
Yann Colletfd416f12016-01-30 03:14:15 +0100173 for ( ; nbBlocks<blockEnd; nbBlocks++) {
Yann Colletde406ee2016-03-20 15:46:10 +0100174 size_t const thisBlockSize = MIN(remaining, blockSize);
Yann Colleted699e62015-12-16 02:37:24 +0100175 blockTable[nbBlocks].srcPtr = srcPtr;
176 blockTable[nbBlocks].cPtr = cPtr;
177 blockTable[nbBlocks].resPtr = resPtr;
178 blockTable[nbBlocks].srcSize = thisBlockSize;
179 blockTable[nbBlocks].cRoom = ZSTD_compressBound(thisBlockSize);
180 srcPtr += thisBlockSize;
181 cPtr += blockTable[nbBlocks].cRoom;
182 resPtr += thisBlockSize;
183 remaining -= thisBlockSize;
Yann Colletfd416f12016-01-30 03:14:15 +0100184 } } }
Yann Collet1c00dc32015-10-21 08:22:25 +0100185
Yann Collet4856a002015-01-24 01:58:16 +0100186 /* warmimg up memory */
Yann Colletd062f132015-12-01 01:31:17 +0100187 RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1);
Yann Collet4856a002015-01-24 01:58:16 +0100188
189 /* Bench */
inikep6d157f12016-04-15 16:54:11 +0200190 { U64 fastestC = (U64)(-1LL), fastestD = (U64)(-1LL);
Yann Colletb9151402016-03-26 17:18:11 +0100191 U64 const crcOrig = XXH64(srcBuffer, srcSize, 0);
inikep83c76b42016-04-28 13:16:01 +0200192 UTIL_time_t coolTime;
Yann Colletde406ee2016-03-20 15:46:10 +0100193 U32 testNb;
inikep19bd48f2016-04-04 12:10:00 +0200194 size_t cSize = 0;
195 double ratio = 0.;
Yann Collet4856a002015-01-24 01:58:16 +0100196
inikepaaaf9232016-05-09 16:19:25 +0200197 UTIL_getTime(&coolTime);
inikep4e26bb62016-03-14 12:48:51 +0100198 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Colletde406ee2016-03-20 15:46:10 +0100199 for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) {
inikep83c76b42016-04-28 13:16:01 +0200200 UTIL_time_t clockStart;
201 U64 clockLoop = g_nbIterations ? TIMELOOP_MICROSEC : 1;
Yann Collet4856a002015-01-24 01:58:16 +0100202
Yann Collet27d3dad2016-03-11 13:41:20 +0100203 /* overheat protection */
inikep83c76b42016-04-28 13:16:01 +0200204 if (UTIL_clockSpanMicro(coolTime, ticksPerSecond) > ACTIVEPERIOD_MICROSEC) {
Yann Collet27d3dad2016-03-11 13:41:20 +0100205 DISPLAY("\rcooling down ... \r");
inikep83c76b42016-04-28 13:16:01 +0200206 UTIL_sleep(COOLPERIOD_SEC);
inikepaaaf9232016-05-09 16:19:25 +0200207 UTIL_getTime(&coolTime);
Yann Collet27d3dad2016-03-11 13:41:20 +0100208 }
Yann Collet4856a002015-01-24 01:58:16 +0100209
210 /* Compression */
inikep2872b6f2016-03-22 14:38:34 +0100211 DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->\r", testNb, displayName, (U32)srcSize);
Yann Collet699b14d2016-03-17 19:37:33 +0100212 memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100213
inikep83c76b42016-04-28 13:16:01 +0200214 UTIL_sleepMilli(1); /* give processor time to other processes */
215 UTIL_waitForNextTick(ticksPerSecond);
inikepaaaf9232016-05-09 16:19:25 +0200216 UTIL_getTime(&clockStart);
Yann Colletde406ee2016-03-20 15:46:10 +0100217
inikepc5e1d292016-04-19 09:37:59 +0200218 { U32 nbLoops = 0;
Yann Colletee1a0842016-06-07 01:40:49 +0200219 ZSTD_CDict* cdict = ZSTD_createCDict(dictBuffer, dictBufferSize, cLevel);
220 if (cdict==NULL) EXM_THROW(1, "ZSTD_createCDict() allocation failure");
inikepc5e1d292016-04-19 09:37:59 +0200221 do {
Yann Colleta5b66e32016-03-26 01:48:27 +0100222 U32 blockNb;
Yann Colleta5b66e32016-03-26 01:48:27 +0100223 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
Yann Colletee1a0842016-06-07 01:40:49 +0200224 size_t const rSize = ZSTD_compress_usingCDict(ctx,
Yann Colleta5b66e32016-03-26 01:48:27 +0100225 blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
Yann Colletee1a0842016-06-07 01:40:49 +0200226 blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize,
227 cdict);
Yann Collet4c56f4a2016-06-27 13:36:54 +0200228 if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingCDict() failed : %s", ZSTD_getErrorName(rSize));
Yann Colleta5b66e32016-03-26 01:48:27 +0100229 blockTable[blockNb].cSize = rSize;
inikepc5e1d292016-04-19 09:37:59 +0200230 }
231 nbLoops++;
inikep83c76b42016-04-28 13:16:01 +0200232 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
Yann Colletee1a0842016-06-07 01:40:49 +0200233 ZSTD_freeCDict(cdict);
inikep83c76b42016-04-28 13:16:01 +0200234 { U64 const clockSpan = UTIL_clockSpanMicro(clockStart, ticksPerSecond);
inikep6d157f12016-04-15 16:54:11 +0200235 if (clockSpan < fastestC*nbLoops) fastestC = clockSpan / nbLoops;
Yann Colletde406ee2016-03-20 15:46:10 +0100236 } }
Yann Collet4856a002015-01-24 01:58:16 +0100237
Yann Collet1c00dc32015-10-21 08:22:25 +0100238 cSize = 0;
Yann Colletde406ee2016-03-20 15:46:10 +0100239 { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; }
Yann Collet2acb5d32015-10-29 16:49:43 +0100240 ratio = (double)srcSize / (double)cSize;
inikep2872b6f2016-03-22 14:38:34 +0100241 DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r",
Yann Colletde406ee2016-03-20 15:46:10 +0100242 testNb, displayName, (U32)srcSize, (U32)cSize, ratio,
inikep06f793a2016-03-29 11:17:58 +0200243 (double)srcSize / fastestC );
Yann Collet4856a002015-01-24 01:58:16 +0100244
Yann Colleta5b66e32016-03-26 01:48:27 +0100245 (void)fastestD; (void)crcOrig; /* unused when decompression disabled */
Yann Collete93d6ce2016-01-31 00:58:06 +0100246#if 1
Yann Collet4856a002015-01-24 01:58:16 +0100247 /* Decompression */
Yann Collet7b51a292016-01-26 15:58:49 +0100248 memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */
Yann Collet4856a002015-01-24 01:58:16 +0100249
inikep83c76b42016-04-28 13:16:01 +0200250 UTIL_sleepMilli(1); /* give processor time to other processes */
251 UTIL_waitForNextTick(ticksPerSecond);
inikepaaaf9232016-05-09 16:19:25 +0200252 UTIL_getTime(&clockStart);
Yann Collet7b51a292016-01-26 15:58:49 +0100253
inikepc5e1d292016-04-19 09:37:59 +0200254 { U32 nbLoops = 0;
Yann Colletee1a0842016-06-07 01:40:49 +0200255 ZSTD_DDict* ddict = ZSTD_createDDict(dictBuffer, dictBufferSize);
256 if (!ddict) EXM_THROW(2, "ZSTD_createDDict() allocation failure");
inikepc5e1d292016-04-19 09:37:59 +0200257 do {
Yann Colleta5b66e32016-03-26 01:48:27 +0100258 U32 blockNb;
Yann Colleta5b66e32016-03-26 01:48:27 +0100259 for (blockNb=0; blockNb<nbBlocks; blockNb++) {
Yann Colletee1a0842016-06-07 01:40:49 +0200260 size_t const regenSize = ZSTD_decompress_usingDDict(dctx,
Yann Colleta5b66e32016-03-26 01:48:27 +0100261 blockTable[blockNb].resPtr, blockTable[blockNb].srcSize,
Yann Colletee1a0842016-06-07 01:40:49 +0200262 blockTable[blockNb].cPtr, blockTable[blockNb].cSize,
263 ddict);
Yann Colleta5b66e32016-03-26 01:48:27 +0100264 if (ZSTD_isError(regenSize)) {
Yann Collet4c56f4a2016-06-27 13:36:54 +0200265 DISPLAY("ZSTD_decompress_usingDDict() failed on block %u : %s \n",
Yann Colleta5b66e32016-03-26 01:48:27 +0100266 blockNb, ZSTD_getErrorName(regenSize));
inikep19bd48f2016-04-04 12:10:00 +0200267 clockLoop = 0; /* force immediate test end */
Yann Colleta5b66e32016-03-26 01:48:27 +0100268 break;
269 }
270 blockTable[blockNb].resSize = regenSize;
inikepc5e1d292016-04-19 09:37:59 +0200271 }
272 nbLoops++;
inikep83c76b42016-04-28 13:16:01 +0200273 } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop);
Yann Colletee1a0842016-06-07 01:40:49 +0200274 ZSTD_freeDDict(ddict);
inikep83c76b42016-04-28 13:16:01 +0200275 { U64 const clockSpan = UTIL_clockSpanMicro(clockStart, ticksPerSecond);
inikep6d157f12016-04-15 16:54:11 +0200276 if (clockSpan < fastestD*nbLoops) fastestD = clockSpan / nbLoops;
Yann Collet7b51a292016-01-26 15:58:49 +0100277 } }
278
inikep2872b6f2016-03-22 14:38:34 +0100279 DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r",
Yann Colletde406ee2016-03-20 15:46:10 +0100280 testNb, displayName, (U32)srcSize, (U32)cSize, ratio,
inikep06f793a2016-03-29 11:17:58 +0200281 (double)srcSize / fastestC,
282 (double)srcSize / fastestD );
Yann Collet4856a002015-01-24 01:58:16 +0100283
284 /* CRC Checking */
Yann Collete162ace2016-05-20 11:24:35 +0200285 { U64 const crcCheck = XXH64(resultBuffer, srcSize, 0);
Yann Colleta5b66e32016-03-26 01:48:27 +0100286 if (crcOrig!=crcCheck) {
287 size_t u;
288 DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck);
289 for (u=0; u<srcSize; u++) {
290 if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) {
291 U32 segNb, bNb, pos;
292 size_t bacc = 0;
293 DISPLAY("Decoding error at pos %u ", (U32)u);
294 for (segNb = 0; segNb < nbBlocks; segNb++) {
295 if (bacc + blockTable[segNb].srcSize > u) break;
296 bacc += blockTable[segNb].srcSize;
297 }
298 pos = (U32)(u - bacc);
299 bNb = pos / (128 KB);
300 DISPLAY("(block %u, sub %u, pos %u) \n", segNb, bNb, pos);
301 break;
Yann Collet03a6dab2016-01-21 02:21:17 +0100302 }
Yann Colleta5b66e32016-03-26 01:48:27 +0100303 if (u==srcSize-1) { /* should never happen */
304 DISPLAY("no difference detected\n");
305 } }
306 break;
307 } } /* CRC Checking */
Yann Collete8c6bb12015-07-26 00:23:57 +0100308#endif
Yann Colletde406ee2016-03-20 15:46:10 +0100309 } /* for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) */
Yann Collet4856a002015-01-24 01:58:16 +0100310
Yann Collete162ace2016-05-20 11:24:35 +0200311 result->ratio = ratio;
312 result->cSize = cSize;
313 result->cSpeed = (double)srcSize / fastestC;
314 result->dSpeed = (double)srcSize / fastestD;
inikep2872b6f2016-03-22 14:38:34 +0100315 DISPLAYLEVEL(2, "%2i#\n", cLevel);
Yann Colletde406ee2016-03-20 15:46:10 +0100316 } /* Bench */
Yann Collet4856a002015-01-24 01:58:16 +0100317
Yann Colleted699e62015-12-16 02:37:24 +0100318 /* clean up */
inikep0bd0fae2016-05-05 13:10:57 +0200319 free(blockTable);
Yann Collet4856a002015-01-24 01:58:16 +0100320 free(compressedBuffer);
321 free(resultBuffer);
Yann Collet31683c02015-12-18 01:26:48 +0100322 ZSTD_freeCCtx(ctx);
323 ZSTD_freeDCtx(dctx);
Yann Collet4856a002015-01-24 01:58:16 +0100324 return 0;
325}
326
327
Yann Collet4856a002015-01-24 01:58:16 +0100328static size_t BMK_findMaxMem(U64 requiredMem)
329{
Yann Colletde406ee2016-03-20 15:46:10 +0100330 size_t const step = 64 MB;
Yann Collet4856a002015-01-24 01:58:16 +0100331 BYTE* testmem = NULL;
332
333 requiredMem = (((requiredMem >> 26) + 1) << 26);
Yann Colletde406ee2016-03-20 15:46:10 +0100334 requiredMem += step;
Yann Collet050efba2015-11-03 09:49:30 +0100335 if (requiredMem > maxMemory) requiredMem = maxMemory;
Yann Collet4856a002015-01-24 01:58:16 +0100336
Yann Colletde406ee2016-03-20 15:46:10 +0100337 do {
Yann Collet4856a002015-01-24 01:58:16 +0100338 testmem = (BYTE*)malloc((size_t)requiredMem);
Yann Colletde406ee2016-03-20 15:46:10 +0100339 requiredMem -= step;
340 } while (!testmem);
341
Yann Collet4856a002015-01-24 01:58:16 +0100342 free(testmem);
Yann Colletde406ee2016-03-20 15:46:10 +0100343 return (size_t)(requiredMem);
Yann Collet4856a002015-01-24 01:58:16 +0100344}
345
Yann Colleted699e62015-12-16 02:37:24 +0100346static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize,
inikepeaba91a2016-03-23 20:30:26 +0100347 const char* displayName, int cLevel, int cLevelLast,
Yann Collet31683c02015-12-18 01:26:48 +0100348 const size_t* fileSizes, unsigned nbFiles,
349 const void* dictBuffer, size_t dictBufferSize)
Yann Collet4856a002015-01-24 01:58:16 +0100350{
inikep4e26bb62016-03-14 12:48:51 +0100351 benchResult_t result, total;
inikepe9554b72016-03-14 18:10:30 +0100352 int l;
inikep4e26bb62016-03-14 12:48:51 +0100353
inikepe9554b72016-03-14 18:10:30 +0100354 const char* pch = strrchr(displayName, '\\'); /* Windows */
355 if (!pch) pch = strrchr(displayName, '/'); /* Linux */
356 if (pch) displayName = pch+1;
inikep4e26bb62016-03-14 12:48:51 +0100357
inikepea4ee3e2016-04-25 13:09:06 +0200358 SET_HIGH_PRIORITY;
359
inikepe9554b72016-03-14 18:10:30 +0100360 memset(&result, 0, sizeof(result));
361 memset(&total, 0, sizeof(total));
inikep5fdd0b42016-03-14 19:51:11 +0100362
inikepeaba91a2016-03-23 20:30:26 +0100363 if (g_displayLevel == 1 && !g_additionalParam)
inikepd7d251c2016-06-22 16:13:25 +0200364 DISPLAY("bench %s %s: input %u bytes, %i iterations, %u KB blocks\n", ZSTD_VERSION_STRING, ZSTD_GIT_COMMIT_STRING, (U32)benchedSize, g_nbIterations, (U32)(g_blockSize>>10));
inikepe9554b72016-03-14 18:10:30 +0100365
366 if (cLevelLast < cLevel) cLevelLast = cLevel;
367
Yann Collet99909862016-04-09 16:17:18 +0200368 for (l=cLevel; l <= cLevelLast; l++) {
inikepe9554b72016-03-14 18:10:30 +0100369 BMK_benchMem(srcBuffer, benchedSize,
inikep472638c2016-03-23 12:28:28 +0100370 displayName, l,
inikepe9554b72016-03-14 18:10:30 +0100371 fileSizes, nbFiles,
372 dictBuffer, dictBufferSize, &result);
373 if (g_displayLevel == 1) {
inikepeaba91a2016-03-23 20:30:26 +0100374 if (g_additionalParam)
inikep38654982016-04-21 12:18:47 +0200375 DISPLAY("%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s (param=%d)\n", -l, (int)result.cSize, result.ratio, result.cSpeed, result.dSpeed, displayName, g_additionalParam);
inikepd700a1a2016-03-15 12:18:44 +0100376 else
inikep38654982016-04-21 12:18:47 +0200377 DISPLAY("%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", -l, (int)result.cSize, result.ratio, result.cSpeed, result.dSpeed, displayName);
inikepe9554b72016-03-14 18:10:30 +0100378 total.cSize += result.cSize;
379 total.cSpeed += result.cSpeed;
380 total.dSpeed += result.dSpeed;
381 total.ratio += result.ratio;
Yann Collet99909862016-04-09 16:17:18 +0200382 } }
383 if (g_displayLevel == 1 && cLevelLast > cLevel) {
inikepe9554b72016-03-14 18:10:30 +0100384 total.cSize /= 1+cLevelLast-cLevel;
385 total.cSpeed /= 1+cLevelLast-cLevel;
386 total.dSpeed /= 1+cLevelLast-cLevel;
387 total.ratio /= 1+cLevelLast-cLevel;
inikep38654982016-04-21 12:18:47 +0200388 DISPLAY("avg%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", (int)total.cSize, total.ratio, total.cSpeed, total.dSpeed, displayName);
inikepe9554b72016-03-14 18:10:30 +0100389 }
Yann Colleted699e62015-12-16 02:37:24 +0100390}
391
Yann Colleted699e62015-12-16 02:37:24 +0100392
Yann Colleta5b66e32016-03-26 01:48:27 +0100393/*! BMK_loadFiles() :
394 Loads `buffer` with content of files listed within `fileNamesTable`.
395 At most, fills `buffer` entirely */
Yann Collet70611352015-12-16 03:01:03 +0100396static void BMK_loadFiles(void* buffer, size_t bufferSize,
397 size_t* fileSizes,
Yann Colleta5b66e32016-03-26 01:48:27 +0100398 const char** fileNamesTable, unsigned nbFiles)
Yann Colleted699e62015-12-16 02:37:24 +0100399{
inikepc0d5f4e2016-04-13 10:48:04 +0200400 size_t pos = 0, totalSize = 0;
Yann Collet699b14d2016-03-17 19:37:33 +0100401 unsigned n;
Yann Colletfd416f12016-01-30 03:14:15 +0100402 for (n=0; n<nbFiles; n++) {
Yann Collete162ace2016-05-20 11:24:35 +0200403 FILE* f;
inikep69fcd7c2016-04-28 12:23:33 +0200404 U64 fileSize = UTIL_getFileSize(fileNamesTable[n]);
405 if (UTIL_isDirectory(fileNamesTable[n])) {
inikepc0d5f4e2016-04-13 10:48:04 +0200406 DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]);
inikepbab43172016-04-29 15:19:40 +0200407 fileSizes[n] = 0;
inikepc0d5f4e2016-04-13 10:48:04 +0200408 continue;
409 }
inikepea4ee3e2016-04-25 13:09:06 +0200410 f = fopen(fileNamesTable[n], "rb");
Yann Colleted699e62015-12-16 02:37:24 +0100411 if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]);
412 DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[n]);
Yann Colleta5b66e32016-03-26 01:48:27 +0100413 if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */
414 { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f);
415 if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]);
416 pos += readSize; }
Yann Colleta52c98d2015-12-16 03:12:31 +0100417 fileSizes[n] = (size_t)fileSize;
inikepc0d5f4e2016-04-13 10:48:04 +0200418 totalSize += (size_t)fileSize;
Yann Colleted699e62015-12-16 02:37:24 +0100419 fclose(f);
420 }
Yann Collet6f9c0562016-05-01 10:26:30 +0200421
inikepc0d5f4e2016-04-13 10:48:04 +0200422 if (totalSize == 0) EXM_THROW(12, "no data to bench");
Yann Colleted699e62015-12-16 02:37:24 +0100423}
424
Yann Collet31683c02015-12-18 01:26:48 +0100425static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles,
inikepeaba91a2016-03-23 20:30:26 +0100426 const char* dictFileName, int cLevel, int cLevelLast)
Yann Colleted699e62015-12-16 02:37:24 +0100427{
428 void* srcBuffer;
429 size_t benchedSize;
Yann Collet31683c02015-12-18 01:26:48 +0100430 void* dictBuffer = NULL;
431 size_t dictBufferSize = 0;
432 size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t));
Yann Collete162ace2016-05-20 11:24:35 +0200433 U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +0100434 char mfName[20] = {0};
Yann Colleted699e62015-12-16 02:37:24 +0100435
Yann Collet31683c02015-12-18 01:26:48 +0100436 if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes");
437
438 /* Load dictionary */
Yann Colletfd416f12016-01-30 03:14:15 +0100439 if (dictFileName != NULL) {
inikep69fcd7c2016-04-28 12:23:33 +0200440 U64 dictFileSize = UTIL_getFileSize(dictFileName);
Yann Collet31683c02015-12-18 01:26:48 +0100441 if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName);
442 dictBufferSize = (size_t)dictFileSize;
443 dictBuffer = malloc(dictBufferSize);
444 if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize);
445 BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1);
446 }
447
Yann Colleted699e62015-12-16 02:37:24 +0100448 /* Memory allocation & restrictions */
449 benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3;
450 if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad;
451 if (benchedSize < totalSizeToLoad)
452 DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20));
453 srcBuffer = malloc(benchedSize);
454 if (!srcBuffer) EXM_THROW(12, "not enough memory");
455
456 /* Load input buffer */
Yann Collet70611352015-12-16 03:01:03 +0100457 BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles);
Yann Colleted699e62015-12-16 02:37:24 +0100458
459 /* Bench */
460 snprintf (mfName, sizeof(mfName), " %u files", nbFiles);
Yann Collete162ace2016-05-20 11:24:35 +0200461 { const char* displayName = (nbFiles > 1) ? mfName : fileNamesTable[0];
462 BMK_benchCLevel(srcBuffer, benchedSize,
463 displayName, cLevel, cLevelLast,
464 fileSizes, nbFiles,
465 dictBuffer, dictBufferSize);
466 }
Yann Collet4856a002015-01-24 01:58:16 +0100467
Yann Colleteeb8ba12015-10-22 16:55:40 +0100468 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100469 free(srcBuffer);
Yann Collet31683c02015-12-18 01:26:48 +0100470 free(dictBuffer);
Yann Collet70611352015-12-16 03:01:03 +0100471 free(fileSizes);
Yann Collet4856a002015-01-24 01:58:16 +0100472}
473
474
inikepeaba91a2016-03-23 20:30:26 +0100475static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility)
Yann Collet4856a002015-01-24 01:58:16 +0100476{
Yann Colleted699e62015-12-16 02:37:24 +0100477 char name[20] = {0};
Yann Collet4856a002015-01-24 01:58:16 +0100478 size_t benchedSize = 10000000;
Yann Collete162ace2016-05-20 11:24:35 +0200479 void* const srcBuffer = malloc(benchedSize);
Yann Collet4856a002015-01-24 01:58:16 +0100480
Yann Collet4856a002015-01-24 01:58:16 +0100481 /* Memory allocation */
Yann Colleted699e62015-12-16 02:37:24 +0100482 if (!srcBuffer) EXM_THROW(21, "not enough memory");
Yann Collet4856a002015-01-24 01:58:16 +0100483
484 /* Fill input buffer */
Yann Colletd062f132015-12-01 01:31:17 +0100485 RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100486
487 /* Bench */
Yann Colleted699e62015-12-16 02:37:24 +0100488 snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100));
inikepeaba91a2016-03-23 20:30:26 +0100489 BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0);
Yann Collet4856a002015-01-24 01:58:16 +0100490
Yann Colleted699e62015-12-16 02:37:24 +0100491 /* clean up */
Yann Collet4856a002015-01-24 01:58:16 +0100492 free(srcBuffer);
Yann Collet4856a002015-01-24 01:58:16 +0100493}
494
495
Yann Collet31683c02015-12-18 01:26:48 +0100496int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
inikep957823f2016-05-25 15:30:55 +0200497 const char* dictFileName, int cLevel, int cLevelLast)
Yann Collet4856a002015-01-24 01:58:16 +0100498{
Yann Collet699b14d2016-03-17 19:37:33 +0100499 double const compressibility = (double)g_compressibilityDefault / 100;
Yann Collet4856a002015-01-24 01:58:16 +0100500
501 if (nbFiles == 0)
inikepeaba91a2016-03-23 20:30:26 +0100502 BMK_syntheticTest(cLevel, cLevelLast, compressibility);
Yann Collet4856a002015-01-24 01:58:16 +0100503 else
inikepeaba91a2016-03-23 20:30:26 +0100504 BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast);
Yann Collet4856a002015-01-24 01:58:16 +0100505 return 0;
506}