2013-12-30 17:16:52 +00:00
|
|
|
/*
|
|
|
|
bench.c - Demo program to benchmark open-source compression algorithm
|
2016-11-22 00:42:23 +00:00
|
|
|
Copyright (C) Yann Collet 2012-2016
|
2014-12-10 16:58:15 +00:00
|
|
|
|
2013-12-30 17:16:52 +00:00
|
|
|
GPL v2 License
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
You can contact the author at :
|
2016-11-03 14:12:57 +00:00
|
|
|
- LZ4 source repository : https://github.com/lz4/lz4
|
2014-12-10 16:58:15 +00:00
|
|
|
- LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
|
2013-12-30 17:16:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(_MSC_VER) || defined(_WIN32)
|
2019-12-04 16:49:06 +00:00
|
|
|
/* S_ISREG & gettimeofday() are not supported by MSVC */
|
2013-12-30 17:16:52 +00:00
|
|
|
# define BMK_LEGACY_TIMER 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2015-03-01 23:11:34 +00:00
|
|
|
/**************************************
|
|
|
|
* Includes
|
|
|
|
**************************************/
|
2016-12-21 15:10:09 +00:00
|
|
|
#include "platform.h" /* _CRT_SECURE_NO_WARNINGS, Large Files support */
|
2016-12-21 08:44:59 +00:00
|
|
|
#include "util.h" /* U32, UTIL_getFileSize */
|
2015-05-03 16:57:46 +00:00
|
|
|
#include <stdlib.h> /* malloc, free */
|
2016-12-20 17:05:43 +00:00
|
|
|
#include <stdio.h> /* fprintf, fopen, ftello */
|
2015-05-03 16:57:46 +00:00
|
|
|
#include <sys/types.h> /* stat64 */
|
|
|
|
#include <sys/stat.h> /* stat64 */
|
|
|
|
#include <string.h> /* strcmp */
|
2016-09-03 03:17:19 +00:00
|
|
|
#include <time.h> /* clock_t, clock(), CLOCKS_PER_SEC */
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2019-04-04 19:47:36 +00:00
|
|
|
#define LZ4_DISABLE_DEPRECATE_WARNINGS /* LZ4_decompress_fast */
|
2013-12-30 17:16:52 +00:00
|
|
|
#include "lz4.h"
|
|
|
|
#include "lz4hc.h"
|
2014-09-10 12:00:39 +00:00
|
|
|
#include "lz4frame.h"
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
#include "xxhash.h"
|
|
|
|
|
|
|
|
|
2015-03-01 23:11:34 +00:00
|
|
|
/**************************************
|
|
|
|
* Constants
|
|
|
|
**************************************/
|
2013-12-30 17:16:52 +00:00
|
|
|
#define PROGRAM_DESCRIPTION "LZ4 speed analyzer"
|
|
|
|
#define AUTHOR "Yann Collet"
|
2016-09-03 03:17:19 +00:00
|
|
|
#define WELCOME_MESSAGE "*** %s v%s %i-bits, by %s ***\n", PROGRAM_DESCRIPTION, LZ4_VERSION_STRING, (int)(sizeof(void*)*8), AUTHOR
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
#define NBLOOPS 6
|
2016-09-03 03:17:19 +00:00
|
|
|
#define TIMELOOP (CLOCKS_PER_SEC * 25 / 10)
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2015-03-15 00:42:27 +00:00
|
|
|
#define KB *(1 <<10)
|
|
|
|
#define MB *(1 <<20)
|
|
|
|
#define GB *(1U<<30)
|
|
|
|
|
2013-12-30 17:16:52 +00:00
|
|
|
#define KNUTH 2654435761U
|
2015-05-03 16:57:46 +00:00
|
|
|
#define MAX_MEM (1920 MB)
|
2015-03-15 00:42:27 +00:00
|
|
|
#define DEFAULT_CHUNKSIZE (4 MB)
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-04-28 20:45:35 +00:00
|
|
|
#define ALL_COMPRESSORS 0
|
|
|
|
#define ALL_DECOMPRESSORS 0
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
|
2015-03-01 23:11:34 +00:00
|
|
|
/**************************************
|
|
|
|
* Local structures
|
|
|
|
**************************************/
|
2013-12-30 17:16:52 +00:00
|
|
|
struct chunkParameters
|
|
|
|
{
|
|
|
|
U32 id;
|
|
|
|
char* origBuffer;
|
|
|
|
char* compressedBuffer;
|
|
|
|
int origSize;
|
|
|
|
int compressedSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-01 23:11:34 +00:00
|
|
|
/**************************************
|
2015-05-03 16:57:46 +00:00
|
|
|
* Macros
|
2015-03-01 23:11:34 +00:00
|
|
|
**************************************/
|
2013-12-30 17:16:52 +00:00
|
|
|
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
|
2015-05-03 16:57:46 +00:00
|
|
|
#define PROGRESS(...) g_noPrompt ? 0 : DISPLAY(__VA_ARGS__)
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
|
2015-03-30 20:32:25 +00:00
|
|
|
/**************************************
|
|
|
|
* Benchmark Parameters
|
|
|
|
**************************************/
|
2015-05-03 16:57:46 +00:00
|
|
|
static int g_chunkSize = DEFAULT_CHUNKSIZE;
|
|
|
|
static int g_nbIterations = NBLOOPS;
|
|
|
|
static int g_pause = 0;
|
|
|
|
static int g_compressionTest = 1;
|
|
|
|
static int g_compressionAlgo = ALL_COMPRESSORS;
|
|
|
|
static int g_decompressionTest = 1;
|
|
|
|
static int g_decompressionAlgo = ALL_DECOMPRESSORS;
|
|
|
|
static int g_noPrompt = 0;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2015-05-03 16:57:46 +00:00
|
|
|
static void BMK_setBlocksize(int bsize)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2015-05-03 16:57:46 +00:00
|
|
|
g_chunkSize = bsize;
|
|
|
|
DISPLAY("-Using Block Size of %i KB-\n", g_chunkSize>>10);
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 16:57:46 +00:00
|
|
|
static void BMK_setNbIterations(int nbLoops)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2015-05-03 16:57:46 +00:00
|
|
|
g_nbIterations = nbLoops;
|
|
|
|
DISPLAY("- %i iterations -\n", g_nbIterations);
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 16:57:46 +00:00
|
|
|
static void BMK_setPause(void)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2015-05-03 16:57:46 +00:00
|
|
|
g_pause = 1;
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 16:57:46 +00:00
|
|
|
|
2015-03-30 20:32:25 +00:00
|
|
|
/*********************************************************
|
|
|
|
* Private functions
|
|
|
|
*********************************************************/
|
2016-09-03 03:17:19 +00:00
|
|
|
static clock_t BMK_GetClockSpan( clock_t clockStart )
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2016-09-03 03:17:19 +00:00
|
|
|
return clock() - clockStart; /* works even if overflow; max span ~30 mn */
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static size_t BMK_findMaxMem(U64 requiredMem)
|
|
|
|
{
|
2015-03-15 00:42:27 +00:00
|
|
|
size_t step = 64 MB;
|
2019-12-04 16:49:06 +00:00
|
|
|
BYTE* testmem = NULL;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2015-03-15 00:42:27 +00:00
|
|
|
requiredMem = (((requiredMem >> 26) + 1) << 26);
|
|
|
|
requiredMem += 2*step;
|
2013-12-30 17:16:52 +00:00
|
|
|
if (requiredMem > MAX_MEM) requiredMem = MAX_MEM;
|
|
|
|
|
2016-09-03 03:17:19 +00:00
|
|
|
while (!testmem) {
|
2015-03-15 00:42:27 +00:00
|
|
|
if (requiredMem > step) requiredMem -= step;
|
|
|
|
else requiredMem >>= 1;
|
2013-12-30 17:16:52 +00:00
|
|
|
testmem = (BYTE*) malloc ((size_t)requiredMem);
|
|
|
|
}
|
|
|
|
free (testmem);
|
2015-03-15 00:42:27 +00:00
|
|
|
|
|
|
|
/* keep some space available */
|
|
|
|
if (requiredMem > step) requiredMem -= step;
|
|
|
|
else requiredMem >>= 1;
|
|
|
|
|
|
|
|
return (size_t)requiredMem;
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-09 05:17:32 +00:00
|
|
|
/*********************************************************
|
|
|
|
* Memory management, to test LZ4_USER_MEMORY_FUNCTIONS
|
|
|
|
*********************************************************/
|
|
|
|
void* LZ4_malloc(size_t s) { return malloc(s); }
|
2020-11-09 18:58:31 +00:00
|
|
|
void* LZ4_calloc(size_t n, size_t s) { return calloc(n,s); }
|
2020-11-09 08:29:52 +00:00
|
|
|
void LZ4_free(void* p) { free(p); }
|
2020-11-09 05:17:32 +00:00
|
|
|
|
|
|
|
|
2014-04-28 20:45:35 +00:00
|
|
|
/*********************************************************
|
2015-03-01 23:11:34 +00:00
|
|
|
* Benchmark function
|
2014-04-28 20:45:35 +00:00
|
|
|
*********************************************************/
|
2015-04-11 17:59:22 +00:00
|
|
|
static LZ4_stream_t LZ4_stream;
|
|
|
|
static void local_LZ4_resetDictT(void)
|
|
|
|
{
|
2019-04-08 21:10:59 +00:00
|
|
|
void* const r = LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream));
|
2019-04-18 23:06:02 +00:00
|
|
|
assert(r != NULL); (void)r;
|
2015-04-11 17:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void local_LZ4_createStream(void)
|
|
|
|
{
|
2019-04-08 21:10:59 +00:00
|
|
|
void* const r = LZ4_initStream(&LZ4_stream, sizeof(LZ4_stream));
|
2019-04-18 23:06:02 +00:00
|
|
|
assert(r != NULL); (void)r;
|
2015-04-11 17:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int local_LZ4_saveDict(const char* in, char* out, int inSize)
|
|
|
|
{
|
|
|
|
(void)in;
|
|
|
|
return LZ4_saveDict(&LZ4_stream, out, inSize);
|
|
|
|
}
|
|
|
|
|
2015-05-03 16:57:46 +00:00
|
|
|
static int local_LZ4_compress_default_large(const char* in, char* out, int inSize)
|
|
|
|
{
|
|
|
|
return LZ4_compress_default(in, out, inSize, LZ4_compressBound(inSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int local_LZ4_compress_default_small(const char* in, char* out, int inSize)
|
|
|
|
{
|
|
|
|
return LZ4_compress_default(in, out, inSize, LZ4_compressBound(inSize)-1);
|
|
|
|
}
|
|
|
|
|
2018-04-26 21:25:12 +00:00
|
|
|
static int local_LZ4_compress_destSize(const char* in, char* out, int inSize)
|
|
|
|
{
|
|
|
|
return LZ4_compress_destSize(in, out, &inSize, LZ4_compressBound(inSize)-1);
|
|
|
|
}
|
|
|
|
|
2015-05-03 16:57:46 +00:00
|
|
|
static int local_LZ4_compress_fast0(const char* in, char* out, int inSize)
|
2015-03-30 20:32:25 +00:00
|
|
|
{
|
|
|
|
return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 0);
|
|
|
|
}
|
|
|
|
|
2015-05-03 16:57:46 +00:00
|
|
|
static int local_LZ4_compress_fast1(const char* in, char* out, int inSize)
|
|
|
|
{
|
|
|
|
return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int local_LZ4_compress_fast2(const char* in, char* out, int inSize)
|
|
|
|
{
|
|
|
|
return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int local_LZ4_compress_fast17(const char* in, char* out, int inSize)
|
|
|
|
{
|
|
|
|
return LZ4_compress_fast(in, out, inSize, LZ4_compressBound(inSize), 17);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int local_LZ4_compress_fast_extState0(const char* in, char* out, int inSize)
|
|
|
|
{
|
|
|
|
return LZ4_compress_fast_extState(&LZ4_stream, in, out, inSize, LZ4_compressBound(inSize), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int local_LZ4_compress_fast_continue0(const char* in, char* out, int inSize)
|
|
|
|
{
|
|
|
|
return LZ4_compress_fast_continue(&LZ4_stream, in, out, inSize, LZ4_compressBound(inSize), 0);
|
|
|
|
}
|
|
|
|
|
2016-11-09 12:19:08 +00:00
|
|
|
#ifndef LZ4_DLL_IMPORT
|
2018-05-22 01:36:49 +00:00
|
|
|
#if defined (__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-04-11 17:59:22 +00:00
|
|
|
/* declare hidden function */
|
2018-05-22 01:36:49 +00:00
|
|
|
extern int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize);
|
|
|
|
|
|
|
|
#if defined (__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
2014-05-19 23:40:29 +00:00
|
|
|
|
2015-04-11 17:59:22 +00:00
|
|
|
static int local_LZ4_compress_forceDict(const char* in, char* out, int inSize)
|
2014-05-19 23:40:29 +00:00
|
|
|
{
|
2015-04-11 17:59:22 +00:00
|
|
|
return LZ4_compress_forceExtDict(&LZ4_stream, in, out, inSize);
|
2014-05-19 23:40:29 +00:00
|
|
|
}
|
2016-11-09 10:45:55 +00:00
|
|
|
#endif
|
2014-05-19 23:40:29 +00:00
|
|
|
|
2015-04-11 17:59:22 +00:00
|
|
|
|
|
|
|
/* HC compression functions */
|
|
|
|
LZ4_streamHC_t LZ4_streamHC;
|
|
|
|
static void local_LZ4_resetStreamHC(void)
|
2014-05-21 18:46:36 +00:00
|
|
|
{
|
2019-04-09 20:55:42 +00:00
|
|
|
LZ4_initStreamHC(&LZ4_streamHC, sizeof(LZ4_streamHC));
|
2014-05-21 18:46:36 +00:00
|
|
|
}
|
|
|
|
|
2015-04-11 17:59:22 +00:00
|
|
|
static int local_LZ4_saveDictHC(const char* in, char* out, int inSize)
|
|
|
|
{
|
|
|
|
(void)in;
|
|
|
|
return LZ4_saveDictHC(&LZ4_streamHC, out, inSize);
|
|
|
|
}
|
2014-05-19 23:40:29 +00:00
|
|
|
|
2016-11-12 16:48:42 +00:00
|
|
|
static int local_LZ4_compress_HC(const char* in, char* out, int inSize)
|
|
|
|
{
|
|
|
|
return LZ4_compress_HC(in, out, inSize, LZ4_compressBound(inSize), 9);
|
|
|
|
}
|
|
|
|
|
2016-11-17 21:02:06 +00:00
|
|
|
static int local_LZ4_compress_HC_extStateHC(const char* in, char* out, int inSize)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2016-11-12 16:48:42 +00:00
|
|
|
return LZ4_compress_HC_extStateHC(&LZ4_streamHC, in, out, inSize, LZ4_compressBound(inSize), 9);
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 21:02:06 +00:00
|
|
|
static int local_LZ4_compress_HC_continue(const char* in, char* out, int inSize)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2016-11-12 16:48:42 +00:00
|
|
|
return LZ4_compress_HC_continue(&LZ4_streamHC, in, out, inSize, LZ4_compressBound(inSize));
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2014-11-02 21:32:12 +00:00
|
|
|
|
2015-04-11 17:59:22 +00:00
|
|
|
/* decompression functions */
|
2014-05-20 00:53:48 +00:00
|
|
|
static int local_LZ4_decompress_fast(const char* in, char* out, int inSize, int outSize)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
|
|
|
(void)inSize;
|
|
|
|
LZ4_decompress_fast(in, out, outSize);
|
|
|
|
return outSize;
|
|
|
|
}
|
|
|
|
|
2018-04-29 14:42:24 +00:00
|
|
|
static int local_LZ4_decompress_fast_usingDict_prefix(const char* in, char* out, int inSize, int outSize)
|
2014-05-03 18:56:08 +00:00
|
|
|
{
|
|
|
|
(void)inSize;
|
2014-09-21 08:56:21 +00:00
|
|
|
LZ4_decompress_fast_usingDict(in, out, outSize, out - 65536, 65536);
|
2014-05-03 18:56:08 +00:00
|
|
|
return outSize;
|
|
|
|
}
|
|
|
|
|
2018-04-29 14:42:24 +00:00
|
|
|
static int local_LZ4_decompress_fast_usingExtDict(const char* in, char* out, int inSize, int outSize)
|
|
|
|
{
|
|
|
|
(void)inSize;
|
|
|
|
LZ4_decompress_fast_usingDict(in, out, outSize, out - 65536, 65535);
|
|
|
|
return outSize;
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:49:06 +00:00
|
|
|
static int local_LZ4_decompress_safe_withPrefix64k(const char* in, char* out, int inSize, int outSize)
|
|
|
|
{
|
|
|
|
LZ4_decompress_safe_withPrefix64k(in, out, inSize, outSize);
|
|
|
|
return outSize;
|
|
|
|
}
|
|
|
|
|
2014-05-20 00:53:48 +00:00
|
|
|
static int local_LZ4_decompress_safe_usingDict(const char* in, char* out, int inSize, int outSize)
|
2014-05-03 18:56:08 +00:00
|
|
|
{
|
2014-09-21 08:56:21 +00:00
|
|
|
LZ4_decompress_safe_usingDict(in, out, inSize, outSize, out - 65536, 65536);
|
2014-05-03 18:56:08 +00:00
|
|
|
return outSize;
|
|
|
|
}
|
|
|
|
|
2016-11-09 12:19:08 +00:00
|
|
|
#ifndef LZ4_DLL_IMPORT
|
2018-05-22 01:36:49 +00:00
|
|
|
#if defined (__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
lz4.c: fixed the LZ4_decompress_safe_continue case
The previous change broke decoding with a ring buffer. That's because
I didn't realize that the "double dictionary mode" was possible, i.e.
that the decoding routine can look both at the first part of the
dictionary passed as prefix and the second part passed via dictStart+dictSize.
So this change introduces the LZ4_decompress_safe_doubleDict helper,
which handles this "double dictionary" situation. (This is a bit of
a misnomer, there is only one dictionary, but I can't think of a better
name, and perhaps the designation is not all too bad.) The helper is
used only once, in LZ4_decompress_safe_continue, it should be inlined
with LZ4_FORCE_O2_GCC_PPC64LE attached to LZ4_decompress_safe_continue.
(Also, in the helper functions, I change the dictStart parameter type
to "const void*", to avoid a cast when calling helpers. In the helpers,
the upcast to "BYTE*" is still required, for compatibility with C++.)
So this fixes the case of LZ4_decompress_safe_continue, and I'm
surprised by the fact that the fuzzer is now happy and does not detect
a similar problem with LZ4_decompress_fast_continue. So before fixing
LZ4_decompress_fast_continue, the next logical step is to enhance
the fuzzer.
2018-04-26 04:46:26 +00:00
|
|
|
extern int LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize, const void* dict, size_t dictSize);
|
2014-09-13 18:49:01 +00:00
|
|
|
|
2018-05-22 01:36:49 +00:00
|
|
|
#if defined (__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-09-13 18:49:01 +00:00
|
|
|
static int local_LZ4_decompress_safe_forceExtDict(const char* in, char* out, int inSize, int outSize)
|
|
|
|
{
|
|
|
|
(void)inSize;
|
2014-09-21 08:56:21 +00:00
|
|
|
LZ4_decompress_safe_forceExtDict(in, out, inSize, outSize, out - 65536, 65536);
|
2014-09-13 18:49:01 +00:00
|
|
|
return outSize;
|
|
|
|
}
|
2016-11-09 10:45:55 +00:00
|
|
|
#endif
|
2014-09-13 18:49:01 +00:00
|
|
|
|
2014-05-20 00:53:48 +00:00
|
|
|
static int local_LZ4_decompress_safe_partial(const char* in, char* out, int inSize, int outSize)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2018-09-07 22:44:19 +00:00
|
|
|
int result = LZ4_decompress_safe_partial(in, out, inSize, outSize - 5, outSize);
|
|
|
|
if (result < 0) return result;
|
|
|
|
return outSize;
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-11 17:59:22 +00:00
|
|
|
|
|
|
|
/* frame functions */
|
|
|
|
static int local_LZ4F_compressFrame(const char* in, char* out, int inSize)
|
|
|
|
{
|
2019-04-05 00:03:05 +00:00
|
|
|
assert(inSize >= 0);
|
|
|
|
return (int)LZ4F_compressFrame(out, LZ4F_compressFrameBound((size_t)inSize, NULL), in, (size_t)inSize, NULL);
|
2015-04-11 17:59:22 +00:00
|
|
|
}
|
|
|
|
|
2014-09-13 14:24:16 +00:00
|
|
|
static LZ4F_decompressionContext_t g_dCtx;
|
|
|
|
|
|
|
|
static int local_LZ4F_decompress(const char* in, char* out, int inSize, int outSize)
|
|
|
|
{
|
2019-04-05 00:03:05 +00:00
|
|
|
size_t srcSize = (size_t)inSize;
|
|
|
|
size_t dstSize = (size_t)outSize;
|
2014-09-13 14:24:16 +00:00
|
|
|
size_t result;
|
2019-04-05 00:03:05 +00:00
|
|
|
assert(inSize >= 0);
|
|
|
|
assert(outSize >= 0);
|
2014-09-13 14:24:16 +00:00
|
|
|
result = LZ4F_decompress(g_dCtx, out, &dstSize, in, &srcSize, NULL);
|
2019-05-31 00:29:51 +00:00
|
|
|
if (result!=0) { DISPLAY("Error decompressing frame : unfinished frame \n"); exit(8); }
|
|
|
|
if (srcSize != (size_t)inSize) { DISPLAY("Error decompressing frame : read size incorrect \n"); exit(9); }
|
2014-09-22 16:38:17 +00:00
|
|
|
return (int)dstSize;
|
2014-09-13 14:24:16 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 00:29:51 +00:00
|
|
|
static int local_LZ4F_decompress_followHint(const char* src, char* dst, int srcSize, int dstSize)
|
|
|
|
{
|
|
|
|
size_t totalInSize = (size_t)srcSize;
|
|
|
|
size_t maxOutSize = (size_t)dstSize;
|
|
|
|
|
|
|
|
size_t inPos = 0;
|
|
|
|
size_t inSize = 0;
|
|
|
|
size_t outPos = 0;
|
|
|
|
size_t outRemaining = maxOutSize - outPos;
|
|
|
|
|
2019-05-31 19:01:33 +00:00
|
|
|
for (;;) {
|
2019-05-31 00:29:51 +00:00
|
|
|
size_t const sizeHint = LZ4F_decompress(g_dCtx, dst+outPos, &outRemaining, src+inPos, &inSize, NULL);
|
|
|
|
assert(!LZ4F_isError(sizeHint));
|
|
|
|
|
|
|
|
inPos += inSize;
|
|
|
|
inSize = sizeHint;
|
|
|
|
|
|
|
|
outPos += outRemaining;
|
|
|
|
outRemaining = maxOutSize - outPos;
|
|
|
|
|
|
|
|
if (!sizeHint) break;
|
2019-05-31 19:01:33 +00:00
|
|
|
}
|
2019-05-31 00:29:51 +00:00
|
|
|
|
|
|
|
/* frame completed */
|
|
|
|
if (inPos != totalInSize) {
|
|
|
|
DISPLAY("Error decompressing frame : must read (%u) full frame (%u) \n",
|
|
|
|
(unsigned)inPos, (unsigned)totalInSize);
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
return (int)outPos;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:57:12 +00:00
|
|
|
/* always provide input by block of 64 KB */
|
|
|
|
static int local_LZ4F_decompress_noHint(const char* src, char* dst, int srcSize, int dstSize)
|
|
|
|
{
|
|
|
|
size_t totalInSize = (size_t)srcSize;
|
|
|
|
size_t maxOutSize = (size_t)dstSize;
|
|
|
|
|
|
|
|
size_t inPos = 0;
|
|
|
|
size_t inSize = 64 KB;
|
|
|
|
size_t outPos = 0;
|
|
|
|
size_t outRemaining = maxOutSize - outPos;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
size_t const sizeHint = LZ4F_decompress(g_dCtx, dst+outPos, &outRemaining, src+inPos, &inSize, NULL);
|
|
|
|
assert(!LZ4F_isError(sizeHint));
|
|
|
|
|
|
|
|
inPos += inSize;
|
|
|
|
inSize = (inPos + 64 KB <= totalInSize) ? 64 KB : totalInSize - inPos;
|
|
|
|
|
|
|
|
outPos += outRemaining;
|
|
|
|
outRemaining = maxOutSize - outPos;
|
|
|
|
|
|
|
|
if (!sizeHint) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* frame completed */
|
|
|
|
if (inPos != totalInSize) {
|
|
|
|
DISPLAY("Error decompressing frame : must read (%u) full frame (%u) \n",
|
|
|
|
(unsigned)inPos, (unsigned)totalInSize);
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
return (int)outPos;
|
|
|
|
|
|
|
|
}
|
2014-09-13 14:24:16 +00:00
|
|
|
|
2015-04-12 07:21:35 +00:00
|
|
|
#define NB_COMPRESSION_ALGORITHMS 100
|
|
|
|
#define NB_DECOMPRESSION_ALGORITHMS 100
|
2016-09-03 03:17:19 +00:00
|
|
|
int fullSpeedBench(const char** fileNamesTable, int nbFiles)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
2016-09-03 03:17:19 +00:00
|
|
|
int fileIdx=0;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2016-09-03 03:17:19 +00:00
|
|
|
/* Init */
|
|
|
|
{ size_t const errorCode = LZ4F_createDecompressionContext(&g_dCtx, LZ4F_VERSION);
|
|
|
|
if (LZ4F_isError(errorCode)) { DISPLAY("dctx allocation issue \n"); return 10; } }
|
2015-04-12 08:29:52 +00:00
|
|
|
|
2016-09-03 03:17:19 +00:00
|
|
|
/* Loop for each fileName */
|
|
|
|
while (fileIdx<nbFiles) {
|
2015-04-12 15:40:58 +00:00
|
|
|
char* orig_buff = NULL;
|
|
|
|
struct chunkParameters* chunkP = NULL;
|
|
|
|
char* compressed_buff=NULL;
|
2016-09-03 03:17:19 +00:00
|
|
|
const char* const inFileName = fileNamesTable[fileIdx++];
|
|
|
|
FILE* const inFile = fopen( inFileName, "rb" );
|
2020-09-29 05:37:20 +00:00
|
|
|
U64 const inFileSize = UTIL_getFileSize(inFileName);
|
|
|
|
size_t benchedSize = BMK_findMaxMem(inFileSize*2) / 2; /* because 2 buffers */
|
2013-12-30 17:16:52 +00:00
|
|
|
int nbChunks;
|
|
|
|
int maxCompressedChunkSize;
|
|
|
|
size_t readSize;
|
2015-04-12 08:29:52 +00:00
|
|
|
int compressedBuffSize;
|
2013-12-30 17:16:52 +00:00
|
|
|
U32 crcOriginal;
|
|
|
|
|
2020-09-29 05:37:20 +00:00
|
|
|
/* Check infile pre-requisites */
|
|
|
|
if (inFile==NULL) { DISPLAY("Pb opening %s \n", inFileName); return 11; }
|
|
|
|
if (inFileSize==0) { DISPLAY("file is empty \n"); fclose(inFile); return 11; }
|
|
|
|
if (benchedSize==0) { DISPLAY("not enough memory \n"); fclose(inFile); return 11; }
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2015-03-30 20:32:25 +00:00
|
|
|
/* Memory size adjustments */
|
2013-12-30 17:16:52 +00:00
|
|
|
if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
|
2020-09-29 05:37:20 +00:00
|
|
|
if (benchedSize < inFileSize) {
|
|
|
|
DISPLAY("Not enough memory for '%s' full size; testing %i MB only... \n",
|
|
|
|
inFileName, (int)(benchedSize>>20));
|
|
|
|
}
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2015-03-30 20:32:25 +00:00
|
|
|
/* Allocation */
|
2015-05-03 16:57:46 +00:00
|
|
|
chunkP = (struct chunkParameters*) malloc(((benchedSize / (size_t)g_chunkSize)+1) * sizeof(struct chunkParameters));
|
2015-04-12 15:40:58 +00:00
|
|
|
orig_buff = (char*) malloc(benchedSize);
|
2019-04-13 00:03:28 +00:00
|
|
|
nbChunks = (int) ((benchedSize + (size_t)g_chunkSize - 1) / (size_t)g_chunkSize);
|
2015-05-03 16:57:46 +00:00
|
|
|
maxCompressedChunkSize = LZ4_compressBound(g_chunkSize);
|
2013-12-30 17:16:52 +00:00
|
|
|
compressedBuffSize = nbChunks * maxCompressedChunkSize;
|
|
|
|
compressed_buff = (char*)malloc((size_t)compressedBuffSize);
|
2016-09-03 03:17:19 +00:00
|
|
|
if(!chunkP || !orig_buff || !compressed_buff) {
|
2020-09-29 05:37:20 +00:00
|
|
|
DISPLAY("\nError: not enough memory! \n");
|
2015-04-12 15:40:58 +00:00
|
|
|
fclose(inFile);
|
|
|
|
free(orig_buff);
|
|
|
|
free(compressed_buff);
|
|
|
|
free(chunkP);
|
|
|
|
return(12);
|
|
|
|
}
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2015-03-30 20:32:25 +00:00
|
|
|
/* Fill in src buffer */
|
2013-12-30 17:16:52 +00:00
|
|
|
DISPLAY("Loading %s... \r", inFileName);
|
|
|
|
readSize = fread(orig_buff, 1, benchedSize, inFile);
|
|
|
|
fclose(inFile);
|
|
|
|
|
2016-09-03 03:17:19 +00:00
|
|
|
if (readSize != benchedSize) {
|
2013-12-30 17:16:52 +00:00
|
|
|
DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
|
|
|
|
free(orig_buff);
|
|
|
|
free(compressed_buff);
|
|
|
|
free(chunkP);
|
|
|
|
return 13;
|
|
|
|
}
|
|
|
|
|
2015-04-01 17:21:03 +00:00
|
|
|
/* Calculating input Checksum */
|
2015-04-12 15:40:58 +00:00
|
|
|
crcOriginal = XXH32(orig_buff, benchedSize,0);
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
|
2015-04-01 17:21:03 +00:00
|
|
|
/* Bench */
|
2016-09-03 03:17:19 +00:00
|
|
|
{ int loopNb, nb_loops, chunkNb, cAlgNb, dAlgNb;
|
2013-12-30 17:16:52 +00:00
|
|
|
size_t cSize=0;
|
|
|
|
double ratio=0.;
|
|
|
|
|
|
|
|
DISPLAY("\r%79s\r", "");
|
|
|
|
DISPLAY(" %s : \n", inFileName);
|
|
|
|
|
2015-04-06 00:02:24 +00:00
|
|
|
/* Bench Compression Algorithms */
|
2016-09-03 03:17:19 +00:00
|
|
|
for (cAlgNb=0; (cAlgNb <= NB_COMPRESSION_ALGORITHMS) && (g_compressionTest); cAlgNb++) {
|
2014-09-13 14:24:16 +00:00
|
|
|
const char* compressorName;
|
2013-12-30 17:16:52 +00:00
|
|
|
int (*compressionFunction)(const char*, char*, int);
|
2015-04-11 17:59:22 +00:00
|
|
|
void (*initFunction)(void) = NULL;
|
2013-12-30 17:16:52 +00:00
|
|
|
double bestTime = 100000000.;
|
|
|
|
|
2015-04-11 17:59:22 +00:00
|
|
|
/* filter compressionAlgo only */
|
2015-05-03 16:57:46 +00:00
|
|
|
if ((g_compressionAlgo != ALL_COMPRESSORS) && (g_compressionAlgo != cAlgNb)) continue;
|
2015-04-11 17:59:22 +00:00
|
|
|
|
2015-04-01 17:21:03 +00:00
|
|
|
/* Init data chunks */
|
2016-09-03 03:17:19 +00:00
|
|
|
{ int i;
|
|
|
|
size_t remaining = benchedSize;
|
|
|
|
char* in = orig_buff;
|
|
|
|
char* out = compressed_buff;
|
2020-09-29 05:37:20 +00:00
|
|
|
assert(nbChunks >= 1);
|
2016-09-03 03:17:19 +00:00
|
|
|
for (i=0; i<nbChunks; i++) {
|
2019-04-05 00:03:05 +00:00
|
|
|
chunkP[i].id = (U32)i;
|
2016-09-03 03:17:19 +00:00
|
|
|
chunkP[i].origBuffer = in; in += g_chunkSize;
|
2019-05-31 00:29:51 +00:00
|
|
|
assert(g_chunkSize > 0);
|
|
|
|
if (remaining > (size_t)g_chunkSize) {
|
|
|
|
chunkP[i].origSize = g_chunkSize;
|
|
|
|
remaining -= (size_t)g_chunkSize;
|
|
|
|
} else {
|
|
|
|
chunkP[i].origSize = (int)remaining;
|
|
|
|
remaining = 0;
|
|
|
|
}
|
2016-09-03 03:17:19 +00:00
|
|
|
chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize;
|
|
|
|
chunkP[i].compressedSize = 0;
|
|
|
|
}
|
2014-11-02 21:32:12 +00:00
|
|
|
}
|
|
|
|
|
2013-12-30 17:16:52 +00:00
|
|
|
switch(cAlgNb)
|
|
|
|
{
|
2015-05-03 16:57:46 +00:00
|
|
|
case 0 : DISPLAY("Compression functions : \n"); continue;
|
|
|
|
case 1 : compressionFunction = local_LZ4_compress_default_large; compressorName = "LZ4_compress_default"; break;
|
|
|
|
case 2 : compressionFunction = local_LZ4_compress_default_small; compressorName = "LZ4_compress_default(small dst)"; break;
|
2018-04-26 21:25:12 +00:00
|
|
|
case 3 : compressionFunction = local_LZ4_compress_destSize; compressorName = "LZ4_compress_destSize"; break;
|
|
|
|
case 4 : compressionFunction = local_LZ4_compress_fast0; compressorName = "LZ4_compress_fast(0)"; break;
|
|
|
|
case 5 : compressionFunction = local_LZ4_compress_fast1; compressorName = "LZ4_compress_fast(1)"; break;
|
|
|
|
case 6 : compressionFunction = local_LZ4_compress_fast2; compressorName = "LZ4_compress_fast(2)"; break;
|
|
|
|
case 7 : compressionFunction = local_LZ4_compress_fast17; compressorName = "LZ4_compress_fast(17)"; break;
|
|
|
|
case 8 : compressionFunction = local_LZ4_compress_fast_extState0; compressorName = "LZ4_compress_fast_extState(0)"; break;
|
|
|
|
case 9 : compressionFunction = local_LZ4_compress_fast_continue0; initFunction = local_LZ4_createStream; compressorName = "LZ4_compress_fast_continue(0)"; break;
|
2015-05-03 16:57:46 +00:00
|
|
|
|
2016-11-12 16:48:42 +00:00
|
|
|
case 10: compressionFunction = local_LZ4_compress_HC; compressorName = "LZ4_compress_HC"; break;
|
2016-11-17 21:02:06 +00:00
|
|
|
case 12: compressionFunction = local_LZ4_compress_HC_extStateHC; compressorName = "LZ4_compress_HC_extStateHC"; break;
|
|
|
|
case 14: compressionFunction = local_LZ4_compress_HC_continue; initFunction = local_LZ4_resetStreamHC; compressorName = "LZ4_compress_HC_continue"; break;
|
2016-11-09 12:19:08 +00:00
|
|
|
#ifndef LZ4_DLL_IMPORT
|
2018-09-07 22:44:19 +00:00
|
|
|
case 20: compressionFunction = local_LZ4_compress_forceDict; initFunction = local_LZ4_resetDictT; compressorName = "LZ4_compress_forceDict"; break;
|
2016-11-09 10:45:55 +00:00
|
|
|
#endif
|
2018-09-07 22:44:19 +00:00
|
|
|
case 30: compressionFunction = local_LZ4F_compressFrame; compressorName = "LZ4F_compressFrame";
|
2014-09-22 16:38:17 +00:00
|
|
|
chunkP[0].origSize = (int)benchedSize; nbChunks=1;
|
2014-09-12 18:28:44 +00:00
|
|
|
break;
|
2015-05-03 16:57:46 +00:00
|
|
|
case 40: compressionFunction = local_LZ4_saveDict; compressorName = "LZ4_saveDict";
|
2016-11-10 23:31:59 +00:00
|
|
|
if (chunkP[0].origSize < 8) { DISPLAY(" cannot bench %s with less then 8 bytes \n", compressorName); continue; }
|
2015-04-11 17:59:22 +00:00
|
|
|
LZ4_loadDict(&LZ4_stream, chunkP[0].origBuffer, chunkP[0].origSize);
|
2014-11-02 21:32:12 +00:00
|
|
|
break;
|
2015-05-03 16:57:46 +00:00
|
|
|
case 41: compressionFunction = local_LZ4_saveDictHC; compressorName = "LZ4_saveDictHC";
|
2016-11-10 23:31:59 +00:00
|
|
|
if (chunkP[0].origSize < 8) { DISPLAY(" cannot bench %s with less then 8 bytes \n", compressorName); continue; }
|
2015-04-11 17:59:22 +00:00
|
|
|
LZ4_loadDictHC(&LZ4_streamHC, chunkP[0].origBuffer, chunkP[0].origSize);
|
2014-11-02 21:32:12 +00:00
|
|
|
break;
|
2015-04-11 17:59:22 +00:00
|
|
|
default :
|
|
|
|
continue; /* unknown ID : just skip */
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2016-09-03 03:17:19 +00:00
|
|
|
for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
|
2013-12-30 17:16:52 +00:00
|
|
|
double averageTime;
|
2016-09-03 03:17:19 +00:00
|
|
|
clock_t clockTime;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2018-04-29 14:42:24 +00:00
|
|
|
PROGRESS("%2i-%-34.34s :%10i ->\r", loopNb, compressorName, (int)benchedSize);
|
2015-04-01 17:21:03 +00:00
|
|
|
{ size_t i; for (i=0; i<benchedSize; i++) compressed_buff[i]=(char)i; } /* warming up memory */
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
nb_loops = 0;
|
2016-09-03 03:17:19 +00:00
|
|
|
clockTime = clock();
|
|
|
|
while(clock() == clockTime);
|
|
|
|
clockTime = clock();
|
|
|
|
while(BMK_GetClockSpan(clockTime) < TIMELOOP) {
|
2015-04-11 17:59:22 +00:00
|
|
|
if (initFunction!=NULL) initFunction();
|
2016-09-03 03:17:19 +00:00
|
|
|
for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
|
2013-12-30 17:16:52 +00:00
|
|
|
chunkP[chunkNb].compressedSize = compressionFunction(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize);
|
2019-06-29 03:55:47 +00:00
|
|
|
if (chunkP[chunkNb].compressedSize==0) {
|
|
|
|
DISPLAY("ERROR ! %s() = 0 !! \n", compressorName);
|
|
|
|
exit(1);
|
|
|
|
} }
|
2013-12-30 17:16:52 +00:00
|
|
|
nb_loops++;
|
|
|
|
}
|
2016-09-03 03:17:19 +00:00
|
|
|
clockTime = BMK_GetClockSpan(clockTime);
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2015-04-12 08:42:58 +00:00
|
|
|
nb_loops += !nb_loops; /* avoid division by zero */
|
2016-09-03 03:17:19 +00:00
|
|
|
averageTime = ((double)clockTime) / nb_loops / CLOCKS_PER_SEC;
|
2013-12-30 17:16:52 +00:00
|
|
|
if (averageTime < bestTime) bestTime = averageTime;
|
2019-06-29 03:55:47 +00:00
|
|
|
cSize=0; for (chunkNb=0; chunkNb<nbChunks; chunkNb++) cSize += (size_t)chunkP[chunkNb].compressedSize;
|
2013-12-30 17:16:52 +00:00
|
|
|
ratio = (double)cSize/(double)benchedSize*100.;
|
2018-04-29 14:42:24 +00:00
|
|
|
PROGRESS("%2i-%-34.34s :%10i ->%9i (%5.2f%%),%7.1f MB/s\r", loopNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000000);
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ratio<100.)
|
2018-04-29 14:42:24 +00:00
|
|
|
DISPLAY("%2i-%-34.34s :%10i ->%9i (%5.2f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 1000000);
|
2013-12-30 17:16:52 +00:00
|
|
|
else
|
2018-04-29 14:42:24 +00:00
|
|
|
DISPLAY("%2i-%-34.34s :%10i ->%9i (%5.1f%%),%7.1f MB/s\n", cAlgNb, compressorName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / bestTime / 100000);
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2015-03-30 20:32:25 +00:00
|
|
|
/* Prepare layout for decompression */
|
2015-04-01 17:21:03 +00:00
|
|
|
/* Init data chunks */
|
2016-09-03 03:17:19 +00:00
|
|
|
{ int i;
|
2014-11-27 21:44:36 +00:00
|
|
|
size_t remaining = benchedSize;
|
|
|
|
char* in = orig_buff;
|
|
|
|
char* out = compressed_buff;
|
2015-04-11 17:59:22 +00:00
|
|
|
|
2015-05-03 16:57:46 +00:00
|
|
|
nbChunks = (int) (((int)benchedSize + (g_chunkSize-1))/ g_chunkSize);
|
2016-09-03 03:17:19 +00:00
|
|
|
for (i=0; i<nbChunks; i++) {
|
2019-05-31 20:25:12 +00:00
|
|
|
chunkP[i].id = (U32)i;
|
2015-05-03 16:57:46 +00:00
|
|
|
chunkP[i].origBuffer = in; in += g_chunkSize;
|
2019-05-31 20:25:12 +00:00
|
|
|
if ((int)remaining > g_chunkSize) {
|
|
|
|
chunkP[i].origSize = g_chunkSize;
|
|
|
|
remaining -= (size_t)g_chunkSize;
|
|
|
|
} else {
|
|
|
|
chunkP[i].origSize = (int)remaining;
|
|
|
|
remaining = 0;
|
|
|
|
}
|
2014-11-27 21:44:36 +00:00
|
|
|
chunkP[i].compressedBuffer = out; out += maxCompressedChunkSize;
|
|
|
|
chunkP[i].compressedSize = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-09-03 03:17:19 +00:00
|
|
|
for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
|
2016-11-12 16:48:42 +00:00
|
|
|
chunkP[chunkNb].compressedSize = LZ4_compress_default(chunkP[chunkNb].origBuffer, chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origSize, maxCompressedChunkSize);
|
2019-06-29 03:55:47 +00:00
|
|
|
if (chunkP[chunkNb].compressedSize==0) {
|
|
|
|
DISPLAY("ERROR ! %s() = 0 !! \n", "LZ4_compress");
|
|
|
|
exit(1);
|
|
|
|
} }
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2015-04-01 17:21:03 +00:00
|
|
|
/* Decompression Algorithms */
|
2018-04-29 14:42:24 +00:00
|
|
|
for (dAlgNb=0; (dAlgNb <= NB_DECOMPRESSION_ALGORITHMS) && g_decompressionTest; dAlgNb++) {
|
2019-05-31 20:25:12 +00:00
|
|
|
const char* dName = NULL;
|
|
|
|
int (*decompressionFunction)(const char*, char*, int, int) = NULL;
|
2013-12-30 17:16:52 +00:00
|
|
|
double bestTime = 100000000.;
|
2018-09-07 22:44:19 +00:00
|
|
|
int checkResult = 1;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2015-05-03 16:57:46 +00:00
|
|
|
if ((g_decompressionAlgo != ALL_DECOMPRESSORS) && (g_decompressionAlgo != dAlgNb)) continue;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
switch(dAlgNb)
|
|
|
|
{
|
2015-05-03 16:57:46 +00:00
|
|
|
case 0: DISPLAY("Decompression functions : \n"); continue;
|
2014-09-13 14:24:16 +00:00
|
|
|
case 1: decompressionFunction = local_LZ4_decompress_fast; dName = "LZ4_decompress_fast"; break;
|
2018-04-29 14:42:24 +00:00
|
|
|
case 2: decompressionFunction = local_LZ4_decompress_fast_usingDict_prefix; dName = "LZ4_decompress_fast_usingDict(prefix)"; break;
|
|
|
|
case 3: decompressionFunction = local_LZ4_decompress_fast_usingExtDict; dName = "LZ4_decompress_fast_using(Ext)Dict"; break;
|
2014-09-13 14:24:16 +00:00
|
|
|
case 4: decompressionFunction = LZ4_decompress_safe; dName = "LZ4_decompress_safe"; break;
|
2019-12-04 16:49:06 +00:00
|
|
|
case 5: decompressionFunction = local_LZ4_decompress_safe_withPrefix64k; dName = "LZ4_decompress_safe_withPrefix64k"; break;
|
2014-09-13 14:24:16 +00:00
|
|
|
case 6: decompressionFunction = local_LZ4_decompress_safe_usingDict; dName = "LZ4_decompress_safe_usingDict"; break;
|
2018-09-07 22:44:19 +00:00
|
|
|
case 7: decompressionFunction = local_LZ4_decompress_safe_partial; dName = "LZ4_decompress_safe_partial"; checkResult = 0; break;
|
2016-11-09 12:19:08 +00:00
|
|
|
#ifndef LZ4_DLL_IMPORT
|
2018-09-07 22:44:19 +00:00
|
|
|
case 8: decompressionFunction = local_LZ4_decompress_safe_forceExtDict; dName = "LZ4_decompress_safe_forceExtDict"; break;
|
2016-11-09 10:45:55 +00:00
|
|
|
#endif
|
2019-05-31 00:29:51 +00:00
|
|
|
case 10:
|
|
|
|
case 11:
|
2019-12-04 16:57:12 +00:00
|
|
|
case 12:
|
2019-05-31 00:29:51 +00:00
|
|
|
if (dAlgNb == 10) { decompressionFunction = local_LZ4F_decompress; dName = "LZ4F_decompress"; } /* can be skipped */
|
|
|
|
if (dAlgNb == 11) { decompressionFunction = local_LZ4F_decompress_followHint; dName = "LZ4F_decompress_followHint"; } /* can be skipped */
|
2019-12-04 16:57:12 +00:00
|
|
|
if (dAlgNb == 12) { decompressionFunction = local_LZ4F_decompress_noHint; dName = "LZ4F_decompress_noHint"; } /* can be skipped */
|
2019-05-31 20:25:12 +00:00
|
|
|
/* prepare compressed data using frame format */
|
2019-05-31 00:29:51 +00:00
|
|
|
{ size_t const fcsize = LZ4F_compressFrame(compressed_buff, (size_t)compressedBuffSize, orig_buff, benchedSize, NULL);
|
|
|
|
assert(!LZ4F_isError(fcsize));
|
2014-09-22 16:38:17 +00:00
|
|
|
chunkP[0].origSize = (int)benchedSize;
|
2019-05-31 00:29:51 +00:00
|
|
|
chunkP[0].compressedSize = (int)fcsize;
|
2014-09-13 14:24:16 +00:00
|
|
|
nbChunks = 1;
|
|
|
|
break;
|
2018-09-18 23:52:02 +00:00
|
|
|
}
|
2015-04-11 17:59:22 +00:00
|
|
|
default :
|
|
|
|
continue; /* skip if unknown ID */
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 20:25:12 +00:00
|
|
|
assert(decompressionFunction != NULL);
|
|
|
|
assert(dName != NULL);
|
|
|
|
|
2015-04-06 00:02:24 +00:00
|
|
|
{ size_t i; for (i=0; i<benchedSize; i++) orig_buff[i]=0; } /* zeroing source area, for CRC checking */
|
2014-09-13 14:24:16 +00:00
|
|
|
|
2016-09-03 03:17:19 +00:00
|
|
|
for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
|
2013-12-30 17:16:52 +00:00
|
|
|
double averageTime;
|
2016-09-03 03:17:19 +00:00
|
|
|
clock_t clockTime;
|
2013-12-30 17:16:52 +00:00
|
|
|
U32 crcDecoded;
|
|
|
|
|
2018-04-29 14:42:24 +00:00
|
|
|
PROGRESS("%2i-%-34.34s :%10i ->\r", loopNb, dName, (int)benchedSize);
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
nb_loops = 0;
|
2016-09-03 03:17:19 +00:00
|
|
|
clockTime = clock();
|
|
|
|
while(clock() == clockTime);
|
|
|
|
clockTime = clock();
|
|
|
|
while(BMK_GetClockSpan(clockTime) < TIMELOOP) {
|
|
|
|
for (chunkNb=0; chunkNb<nbChunks; chunkNb++) {
|
2018-09-07 22:44:19 +00:00
|
|
|
int const decodedSize = decompressionFunction(chunkP[chunkNb].compressedBuffer, chunkP[chunkNb].origBuffer,
|
|
|
|
chunkP[chunkNb].compressedSize, chunkP[chunkNb].origSize);
|
|
|
|
if (chunkP[chunkNb].origSize != decodedSize) {
|
|
|
|
DISPLAY("ERROR ! %s() == %i != %i !! \n",
|
|
|
|
dName, decodedSize, chunkP[chunkNb].origSize);
|
|
|
|
exit(1);
|
|
|
|
} }
|
2013-12-30 17:16:52 +00:00
|
|
|
nb_loops++;
|
|
|
|
}
|
2016-09-03 03:17:19 +00:00
|
|
|
clockTime = BMK_GetClockSpan(clockTime);
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2015-04-12 07:21:35 +00:00
|
|
|
nb_loops += !nb_loops; /* Avoid division by zero */
|
2016-09-03 03:17:19 +00:00
|
|
|
averageTime = (double)clockTime / nb_loops / CLOCKS_PER_SEC;
|
2013-12-30 17:16:52 +00:00
|
|
|
if (averageTime < bestTime) bestTime = averageTime;
|
|
|
|
|
2018-04-29 14:42:24 +00:00
|
|
|
PROGRESS("%2i-%-34.34s :%10i -> %7.1f MB/s\r", loopNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000000);
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2015-03-01 23:11:34 +00:00
|
|
|
/* CRC Checking */
|
2019-04-05 00:03:05 +00:00
|
|
|
crcDecoded = XXH32(orig_buff, benchedSize, 0);
|
2018-09-07 22:44:19 +00:00
|
|
|
if (checkResult && (crcOriginal!=crcDecoded)) {
|
|
|
|
DISPLAY("\n!!! WARNING !!! %14s : Invalid Checksum : %x != %x\n",
|
|
|
|
inFileName, (unsigned)crcOriginal, (unsigned)crcDecoded);
|
|
|
|
exit(1);
|
|
|
|
} }
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2018-04-29 14:42:24 +00:00
|
|
|
DISPLAY("%2i-%-34.34s :%10i -> %7.1f MB/s\n", dAlgNb, dName, (int)benchedSize, (double)benchedSize / bestTime / 1000000);
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-12 15:40:58 +00:00
|
|
|
free(orig_buff);
|
|
|
|
free(compressed_buff);
|
|
|
|
free(chunkP);
|
2016-09-03 03:17:19 +00:00
|
|
|
}
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2016-09-03 03:17:19 +00:00
|
|
|
LZ4F_freeDecompressionContext(g_dCtx);
|
|
|
|
if (g_pause) { printf("press enter...\n"); (void)getchar(); }
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2016-09-03 03:17:19 +00:00
|
|
|
return 0;
|
2013-12-30 17:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-03 03:17:19 +00:00
|
|
|
static int usage(const char* exename)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
|
|
|
DISPLAY( "Usage :\n");
|
|
|
|
DISPLAY( " %s [arg] file1 file2 ... fileX\n", exename);
|
|
|
|
DISPLAY( "Arguments :\n");
|
|
|
|
DISPLAY( " -c : compression tests only\n");
|
|
|
|
DISPLAY( " -d : decompression tests only\n");
|
|
|
|
DISPLAY( " -H/-h : Help (this text + advanced options)\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-03 16:57:46 +00:00
|
|
|
static int usage_advanced(void)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
|
|
|
DISPLAY( "\nAdvanced options :\n");
|
2014-04-28 20:45:35 +00:00
|
|
|
DISPLAY( " -c# : test only compression function # [1-%i]\n", NB_COMPRESSION_ALGORITHMS);
|
|
|
|
DISPLAY( " -d# : test only decompression function # [1-%i]\n", NB_DECOMPRESSION_ALGORITHMS);
|
2013-12-30 17:16:52 +00:00
|
|
|
DISPLAY( " -i# : iteration loops [1-9](default : %i)\n", NBLOOPS);
|
|
|
|
DISPLAY( " -B# : Block size [4-7](default : 7)\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-03 03:17:19 +00:00
|
|
|
static int badusage(const char* exename)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
|
|
|
DISPLAY("Wrong parameters\n");
|
|
|
|
usage(exename);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-03 03:17:19 +00:00
|
|
|
int main(int argc, const char** argv)
|
2013-12-30 17:16:52 +00:00
|
|
|
{
|
|
|
|
int i,
|
|
|
|
filenamesStart=2;
|
2016-09-03 03:17:19 +00:00
|
|
|
const char* exename = argv[0];
|
|
|
|
const char* input_filename=0;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
// Welcome message
|
2014-05-20 22:26:03 +00:00
|
|
|
DISPLAY(WELCOME_MESSAGE);
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
if (argc<2) { badusage(exename); return 1; }
|
|
|
|
|
2016-09-03 03:17:19 +00:00
|
|
|
for(i=1; i<argc; i++) {
|
|
|
|
const char* argument = argv[i];
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
if(!argument) continue; // Protection if argument empty
|
2016-09-03 03:17:19 +00:00
|
|
|
if (!strcmp(argument, "--no-prompt")) {
|
2015-05-03 16:57:46 +00:00
|
|
|
g_noPrompt = 1;
|
2014-04-16 14:25:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-12-30 17:16:52 +00:00
|
|
|
|
|
|
|
// Decode command (note : aggregated commands are allowed)
|
2016-09-03 03:17:19 +00:00
|
|
|
if (argument[0]=='-') {
|
|
|
|
while (argument[1]!=0) {
|
2013-12-30 17:16:52 +00:00
|
|
|
argument ++;
|
|
|
|
|
|
|
|
switch(argument[0])
|
|
|
|
{
|
|
|
|
// Select compression algorithm only
|
|
|
|
case 'c':
|
2015-05-03 16:57:46 +00:00
|
|
|
g_decompressionTest = 0;
|
2016-09-03 03:17:19 +00:00
|
|
|
while ((argument[1]>= '0') && (argument[1]<= '9')) {
|
2015-05-03 16:57:46 +00:00
|
|
|
g_compressionAlgo *= 10;
|
|
|
|
g_compressionAlgo += argument[1] - '0';
|
2014-04-28 20:45:35 +00:00
|
|
|
argument++;
|
|
|
|
}
|
2013-12-30 17:16:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Select decompression algorithm only
|
|
|
|
case 'd':
|
2015-05-03 16:57:46 +00:00
|
|
|
g_compressionTest = 0;
|
2016-09-03 03:17:19 +00:00
|
|
|
while ((argument[1]>= '0') && (argument[1]<= '9')) {
|
2015-05-03 16:57:46 +00:00
|
|
|
g_decompressionAlgo *= 10;
|
|
|
|
g_decompressionAlgo += argument[1] - '0';
|
2014-04-28 20:45:35 +00:00
|
|
|
argument++;
|
|
|
|
}
|
2013-12-30 17:16:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Display help on usage
|
|
|
|
case 'h' :
|
|
|
|
case 'H': usage(exename); usage_advanced(); return 0;
|
|
|
|
|
|
|
|
// Modify Block Properties
|
|
|
|
case 'B':
|
|
|
|
while (argument[1]!=0)
|
|
|
|
switch(argument[1])
|
|
|
|
{
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
2016-09-03 03:17:19 +00:00
|
|
|
{ int B = argument[1] - '0';
|
2013-12-30 17:16:52 +00:00
|
|
|
int S = 1 << (8 + 2*B);
|
2015-05-03 16:57:46 +00:00
|
|
|
BMK_setBlocksize(S);
|
2013-12-30 17:16:52 +00:00
|
|
|
argument++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'D': argument++; break;
|
|
|
|
default : goto _exit_blockProperties;
|
|
|
|
}
|
|
|
|
_exit_blockProperties:
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Modify Nb Iterations
|
|
|
|
case 'i':
|
2016-09-03 03:17:19 +00:00
|
|
|
if ((argument[1] >='0') && (argument[1] <='9')) {
|
2013-12-30 17:16:52 +00:00
|
|
|
int iters = argument[1] - '0';
|
2015-05-03 16:57:46 +00:00
|
|
|
BMK_setNbIterations(iters);
|
2013-12-30 17:16:52 +00:00
|
|
|
argument++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Pause at the end (hidden option)
|
2015-05-03 16:57:46 +00:00
|
|
|
case 'p': BMK_setPause(); break;
|
2013-12-30 17:16:52 +00:00
|
|
|
|
2014-11-02 21:32:12 +00:00
|
|
|
// Unknown command
|
2013-12-30 17:16:52 +00:00
|
|
|
default : badusage(exename); return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// first provided filename is input
|
|
|
|
if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// No input filename ==> Error
|
|
|
|
if(!input_filename) { badusage(exename); return 1; }
|
|
|
|
|
|
|
|
return fullSpeedBench(argv+filenamesStart, argc-filenamesStart);
|
|
|
|
|
|
|
|
}
|