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