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 | * 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 | ***************************************/ |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [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 */ |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [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 | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 60 | # define mili_sleep(mili) { struct timespec t; t.tv_sec=0; t.tv_nsec=mili*1000000ULL; nanosleep(&t, NULL); } |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 61 | # define SET_HIGH_PRIORITY 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) |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 66 | # define SET_HIGH_PRIORITY 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 */ |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 70 | # define SET_HIGH_PRIORITY /* disabled */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 71 | #endif |
| 72 | |
inikep | 1eeddde | 2016-04-08 16:55:17 +0200 | [diff] [blame] | 73 | #if !defined(_WIN32) |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 74 | typedef clock_t BMK_time_t; |
inikep | 1c556a3 | 2016-03-30 10:59:48 +0200 | [diff] [blame] | 75 | # define BMK_initTimer(ticksPerSecond) ticksPerSecond=0 |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 76 | # define BMK_getTime(x) x = clock() |
| 77 | # define BMK_getSpanTimeMicro(ticksPerSecond, clockStart, clockEnd) (1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC) |
| 78 | # define BMK_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) (1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC) |
inikep | 1eeddde | 2016-04-08 16:55:17 +0200 | [diff] [blame] | 79 | #else |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 80 | typedef LARGE_INTEGER BMK_time_t; |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 81 | # define BMK_initTimer(x) if (!QueryPerformanceFrequency(&x)) { fprintf(stderr, "ERROR: QueryPerformance not present\n"); } |
| 82 | # define BMK_getTime(x) QueryPerformanceCounter(&x) |
| 83 | # define BMK_getSpanTimeMicro(ticksPerSecond, clockStart, clockEnd) (1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart) |
| 84 | # define BMK_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) (1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart) |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 85 | #endif |
| 86 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 87 | #include "mem.h" |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 88 | #include "zstd_static.h" |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 89 | #include "datagen.h" /* RDG_genBuffer */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 90 | #include "xxhash.h" |
| 91 | |
| 92 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 93 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 94 | * Compiler specifics |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 95 | ***************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 96 | #if !defined(S_ISREG) |
| 97 | # define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) |
| 98 | #endif |
| 99 | |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 100 | #if defined(_MSC_VER) |
| 101 | # define snprintf sprintf_s |
| 102 | #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) |
| 103 | /* part of <stdio.h> */ |
| 104 | #else |
| 105 | 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] | 106 | #endif |
| 107 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 108 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 109 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 110 | * Constants |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 111 | ***************************************/ |
inikep | 44af12d | 2016-03-14 15:59:04 +0100 | [diff] [blame] | 112 | #ifndef ZSTD_VERSION |
| 113 | # define ZSTD_VERSION "" |
| 114 | #endif |
| 115 | |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 116 | #define NBLOOPS 3 |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 117 | #define TIMELOOP_S 1 |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 118 | #define ACTIVEPERIOD_S 70 |
| 119 | #define COOLPERIOD_S 10 |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 120 | |
| 121 | #define KB *(1 <<10) |
| 122 | #define MB *(1 <<20) |
| 123 | #define GB *(1U<<30) |
| 124 | |
Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 125 | 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] | 126 | |
| 127 | static U32 g_compressibilityDefault = 50; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 128 | |
| 129 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 130 | /* ************************************* |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 131 | * console display |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 132 | ***************************************/ |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 133 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 134 | #define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } |
| 135 | static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */ |
| 136 | |
| 137 | |
| 138 | /* ************************************* |
| 139 | * Exceptions |
| 140 | ***************************************/ |
| 141 | #ifndef DEBUG |
| 142 | # define DEBUG 0 |
| 143 | #endif |
| 144 | #define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__); |
| 145 | #define EXM_THROW(error, ...) \ |
| 146 | { \ |
| 147 | DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \ |
| 148 | DISPLAYLEVEL(1, "Error %i : ", error); \ |
| 149 | DISPLAYLEVEL(1, __VA_ARGS__); \ |
| 150 | DISPLAYLEVEL(1, "\n"); \ |
| 151 | exit(error); \ |
| 152 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 153 | |
| 154 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 155 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 156 | * Benchmark Parameters |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 157 | ***************************************/ |
Yann Collet | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 158 | static U32 g_nbIterations = NBLOOPS; |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 159 | static size_t g_blockSize = 0; |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 160 | int g_additionalParam = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 161 | |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 162 | void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; } |
| 163 | |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 164 | void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; } |
| 165 | |
Yann Collet | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 166 | void BMK_SetNbIterations(unsigned nbLoops) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 167 | { |
Yann Collet | 1d1ae40 | 2016-03-17 19:51:02 +0100 | [diff] [blame] | 168 | g_nbIterations = nbLoops; |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 169 | DISPLAYLEVEL(2, "- %i iterations -\n", g_nbIterations); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 170 | } |
| 171 | |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 172 | void BMK_SetBlockSize(size_t blockSize) |
| 173 | { |
| 174 | g_blockSize = blockSize; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 175 | DISPLAYLEVEL(2, "using blocks of size %u KB \n", (U32)(blockSize>>10)); |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 178 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 179 | /* ******************************************************** |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 180 | * Private functions |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 181 | **********************************************************/ |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 182 | /* returns time span in microseconds */ |
| 183 | static U64 BMK_clockSpan( BMK_time_t clockStart, BMK_time_t ticksPerSecond ) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 184 | { |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 185 | BMK_time_t clockEnd; |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 186 | |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 187 | (void)ticksPerSecond; |
| 188 | BMK_getTime(clockEnd); |
| 189 | return BMK_getSpanTimeMicro(ticksPerSecond, clockStart, clockEnd); |
inikep | 06f793a | 2016-03-29 11:17:58 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 192 | static U64 BMK_getFileSize(const char* infilename) |
| 193 | { |
| 194 | int r; |
| 195 | #if defined(_MSC_VER) |
| 196 | struct _stat64 statbuf; |
| 197 | r = _stat64(infilename, &statbuf); |
| 198 | #else |
| 199 | struct stat statbuf; |
| 200 | r = stat(infilename, &statbuf); |
| 201 | #endif |
| 202 | if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */ |
| 203 | return (U64)statbuf.st_size; |
| 204 | } |
| 205 | |
inikep | 4b3c5ee | 2016-04-14 13:43:51 +0200 | [diff] [blame^] | 206 | static U32 BMK_isDirectory(const char* infilename) |
| 207 | { |
| 208 | int r; |
| 209 | #if defined(_MSC_VER) |
| 210 | struct _stat64 statbuf; |
| 211 | r = _stat64(infilename, &statbuf); |
| 212 | #else |
| 213 | struct stat statbuf; |
| 214 | r = stat(infilename, &statbuf); |
| 215 | #endif |
| 216 | if (!r && S_ISDIR(statbuf.st_mode)) return 1; |
| 217 | return 0; |
| 218 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 219 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 220 | /* ******************************************************** |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 221 | * Bench functions |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 222 | **********************************************************/ |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 223 | typedef struct |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 224 | { |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 225 | const char* srcPtr; |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 226 | size_t srcSize; |
| 227 | char* cPtr; |
| 228 | size_t cRoom; |
| 229 | size_t cSize; |
| 230 | char* resPtr; |
| 231 | size_t resSize; |
| 232 | } blockParam_t; |
| 233 | |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 234 | typedef struct |
| 235 | { |
inikep | c034b73 | 2016-03-14 13:13:42 +0100 | [diff] [blame] | 236 | double ratio; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 237 | size_t cSize; |
inikep | c034b73 | 2016-03-14 13:13:42 +0100 | [diff] [blame] | 238 | double cSpeed; |
| 239 | double dSpeed; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 240 | } benchResult_t; |
| 241 | |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 242 | |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 243 | #define MIN(a,b) ((a)<(b) ? (a) : (b)) |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 244 | #define MAX(a,b) ((a)>(b) ? (a) : (b)) |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 245 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 246 | static int BMK_benchMem(const void* srcBuffer, size_t srcSize, |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 247 | const char* displayName, int cLevel, |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 248 | const size_t* fileSizes, U32 nbFiles, |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 249 | const void* dictBuffer, size_t dictBufferSize, benchResult_t *result) |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 250 | { |
Yann Collet | d64f435 | 2016-03-21 00:07:42 +0100 | [diff] [blame] | 251 | size_t const blockSize = (g_blockSize ? g_blockSize : srcSize) + (!srcSize); /* avoid div by 0 */ |
| 252 | U32 const maxNbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize) + nbFiles; |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 253 | blockParam_t* const blockTable = (blockParam_t*) malloc(maxNbBlocks * sizeof(blockParam_t)); |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 254 | 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] | 255 | void* const compressedBuffer = malloc(maxCompressedSize); |
| 256 | void* const resultBuffer = malloc(srcSize); |
Yann Collet | 2630a5e | 2016-01-14 19:13:22 +0100 | [diff] [blame] | 257 | ZSTD_CCtx* refCtx = ZSTD_createCCtx(); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 258 | ZSTD_CCtx* ctx = ZSTD_createCCtx(); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 259 | ZSTD_DCtx* refDCtx = ZSTD_createDCtx(); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 260 | ZSTD_DCtx* dctx = ZSTD_createDCtx(); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 261 | U32 nbBlocks; |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 262 | BMK_time_t ticksPerSecond; |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 263 | |
| 264 | /* checks */ |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 265 | if (!compressedBuffer || !resultBuffer || !blockTable || !refCtx || !ctx || !refDCtx || !dctx) |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 266 | EXM_THROW(31, "not enough memory"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 267 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 268 | /* init */ |
| 269 | if (strlen(displayName)>17) displayName += strlen(displayName)-17; /* can only display 17 characters */ |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 270 | BMK_initTimer(ticksPerSecond); |
| 271 | |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 272 | /* Init blockTable data */ |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 273 | { const char* srcPtr = (const char*)srcBuffer; |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 274 | char* cPtr = (char*)compressedBuffer; |
| 275 | char* resPtr = (char*)resultBuffer; |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 276 | U32 fileNb; |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 277 | for (nbBlocks=0, fileNb=0; fileNb<nbFiles; fileNb++) { |
Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 278 | size_t remaining = fileSizes[fileNb]; |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 279 | U32 const nbBlocksforThisFile = (U32)((remaining + (blockSize-1)) / blockSize); |
| 280 | U32 const blockEnd = nbBlocks + nbBlocksforThisFile; |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 281 | for ( ; nbBlocks<blockEnd; nbBlocks++) { |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 282 | size_t const thisBlockSize = MIN(remaining, blockSize); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 283 | blockTable[nbBlocks].srcPtr = srcPtr; |
| 284 | blockTable[nbBlocks].cPtr = cPtr; |
| 285 | blockTable[nbBlocks].resPtr = resPtr; |
| 286 | blockTable[nbBlocks].srcSize = thisBlockSize; |
| 287 | blockTable[nbBlocks].cRoom = ZSTD_compressBound(thisBlockSize); |
| 288 | srcPtr += thisBlockSize; |
| 289 | cPtr += blockTable[nbBlocks].cRoom; |
| 290 | resPtr += thisBlockSize; |
| 291 | remaining -= thisBlockSize; |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 292 | } } } |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 293 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 294 | /* warmimg up memory */ |
Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 295 | RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.50, 1); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 296 | |
| 297 | /* Bench */ |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 298 | { double fastestC = 100000000., fastestD = 100000000.; |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 299 | U64 const crcOrig = XXH64(srcBuffer, srcSize, 0); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 300 | U64 crcCheck = 0; |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 301 | BMK_time_t coolTime; |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 302 | U32 testNb; |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 303 | size_t cSize = 0; |
| 304 | double ratio = 0.; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 305 | |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 306 | BMK_getTime(coolTime); |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 307 | DISPLAYLEVEL(2, "\r%79s\r", ""); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 308 | for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) { |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 309 | BMK_time_t clockStart, clockEnd; |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 310 | U64 clockLoop = g_nbIterations ? TIMELOOP_S*1000000ULL : 10; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 311 | |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 312 | /* overheat protection */ |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 313 | if (BMK_clockSpan(coolTime, ticksPerSecond) > ACTIVEPERIOD_S*1000000ULL) { |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 314 | DISPLAY("\rcooling down ... \r"); |
| 315 | BMK_sleep(COOLPERIOD_S); |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 316 | BMK_getTime(coolTime); |
Yann Collet | 27d3dad | 2016-03-11 13:41:20 +0100 | [diff] [blame] | 317 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 318 | |
| 319 | /* Compression */ |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 320 | DISPLAYLEVEL(2, "%2i-%-17.17s :%10u ->\r", testNb, displayName, (U32)srcSize); |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 321 | memset(compressedBuffer, 0xE5, maxCompressedSize); /* warm up and erase result buffer */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 322 | |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 323 | mili_sleep(1); /* give processor time to other processes */ |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 324 | BMK_getTime(clockStart); |
| 325 | do { BMK_getTime(clockEnd); } |
| 326 | while (BMK_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) == 0); |
| 327 | BMK_getTime(clockStart); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 328 | |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 329 | { U32 nbLoops; |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 330 | for (nbLoops = 0 ; BMK_clockSpan(clockStart, ticksPerSecond) < clockLoop ; nbLoops++) { |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 331 | U32 blockNb; |
inikep | 4b3c5ee | 2016-04-14 13:43:51 +0200 | [diff] [blame^] | 332 | { ZSTD_parameters params; |
| 333 | params.cParams = ZSTD_getCParams(cLevel, blockSize, dictBufferSize); |
| 334 | params.fParams.contentSizeFlag = 1; |
| 335 | ZSTD_adjustCParams(¶ms.cParams, blockSize, dictBufferSize); |
| 336 | { size_t const initResult = ZSTD_compressBegin_advanced(refCtx, dictBuffer, dictBufferSize, params, blockSize); |
| 337 | if (ZSTD_isError(initResult)) break; |
| 338 | } } |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 339 | for (blockNb=0; blockNb<nbBlocks; blockNb++) { |
| 340 | size_t const rSize = ZSTD_compress_usingPreparedCCtx(ctx, refCtx, |
| 341 | blockTable[blockNb].cPtr, blockTable[blockNb].cRoom, |
| 342 | blockTable[blockNb].srcPtr,blockTable[blockNb].srcSize); |
| 343 | if (ZSTD_isError(rSize)) EXM_THROW(1, "ZSTD_compress_usingPreparedCCtx() failed : %s", ZSTD_getErrorName(rSize)); |
| 344 | blockTable[blockNb].cSize = rSize; |
| 345 | } } |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 346 | { U64 const clockSpan = BMK_clockSpan(clockStart, ticksPerSecond); |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 347 | if ((double)clockSpan < fastestC*nbLoops) fastestC = (double)clockSpan / nbLoops; |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 348 | } } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 349 | |
Yann Collet | 1c00dc3 | 2015-10-21 08:22:25 +0100 | [diff] [blame] | 350 | cSize = 0; |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 351 | { U32 blockNb; for (blockNb=0; blockNb<nbBlocks; blockNb++) cSize += blockTable[blockNb].cSize; } |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 352 | ratio = (double)srcSize / (double)cSize; |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 353 | 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] | 354 | testNb, displayName, (U32)srcSize, (U32)cSize, ratio, |
inikep | 06f793a | 2016-03-29 11:17:58 +0200 | [diff] [blame] | 355 | (double)srcSize / fastestC ); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 356 | |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 357 | (void)fastestD; (void)crcOrig; /* unused when decompression disabled */ |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 358 | #if 1 |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 359 | /* Decompression */ |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 360 | memset(resultBuffer, 0xD6, srcSize); /* warm result buffer */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 361 | |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 362 | mili_sleep(1); /* give processor time to other processes */ |
inikep | 4c12f23 | 2016-03-29 14:52:13 +0200 | [diff] [blame] | 363 | BMK_getTime(clockStart); |
| 364 | do { BMK_getTime(clockEnd); } |
| 365 | while (BMK_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) == 0); |
| 366 | BMK_getTime(clockStart); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 367 | |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 368 | { U32 nbLoops; |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 369 | for (nbLoops = 0 ; BMK_clockSpan(clockStart, ticksPerSecond) < clockLoop ; nbLoops++) { |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 370 | U32 blockNb; |
| 371 | ZSTD_decompressBegin_usingDict(refDCtx, dictBuffer, dictBufferSize); |
| 372 | for (blockNb=0; blockNb<nbBlocks; blockNb++) { |
| 373 | size_t const regenSize = ZSTD_decompress_usingPreparedDCtx(dctx, refDCtx, |
| 374 | blockTable[blockNb].resPtr, blockTable[blockNb].srcSize, |
| 375 | blockTable[blockNb].cPtr, blockTable[blockNb].cSize); |
| 376 | if (ZSTD_isError(regenSize)) { |
| 377 | DISPLAY("ZSTD_decompress_usingPreparedDCtx() failed on block %u : %s \n", |
| 378 | blockNb, ZSTD_getErrorName(regenSize)); |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 379 | clockLoop = 0; /* force immediate test end */ |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 380 | break; |
| 381 | } |
| 382 | blockTable[blockNb].resSize = regenSize; |
| 383 | } } |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 384 | { U64 const clockSpan = BMK_clockSpan(clockStart, ticksPerSecond); |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 385 | if ((double)clockSpan < fastestD*nbLoops) fastestD = (double)clockSpan / nbLoops; |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 386 | } } |
| 387 | |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 388 | 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] | 389 | testNb, displayName, (U32)srcSize, (U32)cSize, ratio, |
inikep | 06f793a | 2016-03-29 11:17:58 +0200 | [diff] [blame] | 390 | (double)srcSize / fastestC, |
| 391 | (double)srcSize / fastestD ); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 392 | |
| 393 | /* CRC Checking */ |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 394 | { crcCheck = XXH64(resultBuffer, srcSize, 0); |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 395 | if (crcOrig!=crcCheck) { |
| 396 | size_t u; |
| 397 | DISPLAY("!!! WARNING !!! %14s : Invalid Checksum : %x != %x \n", displayName, (unsigned)crcOrig, (unsigned)crcCheck); |
| 398 | for (u=0; u<srcSize; u++) { |
| 399 | if (((const BYTE*)srcBuffer)[u] != ((const BYTE*)resultBuffer)[u]) { |
| 400 | U32 segNb, bNb, pos; |
| 401 | size_t bacc = 0; |
| 402 | DISPLAY("Decoding error at pos %u ", (U32)u); |
| 403 | for (segNb = 0; segNb < nbBlocks; segNb++) { |
| 404 | if (bacc + blockTable[segNb].srcSize > u) break; |
| 405 | bacc += blockTable[segNb].srcSize; |
| 406 | } |
| 407 | pos = (U32)(u - bacc); |
| 408 | bNb = pos / (128 KB); |
| 409 | DISPLAY("(block %u, sub %u, pos %u) \n", segNb, bNb, pos); |
| 410 | break; |
Yann Collet | 03a6dab | 2016-01-21 02:21:17 +0100 | [diff] [blame] | 411 | } |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 412 | if (u==srcSize-1) { /* should never happen */ |
| 413 | DISPLAY("no difference detected\n"); |
| 414 | } } |
| 415 | break; |
| 416 | } } /* CRC Checking */ |
Yann Collet | e8c6bb1 | 2015-07-26 00:23:57 +0100 | [diff] [blame] | 417 | #endif |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 418 | } /* for (testNb = 1; testNb <= (g_nbIterations + !g_nbIterations); testNb++) */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 419 | |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 420 | if (crcOrig == crcCheck) { |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 421 | result->ratio = ratio; |
| 422 | result->cSize = cSize; |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 423 | result->cSpeed = (double)srcSize / fastestC; |
inikep | 06f793a | 2016-03-29 11:17:58 +0200 | [diff] [blame] | 424 | result->dSpeed = (double)srcSize / fastestD; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 425 | } |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 426 | DISPLAYLEVEL(2, "%2i#\n", cLevel); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 427 | } /* Bench */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 428 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 429 | /* clean up */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 430 | free(compressedBuffer); |
| 431 | free(resultBuffer); |
Yann Collet | 2630a5e | 2016-01-14 19:13:22 +0100 | [diff] [blame] | 432 | ZSTD_freeCCtx(refCtx); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 433 | ZSTD_freeCCtx(ctx); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 434 | ZSTD_freeDCtx(refDCtx); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 435 | ZSTD_freeDCtx(dctx); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 440 | static size_t BMK_findMaxMem(U64 requiredMem) |
| 441 | { |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 442 | size_t const step = 64 MB; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 443 | BYTE* testmem = NULL; |
| 444 | |
| 445 | requiredMem = (((requiredMem >> 26) + 1) << 26); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 446 | requiredMem += step; |
Yann Collet | 050efba | 2015-11-03 09:49:30 +0100 | [diff] [blame] | 447 | if (requiredMem > maxMemory) requiredMem = maxMemory; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 448 | |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 449 | do { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 450 | testmem = (BYTE*)malloc((size_t)requiredMem); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 451 | requiredMem -= step; |
| 452 | } while (!testmem); |
| 453 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 454 | free(testmem); |
Yann Collet | de406ee | 2016-03-20 15:46:10 +0100 | [diff] [blame] | 455 | return (size_t)(requiredMem); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 456 | } |
| 457 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 458 | static void BMK_benchCLevel(void* srcBuffer, size_t benchedSize, |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 459 | const char* displayName, int cLevel, int cLevelLast, |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 460 | const size_t* fileSizes, unsigned nbFiles, |
| 461 | const void* dictBuffer, size_t dictBufferSize) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 462 | { |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 463 | benchResult_t result, total; |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 464 | int l; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 465 | |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 466 | SET_HIGH_PRIORITY; |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 467 | |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 468 | const char* pch = strrchr(displayName, '\\'); /* Windows */ |
| 469 | if (!pch) pch = strrchr(displayName, '/'); /* Linux */ |
| 470 | if (pch) displayName = pch+1; |
inikep | 4e26bb6 | 2016-03-14 12:48:51 +0100 | [diff] [blame] | 471 | |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 472 | memset(&result, 0, sizeof(result)); |
| 473 | memset(&total, 0, sizeof(total)); |
inikep | 5fdd0b4 | 2016-03-14 19:51:11 +0100 | [diff] [blame] | 474 | |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 475 | if (g_displayLevel == 1 && !g_additionalParam) |
inikep | 2872b6f | 2016-03-22 14:38:34 +0100 | [diff] [blame] | 476 | 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] | 477 | |
| 478 | if (cLevelLast < cLevel) cLevelLast = cLevel; |
| 479 | |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 480 | for (l=cLevel; l <= cLevelLast; l++) { |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 481 | BMK_benchMem(srcBuffer, benchedSize, |
inikep | 472638c | 2016-03-23 12:28:28 +0100 | [diff] [blame] | 482 | displayName, l, |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 483 | fileSizes, nbFiles, |
| 484 | dictBuffer, dictBufferSize, &result); |
| 485 | if (g_displayLevel == 1) { |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 486 | if (g_additionalParam) |
| 487 | 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] | 488 | else |
| 489 | 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] | 490 | total.cSize += result.cSize; |
| 491 | total.cSpeed += result.cSpeed; |
| 492 | total.dSpeed += result.dSpeed; |
| 493 | total.ratio += result.ratio; |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 494 | } } |
| 495 | if (g_displayLevel == 1 && cLevelLast > cLevel) { |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 496 | total.cSize /= 1+cLevelLast-cLevel; |
| 497 | total.cSpeed /= 1+cLevelLast-cLevel; |
| 498 | total.dSpeed /= 1+cLevelLast-cLevel; |
| 499 | total.ratio /= 1+cLevelLast-cLevel; |
Yann Collet | 9990986 | 2016-04-09 16:17:18 +0200 | [diff] [blame] | 500 | 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); |
inikep | e9554b7 | 2016-03-14 18:10:30 +0100 | [diff] [blame] | 501 | } |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | static U64 BMK_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles) |
| 505 | { |
| 506 | U64 total = 0; |
| 507 | unsigned n; |
| 508 | for (n=0; n<nbFiles; n++) |
| 509 | total += BMK_getFileSize(fileNamesTable[n]); |
| 510 | return total; |
| 511 | } |
| 512 | |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 513 | /*! BMK_loadFiles() : |
| 514 | Loads `buffer` with content of files listed within `fileNamesTable`. |
| 515 | At most, fills `buffer` entirely */ |
Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 516 | static void BMK_loadFiles(void* buffer, size_t bufferSize, |
| 517 | size_t* fileSizes, |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 518 | const char** fileNamesTable, unsigned nbFiles) |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 519 | { |
inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 520 | size_t pos = 0, totalSize = 0; |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 521 | |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 522 | unsigned n; |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 523 | for (n=0; n<nbFiles; n++) { |
inikep | 4b3c5ee | 2016-04-14 13:43:51 +0200 | [diff] [blame^] | 524 | if (BMK_isDirectory(fileNamesTable[n])) { |
inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 525 | DISPLAYLEVEL(2, "Ignoring %s directory... \n", fileNamesTable[n]); |
| 526 | continue; |
| 527 | } |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 528 | U64 fileSize = BMK_getFileSize(fileNamesTable[n]); |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 529 | FILE* const f = fopen(fileNamesTable[n], "rb"); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 530 | if (f==NULL) EXM_THROW(10, "impossible to open file %s", fileNamesTable[n]); |
| 531 | DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[n]); |
Yann Collet | a5b66e3 | 2016-03-26 01:48:27 +0100 | [diff] [blame] | 532 | if (fileSize > bufferSize-pos) fileSize = bufferSize-pos, nbFiles=n; /* buffer too small - stop after this file */ |
| 533 | { size_t const readSize = fread(((char*)buffer)+pos, 1, (size_t)fileSize, f); |
| 534 | if (readSize != (size_t)fileSize) EXM_THROW(11, "could not read %s", fileNamesTable[n]); |
| 535 | pos += readSize; } |
Yann Collet | a52c98d | 2015-12-16 03:12:31 +0100 | [diff] [blame] | 536 | fileSizes[n] = (size_t)fileSize; |
inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 537 | totalSize += (size_t)fileSize; |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 538 | fclose(f); |
| 539 | } |
inikep | c0d5f4e | 2016-04-13 10:48:04 +0200 | [diff] [blame] | 540 | |
| 541 | if (totalSize == 0) EXM_THROW(12, "no data to bench"); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 542 | } |
| 543 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 544 | static void BMK_benchFileTable(const char** fileNamesTable, unsigned nbFiles, |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 545 | const char* dictFileName, int cLevel, int cLevelLast) |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 546 | { |
| 547 | void* srcBuffer; |
| 548 | size_t benchedSize; |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 549 | void* dictBuffer = NULL; |
| 550 | size_t dictBufferSize = 0; |
| 551 | size_t* fileSizes = (size_t*)malloc(nbFiles * sizeof(size_t)); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 552 | U64 totalSizeToLoad = BMK_getTotalFileSize(fileNamesTable, nbFiles); |
| 553 | char mfName[20] = {0}; |
| 554 | const char* displayName = NULL; |
| 555 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 556 | if (!fileSizes) EXM_THROW(12, "not enough memory for fileSizes"); |
| 557 | |
| 558 | /* Load dictionary */ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 559 | if (dictFileName != NULL) { |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 560 | U64 dictFileSize = BMK_getFileSize(dictFileName); |
| 561 | if (dictFileSize > 64 MB) EXM_THROW(10, "dictionary file %s too large", dictFileName); |
| 562 | dictBufferSize = (size_t)dictFileSize; |
| 563 | dictBuffer = malloc(dictBufferSize); |
| 564 | if (dictBuffer==NULL) EXM_THROW(11, "not enough memory for dictionary (%u bytes)", (U32)dictBufferSize); |
| 565 | BMK_loadFiles(dictBuffer, dictBufferSize, fileSizes, &dictFileName, 1); |
| 566 | } |
| 567 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 568 | /* Memory allocation & restrictions */ |
| 569 | benchedSize = BMK_findMaxMem(totalSizeToLoad * 3) / 3; |
| 570 | if ((U64)benchedSize > totalSizeToLoad) benchedSize = (size_t)totalSizeToLoad; |
| 571 | if (benchedSize < totalSizeToLoad) |
| 572 | DISPLAY("Not enough memory; testing %u MB only...\n", (U32)(benchedSize >> 20)); |
| 573 | srcBuffer = malloc(benchedSize); |
| 574 | if (!srcBuffer) EXM_THROW(12, "not enough memory"); |
| 575 | |
| 576 | /* Load input buffer */ |
Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 577 | BMK_loadFiles(srcBuffer, benchedSize, fileSizes, fileNamesTable, nbFiles); |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 578 | |
| 579 | /* Bench */ |
| 580 | snprintf (mfName, sizeof(mfName), " %u files", nbFiles); |
| 581 | if (nbFiles > 1) displayName = mfName; |
| 582 | else displayName = fileNamesTable[0]; |
| 583 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 584 | BMK_benchCLevel(srcBuffer, benchedSize, |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 585 | displayName, cLevel, cLevelLast, |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 586 | fileSizes, nbFiles, |
| 587 | dictBuffer, dictBufferSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 588 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 589 | /* clean up */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 590 | free(srcBuffer); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 591 | free(dictBuffer); |
Yann Collet | 7061135 | 2015-12-16 03:01:03 +0100 | [diff] [blame] | 592 | free(fileSizes); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 596 | static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 597 | { |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 598 | char name[20] = {0}; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 599 | size_t benchedSize = 10000000; |
| 600 | void* srcBuffer = malloc(benchedSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 601 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 602 | /* Memory allocation */ |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 603 | if (!srcBuffer) EXM_THROW(21, "not enough memory"); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 604 | |
| 605 | /* Fill input buffer */ |
Yann Collet | d062f13 | 2015-12-01 01:31:17 +0100 | [diff] [blame] | 606 | RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 607 | |
| 608 | /* Bench */ |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 609 | snprintf (name, sizeof(name), "Synthetic %2u%%", (unsigned)(compressibility*100)); |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 610 | BMK_benchCLevel(srcBuffer, benchedSize, name, cLevel, cLevelLast, &benchedSize, 1, NULL, 0); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 611 | |
Yann Collet | ed699e6 | 2015-12-16 02:37:24 +0100 | [diff] [blame] | 612 | /* clean up */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 613 | free(srcBuffer); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 617 | int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 618 | const char* dictFileName, int cLevel, int cLevelLast) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 619 | { |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 620 | double const compressibility = (double)g_compressibilityDefault / 100; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 621 | |
| 622 | if (nbFiles == 0) |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 623 | BMK_syntheticTest(cLevel, cLevelLast, compressibility); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 624 | else |
inikep | eaba91a | 2016-03-23 20:30:26 +0100 | [diff] [blame] | 625 | BMK_benchFileTable(fileNamesTable, nbFiles, dictFileName, cLevel, cLevelLast); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 626 | return 0; |
| 627 | } |
| 628 | |