Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This source code is licensed under the BSD-style license found in the |
| 6 | * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | */ |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 9 | |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 10 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 11 | /*-************************************ |
| 12 | * Dependencies |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 13 | **************************************/ |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 14 | #include "util.h" /* Compiler options, UTIL_GetFileSize */ |
| 15 | #include <stdlib.h> /* malloc */ |
| 16 | #include <stdio.h> /* fprintf, fopen, ftello64 */ |
| 17 | #include <string.h> /* strcmp */ |
| 18 | #include <math.h> /* log */ |
| 19 | #include <time.h> /* clock_t */ |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 20 | |
| 21 | #include "mem.h" |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 22 | #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_parameters, ZSTD_estimateCCtxSize */ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 23 | #include "zstd.h" |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 24 | #include "datagen.h" |
| 25 | #include "xxhash.h" |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 26 | |
| 27 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 28 | /*-************************************ |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 29 | * Constants |
| 30 | **************************************/ |
inikep | d5ff2c3 | 2016-04-28 14:40:45 +0200 | [diff] [blame] | 31 | #define PROGRAM_DESCRIPTION "ZSTD parameters tester" |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 32 | #define AUTHOR "Yann Collet" |
Yann Collet | 45f84ab | 2016-05-20 12:34:40 +0200 | [diff] [blame] | 33 | #define WELCOME_MESSAGE "*** %s %s %i-bits, by %s (%s) ***\n", PROGRAM_DESCRIPTION, ZSTD_VERSION_STRING, (int)(sizeof(void*)*8), AUTHOR, __DATE__ |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 34 | |
| 35 | |
| 36 | #define KB *(1<<10) |
| 37 | #define MB *(1<<20) |
Yann Collet | 49cc9b7 | 2015-11-27 17:52:57 +0100 | [diff] [blame] | 38 | #define GB *(1ULL<<30) |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 39 | |
Yann Collet | b2ad30c | 2015-10-26 02:45:19 +0100 | [diff] [blame] | 40 | #define NBLOOPS 2 |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 41 | #define TIMELOOP (2 * CLOCKS_PER_SEC) |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 42 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 43 | #define NB_LEVELS_TRACKED 30 |
| 44 | |
Yann Collet | 49cc9b7 | 2015-11-27 17:52:57 +0100 | [diff] [blame] | 45 | static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31)); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 46 | |
| 47 | #define COMPRESSIBILITY_DEFAULT 0.50 |
| 48 | static const size_t sampleSize = 10000000; |
| 49 | |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 50 | static const U32 g_grillDuration_s = 60000; /* about 16 hours */ |
| 51 | static const clock_t g_maxParamTime = 15 * CLOCKS_PER_SEC; |
| 52 | static const clock_t g_maxVariationTime = 60 * CLOCKS_PER_SEC; |
Yann Collet | 2c6992e | 2015-10-27 12:18:00 +0100 | [diff] [blame] | 53 | static const int g_maxNbVariations = 64; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 54 | |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 55 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 56 | /*-************************************ |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 57 | * Macros |
| 58 | **************************************/ |
| 59 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 60 | |
| 61 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 62 | /*-************************************ |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 63 | * Benchmark Parameters |
| 64 | **************************************/ |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 65 | static U32 g_nbIterations = NBLOOPS; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 66 | static double g_compressibility = COMPRESSIBILITY_DEFAULT; |
| 67 | static U32 g_blockSize = 0; |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 68 | static U32 g_rand = 1; |
Yann Collet | 8b91abe | 2015-10-27 02:59:12 +0100 | [diff] [blame] | 69 | static U32 g_singleRun = 0; |
Yann Collet | 2c6992e | 2015-10-27 12:18:00 +0100 | [diff] [blame] | 70 | static U32 g_target = 0; |
Yann Collet | b315bc8 | 2015-10-27 13:12:25 +0100 | [diff] [blame] | 71 | static U32 g_noSeed = 0; |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 72 | static ZSTD_compressionParameters g_params = { 0, 0, 0, 0, 0, 0, ZSTD_greedy }; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 73 | |
| 74 | void BMK_SetNbIterations(int nbLoops) |
| 75 | { |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 76 | g_nbIterations = nbLoops; |
| 77 | DISPLAY("- %u iterations -\n", g_nbIterations); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 81 | /*-******************************************************* |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 82 | * Private functions |
| 83 | *********************************************************/ |
| 84 | |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 85 | static clock_t BMK_clockSpan(clock_t cStart) { return clock() - cStart; } /* works even if overflow ; max span ~ 30 mn */ |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 86 | |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 87 | static U32 BMK_timeSpan(time_t tStart) { return (U32)difftime(time(NULL), tStart); } /* accuracy in seconds only, span can be multiple years */ |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 88 | |
| 89 | |
| 90 | static size_t BMK_findMaxMem(U64 requiredMem) |
| 91 | { |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 92 | size_t const step = 64 MB; |
| 93 | void* testmem = NULL; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 94 | |
| 95 | requiredMem = (((requiredMem >> 26) + 1) << 26); |
Yann Collet | 49cc9b7 | 2015-11-27 17:52:57 +0100 | [diff] [blame] | 96 | if (requiredMem > maxMemory) requiredMem = maxMemory; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 97 | |
| 98 | requiredMem += 2*step; |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 99 | while (!testmem) { |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 100 | requiredMem -= step; |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 101 | testmem = malloc ((size_t)requiredMem); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | free (testmem); |
| 105 | return (size_t) (requiredMem - step); |
| 106 | } |
| 107 | |
| 108 | |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 109 | # define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r))) |
| 110 | U32 FUZ_rand(U32* src) |
| 111 | { |
| 112 | const U32 prime1 = 2654435761U; |
| 113 | const U32 prime2 = 2246822519U; |
| 114 | U32 rand32 = *src; |
| 115 | rand32 *= prime1; |
| 116 | rand32 += prime2; |
| 117 | rand32 = FUZ_rotl32(rand32, 13); |
| 118 | *src = rand32; |
| 119 | return rand32 >> 5; |
| 120 | } |
| 121 | |
| 122 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 123 | /*-******************************************************* |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 124 | * Bench functions |
| 125 | *********************************************************/ |
| 126 | typedef struct { |
| 127 | size_t cSize; |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 128 | double cSpeed; |
| 129 | double dSpeed; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 130 | } BMK_result_t; |
| 131 | |
| 132 | typedef struct |
| 133 | { |
| 134 | const char* srcPtr; |
| 135 | size_t srcSize; |
| 136 | char* cPtr; |
| 137 | size_t cRoom; |
| 138 | size_t cSize; |
| 139 | char* resPtr; |
| 140 | size_t resSize; |
| 141 | } blockParam_t; |
| 142 | |
| 143 | |
| 144 | #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) |
Yann Collet | b315bc8 | 2015-10-27 13:12:25 +0100 | [diff] [blame] | 145 | |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 146 | static size_t BMK_benchParam(BMK_result_t* resultPtr, |
| 147 | const void* srcBuffer, size_t srcSize, |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 148 | ZSTD_CCtx* ctx, |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 149 | const ZSTD_compressionParameters cParams) |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 150 | { |
| 151 | const size_t blockSize = g_blockSize ? g_blockSize : srcSize; |
| 152 | const U32 nbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize); |
| 153 | blockParam_t* const blockTable = (blockParam_t*) malloc(nbBlocks * sizeof(blockParam_t)); |
| 154 | const size_t maxCompressedSize = (size_t)nbBlocks * ZSTD_compressBound(blockSize); |
| 155 | void* const compressedBuffer = malloc(maxCompressedSize); |
| 156 | void* const resultBuffer = malloc(srcSize); |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 157 | ZSTD_parameters params; |
| 158 | U32 Wlog = cParams.windowLog; |
Yann Collet | 8a57b92 | 2016-04-04 13:49:18 +0200 | [diff] [blame] | 159 | U32 Clog = cParams.chainLog; |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 160 | U32 Hlog = cParams.hashLog; |
| 161 | U32 Slog = cParams.searchLog; |
| 162 | U32 Slength = cParams.searchLength; |
| 163 | U32 Tlength = cParams.targetLength; |
| 164 | ZSTD_strategy strat = cParams.strategy; |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 165 | char name[30] = { 0 }; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 166 | U64 crcOrig; |
| 167 | |
| 168 | /* Memory allocation & restrictions */ |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 169 | snprintf(name, 30, "Sw%02uc%02uh%02us%02ul%1ut%03uS%1u", Wlog, Clog, Hlog, Slog, Slength, Tlength, strat); |
| 170 | if (!compressedBuffer || !resultBuffer || !blockTable) { |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 171 | DISPLAY("\nError: not enough memory!\n"); |
| 172 | free(compressedBuffer); |
| 173 | free(resultBuffer); |
| 174 | free(blockTable); |
| 175 | return 12; |
| 176 | } |
| 177 | |
| 178 | /* Calculating input Checksum */ |
| 179 | crcOrig = XXH64(srcBuffer, srcSize, 0); |
| 180 | |
| 181 | /* Init blockTable data */ |
| 182 | { |
| 183 | U32 i; |
| 184 | size_t remaining = srcSize; |
| 185 | const char* srcPtr = (const char*)srcBuffer; |
| 186 | char* cPtr = (char*)compressedBuffer; |
| 187 | char* resPtr = (char*)resultBuffer; |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 188 | for (i=0; i<nbBlocks; i++) { |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 189 | size_t thisBlockSize = MIN(remaining, blockSize); |
| 190 | blockTable[i].srcPtr = srcPtr; |
| 191 | blockTable[i].cPtr = cPtr; |
| 192 | blockTable[i].resPtr = resPtr; |
| 193 | blockTable[i].srcSize = thisBlockSize; |
| 194 | blockTable[i].cRoom = ZSTD_compressBound(thisBlockSize); |
| 195 | srcPtr += thisBlockSize; |
| 196 | cPtr += blockTable[i].cRoom; |
| 197 | resPtr += thisBlockSize; |
| 198 | remaining -= thisBlockSize; |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 199 | } } |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 200 | |
| 201 | /* warmimg up memory */ |
| 202 | RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.10, 1); |
| 203 | |
| 204 | /* Bench */ |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 205 | { U32 loopNb; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 206 | size_t cSize = 0; |
| 207 | double fastestC = 100000000., fastestD = 100000000.; |
| 208 | double ratio = 0.; |
| 209 | U64 crcCheck = 0; |
Yann Collet | 69c2cdb | 2016-07-14 16:52:45 +0200 | [diff] [blame] | 210 | clock_t const benchStart = clock(); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 211 | |
| 212 | DISPLAY("\r%79s\r", ""); |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 213 | memset(¶ms, 0, sizeof(params)); |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 214 | params.cParams = cParams; |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 215 | for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) { |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 216 | int nbLoops; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 217 | U32 blockNb; |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 218 | clock_t roundStart, roundClock; |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 219 | |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 220 | { clock_t const benchTime = BMK_clockSpan(benchStart); |
| 221 | if (benchTime > g_maxParamTime) break; } |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 222 | |
| 223 | /* Compression */ |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 224 | DISPLAY("\r%1u-%s : %9u ->", loopNb, name, (U32)srcSize); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 225 | memset(compressedBuffer, 0xE5, maxCompressedSize); |
| 226 | |
| 227 | nbLoops = 0; |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 228 | roundStart = clock(); |
| 229 | while (clock() == roundStart); |
| 230 | roundStart = clock(); |
| 231 | while (BMK_clockSpan(roundStart) < TIMELOOP) { |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 232 | for (blockNb=0; blockNb<nbBlocks; blockNb++) |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 233 | blockTable[blockNb].cSize = ZSTD_compress_advanced(ctx, |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 234 | blockTable[blockNb].cPtr, blockTable[blockNb].cRoom, |
| 235 | blockTable[blockNb].srcPtr, blockTable[blockNb].srcSize, |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 236 | NULL, 0, |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 237 | params); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 238 | nbLoops++; |
| 239 | } |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 240 | roundClock = BMK_clockSpan(roundStart); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 241 | |
| 242 | cSize = 0; |
| 243 | for (blockNb=0; blockNb<nbBlocks; blockNb++) |
| 244 | cSize += blockTable[blockNb].cSize; |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 245 | if ((double)roundClock < fastestC * CLOCKS_PER_SEC * nbLoops) fastestC = ((double)roundClock / CLOCKS_PER_SEC) / nbLoops; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 246 | ratio = (double)srcSize / (double)cSize; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 247 | DISPLAY("\r"); |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 248 | DISPLAY("%1u-%s : %9u ->", loopNb, name, (U32)srcSize); |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 249 | DISPLAY(" %9u (%4.3f),%7.1f MB/s", (U32)cSize, ratio, (double)srcSize / fastestC / 1000000.); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 250 | resultPtr->cSize = cSize; |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 251 | resultPtr->cSpeed = (double)srcSize / fastestC; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 252 | |
| 253 | #if 1 |
| 254 | /* Decompression */ |
| 255 | memset(resultBuffer, 0xD6, srcSize); |
| 256 | |
| 257 | nbLoops = 0; |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 258 | roundStart = clock(); |
| 259 | while (clock() == roundStart); |
| 260 | roundStart = clock(); |
| 261 | for ( ; BMK_clockSpan(roundStart) < TIMELOOP; nbLoops++) { |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 262 | for (blockNb=0; blockNb<nbBlocks; blockNb++) |
| 263 | blockTable[blockNb].resSize = ZSTD_decompress(blockTable[blockNb].resPtr, blockTable[blockNb].srcSize, |
| 264 | blockTable[blockNb].cPtr, blockTable[blockNb].cSize); |
| 265 | } |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 266 | roundClock = BMK_clockSpan(roundStart); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 267 | |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 268 | if ((double)roundClock < fastestD * CLOCKS_PER_SEC * nbLoops) fastestD = ((double)roundClock / CLOCKS_PER_SEC) / nbLoops; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 269 | DISPLAY("\r"); |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 270 | DISPLAY("%1u-%s : %9u -> ", loopNb, name, (U32)srcSize); |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 271 | DISPLAY("%9u (%4.3f),%7.1f MB/s, ", (U32)cSize, ratio, (double)srcSize / fastestC / 1000000.); |
| 272 | DISPLAY("%7.1f MB/s", (double)srcSize / fastestD / 1000000.); |
| 273 | resultPtr->dSpeed = (double)srcSize / fastestD; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 274 | |
| 275 | /* CRC Checking */ |
| 276 | crcCheck = XXH64(resultBuffer, srcSize, 0); |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 277 | if (crcOrig!=crcCheck) { |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 278 | unsigned u; |
| 279 | unsigned eBlockSize = (unsigned)(MIN(65536*2, blockSize)); |
| 280 | DISPLAY("\n!!! WARNING !!! Invalid Checksum : %x != %x\n", (unsigned)crcOrig, (unsigned)crcCheck); |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 281 | for (u=0; u<srcSize; u++) { |
| 282 | if (((const BYTE*)srcBuffer)[u] != ((BYTE*)resultBuffer)[u]) { |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 283 | printf("Decoding error at pos %u (block %u, pos %u) \n", u, u / eBlockSize, u % eBlockSize); |
| 284 | break; |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 285 | } } |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 286 | break; |
| 287 | } |
| 288 | #endif |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 289 | } } |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 290 | |
| 291 | /* End cleaning */ |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 292 | DISPLAY("\r"); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 293 | free(compressedBuffer); |
| 294 | free(resultBuffer); |
| 295 | return 0; |
| 296 | } |
| 297 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 298 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 299 | const char* g_stratName[] = { "ZSTD_fast ", |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 300 | "ZSTD_dfast ", |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 301 | "ZSTD_greedy ", |
| 302 | "ZSTD_lazy ", |
| 303 | "ZSTD_lazy2 ", |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 304 | "ZSTD_btlazy2", |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 305 | "ZSTD_btopt ", |
| 306 | "ZSTD_btopt2 "}; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 307 | |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 308 | static void BMK_printWinner(FILE* f, U32 cLevel, BMK_result_t result, ZSTD_compressionParameters params, size_t srcSize) |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 309 | { |
| 310 | DISPLAY("\r%79s\r", ""); |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 311 | fprintf(f," {%3u,%3u,%3u,%3u,%3u,%3u, %s }, ", |
Yann Collet | 8a57b92 | 2016-04-04 13:49:18 +0200 | [diff] [blame] | 312 | params.windowLog, params.chainLog, params.hashLog, params.searchLog, params.searchLength, |
Yann Collet | bd828d9 | 2016-02-11 04:38:55 +0100 | [diff] [blame] | 313 | params.targetLength, g_stratName[(U32)(params.strategy)]); |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 314 | fprintf(f, |
Yann Collet | 3564487 | 2015-11-02 16:14:46 +0100 | [diff] [blame] | 315 | "/* level %2u */ /* R:%5.3f at %5.1f MB/s - %5.1f MB/s */\n", |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 316 | cLevel, (double)srcSize / result.cSize, result.cSpeed / 1000000., result.dSpeed / 1000000.); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 320 | static double g_cSpeedTarget[NB_LEVELS_TRACKED] = { 0. }; /* NB_LEVELS_TRACKED : checked at main() */ |
Yann Collet | b2ad30c | 2015-10-26 02:45:19 +0100 | [diff] [blame] | 321 | |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 322 | typedef struct { |
| 323 | BMK_result_t result; |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 324 | ZSTD_compressionParameters params; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 325 | } winnerInfo_t; |
| 326 | |
Yann Collet | 8b91abe | 2015-10-27 02:59:12 +0100 | [diff] [blame] | 327 | static void BMK_printWinners2(FILE* f, const winnerInfo_t* winners, size_t srcSize) |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 328 | { |
Yann Collet | 4110534 | 2016-07-27 15:09:11 +0200 | [diff] [blame] | 329 | int cLevel; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 330 | |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 331 | fprintf(f, "\n /* Proposed configurations : */ \n"); |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 332 | fprintf(f, " /* W, C, H, S, L, T, strat */ \n"); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 333 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 334 | for (cLevel=0; cLevel <= ZSTD_maxCLevel(); cLevel++) |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 335 | BMK_printWinner(f, cLevel, winners[cLevel].result, winners[cLevel].params, srcSize); |
| 336 | } |
| 337 | |
Yann Collet | 8b91abe | 2015-10-27 02:59:12 +0100 | [diff] [blame] | 338 | |
| 339 | static void BMK_printWinners(FILE* f, const winnerInfo_t* winners, size_t srcSize) |
| 340 | { |
| 341 | fseek(f, 0, SEEK_SET); |
| 342 | BMK_printWinners2(f, winners, srcSize); |
Yann Collet | 10ba183 | 2015-10-28 14:05:37 +0100 | [diff] [blame] | 343 | fflush(f); |
Yann Collet | 8b91abe | 2015-10-27 02:59:12 +0100 | [diff] [blame] | 344 | BMK_printWinners2(stdout, winners, srcSize); |
| 345 | } |
| 346 | |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 347 | static int BMK_seed(winnerInfo_t* winners, const ZSTD_compressionParameters params, |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 348 | const void* srcBuffer, size_t srcSize, |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 349 | ZSTD_CCtx* ctx) |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 350 | { |
| 351 | BMK_result_t testResult; |
| 352 | int better = 0; |
Yann Collet | 4110534 | 2016-07-27 15:09:11 +0200 | [diff] [blame] | 353 | int cLevel; |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 354 | |
| 355 | BMK_benchParam(&testResult, srcBuffer, srcSize, ctx, params); |
| 356 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 357 | for (cLevel = 1; cLevel <= ZSTD_maxCLevel(); cLevel++) { |
Yann Collet | 81e49e6 | 2015-10-28 15:56:48 +0100 | [diff] [blame] | 358 | if (testResult.cSpeed < g_cSpeedTarget[cLevel]) |
| 359 | continue; /* not fast enough for this level */ |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 360 | if (winners[cLevel].result.cSize==0) { |
Yann Collet | 81e49e6 | 2015-10-28 15:56:48 +0100 | [diff] [blame] | 361 | /* first solution for this cLevel */ |
| 362 | winners[cLevel].result = testResult; |
| 363 | winners[cLevel].params = params; |
| 364 | BMK_printWinner(stdout, cLevel, testResult, params, srcSize); |
| 365 | better = 1; |
| 366 | continue; |
| 367 | } |
Yann Collet | 1077bb8 | 2015-10-28 12:32:25 +0100 | [diff] [blame] | 368 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 369 | if ((double)testResult.cSize <= ((double)winners[cLevel].result.cSize * (1. + (0.02 / cLevel))) ) { |
Yann Collet | 1077bb8 | 2015-10-28 12:32:25 +0100 | [diff] [blame] | 370 | /* Validate solution is "good enough" */ |
Yann Collet | 81e49e6 | 2015-10-28 15:56:48 +0100 | [diff] [blame] | 371 | double W_ratio = (double)srcSize / testResult.cSize; |
| 372 | double O_ratio = (double)srcSize / winners[cLevel].result.cSize; |
| 373 | double W_ratioNote = log (W_ratio); |
| 374 | double O_ratioNote = log (O_ratio); |
Yann Collet | 1077bb8 | 2015-10-28 12:32:25 +0100 | [diff] [blame] | 375 | size_t W_DMemUsed = (1 << params.windowLog) + (16 KB); |
| 376 | size_t O_DMemUsed = (1 << winners[cLevel].params.windowLog) + (16 KB); |
Yann Collet | 76c5c2a | 2015-10-28 19:07:05 +0100 | [diff] [blame] | 377 | double W_DMemUsed_note = W_ratioNote * ( 40 + 9*cLevel) - log((double)W_DMemUsed); |
| 378 | double O_DMemUsed_note = O_ratioNote * ( 40 + 9*cLevel) - log((double)O_DMemUsed); |
Yann Collet | 1077bb8 | 2015-10-28 12:32:25 +0100 | [diff] [blame] | 379 | |
Yann Collet | e20d5cf | 2016-07-14 20:46:24 +0200 | [diff] [blame] | 380 | size_t W_CMemUsed = (1 << params.windowLog) + ZSTD_estimateCCtxSize(params); |
| 381 | size_t O_CMemUsed = (1 << winners[cLevel].params.windowLog) + ZSTD_estimateCCtxSize(winners[cLevel].params); |
Yann Collet | 81e49e6 | 2015-10-28 15:56:48 +0100 | [diff] [blame] | 382 | double W_CMemUsed_note = W_ratioNote * ( 50 + 13*cLevel) - log((double)W_CMemUsed); |
| 383 | double O_CMemUsed_note = O_ratioNote * ( 50 + 13*cLevel) - log((double)O_CMemUsed); |
Yann Collet | 1077bb8 | 2015-10-28 12:32:25 +0100 | [diff] [blame] | 384 | |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 385 | double W_CSpeed_note = W_ratioNote * ( 30 + 10*cLevel) + log(testResult.cSpeed); |
| 386 | double O_CSpeed_note = O_ratioNote * ( 30 + 10*cLevel) + log(winners[cLevel].result.cSpeed); |
Yann Collet | 10ba183 | 2015-10-28 14:05:37 +0100 | [diff] [blame] | 387 | |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 388 | double W_DSpeed_note = W_ratioNote * ( 20 + 2*cLevel) + log(testResult.dSpeed); |
| 389 | double O_DSpeed_note = O_ratioNote * ( 20 + 2*cLevel) + log(winners[cLevel].result.dSpeed); |
Yann Collet | 10ba183 | 2015-10-28 14:05:37 +0100 | [diff] [blame] | 390 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 391 | if (W_DMemUsed_note < O_DMemUsed_note) { |
Yann Collet | 81e49e6 | 2015-10-28 15:56:48 +0100 | [diff] [blame] | 392 | /* uses too much Decompression memory for too little benefit */ |
| 393 | if (W_ratio > O_ratio) |
| 394 | DISPLAY ("Decompression Memory : %5.3f @ %4.1f MB vs %5.3f @ %4.1f MB : not enough for level %i\n", |
| 395 | W_ratio, (double)(W_DMemUsed) / 1024 / 1024, |
| 396 | O_ratio, (double)(O_DMemUsed) / 1024 / 1024, cLevel); |
| 397 | continue; |
| 398 | } |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 399 | if (W_CMemUsed_note < O_CMemUsed_note) { |
Yann Collet | 81e49e6 | 2015-10-28 15:56:48 +0100 | [diff] [blame] | 400 | /* uses too much memory for compression for too little benefit */ |
| 401 | if (W_ratio > O_ratio) |
| 402 | DISPLAY ("Compression Memory : %5.3f @ %4.1f MB vs %5.3f @ %4.1f MB : not enough for level %i\n", |
| 403 | W_ratio, (double)(W_CMemUsed) / 1024 / 1024, |
| 404 | O_ratio, (double)(O_CMemUsed) / 1024 / 1024, cLevel); |
| 405 | continue; |
| 406 | } |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 407 | if (W_CSpeed_note < O_CSpeed_note ) { |
Yann Collet | 81e49e6 | 2015-10-28 15:56:48 +0100 | [diff] [blame] | 408 | /* too large compression speed difference for the compression benefit */ |
| 409 | if (W_ratio > O_ratio) |
| 410 | DISPLAY ("Compression Speed : %5.3f @ %4.1f MB/s vs %5.3f @ %4.1f MB/s : not enough for level %i\n", |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 411 | W_ratio, testResult.cSpeed / 1000000, |
| 412 | O_ratio, winners[cLevel].result.cSpeed / 1000000., cLevel); |
Yann Collet | 81e49e6 | 2015-10-28 15:56:48 +0100 | [diff] [blame] | 413 | continue; |
| 414 | } |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 415 | if (W_DSpeed_note < O_DSpeed_note ) { |
Yann Collet | 81e49e6 | 2015-10-28 15:56:48 +0100 | [diff] [blame] | 416 | /* too large decompression speed difference for the compression benefit */ |
| 417 | if (W_ratio > O_ratio) |
| 418 | DISPLAY ("Decompression Speed : %5.3f @ %4.1f MB/s vs %5.3f @ %4.1f MB/s : not enough for level %i\n", |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 419 | W_ratio, testResult.dSpeed / 1000000., |
| 420 | O_ratio, winners[cLevel].result.dSpeed / 1000000., cLevel); |
Yann Collet | 81e49e6 | 2015-10-28 15:56:48 +0100 | [diff] [blame] | 421 | continue; |
| 422 | } |
| 423 | |
| 424 | if (W_ratio < O_ratio) |
| 425 | DISPLAY("Solution %4.3f selected over %4.3f at level %i, due to better secondary statistics \n", W_ratio, O_ratio, cLevel); |
| 426 | |
| 427 | winners[cLevel].result = testResult; |
| 428 | winners[cLevel].params = params; |
| 429 | BMK_printWinner(stdout, cLevel, testResult, params, srcSize); |
Yann Collet | 1077bb8 | 2015-10-28 12:32:25 +0100 | [diff] [blame] | 430 | |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 431 | better = 1; |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 432 | } } |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 433 | |
| 434 | return better; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 435 | } |
| 436 | |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 437 | |
Yann Collet | 7ccff59 | 2015-11-09 12:07:44 +0100 | [diff] [blame] | 438 | /* nullified useless params, to ensure count stats */ |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 439 | static ZSTD_compressionParameters* sanitizeParams(ZSTD_compressionParameters params) |
Yann Collet | 7ccff59 | 2015-11-09 12:07:44 +0100 | [diff] [blame] | 440 | { |
| 441 | g_params = params; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 442 | if (params.strategy == ZSTD_fast) |
Yann Collet | 8a57b92 | 2016-04-04 13:49:18 +0200 | [diff] [blame] | 443 | g_params.chainLog = 0, g_params.searchLog = 0; |
Yann Collet | 650a877 | 2016-07-13 11:49:05 +0200 | [diff] [blame] | 444 | if (params.strategy == ZSTD_dfast) |
| 445 | g_params.searchLog = 0; |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 446 | if (params.strategy != ZSTD_btopt && params.strategy != ZSTD_btopt2) |
Yann Collet | bd828d9 | 2016-02-11 04:38:55 +0100 | [diff] [blame] | 447 | g_params.targetLength = 0; |
Yann Collet | 7ccff59 | 2015-11-09 12:07:44 +0100 | [diff] [blame] | 448 | return &g_params; |
| 449 | } |
| 450 | |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 451 | |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 452 | static void paramVariation(ZSTD_compressionParameters* ptr) |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 453 | { |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 454 | ZSTD_compressionParameters p; |
| 455 | U32 validated = 0; |
| 456 | while (!validated) { |
| 457 | U32 nbChanges = (FUZ_rand(&g_rand) & 3) + 1; |
| 458 | p = *ptr; |
| 459 | for ( ; nbChanges ; nbChanges--) { |
| 460 | const U32 changeID = FUZ_rand(&g_rand) % 14; |
| 461 | switch(changeID) |
| 462 | { |
| 463 | case 0: |
Yann Collet | 8a57b92 | 2016-04-04 13:49:18 +0200 | [diff] [blame] | 464 | p.chainLog++; break; |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 465 | case 1: |
Yann Collet | 8a57b92 | 2016-04-04 13:49:18 +0200 | [diff] [blame] | 466 | p.chainLog--; break; |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 467 | case 2: |
| 468 | p.hashLog++; break; |
| 469 | case 3: |
| 470 | p.hashLog--; break; |
| 471 | case 4: |
| 472 | p.searchLog++; break; |
| 473 | case 5: |
| 474 | p.searchLog--; break; |
| 475 | case 6: |
| 476 | p.windowLog++; break; |
| 477 | case 7: |
| 478 | p.windowLog--; break; |
| 479 | case 8: |
| 480 | p.searchLength++; break; |
| 481 | case 9: |
| 482 | p.searchLength--; break; |
| 483 | case 10: |
| 484 | p.strategy = (ZSTD_strategy)(((U32)p.strategy)+1); break; |
| 485 | case 11: |
| 486 | p.strategy = (ZSTD_strategy)(((U32)p.strategy)-1); break; |
| 487 | case 12: |
| 488 | p.targetLength *= 1 + ((double)(FUZ_rand(&g_rand)&255)) / 256.; break; |
| 489 | case 13: |
| 490 | p.targetLength /= 1 + ((double)(FUZ_rand(&g_rand)&255)) / 256.; break; |
| 491 | } |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 492 | } |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 493 | validated = !ZSTD_isError(ZSTD_checkCParams(p)); |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 494 | } |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 495 | *ptr = p; |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | |
Yann Collet | 6d2c9e6 | 2015-11-09 11:39:48 +0100 | [diff] [blame] | 499 | #define PARAMTABLELOG 25 |
| 500 | #define PARAMTABLESIZE (1<<PARAMTABLELOG) |
| 501 | #define PARAMTABLEMASK (PARAMTABLESIZE-1) |
| 502 | static BYTE g_alreadyTested[PARAMTABLESIZE] = {0}; /* init to zero */ |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 503 | |
| 504 | #define NB_TESTS_PLAYED(p) \ |
Yann Collet | 7ccff59 | 2015-11-09 12:07:44 +0100 | [diff] [blame] | 505 | g_alreadyTested[(XXH64(sanitizeParams(p), sizeof(p), 0) >> 3) & PARAMTABLEMASK] |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 506 | |
| 507 | |
Yann Collet | 6d2c9e6 | 2015-11-09 11:39:48 +0100 | [diff] [blame] | 508 | #define MAX(a,b) ( (a) > (b) ? (a) : (b) ) |
| 509 | |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 510 | static void playAround(FILE* f, winnerInfo_t* winners, |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 511 | ZSTD_compressionParameters params, |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 512 | const void* srcBuffer, size_t srcSize, |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 513 | ZSTD_CCtx* ctx) |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 514 | { |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 515 | int nbVariations = 0; |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 516 | clock_t const clockStart = clock(); |
Yann Collet | b315bc8 | 2015-10-27 13:12:25 +0100 | [diff] [blame] | 517 | |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 518 | while (BMK_clockSpan(clockStart) < g_maxVariationTime) { |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 519 | ZSTD_compressionParameters p = params; |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 520 | |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 521 | if (nbVariations++ > g_maxNbVariations) break; |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 522 | paramVariation(&p); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 523 | |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 524 | /* exclude faster if already played params */ |
Yann Collet | 21f9693 | 2015-11-01 14:32:59 +0100 | [diff] [blame] | 525 | if (FUZ_rand(&g_rand) & ((1 << NB_TESTS_PLAYED(p))-1)) |
| 526 | continue; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 527 | |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 528 | /* test */ |
| 529 | NB_TESTS_PLAYED(p)++; |
| 530 | if (!BMK_seed(winners, p, srcBuffer, srcSize, ctx)) continue; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 531 | |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 532 | /* improvement found => search more */ |
| 533 | BMK_printWinners(f, winners, srcSize); |
| 534 | playAround(f, winners, p, srcBuffer, srcSize, ctx); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | } |
| 538 | |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 539 | |
Yann Collet | eb70043 | 2016-06-01 18:59:55 +0200 | [diff] [blame] | 540 | static ZSTD_compressionParameters randomParams(void) |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 541 | { |
Yann Collet | eb70043 | 2016-06-01 18:59:55 +0200 | [diff] [blame] | 542 | ZSTD_compressionParameters p; |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 543 | U32 validated = 0; |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 544 | while (!validated) { |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 545 | /* totally random entry */ |
Yann Collet | eb70043 | 2016-06-01 18:59:55 +0200 | [diff] [blame] | 546 | p.chainLog = FUZ_rand(&g_rand) % (ZSTD_CHAINLOG_MAX+1 - ZSTD_CHAINLOG_MIN) + ZSTD_CHAINLOG_MIN; |
| 547 | p.hashLog = FUZ_rand(&g_rand) % (ZSTD_HASHLOG_MAX+1 - ZSTD_HASHLOG_MIN) + ZSTD_HASHLOG_MIN; |
| 548 | p.searchLog = FUZ_rand(&g_rand) % (ZSTD_SEARCHLOG_MAX+1 - ZSTD_SEARCHLOG_MIN) + ZSTD_SEARCHLOG_MIN; |
| 549 | p.windowLog = FUZ_rand(&g_rand) % (ZSTD_WINDOWLOG_MAX+1 - ZSTD_WINDOWLOG_MIN) + ZSTD_WINDOWLOG_MIN; |
| 550 | p.searchLength=FUZ_rand(&g_rand) % (ZSTD_SEARCHLENGTH_MAX+1 - ZSTD_SEARCHLENGTH_MIN) + ZSTD_SEARCHLENGTH_MIN; |
| 551 | p.targetLength=FUZ_rand(&g_rand) % (ZSTD_TARGETLENGTH_MAX+1 - ZSTD_TARGETLENGTH_MIN) + ZSTD_TARGETLENGTH_MIN; |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 552 | p.strategy = (ZSTD_strategy) (FUZ_rand(&g_rand) % (ZSTD_btopt2 +1)); |
Yann Collet | eb70043 | 2016-06-01 18:59:55 +0200 | [diff] [blame] | 553 | validated = !ZSTD_isError(ZSTD_checkCParams(p)); |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 554 | } |
Yann Collet | eb70043 | 2016-06-01 18:59:55 +0200 | [diff] [blame] | 555 | return p; |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 556 | } |
| 557 | |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 558 | static void BMK_selectRandomStart( |
| 559 | FILE* f, winnerInfo_t* winners, |
| 560 | const void* srcBuffer, size_t srcSize, |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 561 | ZSTD_CCtx* ctx) |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 562 | { |
Yann Collet | eb70043 | 2016-06-01 18:59:55 +0200 | [diff] [blame] | 563 | U32 const id = (FUZ_rand(&g_rand) % (ZSTD_maxCLevel()+1)); |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 564 | if ((id==0) || (winners[id].params.windowLog==0)) { |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 565 | /* totally random entry */ |
Yann Collet | eb70043 | 2016-06-01 18:59:55 +0200 | [diff] [blame] | 566 | ZSTD_compressionParameters const p = ZSTD_adjustCParams(randomParams(), srcSize, 0); |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 567 | playAround(f, winners, p, srcBuffer, srcSize, ctx); |
| 568 | } |
| 569 | else |
| 570 | playAround(f, winners, winners[id].params, srcBuffer, srcSize, ctx); |
| 571 | } |
| 572 | |
| 573 | |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 574 | static void BMK_benchMem(void* srcBuffer, size_t srcSize) |
| 575 | { |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 576 | ZSTD_CCtx* const ctx = ZSTD_createCCtx(); |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 577 | ZSTD_compressionParameters params; |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 578 | winnerInfo_t winners[NB_LEVELS_TRACKED]; |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 579 | const char* const rfName = "grillResults.txt"; |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 580 | FILE* const f = fopen(rfName, "w"); |
Yann Collet | 43e0cd5 | 2015-11-09 16:38:17 +0100 | [diff] [blame] | 581 | const size_t blockSize = g_blockSize ? g_blockSize : srcSize; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 582 | |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 583 | /* init */ |
| 584 | if (ctx==NULL) { DISPLAY("ZSTD_createCCtx() failed \n"); exit(1); } |
| 585 | memset(winners, 0, sizeof(winners)); |
| 586 | if (f==NULL) { DISPLAY("error opening %s \n", rfName); exit(1); } |
| 587 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 588 | if (g_singleRun) { |
Yann Collet | 1077bb8 | 2015-10-28 12:32:25 +0100 | [diff] [blame] | 589 | BMK_result_t testResult; |
Yann Collet | eb70043 | 2016-06-01 18:59:55 +0200 | [diff] [blame] | 590 | g_params = ZSTD_adjustCParams(g_params, srcSize, 0); |
Yann Collet | 8b91abe | 2015-10-27 02:59:12 +0100 | [diff] [blame] | 591 | BMK_benchParam(&testResult, srcBuffer, srcSize, ctx, g_params); |
| 592 | DISPLAY("\n"); |
| 593 | return; |
| 594 | } |
| 595 | |
Yann Collet | 2c6992e | 2015-10-27 12:18:00 +0100 | [diff] [blame] | 596 | if (g_target) |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 597 | g_cSpeedTarget[1] = g_target * 1000000; |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 598 | else { |
Yann Collet | f12c130 | 2015-11-05 18:16:59 +0100 | [diff] [blame] | 599 | /* baseline config for level 1 */ |
Yann Collet | 1077bb8 | 2015-10-28 12:32:25 +0100 | [diff] [blame] | 600 | BMK_result_t testResult; |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 601 | params = ZSTD_getCParams(1, blockSize, 0); |
Yann Collet | 2c6992e | 2015-10-27 12:18:00 +0100 | [diff] [blame] | 602 | BMK_benchParam(&testResult, srcBuffer, srcSize, ctx, params); |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 603 | g_cSpeedTarget[1] = (testResult.cSpeed * 31) / 32; |
Yann Collet | 2c6992e | 2015-10-27 12:18:00 +0100 | [diff] [blame] | 604 | } |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 605 | |
Yann Collet | f12c130 | 2015-11-05 18:16:59 +0100 | [diff] [blame] | 606 | /* establish speed objectives (relative to level 1) */ |
Yann Collet | 4110534 | 2016-07-27 15:09:11 +0200 | [diff] [blame] | 607 | { int i; |
| 608 | for (i=2; i<=ZSTD_maxCLevel(); i++) |
| 609 | g_cSpeedTarget[i] = (g_cSpeedTarget[i-1] * 25) / 32; |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 610 | } |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 611 | |
| 612 | /* populate initial solution */ |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 613 | { const int maxSeeds = g_noSeed ? 1 : ZSTD_maxCLevel(); |
| 614 | int i; |
| 615 | for (i=0; i<=maxSeeds; i++) { |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 616 | params = ZSTD_getCParams(i, blockSize, 0); |
Yann Collet | b315bc8 | 2015-10-27 13:12:25 +0100 | [diff] [blame] | 617 | BMK_seed(winners, params, srcBuffer, srcSize, ctx); |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 618 | } } |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 619 | BMK_printWinners(f, winners, srcSize); |
| 620 | |
| 621 | /* start tests */ |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 622 | { const time_t grillStart = time(NULL); |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 623 | do { |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 624 | BMK_selectRandomStart(f, winners, srcBuffer, srcSize, ctx); |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 625 | } while (BMK_timeSpan(grillStart) < g_grillDuration_s); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 626 | } |
| 627 | |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 628 | /* end summary */ |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 629 | BMK_printWinners(f, winners, srcSize); |
Yann Collet | 2c6992e | 2015-10-27 12:18:00 +0100 | [diff] [blame] | 630 | DISPLAY("grillParams operations completed \n"); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 631 | |
| 632 | /* clean up*/ |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 633 | fclose(f); |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 634 | ZSTD_freeCCtx(ctx); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | |
| 638 | static int benchSample(void) |
| 639 | { |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 640 | void* origBuff; |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 641 | size_t const benchedSize = sampleSize; |
| 642 | const char* const name = "Sample 10MiB"; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 643 | |
| 644 | /* Allocation */ |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 645 | origBuff = malloc(benchedSize); |
| 646 | if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); return 12; } |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 647 | |
| 648 | /* Fill buffer */ |
| 649 | RDG_genBuffer(origBuff, benchedSize, g_compressibility, 0.0, 0); |
| 650 | |
| 651 | /* bench */ |
| 652 | DISPLAY("\r%79s\r", ""); |
| 653 | DISPLAY("using %s %i%%: \n", name, (int)(g_compressibility*100)); |
| 654 | BMK_benchMem(origBuff, benchedSize); |
| 655 | |
| 656 | free(origBuff); |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 661 | int benchFiles(const char** fileNamesTable, int nbFiles) |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 662 | { |
| 663 | int fileIdx=0; |
| 664 | |
| 665 | /* Loop for each file */ |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 666 | while (fileIdx<nbFiles) { |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 667 | const char* const inFileName = fileNamesTable[fileIdx++]; |
| 668 | FILE* const inFile = fopen( inFileName, "rb" ); |
| 669 | U64 const inFileSize = UTIL_getFileSize(inFileName); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 670 | size_t benchedSize; |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 671 | void* origBuff; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 672 | |
| 673 | /* Check file existence */ |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 674 | if (inFile==NULL) { |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 675 | DISPLAY( "Pb opening %s\n", inFileName); |
| 676 | return 11; |
| 677 | } |
| 678 | |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 679 | /* Memory allocation */ |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 680 | benchedSize = BMK_findMaxMem(inFileSize*3) / 3; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 681 | if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize; |
| 682 | if (benchedSize < inFileSize) |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 683 | DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", inFileName, (int)(benchedSize>>20)); |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 684 | origBuff = malloc(benchedSize); |
| 685 | if (origBuff==NULL) { |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 686 | DISPLAY("\nError: not enough memory!\n"); |
| 687 | fclose(inFile); |
| 688 | return 12; |
| 689 | } |
| 690 | |
| 691 | /* Fill input buffer */ |
| 692 | DISPLAY("Loading %s... \r", inFileName); |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 693 | { size_t const readSize = fread(origBuff, 1, benchedSize, inFile); |
| 694 | fclose(inFile); |
| 695 | if(readSize != benchedSize) { |
| 696 | DISPLAY("\nError: problem reading file '%s' !! \n", inFileName); |
| 697 | free(origBuff); |
| 698 | return 13; |
| 699 | } } |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 700 | |
| 701 | /* bench */ |
| 702 | DISPLAY("\r%79s\r", ""); |
| 703 | DISPLAY("using %s : \n", inFileName); |
| 704 | BMK_benchMem(origBuff, benchedSize); |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 705 | |
| 706 | /* clean */ |
| 707 | free(origBuff); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 714 | int optimizeForSize(const char* inFileName, U32 targetSpeed) |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 715 | { |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 716 | FILE* const inFile = fopen( inFileName, "rb" ); |
| 717 | U64 const inFileSize = UTIL_getFileSize(inFileName); |
| 718 | size_t benchedSize = BMK_findMaxMem(inFileSize*3) / 3; |
| 719 | void* origBuff; |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 720 | |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 721 | /* Init */ |
| 722 | if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; } |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 723 | |
| 724 | /* Memory allocation & restrictions */ |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 725 | if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize; |
| 726 | if (benchedSize < inFileSize) |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 727 | DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", inFileName, (int)(benchedSize>>20)); |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 728 | |
| 729 | /* Alloc */ |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 730 | origBuff = malloc(benchedSize); |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 731 | if(!origBuff) { |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 732 | DISPLAY("\nError: not enough memory!\n"); |
| 733 | fclose(inFile); |
| 734 | return 12; |
| 735 | } |
| 736 | |
| 737 | /* Fill input buffer */ |
| 738 | DISPLAY("Loading %s... \r", inFileName); |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 739 | { size_t const readSize = fread(origBuff, 1, benchedSize, inFile); |
| 740 | fclose(inFile); |
| 741 | if(readSize != benchedSize) { |
| 742 | DISPLAY("\nError: problem reading file '%s' !! \n", inFileName); |
| 743 | free(origBuff); |
| 744 | return 13; |
| 745 | } } |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 746 | |
| 747 | /* bench */ |
| 748 | DISPLAY("\r%79s\r", ""); |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 749 | DISPLAY("optimizing for %s - limit speed %u MB/s \n", inFileName, targetSpeed); |
| 750 | targetSpeed *= 1000; |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 751 | |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 752 | { ZSTD_CCtx* const ctx = ZSTD_createCCtx(); |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 753 | ZSTD_compressionParameters params; |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 754 | winnerInfo_t winner; |
| 755 | BMK_result_t candidate; |
Yann Collet | 7fe531e | 2015-11-29 02:38:09 +0100 | [diff] [blame] | 756 | const size_t blockSize = g_blockSize ? g_blockSize : benchedSize; |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 757 | |
| 758 | /* init */ |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 759 | if (ctx==NULL) { DISPLAY("\n ZSTD_createCCtx error \n"); free(origBuff); return 14;} |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 760 | memset(&winner, 0, sizeof(winner)); |
| 761 | winner.result.cSize = (size_t)(-1); |
| 762 | |
| 763 | /* find best solution from default params */ |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 764 | { const int maxSeeds = g_noSeed ? 1 : ZSTD_maxCLevel(); |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 765 | int i; |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 766 | for (i=1; i<=maxSeeds; i++) { |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 767 | params = ZSTD_getCParams(i, blockSize, 0); |
Yann Collet | 7fe531e | 2015-11-29 02:38:09 +0100 | [diff] [blame] | 768 | BMK_benchParam(&candidate, origBuff, benchedSize, ctx, params); |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 769 | if (candidate.cSpeed < targetSpeed) |
| 770 | break; |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 771 | if ( (candidate.cSize < winner.result.cSize) |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 772 | | ((candidate.cSize == winner.result.cSize) & (candidate.cSpeed > winner.result.cSpeed)) ) |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 773 | { |
| 774 | winner.params = params; |
| 775 | winner.result = candidate; |
Yann Collet | 7fe531e | 2015-11-29 02:38:09 +0100 | [diff] [blame] | 776 | BMK_printWinner(stdout, i, winner.result, winner.params, benchedSize); |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 777 | } } |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 778 | } |
Yann Collet | 7fe531e | 2015-11-29 02:38:09 +0100 | [diff] [blame] | 779 | BMK_printWinner(stdout, 99, winner.result, winner.params, benchedSize); |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 780 | |
| 781 | /* start tests */ |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 782 | { time_t const grillStart = time(NULL); |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 783 | do { |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 784 | params = winner.params; |
| 785 | paramVariation(¶ms); |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 786 | if ((FUZ_rand(&g_rand) & 15) == 3) params = randomParams(); |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 787 | |
| 788 | /* exclude faster if already played set of params */ |
| 789 | if (FUZ_rand(&g_rand) & ((1 << NB_TESTS_PLAYED(params))-1)) continue; |
| 790 | |
| 791 | /* test */ |
| 792 | NB_TESTS_PLAYED(params)++; |
Yann Collet | 7fe531e | 2015-11-29 02:38:09 +0100 | [diff] [blame] | 793 | BMK_benchParam(&candidate, origBuff, benchedSize, ctx, params); |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 794 | |
| 795 | /* improvement found => new winner */ |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 796 | if ( (candidate.cSpeed > targetSpeed) |
| 797 | & ( (candidate.cSize < winner.result.cSize) |
| 798 | | ((candidate.cSize == winner.result.cSize) & (candidate.cSpeed > winner.result.cSpeed)) ) ) |
| 799 | { |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 800 | winner.params = params; |
| 801 | winner.result = candidate; |
Yann Collet | 7fe531e | 2015-11-29 02:38:09 +0100 | [diff] [blame] | 802 | BMK_printWinner(stdout, 99, winner.result, winner.params, benchedSize); |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 803 | } |
Yann Collet | 158e770 | 2016-07-13 16:45:24 +0200 | [diff] [blame] | 804 | } while (BMK_timeSpan(grillStart) < g_grillDuration_s); |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /* end summary */ |
Yann Collet | 7fe531e | 2015-11-29 02:38:09 +0100 | [diff] [blame] | 808 | BMK_printWinner(stdout, 99, winner.result, winner.params, benchedSize); |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 809 | DISPLAY("grillParams size - optimizer completed \n"); |
| 810 | |
| 811 | /* clean up*/ |
| 812 | ZSTD_freeCCtx(ctx); |
| 813 | } |
| 814 | |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 815 | free(origBuff); |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 820 | static int usage(const char* exename) |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 821 | { |
| 822 | DISPLAY( "Usage :\n"); |
| 823 | DISPLAY( " %s [arg] file\n", exename); |
| 824 | DISPLAY( "Arguments :\n"); |
Yann Collet | b5d2a0c | 2015-11-23 17:10:19 +0100 | [diff] [blame] | 825 | DISPLAY( " file : path to the file used as reference (if none, generates a compressible sample)\n"); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 826 | DISPLAY( " -H/-h : Help (this text + advanced options)\n"); |
| 827 | return 0; |
| 828 | } |
| 829 | |
Yann Collet | 04b12d8 | 2016-02-11 06:23:24 +0100 | [diff] [blame] | 830 | static int usage_advanced(void) |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 831 | { |
| 832 | DISPLAY( "\nAdvanced options :\n"); |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 833 | DISPLAY( " -T# : set level 1 speed objective \n"); |
| 834 | DISPLAY( " -B# : cut input into blocks of size # (default : single block) \n"); |
| 835 | DISPLAY( " -i# : iteration loops [1-9](default : %i) \n", NBLOOPS); |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 836 | DISPLAY( " -O# : find Optimized parameters for # target speed (default : 0) \n"); |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 837 | DISPLAY( " -S : Single run \n"); |
| 838 | DISPLAY( " -P# : generated sample compressibility (default : %.1f%%) \n", COMPRESSIBILITY_DEFAULT * 100); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 839 | return 0; |
| 840 | } |
| 841 | |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 842 | static int badusage(const char* exename) |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 843 | { |
| 844 | DISPLAY("Wrong parameters\n"); |
| 845 | usage(exename); |
Yann Collet | 8b91abe | 2015-10-27 02:59:12 +0100 | [diff] [blame] | 846 | return 1; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 847 | } |
| 848 | |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 849 | int main(int argc, const char** argv) |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 850 | { |
| 851 | int i, |
| 852 | filenamesStart=0, |
| 853 | result; |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 854 | const char* exename=argv[0]; |
| 855 | const char* input_filename=0; |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 856 | U32 optimizer = 0; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 857 | U32 main_pause = 0; |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 858 | U32 targetSpeed = 0; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 859 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 860 | /* checks */ |
| 861 | if (NB_LEVELS_TRACKED <= ZSTD_maxCLevel()) { |
| 862 | DISPLAY("Error : NB_LEVELS_TRACKED <= ZSTD_maxCLevel() \n"); |
| 863 | exit(1); |
| 864 | } |
| 865 | |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 866 | /* Welcome message */ |
| 867 | DISPLAY(WELCOME_MESSAGE); |
| 868 | |
| 869 | if (argc<1) { badusage(exename); return 1; } |
| 870 | |
Yann Collet | 0dbf287 | 2016-04-08 02:02:12 +0200 | [diff] [blame] | 871 | for(i=1; i<argc; i++) { |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 872 | const char* argument = argv[i]; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 873 | |
Yann Collet | b315bc8 | 2015-10-27 13:12:25 +0100 | [diff] [blame] | 874 | if(!argument) continue; /* Protection if argument empty */ |
| 875 | |
| 876 | if(!strcmp(argument,"--no-seed")) { g_noSeed = 1; continue; } |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 877 | |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 878 | /* Decode command (note : aggregated commands are allowed) */ |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 879 | if (argument[0]=='-') { |
Yann Collet | 8b91abe | 2015-10-27 02:59:12 +0100 | [diff] [blame] | 880 | argument++; |
| 881 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 882 | while (argument[0]!=0) { |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 883 | |
| 884 | switch(argument[0]) |
| 885 | { |
| 886 | /* Display help on usage */ |
| 887 | case 'h' : |
| 888 | case 'H': usage(exename); usage_advanced(); return 0; |
| 889 | |
| 890 | /* Pause at the end (hidden option) */ |
Yann Collet | 8b91abe | 2015-10-27 02:59:12 +0100 | [diff] [blame] | 891 | case 'p': main_pause = 1; argument++; break; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 892 | |
| 893 | /* Modify Nb Iterations */ |
| 894 | case 'i': |
Yann Collet | 8b91abe | 2015-10-27 02:59:12 +0100 | [diff] [blame] | 895 | argument++; |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 896 | if ((argument[0] >='0') & (argument[0] <='9')) |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 897 | g_nbIterations = *argument++ - '0'; |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 898 | break; |
| 899 | |
| 900 | /* Sample compressibility (when no file provided) */ |
| 901 | case 'P': |
Yann Collet | 8b91abe | 2015-10-27 02:59:12 +0100 | [diff] [blame] | 902 | argument++; |
Yann Collet | 0dbf287 | 2016-04-08 02:02:12 +0200 | [diff] [blame] | 903 | { U32 proba32 = 0; |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 904 | while ((argument[0]>= '0') & (argument[0]<= '9')) |
Yann Collet | 0dbf287 | 2016-04-08 02:02:12 +0200 | [diff] [blame] | 905 | proba32 = (proba32*10) + (*argument++ - '0'); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 906 | g_compressibility = (double)proba32 / 100.; |
| 907 | } |
| 908 | break; |
| 909 | |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 910 | case 'O': |
| 911 | argument++; |
| 912 | optimizer=1; |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 913 | targetSpeed = 0; |
| 914 | while ((*argument >= '0') & (*argument <= '9')) |
| 915 | targetSpeed = (targetSpeed*10) + (*argument++ - '0'); |
Yann Collet | 09116c2 | 2015-11-27 17:46:14 +0100 | [diff] [blame] | 916 | break; |
| 917 | |
Yann Collet | 8b91abe | 2015-10-27 02:59:12 +0100 | [diff] [blame] | 918 | /* Run Single conf */ |
| 919 | case 'S': |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 920 | g_singleRun = 1; |
| 921 | argument++; |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 922 | g_params = ZSTD_getCParams(2, g_blockSize, 0); |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 923 | for ( ; ; ) { |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 924 | switch(*argument) |
| 925 | { |
| 926 | case 'w': |
| 927 | g_params.windowLog = 0; |
| 928 | argument++; |
| 929 | while ((*argument>= '0') && (*argument<='9')) |
| 930 | g_params.windowLog *= 10, g_params.windowLog += *argument++ - '0'; |
| 931 | continue; |
| 932 | case 'c': |
Yann Collet | 8a57b92 | 2016-04-04 13:49:18 +0200 | [diff] [blame] | 933 | g_params.chainLog = 0; |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 934 | argument++; |
| 935 | while ((*argument>= '0') && (*argument<='9')) |
Yann Collet | 8a57b92 | 2016-04-04 13:49:18 +0200 | [diff] [blame] | 936 | g_params.chainLog *= 10, g_params.chainLog += *argument++ - '0'; |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 937 | continue; |
| 938 | case 'h': |
| 939 | g_params.hashLog = 0; |
| 940 | argument++; |
| 941 | while ((*argument>= '0') && (*argument<='9')) |
| 942 | g_params.hashLog *= 10, g_params.hashLog += *argument++ - '0'; |
| 943 | continue; |
| 944 | case 's': |
| 945 | g_params.searchLog = 0; |
| 946 | argument++; |
| 947 | while ((*argument>= '0') && (*argument<='9')) |
| 948 | g_params.searchLog *= 10, g_params.searchLog += *argument++ - '0'; |
| 949 | continue; |
Yann Collet | 9b11b46 | 2015-11-01 12:40:22 +0100 | [diff] [blame] | 950 | case 'l': /* search length */ |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 951 | g_params.searchLength = 0; |
| 952 | argument++; |
| 953 | while ((*argument>= '0') && (*argument<='9')) |
| 954 | g_params.searchLength *= 10, g_params.searchLength += *argument++ - '0'; |
| 955 | continue; |
Yann Collet | 04b12d8 | 2016-02-11 06:23:24 +0100 | [diff] [blame] | 956 | case 't': /* target length */ |
| 957 | g_params.targetLength = 0; |
Yann Collet | 9b11b46 | 2015-11-01 12:40:22 +0100 | [diff] [blame] | 958 | argument++; |
Yann Collet | 04b12d8 | 2016-02-11 06:23:24 +0100 | [diff] [blame] | 959 | while ((*argument>= '0') && (*argument<='9')) |
| 960 | g_params.targetLength *= 10, g_params.targetLength += *argument++ - '0'; |
| 961 | continue; |
| 962 | case 'S': /* strategy */ |
| 963 | argument++; |
| 964 | while ((*argument>= '0') && (*argument<='9')) |
| 965 | g_params.strategy = (ZSTD_strategy)(*argument++ - '0'); |
Yann Collet | 9b11b46 | 2015-11-01 12:40:22 +0100 | [diff] [blame] | 966 | continue; |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 967 | case 'L': |
Yann Collet | 0dbf287 | 2016-04-08 02:02:12 +0200 | [diff] [blame] | 968 | { int cLevel = 0; |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 969 | argument++; |
| 970 | while ((*argument>= '0') && (*argument<='9')) |
| 971 | cLevel *= 10, cLevel += *argument++ - '0'; |
Yann Collet | 51d5004 | 2016-03-30 20:42:19 +0200 | [diff] [blame] | 972 | g_params = ZSTD_getCParams(cLevel, g_blockSize, 0); |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 973 | continue; |
| 974 | } |
| 975 | default : ; |
| 976 | } |
Yann Collet | 8b91abe | 2015-10-27 02:59:12 +0100 | [diff] [blame] | 977 | break; |
| 978 | } |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 979 | break; |
Yann Collet | 8b91abe | 2015-10-27 02:59:12 +0100 | [diff] [blame] | 980 | |
Yann Collet | f12c130 | 2015-11-05 18:16:59 +0100 | [diff] [blame] | 981 | /* target level1 speed objective, in MB/s */ |
Yann Collet | 2c6992e | 2015-10-27 12:18:00 +0100 | [diff] [blame] | 982 | case 'T': |
| 983 | argument++; |
| 984 | g_target = 0; |
Yann Collet | 0dbf287 | 2016-04-08 02:02:12 +0200 | [diff] [blame] | 985 | while ((*argument >= '0') && (*argument <= '9')) |
| 986 | g_target = (g_target*10) + (*argument++ - '0'); |
Yann Collet | 2c6992e | 2015-10-27 12:18:00 +0100 | [diff] [blame] | 987 | break; |
| 988 | |
| 989 | /* cut input into blocks */ |
| 990 | case 'B': |
Yann Collet | 0dbf287 | 2016-04-08 02:02:12 +0200 | [diff] [blame] | 991 | g_blockSize = 0; |
| 992 | argument++; |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 993 | while ((*argument >='0') & (*argument <='9')) |
Yann Collet | 0dbf287 | 2016-04-08 02:02:12 +0200 | [diff] [blame] | 994 | g_blockSize = (g_blockSize*10) + (*argument++ - '0'); |
| 995 | if (*argument=='K') g_blockSize<<=10, argument++; /* allows using KB notation */ |
| 996 | if (*argument=='M') g_blockSize<<=20, argument++; |
| 997 | if (*argument=='B') argument++; |
| 998 | DISPLAY("using %u KB block size \n", g_blockSize>>10); |
Yann Collet | 2c6992e | 2015-10-27 12:18:00 +0100 | [diff] [blame] | 999 | break; |
| 1000 | |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 1001 | /* Unknown command */ |
Yann Collet | 8b91abe | 2015-10-27 02:59:12 +0100 | [diff] [blame] | 1002 | default : return badusage(exename); |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | continue; |
Yann Collet | 0dbf287 | 2016-04-08 02:02:12 +0200 | [diff] [blame] | 1006 | } /* if (argument[0]=='-') */ |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 1007 | |
| 1008 | /* first provided filename is input */ |
| 1009 | if (!input_filename) { input_filename=argument; filenamesStart=i; continue; } |
| 1010 | } |
| 1011 | |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 1012 | if (filenamesStart==0) |
| 1013 | result = benchSample(); |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 1014 | else { |
Yann Collet | 00fd7a2 | 2015-11-28 16:03:22 +0100 | [diff] [blame] | 1015 | if (optimizer) |
Yann Collet | 696c4d7 | 2016-07-13 13:11:08 +0200 | [diff] [blame] | 1016 | result = optimizeForSize(input_filename, targetSpeed); |
Yann Collet | 00fd7a2 | 2015-11-28 16:03:22 +0100 | [diff] [blame] | 1017 | else |
| 1018 | result = benchFiles(argv+filenamesStart, argc-filenamesStart); |
| 1019 | } |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 1020 | |
| 1021 | if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; } |
| 1022 | |
| 1023 | return result; |
| 1024 | } |