Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
Yann Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 2 | * Copyright (c) 2015-present, Yann Collet, Facebook, Inc. |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 5 | * This source code is licensed under both the BSD-style license (found in the |
| 6 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 7 | * in the COPYING file in the root directory of this source tree). |
Yann Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 8 | * You may select, at your option, one of the above-listed licenses. |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 9 | */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 10 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 11 | |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 12 | /*_************************************ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 13 | * Includes |
| 14 | **************************************/ |
inikep | 13c8424 | 2016-05-05 13:58:56 +0200 | [diff] [blame] | 15 | #include "util.h" /* Compiler options, UTIL_GetFileSize */ |
| 16 | #include <stdlib.h> /* malloc */ |
| 17 | #include <stdio.h> /* fprintf, fopen, ftello64 */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 18 | |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 19 | #include "mem.h" /* U32 */ |
Przemyslaw Skibinski | cc38870 | 2016-11-21 13:58:58 +0100 | [diff] [blame] | 20 | #ifndef ZSTD_DLL_IMPORT |
| 21 | #include "zstd_internal.h" /* ZSTD_blockHeaderSize, blockType_e, KB, MB */ |
Przemyslaw Skibinski | cc38870 | 2016-11-21 13:58:58 +0100 | [diff] [blame] | 22 | #else |
| 23 | #define KB *(1 <<10) |
| 24 | #define MB *(1 <<20) |
| 25 | #define GB *(1U<<30) |
Yann Collet | 952d06f | 2017-02-27 17:58:02 -0800 | [diff] [blame] | 26 | typedef enum { bt_raw, bt_rle, bt_compressed, bt_reserved } blockType_e; |
Przemyslaw Skibinski | cc38870 | 2016-11-21 13:58:58 +0100 | [diff] [blame] | 27 | #endif |
cyan4973 | b5bb7c6 | 2017-06-29 19:59:37 -0700 | [diff] [blame] | 28 | #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressBegin, ZSTD_compressContinue, etc. */ |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 29 | #include "zstd.h" /* ZSTD_versionString */ |
| 30 | #include "util.h" /* time functions */ |
Yann Collet | fb98fd0 | 2015-02-10 18:15:20 +0100 | [diff] [blame] | 31 | #include "datagen.h" |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 32 | |
| 33 | |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 34 | /*_************************************ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 35 | * Constants |
| 36 | **************************************/ |
Yann Collet | fd9d6b8 | 2015-10-26 00:06:36 +0100 | [diff] [blame] | 37 | #define PROGRAM_DESCRIPTION "Zstandard speed analyzer" |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 38 | #define AUTHOR "Yann Collet" |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 39 | #define WELCOME_MESSAGE "*** %s %s %i-bits, by %s (%s) ***\n", PROGRAM_DESCRIPTION, ZSTD_versionString(), (int)(sizeof(void*)*8), AUTHOR, __DATE__ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 40 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 41 | #define NBLOOPS 6 |
Yann Collet | cdabd4a | 2016-03-17 16:18:36 +0100 | [diff] [blame] | 42 | #define TIMELOOP_S 2 |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 43 | |
| 44 | #define KNUTH 2654435761U |
| 45 | #define MAX_MEM (1984 MB) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 46 | |
Yann Collet | f66d2ba | 2015-02-11 08:34:50 +0100 | [diff] [blame] | 47 | #define COMPRESSIBILITY_DEFAULT 0.50 |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 48 | static const size_t g_sampleSize = 10000000; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 49 | |
| 50 | |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 51 | /*_************************************ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 52 | * Macros |
| 53 | **************************************/ |
| 54 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 55 | |
| 56 | |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 57 | /*_************************************ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 58 | * Benchmark Parameters |
| 59 | **************************************/ |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 60 | static U32 g_nbIterations = NBLOOPS; |
Yann Collet | f66d2ba | 2015-02-11 08:34:50 +0100 | [diff] [blame] | 61 | static double g_compressibility = COMPRESSIBILITY_DEFAULT; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 62 | |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 63 | static void BMK_SetNbIterations(U32 nbLoops) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 64 | { |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 65 | g_nbIterations = nbLoops; |
| 66 | DISPLAY("- %i iterations -\n", g_nbIterations); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 70 | /*_******************************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 71 | * Private functions |
| 72 | *********************************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 73 | static size_t BMK_findMaxMem(U64 requiredMem) |
| 74 | { |
Yann Collet | b58c685 | 2016-03-25 20:29:35 +0100 | [diff] [blame] | 75 | size_t const step = 64 MB; |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 76 | void* testmem = NULL; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 77 | |
| 78 | requiredMem = (((requiredMem >> 26) + 1) << 26); |
| 79 | if (requiredMem > MAX_MEM) requiredMem = MAX_MEM; |
| 80 | |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 81 | requiredMem += step; |
| 82 | do { |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 83 | testmem = malloc ((size_t)requiredMem); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 84 | requiredMem -= step; |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 85 | } while (!testmem); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 86 | |
| 87 | free (testmem); |
Yann Collet | 699b14d | 2016-03-17 19:37:33 +0100 | [diff] [blame] | 88 | return (size_t) requiredMem; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 92 | /*_******************************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 93 | * Benchmark wrappers |
| 94 | *********************************************************/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 95 | size_t local_ZSTD_compress(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize) |
| 96 | { |
| 97 | (void)buff2; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 98 | return ZSTD_compress(dst, dstSize, src, srcSize, 1); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Yann Collet | 31922d7 | 2016-03-16 16:05:18 +0100 | [diff] [blame] | 101 | static size_t g_cSize = 0; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 102 | size_t local_ZSTD_decompress(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize) |
| 103 | { |
| 104 | (void)src; (void)srcSize; |
| 105 | return ZSTD_decompress(dst, dstSize, buff2, g_cSize); |
| 106 | } |
| 107 | |
Przemyslaw Skibinski | cc38870 | 2016-11-21 13:58:58 +0100 | [diff] [blame] | 108 | static ZSTD_DCtx* g_zdc = NULL; |
cyan4973 | b5bb7c6 | 2017-06-29 19:59:37 -0700 | [diff] [blame] | 109 | |
| 110 | #ifndef ZSTD_DLL_IMPORT |
Yann Collet | cfdeb34 | 2015-11-12 16:00:04 +0100 | [diff] [blame] | 111 | extern size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* ctx, const void* src, size_t srcSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 112 | size_t local_ZSTD_decodeLiteralsBlock(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize) |
| 113 | { |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 114 | (void)src; (void)srcSize; (void)dst; (void)dstSize; |
Yann Collet | 31922d7 | 2016-03-16 16:05:18 +0100 | [diff] [blame] | 115 | return ZSTD_decodeLiteralsBlock((ZSTD_DCtx*)g_zdc, buff2, g_cSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Yann Collet | 0be21d7 | 2016-09-13 17:33:47 +0200 | [diff] [blame] | 118 | extern size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeq, const void* src, size_t srcSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 119 | size_t local_ZSTD_decodeSeqHeaders(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize) |
| 120 | { |
Yann Collet | f3cb79b | 2015-08-20 00:02:43 +0100 | [diff] [blame] | 121 | int nbSeq; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 122 | (void)src; (void)srcSize; (void)dst; (void)dstSize; |
Yann Collet | 0be21d7 | 2016-09-13 17:33:47 +0200 | [diff] [blame] | 123 | return ZSTD_decodeSeqHeaders(g_zdc, &nbSeq, buff2, g_cSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 124 | } |
Przemyslaw Skibinski | 179555c | 2016-11-15 18:05:46 +0100 | [diff] [blame] | 125 | #endif |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 126 | |
Yann Collet | 589f011 | 2016-10-28 15:17:38 -0700 | [diff] [blame] | 127 | static ZSTD_CStream* g_cstream= NULL; |
| 128 | size_t local_ZSTD_compressStream(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize) |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 129 | { |
Yann Collet | 589f011 | 2016-10-28 15:17:38 -0700 | [diff] [blame] | 130 | ZSTD_outBuffer buffOut; |
| 131 | ZSTD_inBuffer buffIn; |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 132 | (void)buff2; |
Yann Collet | 589f011 | 2016-10-28 15:17:38 -0700 | [diff] [blame] | 133 | ZSTD_initCStream(g_cstream, 1); |
| 134 | buffOut.dst = dst; |
| 135 | buffOut.size = dstCapacity; |
| 136 | buffOut.pos = 0; |
| 137 | buffIn.src = src; |
| 138 | buffIn.size = srcSize; |
| 139 | buffIn.pos = 0; |
| 140 | ZSTD_compressStream(g_cstream, &buffOut, &buffIn); |
| 141 | ZSTD_endStream(g_cstream, &buffOut); |
| 142 | return buffOut.pos; |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 143 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 144 | |
Yann Collet | 2e84bec | 2017-06-29 13:03:10 -0700 | [diff] [blame] | 145 | static size_t local_ZSTD_compress_generic_end(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize) |
| 146 | { |
| 147 | ZSTD_outBuffer buffOut; |
| 148 | ZSTD_inBuffer buffIn; |
| 149 | (void)buff2; |
| 150 | ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1); |
| 151 | buffOut.dst = dst; |
| 152 | buffOut.size = dstCapacity; |
| 153 | buffOut.pos = 0; |
| 154 | buffIn.src = src; |
| 155 | buffIn.size = srcSize; |
| 156 | buffIn.pos = 0; |
| 157 | ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end); |
| 158 | return buffOut.pos; |
| 159 | } |
| 160 | |
| 161 | static size_t local_ZSTD_compress_generic_continue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize) |
| 162 | { |
| 163 | ZSTD_outBuffer buffOut; |
| 164 | ZSTD_inBuffer buffIn; |
| 165 | (void)buff2; |
| 166 | ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1); |
| 167 | buffOut.dst = dst; |
| 168 | buffOut.size = dstCapacity; |
| 169 | buffOut.pos = 0; |
| 170 | buffIn.src = src; |
| 171 | buffIn.size = srcSize; |
| 172 | buffIn.pos = 0; |
| 173 | ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_continue); |
| 174 | ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end); |
| 175 | return buffOut.pos; |
| 176 | } |
| 177 | |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 178 | static size_t local_ZSTD_compress_generic_T2_end(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize) |
| 179 | { |
| 180 | ZSTD_outBuffer buffOut; |
| 181 | ZSTD_inBuffer buffIn; |
| 182 | (void)buff2; |
| 183 | ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1); |
| 184 | ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_nbThreads, 2); |
| 185 | buffOut.dst = dst; |
| 186 | buffOut.size = dstCapacity; |
| 187 | buffOut.pos = 0; |
| 188 | buffIn.src = src; |
| 189 | buffIn.size = srcSize; |
| 190 | buffIn.pos = 0; |
| 191 | while (ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end)) {} |
| 192 | return buffOut.pos; |
| 193 | } |
| 194 | |
| 195 | static size_t local_ZSTD_compress_generic_T2_continue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize) |
| 196 | { |
| 197 | ZSTD_outBuffer buffOut; |
| 198 | ZSTD_inBuffer buffIn; |
| 199 | (void)buff2; |
| 200 | ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1); |
| 201 | ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_nbThreads, 2); |
| 202 | buffOut.dst = dst; |
| 203 | buffOut.size = dstCapacity; |
| 204 | buffOut.pos = 0; |
| 205 | buffIn.src = src; |
| 206 | buffIn.size = srcSize; |
| 207 | buffIn.pos = 0; |
| 208 | ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_continue); |
| 209 | while(ZSTD_compress_generic(g_cstream, &buffOut, &buffIn, ZSTD_e_end)) {} |
| 210 | return buffOut.pos; |
| 211 | } |
| 212 | |
Yann Collet | 589f011 | 2016-10-28 15:17:38 -0700 | [diff] [blame] | 213 | static ZSTD_DStream* g_dstream= NULL; |
| 214 | static size_t local_ZSTD_decompressStream(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize) |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 215 | { |
Yann Collet | 589f011 | 2016-10-28 15:17:38 -0700 | [diff] [blame] | 216 | ZSTD_outBuffer buffOut; |
| 217 | ZSTD_inBuffer buffIn; |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 218 | (void)src; (void)srcSize; |
Yann Collet | 589f011 | 2016-10-28 15:17:38 -0700 | [diff] [blame] | 219 | ZSTD_initDStream(g_dstream); |
| 220 | buffOut.dst = dst; |
| 221 | buffOut.size = dstCapacity; |
| 222 | buffOut.pos = 0; |
| 223 | buffIn.src = buff2; |
| 224 | buffIn.size = g_cSize; |
| 225 | buffIn.pos = 0; |
| 226 | ZSTD_decompressStream(g_dstream, &buffOut, &buffIn); |
| 227 | return buffOut.pos; |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Yann Collet | 31922d7 | 2016-03-16 16:05:18 +0100 | [diff] [blame] | 230 | static ZSTD_CCtx* g_zcc = NULL; |
cyan4973 | b5bb7c6 | 2017-06-29 19:59:37 -0700 | [diff] [blame] | 231 | |
| 232 | #ifndef ZSTD_DLL_IMPORT |
Yann Collet | 31922d7 | 2016-03-16 16:05:18 +0100 | [diff] [blame] | 233 | size_t local_ZSTD_compressContinue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize) |
| 234 | { |
Yann Collet | 31922d7 | 2016-03-16 16:05:18 +0100 | [diff] [blame] | 235 | (void)buff2; |
| 236 | ZSTD_compressBegin(g_zcc, 1); |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 237 | return ZSTD_compressEnd(g_zcc, dst, dstCapacity, src, srcSize); |
Yann Collet | 31922d7 | 2016-03-16 16:05:18 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Przemyslaw Skibinski | 4beb51f | 2016-12-20 10:17:21 +0100 | [diff] [blame] | 240 | #define FIRST_BLOCK_SIZE 8 |
| 241 | size_t local_ZSTD_compressContinue_extDict(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize) |
| 242 | { |
| 243 | BYTE firstBlockBuf[FIRST_BLOCK_SIZE]; |
| 244 | |
| 245 | (void)buff2; |
| 246 | memcpy(firstBlockBuf, src, FIRST_BLOCK_SIZE); |
| 247 | ZSTD_compressBegin(g_zcc, 1); |
| 248 | |
| 249 | { size_t const compressResult = ZSTD_compressContinue(g_zcc, dst, dstCapacity, firstBlockBuf, FIRST_BLOCK_SIZE); |
| 250 | if (ZSTD_isError(compressResult)) { DISPLAY("local_ZSTD_compressContinue_extDict error : %s\n", ZSTD_getErrorName(compressResult)); return compressResult; } |
| 251 | dst = (BYTE*)dst + compressResult; |
| 252 | dstCapacity -= compressResult; |
| 253 | } |
| 254 | return ZSTD_compressEnd(g_zcc, dst, dstCapacity, (const BYTE*)src + FIRST_BLOCK_SIZE, srcSize - FIRST_BLOCK_SIZE); |
| 255 | } |
| 256 | |
Yann Collet | 31922d7 | 2016-03-16 16:05:18 +0100 | [diff] [blame] | 257 | size_t local_ZSTD_decompressContinue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize) |
| 258 | { |
| 259 | size_t regeneratedSize = 0; |
| 260 | const BYTE* ip = (const BYTE*)buff2; |
| 261 | const BYTE* const iend = ip + g_cSize; |
| 262 | BYTE* op = (BYTE*)dst; |
| 263 | size_t remainingCapacity = dstCapacity; |
| 264 | |
| 265 | (void)src; (void)srcSize; |
| 266 | ZSTD_decompressBegin(g_zdc); |
| 267 | while (ip < iend) { |
| 268 | size_t const iSize = ZSTD_nextSrcSizeToDecompress(g_zdc); |
| 269 | size_t const decodedSize = ZSTD_decompressContinue(g_zdc, op, remainingCapacity, ip, iSize); |
| 270 | ip += iSize; |
| 271 | regeneratedSize += decodedSize; |
| 272 | op += decodedSize; |
| 273 | remainingCapacity -= decodedSize; |
| 274 | } |
| 275 | |
| 276 | return regeneratedSize; |
| 277 | } |
Przemyslaw Skibinski | cc38870 | 2016-11-21 13:58:58 +0100 | [diff] [blame] | 278 | #endif |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 279 | |
| 280 | |
| 281 | /*_******************************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 282 | * Bench functions |
| 283 | *********************************************************/ |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 284 | static size_t benchMem(const void* src, size_t srcSize, U32 benchNb) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 285 | { |
| 286 | BYTE* dstBuff; |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 287 | size_t const dstBuffSize = ZSTD_compressBound(srcSize); |
| 288 | void* buff2; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 289 | const char* benchName; |
| 290 | size_t (*benchFunction)(void* dst, size_t dstSize, void* verifBuff, const void* src, size_t srcSize); |
| 291 | double bestTime = 100000000.; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 292 | |
Yann Collet | fb98fd0 | 2015-02-10 18:15:20 +0100 | [diff] [blame] | 293 | /* Selection */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 294 | switch(benchNb) |
| 295 | { |
| 296 | case 1: |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 297 | benchFunction = local_ZSTD_compress; benchName = "compress(1)"; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 298 | break; |
Yann Collet | f141eaa | 2016-03-15 21:13:52 +0100 | [diff] [blame] | 299 | case 2: |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 300 | benchFunction = local_ZSTD_decompress; benchName = "decompress"; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 301 | break; |
Przemyslaw Skibinski | cc38870 | 2016-11-21 13:58:58 +0100 | [diff] [blame] | 302 | #ifndef ZSTD_DLL_IMPORT |
Yann Collet | 31922d7 | 2016-03-16 16:05:18 +0100 | [diff] [blame] | 303 | case 11: |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 304 | benchFunction = local_ZSTD_compressContinue; benchName = "compressContinue(1)"; |
Yann Collet | 31922d7 | 2016-03-16 16:05:18 +0100 | [diff] [blame] | 305 | break; |
| 306 | case 12: |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 307 | benchFunction = local_ZSTD_compressContinue_extDict; benchName = "compressContinue_extDict"; |
Przemyslaw Skibinski | 4beb51f | 2016-12-20 10:17:21 +0100 | [diff] [blame] | 308 | break; |
| 309 | case 13: |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 310 | benchFunction = local_ZSTD_decompressContinue; benchName = "decompressContinue"; |
Yann Collet | 31922d7 | 2016-03-16 16:05:18 +0100 | [diff] [blame] | 311 | break; |
Nick Terrell | 5152fb2 | 2017-03-29 18:51:58 -0700 | [diff] [blame] | 312 | case 31: |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 313 | benchFunction = local_ZSTD_decodeLiteralsBlock; benchName = "decodeLiteralsBlock"; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 314 | break; |
| 315 | case 32: |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 316 | benchFunction = local_ZSTD_decodeSeqHeaders; benchName = "decodeSeqHeaders"; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 317 | break; |
Przemyslaw Skibinski | 179555c | 2016-11-15 18:05:46 +0100 | [diff] [blame] | 318 | #endif |
Nick Terrell | 5152fb2 | 2017-03-29 18:51:58 -0700 | [diff] [blame] | 319 | case 41: |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 320 | benchFunction = local_ZSTD_compressStream; benchName = "compressStream(1)"; |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 321 | break; |
| 322 | case 42: |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 323 | benchFunction = local_ZSTD_decompressStream; benchName = "decompressStream"; |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 324 | break; |
Yann Collet | 2e84bec | 2017-06-29 13:03:10 -0700 | [diff] [blame] | 325 | case 51: |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 326 | benchFunction = local_ZSTD_compress_generic_continue; benchName = "compress_generic, continue"; |
Yann Collet | 2e84bec | 2017-06-29 13:03:10 -0700 | [diff] [blame] | 327 | break; |
| 328 | case 52: |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 329 | benchFunction = local_ZSTD_compress_generic_end; benchName = "compress_generic, end"; |
| 330 | break; |
| 331 | case 61: |
| 332 | benchFunction = local_ZSTD_compress_generic_T2_continue; benchName = "compress_generic, -T2, continue"; |
| 333 | break; |
| 334 | case 62: |
| 335 | benchFunction = local_ZSTD_compress_generic_T2_end; benchName = "compress_generic, -T2, end"; |
Yann Collet | 2e84bec | 2017-06-29 13:03:10 -0700 | [diff] [blame] | 336 | break; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 337 | default : |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | /* Allocation */ |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 342 | dstBuff = (BYTE*)malloc(dstBuffSize); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 343 | buff2 = malloc(dstBuffSize); |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 344 | if ((!dstBuff) || (!buff2)) { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 345 | DISPLAY("\nError: not enough memory!\n"); |
| 346 | free(dstBuff); free(buff2); |
| 347 | return 12; |
| 348 | } |
Yann Collet | 2e84bec | 2017-06-29 13:03:10 -0700 | [diff] [blame] | 349 | if (g_zcc==NULL) g_zcc = ZSTD_createCCtx(); |
| 350 | if (g_zdc==NULL) g_zdc = ZSTD_createDCtx(); |
| 351 | if (g_cstream==NULL) g_cstream = ZSTD_createCStream(); |
| 352 | if (g_dstream==NULL) g_dstream = ZSTD_createDStream(); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 353 | |
| 354 | /* Preparation */ |
| 355 | switch(benchNb) |
| 356 | { |
Yann Collet | f141eaa | 2016-03-15 21:13:52 +0100 | [diff] [blame] | 357 | case 2: |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 358 | g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 359 | break; |
Przemyslaw Skibinski | cc38870 | 2016-11-21 13:58:58 +0100 | [diff] [blame] | 360 | #ifndef ZSTD_DLL_IMPORT |
Przemyslaw Skibinski | 4beb51f | 2016-12-20 10:17:21 +0100 | [diff] [blame] | 361 | case 13 : |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 362 | g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1); |
| 363 | break; |
Yann Collet | fb98fd0 | 2015-02-10 18:15:20 +0100 | [diff] [blame] | 364 | case 31: /* ZSTD_decodeLiteralsBlock */ |
Yann Collet | 31922d7 | 2016-03-16 16:05:18 +0100 | [diff] [blame] | 365 | { blockProperties_t bp; |
Yann Collet | 542c9df | 2017-05-09 15:46:07 -0700 | [diff] [blame] | 366 | ZSTD_frameHeader zfp; |
Yann Collet | 512220a | 2016-03-17 16:42:16 +0100 | [diff] [blame] | 367 | size_t frameHeaderSize, skippedSize; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 368 | g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1); |
Yann Collet | 542c9df | 2017-05-09 15:46:07 -0700 | [diff] [blame] | 369 | frameHeaderSize = ZSTD_getFrameHeader(&zfp, dstBuff, ZSTD_frameHeaderSize_min); |
Yann Collet | 512220a | 2016-03-17 16:42:16 +0100 | [diff] [blame] | 370 | if (frameHeaderSize==0) frameHeaderSize = ZSTD_frameHeaderSize_min; |
| 371 | ZSTD_getcBlockSize(dstBuff+frameHeaderSize, dstBuffSize, &bp); /* Get 1st block type */ |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 372 | if (bp.blockType != bt_compressed) { |
Yann Collet | 759433d | 2015-01-24 13:31:55 +0100 | [diff] [blame] | 373 | DISPLAY("ZSTD_decodeLiteralsBlock : impossible to test on this sample (not compressible)\n"); |
Yann Collet | 93a823c | 2015-11-13 15:08:43 +0100 | [diff] [blame] | 374 | goto _cleanOut; |
Yann Collet | 759433d | 2015-01-24 13:31:55 +0100 | [diff] [blame] | 375 | } |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 376 | skippedSize = frameHeaderSize + ZSTD_blockHeaderSize; |
Yann Collet | 512220a | 2016-03-17 16:42:16 +0100 | [diff] [blame] | 377 | memcpy(buff2, dstBuff+skippedSize, g_cSize-skippedSize); |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 378 | srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */ |
Yann Collet | ea1f50b | 2017-09-27 13:51:05 -0700 | [diff] [blame^] | 379 | ZSTD_decompressBegin(g_zdc); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 380 | break; |
| 381 | } |
Yann Collet | fb98fd0 | 2015-02-10 18:15:20 +0100 | [diff] [blame] | 382 | case 32: /* ZSTD_decodeSeqHeaders */ |
Yann Collet | 31922d7 | 2016-03-16 16:05:18 +0100 | [diff] [blame] | 383 | { blockProperties_t bp; |
Yann Collet | 542c9df | 2017-05-09 15:46:07 -0700 | [diff] [blame] | 384 | ZSTD_frameHeader zfp; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 385 | const BYTE* ip = dstBuff; |
| 386 | const BYTE* iend; |
Yann Collet | 512220a | 2016-03-17 16:42:16 +0100 | [diff] [blame] | 387 | size_t frameHeaderSize, cBlockSize; |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 388 | ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1); /* it would be better to use direct block compression here */ |
Yann Collet | 512220a | 2016-03-17 16:42:16 +0100 | [diff] [blame] | 389 | g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1); |
Yann Collet | 542c9df | 2017-05-09 15:46:07 -0700 | [diff] [blame] | 390 | frameHeaderSize = ZSTD_getFrameHeader(&zfp, dstBuff, ZSTD_frameHeaderSize_min); |
Yann Collet | 512220a | 2016-03-17 16:42:16 +0100 | [diff] [blame] | 391 | if (frameHeaderSize==0) frameHeaderSize = ZSTD_frameHeaderSize_min; |
| 392 | ip += frameHeaderSize; /* Skip frame Header */ |
| 393 | cBlockSize = ZSTD_getcBlockSize(ip, dstBuffSize, &bp); /* Get 1st block type */ |
Yann Collet | 69afc71 | 2016-03-15 21:44:39 +0100 | [diff] [blame] | 394 | if (bp.blockType != bt_compressed) { |
Yann Collet | 759433d | 2015-01-24 13:31:55 +0100 | [diff] [blame] | 395 | DISPLAY("ZSTD_decodeSeqHeaders : impossible to test on this sample (not compressible)\n"); |
Yann Collet | 93a823c | 2015-11-13 15:08:43 +0100 | [diff] [blame] | 396 | goto _cleanOut; |
Yann Collet | 759433d | 2015-01-24 13:31:55 +0100 | [diff] [blame] | 397 | } |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 398 | iend = ip + ZSTD_blockHeaderSize + cBlockSize; /* End of first block */ |
| 399 | ip += ZSTD_blockHeaderSize; /* skip block header */ |
Yann Collet | 0be21d7 | 2016-09-13 17:33:47 +0200 | [diff] [blame] | 400 | ZSTD_decompressBegin(g_zdc); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 401 | ip += ZSTD_decodeLiteralsBlock(g_zdc, ip, iend-ip); /* skip literal segment */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 402 | g_cSize = iend-ip; |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 403 | memcpy(buff2, ip, g_cSize); /* copy rest of block (it starts by SeqHeader) */ |
| 404 | srcSize = srcSize > 128 KB ? 128 KB : srcSize; /* speed relative to block */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 405 | break; |
| 406 | } |
Przemyslaw Skibinski | 811b34d | 2016-11-15 19:02:39 +0100 | [diff] [blame] | 407 | #else |
| 408 | case 31: |
| 409 | goto _cleanOut; |
Przemyslaw Skibinski | 179555c | 2016-11-15 18:05:46 +0100 | [diff] [blame] | 410 | #endif |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 411 | case 42 : |
Yann Collet | 34b20ec | 2016-03-15 20:47:23 +0100 | [diff] [blame] | 412 | g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1); |
| 413 | break; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 414 | |
| 415 | /* test functions */ |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 416 | /* by convention, test functions can be added > 100 */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 417 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 418 | default : ; |
| 419 | } |
| 420 | |
Yann Collet | 759433d | 2015-01-24 13:31:55 +0100 | [diff] [blame] | 421 | { size_t i; for (i=0; i<dstBuffSize; i++) dstBuff[i]=(BYTE)i; } /* warming up memory */ |
| 422 | |
Yann Collet | 589f011 | 2016-10-28 15:17:38 -0700 | [diff] [blame] | 423 | { U32 loopNb; |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 424 | # define TIME_SEC_MICROSEC (1*1000000ULL) /* 1 second */ |
| 425 | U64 const clockLoop = TIMELOOP_S * TIME_SEC_MICROSEC; |
Yann Collet | 952d06f | 2017-02-27 17:58:02 -0800 | [diff] [blame] | 426 | DISPLAY("%2i- %-30.30s : \r", benchNb, benchName); |
Yann Collet | 589f011 | 2016-10-28 15:17:38 -0700 | [diff] [blame] | 427 | for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) { |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 428 | UTIL_time_t clockStart; |
Yann Collet | 589f011 | 2016-10-28 15:17:38 -0700 | [diff] [blame] | 429 | size_t benchResult=0; |
Yann Collet | 83d0c76 | 2017-05-15 17:15:46 -0700 | [diff] [blame] | 430 | U32 nbRounds; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 431 | |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 432 | UTIL_sleepMilli(1); /* give processor time to other processes */ |
Yann Collet | c95c0c9 | 2017-09-12 18:12:46 -0700 | [diff] [blame] | 433 | UTIL_waitForNextTick(); |
| 434 | clockStart = UTIL_getTime(); |
| 435 | for (nbRounds=0; UTIL_clockSpanMicro(clockStart) < clockLoop; nbRounds++) { |
Yann Collet | 589f011 | 2016-10-28 15:17:38 -0700 | [diff] [blame] | 436 | benchResult = benchFunction(dstBuff, dstBuffSize, buff2, src, srcSize); |
| 437 | if (ZSTD_isError(benchResult)) { DISPLAY("ERROR ! %s() => %s !! \n", benchName, ZSTD_getErrorName(benchResult)); exit(1); } |
| 438 | } |
Yann Collet | c95c0c9 | 2017-09-12 18:12:46 -0700 | [diff] [blame] | 439 | { U64 const clockSpanMicro = UTIL_clockSpanMicro(clockStart); |
Yann Collet | e7e5a8c | 2017-06-29 18:56:24 -0700 | [diff] [blame] | 440 | double const averageTime = (double)clockSpanMicro / TIME_SEC_MICROSEC / nbRounds; |
Yann Collet | 83d0c76 | 2017-05-15 17:15:46 -0700 | [diff] [blame] | 441 | if (averageTime < bestTime) bestTime = averageTime; |
| 442 | DISPLAY("%2i- %-30.30s : %7.1f MB/s (%9u)\r", loopNb, benchName, (double)srcSize / (1 MB) / bestTime, (U32)benchResult); |
| 443 | } } } |
Yann Collet | cdabd4a | 2016-03-17 16:18:36 +0100 | [diff] [blame] | 444 | DISPLAY("%2u\n", benchNb); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 445 | |
Yann Collet | 93a823c | 2015-11-13 15:08:43 +0100 | [diff] [blame] | 446 | _cleanOut: |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 447 | free(dstBuff); |
| 448 | free(buff2); |
cyan4973 | 4b26306 | 2017-07-01 08:03:59 -0700 | [diff] [blame] | 449 | ZSTD_freeCCtx(g_zcc); g_zcc=NULL; |
| 450 | ZSTD_freeDCtx(g_zdc); g_zdc=NULL; |
| 451 | ZSTD_freeCStream(g_cstream); g_cstream=NULL; |
| 452 | ZSTD_freeDStream(g_dstream); g_dstream=NULL; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 457 | static int benchSample(U32 benchNb) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 458 | { |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 459 | size_t const benchedSize = g_sampleSize; |
Yann Collet | f66d2ba | 2015-02-11 08:34:50 +0100 | [diff] [blame] | 460 | const char* name = "Sample 10MiB"; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 461 | |
| 462 | /* Allocation */ |
Yann Collet | 69afc71 | 2016-03-15 21:44:39 +0100 | [diff] [blame] | 463 | void* origBuff = malloc(benchedSize); |
| 464 | if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); return 12; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 465 | |
| 466 | /* Fill buffer */ |
Yann Collet | f66d2ba | 2015-02-11 08:34:50 +0100 | [diff] [blame] | 467 | RDG_genBuffer(origBuff, benchedSize, g_compressibility, 0.0, 0); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 468 | |
| 469 | /* bench */ |
| 470 | DISPLAY("\r%79s\r", ""); |
| 471 | DISPLAY(" %s : \n", name); |
| 472 | if (benchNb) |
| 473 | benchMem(origBuff, benchedSize, benchNb); |
| 474 | else |
| 475 | for (benchNb=0; benchNb<100; benchNb++) benchMem(origBuff, benchedSize, benchNb); |
| 476 | |
| 477 | free(origBuff); |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 482 | static int benchFiles(const char** fileNamesTable, const int nbFiles, U32 benchNb) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 483 | { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 484 | /* Loop for each file */ |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 485 | int fileIdx; |
| 486 | for (fileIdx=0; fileIdx<nbFiles; fileIdx++) { |
| 487 | const char* inFileName = fileNamesTable[fileIdx]; |
Yann Collet | 69afc71 | 2016-03-15 21:44:39 +0100 | [diff] [blame] | 488 | FILE* inFile = fopen( inFileName, "rb" ); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 489 | U64 inFileSize; |
| 490 | size_t benchedSize; |
Yann Collet | 69afc71 | 2016-03-15 21:44:39 +0100 | [diff] [blame] | 491 | void* origBuff; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 492 | |
| 493 | /* Check file existence */ |
Yann Collet | 69afc71 | 2016-03-15 21:44:39 +0100 | [diff] [blame] | 494 | if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 495 | |
Yann Collet | 695a6cb | 2015-12-10 15:51:38 +0100 | [diff] [blame] | 496 | /* Memory allocation & restrictions */ |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 497 | inFileSize = UTIL_getFileSize(inFileName); |
Yann Collet | 69afc71 | 2016-03-15 21:44:39 +0100 | [diff] [blame] | 498 | benchedSize = BMK_findMaxMem(inFileSize*3) / 3; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 499 | if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize; |
| 500 | if (benchedSize < inFileSize) |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 501 | DISPLAY("Not enough memory for '%s' full size; testing %u MB only...\n", inFileName, (U32)(benchedSize>>20)); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 502 | |
Yann Collet | 18a6871 | 2015-01-24 12:08:52 +0100 | [diff] [blame] | 503 | /* Alloc */ |
Yann Collet | 69afc71 | 2016-03-15 21:44:39 +0100 | [diff] [blame] | 504 | origBuff = malloc(benchedSize); |
| 505 | if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); fclose(inFile); return 12; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 506 | |
Yann Collet | 18a6871 | 2015-01-24 12:08:52 +0100 | [diff] [blame] | 507 | /* Fill input buffer */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 508 | DISPLAY("Loading %s... \r", inFileName); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 509 | { |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 510 | size_t readSize = fread(origBuff, 1, benchedSize, inFile); |
| 511 | fclose(inFile); |
| 512 | if (readSize != benchedSize) { |
| 513 | DISPLAY("\nError: problem reading file '%s' !! \n", inFileName); |
| 514 | free(origBuff); |
| 515 | return 13; |
| 516 | } } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 517 | |
Yann Collet | 18a6871 | 2015-01-24 12:08:52 +0100 | [diff] [blame] | 518 | /* bench */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 519 | DISPLAY("\r%79s\r", ""); |
| 520 | DISPLAY(" %s : \n", inFileName); |
| 521 | if (benchNb) |
| 522 | benchMem(origBuff, benchedSize, benchNb); |
| 523 | else |
| 524 | for (benchNb=0; benchNb<100; benchNb++) benchMem(origBuff, benchedSize, benchNb); |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 525 | |
| 526 | free(origBuff); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | |
Yann Collet | 69afc71 | 2016-03-15 21:44:39 +0100 | [diff] [blame] | 533 | static int usage(const char* exename) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 534 | { |
| 535 | DISPLAY( "Usage :\n"); |
| 536 | DISPLAY( " %s [arg] file1 file2 ... fileX\n", exename); |
| 537 | DISPLAY( "Arguments :\n"); |
| 538 | DISPLAY( " -H/-h : Help (this text + advanced options)\n"); |
| 539 | return 0; |
| 540 | } |
| 541 | |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 542 | static int usage_advanced(const char* exename) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 543 | { |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 544 | usage(exename); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 545 | DISPLAY( "\nAdvanced options :\n"); |
| 546 | DISPLAY( " -b# : test only function # \n"); |
| 547 | DISPLAY( " -i# : iteration loops [1-9](default : %i)\n", NBLOOPS); |
Yann Collet | f66d2ba | 2015-02-11 08:34:50 +0100 | [diff] [blame] | 548 | DISPLAY( " -P# : sample compressibility (default : %.1f%%)\n", COMPRESSIBILITY_DEFAULT * 100); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 549 | return 0; |
| 550 | } |
| 551 | |
Yann Collet | 69afc71 | 2016-03-15 21:44:39 +0100 | [diff] [blame] | 552 | static int badusage(const char* exename) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 553 | { |
| 554 | DISPLAY("Wrong parameters\n"); |
| 555 | usage(exename); |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 556 | return 1; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 557 | } |
| 558 | |
Yann Collet | 69afc71 | 2016-03-15 21:44:39 +0100 | [diff] [blame] | 559 | int main(int argc, const char** argv) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 560 | { |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 561 | int i, filenamesStart=0, result; |
| 562 | const char* exename = argv[0]; |
| 563 | const char* input_filename = NULL; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 564 | U32 benchNb = 0, main_pause = 0; |
| 565 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 566 | DISPLAY(WELCOME_MESSAGE); |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 567 | if (argc<1) return badusage(exename); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 568 | |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 569 | for(i=1; i<argc; i++) { |
Yann Collet | 69afc71 | 2016-03-15 21:44:39 +0100 | [diff] [blame] | 570 | const char* argument = argv[i]; |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 571 | if(!argument) continue; /* Protection if argument empty */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 572 | |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 573 | /* Commands (note : aggregated commands are allowed) */ |
| 574 | if (argument[0]=='-') { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 575 | |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 576 | while (argument[1]!=0) { |
| 577 | argument++; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 578 | |
| 579 | switch(argument[0]) |
| 580 | { |
Yann Collet | f66d2ba | 2015-02-11 08:34:50 +0100 | [diff] [blame] | 581 | /* Display help on usage */ |
Yann Collet | 589f011 | 2016-10-28 15:17:38 -0700 | [diff] [blame] | 582 | case 'h': |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 583 | case 'H': return usage_advanced(exename); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 584 | |
Yann Collet | f66d2ba | 2015-02-11 08:34:50 +0100 | [diff] [blame] | 585 | /* Pause at the end (hidden option) */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 586 | case 'p': main_pause = 1; break; |
| 587 | |
Yann Collet | f66d2ba | 2015-02-11 08:34:50 +0100 | [diff] [blame] | 588 | /* Select specific algorithm to bench */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 589 | case 'b': |
| 590 | benchNb = 0; |
Yann Collet | 69afc71 | 2016-03-15 21:44:39 +0100 | [diff] [blame] | 591 | while ((argument[1]>= '0') && (argument[1]<= '9')) { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 592 | benchNb *= 10; |
| 593 | benchNb += argument[1] - '0'; |
| 594 | argument++; |
| 595 | } |
| 596 | break; |
| 597 | |
Yann Collet | f66d2ba | 2015-02-11 08:34:50 +0100 | [diff] [blame] | 598 | /* Modify Nb Iterations */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 599 | case 'i': |
Yann Collet | 69afc71 | 2016-03-15 21:44:39 +0100 | [diff] [blame] | 600 | if ((argument[1] >='0') && (argument[1] <='9')) { |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 601 | int iters = argument[1] - '0'; |
| 602 | BMK_SetNbIterations(iters); |
| 603 | argument++; |
| 604 | } |
| 605 | break; |
| 606 | |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 607 | /* Select compressibility of synthetic sample */ |
Yann Collet | f66d2ba | 2015-02-11 08:34:50 +0100 | [diff] [blame] | 608 | case 'P': |
Yann Collet | 69afc71 | 2016-03-15 21:44:39 +0100 | [diff] [blame] | 609 | { U32 proba32 = 0; |
| 610 | while ((argument[1]>= '0') && (argument[1]<= '9')) { |
Yann Collet | f66d2ba | 2015-02-11 08:34:50 +0100 | [diff] [blame] | 611 | proba32 *= 10; |
| 612 | proba32 += argument[1] - '0'; |
| 613 | argument++; |
| 614 | } |
| 615 | g_compressibility = (double)proba32 / 100.; |
| 616 | } |
| 617 | break; |
| 618 | |
| 619 | /* Unknown command */ |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 620 | default : return badusage(exename); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | continue; |
| 624 | } |
| 625 | |
Yann Collet | f66d2ba | 2015-02-11 08:34:50 +0100 | [diff] [blame] | 626 | /* first provided filename is input */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 627 | if (!input_filename) { input_filename=argument; filenamesStart=i; continue; } |
| 628 | } |
| 629 | |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 630 | if (filenamesStart==0) /* no input file */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 631 | result = benchSample(benchNb); |
Yann Collet | 569b81a | 2016-03-16 15:26:51 +0100 | [diff] [blame] | 632 | else |
| 633 | result = benchFiles(argv+filenamesStart, argc-filenamesStart, benchNb); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 634 | |
Yann Collet | b5e06dc | 2015-07-04 23:20:56 -0800 | [diff] [blame] | 635 | if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 636 | |
| 637 | return result; |
| 638 | } |