Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1 | /* |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 2 | bench.c - open-source compression benchmark module |
| 3 | Copyright (C) Yann Collet 2012-2016 |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 4 | |
| 5 | GPL v2 License |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License along |
| 18 | with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | |
| 21 | You can contact the author at : |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 22 | - zstd homepage : http://www.zstd.net |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 23 | - zstd source repository : https://github.com/Cyan4973/zstd |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 24 | */ |
| 25 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 26 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 27 | * Includes |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 28 | ***************************************/ |
inikep | 55d047a | 2016-04-28 16:50:13 +0200 | [diff] [blame] | 29 | #include "util.h" /* UTIL_GetFileSize */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 30 | #include <stdlib.h> /* malloc, free */ |
| 31 | #include <string.h> /* memset */ |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 32 | #include <stdio.h> /* fprintf, fopen, ftello64 */ |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 33 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 34 | #include "mem.h" |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 35 | #include "zstd_static.h" |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 36 | #include "datagen.h" /* RDG_genBuffer */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 37 | #include "xxhash.h" |
| 38 | |
| 39 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 40 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 41 | * Compiler specifics |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 42 | ***************************************/ |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 43 | #if defined(_MSC_VER) |
inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame^] | 44 | |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 45 | #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) |
| 46 | /* part of <stdio.h> */ |
| 47 | #else |
| 48 | extern int snprintf (char* s, size_t maxlen, const char* format, ...); /* not declared in <stdio.h> when C version < c99 */ |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 49 | #endif |
| 50 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 51 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 52 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 53 | * Constants |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 54 | ***************************************/ |
inikep | 44af12d | 2016-03-14 15:59:04 +0100 | [diff] [blame] | 55 | #ifndef ZSTD_VERSION |
| 56 | # define ZSTD_VERSION "" |
| 57 | #endif |
| 58 | |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 59 | #define NBLOOPS 3 |
| 60 | #define TIMELOOP_MICROSEC 1*1000000ULL /* 1 second */ |
| 61 | #define ACTIVEPERIOD_MICROSEC 70*1000000ULL /* 70 seconds */ |
| 62 | #define COOLPERIOD_SEC 10 |
inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame^] | 63 | #define MAX_LIST_SIZE (64*1024) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 64 | |
| 65 | #define KB *(1 <<10) |
| 66 | #define MB *(1 <<20) |
| 67 | #define GB *(1U<<30) |
| 68 | |
Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 69 | 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] | 70 | |
| 71 | static U32 g_compressibilityDefault = 50; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 72 | |
| 73 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 74 | /* ************************************* |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 75 | * console display |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 76 | ***************************************/ |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 77 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 78 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 79 | static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ |
| 80 | |
| 81 | |
| 82 | /* ************************************* |
| 83 | * Exceptions |
| 84 | ***************************************/ |
| 85 | #ifndef DEBUG |
| 86 | # define DEBUG 0 |
| 87 | #endif |
| 88 | #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); |
| 89 | #define EXM_THROW(error, ...) \ |
| 90 | { \ |
| 91 | DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ |
| 92 | DISPLAYLEVEL(1, "Error %i : ", error); \ |
| 93 | DISPLAYLEVEL(1, __VA_ARGS__); \ |
| 94 | DISPLAYLEVEL(1, "\n"); \ |
| 95 | exit(error); \ |
| 96 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 97 | |
| 98 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 99 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 100 | * Benchmark Parameters |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 101 | ***************************************/ |
Yann Collet | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 102 | static U32 g_nbIterations = NBLOOPS; |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 103 | static size_t g_blockSize = 0; |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 104 | int g_additionalParam = 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 | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 110 | void BMK_SetNbIterations(unsigned nbLoops) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 111 | { |
Yann Collet | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 112 | g_nbIterations = nbLoops; |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 113 | DISPLAYLEVEL(2, "- %i iterations -\n", g_nbIterations); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 116 | void BMK_SetBlockSize(size_t blockSize) |
| 117 | { |
| 118 | g_blockSize = blockSize; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 119 | DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10)); |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 122 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 123 | /* ******************************************************** |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 124 | * Bench functions |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 125 | **********************************************************/ |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 126 | typedef struct |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 127 | { |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 128 | const char* srcPtr; |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 129 | size_t srcSize; |
| 130 | char* cPtr; |
| 131 | size_t cRoom; |
| 132 | size_t cSize; |
| 133 | char* resPtr; |
| 134 | size_t resSize; |
| 135 | } blockParam_t; |
| 136 | |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 137 | typedef struct |
| 138 | { |
inikep | c034b73 | 2016-03-14 13:13:42 +0100 | [diff] [blame] | 139 | double ratio; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 140 | size_t cSize; |
inikep | c034b73 | 2016-03-14 13:13:42 +0100 | [diff] [blame] | 141 | double cSpeed; |
| 142 | double dSpeed; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 143 | } benchResult_t; |
| 144 | |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 145 | |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 146 | #define MIN(a,b) ((a)<(b) ? (a) : (b)) |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 147 | #define MAX(a,b) ((a)>(b) ? (a) : (b)) |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 148 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 149 | static int BMK_benchMem(const void* srcBuffer, size_t srcSize, |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 150 | const char* displayName, int cLevel, |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 151 | const size_t* fileSizes, U32 nbFiles, |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 152 | const void* dictBuffer, size_t dictBufferSize, benchResult_t *result) |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 153 | { |
inikep | 3865498 | 2016-04-21 12:18:47 +0200 | [diff] [blame] | 154 | size_t const blockSize = (g_blockSize>=32 ? g_blockSize : srcSize) + (!srcSize); /* avoid div by 0 */ |
Yann Collet | d64f435 | 2016-03-21 00:07:42 +0100 | [diff] [blame] | 155 | U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles; |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 156 | blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t)); |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 157 | 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] | 158 | void* const compressedBuffer = malloc(maxCompressedSize); |
| 159 | void* const resultBuffer = malloc(srcSize); |
Yann Collet | 2630a5e | 2016-01-14 19:13:22 +0100 | [diff] [blame] | 160 | ZSTD_CCtx* refCtx = ZSTD_createCCtx(); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 161 | ZSTD_CCtx* ctx = ZSTD_createCCtx(); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 162 | ZSTD_DCtx* refDCtx = ZSTD_createDCtx(); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 163 | ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 164 | U32 nbBlocks; |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 165 | UTIL_time_t ticksPerSecond; |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 166 | |
| 167 | /* checks */ |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 168 | if (!compressedBuffer || !resultBuffer || !blockTable || !refCtx || !ctx || !refDCtx || !dctx) |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 169 | EXM_THROW(31, "not enough memory"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 170 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 171 | /* init */ |
| 172 | if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */ |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 173 | UTIL_initTimer(ticksPerSecond); |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 174 | |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 175 | /* Init blockTable data */ |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 176 | { const char* srcPtr = (const char*)srcBuffer; |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 177 | char* cPtr = (char*)compressedBuffer; |
| 178 | char* resPtr = (char*)resultBuffer; |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 179 | U32 fileNb; |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 180 | for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) { |
Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 181 | size_t remaining = fileSizes[fileNb]; |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 182 | U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize); |
| 183 | U32 const blockEnd = nbBlocks + nbBlocksforThisFile; |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 184 | for ( ; nbBlocks<blockEnd; nbBlocks++) { |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 185 | size_t const thisBlockSize = MIN(remaining, blockSize); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 186 | blockTable[nbBlocks].srcPtr = srcPtr; |
| 187 | blockTable[nbBlocks].cPtr = cPtr; |
| 188 | blockTable[nbBlocks].resPtr = resPtr; |
| 189 | blockTable[nbBlocks].srcSize = thisBlockSize; |
| 190 | blockTable[nbBlocks].cRoom = ZSTD_compressBound(thisBlockSize); |
| 191 | srcPtr += thisBlockSize; |
| 192 | cPtr += blockTable[nbBlocks].cRoom; |
| 193 | resPtr += thisBlockSize; |
| 194 | remaining -= thisBlockSize; |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 195 | } } } |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 196 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 197 | /* warmimg up memory */ |
Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 198 | RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 199 | |
| 200 | /* Bench */ |
inikep | 6d157f1 | 2016-04-15 16:54:11 +0200 | [diff] [blame] | 201 | { U64 fastestC = (U64)(-1LL), fastestD = (U64)(-1LL); |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 202 | U64 const crcOrig = XXH64(srcBuffer, srcSize, 0); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 203 | U64 crcCheck = 0; |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 204 | UTIL_time_t coolTime; |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 205 | U32 testNb; |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 206 | size_t cSize = 0; |
| 207 | double ratio = 0.; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 208 | |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 209 | UTIL_getTime(coolTime); |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 210 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 211 | for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) { |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 212 | UTIL_time_t clockStart; |
| 213 | U64 clockLoop = g_nbIterations ? TIMELOOP_MICROSEC : 1; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 214 | |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 215 | /* overheat protection */ |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 216 | if (UTIL_clockSpanMicro(coolTime, ticksPerSecond) > ACTIVEPERIOD_MICROSEC) { |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 217 | DISPLAY("\rcooling down ... \r"); |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 218 | UTIL_sleep(COOLPERIOD_SEC); |
| 219 | UTIL_getTime(coolTime); |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 220 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 221 | |
| 222 | /* Compression */ |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 223 | DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->\r", testNb, displayName, (U32)srcSize); |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 224 | memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 225 | |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 226 | UTIL_sleepMilli(1); /* give processor time to other processes */ |
| 227 | UTIL_waitForNextTick(ticksPerSecond); |
| 228 | UTIL_getTime(clockStart); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 229 | |
inikep | c5e1d29 | 2016-04-19 09:37:59 +0200 | [diff] [blame] | 230 | { U32 nbLoops = 0; |
| 231 | do { |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 232 | U32 blockNb; |
inikep | 4b3c5ee | 2016-04-14 13:43:51 +0200 | [diff] [blame] | 233 | { ZSTD_parameters params; |
| 234 | params.cParams = ZSTD_getCParams(cLevel, blockSize, dictBufferSize); |
| 235 | params.fParams.contentSizeFlag = 1; |
| 236 | ZSTD_adjustCParams(¶ms.cParams, blockSize, dictBufferSize); |
| 237 | { size_t const initResult = ZSTD_compressBegin_advanced(refCtx, dictBuffer, dictBufferSize, params, blockSize); |
| 238 | if (ZSTD_isError(initResult)) break; |
| 239 | } } |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 240 | for (blockNb=0; blockNb<nbBlocks; blockNb++) { |
| 241 | size_t const rSize = ZSTD_compress_usingPreparedCCtx(ctx, refCtx, |
| 242 | blockTable[blockNb].cPtr, blockTable[blockNb].cRoom, |
| 243 | blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize); |
| 244 | if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingPreparedCCtx() failed : %s", ZSTD_getErrorName(rSize)); |
| 245 | blockTable[blockNb].cSize = rSize; |
inikep | c5e1d29 | 2016-04-19 09:37:59 +0200 | [diff] [blame] | 246 | } |
| 247 | nbLoops++; |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 248 | } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop); |
| 249 | { U64 const clockSpan = UTIL_clockSpanMicro(clockStart, ticksPerSecond); |
inikep | 6d157f1 | 2016-04-15 16:54:11 +0200 | [diff] [blame] | 250 | if (clockSpan < fastestC*nbLoops) fastestC = clockSpan / nbLoops; |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 251 | } } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 252 | |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 253 | cSize = 0; |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 254 | { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; } |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 255 | ratio = (double)srcSize / (double)cSize; |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 256 | DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r", |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 257 | testNb, displayName, (U32)srcSize, (U32)cSize, ratio, |
inikep | 06f793a | 2016-03-29 11:17:58 +0200 | [diff] [blame] | 258 | (double)srcSize / fastestC ); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 259 | |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 260 | (void)fastestD; (void)crcOrig; /* unused when decompression disabled */ |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 261 | #if 1 |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 262 | /* Decompression */ |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 263 | memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 264 | |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 265 | UTIL_sleepMilli(1); /* give processor time to other processes */ |
| 266 | UTIL_waitForNextTick(ticksPerSecond); |
| 267 | UTIL_getTime(clockStart); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 268 | |
inikep | c5e1d29 | 2016-04-19 09:37:59 +0200 | [diff] [blame] | 269 | { U32 nbLoops = 0; |
| 270 | do { |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 271 | U32 blockNb; |
| 272 | ZSTD_decompressBegin_usingDict(refDCtx, dictBuffer, dictBufferSize); |
| 273 | for (blockNb=0; blockNb<nbBlocks; blockNb++) { |
| 274 | size_t const regenSize = ZSTD_decompress_usingPreparedDCtx(dctx, refDCtx, |
| 275 | blockTable[blockNb].resPtr, blockTable[blockNb].srcSize, |
| 276 | blockTable[blockNb].cPtr, blockTable[blockNb].cSize); |
| 277 | if (ZSTD_isError(regenSize)) { |
| 278 | DISPLAY("ZSTD_decompress_usingPreparedDCtx() failed on block %u : %s \n", |
| 279 | blockNb, ZSTD_getErrorName(regenSize)); |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 280 | clockLoop = 0; /* force immediate test end */ |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 281 | break; |
| 282 | } |
| 283 | blockTable[blockNb].resSize = regenSize; |
inikep | c5e1d29 | 2016-04-19 09:37:59 +0200 | [diff] [blame] | 284 | } |
| 285 | nbLoops++; |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 286 | } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop); |
| 287 | { U64 const clockSpan = UTIL_clockSpanMicro(clockStart, ticksPerSecond); |
inikep | 6d157f1 | 2016-04-15 16:54:11 +0200 | [diff] [blame] | 288 | if (clockSpan < fastestD*nbLoops) fastestD = clockSpan / nbLoops; |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 289 | } } |
| 290 | |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 291 | DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r", |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 292 | testNb, displayName, (U32)srcSize, (U32)cSize, ratio, |
inikep | 06f793a | 2016-03-29 11:17:58 +0200 | [diff] [blame] | 293 | (double)srcSize / fastestC, |
| 294 | (double)srcSize / fastestD ); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 295 | |
| 296 | /* CRC Checking */ |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 297 | { crcCheck = XXH64(resultBuffer, srcSize, 0); |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 298 | if (crcOrig!=crcCheck) { |
| 299 | size_t u; |
| 300 | DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck); |
| 301 | for (u=0; u<srcSize; u++) { |
| 302 | if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) { |
| 303 | U32 segNb, bNb, pos; |
| 304 | size_t bacc = 0; |
| 305 | DISPLAY("Decoding error at pos %u ", (U32)u); |
| 306 | for (segNb = 0; segNb < nbBlocks; segNb++) { |
| 307 | if (bacc + blockTable[segNb].srcSize > u) break; |
| 308 | bacc += blockTable[segNb].srcSize; |
| 309 | } |
| 310 | pos = (U32)(u - bacc); |
| 311 | bNb = pos / (128 KB); |
| 312 | DISPLAY("(block %u, sub %u, pos %u) \n", segNb, bNb, pos); |
| 313 | break; |
Yann Collet | 03a6dab | 2016-01-21 02:21:17 +0100 | [diff] [blame] | 314 | } |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 315 | if (u==srcSize-1) { /* should never happen */ |
| 316 | DISPLAY("no difference detected\n"); |
| 317 | } } |
| 318 | break; |
| 319 | } } /* CRC Checking */ |
Yann Collet | e8c6bb1 | 2015-07-26 00:23:57 +0100 | [diff] [blame] | 320 | #endif |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 321 | } /* for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 322 | |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 323 | if (crcOrig == crcCheck) { |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 324 | result->ratio = ratio; |
| 325 | result->cSize = cSize; |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 326 | result->cSpeed = (double)srcSize / fastestC; |
inikep | 06f793a | 2016-03-29 11:17:58 +0200 | [diff] [blame] | 327 | result->dSpeed = (double)srcSize / fastestD; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 328 | } |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 329 | DISPLAYLEVEL(2, "%2i#\n", cLevel); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 330 | } /* Bench */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 331 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 332 | /* clean up */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 333 | free(compressedBuffer); |
| 334 | free(resultBuffer); |
Yann Collet | 2630a5e | 2016-01-14 19:13:22 +0100 | [diff] [blame] | 335 | ZSTD_freeCCtx(refCtx); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 336 | ZSTD_freeCCtx(ctx); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 337 | ZSTD_freeDCtx(refDCtx); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 338 | ZSTD_freeDCtx(dctx); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 343 | static size_t BMK_findMaxMem(U64 requiredMem) |
| 344 | { |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 345 | size_t const step = 64 MB; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 346 | BYTE* testmem = NULL; |
| 347 | |
| 348 | requiredMem = (((requiredMem >> 26) + 1) << 26); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 349 | requiredMem += step; |
Yann Collet | 050efba | 2015-11-03 09:49:30 +0100 | [diff] [blame] | 350 | if (requiredMem > maxMemory) requiredMem = maxMemory; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 351 | |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 352 | do { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 353 | testmem = (BYTE*)malloc((size_t)requiredMem); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 354 | requiredMem -= step; |
| 355 | } while (!testmem); |
| 356 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 357 | free(testmem); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 358 | return (size_t)(requiredMem); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 359 | } |
| 360 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 361 | static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize, |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 362 | const char* displayName, int cLevel, int cLevelLast, |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 363 | const size_t* fileSizes, unsigned nbFiles, |
| 364 | const void* dictBuffer, size_t dictBufferSize) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 365 | { |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 366 | benchResult_t result, total; |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 367 | int l; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 368 | |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 369 | const char* pch = strrchr(displayName, '\\'); /* Windows */ |
| 370 | if (!pch) pch = strrchr(displayName, '/'); /* Linux */ |
| 371 | if (pch) displayName = pch+1; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 372 | |
inikep | ea4ee3e | 2016-04-25 13:09:06 +0200 | [diff] [blame] | 373 | SET_HIGH_PRIORITY; |
| 374 | |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 375 | memset(&result, 0, sizeof(result)); |
| 376 | memset(&total, 0, sizeof(total)); |
inikep | 5fdd0b4 | 2016-03-14 19:51:11 +0100 | [diff] [blame] | 377 | |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 378 | if (g_displayLevel == 1 && !g_additionalParam) |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 379 | DISPLAY("bench %s: input %u bytes, %i iterations, %u KB blocks\n", ZSTD_VERSION, (U32)benchedSize, g_nbIterations, (U32)(g_blockSize>>10)); |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 380 | |
| 381 | if (cLevelLast < cLevel) cLevelLast = cLevel; |
| 382 | |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 383 | for (l=cLevel; l <= cLevelLast; l++) { |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 384 | BMK_benchMem(srcBuffer, benchedSize, |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 385 | displayName, l, |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 386 | fileSizes, nbFiles, |
| 387 | dictBuffer, dictBufferSize, &result); |
| 388 | if (g_displayLevel == 1) { |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 389 | if (g_additionalParam) |
inikep | 3865498 | 2016-04-21 12:18:47 +0200 | [diff] [blame] | 390 | DISPLAY("%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s (param=%d)\n", -l, (int)result.cSize, result.ratio, result.cSpeed, result.dSpeed, displayName, g_additionalParam); |
inikep | d700a1a | 2016-03-15 12:18:44 +0100 | [diff] [blame] | 391 | else |
inikep | 3865498 | 2016-04-21 12:18:47 +0200 | [diff] [blame] | 392 | DISPLAY("%-3i%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", -l, (int)result.cSize, result.ratio, result.cSpeed, result.dSpeed, displayName); |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 393 | total.cSize += result.cSize; |
| 394 | total.cSpeed += result.cSpeed; |
| 395 | total.dSpeed += result.dSpeed; |
| 396 | total.ratio += result.ratio; |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 397 | } } |
| 398 | if (g_displayLevel == 1 && cLevelLast > cLevel) { |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 399 | total.cSize /= 1+cLevelLast-cLevel; |
| 400 | total.cSpeed /= 1+cLevelLast-cLevel; |
| 401 | total.dSpeed /= 1+cLevelLast-cLevel; |
| 402 | total.ratio /= 1+cLevelLast-cLevel; |
inikep | 3865498 | 2016-04-21 12:18:47 +0200 | [diff] [blame] | 403 | DISPLAY("avg%11i (%5.3f) %6.2f MB/s %6.1f MB/s %s\n", (int)total.cSize, total.ratio, total.cSpeed, total.dSpeed, displayName); |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 404 | } |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 405 | } |
| 406 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 407 | |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 408 | /*! BMK_loadFiles() : |
| 409 | Loads `buffer` with content of files listed within `fileNamesTable`. |
| 410 | At most, fills `buffer` entirely */ |
Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 411 | static void BMK_loadFiles(void* buffer, size_t bufferSize, |
| 412 | size_t* fileSizes, |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 413 | const char** fileNamesTable, unsigned nbFiles) |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 414 | { |
inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 415 | size_t pos = 0, totalSize = 0; |
inikep | ea4ee3e | 2016-04-25 13:09:06 +0200 | [diff] [blame] | 416 | FILE* f; |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 417 | unsigned n; |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 418 | for (n=0; n<nbFiles; n++) { |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 419 | U64 fileSize = UTIL_getFileSize(fileNamesTable[n]); |
| 420 | if (UTIL_isDirectory(fileNamesTable[n])) { |
inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 421 | DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]); |
inikep | bab4317 | 2016-04-29 15:19:40 +0200 | [diff] [blame] | 422 | fileSizes[n] = 0; |
inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 423 | continue; |
| 424 | } |
inikep | ea4ee3e | 2016-04-25 13:09:06 +0200 | [diff] [blame] | 425 | f = fopen(fileNamesTable[n], "rb"); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 426 | if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]); |
| 427 | DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[n]); |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 428 | if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */ |
| 429 | { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f); |
| 430 | if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]); |
| 431 | pos += readSize; } |
Yann Collet | a52c98d | 2015-12-16 03:12:31 +0100 | [diff] [blame] | 432 | fileSizes[n] = (size_t)fileSize; |
inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 433 | totalSize += (size_t)fileSize; |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 434 | fclose(f); |
| 435 | } |
inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 436 | |
| 437 | if (totalSize == 0) EXM_THROW(12, "no data to bench"); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 438 | } |
| 439 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 440 | static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles, |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 441 | const char* dictFileName, int cLevel, int cLevelLast) |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 442 | { |
| 443 | void* srcBuffer; |
| 444 | size_t benchedSize; |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 445 | void* dictBuffer = NULL; |
| 446 | size_t dictBufferSize = 0; |
| 447 | size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t)); |
inikep | 55d047a | 2016-04-28 16:50:13 +0200 | [diff] [blame] | 448 | U64 totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 449 | char mfName[20] = {0}; |
| 450 | const char* displayName = NULL; |
| 451 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 452 | if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes"); |
| 453 | |
| 454 | /* Load dictionary */ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 455 | if (dictFileName != NULL) { |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 456 | U64 dictFileSize = UTIL_getFileSize(dictFileName); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 457 | if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName); |
| 458 | dictBufferSize = (size_t)dictFileSize; |
| 459 | dictBuffer = malloc(dictBufferSize); |
| 460 | if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize); |
| 461 | BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1); |
| 462 | } |
| 463 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 464 | /* Memory allocation & restrictions */ |
| 465 | benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3; |
| 466 | if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad; |
| 467 | if (benchedSize < totalSizeToLoad) |
| 468 | DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20)); |
| 469 | srcBuffer = malloc(benchedSize); |
| 470 | if (!srcBuffer) EXM_THROW(12, "not enough memory"); |
| 471 | |
| 472 | /* Load input buffer */ |
Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 473 | BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 474 | |
| 475 | /* Bench */ |
| 476 | snprintf (mfName, sizeof(mfName), " %u files", nbFiles); |
| 477 | if (nbFiles > 1) displayName = mfName; |
| 478 | else displayName = fileNamesTable[0]; |
| 479 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 480 | BMK_benchCLevel(srcBuffer, benchedSize, |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 481 | displayName, cLevel, cLevelLast, |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 482 | fileSizes, nbFiles, |
| 483 | dictBuffer, dictBufferSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 484 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 485 | /* clean up */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 486 | free(srcBuffer); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 487 | free(dictBuffer); |
Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 488 | free(fileSizes); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 492 | static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 493 | { |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 494 | char name[20] = {0}; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 495 | size_t benchedSize = 10000000; |
| 496 | void* srcBuffer = malloc(benchedSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 497 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 498 | /* Memory allocation */ |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 499 | if (!srcBuffer) EXM_THROW(21, "not enough memory"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 500 | |
| 501 | /* Fill input buffer */ |
Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 502 | RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 503 | |
| 504 | /* Bench */ |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 505 | snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100)); |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 506 | BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 507 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 508 | /* clean up */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 509 | free(srcBuffer); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 513 | int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 514 | const char* dictFileName, int cLevel, int cLevelLast) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 515 | { |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 516 | double const compressibility = (double)g_compressibilityDefault / 100; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 517 | |
| 518 | if (nbFiles == 0) |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 519 | BMK_syntheticTest(cLevel, cLevelLast, compressibility); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 520 | else |
inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame^] | 521 | { |
| 522 | #ifdef UTIL_HAS_CREATEFILELIST |
| 523 | char* buf; |
| 524 | const char** filenameTable; |
| 525 | unsigned i; |
| 526 | nbFiles = UTIL_createFileList(fileNamesTable, nbFiles, MAX_LIST_SIZE, &filenameTable, &buf); |
| 527 | if (filenameTable) { |
| 528 | for (i=0; i<nbFiles; i++) printf ("%d %s\n", i, filenameTable[i]); |
| 529 | BMK_benchFileTable(filenameTable, nbFiles, dictFileName, cLevel, cLevelLast); |
| 530 | UTIL_freeFileList(filenameTable, buf); |
| 531 | } |
| 532 | #else |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 533 | BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast); |
inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame^] | 534 | #endif |
| 535 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | |