| 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 | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 9 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 10 | |
| Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 11 | |
| 12 | /* ************************************** |
| Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 13 | * Tuning parameters |
| 14 | ****************************************/ |
| 15 | #ifndef BMK_TIMETEST_DEFAULT_S /* default minimum time per test */ |
| 16 | #define BMK_TIMETEST_DEFAULT_S 3 |
| 17 | #endif |
| 18 | |
| 19 | |
| 20 | /* ************************************** |
| Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 21 | * Compiler Warnings |
| 22 | ****************************************/ |
| 23 | #ifdef _MSC_VER |
| Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 24 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 25 | #endif |
| 26 | |
| 27 | |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 28 | /* ************************************* |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 29 | * Includes |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 30 | ***************************************/ |
| Przemyslaw Skibinski | 7a8a03c | 2016-12-21 15:08:44 +0100 | [diff] [blame] | 31 | #include "platform.h" /* Large Files support */ |
| Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 32 | #include "util.h" /* UTIL_getFileSize, UTIL_sleep */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 33 | #include <stdlib.h> /* malloc, free */ |
| 34 | #include <string.h> /* memset */ |
| Yann Collet | c6a6417 | 2016-12-31 03:31:26 +0100 | [diff] [blame] | 35 | #include <stdio.h> /* fprintf, fopen */ |
| Yann Collet | 7ae67bb | 2016-09-06 06:28:05 +0200 | [diff] [blame] | 36 | #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */ |
| inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 37 | |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 38 | #include "mem.h" |
| Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 39 | #define ZSTD_STATIC_LINKING_ONLY |
| 40 | #include "zstd.h" |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 41 | #include "datagen.h" /* RDG_genBuffer */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 42 | #include "xxhash.h" |
| Nick Terrell | 83c387e | 2017-01-26 15:25:32 -0800 | [diff] [blame] | 43 | #include "zstdmt_compress.h" |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 44 | |
| 45 | |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 46 | /* ************************************* |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 47 | * Constants |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 48 | ***************************************/ |
| inikep | f2f59d7 | 2016-06-22 15:42:26 +0200 | [diff] [blame] | 49 | #ifndef ZSTD_GIT_COMMIT |
| inikep | d7d251c | 2016-06-22 16:13:25 +0200 | [diff] [blame] | 50 | # define ZSTD_GIT_COMMIT_STRING "" |
| 51 | #else |
| 52 | # define ZSTD_GIT_COMMIT_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_GIT_COMMIT) |
| inikep | f2f59d7 | 2016-06-22 15:42:26 +0200 | [diff] [blame] | 53 | #endif |
| 54 | |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 55 | #define TIMELOOP_MICROSEC 1*1000000ULL /* 1 second */ |
| 56 | #define ACTIVEPERIOD_MICROSEC 70*1000000ULL /* 70 seconds */ |
| 57 | #define COOLPERIOD_SEC 10 |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 58 | |
| 59 | #define KB *(1 <<10) |
| 60 | #define MB *(1 <<20) |
| 61 | #define GB *(1U<<30) |
| 62 | |
| Yann Collet | 87c18b2 | 2016-08-26 01:43:47 +0200 | [diff] [blame] | 63 | static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31)); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 64 | |
| 65 | static U32 g_compressibilityDefault = 50; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 66 | |
| 67 | |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 68 | /* ************************************* |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 69 | * console display |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 70 | ***************************************/ |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 71 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 72 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 73 | static int g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 74 | |
| Yann Collet | bf2bc11 | 2016-08-02 23:48:13 +0200 | [diff] [blame] | 75 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
| 76 | if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \ |
| 77 | { g_time = clock(); DISPLAY(__VA_ARGS__); \ |
| Sean Purcell | 042ba12 | 2017-03-23 11:13:52 -0700 | [diff] [blame] | 78 | if (g_displayLevel>=4) fflush(stderr); } } |
| Yann Collet | bf2bc11 | 2016-08-02 23:48:13 +0200 | [diff] [blame] | 79 | static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100; |
| 80 | static clock_t g_time = 0; |
| 81 | |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 82 | |
| 83 | /* ************************************* |
| 84 | * Exceptions |
| 85 | ***************************************/ |
| 86 | #ifndef DEBUG |
| 87 | # define DEBUG 0 |
| 88 | #endif |
| 89 | #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); |
| 90 | #define EXM_THROW(error, ...) \ |
| 91 | { \ |
| Yann Collet | 81d6380 | 2017-04-04 15:21:09 -0700 | [diff] [blame] | 92 | DEBUGOUTPUT("%s: %i: \n", __FILE__, __LINE__); \ |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 93 | DISPLAYLEVEL(1, "Error %i : ", error); \ |
| 94 | DISPLAYLEVEL(1, __VA_ARGS__); \ |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 95 | DISPLAYLEVEL(1, " \n"); \ |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 96 | exit(error); \ |
| 97 | } |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 98 | |
| 99 | |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 100 | /* ************************************* |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 101 | * Benchmark Parameters |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 102 | ***************************************/ |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 103 | static int g_additionalParam = 0; |
| 104 | static U32 g_decodeOnly = 0; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 105 | |
| inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 106 | void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; } |
| 107 | |
| inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 108 | void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; } |
| 109 | |
| Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 110 | static U32 g_nbSeconds = BMK_TIMETEST_DEFAULT_S; |
| 111 | void BMK_setNbSeconds(unsigned nbSeconds) |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 112 | { |
| Przemyslaw Skibinski | 26306fc | 2016-11-03 11:38:01 +0100 | [diff] [blame] | 113 | g_nbSeconds = nbSeconds; |
| Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 114 | DISPLAYLEVEL(3, "- test >= %u seconds per compression / decompression - \n", g_nbSeconds); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 117 | static size_t g_blockSize = 0; |
| 118 | void BMK_setBlockSize(size_t blockSize) |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 119 | { |
| 120 | g_blockSize = blockSize; |
| Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 121 | if (g_blockSize) DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10)); |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| Yann Collet | ab7a579 | 2016-12-28 16:11:09 +0100 | [diff] [blame] | 124 | void BMK_setDecodeOnlyMode(unsigned decodeFlag) { g_decodeOnly = (decodeFlag>0); } |
| 125 | |
| Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 126 | static U32 g_nbThreads = 1; |
| 127 | void BMK_setNbThreads(unsigned nbThreads) { |
| 128 | #ifndef ZSTD_MULTITHREAD |
| 129 | if (nbThreads > 1) DISPLAYLEVEL(2, "Note : multi-threading is disabled \n"); |
| 130 | #endif |
| 131 | g_nbThreads = nbThreads; |
| 132 | } |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 133 | |
| Yann Collet | 3d93f2f | 2016-12-27 07:19:36 +0100 | [diff] [blame] | 134 | |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 135 | /* ******************************************************** |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 136 | * Bench functions |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 137 | **********************************************************/ |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 138 | typedef struct { |
| 139 | const void* srcPtr; |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 140 | size_t srcSize; |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 141 | void* cPtr; |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 142 | size_t cRoom; |
| 143 | size_t cSize; |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 144 | void* resPtr; |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 145 | size_t resSize; |
| 146 | } blockParam_t; |
| 147 | |
| Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 148 | |
| Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 149 | #define MIN(a,b) ((a)<(b) ? (a) : (b)) |
| Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 150 | #define MAX(a,b) ((a)>(b) ? (a) : (b)) |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 151 | |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 152 | static int BMK_benchMem(const void* srcBuffer, size_t srcSize, |
| inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 153 | const char* displayName, int cLevel, |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 154 | const size_t* fileSizes, U32 nbFiles, |
| Przemyslaw Skibinski | 897b8bb | 2016-12-13 13:03:41 +0100 | [diff] [blame] | 155 | const void* dictBuffer, size_t dictBufferSize, |
| Yann Collet | c200738 | 2017-04-04 15:35:06 -0700 | [diff] [blame^] | 156 | const ZSTD_compressionParameters* comprParams) |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 157 | { |
| Yann Collet | e63c631 | 2016-12-06 17:46:49 -0800 | [diff] [blame] | 158 | size_t const blockSize = ((g_blockSize>=32 && !g_decodeOnly) ? g_blockSize : srcSize) + (!srcSize) /* avoid div by 0 */ ; |
| Yann Collet | 5bde4be | 2017-03-29 12:10:38 -0700 | [diff] [blame] | 159 | size_t const avgSize = MIN(blockSize, (srcSize / nbFiles)); |
| Yann Collet | d64f435 | 2016-03-21 00:07:42 +0100 | [diff] [blame] | 160 | U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles; |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 161 | blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t)); |
| Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 162 | size_t const maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */ |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 163 | void* const compressedBuffer = malloc(maxCompressedSize); |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 164 | void* resultBuffer = malloc(srcSize); |
| Yann Collet | 458c8a9 | 2017-01-19 17:44:15 -0800 | [diff] [blame] | 165 | ZSTDMT_CCtx* const mtctx = ZSTDMT_createCCtx(g_nbThreads); |
| Yann Collet | 3c242e7 | 2016-07-13 14:56:24 +0200 | [diff] [blame] | 166 | ZSTD_CCtx* const ctx = ZSTD_createCCtx(); |
| 167 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
| Yann Collet | e63c631 | 2016-12-06 17:46:49 -0800 | [diff] [blame] | 168 | size_t const loadedCompressedSize = srcSize; |
| 169 | size_t cSize = 0; |
| 170 | double ratio = 0.; |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 171 | U32 nbBlocks; |
| Przemyslaw Skibinski | e052c60 | 2017-02-20 11:27:11 +0100 | [diff] [blame] | 172 | UTIL_freq_t ticksPerSecond; |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 173 | |
| 174 | /* checks */ |
| Yann Collet | ec224d2 | 2016-06-27 13:39:30 +0200 | [diff] [blame] | 175 | if (!compressedBuffer || !resultBuffer || !blockTable || !ctx || !dctx) |
| Yann Collet | 3d2cd7f | 2016-06-27 15:12:26 +0200 | [diff] [blame] | 176 | EXM_THROW(31, "allocation error : not enough memory"); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 177 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 178 | /* init */ |
| Yann Collet | c200738 | 2017-04-04 15:35:06 -0700 | [diff] [blame^] | 179 | if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* display last 17 characters */ |
| inikep | aaaf923 | 2016-05-09 16:19:25 +0200 | [diff] [blame] | 180 | UTIL_initTimer(&ticksPerSecond); |
| inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 181 | |
| Yann Collet | 5bde4be | 2017-03-29 12:10:38 -0700 | [diff] [blame] | 182 | if (g_decodeOnly) { /* benchmark only decompression : source must be already compressed */ |
| Yann Collet | c200738 | 2017-04-04 15:35:06 -0700 | [diff] [blame^] | 183 | const char* srcPtr = (const char*)srcBuffer; |
| 184 | U64 totalDSize64 = 0; |
| Yann Collet | e63c631 | 2016-12-06 17:46:49 -0800 | [diff] [blame] | 185 | U32 fileNb; |
| 186 | for (fileNb=0; fileNb<nbFiles; fileNb++) { |
| Sean Purcell | 4e70971 | 2017-02-07 13:50:09 -0800 | [diff] [blame] | 187 | U64 const fSize64 = ZSTD_findDecompressedSize(srcPtr, fileSizes[fileNb]); |
| Yann Collet | e63c631 | 2016-12-06 17:46:49 -0800 | [diff] [blame] | 188 | if (fSize64==0) EXM_THROW(32, "Impossible to determine original size "); |
| Yann Collet | c200738 | 2017-04-04 15:35:06 -0700 | [diff] [blame^] | 189 | totalDSize64 += fSize64; |
| Yann Collet | e63c631 | 2016-12-06 17:46:49 -0800 | [diff] [blame] | 190 | srcPtr += fileSizes[fileNb]; |
| 191 | } |
| Yann Collet | c200738 | 2017-04-04 15:35:06 -0700 | [diff] [blame^] | 192 | { size_t const decodedSize = (size_t)totalDSize64; |
| 193 | if (totalDSize64 > decodedSize) EXM_THROW(32, "original size is too large"); /* size_t overflow */ |
| Yann Collet | e63c631 | 2016-12-06 17:46:49 -0800 | [diff] [blame] | 194 | free(resultBuffer); |
| 195 | resultBuffer = malloc(decodedSize); |
| 196 | if (!resultBuffer) EXM_THROW(33, "not enough memory"); |
| 197 | cSize = srcSize; |
| 198 | srcSize = decodedSize; |
| 199 | ratio = (double)srcSize / (double)cSize; |
| 200 | } } |
| 201 | |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 202 | /* Init blockTable data */ |
| Yann Collet | e63c631 | 2016-12-06 17:46:49 -0800 | [diff] [blame] | 203 | { const char* srcPtr = (const char*)srcBuffer; |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 204 | char* cPtr = (char*)compressedBuffer; |
| 205 | char* resPtr = (char*)resultBuffer; |
| Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 206 | U32 fileNb; |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 207 | for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) { |
| Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 208 | size_t remaining = fileSizes[fileNb]; |
| Yann Collet | e63c631 | 2016-12-06 17:46:49 -0800 | [diff] [blame] | 209 | U32 const nbBlocksforThisFile = g_decodeOnly ? 1 : (U32)((remaining + (blockSize-1)) / blockSize); |
| Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 210 | U32 const blockEnd = nbBlocks + nbBlocksforThisFile; |
| Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 211 | for ( ; nbBlocks<blockEnd; nbBlocks++) { |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 212 | size_t const thisBlockSize = MIN(remaining, blockSize); |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 213 | blockTable[nbBlocks].srcPtr = (const void*)srcPtr; |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 214 | blockTable[nbBlocks].srcSize = thisBlockSize; |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 215 | blockTable[nbBlocks].cPtr = (void*)cPtr; |
| Yann Collet | e63c631 | 2016-12-06 17:46:49 -0800 | [diff] [blame] | 216 | blockTable[nbBlocks].cRoom = g_decodeOnly ? thisBlockSize : ZSTD_compressBound(thisBlockSize); |
| 217 | blockTable[nbBlocks].cSize = blockTable[nbBlocks].cRoom; |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 218 | blockTable[nbBlocks].resPtr = (void*)resPtr; |
| Sean Purcell | 4e70971 | 2017-02-07 13:50:09 -0800 | [diff] [blame] | 219 | blockTable[nbBlocks].resSize = g_decodeOnly ? (size_t) ZSTD_findDecompressedSize(srcPtr, thisBlockSize) : thisBlockSize; |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 220 | srcPtr += thisBlockSize; |
| 221 | cPtr += blockTable[nbBlocks].cRoom; |
| 222 | resPtr += thisBlockSize; |
| 223 | remaining -= thisBlockSize; |
| Yann Collet | e63c631 | 2016-12-06 17:46:49 -0800 | [diff] [blame] | 224 | } } } |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 225 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 226 | /* warmimg up memory */ |
| Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 227 | RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 228 | |
| 229 | /* Bench */ |
| inikep | 6d157f1 | 2016-04-15 16:54:11 +0200 | [diff] [blame] | 230 | { U64 fastestC = (U64)(-1LL), fastestD = (U64)(-1LL); |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 231 | U64 const crcOrig = g_decodeOnly ? 0 : XXH64(srcBuffer, srcSize, 0); |
| Przemyslaw Skibinski | 74dcd8d | 2017-02-21 12:22:05 +0100 | [diff] [blame] | 232 | UTIL_time_t coolTime; |
| Yann Collet | 4f5350f | 2016-11-29 13:12:24 -0800 | [diff] [blame] | 233 | U64 const maxTime = (g_nbSeconds * TIMELOOP_MICROSEC) + 1; |
| Yann Collet | a9febe8 | 2016-08-01 13:37:17 +0200 | [diff] [blame] | 234 | U64 totalCTime=0, totalDTime=0; |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 235 | U32 cCompleted=g_decodeOnly, dCompleted=0; |
| Yann Collet | a9febe8 | 2016-08-01 13:37:17 +0200 | [diff] [blame] | 236 | # define NB_MARKS 4 |
| 237 | const char* const marks[NB_MARKS] = { " |", " /", " =", "\\" }; |
| 238 | U32 markNb = 0; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 239 | |
| Yann Collet | f8804d1 | 2017-01-20 17:23:19 -0800 | [diff] [blame] | 240 | UTIL_getTime(&coolTime); |
| inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 241 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 242 | while (!cCompleted || !dCompleted) { |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 243 | |
| Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 244 | /* overheat protection */ |
| Przemyslaw Skibinski | 74dcd8d | 2017-02-21 12:22:05 +0100 | [diff] [blame] | 245 | if (UTIL_clockSpanMicro(coolTime, ticksPerSecond) > ACTIVEPERIOD_MICROSEC) { |
| Yann Collet | 235911e | 2016-07-31 01:32:48 +0200 | [diff] [blame] | 246 | DISPLAYLEVEL(2, "\rcooling down ... \r"); |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 247 | UTIL_sleep(COOLPERIOD_SEC); |
| cyan4973 | 5fba09f | 2017-01-20 12:23:30 -0800 | [diff] [blame] | 248 | UTIL_getTime(&coolTime); |
| Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 249 | } |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 250 | |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 251 | if (!g_decodeOnly) { |
| Przemyslaw Skibinski | 74dcd8d | 2017-02-21 12:22:05 +0100 | [diff] [blame] | 252 | UTIL_time_t clockStart; |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 253 | /* Compression */ |
| 254 | DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (U32)srcSize); |
| 255 | if (!cCompleted) memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 256 | |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 257 | UTIL_sleepMilli(1); /* give processor time to other processes */ |
| 258 | UTIL_waitForNextTick(ticksPerSecond); |
| Yann Collet | f8804d1 | 2017-01-20 17:23:19 -0800 | [diff] [blame] | 259 | UTIL_getTime(&clockStart); |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 260 | |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 261 | if (!cCompleted) { /* still some time to do compression tests */ |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 262 | ZSTD_customMem const cmem = { NULL, NULL, NULL }; |
| Yann Collet | c200738 | 2017-04-04 15:35:06 -0700 | [diff] [blame^] | 263 | U64 const clockLoop = g_nbSeconds ? TIMELOOP_MICROSEC : 1; |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 264 | U32 nbLoops = 0; |
| Yann Collet | c200738 | 2017-04-04 15:35:06 -0700 | [diff] [blame^] | 265 | ZSTD_parameters zparams = ZSTD_getParams(cLevel, avgSize, dictBufferSize); |
| 266 | ZSTD_CDict* cdict; |
| Przemyslaw Skibinski | 897b8bb | 2016-12-13 13:03:41 +0100 | [diff] [blame] | 267 | if (comprParams->windowLog) zparams.cParams.windowLog = comprParams->windowLog; |
| 268 | if (comprParams->chainLog) zparams.cParams.chainLog = comprParams->chainLog; |
| 269 | if (comprParams->hashLog) zparams.cParams.hashLog = comprParams->hashLog; |
| 270 | if (comprParams->searchLog) zparams.cParams.searchLog = comprParams->searchLog; |
| 271 | if (comprParams->searchLength) zparams.cParams.searchLength = comprParams->searchLength; |
| 272 | if (comprParams->targetLength) zparams.cParams.targetLength = comprParams->targetLength; |
| Przemyslaw Skibinski | c71e552 | 2016-12-13 20:04:32 +0100 | [diff] [blame] | 273 | if (comprParams->strategy) zparams.cParams.strategy = (ZSTD_strategy)(comprParams->strategy - 1); |
| Yann Collet | c200738 | 2017-04-04 15:35:06 -0700 | [diff] [blame^] | 274 | cdict = ZSTD_createCDict_advanced(dictBuffer, dictBufferSize, 1, zparams, cmem); |
| 275 | if (cdict==NULL) EXM_THROW(1, "ZSTD_createCDict_advanced() allocation failure"); |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 276 | do { |
| 277 | U32 blockNb; |
| 278 | size_t rSize; |
| 279 | for (blockNb=0; blockNb<nbBlocks; blockNb++) { |
| 280 | if (dictBufferSize) { |
| 281 | rSize = ZSTD_compress_usingCDict(ctx, |
| 282 | blockTable[blockNb].cPtr, blockTable[blockNb].cRoom, |
| 283 | blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize, |
| 284 | cdict); |
| Yann Collet | 458c8a9 | 2017-01-19 17:44:15 -0800 | [diff] [blame] | 285 | } else { |
| 286 | #ifdef ZSTD_MULTITHREAD /* note : limitation : MT single-pass does not support compression with dictionary */ |
| 287 | rSize = ZSTDMT_compressCCtx(mtctx, |
| Yann Collet | 3d93f2f | 2016-12-27 07:19:36 +0100 | [diff] [blame] | 288 | blockTable[blockNb].cPtr, blockTable[blockNb].cRoom, |
| 289 | blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize, |
| 290 | cLevel); |
| Yann Collet | 458c8a9 | 2017-01-19 17:44:15 -0800 | [diff] [blame] | 291 | #else |
| Przemyslaw Skibinski | 897b8bb | 2016-12-13 13:03:41 +0100 | [diff] [blame] | 292 | rSize = ZSTD_compress_advanced (ctx, |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 293 | blockTable[blockNb].cPtr, blockTable[blockNb].cRoom, |
| Przemyslaw Skibinski | 897b8bb | 2016-12-13 13:03:41 +0100 | [diff] [blame] | 294 | blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize, NULL, 0, zparams); |
| Yann Collet | 458c8a9 | 2017-01-19 17:44:15 -0800 | [diff] [blame] | 295 | #endif |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 296 | } |
| 297 | if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingCDict() failed : %s", ZSTD_getErrorName(rSize)); |
| 298 | blockTable[blockNb].cSize = rSize; |
| Przemyslaw Skibinski | 2558b4c | 2016-11-18 11:46:30 +0100 | [diff] [blame] | 299 | } |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 300 | nbLoops++; |
| Przemyslaw Skibinski | 74dcd8d | 2017-02-21 12:22:05 +0100 | [diff] [blame] | 301 | } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop); |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 302 | ZSTD_freeCDict(cdict); |
| Przemyslaw Skibinski | 74dcd8d | 2017-02-21 12:22:05 +0100 | [diff] [blame] | 303 | { U64 const clockSpanMicro = UTIL_clockSpanMicro(clockStart, ticksPerSecond); |
| Yann Collet | c6a6417 | 2016-12-31 03:31:26 +0100 | [diff] [blame] | 304 | if (clockSpanMicro < fastestC*nbLoops) fastestC = clockSpanMicro / nbLoops; |
| 305 | totalCTime += clockSpanMicro; |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 306 | cCompleted = (totalCTime >= maxTime); |
| 307 | } } |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 308 | |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 309 | cSize = 0; |
| 310 | { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; } |
| 311 | ratio = (double)srcSize / (double)cSize; |
| 312 | markNb = (markNb+1) % NB_MARKS; |
| 313 | DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r", |
| 314 | marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratio, |
| 315 | (double)srcSize / fastestC ); |
| 316 | } else { /* g_decodeOnly */ |
| Yann Collet | e63c631 | 2016-12-06 17:46:49 -0800 | [diff] [blame] | 317 | memcpy(compressedBuffer, srcBuffer, loadedCompressedSize); |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 318 | } |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 319 | |
| Yann Collet | 5eb749e | 2017-01-11 18:21:25 +0100 | [diff] [blame] | 320 | #if 0 /* disable decompression test */ |
| Yann Collet | 3d93f2f | 2016-12-27 07:19:36 +0100 | [diff] [blame] | 321 | dCompleted=1; |
| 322 | (void)totalDTime; (void)fastestD; (void)crcOrig; /* unused when decompression disabled */ |
| 323 | #else |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 324 | /* Decompression */ |
| Yann Collet | d1733f7 | 2016-08-21 01:04:46 +0200 | [diff] [blame] | 325 | if (!dCompleted) memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 326 | |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 327 | UTIL_sleepMilli(1); /* give processor time to other processes */ |
| 328 | UTIL_waitForNextTick(ticksPerSecond); |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 329 | |
| Yann Collet | a9febe8 | 2016-08-01 13:37:17 +0200 | [diff] [blame] | 330 | if (!dCompleted) { |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 331 | U64 clockLoop = g_nbSeconds ? TIMELOOP_MICROSEC : 1; |
| Yann Collet | a9febe8 | 2016-08-01 13:37:17 +0200 | [diff] [blame] | 332 | U32 nbLoops = 0; |
| Przemyslaw Skibinski | 74dcd8d | 2017-02-21 12:22:05 +0100 | [diff] [blame] | 333 | UTIL_time_t clockStart; |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 334 | ZSTD_DDict* const ddict = ZSTD_createDDict(dictBuffer, dictBufferSize); |
| Yann Collet | ee1a084 | 2016-06-07 01:40:49 +0200 | [diff] [blame] | 335 | if (!ddict) EXM_THROW(2, "ZSTD_createDDict() allocation failure"); |
| Yann Collet | f8804d1 | 2017-01-20 17:23:19 -0800 | [diff] [blame] | 336 | UTIL_getTime(&clockStart); |
| inikep | c5e1d29 | 2016-04-19 09:37:59 +0200 | [diff] [blame] | 337 | do { |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 338 | U32 blockNb; |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 339 | for (blockNb=0; blockNb<nbBlocks; blockNb++) { |
| Yann Collet | ee1a084 | 2016-06-07 01:40:49 +0200 | [diff] [blame] | 340 | size_t const regenSize = ZSTD_decompress_usingDDict(dctx, |
| Yann Collet | e63c631 | 2016-12-06 17:46:49 -0800 | [diff] [blame] | 341 | blockTable[blockNb].resPtr, blockTable[blockNb].resSize, |
| Yann Collet | ee1a084 | 2016-06-07 01:40:49 +0200 | [diff] [blame] | 342 | blockTable[blockNb].cPtr, blockTable[blockNb].cSize, |
| 343 | ddict); |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 344 | if (ZSTD_isError(regenSize)) { |
| Yann Collet | 5eb749e | 2017-01-11 18:21:25 +0100 | [diff] [blame] | 345 | DISPLAY("ZSTD_decompress_usingDDict() failed on block %u of size %u : %s \n", |
| 346 | blockNb, (U32)blockTable[blockNb].cSize, ZSTD_getErrorName(regenSize)); |
| inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 347 | clockLoop = 0; /* force immediate test end */ |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 348 | break; |
| 349 | } |
| 350 | blockTable[blockNb].resSize = regenSize; |
| inikep | c5e1d29 | 2016-04-19 09:37:59 +0200 | [diff] [blame] | 351 | } |
| 352 | nbLoops++; |
| Przemyslaw Skibinski | 74dcd8d | 2017-02-21 12:22:05 +0100 | [diff] [blame] | 353 | } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop); |
| Yann Collet | ee1a084 | 2016-06-07 01:40:49 +0200 | [diff] [blame] | 354 | ZSTD_freeDDict(ddict); |
| Przemyslaw Skibinski | 74dcd8d | 2017-02-21 12:22:05 +0100 | [diff] [blame] | 355 | { U64 const clockSpanMicro = UTIL_clockSpanMicro(clockStart, ticksPerSecond); |
| Yann Collet | 5eb749e | 2017-01-11 18:21:25 +0100 | [diff] [blame] | 356 | if (clockSpanMicro < fastestD*nbLoops) fastestD = clockSpanMicro / nbLoops; |
| 357 | totalDTime += clockSpanMicro; |
| Yann Collet | 4f5350f | 2016-11-29 13:12:24 -0800 | [diff] [blame] | 358 | dCompleted = (totalDTime >= maxTime); |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 359 | } } |
| 360 | |
| Yann Collet | a9febe8 | 2016-08-01 13:37:17 +0200 | [diff] [blame] | 361 | markNb = (markNb+1) % NB_MARKS; |
| 362 | DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r", |
| 363 | marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratio, |
| inikep | 06f793a | 2016-03-29 11:17:58 +0200 | [diff] [blame] | 364 | (double)srcSize / fastestC, |
| 365 | (double)srcSize / fastestD ); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 366 | |
| 367 | /* CRC Checking */ |
| Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 368 | { U64 const crcCheck = XXH64(resultBuffer, srcSize, 0); |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 369 | if (!g_decodeOnly && (crcOrig!=crcCheck)) { |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 370 | size_t u; |
| 371 | DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck); |
| 372 | for (u=0; u<srcSize; u++) { |
| 373 | if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) { |
| 374 | U32 segNb, bNb, pos; |
| 375 | size_t bacc = 0; |
| 376 | DISPLAY("Decoding error at pos %u ", (U32)u); |
| 377 | for (segNb = 0; segNb < nbBlocks; segNb++) { |
| 378 | if (bacc + blockTable[segNb].srcSize > u) break; |
| 379 | bacc += blockTable[segNb].srcSize; |
| 380 | } |
| 381 | pos = (U32)(u - bacc); |
| 382 | bNb = pos / (128 KB); |
| 383 | DISPLAY("(block %u, sub %u, pos %u) \n", segNb, bNb, pos); |
| Yann Collet | 736788f | 2017-01-19 12:12:50 -0800 | [diff] [blame] | 384 | if (u>5) { |
| 385 | int n; |
| 386 | for (n=-5; n<0; n++) DISPLAY("%02X ", ((const BYTE*)srcBuffer)[u+n]); |
| 387 | DISPLAY(" :%02X: ", ((const BYTE*)srcBuffer)[u]); |
| 388 | for (n=1; n<3; n++) DISPLAY("%02X ", ((const BYTE*)srcBuffer)[u+n]); |
| 389 | DISPLAY(" \n"); |
| 390 | for (n=-5; n<0; n++) DISPLAY("%02X ", ((const BYTE*)resultBuffer)[u+n]); |
| 391 | DISPLAY(" :%02X: ", ((const BYTE*)resultBuffer)[u]); |
| 392 | for (n=1; n<3; n++) DISPLAY("%02X ", ((const BYTE*)resultBuffer)[u+n]); |
| 393 | DISPLAY(" \n"); |
| 394 | } |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 395 | break; |
| Yann Collet | 03a6dab | 2016-01-21 02:21:17 +0100 | [diff] [blame] | 396 | } |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 397 | if (u==srcSize-1) { /* should never happen */ |
| 398 | DISPLAY("no difference detected\n"); |
| 399 | } } |
| 400 | break; |
| 401 | } } /* CRC Checking */ |
| Yann Collet | e8c6bb1 | 2015-07-26 00:23:57 +0100 | [diff] [blame] | 402 | #endif |
| Przemyslaw Skibinski | 26306fc | 2016-11-03 11:38:01 +0100 | [diff] [blame] | 403 | } /* for (testNb = 1; testNb <= (g_nbSeconds + !g_nbSeconds); testNb++) */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 404 | |
| inikep | 7132fb1 | 2016-08-10 14:59:18 +0200 | [diff] [blame] | 405 | if (g_displayLevel == 1) { |
| 406 | double cSpeed = (double)srcSize / fastestC; |
| 407 | double dSpeed = (double)srcSize / fastestD; |
| 408 | if (g_additionalParam) |
| 409 | DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s (param=%d)\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName, g_additionalParam); |
| 410 | else |
| 411 | DISPLAY("-%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", cLevel, (int)cSize, ratio, cSpeed, dSpeed, displayName); |
| 412 | } |
| inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 413 | DISPLAYLEVEL(2, "%2i#\n", cLevel); |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 414 | } /* Bench */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 415 | |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 416 | /* clean up */ |
| inikep | 0bd0fae | 2016-05-05 13:10:57 +0200 | [diff] [blame] | 417 | free(blockTable); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 418 | free(compressedBuffer); |
| 419 | free(resultBuffer); |
| Yann Collet | 458c8a9 | 2017-01-19 17:44:15 -0800 | [diff] [blame] | 420 | ZSTDMT_freeCCtx(mtctx); |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 421 | ZSTD_freeCCtx(ctx); |
| 422 | ZSTD_freeDCtx(dctx); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 427 | static size_t BMK_findMaxMem(U64 requiredMem) |
| 428 | { |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 429 | size_t const step = 64 MB; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 430 | BYTE* testmem = NULL; |
| 431 | |
| 432 | requiredMem = (((requiredMem >> 26) + 1) << 26); |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 433 | requiredMem += step; |
| Yann Collet | 050efba | 2015-11-03 09:49:30 +0100 | [diff] [blame] | 434 | if (requiredMem > maxMemory) requiredMem = maxMemory; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 435 | |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 436 | do { |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 437 | testmem = (BYTE*)malloc((size_t)requiredMem); |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 438 | requiredMem -= step; |
| 439 | } while (!testmem); |
| 440 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 441 | free(testmem); |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 442 | return (size_t)(requiredMem); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 443 | } |
| 444 | |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 445 | static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize, |
| inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 446 | const char* displayName, int cLevel, int cLevelLast, |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 447 | const size_t* fileSizes, unsigned nbFiles, |
| Przemyslaw Skibinski | 897b8bb | 2016-12-13 13:03:41 +0100 | [diff] [blame] | 448 | const void* dictBuffer, size_t dictBufferSize, |
| Przemyslaw Skibinski | d05014c | 2017-02-07 16:48:01 +0100 | [diff] [blame] | 449 | ZSTD_compressionParameters *compressionParams, int setRealTimePrio) |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 450 | { |
| inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 451 | int l; |
| inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 452 | |
| inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 453 | const char* pch = strrchr(displayName, '\\'); /* Windows */ |
| 454 | if (!pch) pch = strrchr(displayName, '/'); /* Linux */ |
| 455 | if (pch) displayName = pch+1; |
| inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 456 | |
| Przemyslaw Skibinski | d05014c | 2017-02-07 16:48:01 +0100 | [diff] [blame] | 457 | if (setRealTimePrio) { |
| 458 | DISPLAYLEVEL(2, "Note : switching to a real-time priority \n"); |
| 459 | SET_REALTIME_PRIORITY; |
| 460 | } |
| inikep | ea4ee3e | 2016-04-25 13:09:06 +0200 | [diff] [blame] | 461 | |
| inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 462 | if (g_displayLevel == 1 && !g_additionalParam) |
| Przemyslaw Skibinski | 26306fc | 2016-11-03 11:38:01 +0100 | [diff] [blame] | 463 | DISPLAY("bench %s %s: input %u bytes, %u seconds, %u KB blocks\n", ZSTD_VERSION_STRING, ZSTD_GIT_COMMIT_STRING, (U32)benchedSize, g_nbSeconds, (U32)(g_blockSize>>10)); |
| inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 464 | |
| 465 | if (cLevelLast < cLevel) cLevelLast = cLevel; |
| 466 | |
| Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 467 | for (l=cLevel; l <= cLevelLast; l++) { |
| inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 468 | BMK_benchMem(srcBuffer, benchedSize, |
| inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 469 | displayName, l, |
| inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 470 | fileSizes, nbFiles, |
| Przemyslaw Skibinski | 897b8bb | 2016-12-13 13:03:41 +0100 | [diff] [blame] | 471 | dictBuffer, dictBufferSize, compressionParams); |
| inikep | 7132fb1 | 2016-08-10 14:59:18 +0200 | [diff] [blame] | 472 | } |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 473 | } |
| 474 | |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 475 | |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 476 | /*! BMK_loadFiles() : |
| 477 | Loads `buffer` with content of files listed within `fileNamesTable`. |
| 478 | At most, fills `buffer` entirely */ |
| Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 479 | static void BMK_loadFiles(void* buffer, size_t bufferSize, |
| 480 | size_t* fileSizes, |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 481 | const char** fileNamesTable, unsigned nbFiles) |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 482 | { |
| inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 483 | size_t pos = 0, totalSize = 0; |
| Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 484 | unsigned n; |
| Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 485 | for (n=0; n<nbFiles; n++) { |
| Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 486 | FILE* f; |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 487 | U64 fileSize = UTIL_getFileSize(fileNamesTable[n]); |
| 488 | if (UTIL_isDirectory(fileNamesTable[n])) { |
| inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 489 | DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]); |
| inikep | bab4317 | 2016-04-29 15:19:40 +0200 | [diff] [blame] | 490 | fileSizes[n] = 0; |
| inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 491 | continue; |
| 492 | } |
| inikep | ea4ee3e | 2016-04-25 13:09:06 +0200 | [diff] [blame] | 493 | f = fopen(fileNamesTable[n], "rb"); |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 494 | if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]); |
| Yann Collet | bf2bc11 | 2016-08-02 23:48:13 +0200 | [diff] [blame] | 495 | DISPLAYUPDATE(2, "Loading %s... \r", fileNamesTable[n]); |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 496 | if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */ |
| 497 | { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f); |
| 498 | if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]); |
| 499 | pos += readSize; } |
| Yann Collet | a52c98d | 2015-12-16 03:12:31 +0100 | [diff] [blame] | 500 | fileSizes[n] = (size_t)fileSize; |
| inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 501 | totalSize += (size_t)fileSize; |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 502 | fclose(f); |
| 503 | } |
| Yann Collet | 6f9c056 | 2016-05-01 10:26:30 +0200 | [diff] [blame] | 504 | |
| inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 505 | if (totalSize == 0) EXM_THROW(12, "no data to bench"); |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 506 | } |
| 507 | |
| Przemyslaw Skibinski | d05014c | 2017-02-07 16:48:01 +0100 | [diff] [blame] | 508 | static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles, const char* dictFileName, int cLevel, |
| 509 | int cLevelLast, ZSTD_compressionParameters *compressionParams, int setRealTimePrio) |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 510 | { |
| 511 | void* srcBuffer; |
| 512 | size_t benchedSize; |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 513 | void* dictBuffer = NULL; |
| 514 | size_t dictBufferSize = 0; |
| 515 | size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t)); |
| Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 516 | U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles); |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 517 | char mfName[20] = {0}; |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 518 | |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 519 | if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes"); |
| 520 | |
| 521 | /* Load dictionary */ |
| Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 522 | if (dictFileName != NULL) { |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 523 | U64 dictFileSize = UTIL_getFileSize(dictFileName); |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 524 | if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName); |
| 525 | dictBufferSize = (size_t)dictFileSize; |
| 526 | dictBuffer = malloc(dictBufferSize); |
| 527 | if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize); |
| 528 | BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1); |
| 529 | } |
| 530 | |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 531 | /* Memory allocation & restrictions */ |
| 532 | benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3; |
| 533 | if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad; |
| 534 | if (benchedSize < totalSizeToLoad) |
| 535 | DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20)); |
| 536 | srcBuffer = malloc(benchedSize); |
| 537 | if (!srcBuffer) EXM_THROW(12, "not enough memory"); |
| 538 | |
| 539 | /* Load input buffer */ |
| Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 540 | BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles); |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 541 | |
| 542 | /* Bench */ |
| 543 | snprintf (mfName, sizeof(mfName), " %u files", nbFiles); |
| Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 544 | { const char* displayName = (nbFiles > 1) ? mfName : fileNamesTable[0]; |
| 545 | BMK_benchCLevel(srcBuffer, benchedSize, |
| 546 | displayName, cLevel, cLevelLast, |
| 547 | fileSizes, nbFiles, |
| Przemyslaw Skibinski | d05014c | 2017-02-07 16:48:01 +0100 | [diff] [blame] | 548 | dictBuffer, dictBufferSize, compressionParams, setRealTimePrio); |
| Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 549 | } |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 550 | |
| Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 551 | /* clean up */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 552 | free(srcBuffer); |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 553 | free(dictBuffer); |
| Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 554 | free(fileSizes); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | |
| Przemyslaw Skibinski | d05014c | 2017-02-07 16:48:01 +0100 | [diff] [blame] | 558 | static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility, ZSTD_compressionParameters* compressionParams, int setRealTimePrio) |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 559 | { |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 560 | char name[20] = {0}; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 561 | size_t benchedSize = 10000000; |
| Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 562 | void* const srcBuffer = malloc(benchedSize); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 563 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 564 | /* Memory allocation */ |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 565 | if (!srcBuffer) EXM_THROW(21, "not enough memory"); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 566 | |
| 567 | /* Fill input buffer */ |
| Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 568 | RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 569 | |
| 570 | /* Bench */ |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 571 | snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100)); |
| Przemyslaw Skibinski | d05014c | 2017-02-07 16:48:01 +0100 | [diff] [blame] | 572 | BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0, compressionParams, setRealTimePrio); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 573 | |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 574 | /* clean up */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 575 | free(srcBuffer); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | |
| Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 579 | int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, const char* dictFileName, |
| Przemyslaw Skibinski | d05014c | 2017-02-07 16:48:01 +0100 | [diff] [blame] | 580 | int cLevel, int cLevelLast, ZSTD_compressionParameters* compressionParams, int setRealTimePrio) |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 581 | { |
| Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 582 | double const compressibility = (double)g_compressibilityDefault / 100; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 583 | |
| Yann Collet | c1c040e | 2017-03-01 16:49:20 -0800 | [diff] [blame] | 584 | if (cLevel < 1) cLevel = 1; /* minimum compression level */ |
| Przemyslaw Skibinski | fd0ac93 | 2016-11-23 21:45:29 +0100 | [diff] [blame] | 585 | if (cLevel > ZSTD_maxCLevel()) cLevel = ZSTD_maxCLevel(); |
| 586 | if (cLevelLast > ZSTD_maxCLevel()) cLevelLast = ZSTD_maxCLevel(); |
| Przemyslaw Skibinski | 5ddcd9d | 2016-11-21 16:37:56 +0100 | [diff] [blame] | 587 | if (cLevelLast < cLevel) cLevelLast = cLevel; |
| Yann Collet | d946501 | 2016-12-06 16:49:23 -0800 | [diff] [blame] | 588 | if (cLevelLast > cLevel) DISPLAYLEVEL(2, "Benchmarking levels from %d to %d\n", cLevel, cLevelLast); |
| Przemyslaw Skibinski | 5ddcd9d | 2016-11-21 16:37:56 +0100 | [diff] [blame] | 589 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 590 | if (nbFiles == 0) |
| Przemyslaw Skibinski | d05014c | 2017-02-07 16:48:01 +0100 | [diff] [blame] | 591 | BMK_syntheticTest(cLevel, cLevelLast, compressibility, compressionParams, setRealTimePrio); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 592 | else |
| Przemyslaw Skibinski | d05014c | 2017-02-07 16:48:01 +0100 | [diff] [blame] | 593 | BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast, compressionParams, setRealTimePrio); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 594 | return 0; |
| 595 | } |