| 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 | 957823f | 2016-05-25 15:30:55 +0200 | [diff] [blame] | 29 | #include "util.h" /* Compiler options, UTIL_GetFileSize, UTIL_sleep */ |
| 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 */ |
| Yann Collet | bf2bc11 | 2016-08-02 23:48:13 +0200 | [diff] [blame^] | 33 | #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */ |
| inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 34 | |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 35 | #include "mem.h" |
| Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 36 | #define ZSTD_STATIC_LINKING_ONLY |
| 37 | #include "zstd.h" |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 38 | #include "datagen.h" /* RDG_genBuffer */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 39 | #include "xxhash.h" |
| 40 | |
| 41 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 42 | |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 43 | /* ************************************* |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 44 | * Constants |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 45 | ***************************************/ |
| inikep | f2f59d7 | 2016-06-22 15:42:26 +0200 | [diff] [blame] | 46 | #ifndef ZSTD_GIT_COMMIT |
| inikep | d7d251c | 2016-06-22 16:13:25 +0200 | [diff] [blame] | 47 | # define ZSTD_GIT_COMMIT_STRING "" |
| 48 | #else |
| 49 | # define ZSTD_GIT_COMMIT_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_GIT_COMMIT) |
| inikep | f2f59d7 | 2016-06-22 15:42:26 +0200 | [diff] [blame] | 50 | #endif |
| 51 | |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 52 | #define NBLOOPS 3 |
| 53 | #define TIMELOOP_MICROSEC 1*1000000ULL /* 1 second */ |
| 54 | #define ACTIVEPERIOD_MICROSEC 70*1000000ULL /* 70 seconds */ |
| 55 | #define COOLPERIOD_SEC 10 |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 56 | |
| 57 | #define KB *(1 <<10) |
| 58 | #define MB *(1 <<20) |
| 59 | #define GB *(1U<<30) |
| 60 | |
| Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 61 | 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] | 62 | |
| 63 | static U32 g_compressibilityDefault = 50; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 64 | |
| 65 | |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 66 | /* ************************************* |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 67 | * console display |
| 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 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 70 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 71 | static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ |
| 72 | |
| Yann Collet | bf2bc11 | 2016-08-02 23:48:13 +0200 | [diff] [blame^] | 73 | #define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \ |
| 74 | if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \ |
| 75 | { g_time = clock(); DISPLAY(__VA_ARGS__); \ |
| 76 | if (g_displayLevel>=4) fflush(stdout); } } |
| 77 | static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100; |
| 78 | static clock_t g_time = 0; |
| 79 | |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 80 | |
| 81 | /* ************************************* |
| 82 | * Exceptions |
| 83 | ***************************************/ |
| 84 | #ifndef DEBUG |
| 85 | # define DEBUG 0 |
| 86 | #endif |
| 87 | #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); |
| 88 | #define EXM_THROW(error, ...) \ |
| 89 | { \ |
| 90 | DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ |
| 91 | DISPLAYLEVEL(1, "Error %i : ", error); \ |
| 92 | DISPLAYLEVEL(1, __VA_ARGS__); \ |
| 93 | DISPLAYLEVEL(1, "\n"); \ |
| 94 | exit(error); \ |
| 95 | } |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 96 | |
| 97 | |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 98 | /* ************************************* |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 99 | * Benchmark Parameters |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 100 | ***************************************/ |
| Yann Collet | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 101 | static U32 g_nbIterations = NBLOOPS; |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 102 | static size_t g_blockSize = 0; |
| inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 103 | int g_additionalParam = 0; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 104 | |
| inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 105 | void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; } |
| 106 | |
| inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 107 | void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; } |
| 108 | |
| Yann Collet | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 109 | void BMK_SetNbIterations(unsigned nbLoops) |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 110 | { |
| Yann Collet | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 111 | g_nbIterations = nbLoops; |
| Yann Collet | de4c04f | 2016-08-02 11:27:05 +0200 | [diff] [blame] | 112 | DISPLAYLEVEL(3, "- test >= %i seconds per compression / decompression -\n", g_nbIterations); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 115 | void BMK_SetBlockSize(size_t blockSize) |
| 116 | { |
| 117 | g_blockSize = blockSize; |
| inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 118 | DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10)); |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 121 | |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 122 | /* ******************************************************** |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 123 | * Bench functions |
| Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 124 | **********************************************************/ |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 125 | typedef struct |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 126 | { |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 127 | const char* srcPtr; |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 128 | size_t srcSize; |
| 129 | char* cPtr; |
| 130 | size_t cRoom; |
| 131 | size_t cSize; |
| 132 | char* resPtr; |
| 133 | size_t resSize; |
| 134 | } blockParam_t; |
| 135 | |
| inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 136 | typedef struct |
| 137 | { |
| inikep | c034b73 | 2016-03-14 13:13:42 +0100 | [diff] [blame] | 138 | double ratio; |
| inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 139 | size_t cSize; |
| inikep | c034b73 | 2016-03-14 13:13:42 +0100 | [diff] [blame] | 140 | double cSpeed; |
| 141 | double dSpeed; |
| inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 142 | } benchResult_t; |
| 143 | |
| Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 144 | |
| Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 145 | #define MIN(a,b) ((a)<(b) ? (a) : (b)) |
| Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 146 | #define MAX(a,b) ((a)>(b) ? (a) : (b)) |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 147 | |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 148 | static int BMK_benchMem(const void* srcBuffer, size_t srcSize, |
| inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 149 | const char* displayName, int cLevel, |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 150 | const size_t* fileSizes, U32 nbFiles, |
| inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 151 | const void* dictBuffer, size_t dictBufferSize, benchResult_t *result) |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 152 | { |
| Yann Collet | 3c242e7 | 2016-07-13 14:56:24 +0200 | [diff] [blame] | 153 | 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] | 154 | U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles; |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 155 | blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t)); |
| Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 156 | 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] | 157 | void* const compressedBuffer = malloc(maxCompressedSize); |
| 158 | void* const resultBuffer = malloc(srcSize); |
| Yann Collet | 3c242e7 | 2016-07-13 14:56:24 +0200 | [diff] [blame] | 159 | ZSTD_CCtx* const ctx = ZSTD_createCCtx(); |
| 160 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 161 | U32 nbBlocks; |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 162 | UTIL_time_t ticksPerSecond; |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 163 | |
| 164 | /* checks */ |
| Yann Collet | ec224d2 | 2016-06-27 13:39:30 +0200 | [diff] [blame] | 165 | if (!compressedBuffer || !resultBuffer || !blockTable || !ctx || !dctx) |
| Yann Collet | 3d2cd7f | 2016-06-27 15:12:26 +0200 | [diff] [blame] | 166 | EXM_THROW(31, "allocation error : not enough memory"); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 167 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 168 | /* init */ |
| 169 | if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */ |
| inikep | aaaf923 | 2016-05-09 16:19:25 +0200 | [diff] [blame] | 170 | UTIL_initTimer(&ticksPerSecond); |
| inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 171 | |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 172 | /* Init blockTable data */ |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 173 | { const char* srcPtr = (const char*)srcBuffer; |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 174 | char* cPtr = (char*)compressedBuffer; |
| 175 | char* resPtr = (char*)resultBuffer; |
| Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 176 | U32 fileNb; |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 177 | for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) { |
| Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 178 | size_t remaining = fileSizes[fileNb]; |
| Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 179 | U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize); |
| 180 | U32 const blockEnd = nbBlocks + nbBlocksforThisFile; |
| Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 181 | for ( ; nbBlocks<blockEnd; nbBlocks++) { |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 182 | size_t const thisBlockSize = MIN(remaining, blockSize); |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 183 | blockTable[nbBlocks].srcPtr = srcPtr; |
| 184 | blockTable[nbBlocks].cPtr = cPtr; |
| 185 | blockTable[nbBlocks].resPtr = resPtr; |
| 186 | blockTable[nbBlocks].srcSize = thisBlockSize; |
| 187 | blockTable[nbBlocks].cRoom = ZSTD_compressBound(thisBlockSize); |
| 188 | srcPtr += thisBlockSize; |
| 189 | cPtr += blockTable[nbBlocks].cRoom; |
| 190 | resPtr += thisBlockSize; |
| 191 | remaining -= thisBlockSize; |
| Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 192 | } } } |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 193 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 194 | /* warmimg up memory */ |
| Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 195 | RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 196 | |
| 197 | /* Bench */ |
| inikep | 6d157f1 | 2016-04-15 16:54:11 +0200 | [diff] [blame] | 198 | { U64 fastestC = (U64)(-1LL), fastestD = (U64)(-1LL); |
| Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 199 | U64 const crcOrig = XXH64(srcBuffer, srcSize, 0); |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 200 | UTIL_time_t coolTime; |
| Yann Collet | a9febe8 | 2016-08-01 13:37:17 +0200 | [diff] [blame] | 201 | U64 const maxTime = (g_nbIterations * TIMELOOP_MICROSEC) + 100; |
| 202 | U64 totalCTime=0, totalDTime=0; |
| 203 | U32 cCompleted=0, dCompleted=0; |
| 204 | # define NB_MARKS 4 |
| 205 | const char* const marks[NB_MARKS] = { " |", " /", " =", "\\" }; |
| 206 | U32 markNb = 0; |
| inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 207 | size_t cSize = 0; |
| 208 | double ratio = 0.; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 209 | |
| inikep | aaaf923 | 2016-05-09 16:19:25 +0200 | [diff] [blame] | 210 | UTIL_getTime(&coolTime); |
| inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 211 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
| Yann Collet | a9febe8 | 2016-08-01 13:37:17 +0200 | [diff] [blame] | 212 | while (!cCompleted | !dCompleted) { |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 213 | UTIL_time_t clockStart; |
| 214 | U64 clockLoop = g_nbIterations ? TIMELOOP_MICROSEC : 1; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 215 | |
| Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 216 | /* overheat protection */ |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 217 | if (UTIL_clockSpanMicro(coolTime, ticksPerSecond) > ACTIVEPERIOD_MICROSEC) { |
| Yann Collet | 235911e | 2016-07-31 01:32:48 +0200 | [diff] [blame] | 218 | DISPLAYLEVEL(2, "\rcooling down ... \r"); |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 219 | UTIL_sleep(COOLPERIOD_SEC); |
| inikep | aaaf923 | 2016-05-09 16:19:25 +0200 | [diff] [blame] | 220 | UTIL_getTime(&coolTime); |
| Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 221 | } |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 222 | |
| 223 | /* Compression */ |
| Yann Collet | a9febe8 | 2016-08-01 13:37:17 +0200 | [diff] [blame] | 224 | DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (U32)srcSize); |
| 225 | if (!cCompleted) memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 226 | |
| Yann Collet | 3d2cd7f | 2016-06-27 15:12:26 +0200 | [diff] [blame] | 227 | UTIL_sleepMilli(1); /* give processor time to other processes */ |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 228 | UTIL_waitForNextTick(ticksPerSecond); |
| inikep | aaaf923 | 2016-05-09 16:19:25 +0200 | [diff] [blame] | 229 | UTIL_getTime(&clockStart); |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 230 | |
| Yann Collet | a9febe8 | 2016-08-01 13:37:17 +0200 | [diff] [blame] | 231 | if (!cCompleted) { /* still some time to do compression tests */ |
| Yann Collet | 3c242e7 | 2016-07-13 14:56:24 +0200 | [diff] [blame] | 232 | ZSTD_parameters const zparams = ZSTD_getParams(cLevel, blockSize, dictBufferSize); |
| Yann Collet | 3d2cd7f | 2016-06-27 15:12:26 +0200 | [diff] [blame] | 233 | ZSTD_customMem const cmem = { NULL, NULL, NULL }; |
| 234 | U32 nbLoops = 0; |
| 235 | ZSTD_CDict* cdict = ZSTD_createCDict_advanced(dictBuffer, dictBufferSize, zparams, cmem); |
| Yann Collet | 3c242e7 | 2016-07-13 14:56:24 +0200 | [diff] [blame] | 236 | if (cdict==NULL) EXM_THROW(1, "ZSTD_createCDict_advanced() allocation failure"); |
| inikep | c5e1d29 | 2016-04-19 09:37:59 +0200 | [diff] [blame] | 237 | do { |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 238 | U32 blockNb; |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 239 | for (blockNb=0; blockNb<nbBlocks; blockNb++) { |
| Yann Collet | ee1a084 | 2016-06-07 01:40:49 +0200 | [diff] [blame] | 240 | size_t const rSize = ZSTD_compress_usingCDict(ctx, |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 241 | blockTable[blockNb].cPtr, blockTable[blockNb].cRoom, |
| Yann Collet | ee1a084 | 2016-06-07 01:40:49 +0200 | [diff] [blame] | 242 | blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize, |
| 243 | cdict); |
| Yann Collet | 4c56f4a | 2016-06-27 13:36:54 +0200 | [diff] [blame] | 244 | if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingCDict() failed : %s", ZSTD_getErrorName(rSize)); |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 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); |
| Yann Collet | ee1a084 | 2016-06-07 01:40:49 +0200 | [diff] [blame] | 249 | ZSTD_freeCDict(cdict); |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 250 | { U64 const clockSpan = UTIL_clockSpanMicro(clockStart, ticksPerSecond); |
| inikep | 6d157f1 | 2016-04-15 16:54:11 +0200 | [diff] [blame] | 251 | if (clockSpan < fastestC*nbLoops) fastestC = clockSpan / nbLoops; |
| Yann Collet | a9febe8 | 2016-08-01 13:37:17 +0200 | [diff] [blame] | 252 | totalCTime += clockSpan; |
| 253 | cCompleted = totalCTime>maxTime; |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 254 | } } |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 255 | |
| Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 256 | cSize = 0; |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 257 | { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; } |
| Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 258 | ratio = (double)srcSize / (double)cSize; |
| Yann Collet | a9febe8 | 2016-08-01 13:37:17 +0200 | [diff] [blame] | 259 | markNb = (markNb+1) % NB_MARKS; |
| 260 | DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s\r", |
| 261 | marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratio, |
| inikep | 06f793a | 2016-03-29 11:17:58 +0200 | [diff] [blame] | 262 | (double)srcSize / fastestC ); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 263 | |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 264 | (void)fastestD; (void)crcOrig; /* unused when decompression disabled */ |
| Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 265 | #if 1 |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 266 | /* Decompression */ |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 267 | memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 268 | |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 269 | UTIL_sleepMilli(1); /* give processor time to other processes */ |
| 270 | UTIL_waitForNextTick(ticksPerSecond); |
| inikep | aaaf923 | 2016-05-09 16:19:25 +0200 | [diff] [blame] | 271 | UTIL_getTime(&clockStart); |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 272 | |
| Yann Collet | a9febe8 | 2016-08-01 13:37:17 +0200 | [diff] [blame] | 273 | if (!dCompleted) { |
| 274 | U32 nbLoops = 0; |
| Yann Collet | ee1a084 | 2016-06-07 01:40:49 +0200 | [diff] [blame] | 275 | ZSTD_DDict* ddict = ZSTD_createDDict(dictBuffer, dictBufferSize); |
| 276 | if (!ddict) EXM_THROW(2, "ZSTD_createDDict() allocation failure"); |
| inikep | c5e1d29 | 2016-04-19 09:37:59 +0200 | [diff] [blame] | 277 | do { |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 278 | U32 blockNb; |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 279 | for (blockNb=0; blockNb<nbBlocks; blockNb++) { |
| Yann Collet | ee1a084 | 2016-06-07 01:40:49 +0200 | [diff] [blame] | 280 | size_t const regenSize = ZSTD_decompress_usingDDict(dctx, |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 281 | blockTable[blockNb].resPtr, blockTable[blockNb].srcSize, |
| Yann Collet | ee1a084 | 2016-06-07 01:40:49 +0200 | [diff] [blame] | 282 | blockTable[blockNb].cPtr, blockTable[blockNb].cSize, |
| 283 | ddict); |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 284 | if (ZSTD_isError(regenSize)) { |
| Yann Collet | 4c56f4a | 2016-06-27 13:36:54 +0200 | [diff] [blame] | 285 | DISPLAY("ZSTD_decompress_usingDDict() failed on block %u : %s \n", |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 286 | blockNb, ZSTD_getErrorName(regenSize)); |
| inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 287 | clockLoop = 0; /* force immediate test end */ |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 288 | break; |
| 289 | } |
| 290 | blockTable[blockNb].resSize = regenSize; |
| inikep | c5e1d29 | 2016-04-19 09:37:59 +0200 | [diff] [blame] | 291 | } |
| 292 | nbLoops++; |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 293 | } while (UTIL_clockSpanMicro(clockStart, ticksPerSecond) < clockLoop); |
| Yann Collet | ee1a084 | 2016-06-07 01:40:49 +0200 | [diff] [blame] | 294 | ZSTD_freeDDict(ddict); |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 295 | { U64 const clockSpan = UTIL_clockSpanMicro(clockStart, ticksPerSecond); |
| inikep | 6d157f1 | 2016-04-15 16:54:11 +0200 | [diff] [blame] | 296 | if (clockSpan < fastestD*nbLoops) fastestD = clockSpan / nbLoops; |
| Yann Collet | a9febe8 | 2016-08-01 13:37:17 +0200 | [diff] [blame] | 297 | totalDTime += clockSpan; |
| 298 | dCompleted = totalDTime>maxTime; |
| Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 299 | } } |
| 300 | |
| Yann Collet | a9febe8 | 2016-08-01 13:37:17 +0200 | [diff] [blame] | 301 | markNb = (markNb+1) % NB_MARKS; |
| 302 | DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.3f),%6.1f MB/s ,%6.1f MB/s\r", |
| 303 | marks[markNb], displayName, (U32)srcSize, (U32)cSize, ratio, |
| inikep | 06f793a | 2016-03-29 11:17:58 +0200 | [diff] [blame] | 304 | (double)srcSize / fastestC, |
| 305 | (double)srcSize / fastestD ); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 306 | |
| 307 | /* CRC Checking */ |
| Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 308 | { U64 const crcCheck = XXH64(resultBuffer, srcSize, 0); |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 309 | if (crcOrig!=crcCheck) { |
| 310 | size_t u; |
| 311 | DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck); |
| 312 | for (u=0; u<srcSize; u++) { |
| 313 | if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) { |
| 314 | U32 segNb, bNb, pos; |
| 315 | size_t bacc = 0; |
| 316 | DISPLAY("Decoding error at pos %u ", (U32)u); |
| 317 | for (segNb = 0; segNb < nbBlocks; segNb++) { |
| 318 | if (bacc + blockTable[segNb].srcSize > u) break; |
| 319 | bacc += blockTable[segNb].srcSize; |
| 320 | } |
| 321 | pos = (U32)(u - bacc); |
| 322 | bNb = pos / (128 KB); |
| 323 | DISPLAY("(block %u, sub %u, pos %u) \n", segNb, bNb, pos); |
| 324 | break; |
| Yann Collet | 03a6dab | 2016-01-21 02:21:17 +0100 | [diff] [blame] | 325 | } |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 326 | if (u==srcSize-1) { /* should never happen */ |
| 327 | DISPLAY("no difference detected\n"); |
| 328 | } } |
| 329 | break; |
| 330 | } } /* CRC Checking */ |
| Yann Collet | e8c6bb1 | 2015-07-26 00:23:57 +0100 | [diff] [blame] | 331 | #endif |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 332 | } /* for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 333 | |
| Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 334 | result->ratio = ratio; |
| 335 | result->cSize = cSize; |
| 336 | result->cSpeed = (double)srcSize / fastestC; |
| 337 | result->dSpeed = (double)srcSize / fastestD; |
| inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 338 | DISPLAYLEVEL(2, "%2i#\n", cLevel); |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 339 | } /* Bench */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 340 | |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 341 | /* clean up */ |
| inikep | 0bd0fae | 2016-05-05 13:10:57 +0200 | [diff] [blame] | 342 | free(blockTable); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 343 | free(compressedBuffer); |
| 344 | free(resultBuffer); |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 345 | ZSTD_freeCCtx(ctx); |
| 346 | ZSTD_freeDCtx(dctx); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 351 | static size_t BMK_findMaxMem(U64 requiredMem) |
| 352 | { |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 353 | size_t const step = 64 MB; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 354 | BYTE* testmem = NULL; |
| 355 | |
| 356 | requiredMem = (((requiredMem >> 26) + 1) << 26); |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 357 | requiredMem += step; |
| Yann Collet | 050efba | 2015-11-03 09:49:30 +0100 | [diff] [blame] | 358 | if (requiredMem > maxMemory) requiredMem = maxMemory; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 359 | |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 360 | do { |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 361 | testmem = (BYTE*)malloc((size_t)requiredMem); |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 362 | requiredMem -= step; |
| 363 | } while (!testmem); |
| 364 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 365 | free(testmem); |
| Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 366 | return (size_t)(requiredMem); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 367 | } |
| 368 | |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 369 | static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize, |
| inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 370 | const char* displayName, int cLevel, int cLevelLast, |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 371 | const size_t* fileSizes, unsigned nbFiles, |
| 372 | const void* dictBuffer, size_t dictBufferSize) |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 373 | { |
| Yann Collet | 235911e | 2016-07-31 01:32:48 +0200 | [diff] [blame] | 374 | benchResult_t result; |
| inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 375 | int l; |
| inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 376 | |
| inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 377 | const char* pch = strrchr(displayName, '\\'); /* Windows */ |
| 378 | if (!pch) pch = strrchr(displayName, '/'); /* Linux */ |
| 379 | if (pch) displayName = pch+1; |
| inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 380 | |
| inikep | ea4ee3e | 2016-04-25 13:09:06 +0200 | [diff] [blame] | 381 | SET_HIGH_PRIORITY; |
| 382 | |
| inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 383 | memset(&result, 0, sizeof(result)); |
| inikep | 5fdd0b4 | 2016-03-14 19:51:11 +0100 | [diff] [blame] | 384 | |
| inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 385 | if (g_displayLevel == 1 && !g_additionalParam) |
| inikep | d7d251c | 2016-06-22 16:13:25 +0200 | [diff] [blame] | 386 | DISPLAY("bench %s %s: input %u bytes, %i iterations, %u KB blocks\n", ZSTD_VERSION_STRING, ZSTD_GIT_COMMIT_STRING, (U32)benchedSize, g_nbIterations, (U32)(g_blockSize>>10)); |
| inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 387 | |
| 388 | if (cLevelLast < cLevel) cLevelLast = cLevel; |
| 389 | |
| Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 390 | for (l=cLevel; l <= cLevelLast; l++) { |
| inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 391 | BMK_benchMem(srcBuffer, benchedSize, |
| inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 392 | displayName, l, |
| inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 393 | fileSizes, nbFiles, |
| 394 | dictBuffer, dictBufferSize, &result); |
| 395 | if (g_displayLevel == 1) { |
| inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 396 | if (g_additionalParam) |
| inikep | 3865498 | 2016-04-21 12:18:47 +0200 | [diff] [blame] | 397 | 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] | 398 | else |
| inikep | 3865498 | 2016-04-21 12:18:47 +0200 | [diff] [blame] | 399 | 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); |
| Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 400 | } } |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 401 | } |
| 402 | |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 403 | |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 404 | /*! BMK_loadFiles() : |
| 405 | Loads `buffer` with content of files listed within `fileNamesTable`. |
| 406 | At most, fills `buffer` entirely */ |
| Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 407 | static void BMK_loadFiles(void* buffer, size_t bufferSize, |
| 408 | size_t* fileSizes, |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 409 | const char** fileNamesTable, unsigned nbFiles) |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 410 | { |
| inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 411 | size_t pos = 0, totalSize = 0; |
| Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 412 | unsigned n; |
| Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 413 | for (n=0; n<nbFiles; n++) { |
| Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 414 | FILE* f; |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 415 | U64 fileSize = UTIL_getFileSize(fileNamesTable[n]); |
| 416 | if (UTIL_isDirectory(fileNamesTable[n])) { |
| inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 417 | DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]); |
| inikep | bab4317 | 2016-04-29 15:19:40 +0200 | [diff] [blame] | 418 | fileSizes[n] = 0; |
| inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 419 | continue; |
| 420 | } |
| inikep | ea4ee3e | 2016-04-25 13:09:06 +0200 | [diff] [blame] | 421 | f = fopen(fileNamesTable[n], "rb"); |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 422 | 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^] | 423 | DISPLAYUPDATE(2, "Loading %s... \r", fileNamesTable[n]); |
| Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 424 | if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */ |
| 425 | { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f); |
| 426 | if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]); |
| 427 | pos += readSize; } |
| Yann Collet | a52c98d | 2015-12-16 03:12:31 +0100 | [diff] [blame] | 428 | fileSizes[n] = (size_t)fileSize; |
| inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 429 | totalSize += (size_t)fileSize; |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 430 | fclose(f); |
| 431 | } |
| Yann Collet | 6f9c056 | 2016-05-01 10:26:30 +0200 | [diff] [blame] | 432 | |
| inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 433 | if (totalSize == 0) EXM_THROW(12, "no data to bench"); |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 434 | } |
| 435 | |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 436 | static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles, |
| inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 437 | const char* dictFileName, int cLevel, int cLevelLast) |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 438 | { |
| 439 | void* srcBuffer; |
| 440 | size_t benchedSize; |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 441 | void* dictBuffer = NULL; |
| 442 | size_t dictBufferSize = 0; |
| 443 | size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t)); |
| Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 444 | U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles); |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 445 | char mfName[20] = {0}; |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 446 | |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 447 | if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes"); |
| 448 | |
| 449 | /* Load dictionary */ |
| Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 450 | if (dictFileName != NULL) { |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 451 | U64 dictFileSize = UTIL_getFileSize(dictFileName); |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 452 | if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName); |
| 453 | dictBufferSize = (size_t)dictFileSize; |
| 454 | dictBuffer = malloc(dictBufferSize); |
| 455 | if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize); |
| 456 | BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1); |
| 457 | } |
| 458 | |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 459 | /* Memory allocation & restrictions */ |
| 460 | benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3; |
| 461 | if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad; |
| 462 | if (benchedSize < totalSizeToLoad) |
| 463 | DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20)); |
| 464 | srcBuffer = malloc(benchedSize); |
| 465 | if (!srcBuffer) EXM_THROW(12, "not enough memory"); |
| 466 | |
| 467 | /* Load input buffer */ |
| Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 468 | BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles); |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 469 | |
| 470 | /* Bench */ |
| 471 | snprintf (mfName, sizeof(mfName), " %u files", nbFiles); |
| Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 472 | { const char* displayName = (nbFiles > 1) ? mfName : fileNamesTable[0]; |
| 473 | BMK_benchCLevel(srcBuffer, benchedSize, |
| 474 | displayName, cLevel, cLevelLast, |
| 475 | fileSizes, nbFiles, |
| 476 | dictBuffer, dictBufferSize); |
| 477 | } |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 478 | |
| Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 479 | /* clean up */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 480 | free(srcBuffer); |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 481 | free(dictBuffer); |
| Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 482 | free(fileSizes); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | |
| inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 486 | static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility) |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 487 | { |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 488 | char name[20] = {0}; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 489 | size_t benchedSize = 10000000; |
| Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 490 | void* const srcBuffer = malloc(benchedSize); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 491 | |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 492 | /* Memory allocation */ |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 493 | if (!srcBuffer) EXM_THROW(21, "not enough memory"); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 494 | |
| 495 | /* Fill input buffer */ |
| Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 496 | RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 497 | |
| 498 | /* Bench */ |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 499 | snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100)); |
| inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 500 | BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 501 | |
| Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 502 | /* clean up */ |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 503 | free(srcBuffer); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | |
| Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 507 | int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, |
| inikep | 957823f | 2016-05-25 15:30:55 +0200 | [diff] [blame] | 508 | const char* dictFileName, int cLevel, int cLevelLast) |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 509 | { |
| Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 510 | double const compressibility = (double)g_compressibilityDefault / 100; |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 511 | |
| 512 | if (nbFiles == 0) |
| inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 513 | BMK_syntheticTest(cLevel, cLevelLast, compressibility); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 514 | else |
| inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 515 | BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast); |
| Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 516 | return 0; |
| 517 | } |