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