Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 1 | /* |
| 2 | bench.c - Demo module to benchmark open-source compression algorithms |
| 3 | Copyright (C) Yann Collet 2012-2015 |
| 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 : |
| 22 | - zstd source repository : https://github.com/Cyan4973/zstd |
| 23 | - ztsd public forum : https://groups.google.com/forum/#!forum/lz4c |
| 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 | * Compiler Options |
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 | /* Disable some Visual warning messages */ |
Yann Collet | 6c8b925 | 2015-12-16 02:44:56 +0100 | [diff] [blame] | 30 | #ifdef _MSC_VER |
| 31 | # define _CRT_SECURE_NO_WARNINGS /* fopen */ |
| 32 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
| 33 | #endif |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 34 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 35 | /* Unix Large Files support (>4GB) */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 36 | #define _FILE_OFFSET_BITS 64 |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 37 | #if (defined(__sun__) && (!defined(__LP64__))) /* Sun Solaris 32-bits requires specific definitions */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 38 | # define _LARGEFILE_SOURCE |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 39 | #elif ! defined(__LP64__) /* No point defining Large file for 64 bit */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 40 | # define _LARGEFILE64_SOURCE |
| 41 | #endif |
| 42 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 43 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 44 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 45 | * Includes |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 46 | ***************************************/ |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 47 | #define _POSIX_C_SOURCE 199309L /* before <time.h> - needed for nanosleep() */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 48 | #include <stdlib.h> /* malloc, free */ |
| 49 | #include <string.h> /* memset */ |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 50 | #include <stdio.h> /* fprintf, fopen, ftello64 */ |
| 51 | #include <sys/types.h> /* stat64 */ |
| 52 | #include <sys/stat.h> /* stat64 */ |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 53 | #include <time.h> /* clock_t, nanosleep, clock, CLOCKS_PER_SEC */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 54 | |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 55 | /* sleep : posix - windows - others */ |
| 56 | #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) |
| 57 | # include <unistd.h> |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 58 | # include <sys/resource.h> /* setpriority */ |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 59 | # define BMK_sleep(s) sleep(s) |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 60 | # define mili_sleep(mili) { struct timespec t; t.tv_sec=0; t.tv_nsec=mili*1000000L; nanosleep(&t, NULL); } |
| 61 | # define setHighPriority() setpriority(PRIO_PROCESS, 0, -20) |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 62 | #elif defined(_WIN32) |
| 63 | # include <windows.h> |
| 64 | # define BMK_sleep(s) Sleep(1000*s) |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 65 | # define mili_sleep(mili) Sleep(mili) |
| 66 | # define setHighPriority() SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 67 | #else |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 68 | # define BMK_sleep(s) /* disabled */ |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 69 | # define mili_sleep(mili) /* disabled */ |
| 70 | # define setHighPriority() /* disabled */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 71 | #endif |
| 72 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 73 | #include "mem.h" |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 74 | #include "zstd_static.h" |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame^] | 75 | #include "zstd_internal.h" /* ZSTD_compressBegin_targetSrcSize */ |
| 76 | #include "datagen.h" /* RDG_genBuffer */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 77 | #include "xxhash.h" |
| 78 | |
| 79 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 80 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 81 | * Compiler specifics |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 82 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 83 | #if !defined(S_ISREG) |
| 84 | # define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) |
| 85 | #endif |
| 86 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 87 | #ifdef _MSC_VER |
| 88 | #define snprintf sprintf_s |
| 89 | #endif |
| 90 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 91 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 92 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 93 | * Constants |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 94 | ***************************************/ |
inikep | 44af12d | 2016-03-14 15:59:04 +0100 | [diff] [blame] | 95 | #ifndef ZSTD_VERSION |
| 96 | # define ZSTD_VERSION "" |
| 97 | #endif |
| 98 | |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 99 | #define NBLOOPS 3 |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 100 | #define TIMELOOP_S 1 |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 101 | #define ACTIVEPERIOD_S 70 |
| 102 | #define COOLPERIOD_S 10 |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 103 | |
| 104 | #define KB *(1 <<10) |
| 105 | #define MB *(1 <<20) |
| 106 | #define GB *(1U<<30) |
| 107 | |
Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 108 | 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] | 109 | |
| 110 | static U32 g_compressibilityDefault = 50; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 111 | |
| 112 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 113 | /* ************************************* |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 114 | * console display |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 115 | ***************************************/ |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 116 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 117 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 118 | static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ |
| 119 | |
| 120 | |
| 121 | /* ************************************* |
| 122 | * Exceptions |
| 123 | ***************************************/ |
| 124 | #ifndef DEBUG |
| 125 | # define DEBUG 0 |
| 126 | #endif |
| 127 | #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); |
| 128 | #define EXM_THROW(error, ...) \ |
| 129 | { \ |
| 130 | DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ |
| 131 | DISPLAYLEVEL(1, "Error %i : ", error); \ |
| 132 | DISPLAYLEVEL(1, __VA_ARGS__); \ |
| 133 | DISPLAYLEVEL(1, "\n"); \ |
| 134 | exit(error); \ |
| 135 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 136 | |
| 137 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 138 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 139 | * Benchmark Parameters |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 140 | ***************************************/ |
Yann Collet | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 141 | static U32 g_nbIterations = NBLOOPS; |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 142 | static size_t g_blockSize = 0; |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame^] | 143 | int g_additionalParam = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 144 | |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 145 | void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; } |
| 146 | |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame^] | 147 | void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; } |
| 148 | |
Yann Collet | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 149 | void BMK_SetNbIterations(unsigned nbLoops) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 150 | { |
Yann Collet | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 151 | g_nbIterations = nbLoops; |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 152 | DISPLAYLEVEL(2, "- %i iterations -\n", g_nbIterations); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 155 | void BMK_SetBlockSize(size_t blockSize) |
| 156 | { |
| 157 | g_blockSize = blockSize; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 158 | DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10)); |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 161 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 162 | /* ******************************************************** |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 163 | * Private functions |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 164 | **********************************************************/ |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 165 | static clock_t BMK_clockSpan( clock_t clockStart ) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 166 | { |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 167 | return clock() - clockStart; /* works even if overflow, span limited to <= ~30mn */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 170 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 171 | static U64 BMK_getFileSize(const char* infilename) |
| 172 | { |
| 173 | int r; |
| 174 | #if defined(_MSC_VER) |
| 175 | struct _stat64 statbuf; |
| 176 | r = _stat64(infilename, &statbuf); |
| 177 | #else |
| 178 | struct stat statbuf; |
| 179 | r = stat(infilename, &statbuf); |
| 180 | #endif |
| 181 | if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */ |
| 182 | return (U64)statbuf.st_size; |
| 183 | } |
| 184 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 185 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 186 | /* ******************************************************** |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 187 | * Bench functions |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 188 | **********************************************************/ |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 189 | typedef struct |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 190 | { |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 191 | const char* srcPtr; |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 192 | size_t srcSize; |
| 193 | char* cPtr; |
| 194 | size_t cRoom; |
| 195 | size_t cSize; |
| 196 | char* resPtr; |
| 197 | size_t resSize; |
| 198 | } blockParam_t; |
| 199 | |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 200 | typedef struct |
| 201 | { |
inikep | c034b73 | 2016-03-14 13:13:42 +0100 | [diff] [blame] | 202 | double ratio; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 203 | size_t cSize; |
inikep | c034b73 | 2016-03-14 13:13:42 +0100 | [diff] [blame] | 204 | double cSpeed; |
| 205 | double dSpeed; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 206 | } benchResult_t; |
| 207 | |
| 208 | |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 209 | #define MIN(a,b) ((a)<(b) ? (a) : (b)) |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 210 | #define MAX(a,b) ((a)>(b) ? (a) : (b)) |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 211 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 212 | static int BMK_benchMem(const void* srcBuffer, size_t srcSize, |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 213 | const char* displayName, int cLevel, |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 214 | const size_t* fileSizes, U32 nbFiles, |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 215 | const void* dictBuffer, size_t dictBufferSize, benchResult_t *result) |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 216 | { |
Yann Collet | 2c7ac7c | 2015-11-04 17:52:18 +0100 | [diff] [blame] | 217 | const size_t blockSize = (g_blockSize ? g_blockSize : srcSize) + (!srcSize); /* avoid div by 0 */ |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 218 | const U32 maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles; |
| 219 | blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t)); |
Yann Collet | 367060b | 2015-12-17 00:07:10 +0100 | [diff] [blame] | 220 | const size_t maxCompressedSize = ZSTD_compressBound(srcSize) + (maxNbBlocks * 1024); /* add some room for safety */ |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 221 | void* const compressedBuffer = malloc(maxCompressedSize); |
| 222 | void* const resultBuffer = malloc(srcSize); |
Yann Collet | 2630a5e | 2016-01-14 19:13:22 +0100 | [diff] [blame] | 223 | ZSTD_CCtx* refCtx = ZSTD_createCCtx(); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 224 | ZSTD_CCtx* ctx = ZSTD_createCCtx(); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 225 | ZSTD_DCtx* refDCtx = ZSTD_createDCtx(); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 226 | ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
Yann Collet | c776c46 | 2015-10-29 19:10:54 +0100 | [diff] [blame] | 227 | |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 228 | U64 const crcOrig = XXH64(srcBuffer, srcSize, 0); |
| 229 | U32 nbBlocks; |
| 230 | |
| 231 | /* checks */ |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 232 | if (!compressedBuffer || !resultBuffer || !blockTable || !refCtx || !ctx || !refDCtx || !dctx) |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 233 | EXM_THROW(31, "not enough memory"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 234 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 235 | /* init */ |
| 236 | if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */ |
| 237 | |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 238 | /* Init blockTable data */ |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 239 | { const char* srcPtr = (const char*)srcBuffer; |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 240 | char* cPtr = (char*)compressedBuffer; |
| 241 | char* resPtr = (char*)resultBuffer; |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 242 | U32 fileNb; |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 243 | for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) { |
Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 244 | size_t remaining = fileSizes[fileNb]; |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 245 | U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize); |
| 246 | U32 const blockEnd = nbBlocks + nbBlocksforThisFile; |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 247 | for ( ; nbBlocks<blockEnd; nbBlocks++) { |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 248 | size_t const thisBlockSize = MIN(remaining, blockSize); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 249 | blockTable[nbBlocks].srcPtr = srcPtr; |
| 250 | blockTable[nbBlocks].cPtr = cPtr; |
| 251 | blockTable[nbBlocks].resPtr = resPtr; |
| 252 | blockTable[nbBlocks].srcSize = thisBlockSize; |
| 253 | blockTable[nbBlocks].cRoom = ZSTD_compressBound(thisBlockSize); |
| 254 | srcPtr += thisBlockSize; |
| 255 | cPtr += blockTable[nbBlocks].cRoom; |
| 256 | resPtr += thisBlockSize; |
| 257 | remaining -= thisBlockSize; |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 258 | } } } |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 259 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 260 | /* warmimg up memory */ |
Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 261 | RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 262 | |
| 263 | /* Bench */ |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 264 | { size_t cSize = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 265 | double fastestC = 100000000., fastestD = 100000000.; |
| 266 | double ratio = 0.; |
| 267 | U64 crcCheck = 0; |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 268 | clock_t coolTime = clock(); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 269 | U32 testNb; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 270 | |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 271 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 272 | for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 273 | int nbLoops; |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 274 | clock_t clockStart, clockSpan; |
Yann Collet | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 275 | clock_t const clockLoop = g_nbIterations ? TIMELOOP_S * CLOCKS_PER_SEC : 10; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 276 | |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 277 | /* overheat protection */ |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 278 | if (BMK_clockSpan(coolTime) > ACTIVEPERIOD_S * CLOCKS_PER_SEC) { |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 279 | DISPLAY("\rcooling down ... \r"); |
| 280 | BMK_sleep(COOLPERIOD_S); |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 281 | coolTime = clock(); |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 282 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 283 | |
| 284 | /* Compression */ |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 285 | DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->\r", testNb, displayName, (U32)srcSize); |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 286 | memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 287 | |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 288 | mili_sleep(1); /* give processor time to other processes */ |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 289 | clockStart = clock(); |
| 290 | while (clock() == clockStart); |
| 291 | clockStart = clock(); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 292 | |
| 293 | for (nbLoops = 0 ; BMK_clockSpan(clockStart) < clockLoop ; nbLoops++) { |
| 294 | U32 blockNb; |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame^] | 295 | ZSTD_compressBegin_targetSrcSize(refCtx, dictBuffer, dictBufferSize, blockSize, cLevel); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 296 | for (blockNb=0; blockNb<nbBlocks; blockNb++) { |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 297 | size_t const rSize = ZSTD_compress_usingPreparedCCtx(ctx, refCtx, |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 298 | blockTable[blockNb].cPtr, blockTable[blockNb].cRoom, |
| 299 | blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize); |
| 300 | if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingPreparedCCtx() failed : %s", ZSTD_getErrorName(rSize)); |
Yann Collet | 2630a5e | 2016-01-14 19:13:22 +0100 | [diff] [blame] | 301 | blockTable[blockNb].cSize = rSize; |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 302 | } } |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 303 | clockSpan = BMK_clockSpan(clockStart); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 304 | |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 305 | if ((double)clockSpan < fastestC*nbLoops) fastestC = (double)clockSpan / nbLoops; |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 306 | cSize = 0; |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 307 | { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; } |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 308 | ratio = (double)srcSize / (double)cSize; |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 309 | 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] | 310 | testNb, displayName, (U32)srcSize, (U32)cSize, ratio, |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 311 | (double)srcSize / 1000000. / (fastestC / CLOCKS_PER_SEC) ); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 312 | |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 313 | #if 1 |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 314 | /* Decompression */ |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 315 | memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 316 | |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 317 | mili_sleep(1); /* give processor time to other processes */ |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 318 | clockStart = clock(); |
| 319 | while (clock() == clockStart); |
| 320 | clockStart = clock(); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 321 | |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 322 | for (nbLoops = 0 ; BMK_clockSpan(clockStart) < clockLoop ; nbLoops++) { |
| 323 | U32 blockNb; |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 324 | ZSTD_decompressBegin_usingDict(refDCtx, dictBuffer, dictBufferSize); |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 325 | for (blockNb=0; blockNb<nbBlocks; blockNb++) { |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 326 | size_t regenSize = ZSTD_decompress_usingPreparedDCtx(dctx, refDCtx, |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 327 | blockTable[blockNb].resPtr, blockTable[blockNb].srcSize, |
| 328 | blockTable[blockNb].cPtr, blockTable[blockNb].cSize); |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 329 | if (ZSTD_isError(regenSize)) { |
| 330 | DISPLAY("ZSTD_decompress_usingPreparedDCtx() failed on block %u : %s", |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 331 | blockNb, ZSTD_getErrorName(regenSize)); |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 332 | goto _findError; |
| 333 | } |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 334 | blockTable[blockNb].resSize = regenSize; |
| 335 | } } |
| 336 | |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 337 | clockSpan = BMK_clockSpan(clockStart); |
| 338 | if ((double)clockSpan < fastestD*nbLoops) fastestD = (double)clockSpan / nbLoops; |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 339 | 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] | 340 | testNb, displayName, (U32)srcSize, (U32)cSize, ratio, |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 341 | (double)srcSize / 1000000. / (fastestC / CLOCKS_PER_SEC), |
| 342 | (double)srcSize / 1000000. / (fastestD / CLOCKS_PER_SEC) ); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 343 | |
| 344 | /* CRC Checking */ |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 345 | _findError: |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 346 | crcCheck = XXH64(resultBuffer, srcSize, 0); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 347 | if (crcOrig!=crcCheck) { |
Yann Collet | 03a6dab | 2016-01-21 02:21:17 +0100 | [diff] [blame] | 348 | size_t u; |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 349 | DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n", displayName, (unsigned)crcOrig, (unsigned)crcCheck); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 350 | for (u=0; u<srcSize; u++) { |
| 351 | if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) { |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 352 | U32 segNb, bNb, pos; |
Yann Collet | 03a6dab | 2016-01-21 02:21:17 +0100 | [diff] [blame] | 353 | size_t bacc = 0; |
| 354 | printf("Decoding error at pos %u ", (U32)u); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 355 | for (segNb = 0; segNb < nbBlocks; segNb++) { |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 356 | if (bacc + blockTable[segNb].srcSize > u) break; |
| 357 | bacc += blockTable[segNb].srcSize; |
Yann Collet | 03a6dab | 2016-01-21 02:21:17 +0100 | [diff] [blame] | 358 | } |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 359 | pos = (U32)(u - bacc); |
| 360 | bNb = pos / (128 KB); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 361 | printf("(block %u, sub %u, pos %u) \n", segNb, bNb, pos); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 362 | break; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 363 | } |
| 364 | if (u==srcSize-1) { /* should never happen */ |
| 365 | printf("no difference detected\n"); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 366 | } } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 367 | break; |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 368 | } /* if (crcOrig!=crcCheck) */ |
Yann Collet | e8c6bb1 | 2015-07-26 00:23:57 +0100 | [diff] [blame] | 369 | #endif |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 370 | } /* for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 371 | |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 372 | if (crcOrig == crcCheck) { |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 373 | result->ratio = ratio; |
| 374 | result->cSize = cSize; |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 375 | result->cSpeed = (double)srcSize / 1000000. / (fastestC / CLOCKS_PER_SEC); |
| 376 | result->dSpeed = (double)srcSize / 1000000. / (fastestD / CLOCKS_PER_SEC); |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 377 | } |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 378 | DISPLAYLEVEL(2, "%2i#\n", cLevel); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 379 | } /* Bench */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 380 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 381 | /* clean up */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 382 | free(compressedBuffer); |
| 383 | free(resultBuffer); |
Yann Collet | 2630a5e | 2016-01-14 19:13:22 +0100 | [diff] [blame] | 384 | ZSTD_freeCCtx(refCtx); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 385 | ZSTD_freeCCtx(ctx); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 386 | ZSTD_freeDCtx(refDCtx); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 387 | ZSTD_freeDCtx(dctx); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 392 | static size_t BMK_findMaxMem(U64 requiredMem) |
| 393 | { |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 394 | size_t const step = 64 MB; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 395 | BYTE* testmem = NULL; |
| 396 | |
| 397 | requiredMem = (((requiredMem >> 26) + 1) << 26); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 398 | requiredMem += step; |
Yann Collet | 050efba | 2015-11-03 09:49:30 +0100 | [diff] [blame] | 399 | if (requiredMem > maxMemory) requiredMem = maxMemory; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 400 | |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 401 | do { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 402 | testmem = (BYTE*)malloc((size_t)requiredMem); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 403 | requiredMem -= step; |
| 404 | } while (!testmem); |
| 405 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 406 | free(testmem); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 407 | return (size_t)(requiredMem); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 408 | } |
| 409 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 410 | static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize, |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame^] | 411 | const char* displayName, int cLevel, int cLevelLast, |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 412 | const size_t* fileSizes, unsigned nbFiles, |
| 413 | const void* dictBuffer, size_t dictBufferSize) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 414 | { |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 415 | benchResult_t result, total; |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 416 | int l; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 417 | |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 418 | setHighPriority(); |
| 419 | |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 420 | const char* pch = strrchr(displayName, '\\'); /* Windows */ |
| 421 | if (!pch) pch = strrchr(displayName, '/'); /* Linux */ |
| 422 | if (pch) displayName = pch+1; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 423 | |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 424 | memset(&result, 0, sizeof(result)); |
| 425 | memset(&total, 0, sizeof(total)); |
inikep | 5fdd0b4 | 2016-03-14 19:51:11 +0100 | [diff] [blame] | 426 | |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame^] | 427 | if (g_displayLevel == 1 && !g_additionalParam) |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 428 | 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] | 429 | |
| 430 | if (cLevelLast < cLevel) cLevelLast = cLevel; |
| 431 | |
| 432 | for (l=cLevel; l <= cLevelLast; l++) { |
| 433 | BMK_benchMem(srcBuffer, benchedSize, |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 434 | displayName, l, |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 435 | fileSizes, nbFiles, |
| 436 | dictBuffer, dictBufferSize, &result); |
| 437 | if (g_displayLevel == 1) { |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame^] | 438 | if (g_additionalParam) |
| 439 | DISPLAY("%-3i%11i (%5.3f) %6.1f 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] | 440 | else |
| 441 | DISPLAY("%-3i%11i (%5.3f) %6.1f 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] | 442 | total.cSize += result.cSize; |
| 443 | total.cSpeed += result.cSpeed; |
| 444 | total.dSpeed += result.dSpeed; |
| 445 | total.ratio += result.ratio; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 446 | } |
Yann Collet | c776c46 | 2015-10-29 19:10:54 +0100 | [diff] [blame] | 447 | } |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 448 | if (g_displayLevel == 1 && cLevelLast > cLevel) |
| 449 | { |
| 450 | total.cSize /= 1+cLevelLast-cLevel; |
| 451 | total.cSpeed /= 1+cLevelLast-cLevel; |
| 452 | total.dSpeed /= 1+cLevelLast-cLevel; |
| 453 | total.ratio /= 1+cLevelLast-cLevel; |
| 454 | DISPLAY("avg%11i (%5.3f) %6.1f MB/s %6.1f MB/s %s\n", (int)total.cSize, total.ratio, total.cSpeed, total.dSpeed, displayName); |
| 455 | } |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | static U64 BMK_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles) |
| 459 | { |
| 460 | U64 total = 0; |
| 461 | unsigned n; |
| 462 | for (n=0; n<nbFiles; n++) |
| 463 | total += BMK_getFileSize(fileNamesTable[n]); |
| 464 | return total; |
| 465 | } |
| 466 | |
Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 467 | static void BMK_loadFiles(void* buffer, size_t bufferSize, |
| 468 | size_t* fileSizes, |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 469 | const char** fileNamesTable, unsigned const nbFiles) |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 470 | { |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 471 | size_t pos = 0; |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 472 | |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 473 | unsigned n; |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 474 | for (n=0; n<nbFiles; n++) { |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 475 | size_t readSize; |
| 476 | U64 fileSize = BMK_getFileSize(fileNamesTable[n]); |
| 477 | FILE* f = fopen(fileNamesTable[n], "rb"); |
| 478 | if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]); |
| 479 | DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[n]); |
| 480 | if (fileSize > bufferSize-pos) fileSize = bufferSize-pos; |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 481 | readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 482 | if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]); |
| 483 | pos += readSize; |
Yann Collet | a52c98d | 2015-12-16 03:12:31 +0100 | [diff] [blame] | 484 | fileSizes[n] = (size_t)fileSize; |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 485 | fclose(f); |
| 486 | } |
| 487 | } |
| 488 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 489 | static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles, |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame^] | 490 | const char* dictFileName, int cLevel, int cLevelLast) |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 491 | { |
| 492 | void* srcBuffer; |
| 493 | size_t benchedSize; |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 494 | void* dictBuffer = NULL; |
| 495 | size_t dictBufferSize = 0; |
| 496 | size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t)); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 497 | U64 totalSizeToLoad = BMK_getTotalFileSize(fileNamesTable, nbFiles); |
| 498 | char mfName[20] = {0}; |
| 499 | const char* displayName = NULL; |
| 500 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 501 | if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes"); |
| 502 | |
| 503 | /* Load dictionary */ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 504 | if (dictFileName != NULL) { |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 505 | U64 dictFileSize = BMK_getFileSize(dictFileName); |
| 506 | if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName); |
| 507 | dictBufferSize = (size_t)dictFileSize; |
| 508 | dictBuffer = malloc(dictBufferSize); |
| 509 | if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize); |
| 510 | BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1); |
| 511 | } |
| 512 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 513 | /* Memory allocation & restrictions */ |
| 514 | benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3; |
| 515 | if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad; |
| 516 | if (benchedSize < totalSizeToLoad) |
| 517 | DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20)); |
| 518 | srcBuffer = malloc(benchedSize); |
| 519 | if (!srcBuffer) EXM_THROW(12, "not enough memory"); |
| 520 | |
| 521 | /* Load input buffer */ |
Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 522 | BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 523 | |
| 524 | /* Bench */ |
| 525 | snprintf (mfName, sizeof(mfName), " %u files", nbFiles); |
| 526 | if (nbFiles > 1) displayName = mfName; |
| 527 | else displayName = fileNamesTable[0]; |
| 528 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 529 | BMK_benchCLevel(srcBuffer, benchedSize, |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame^] | 530 | displayName, cLevel, cLevelLast, |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 531 | fileSizes, nbFiles, |
| 532 | dictBuffer, dictBufferSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 533 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 534 | /* clean up */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 535 | free(srcBuffer); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 536 | free(dictBuffer); |
Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 537 | free(fileSizes); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame^] | 541 | static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 542 | { |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 543 | char name[20] = {0}; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 544 | size_t benchedSize = 10000000; |
| 545 | void* srcBuffer = malloc(benchedSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 546 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 547 | /* Memory allocation */ |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 548 | if (!srcBuffer) EXM_THROW(21, "not enough memory"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 549 | |
| 550 | /* Fill input buffer */ |
Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 551 | RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 552 | |
| 553 | /* Bench */ |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 554 | snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100)); |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame^] | 555 | BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 556 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 557 | /* clean up */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 558 | free(srcBuffer); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 562 | int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame^] | 563 | const char* dictFileName, int cLevel, int cLevelLast) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 564 | { |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 565 | double const compressibility = (double)g_compressibilityDefault / 100; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 566 | |
| 567 | if (nbFiles == 0) |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame^] | 568 | BMK_syntheticTest(cLevel, cLevelLast, compressibility); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 569 | else |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame^] | 570 | BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |