summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorConrad Meyer <cem@FreeBSD.org>2018-10-22 20:00:30 +0000
committerConrad Meyer <cem@FreeBSD.org>2018-10-22 20:00:30 +0000
commit706cfae467a217cc786fd96a72cc2e33c61987e4 (patch)
treee7673904660df47b5abd9a1c33cf982a514dac66 /tests
parent42239e68a5cfba3b37b054425eace8d14e0844e3 (diff)
Notes
Diffstat (limited to 'tests')
-rw-r--r--tests/.gitignore2
-rw-r--r--tests/Makefile57
-rw-r--r--tests/README.md53
-rw-r--r--tests/decodecorpus.c14
-rw-r--r--tests/fullbench.c477
-rw-r--r--tests/fuzz/fuzz.h6
-rwxr-xr-xtests/fuzz/fuzz.py43
-rw-r--r--tests/fuzz/regression_driver.c2
-rw-r--r--tests/fuzz/zstd_helpers.c4
-rw-r--r--tests/fuzzer.c294
-rw-r--r--tests/gzip/Makefile2
-rw-r--r--tests/legacy.c5
-rwxr-xr-xtests/libzstd_partial_builds.sh36
-rw-r--r--tests/longmatch.c16
-rw-r--r--tests/paramgrill.c2957
-rwxr-xr-xtests/playTests.sh229
-rw-r--r--tests/poolTests.c193
-rwxr-xr-xtests/rateLimiter.py40
-rw-r--r--tests/roundTripCrash.c2
-rw-r--r--tests/symbols.c2
-rwxr-xr-xtests/test-zstd-versions.py2
-rw-r--r--tests/zstreamtest.c368
22 files changed, 3808 insertions, 996 deletions
diff --git a/tests/.gitignore b/tests/.gitignore
index 4911b2d62a568..1f08c3995e858 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -1,6 +1,7 @@
# local binary (Makefile)
fullbench
fullbench32
+fullbench-lib
fuzzer
fuzzer32
fuzzer-dll
@@ -26,6 +27,7 @@ invalidDictionaries
checkTag
zcat
zstdcat
+tm
# Tmp test directory
zstdtest
diff --git a/tests/Makefile b/tests/Makefile
index 5b35ad406318d..2a96829f61c5d 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -24,15 +24,18 @@ PYTHON ?= python3
TESTARTEFACT := versionsTest
DEBUGLEVEL ?= 1
-DEBUGFLAGS = -g -DZSTD_DEBUG=$(DEBUGLEVEL)
+DEBUGFLAGS = -g -DDEBUGLEVEL=$(DEBUGLEVEL)
CPPFLAGS += -I$(ZSTDDIR) -I$(ZSTDDIR)/common -I$(ZSTDDIR)/compress \
-I$(ZSTDDIR)/dictBuilder -I$(ZSTDDIR)/deprecated -I$(PRGDIR)
+ifeq ($(OS),Windows_NT) # MinGW assumed
+CPPFLAGS += -D__USE_MINGW_ANSI_STDIO # compatibility with %zu formatting
+endif
CFLAGS ?= -O3
CFLAGS += -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \
-Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \
-Wstrict-prototypes -Wundef -Wformat-security \
-Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \
- -Wredundant-decls
+ -Wredundant-decls -Wmissing-prototypes
CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS)
FLAGS = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS)
@@ -78,7 +81,8 @@ DECODECORPUS_TESTTIME ?= -T30
default: fullbench
@echo $(ZSTDMT_OBJECTS)
-all: fullbench fuzzer zstreamtest paramgrill datagen decodecorpus roundTripCrash
+all: fullbench fuzzer zstreamtest paramgrill datagen decodecorpus roundTripCrash \
+ fullbench-lib
all32: fullbench32 fuzzer32 zstreamtest32
@@ -88,13 +92,8 @@ allnothread: fullbench fuzzer paramgrill datagen decodecorpus
dll: fuzzer-dll zstreamtest-dll
-zstd:
- $(MAKE) -C $(PRGDIR) $@ MOREFLAGS+="$(DEBUGFLAGS)"
-
-zstd32:
- $(MAKE) -C $(PRGDIR) $@ MOREFLAGS+="$(DEBUGFLAGS)"
-
-zstd-nolegacy:
+PHONY: zstd zstd32 zstd-nolegacy # must be phony, only external makefile knows how to build them, or if they need an update
+zstd zstd32 zstd-nolegacy:
$(MAKE) -C $(PRGDIR) $@ MOREFLAGS+="$(DEBUGFLAGS)"
gzstd:
@@ -131,13 +130,14 @@ zstdmt_d_%.o : $(ZSTDDIR)/decompress/%.c
fullbench32: CPPFLAGS += -m32
fullbench fullbench32 : CPPFLAGS += $(MULTITHREAD_CPP)
fullbench fullbench32 : LDFLAGS += $(MULTITHREAD_LD)
-fullbench fullbench32 : DEBUGFLAGS = # turn off assert() for speed measurements
+fullbench fullbench32 : DEBUGFLAGS = -DNDEBUG # turn off assert() for speed measurements
fullbench fullbench32 : $(ZSTD_FILES)
-fullbench fullbench32 : $(PRGDIR)/datagen.c fullbench.c
+fullbench fullbench32 : $(PRGDIR)/datagen.c $(PRGDIR)/bench.c fullbench.c
$(CC) $(FLAGS) $^ -o $@$(EXT)
+fullbench-lib : CPPFLAGS += -DXXH_NAMESPACE=ZSTD_
fullbench-lib : zstd-staticLib
-fullbench-lib : $(PRGDIR)/datagen.c fullbench.c
+fullbench-lib : $(PRGDIR)/datagen.c $(PRGDIR)/bench.c fullbench.c
$(CC) $(FLAGS) $(filter %.c,$^) -o $@$(EXT) $(ZSTDDIR)/libzstd.a
# note : broken : requires unavailable symbols
@@ -202,8 +202,8 @@ zstreamtest-dll : $(ZSTDDIR)/common/xxhash.c # xxh symbols not exposed from dll
zstreamtest-dll : $(ZSTREAM_LOCAL_FILES)
$(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) $(LDFLAGS) -o $@$(EXT)
-paramgrill : DEBUGFLAGS = # turn off assert() for speed measurements
-paramgrill : $(ZSTD_FILES) $(PRGDIR)/datagen.c paramgrill.c
+paramgrill : DEBUGFLAGS = # turn off assert() by default for speed measurements
+paramgrill : $(ZSTD_FILES) $(PRGDIR)/bench.c $(PRGDIR)/datagen.c paramgrill.c
$(CC) $(FLAGS) $^ -lm -o $@$(EXT)
datagen : $(PRGDIR)/datagen.c datagencli.c
@@ -245,13 +245,14 @@ checkTag: checkTag.c $(ZSTDDIR)/zstd.h
clean:
$(MAKE) -C $(ZSTDDIR) clean
+ $(MAKE) -C $(PRGDIR) clean
@$(RM) -fR $(TESTARTEFACT)
@$(RM) -f core *.o tmp* result* *.gcda dictionary *.zst \
$(PRGDIR)/zstd$(EXT) $(PRGDIR)/zstd32$(EXT) \
fullbench$(EXT) fullbench32$(EXT) \
fullbench-lib$(EXT) fullbench-dll$(EXT) \
fuzzer$(EXT) fuzzer32$(EXT) zbufftest$(EXT) zbufftest32$(EXT) \
- fuzzer-dll$(EXT) zstreamtest-dll$(EXT) zbufftest-dll$(EXT)\
+ fuzzer-dll$(EXT) zstreamtest-dll$(EXT) zbufftest-dll$(EXT) \
zstreamtest$(EXT) zstreamtest32$(EXT) \
datagen$(EXT) paramgrill$(EXT) roundTripCrash$(EXT) longmatch$(EXT) \
symbols$(EXT) invalidDictionaries$(EXT) legacy$(EXT) poolTests$(EXT) \
@@ -260,7 +261,7 @@ clean:
#----------------------------------------------------------------------------------
-#make valgrindTest is validated only for Linux, OSX, BSD, Hurd and Solaris targets
+#make valgrindTest is validated only for Linux, macOS, BSD, Hurd and Solaris targets
#----------------------------------------------------------------------------------
ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU OpenBSD FreeBSD NetBSD DragonFly SunOS))
HOST_OS = POSIX
@@ -301,11 +302,6 @@ endif
list:
@$(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | xargs
-.PHONY: zstd-playTests
-zstd-playTests: datagen
- file $(ZSTD)
- ZSTD="$(QEMU_SYS) $(ZSTD)" ./playTests.sh $(ZSTDRTTEST)
-
.PHONY: shortest
shortest: ZSTDRTTEST=
shortest: test-zstd
@@ -323,14 +319,21 @@ test32: test-zstd32 test-fullbench32 test-fuzzer32 test-zstream32
test-all: test test32 valgrindTest test-decodecorpus-cli
+
+.PHONY: test-zstd test-zstd32 test-zstd-nolegacy
test-zstd: ZSTD = $(PRGDIR)/zstd
-test-zstd: zstd zstd-playTests
+test-zstd: zstd
test-zstd32: ZSTD = $(PRGDIR)/zstd32
-test-zstd32: zstd32 zstd-playTests
+test-zstd32: zstd32
test-zstd-nolegacy: ZSTD = $(PRGDIR)/zstd-nolegacy
-test-zstd-nolegacy: zstd-nolegacy zstd-playTests
+test-zstd-nolegacy: zstd-nolegacy
+
+test-zstd test-zstd32 test-zstd-nolegacy: datagen
+ file $(ZSTD)
+ ZSTD="$(QEMU_SYS) $(ZSTD)" ./playTests.sh $(ZSTDRTTEST)
+
test-gzstd: gzstd
$(PRGDIR)/zstd -f README.md test-zstd-speed.py
@@ -360,6 +363,9 @@ test-fullbench32: fullbench32 datagen
test-fuzzer: fuzzer
$(QEMU_SYS) ./fuzzer -v $(FUZZERTEST) $(FUZZER_FLAGS)
+test-fuzzer-stackmode: MOREFLAGS += -DZSTD_HEAPMODE=0
+test-fuzzer-stackmode: test-fuzzer
+
test-fuzzer32: fuzzer32
$(QEMU_SYS) ./fuzzer32 -v $(FUZZERTEST) $(FUZZER_FLAGS)
@@ -373,7 +379,6 @@ test-zstream: zstreamtest
$(QEMU_SYS) ./zstreamtest -v $(ZSTREAM_TESTTIME) $(FUZZER_FLAGS)
$(QEMU_SYS) ./zstreamtest --mt -t1 $(ZSTREAM_TESTTIME) $(FUZZER_FLAGS)
$(QEMU_SYS) ./zstreamtest --newapi -t1 $(ZSTREAM_TESTTIME) $(FUZZER_FLAGS)
- $(QEMU_SYS) ./zstreamtest --opaqueapi -t1 $(ZSTREAM_TESTTIME) $(FUZZER_FLAGS)
test-zstream32: zstreamtest32
$(QEMU_SYS) ./zstreamtest32 $(ZSTREAM_TESTTIME) $(FUZZER_FLAGS)
diff --git a/tests/README.md b/tests/README.md
index 24a28ab7b9d03..f28766bd19423 100644
--- a/tests/README.md
+++ b/tests/README.md
@@ -88,3 +88,56 @@ as well as the 10,000 original files for more detailed comparison of decompressi
will choose a random seed, and for 1 minute,
generate random test frames and ensure that the
zstd library correctly decompresses them in both simple and streaming modes.
+
+#### `paramgrill` - tool for generating compression table parameters and optimizing parameters on file given constraints
+
+Full list of arguments
+```
+ -T# : set level 1 speed objective
+ -B# : cut input into blocks of size # (default : single block)
+ -S : benchmarks a single run (example command: -Sl3w10h12)
+ w# - windowLog
+ h# - hashLog
+ c# - chainLog
+ s# - searchLog
+ l# - searchLength
+ t# - targetLength
+ S# - strategy
+ L# - level
+ --zstd= : Single run, parameter selection syntax same as zstdcli with more parameters
+ (Added forceAttachDictionary / fadt)
+ When invoked with --optimize, this represents the sample to exceed.
+ --optimize= : find parameters to maximize compression ratio given parameters
+ Can use all --zstd= commands to constrain the type of solution found in addition to the following constraints
+ cSpeed= : Minimum compression speed
+ dSpeed= : Minimum decompression speed
+ cMem= : Maximum compression memory
+ lvl= : Searches for solutions which are strictly better than that compression lvl in ratio and cSpeed,
+ stc= : When invoked with lvl=, represents percentage slack in ratio/cSpeed allowed for a solution to be considered (Default 100%)
+ : In normal operation, represents percentage slack in choosing viable starting strategy selection in choosing the default parameters
+ (Lower value will begin with stronger strategies) (Default 90%)
+ speedRatio= (accepts decimals)
+ : determines value of gains in speed vs gains in ratio
+ when determining overall winner (default 5 (1% ratio = 5% speed)).
+ tries= : Maximum number of random restarts on a single strategy before switching (Default 5)
+ Higher values will make optimizer run longer, more chances to find better solution.
+ memLog : Limits the log of the size of each memotable (1 per strategy). Will use hash tables when state space is larger than max size.
+ Setting memLog = 0 turns off memoization
+ --display= : specifiy which parameters are included in the output
+ can use all --zstd parameter names and 'cParams' as a shorthand for all parameters used in ZSTD_compressionParameters
+ (Default: display all params available)
+ -P# : generated sample compressibility (when no file is provided)
+ -t# : Caps runtime of operation in seconds (default : 99999 seconds (about 27 hours ))
+ -v : Prints Benchmarking output
+ -D : Next argument dictionary file
+ -s : Benchmark all files separately
+ -q : Quiet, repeat for more quiet
+ -q Prints parameters + results whenever a new best is found
+ -qq Only prints parameters whenever a new best is found, prints final parameters + results
+ -qqq Only print final parameters + results
+ -qqqq Only prints final parameter set in the form --zstd=
+ -v : Verbose, cancels quiet, repeat for more volume
+ -v Prints all candidate parameters and results
+
+```
+ Any inputs afterwards are treated as files to benchmark.
diff --git a/tests/decodecorpus.c b/tests/decodecorpus.c
index 407653119dd25..2c2276004a955 100644
--- a/tests/decodecorpus.c
+++ b/tests/decodecorpus.c
@@ -437,7 +437,8 @@ static size_t writeHufHeader(U32* seed, HUF_CElt* hufTable, void* dst, size_t ds
U32 count[HUF_SYMBOLVALUE_MAX+1];
/* Scan input and build symbol stats */
- { size_t const largest = FSE_count_wksp (count, &maxSymbolValue, (const BYTE*)src, srcSize, WKSP);
+ { size_t const largest = HIST_count_wksp (count, &maxSymbolValue, (const BYTE*)src, srcSize, WKSP);
+ assert(!HIST_isError(largest));
if (largest == srcSize) { *ostart = ((const BYTE*)src)[0]; return 0; } /* single symbol, rle */
if (largest <= (srcSize >> 7)+1) return 0; /* Fast heuristic : not compressible enough */
}
@@ -619,6 +620,8 @@ static size_t writeLiteralsBlock(U32* seed, frame_t* frame, size_t contentSize)
}
static inline void initSeqStore(seqStore_t *seqStore) {
+ seqStore->maxNbSeq = MAX_NB_SEQ;
+ seqStore->maxNbLit = ZSTD_BLOCKSIZE_MAX;
seqStore->sequencesStart = SEQUENCE_BUFFER;
seqStore->litStart = SEQUENCE_LITERAL_BUFFER;
seqStore->llCode = SEQUENCE_LLCODE;
@@ -834,7 +837,8 @@ static size_t writeSequences(U32* seed, frame_t* frame, seqStore_t* seqStorePtr,
/* CTable for Literal Lengths */
{ U32 max = MaxLL;
- size_t const mostFrequent = FSE_countFast_wksp(count, &max, llCodeTable, nbSeq, WKSP);
+ size_t const mostFrequent = HIST_countFast_wksp(count, &max, llCodeTable, nbSeq, WKSP); /* cannot fail */
+ assert(!HIST_isError(mostFrequent));
if (mostFrequent == nbSeq) {
/* do RLE if we have the chance */
*op++ = llCodeTable[0];
@@ -865,7 +869,8 @@ static size_t writeSequences(U32* seed, frame_t* frame, seqStore_t* seqStorePtr,
/* CTable for Offsets */
/* see Literal Lengths for descriptions of mode choices */
{ U32 max = MaxOff;
- size_t const mostFrequent = FSE_countFast_wksp(count, &max, ofCodeTable, nbSeq, WKSP);
+ size_t const mostFrequent = HIST_countFast_wksp(count, &max, ofCodeTable, nbSeq, WKSP); /* cannot fail */
+ assert(!HIST_isError(mostFrequent));
if (mostFrequent == nbSeq) {
*op++ = ofCodeTable[0];
FSE_buildCTable_rle(CTable_OffsetBits, (BYTE)max);
@@ -892,7 +897,8 @@ static size_t writeSequences(U32* seed, frame_t* frame, seqStore_t* seqStorePtr,
/* CTable for MatchLengths */
/* see Literal Lengths for descriptions of mode choices */
{ U32 max = MaxML;
- size_t const mostFrequent = FSE_countFast_wksp(count, &max, mlCodeTable, nbSeq, WKSP);
+ size_t const mostFrequent = HIST_countFast_wksp(count, &max, mlCodeTable, nbSeq, WKSP); /* cannot fail */
+ assert(!HIST_isError(mostFrequent));
if (mostFrequent == nbSeq) {
*op++ = *mlCodeTable;
FSE_buildCTable_rle(CTable_MatchLength, (BYTE)max);
diff --git a/tests/fullbench.c b/tests/fullbench.c
index 6abdd4da00fa2..b05f1537cd706 100644
--- a/tests/fullbench.c
+++ b/tests/fullbench.c
@@ -30,6 +30,7 @@
#include "zstd.h" /* ZSTD_versionString */
#include "util.h" /* time functions */
#include "datagen.h"
+#include "bench.h" /* CustomBench*/
/*_************************************
@@ -45,9 +46,13 @@
#define KNUTH 2654435761U
#define MAX_MEM (1984 MB)
+#define DEFAULT_CLEVEL 1
+
#define COMPRESSIBILITY_DEFAULT 0.50
static const size_t g_sampleSize = 10000000;
+#define TIMELOOP_NANOSEC (1*1000000000ULL) /* 1 second */
+
/*_************************************
* Macros
@@ -93,14 +98,26 @@ static size_t BMK_findMaxMem(U64 requiredMem)
/*_*******************************************************
* Benchmark wrappers
*********************************************************/
-size_t local_ZSTD_compress(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
+
+static ZSTD_CCtx* g_zcc = NULL;
+
+static size_t
+local_ZSTD_compress(const void* src, size_t srcSize,
+ void* dst, size_t dstSize,
+ void* buff2)
{
- (void)buff2;
- return ZSTD_compress(dst, dstSize, src, srcSize, 1);
+ ZSTD_parameters p;
+ ZSTD_frameParameters f = { 1 /* contentSizeHeader*/, 0, 0 };
+ p.fParams = f;
+ p.cParams = *(ZSTD_compressionParameters*)buff2;
+ return ZSTD_compress_advanced (g_zcc, dst, dstSize, src, srcSize, NULL ,0, p);
+ //return ZSTD_compress(dst, dstSize, src, srcSize, cLevel);
}
static size_t g_cSize = 0;
-size_t local_ZSTD_decompress(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
+static size_t local_ZSTD_decompress(const void* src, size_t srcSize,
+ void* dst, size_t dstSize,
+ void* buff2)
{
(void)src; (void)srcSize;
return ZSTD_decompress(dst, dstSize, buff2, g_cSize);
@@ -110,14 +127,14 @@ static ZSTD_DCtx* g_zdc = NULL;
#ifndef ZSTD_DLL_IMPORT
extern size_t ZSTD_decodeLiteralsBlock(ZSTD_DCtx* ctx, const void* src, size_t srcSize);
-size_t local_ZSTD_decodeLiteralsBlock(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
+static size_t local_ZSTD_decodeLiteralsBlock(const void* src, size_t srcSize, void* dst, size_t dstSize, void* buff2)
{
(void)src; (void)srcSize; (void)dst; (void)dstSize;
return ZSTD_decodeLiteralsBlock((ZSTD_DCtx*)g_zdc, buff2, g_cSize);
}
extern size_t ZSTD_decodeSeqHeaders(ZSTD_DCtx* dctx, int* nbSeq, const void* src, size_t srcSize);
-size_t local_ZSTD_decodeSeqHeaders(void* dst, size_t dstSize, void* buff2, const void* src, size_t srcSize)
+static size_t local_ZSTD_decodeSeqHeaders(const void* src, size_t srcSize, void* dst, size_t dstSize, void* buff2)
{
int nbSeq;
(void)src; (void)srcSize; (void)dst; (void)dstSize;
@@ -126,12 +143,18 @@ size_t local_ZSTD_decodeSeqHeaders(void* dst, size_t dstSize, void* buff2, const
#endif
static ZSTD_CStream* g_cstream= NULL;
-size_t local_ZSTD_compressStream(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
+static size_t
+local_ZSTD_compressStream(const void* src, size_t srcSize,
+ void* dst, size_t dstCapacity,
+ void* buff2)
{
ZSTD_outBuffer buffOut;
ZSTD_inBuffer buffIn;
- (void)buff2;
- ZSTD_initCStream(g_cstream, 1);
+ ZSTD_parameters p;
+ ZSTD_frameParameters f = {1 /* contentSizeHeader*/, 0, 0};
+ p.fParams = f;
+ p.cParams = *(ZSTD_compressionParameters*)buff2;
+ ZSTD_initCStream_advanced(g_cstream, NULL, 0, p, ZSTD_CONTENTSIZE_UNKNOWN);
buffOut.dst = dst;
buffOut.size = dstCapacity;
buffOut.pos = 0;
@@ -143,12 +166,14 @@ size_t local_ZSTD_compressStream(void* dst, size_t dstCapacity, void* buff2, con
return buffOut.pos;
}
-static size_t local_ZSTD_compress_generic_end(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
+static size_t
+local_ZSTD_compress_generic_end(const void* src, size_t srcSize,
+ void* dst, size_t dstCapacity,
+ void* buff2)
{
ZSTD_outBuffer buffOut;
ZSTD_inBuffer buffIn;
(void)buff2;
- ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
buffOut.dst = dst;
buffOut.size = dstCapacity;
buffOut.pos = 0;
@@ -159,12 +184,14 @@ static size_t local_ZSTD_compress_generic_end(void* dst, size_t dstCapacity, voi
return buffOut.pos;
}
-static size_t local_ZSTD_compress_generic_continue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
+static size_t
+local_ZSTD_compress_generic_continue(const void* src, size_t srcSize,
+ void* dst, size_t dstCapacity,
+ void* buff2)
{
ZSTD_outBuffer buffOut;
ZSTD_inBuffer buffIn;
(void)buff2;
- ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
buffOut.dst = dst;
buffOut.size = dstCapacity;
buffOut.pos = 0;
@@ -176,12 +203,14 @@ static size_t local_ZSTD_compress_generic_continue(void* dst, size_t dstCapacity
return buffOut.pos;
}
-static size_t local_ZSTD_compress_generic_T2_end(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
+static size_t
+local_ZSTD_compress_generic_T2_end(const void* src, size_t srcSize,
+ void* dst, size_t dstCapacity,
+ void* buff2)
{
ZSTD_outBuffer buffOut;
ZSTD_inBuffer buffIn;
(void)buff2;
- ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_nbWorkers, 2);
buffOut.dst = dst;
buffOut.size = dstCapacity;
@@ -193,12 +222,14 @@ static size_t local_ZSTD_compress_generic_T2_end(void* dst, size_t dstCapacity,
return buffOut.pos;
}
-static size_t local_ZSTD_compress_generic_T2_continue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
+static size_t
+local_ZSTD_compress_generic_T2_continue(const void* src, size_t srcSize,
+ void* dst, size_t dstCapacity,
+ void* buff2)
{
ZSTD_outBuffer buffOut;
ZSTD_inBuffer buffIn;
(void)buff2;
- ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, 1);
ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_nbWorkers, 2);
buffOut.dst = dst;
buffOut.size = dstCapacity;
@@ -212,7 +243,10 @@ static size_t local_ZSTD_compress_generic_T2_continue(void* dst, size_t dstCapac
}
static ZSTD_DStream* g_dstream= NULL;
-static size_t local_ZSTD_decompressStream(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
+static size_t
+local_ZSTD_decompressStream(const void* src, size_t srcSize,
+ void* dst, size_t dstCapacity,
+ void* buff2)
{
ZSTD_outBuffer buffOut;
ZSTD_inBuffer buffIn;
@@ -228,34 +262,52 @@ static size_t local_ZSTD_decompressStream(void* dst, size_t dstCapacity, void* b
return buffOut.pos;
}
-static ZSTD_CCtx* g_zcc = NULL;
-
#ifndef ZSTD_DLL_IMPORT
-size_t local_ZSTD_compressContinue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
+static size_t local_ZSTD_compressContinue(const void* src, size_t srcSize,
+ void* dst, size_t dstCapacity,
+ void* buff2)
{
- (void)buff2;
- ZSTD_compressBegin(g_zcc, 1 /* compressionLevel */);
+ ZSTD_parameters p;
+ ZSTD_frameParameters f = { 1 /* contentSizeHeader*/, 0, 0 };
+ p.fParams = f;
+ p.cParams = *(ZSTD_compressionParameters*)buff2;
+ ZSTD_compressBegin_advanced(g_zcc, NULL, 0, p, srcSize);
return ZSTD_compressEnd(g_zcc, dst, dstCapacity, src, srcSize);
}
#define FIRST_BLOCK_SIZE 8
-size_t local_ZSTD_compressContinue_extDict(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
+static size_t local_ZSTD_compressContinue_extDict(const void* src, size_t srcSize,
+ void* dst, size_t dstCapacity,
+ void* buff2)
{
BYTE firstBlockBuf[FIRST_BLOCK_SIZE];
- (void)buff2;
+ ZSTD_parameters p;
+ ZSTD_frameParameters f = { 1, 0, 0 };
+ p.fParams = f;
+ p.cParams = *(ZSTD_compressionParameters*)buff2;
+ ZSTD_compressBegin_advanced(g_zcc, NULL, 0, p, srcSize);
memcpy(firstBlockBuf, src, FIRST_BLOCK_SIZE);
- ZSTD_compressBegin(g_zcc, 1);
- { size_t const compressResult = ZSTD_compressContinue(g_zcc, dst, dstCapacity, firstBlockBuf, FIRST_BLOCK_SIZE);
- if (ZSTD_isError(compressResult)) { DISPLAY("local_ZSTD_compressContinue_extDict error : %s\n", ZSTD_getErrorName(compressResult)); return compressResult; }
+ { size_t const compressResult = ZSTD_compressContinue(g_zcc,
+ dst, dstCapacity,
+ firstBlockBuf, FIRST_BLOCK_SIZE);
+ if (ZSTD_isError(compressResult)) {
+ DISPLAY("local_ZSTD_compressContinue_extDict error : %s\n",
+ ZSTD_getErrorName(compressResult));
+ return compressResult;
+ }
dst = (BYTE*)dst + compressResult;
dstCapacity -= compressResult;
}
- return ZSTD_compressEnd(g_zcc, dst, dstCapacity, (const BYTE*)src + FIRST_BLOCK_SIZE, srcSize - FIRST_BLOCK_SIZE);
+ return ZSTD_compressEnd(g_zcc, dst, dstCapacity,
+ (const BYTE*)src + FIRST_BLOCK_SIZE,
+ srcSize - FIRST_BLOCK_SIZE);
}
-size_t local_ZSTD_decompressContinue(void* dst, size_t dstCapacity, void* buff2, const void* src, size_t srcSize)
+static size_t local_ZSTD_decompressContinue(const void* src, size_t srcSize,
+ void* dst, size_t dstCapacity,
+ void* buff2)
{
size_t regeneratedSize = 0;
const BYTE* ip = (const BYTE*)buff2;
@@ -263,7 +315,7 @@ size_t local_ZSTD_decompressContinue(void* dst, size_t dstCapacity, void* buff2,
BYTE* op = (BYTE*)dst;
size_t remainingCapacity = dstCapacity;
- (void)src; (void)srcSize;
+ (void)src; (void)srcSize; /* unused */
ZSTD_decompressBegin(g_zdc);
while (ip < iend) {
size_t const iSize = ZSTD_nextSrcSizeToDecompress(g_zdc);
@@ -282,27 +334,30 @@ size_t local_ZSTD_decompressContinue(void* dst, size_t dstCapacity, void* buff2,
/*_*******************************************************
* Bench functions
*********************************************************/
-static size_t benchMem(const void* src, size_t srcSize, U32 benchNb)
+static size_t benchMem(U32 benchNb,
+ const void* src, size_t srcSize,
+ int cLevel, ZSTD_compressionParameters cparams)
{
+ size_t dstBuffSize = ZSTD_compressBound(srcSize);
BYTE* dstBuff;
- size_t const dstBuffSize = ZSTD_compressBound(srcSize);
+ void* dstBuff2;
void* buff2;
const char* benchName;
- size_t (*benchFunction)(void* dst, size_t dstSize, void* verifBuff, const void* src, size_t srcSize);
- double bestTime = 100000000.;
+ BMK_benchFn_t benchFunction;
+ int errorcode = 0;
/* Selection */
switch(benchNb)
{
case 1:
- benchFunction = local_ZSTD_compress; benchName = "compress(1)";
+ benchFunction = local_ZSTD_compress; benchName = "compress";
break;
case 2:
benchFunction = local_ZSTD_decompress; benchName = "decompress";
break;
#ifndef ZSTD_DLL_IMPORT
case 11:
- benchFunction = local_ZSTD_compressContinue; benchName = "compressContinue(1)";
+ benchFunction = local_ZSTD_compressContinue; benchName = "compressContinue";
break;
case 12:
benchFunction = local_ZSTD_compressContinue_extDict; benchName = "compressContinue_extDict";
@@ -318,7 +373,7 @@ static size_t benchMem(const void* src, size_t srcSize, U32 benchNb)
break;
#endif
case 41:
- benchFunction = local_ZSTD_compressStream; benchName = "compressStream(1)";
+ benchFunction = local_ZSTD_compressStream; benchName = "compressStream";
break;
case 42:
benchFunction = local_ZSTD_decompressStream; benchName = "decompressStream";
@@ -341,32 +396,65 @@ static size_t benchMem(const void* src, size_t srcSize, U32 benchNb)
/* Allocation */
dstBuff = (BYTE*)malloc(dstBuffSize);
- buff2 = malloc(dstBuffSize);
- if ((!dstBuff) || (!buff2)) {
+ dstBuff2 = malloc(dstBuffSize);
+ if ((!dstBuff) || (!dstBuff2)) {
DISPLAY("\nError: not enough memory!\n");
- free(dstBuff); free(buff2);
+ free(dstBuff); free(dstBuff2);
return 12;
}
+ buff2 = dstBuff2;
if (g_zcc==NULL) g_zcc = ZSTD_createCCtx();
if (g_zdc==NULL) g_zdc = ZSTD_createDCtx();
if (g_cstream==NULL) g_cstream = ZSTD_createCStream();
if (g_dstream==NULL) g_dstream = ZSTD_createDStream();
+ /* DISPLAY("params: cLevel %d, wlog %d hlog %d clog %d slog %d slen %d tlen %d strat %d \n",
+ cLevel, cparams->windowLog, cparams->hashLog, cparams->chainLog, cparams->searchLog,
+ cparams->searchLength, cparams->targetLength, cparams->strategy); */
+
+ ZSTD_CCtx_setParameter(g_zcc, ZSTD_p_compressionLevel, cLevel);
+ ZSTD_CCtx_setParameter(g_zcc, ZSTD_p_windowLog, cparams.windowLog);
+ ZSTD_CCtx_setParameter(g_zcc, ZSTD_p_hashLog, cparams.hashLog);
+ ZSTD_CCtx_setParameter(g_zcc, ZSTD_p_chainLog, cparams.chainLog);
+ ZSTD_CCtx_setParameter(g_zcc, ZSTD_p_searchLog, cparams.searchLog);
+ ZSTD_CCtx_setParameter(g_zcc, ZSTD_p_minMatch, cparams.searchLength);
+ ZSTD_CCtx_setParameter(g_zcc, ZSTD_p_targetLength, cparams.targetLength);
+ ZSTD_CCtx_setParameter(g_zcc, ZSTD_p_compressionStrategy, cparams.strategy);
+
+
+ ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionLevel, cLevel);
+ ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_windowLog, cparams.windowLog);
+ ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_hashLog, cparams.hashLog);
+ ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_chainLog, cparams.chainLog);
+ ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_searchLog, cparams.searchLog);
+ ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_minMatch, cparams.searchLength);
+ ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_targetLength, cparams.targetLength);
+ ZSTD_CCtx_setParameter(g_cstream, ZSTD_p_compressionStrategy, cparams.strategy);
+
/* Preparation */
switch(benchNb)
{
+ case 1:
+ buff2 = &cparams;
+ break;
case 2:
- g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
+ g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, cLevel);
break;
#ifndef ZSTD_DLL_IMPORT
+ case 11:
+ buff2 = &cparams;
+ break;
+ case 12:
+ buff2 = &cparams;
+ break;
case 13 :
- g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
+ g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, cLevel);
break;
case 31: /* ZSTD_decodeLiteralsBlock */
{ blockProperties_t bp;
ZSTD_frameHeader zfp;
size_t frameHeaderSize, skippedSize;
- g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1);
+ g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, cLevel);
frameHeaderSize = ZSTD_getFrameHeader(&zfp, dstBuff, ZSTD_frameHeaderSize_min);
if (frameHeaderSize==0) frameHeaderSize = ZSTD_frameHeaderSize_min;
ZSTD_getcBlockSize(dstBuff+frameHeaderSize, dstBuffSize, &bp); /* Get 1st block type */
@@ -386,8 +474,8 @@ static size_t benchMem(const void* src, size_t srcSize, U32 benchNb)
const BYTE* ip = dstBuff;
const BYTE* iend;
size_t frameHeaderSize, cBlockSize;
- ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1); /* it would be better to use direct block compression here */
- g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, 1);
+ ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, cLevel); /* it would be better to use direct block compression here */
+ g_cSize = ZSTD_compress(dstBuff, dstBuffSize, src, srcSize, cLevel);
frameHeaderSize = ZSTD_getFrameHeader(&zfp, dstBuff, ZSTD_frameHeaderSize_min);
if (frameHeaderSize==0) frameHeaderSize = ZSTD_frameHeaderSize_min;
ip += frameHeaderSize; /* Skip frame Header */
@@ -409,8 +497,11 @@ static size_t benchMem(const void* src, size_t srcSize, U32 benchNb)
case 31:
goto _cleanOut;
#endif
+ case 41 :
+ buff2 = &cparams;
+ break;
case 42 :
- g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, 1);
+ g_cSize = ZSTD_compress(buff2, dstBuffSize, src, srcSize, cLevel);
break;
/* test functions */
@@ -419,138 +510,190 @@ static size_t benchMem(const void* src, size_t srcSize, U32 benchNb)
default : ;
}
- /* warming up memory */
+ /* warming up dstBuff */
{ size_t i; for (i=0; i<dstBuffSize; i++) dstBuff[i]=(BYTE)i; }
/* benchmark loop */
- { U32 loopNb;
- U32 nbRounds = (U32)((50 MB) / (srcSize+1)) + 1; /* initial conservative speed estimate */
-# define TIME_SEC_MICROSEC (1*1000000ULL) /* 1 second */
-# define TIME_SEC_NANOSEC (1*1000000000ULL) /* 1 second */
- DISPLAY("%2i- %-30.30s : \r", benchNb, benchName);
- for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
- UTIL_time_t clockStart;
- size_t benchResult=0;
- U32 roundNb;
-
- UTIL_sleepMilli(5); /* give processor time to other processes */
- UTIL_waitForNextTick();
- clockStart = UTIL_getTime();
- for (roundNb=0; roundNb < nbRounds; roundNb++) {
- benchResult = benchFunction(dstBuff, dstBuffSize, buff2, src, srcSize);
- if (ZSTD_isError(benchResult)) {
- DISPLAY("ERROR ! %s() => %s !! \n", benchName, ZSTD_getErrorName(benchResult));
- exit(1);
- } }
- { U64 const clockSpanNano = UTIL_clockSpanNano(clockStart);
- double const averageTime = (double)clockSpanNano / TIME_SEC_NANOSEC / nbRounds;
- if (clockSpanNano > 0) {
- if (averageTime < bestTime) bestTime = averageTime;
- assert(bestTime > (1./2000000000));
- nbRounds = (U32)(1. / bestTime); /* aim for 1 sec */
- DISPLAY("%2i- %-30.30s : %7.1f MB/s (%9u)\r",
- loopNb, benchName,
- (double)srcSize / (1 MB) / bestTime,
- (U32)benchResult);
- } else {
- assert(nbRounds < 40000000); /* avoid overflow */
- nbRounds *= 100;
- }
- } } }
- DISPLAY("%2u\n", benchNb);
+ { BMK_timedFnState_t* const tfs = BMK_createTimedFnState(g_nbIterations * 1000, 1000);
+ BMK_runTime_t bestResult;
+ bestResult.sumOfReturn = 0;
+ bestResult.nanoSecPerRun = (unsigned long long)(-1LL);
+ assert(tfs != NULL);
+ for (;;) {
+ void* const dstBuffv = dstBuff;
+ BMK_runOutcome_t const bOutcome =
+ BMK_benchTimedFn( tfs,
+ benchFunction, buff2,
+ NULL, NULL, /* initFn */
+ 1, /* blockCount */
+ &src, &srcSize,
+ &dstBuffv, &dstBuffSize,
+ NULL);
+
+ if (!BMK_isSuccessful_runOutcome(bOutcome)) {
+ DISPLAY("ERROR benchmarking function ! ! \n");
+ errorcode = 1;
+ goto _cleanOut;
+ }
+
+ { BMK_runTime_t const newResult = BMK_extract_runTime(bOutcome);
+ if (newResult.nanoSecPerRun < bestResult.nanoSecPerRun )
+ bestResult.nanoSecPerRun = newResult.nanoSecPerRun;
+ DISPLAY("\r%2u#%-29.29s:%8.1f MB/s (%8u) ",
+ benchNb, benchName,
+ (double)srcSize * TIMELOOP_NANOSEC / bestResult.nanoSecPerRun / MB_UNIT,
+ (unsigned)newResult.sumOfReturn );
+ }
+
+ if ( BMK_isCompleted_TimedFn(tfs) ) break;
+ }
+ BMK_freeTimedFnState(tfs);
+ }
+ DISPLAY("\n");
_cleanOut:
free(dstBuff);
- free(buff2);
+ free(dstBuff2);
ZSTD_freeCCtx(g_zcc); g_zcc=NULL;
ZSTD_freeDCtx(g_zdc); g_zdc=NULL;
ZSTD_freeCStream(g_cstream); g_cstream=NULL;
ZSTD_freeDStream(g_dstream); g_dstream=NULL;
- return 0;
+ return errorcode;
}
-static int benchSample(U32 benchNb)
+static int benchSample(U32 benchNb,
+ int cLevel, ZSTD_compressionParameters cparams)
{
size_t const benchedSize = g_sampleSize;
- const char* name = "Sample 10MiB";
+ const char* const name = "Sample 10MiB";
/* Allocation */
- void* origBuff = malloc(benchedSize);
+ void* const origBuff = malloc(benchedSize);
if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); return 12; }
/* Fill buffer */
RDG_genBuffer(origBuff, benchedSize, g_compressibility, 0.0, 0);
/* bench */
- DISPLAY("\r%79s\r", "");
+ DISPLAY("\r%70s\r", "");
DISPLAY(" %s : \n", name);
- if (benchNb)
- benchMem(origBuff, benchedSize, benchNb);
- else
- for (benchNb=0; benchNb<100; benchNb++) benchMem(origBuff, benchedSize, benchNb);
+ if (benchNb) {
+ benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
+ } else { /* 0 == run all tests */
+ for (benchNb=0; benchNb<100; benchNb++) {
+ benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
+ } }
free(origBuff);
return 0;
}
-static int benchFiles(const char** fileNamesTable, const int nbFiles, U32 benchNb)
+static int benchFiles(U32 benchNb,
+ const char** fileNamesTable, const int nbFiles,
+ int cLevel, ZSTD_compressionParameters cparams)
{
/* Loop for each file */
int fileIdx;
for (fileIdx=0; fileIdx<nbFiles; fileIdx++) {
const char* const inFileName = fileNamesTable[fileIdx];
FILE* const inFile = fopen( inFileName, "rb" );
- U64 inFileSize;
size_t benchedSize;
- void* origBuff;
/* Check file existence */
if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
/* Memory allocation & restrictions */
- inFileSize = UTIL_getFileSize(inFileName);
- if (inFileSize == UTIL_FILESIZE_UNKNOWN) {
- DISPLAY( "Cannot measure size of %s\n", inFileName);
- fclose(inFile);
- return 11;
- }
- benchedSize = BMK_findMaxMem(inFileSize*3) / 3;
- if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
- if (benchedSize < inFileSize)
- DISPLAY("Not enough memory for '%s' full size; testing %u MB only...\n", inFileName, (U32)(benchedSize>>20));
+ { U64 const inFileSize = UTIL_getFileSize(inFileName);
+ if (inFileSize == UTIL_FILESIZE_UNKNOWN) {
+ DISPLAY( "Cannot measure size of %s\n", inFileName);
+ fclose(inFile);
+ return 11;
+ }
+ benchedSize = BMK_findMaxMem(inFileSize*3) / 3;
+ if ((U64)benchedSize > inFileSize)
+ benchedSize = (size_t)inFileSize;
+ if ((U64)benchedSize < inFileSize) {
+ DISPLAY("Not enough memory for '%s' full size; testing %u MB only... \n",
+ inFileName, (U32)(benchedSize>>20));
+ } }
/* Alloc */
- origBuff = malloc(benchedSize);
- if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); fclose(inFile); return 12; }
-
- /* Fill input buffer */
- DISPLAY("Loading %s... \r", inFileName);
- {
- size_t readSize = fread(origBuff, 1, benchedSize, inFile);
- fclose(inFile);
- if (readSize != benchedSize) {
- DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
- free(origBuff);
- return 13;
- } }
+ { void* const origBuff = malloc(benchedSize);
+ if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); fclose(inFile); return 12; }
+
+ /* Fill input buffer */
+ DISPLAY("Loading %s... \r", inFileName);
+ { size_t const readSize = fread(origBuff, 1, benchedSize, inFile);
+ fclose(inFile);
+ if (readSize != benchedSize) {
+ DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
+ free(origBuff);
+ return 13;
+ } }
- /* bench */
- DISPLAY("\r%79s\r", "");
- DISPLAY(" %s : \n", inFileName);
- if (benchNb)
- benchMem(origBuff, benchedSize, benchNb);
- else
- for (benchNb=0; benchNb<100; benchNb++) benchMem(origBuff, benchedSize, benchNb);
+ /* bench */
+ DISPLAY("\r%70s\r", ""); /* blank line */
+ DISPLAY(" %s : \n", inFileName);
+ if (benchNb) {
+ benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
+ } else {
+ for (benchNb=0; benchNb<100; benchNb++) {
+ benchMem(benchNb, origBuff, benchedSize, cLevel, cparams);
+ } }
- free(origBuff);
- }
+ free(origBuff);
+ } }
return 0;
}
+
+/*_*******************************************************
+* Argument Parsing
+*********************************************************/
+
+#define ERROR_OUT(msg) { DISPLAY("%s \n", msg); exit(1); }
+
+static unsigned readU32FromChar(const char** stringPtr)
+{
+ const char errorMsg[] = "error: numeric value too large";
+ unsigned result = 0;
+ while ((**stringPtr >='0') && (**stringPtr <='9')) {
+ unsigned const max = (((unsigned)(-1)) / 10) - 1;
+ if (result > max) ERROR_OUT(errorMsg);
+ result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
+ }
+ if ((**stringPtr=='K') || (**stringPtr=='M')) {
+ unsigned const maxK = ((unsigned)(-1)) >> 10;
+ if (result > maxK) ERROR_OUT(errorMsg);
+ result <<= 10;
+ if (**stringPtr=='M') {
+ if (result > maxK) ERROR_OUT(errorMsg);
+ result <<= 10;
+ }
+ (*stringPtr)++; /* skip `K` or `M` */
+ if (**stringPtr=='i') (*stringPtr)++;
+ if (**stringPtr=='B') (*stringPtr)++;
+ }
+ return result;
+}
+
+static unsigned longCommandWArg(const char** stringPtr, const char* longCommand)
+{
+ size_t const comSize = strlen(longCommand);
+ int const result = !strncmp(*stringPtr, longCommand, comSize);
+ if (result) *stringPtr += comSize;
+ return result;
+}
+
+
+/*_*******************************************************
+* Command line
+*********************************************************/
+
static int usage(const char* exename)
{
DISPLAY( "Usage :\n");
@@ -567,6 +710,8 @@ static int usage_advanced(const char* exename)
DISPLAY( " -b# : test only function # \n");
DISPLAY( " -i# : iteration loops [1-9](default : %i)\n", NBLOOPS);
DISPLAY( " -P# : sample compressibility (default : %.1f%%)\n", COMPRESSIBILITY_DEFAULT * 100);
+ DISPLAY( " -l# : benchmark functions at that compression level (default : %i)\n", DEFAULT_CLEVEL);
+ DISPLAY( " --zstd : custom parameter selection. Format same as zstdcli \n");
return 0;
}
@@ -579,23 +724,45 @@ static int badusage(const char* exename)
int main(int argc, const char** argv)
{
- int i, filenamesStart=0, result;
- const char* exename = argv[0];
+ int argNb, filenamesStart=0, result;
+ const char* const exename = argv[0];
const char* input_filename = NULL;
U32 benchNb = 0, main_pause = 0;
+ int cLevel = DEFAULT_CLEVEL;
+ ZSTD_compressionParameters cparams = ZSTD_getCParams(cLevel, 0, 0);
DISPLAY(WELCOME_MESSAGE);
if (argc<1) return badusage(exename);
- for(i=1; i<argc; i++) {
- const char* argument = argv[i];
+ for (argNb=1; argNb<argc; argNb++) {
+ const char* argument = argv[argNb];
assert(argument != NULL);
- /* Commands (note : aggregated commands are allowed) */
- if (argument[0]=='-') {
+ if (longCommandWArg(&argument, "--zstd=")) {
+ for ( ; ;) {
+ if (longCommandWArg(&argument, "windowLog=") || longCommandWArg(&argument, "wlog=")) { cparams.windowLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
+ if (longCommandWArg(&argument, "chainLog=") || longCommandWArg(&argument, "clog=")) { cparams.chainLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
+ if (longCommandWArg(&argument, "hashLog=") || longCommandWArg(&argument, "hlog=")) { cparams.hashLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
+ if (longCommandWArg(&argument, "searchLog=") || longCommandWArg(&argument, "slog=")) { cparams.searchLog = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
+ if (longCommandWArg(&argument, "searchLength=") || longCommandWArg(&argument, "slen=")) { cparams.searchLength = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
+ if (longCommandWArg(&argument, "targetLength=") || longCommandWArg(&argument, "tlen=")) { cparams.targetLength = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
+ if (longCommandWArg(&argument, "strategy=") || longCommandWArg(&argument, "strat=")) { cparams.strategy = (ZSTD_strategy)(readU32FromChar(&argument)); if (argument[0]==',') { argument++; continue; } else break; }
+ if (longCommandWArg(&argument, "level=") || longCommandWArg(&argument, "lvl=")) { cLevel = (int)readU32FromChar(&argument); cparams = ZSTD_getCParams(cLevel, 0, 0); if (argument[0]==',') { argument++; continue; } else break; }
+ DISPLAY("invalid compression parameter \n");
+ return 1;
+ }
+
+ /* check end of string */
+ if (argument[0] != 0) {
+ DISPLAY("invalid --zstd= format \n");
+ return 1;
+ } else {
+ continue;
+ }
- while (argument[1]!=0) {
- argument++;
+ } else if (argument[0]=='-') { /* Commands (note : aggregated commands are allowed) */
+ argument++;
+ while (argument[0]!=0) {
switch(argument[0])
{
@@ -608,33 +775,25 @@ int main(int argc, const char** argv)
/* Select specific algorithm to bench */
case 'b':
- benchNb = 0;
- while ((argument[1]>= '0') && (argument[1]<= '9')) {
- benchNb *= 10;
- benchNb += argument[1] - '0';
- argument++;
- }
+ argument++;
+ benchNb = readU32FromChar(&argument);
break;
/* Modify Nb Iterations */
case 'i':
- if ((argument[1] >='0') && (argument[1] <='9')) {
- int iters = argument[1] - '0';
- BMK_SetNbIterations(iters);
- argument++;
- }
+ argument++;
+ BMK_SetNbIterations((int)readU32FromChar(&argument));
break;
/* Select compressibility of synthetic sample */
case 'P':
- { U32 proba32 = 0;
- while ((argument[1]>= '0') && (argument[1]<= '9')) {
- proba32 *= 10;
- proba32 += argument[1] - '0';
- argument++;
- }
- g_compressibility = (double)proba32 / 100.;
- }
+ argument++;
+ g_compressibility = (double)readU32FromChar(&argument) / 100.;
+ break;
+ case 'l':
+ argument++;
+ cLevel = readU32FromChar(&argument);
+ cparams = ZSTD_getCParams(cLevel, 0, 0);
break;
/* Unknown command */
@@ -645,13 +804,15 @@ int main(int argc, const char** argv)
}
/* first provided filename is input */
- if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
+ if (!input_filename) { input_filename=argument; filenamesStart=argNb; continue; }
}
+
+
if (filenamesStart==0) /* no input file */
- result = benchSample(benchNb);
+ result = benchSample(benchNb, cLevel, cparams);
else
- result = benchFiles(argv+filenamesStart, argc-filenamesStart, benchNb);
+ result = benchFiles(benchNb, argv+filenamesStart, argc-filenamesStart, cLevel, cparams);
if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; }
diff --git a/tests/fuzz/fuzz.h b/tests/fuzz/fuzz.h
index a64845473c2b0..8850025b0fd25 100644
--- a/tests/fuzz/fuzz.h
+++ b/tests/fuzz/fuzz.h
@@ -23,10 +23,10 @@
* the data to zstd functions. Every fuzzer initializes the RNG exactly
* once before doing anything else, even if it is unused.
* Default: 4.
- * @param ZSTD_DEBUG:
- * This is a parameter for the zstd library. Defining `ZSTD_DEBUG=1`
+ * @param DEBUGLEVEL:
+ * This is a parameter for the zstd library. Defining `DEBUGLEVEL=1`
* enables assert() statements in the zstd library. Higher levels enable
- * logging, so aren't recommended. Defining `ZSTD_DEBUG=1` is
+ * logging, so aren't recommended. Defining `DEBUGLEVEL=1` is
* recommended.
* @param MEM_FORCE_MEMORY_ACCESS:
* This flag controls how the zstd library accesses unaligned memory.
diff --git a/tests/fuzz/fuzz.py b/tests/fuzz/fuzz.py
index b591e4f6734e3..8ce293a3a695f 100755
--- a/tests/fuzz/fuzz.py
+++ b/tests/fuzz/fuzz.py
@@ -13,6 +13,7 @@ import argparse
import contextlib
import os
import re
+import shlex
import shutil
import subprocess
import sys
@@ -147,15 +148,18 @@ def compiler_version(cc, cxx):
"""
cc_version_bytes = subprocess.check_output([cc, "--version"])
cxx_version_bytes = subprocess.check_output([cxx, "--version"])
- if cc_version_bytes.startswith(b'clang'):
- assert(cxx_version_bytes.startswith(b'clang'))
+ compiler = None
+ version = None
+ if b'clang' in cc_version_bytes:
+ assert(b'clang' in cxx_version_bytes)
compiler = 'clang'
- if cc_version_bytes.startswith(b'gcc'):
- assert(cxx_version_bytes.startswith(b'g++'))
+ elif b'gcc' in cc_version_bytes:
+ assert(b'gcc' in cxx_version_bytes)
compiler = 'gcc'
- version_regex = b'([0-9])+\.([0-9])+\.([0-9])+'
- version_match = re.search(version_regex, cc_version_bytes)
- version = tuple(int(version_match.group(i)) for i in range(1, 4))
+ if compiler is not None:
+ version_regex = b'([0-9])+\.([0-9])+\.([0-9])+'
+ version_match = re.search(version_regex, cc_version_bytes)
+ version = tuple(int(version_match.group(i)) for i in range(1, 4))
return compiler, version
@@ -248,7 +252,7 @@ def build_parser(args):
dest='debug',
type=int,
default=1,
- help='Set ZSTD_DEBUG (default: 1)')
+ help='Set DEBUGLEVEL (default: 1)')
parser.add_argument(
'--force-memory-access',
dest='memory_access',
@@ -265,7 +269,7 @@ def build_parser(args):
'--disable-fuzzing-mode',
dest='fuzzing_mode',
action='store_false',
- help='Do not define FUZZING_BUILD_MORE_UNSAFE_FOR_PRODUCTION')
+ help='Do not define FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION')
parser.add_argument(
'--enable-stateful-fuzzing',
dest='stateful_fuzzing',
@@ -346,16 +350,16 @@ def build(args):
targets = args.TARGET
cc = args.cc
cxx = args.cxx
- cppflags = [args.cppflags]
- cflags = [args.cflags]
- ldflags = [args.ldflags]
- cxxflags = [args.cxxflags]
- mflags = [args.mflags] if args.mflags else []
+ cppflags = shlex.split(args.cppflags)
+ cflags = shlex.split(args.cflags)
+ ldflags = shlex.split(args.ldflags)
+ cxxflags = shlex.split(args.cxxflags)
+ mflags = shlex.split(args.mflags)
# Flags to be added to both cflags and cxxflags
common_flags = []
cppflags += [
- '-DZSTD_DEBUG={}'.format(args.debug),
+ '-DDEBUGLEVEL={}'.format(args.debug),
'-DMEM_FORCE_MEMORY_ACCESS={}'.format(args.memory_access),
'-DFUZZ_RNG_SEED_SIZE={}'.format(args.fuzz_rng_seed_size),
]
@@ -399,7 +403,7 @@ def build(args):
cppflags += ['-DSTATEFUL_FUZZING']
if args.fuzzing_mode:
- cppflags += ['-DFUZZING_BUILD_MORE_UNSAFE_FOR_PRODUCTION']
+ cppflags += ['-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION']
if args.lib_fuzzing_engine == 'libregression.a':
targets = ['libregression.a'] + targets
@@ -750,11 +754,10 @@ def zip_cmd(args):
for target in args.TARGET:
# Zip the seed_corpus
seed_corpus = abs_join(CORPORA_DIR, "{}_seed_corpus".format(target))
- seeds = [abs_join(seed_corpus, f) for f in os.listdir(seed_corpus)]
zip_file = "{}.zip".format(seed_corpus)
- cmd = ["zip", "-q", "-j", "-9", zip_file]
- print(' '.join(cmd + [abs_join(seed_corpus, '*')]))
- subprocess.check_call(cmd + seeds)
+ cmd = ["zip", "-r", "-q", "-j", "-9", zip_file, "."]
+ print(' '.join(cmd))
+ subprocess.check_call(cmd, cwd=seed_corpus)
def list_cmd(args):
diff --git a/tests/fuzz/regression_driver.c b/tests/fuzz/regression_driver.c
index 2b714d29e9dd6..1553d436ce038 100644
--- a/tests/fuzz/regression_driver.c
+++ b/tests/fuzz/regression_driver.c
@@ -16,7 +16,7 @@
#include <stdlib.h>
int main(int argc, char const **argv) {
- size_t const kMaxFileSize = (size_t)1 << 20;
+ size_t const kMaxFileSize = (size_t)1 << 27;
int const kFollowLinks = 1;
char *fileNamesBuf = NULL;
char const **files = argv + 1;
diff --git a/tests/fuzz/zstd_helpers.c b/tests/fuzz/zstd_helpers.c
index 6fc38361b7adc..bf5eccff83c70 100644
--- a/tests/fuzz/zstd_helpers.c
+++ b/tests/fuzz/zstd_helpers.c
@@ -34,8 +34,7 @@ ZSTD_compressionParameters FUZZ_randomCParams(size_t srcSize, uint32_t *state)
cParams.searchLog = FUZZ_rand32(state, ZSTD_SEARCHLOG_MIN, 9);
cParams.searchLength = FUZZ_rand32(state, ZSTD_SEARCHLENGTH_MIN,
ZSTD_SEARCHLENGTH_MAX);
- cParams.targetLength = FUZZ_rand32(state, ZSTD_TARGETLENGTH_MIN,
- 512);
+ cParams.targetLength = FUZZ_rand32(state, 0, 512);
cParams.strategy = FUZZ_rand32(state, ZSTD_fast, ZSTD_btultra);
return ZSTD_adjustCParams(cParams, srcSize, 0);
}
@@ -72,6 +71,7 @@ void FUZZ_setRandomParameters(ZSTD_CCtx *cctx, size_t srcSize, uint32_t *state)
setRand(cctx, ZSTD_p_contentSizeFlag, 0, 1, state);
setRand(cctx, ZSTD_p_checksumFlag, 0, 1, state);
setRand(cctx, ZSTD_p_dictIDFlag, 0, 1, state);
+ setRand(cctx, ZSTD_p_forceAttachDict, -2, 2, state);
/* Select long distance matchig parameters */
setRand(cctx, ZSTD_p_enableLongDistanceMatching, 0, 1, state);
setRand(cctx, ZSTD_p_ldmHashLog, ZSTD_HASHLOG_MIN, 16, state);
diff --git a/tests/fuzzer.c b/tests/fuzzer.c
index e97b841e8535c..5616285b9ed70 100644
--- a/tests/fuzzer.c
+++ b/tests/fuzzer.c
@@ -27,6 +27,7 @@
#include <string.h> /* strcmp */
#include <assert.h>
#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressContinue, ZSTD_compressBlock */
+#include "fse.h"
#include "zstd.h" /* ZSTD_VERSION_STRING */
#include "zstd_errors.h" /* ZSTD_getErrorCode */
#include "zstdmt_compress.h"
@@ -66,14 +67,20 @@ static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER;
if (g_displayLevel>=4) fflush(stderr); } }
+/*-*******************************************************
+* Compile time test
+*********************************************************/
#undef MIN
#undef MAX
+/* Declaring the function is it isn't unused */
+void FUZ_bug976(void);
void FUZ_bug976(void)
{ /* these constants shall not depend on MIN() macro */
assert(ZSTD_HASHLOG_MAX < 31);
assert(ZSTD_CHAINLOG_MAX < 31);
}
+
/*-*******************************************************
* Internal functions
*********************************************************/
@@ -117,6 +124,13 @@ static unsigned FUZ_highbit32(U32 v32)
#define CHECK(fn) { CHECK_V(err, fn); }
#define CHECKPLUS(var, fn, more) { CHECK_V(var, fn); more; }
+#define CHECK_EQ(lhs, rhs) { \
+ if ((lhs) != (rhs)) { \
+ DISPLAY("Error L%u => %s != %s ", __LINE__, #lhs, #rhs); \
+ goto _output_error; \
+ } \
+}
+
/*=============================================
* Memory Tests
@@ -167,13 +181,9 @@ static void FUZ_displayMallocStats(mallocCounter_t count)
(U32)(count.totalMalloc >> 10));
}
-static int FUZ_mallocTests(unsigned seed, double compressibility, unsigned part)
+static int FUZ_mallocTests_internal(unsigned seed, double compressibility, unsigned part,
+ void* inBuffer, size_t inSize, void* outBuffer, size_t outSize)
{
- size_t const inSize = 64 MB + 16 MB + 4 MB + 1 MB + 256 KB + 64 KB; /* 85.3 MB */
- size_t const outSize = ZSTD_compressBound(inSize);
- void* const inBuffer = malloc(inSize);
- void* const outBuffer = malloc(outSize);
-
/* test only played in verbose mode, as they are long */
if (g_displayLevel<3) return 0;
@@ -258,6 +268,28 @@ static int FUZ_mallocTests(unsigned seed, double compressibility, unsigned part)
return 0;
}
+static int FUZ_mallocTests(unsigned seed, double compressibility, unsigned part)
+{
+ size_t const inSize = 64 MB + 16 MB + 4 MB + 1 MB + 256 KB + 64 KB; /* 85.3 MB */
+ size_t const outSize = ZSTD_compressBound(inSize);
+ void* const inBuffer = malloc(inSize);
+ void* const outBuffer = malloc(outSize);
+ int result;
+
+ /* Create compressible noise */
+ if (!inBuffer || !outBuffer) {
+ DISPLAY("Not enough memory, aborting \n");
+ exit(1);
+ }
+
+ result = FUZ_mallocTests_internal(seed, compressibility, part,
+ inBuffer, inSize, outBuffer, outSize);
+
+ free(inBuffer);
+ free(outBuffer);
+ return result;
+}
+
#else
static int FUZ_mallocTests(unsigned seed, double compressibility, unsigned part)
@@ -303,9 +335,13 @@ static int basicUnitTests(U32 seed, double compressibility)
DISPLAYLEVEL(3, "OK : %s \n", errorString);
}
+ DISPLAYLEVEL(3, "test%3i : min compression level : ", testNb++);
+ { int const mcl = ZSTD_minCLevel();
+ DISPLAYLEVEL(3, "%i (OK) \n", mcl);
+ }
DISPLAYLEVEL(3, "test%3i : compress %u bytes : ", testNb++, (U32)CNBuffSize);
- { ZSTD_CCtx* cctx = ZSTD_createCCtx();
+ { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
if (cctx==NULL) goto _output_error;
CHECKPLUS(r, ZSTD_compressCCtx(cctx,
compressedBuffer, compressedBufferSize,
@@ -368,6 +404,12 @@ static int basicUnitTests(U32 seed, double compressibility)
if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; }
DISPLAYLEVEL(3, "OK \n");
+ DISPLAYLEVEL(3, "test%3i : decompress too large input : ", testNb++);
+ { size_t const r = ZSTD_decompress(decodedBuffer, CNBuffSize, compressedBuffer, compressedBufferSize);
+ if (!ZSTD_isError(r)) goto _output_error;
+ if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; }
+ DISPLAYLEVEL(3, "OK \n");
+
DISPLAYLEVEL(3, "test%3d : check CCtx size after compressing empty input : ", testNb++);
{ ZSTD_CCtx* cctx = ZSTD_createCCtx();
size_t const r = ZSTD_compressCCtx(cctx, compressedBuffer, compressedBufferSize, NULL, 0, 19);
@@ -394,14 +436,80 @@ static int basicUnitTests(U32 seed, double compressibility)
}
DISPLAYLEVEL(3, "OK \n");
- DISPLAYLEVEL(3, "test%3d : large window log smaller data : ", testNb++);
+ DISPLAYLEVEL(3, "test%3d : re-using a CCtx should compress the same : ", testNb++);
+ { int i;
+ for (i=0; i<20; i++)
+ ((char*)CNBuffer)[i] = (char)i; /* ensure no match during initial section */
+ memcpy((char*)CNBuffer + 20, CNBuffer, 10); /* create one match, starting from beginning of sample, which is the difficult case (see #1241) */
+ for (i=1; i<=19; i++) {
+ ZSTD_CCtx* const cctx = ZSTD_createCCtx();
+ size_t size1, size2;
+ DISPLAYLEVEL(5, "l%i ", i);
+ size1 = ZSTD_compressCCtx(cctx, compressedBuffer, compressedBufferSize, CNBuffer, 30, i);
+ CHECK_Z(size1);
+ size2 = ZSTD_compressCCtx(cctx, compressedBuffer, compressedBufferSize, CNBuffer, 30, i);
+ CHECK_Z(size2);
+ CHECK_EQ(size1, size2);
+
+ ZSTD_freeCCtx(cctx);
+ }
+ }
+ DISPLAYLEVEL(3, "OK \n");
+
+ DISPLAYLEVEL(3, "test%3d : ZSTD_CCtx_getParameter() : ", testNb++);
{ ZSTD_CCtx* const cctx = ZSTD_createCCtx();
- ZSTD_parameters params = ZSTD_getParams(1, ZSTD_CONTENTSIZE_UNKNOWN, 0);
- size_t const nbCompressions = (1U << 31) / CNBuffSize + 1;
- size_t i;
+ ZSTD_outBuffer out = {NULL, 0, 0};
+ ZSTD_inBuffer in = {NULL, 0, 0};
+ unsigned value;
+
+ CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_compressionLevel, &value));
+ CHECK_EQ(value, 3);
+ CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_hashLog, &value));
+ CHECK_EQ(value, 0);
+ CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_p_hashLog, ZSTD_HASHLOG_MIN));
+ CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_compressionLevel, &value));
+ CHECK_EQ(value, 3);
+ CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_hashLog, &value));
+ CHECK_EQ(value, ZSTD_HASHLOG_MIN);
+ CHECK_Z(ZSTD_CCtx_setParameter(cctx, ZSTD_p_compressionLevel, 7));
+ CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_compressionLevel, &value));
+ CHECK_EQ(value, 7);
+ CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_hashLog, &value));
+ CHECK_EQ(value, ZSTD_HASHLOG_MIN);
+ /* Start a compression job */
+ ZSTD_compress_generic(cctx, &out, &in, ZSTD_e_continue);
+ CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_compressionLevel, &value));
+ CHECK_EQ(value, 7);
+ CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_hashLog, &value));
+ CHECK_EQ(value, ZSTD_HASHLOG_MIN);
+ /* Reset the CCtx */
+ ZSTD_CCtx_reset(cctx);
+ CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_compressionLevel, &value));
+ CHECK_EQ(value, 7);
+ CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_hashLog, &value));
+ CHECK_EQ(value, ZSTD_HASHLOG_MIN);
+ /* Reset the parameters */
+ ZSTD_CCtx_resetParameters(cctx);
+ CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_compressionLevel, &value));
+ CHECK_EQ(value, 3);
+ CHECK_Z(ZSTD_CCtx_getParameter(cctx, ZSTD_p_hashLog, &value));
+ CHECK_EQ(value, 0);
+
+ ZSTD_freeCCtx(cctx);
+ }
+ DISPLAYLEVEL(3, "OK \n");
+
+ /* this test is really too long, and should be made faster */
+ DISPLAYLEVEL(3, "test%3d : overflow protection with large windowLog : ", testNb++);
+ { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
+ ZSTD_parameters params = ZSTD_getParams(-9, ZSTD_CONTENTSIZE_UNKNOWN, 0);
+ size_t const nbCompressions = ((1U << 31) / CNBuffSize) + 1; /* ensure U32 overflow protection is triggered */
+ size_t cnb;
+ assert(cctx != NULL);
params.fParams.contentSizeFlag = 0;
params.cParams.windowLog = ZSTD_WINDOWLOG_MAX;
- for (i = 0; i < nbCompressions; ++i) {
+ for (cnb = 0; cnb < nbCompressions; ++cnb) {
+ DISPLAYLEVEL(6, "run %zu / %zu \n", cnb, nbCompressions);
CHECK_Z( ZSTD_compressBegin_advanced(cctx, NULL, 0, params, ZSTD_CONTENTSIZE_UNKNOWN) ); /* re-use same parameters */
CHECK_Z( ZSTD_compressEnd(cctx, compressedBuffer, compressedBufferSize, CNBuffer, CNBuffSize) );
}
@@ -409,6 +517,39 @@ static int basicUnitTests(U32 seed, double compressibility)
}
DISPLAYLEVEL(3, "OK \n");
+ DISPLAYLEVEL(3, "test%3d : size down context : ", testNb++);
+ { ZSTD_CCtx* const largeCCtx = ZSTD_createCCtx();
+ assert(largeCCtx != NULL);
+ CHECK_Z( ZSTD_compressBegin(largeCCtx, 19) ); /* streaming implies ZSTD_CONTENTSIZE_UNKNOWN, which maximizes memory usage */
+ CHECK_Z( ZSTD_compressEnd(largeCCtx, compressedBuffer, compressedBufferSize, CNBuffer, 1) );
+ { size_t const largeCCtxSize = ZSTD_sizeof_CCtx(largeCCtx); /* size of context must be measured after compression */
+ { ZSTD_CCtx* const smallCCtx = ZSTD_createCCtx();
+ assert(smallCCtx != NULL);
+ CHECK_Z(ZSTD_compressCCtx(smallCCtx, compressedBuffer, compressedBufferSize, CNBuffer, 1, 1));
+ { size_t const smallCCtxSize = ZSTD_sizeof_CCtx(smallCCtx);
+ DISPLAYLEVEL(5, "(large) %zuKB > 32*%zuKB (small) : ",
+ largeCCtxSize>>10, smallCCtxSize>>10);
+ assert(largeCCtxSize > 32* smallCCtxSize); /* note : "too large" definition is handled within zstd_compress.c .
+ * make this test case extreme, so that it doesn't depend on a possibly fluctuating definition */
+ }
+ ZSTD_freeCCtx(smallCCtx);
+ }
+ { U32 const maxNbAttempts = 1100; /* nb of usages before triggering size down is handled within zstd_compress.c.
+ * currently defined as 128x, but could be adjusted in the future.
+ * make this test long enough so that it's not too much tied to the current definition within zstd_compress.c */
+ U32 u;
+ for (u=0; u<maxNbAttempts; u++) {
+ CHECK_Z(ZSTD_compressCCtx(largeCCtx, compressedBuffer, compressedBufferSize, CNBuffer, 1, 1));
+ if (ZSTD_sizeof_CCtx(largeCCtx) < largeCCtxSize) break; /* sized down */
+ }
+ DISPLAYLEVEL(5, "size down after %u attempts : ", u);
+ if (u==maxNbAttempts) goto _output_error; /* no sizedown happened */
+ }
+ }
+ ZSTD_freeCCtx(largeCCtx);
+ }
+ DISPLAYLEVEL(3, "OK \n");
+
/* Static CCtx tests */
#define STATIC_CCTX_LEVEL 3
DISPLAYLEVEL(3, "test%3i : create static CCtx for level %u :", testNb++, STATIC_CCTX_LEVEL);
@@ -1024,12 +1165,40 @@ static int basicUnitTests(U32 seed, double compressibility)
ZSTD_freeCCtx(cctx);
}
+ /* negative compression level test : ensure simple API and advanced API produce same result */
+ DISPLAYLEVEL(3, "test%3i : negative compression level : ", testNb++);
+ { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
+ size_t const srcSize = CNBuffSize / 5;
+ int const compressionLevel = -1;
+
+ assert(cctx != NULL);
+ { ZSTD_parameters const params = ZSTD_getParams(compressionLevel, srcSize, 0);
+ size_t const cSize_1pass = ZSTD_compress_advanced(cctx,
+ compressedBuffer, compressedBufferSize,
+ CNBuffer, srcSize,
+ NULL, 0,
+ params);
+ if (ZSTD_isError(cSize_1pass)) goto _output_error;
+
+ CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_p_compressionLevel, (unsigned)compressionLevel) );
+ { ZSTD_inBuffer in = { CNBuffer, srcSize, 0 };
+ ZSTD_outBuffer out = { compressedBuffer, compressedBufferSize, 0 };
+ size_t const compressionResult = ZSTD_compress_generic(cctx, &out, &in, ZSTD_e_end);
+ DISPLAYLEVEL(5, "simple=%zu vs %zu=advanced : ", cSize_1pass, out.pos);
+ if (ZSTD_isError(compressionResult)) goto _output_error;
+ if (out.pos != cSize_1pass) goto _output_error;
+ } }
+ ZSTD_freeCCtx(cctx);
+ }
+ DISPLAYLEVEL(3, "OK \n");
+
/* parameters order test */
{ size_t const inputSize = CNBuffSize / 2;
U64 xxh64;
- { ZSTD_CCtx* cctx = ZSTD_createCCtx();
+ { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
DISPLAYLEVEL(3, "test%3i : parameters in order : ", testNb++);
+ assert(cctx != NULL);
CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_p_compressionLevel, 2) );
CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_p_enableLongDistanceMatching, 1) );
CHECK( ZSTD_CCtx_setParameter(cctx, ZSTD_p_windowLog, 18) );
@@ -1085,9 +1254,13 @@ static int basicUnitTests(U32 seed, double compressibility)
DISPLAYLEVEL(3, "OK : %s \n", ZSTD_getErrorName(decodeResult));
}
- DISPLAYLEVEL(3, "test%3i : decompress with magic-less instruction : ", testNb++);
+ DISPLAYLEVEL(3, "test%3i : decompress of magic-less frame : ", testNb++);
ZSTD_DCtx_reset(dctx);
CHECK( ZSTD_DCtx_setFormat(dctx, ZSTD_f_zstd1_magicless) );
+ { ZSTD_frameHeader zfh;
+ size_t const zfhrt = ZSTD_getFrameHeader_advanced(&zfh, compressedBuffer, cSize, ZSTD_f_zstd1_magicless);
+ if (zfhrt != 0) goto _output_error;
+ }
{ ZSTD_inBuffer in = { compressedBuffer, cSize, 0 };
ZSTD_outBuffer out = { decodedBuffer, CNBuffSize, 0 };
size_t const result = ZSTD_decompress_generic(dctx, &out, &in);
@@ -1120,6 +1293,20 @@ static int basicUnitTests(U32 seed, double compressibility)
if (r != blockSize) goto _output_error; }
DISPLAYLEVEL(3, "OK \n");
+ /* very long stream of block compression */
+ DISPLAYLEVEL(3, "test%3i : Huge block streaming compression test : ", testNb++);
+ CHECK( ZSTD_compressBegin(cctx, -99) ); /* we just want to quickly overflow internal U32 index */
+ CHECK( ZSTD_getBlockSize(cctx) >= blockSize);
+ { U64 const toCompress = 5000000000ULL; /* > 4 GB */
+ U64 compressed = 0;
+ while (compressed < toCompress) {
+ size_t const blockCSize = ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), CNBuffer, blockSize);
+ if (ZSTD_isError(cSize)) goto _output_error;
+ compressed += blockCSize;
+ }
+ }
+ DISPLAYLEVEL(3, "OK \n");
+
/* dictionary block compression */
DISPLAYLEVEL(3, "test%3i : Dictionary Block compression test : ", testNb++);
CHECK( ZSTD_compressBegin_usingDict(cctx, CNBuffer, dictSize, 5) );
@@ -1142,6 +1329,15 @@ static int basicUnitTests(U32 seed, double compressibility)
if (r != blockSize) goto _output_error; }
DISPLAYLEVEL(3, "OK \n");
+ DISPLAYLEVEL(3, "test%3i : Block compression with CDict : ", testNb++);
+ { ZSTD_CDict* const cdict = ZSTD_createCDict(CNBuffer, dictSize, 3);
+ if (cdict==NULL) goto _output_error;
+ CHECK( ZSTD_compressBegin_usingCDict(cctx, cdict) );
+ CHECK( ZSTD_compressBlock(cctx, compressedBuffer, ZSTD_compressBound(blockSize), (char*)CNBuffer+dictSize, blockSize) );
+ ZSTD_freeCDict(cdict);
+ }
+ DISPLAYLEVEL(3, "OK \n");
+
ZSTD_freeCCtx(cctx);
}
ZSTD_freeDCtx(dctx);
@@ -1199,6 +1395,24 @@ static int basicUnitTests(U32 seed, double compressibility)
((BYTE*)CNBuffer)[i+1] = _3BytesSeqs[id][1];
((BYTE*)CNBuffer)[i+2] = _3BytesSeqs[id][2];
} } }
+ DISPLAYLEVEL(3, "test%3i : growing nbSeq : ", testNb++);
+ { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
+ size_t const maxNbSeq = _3BYTESTESTLENGTH / 3;
+ size_t const bound = ZSTD_compressBound(_3BYTESTESTLENGTH);
+ size_t nbSeq = 1;
+ while (nbSeq <= maxNbSeq) {
+ CHECK(ZSTD_compressCCtx(cctx, compressedBuffer, bound, CNBuffer, nbSeq * 3, 19));
+ /* Check every sequence for the first 100, then skip more rapidly. */
+ if (nbSeq < 100) {
+ ++nbSeq;
+ } else {
+ nbSeq += (nbSeq >> 2);
+ }
+ }
+ ZSTD_freeCCtx(cctx);
+ }
+ DISPLAYLEVEL(3, "OK \n");
+
DISPLAYLEVEL(3, "test%3i : compress lots 3-bytes sequences : ", testNb++);
{ CHECK_V(r, ZSTD_compress(compressedBuffer, ZSTD_compressBound(_3BYTESTESTLENGTH),
CNBuffer, _3BYTESTESTLENGTH, 19) );
@@ -1210,8 +1424,26 @@ static int basicUnitTests(U32 seed, double compressibility)
if (r != _3BYTESTESTLENGTH) goto _output_error; }
DISPLAYLEVEL(3, "OK \n");
- DISPLAYLEVEL(3, "test%3i : incompressible data and ill suited dictionary : ", testNb++);
+
+ DISPLAYLEVEL(3, "test%3i : growing literals buffer : ", testNb++);
RDG_genBuffer(CNBuffer, CNBuffSize, 0.0, 0.1, seed);
+ { ZSTD_CCtx* const cctx = ZSTD_createCCtx();
+ size_t const bound = ZSTD_compressBound(CNBuffSize);
+ size_t size = 1;
+ while (size <= CNBuffSize) {
+ CHECK(ZSTD_compressCCtx(cctx, compressedBuffer, bound, CNBuffer, size, 3));
+ /* Check every size for the first 100, then skip more rapidly. */
+ if (size < 100) {
+ ++size;
+ } else {
+ size += (size >> 2);
+ }
+ }
+ ZSTD_freeCCtx(cctx);
+ }
+ DISPLAYLEVEL(3, "OK \n");
+
+ DISPLAYLEVEL(3, "test%3i : incompressible data and ill suited dictionary : ", testNb++);
{ /* Train a dictionary on low characters */
size_t dictSize = 16 KB;
void* const dictBuffer = malloc(dictSize);
@@ -1286,6 +1518,24 @@ static int basicUnitTests(U32 seed, double compressibility)
}
DISPLAYLEVEL(3, "OK \n");
+ DISPLAYLEVEL(3, "test%3i : testing FSE_normalizeCount() PR#1255: ", testNb++);
+ {
+ short norm[32];
+ unsigned count[32];
+ unsigned const tableLog = 5;
+ size_t const nbSeq = 32;
+ unsigned const maxSymbolValue = 31;
+ size_t i;
+
+ for (i = 0; i < 32; ++i)
+ count[i] = 1;
+ /* Calling FSE_normalizeCount() on a uniform distribution should not
+ * cause a division by zero.
+ */
+ FSE_normalizeCount(norm, tableLog, count, nbSeq, maxSymbolValue);
+ }
+ DISPLAYLEVEL(3, "OK \n");
+
_end:
free(CNBuffer);
free(compressedBuffer);
@@ -1359,7 +1609,6 @@ static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, U32 const maxD
size_t const dstBufferSize = (size_t)1<<maxSampleLog;
size_t const cBufferSize = ZSTD_compressBound(dstBufferSize);
BYTE* cNoiseBuffer[5];
- BYTE* srcBuffer; /* jumping pointer */
BYTE* const cBuffer = (BYTE*) malloc (cBufferSize);
BYTE* const dstBuffer = (BYTE*) malloc (dstBufferSize);
BYTE* const mirrorBuffer = (BYTE*) malloc (dstBufferSize);
@@ -1368,7 +1617,7 @@ static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, U32 const maxD
ZSTD_DCtx* const dctx = ZSTD_createDCtx();
U32 result = 0;
U32 testNb = 0;
- U32 coreSeed = seed, lseed = 0;
+ U32 coreSeed = seed;
UTIL_time_t const startClock = UTIL_getTime();
U64 const maxClockSpan = maxDurationS * SEC_TO_MICRO;
int const cLevelLimiter = bigTests ? 3 : 2;
@@ -1389,13 +1638,14 @@ static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, U32 const maxD
RDG_genBuffer(cNoiseBuffer[2], srcBufferSize, compressibility, 0., coreSeed);
RDG_genBuffer(cNoiseBuffer[3], srcBufferSize, 0.95, 0., coreSeed); /* highly compressible */
RDG_genBuffer(cNoiseBuffer[4], srcBufferSize, 1.00, 0., coreSeed); /* sparse content */
- srcBuffer = cNoiseBuffer[2];
/* catch up testNb */
for (testNb=1; testNb < startTest; testNb++) FUZ_rand(&coreSeed);
/* main test loop */
for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < maxClockSpan); testNb++ ) {
+ BYTE* srcBuffer; /* jumping pointer */
+ U32 lseed;
size_t sampleSize, maxTestSize, totalTestSize;
size_t cSize, totalCSize, totalGenSize;
U64 crcOrig;
@@ -1626,11 +1876,9 @@ static int fuzzerTests(U32 seed, U32 nbTests, unsigned startTest, U32 const maxD
CHECK (totalGenSize != totalTestSize, "streaming decompressed data : wrong size")
CHECK (totalCSize != cSize, "compressed data should be fully read")
{ U64 const crcDest = XXH64(dstBuffer, totalTestSize, 0);
- if (crcDest!=crcOrig) {
- size_t const errorPos = findDiff(mirrorBuffer, dstBuffer, totalTestSize);
- CHECK (1, "streaming decompressed data corrupted : byte %u / %u (%02X!=%02X)",
- (U32)errorPos, (U32)totalTestSize, dstBuffer[errorPos], mirrorBuffer[errorPos]);
- } }
+ CHECK(crcOrig != crcDest, "streaming decompressed data corrupted (pos %u / %u)",
+ (U32)findDiff(mirrorBuffer, dstBuffer, totalTestSize), (U32)totalTestSize);
+ }
} /* for ( ; (testNb <= nbTests) */
DISPLAY("\r%u fuzzer tests completed \n", testNb-1);
diff --git a/tests/gzip/Makefile b/tests/gzip/Makefile
index 40a0ba97d2b37..c5d67206b99d9 100644
--- a/tests/gzip/Makefile
+++ b/tests/gzip/Makefile
@@ -33,7 +33,7 @@ clean:
#------------------------------------------------------------------------------
-# validated only for Linux, OSX, Hurd and some BSD targets
+# validated only for Linux, macOS, Hurd and some BSD targets
#------------------------------------------------------------------------------
ifneq (,$(filter $(shell uname),Linux Darwin GNU/kFreeBSD GNU FreeBSD DragonFly NetBSD))
diff --git a/tests/legacy.c b/tests/legacy.c
index 847e1d25e96b7..e1cf82f2f9d83 100644
--- a/tests/legacy.c
+++ b/tests/legacy.c
@@ -36,7 +36,7 @@ size_t const COMPRESSED_SIZE = 917;
const char* const EXPECTED; /* content is at end of file */
-int testSimpleAPI(void)
+static int testSimpleAPI(void)
{
size_t const size = strlen(EXPECTED);
char* const output = malloc(size);
@@ -71,7 +71,8 @@ int testSimpleAPI(void)
return 0;
}
-int testStreamingAPI(void)
+
+static int testStreamingAPI(void)
{
size_t const outBuffSize = ZSTD_DStreamOutSize();
char* const outBuff = malloc(outBuffSize);
diff --git a/tests/libzstd_partial_builds.sh b/tests/libzstd_partial_builds.sh
new file mode 100755
index 0000000000000..34d8ea55231eb
--- /dev/null
+++ b/tests/libzstd_partial_builds.sh
@@ -0,0 +1,36 @@
+#!/bin/sh -e
+
+die() {
+ $ECHO "$@" 1>&2
+ exit 1
+}
+
+DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+
+INTOVOID="/dev/null"
+case "$OS" in
+ Windows*)
+ INTOVOID="NUL"
+ ;;
+esac
+
+ZSTD_LIB_COMPRESSION=0 CFLAGS= make -C $DIR/../lib libzstd.a > $INTOVOID
+nm $DIR/../lib/libzstd.a | grep ".*\.o:" > tmplog
+! grep -q "zstd_compress" tmplog && grep -q "zstd_decompress" tmplog && ! grep -q "dict" tmplog && grep -q "zstd_v" tmplog && ! grep -q "zbuff" tmplog && make clean && rm -f tmplog || die "Compression macro failed"
+
+
+ZSTD_LIB_DECOMPRESSION=0 CFLAGS= make -C $DIR/../lib libzstd.a > $INTOVOID
+nm $DIR/../lib/libzstd.a | grep ".*\.o:" > tmplog
+grep -q "zstd_compress" tmplog && ! grep -q "zstd_decompress" tmplog && grep -q "dict" tmplog && ! grep -q "zstd_v" tmplog && ! grep -q "zbuff" tmplog && make clean && rm -f tmplog || die "Decompression macro failed"
+
+ZSTD_LIB_DEPRECATED=0 CFLAGS= make -C $DIR/../lib libzstd.a > $INTOVOID
+nm $DIR/../lib/libzstd.a | grep ".*\.o:" > tmplog
+grep -q "zstd_compress" tmplog && grep -q "zstd_decompress" tmplog && grep -q "dict" tmplog && grep -q "zstd_v" tmplog && ! grep -q "zbuff" tmplog && make clean && rm -f tmplog || die "Deprecated macro failed"
+
+ZSTD_LIB_DICTBUILDER=0 CFLAGS= make -C $DIR/../lib libzstd.a > $INTOVOID
+nm $DIR/../lib/libzstd.a | grep ".*\.o:" > tmplog
+grep -q "zstd_compress" tmplog && grep -q "zstd_decompress" tmplog && ! grep -q "dict" tmplog && grep -q "zstd_v" tmplog && grep -q "zbuff" tmplog && make clean && rm -f tmplog || die "Dictbuilder macro failed"
+
+ZSTD_LIB_DECOMPRESSION=0 ZSTD_LIB_DICTBUILDER=0 CFLAGS= make -C $DIR/../lib libzstd.a > $INTOVOID
+nm $DIR/../lib/libzstd.a | grep ".*\.o:" > tmplog
+grep -q "zstd_compress" tmplog && ! grep -q "zstd_decompress" tmplog && ! grep -q "dict" tmplog && ! grep -q "zstd_v" tmplog && ! grep -q "zbuff" tmplog && make clean && rm -f tmplog || die "Multi-macro failed" \ No newline at end of file
diff --git a/tests/longmatch.c b/tests/longmatch.c
index ed3861571d9b7..1271e9ab1039c 100644
--- a/tests/longmatch.c
+++ b/tests/longmatch.c
@@ -17,25 +17,25 @@
#define ZSTD_STATIC_LINKING_ONLY
#include "zstd.h"
-int compress(ZSTD_CStream *ctx, ZSTD_outBuffer out, const void *data, size_t size) {
+static int
+compress(ZSTD_CStream *ctx, ZSTD_outBuffer out, const void *data, size_t size)
+{
ZSTD_inBuffer in = { data, size, 0 };
while (in.pos < in.size) {
ZSTD_outBuffer tmp = out;
const size_t rc = ZSTD_compressStream(ctx, &tmp, &in);
- if (ZSTD_isError(rc)) {
- return 1;
- }
+ if (ZSTD_isError(rc)) return 1;
}
- {
- ZSTD_outBuffer tmp = out;
+ { ZSTD_outBuffer tmp = out;
const size_t rc = ZSTD_flushStream(ctx, &tmp);
if (rc != 0) { return 1; }
}
return 0;
}
-int main(int argc, const char** argv) {
- ZSTD_CStream *ctx;
+int main(int argc, const char** argv)
+{
+ ZSTD_CStream* ctx;
ZSTD_parameters params;
size_t rc;
unsigned windowLog;
diff --git a/tests/paramgrill.c b/tests/paramgrill.c
index 13b102b2d042d..7a4be854a46bf 100644
--- a/tests/paramgrill.c
+++ b/tests/paramgrill.c
@@ -17,7 +17,7 @@
#include <stdio.h> /* fprintf, fopen, ftello64 */
#include <string.h> /* strcmp */
#include <math.h> /* log */
-#include <time.h>
+#include <assert.h>
#include "mem.h"
#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_parameters, ZSTD_estimateCCtxSize */
@@ -25,6 +25,9 @@
#include "datagen.h"
#include "xxhash.h"
#include "util.h"
+#include "bench.h"
+#include "zstd_errors.h"
+#include "zstd_internal.h" /* should not be needed */
/*-************************************
@@ -32,25 +35,15 @@
**************************************/
#define PROGRAM_DESCRIPTION "ZSTD parameters tester"
#define AUTHOR "Yann Collet"
-#define WELCOME_MESSAGE "*** %s %s %i-bits, by %s (%s) ***\n", PROGRAM_DESCRIPTION, ZSTD_VERSION_STRING, (int)(sizeof(void*)*8), AUTHOR, __DATE__
+#define WELCOME_MESSAGE "*** %s %s %i-bits, by %s ***\n", PROGRAM_DESCRIPTION, ZSTD_VERSION_STRING, (int)(sizeof(void*)*8), AUTHOR
-
-#define KB *(1<<10)
-#define MB *(1<<20)
-#define GB *(1ULL<<30)
-
-#define NBLOOPS 2
-#define TIMELOOP (2 * SEC_TO_MICRO)
-
-#define NB_LEVELS_TRACKED 30
+#define TIMELOOP_NANOSEC (1*1000000000ULL) /* 1 second */
+#define NB_LEVELS_TRACKED 22 /* ensured being >= ZSTD_maxCLevel() in BMK_init_level_constraints() */
static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31));
#define COMPRESSIBILITY_DEFAULT 0.50
-static const size_t sampleSize = 10000000;
-static const double g_grillDuration_s = 90000; /* about 24 hours */
-static const U64 g_maxParamTime = 15 * SEC_TO_MICRO;
static const U64 g_maxVariationTime = 60 * SEC_TO_MICRO;
static const int g_maxNbVariations = 64;
@@ -59,38 +52,301 @@ static const int g_maxNbVariations = 64;
* Macros
**************************************/
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
+#define DISPLAYLEVEL(n, ...) if(g_displayLevel >= n) { fprintf(stderr, __VA_ARGS__); }
+#define DEBUGOUTPUT(...) { if (DEBUG) DISPLAY(__VA_ARGS__); }
+
+#define TIMED 0
+#ifndef DEBUG
+# define DEBUG 0
+#endif
#undef MIN
#undef MAX
#define MIN(a,b) ( (a) < (b) ? (a) : (b) )
#define MAX(a,b) ( (a) > (b) ? (a) : (b) )
+#define CUSTOM_LEVEL 99
+#define BASE_CLEVEL 1
+
+#define FADT_MIN 0
+#define FADT_MAX ((U32)-1)
+
+#define WLOG_RANGE (ZSTD_WINDOWLOG_MAX - ZSTD_WINDOWLOG_MIN + 1)
+#define CLOG_RANGE (ZSTD_CHAINLOG_MAX - ZSTD_CHAINLOG_MIN + 1)
+#define HLOG_RANGE (ZSTD_HASHLOG_MAX - ZSTD_HASHLOG_MIN + 1)
+#define SLOG_RANGE (ZSTD_SEARCHLOG_MAX - ZSTD_SEARCHLOG_MIN + 1)
+#define SLEN_RANGE (ZSTD_SEARCHLENGTH_MAX - ZSTD_SEARCHLENGTH_MIN + 1)
+#define TLEN_RANGE 17
+#define STRT_RANGE (ZSTD_btultra - ZSTD_fast + 1)
+#define FADT_RANGE 3
+
+#define CHECKTIME(r) { if(BMK_timeSpan(g_time) > g_timeLimit_s) { DEBUGOUTPUT("Time Limit Reached\n"); return r; } }
+#define CHECKTIMEGT(ret, val, _gototag) {if(BMK_timeSpan(g_time) > g_timeLimit_s) { DEBUGOUTPUT("Time Limit Reached\n"); ret = val; goto _gototag; } }
+
+#define PARAM_UNSET ((U32)-2) /* can't be -1 b/c fadt uses -1 */
+
+static const char* g_stratName[ZSTD_btultra+1] = {
+ "(none) ", "ZSTD_fast ", "ZSTD_dfast ",
+ "ZSTD_greedy ", "ZSTD_lazy ", "ZSTD_lazy2 ",
+ "ZSTD_btlazy2 ", "ZSTD_btopt ", "ZSTD_btultra "};
+
+static const U32 tlen_table[TLEN_RANGE] = { 0, 1, 2, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 256, 512, 999 };
/*-************************************
-* Benchmark Parameters
+* Setup for Adding new params
**************************************/
-static U32 g_nbIterations = NBLOOPS;
-static double g_compressibility = COMPRESSIBILITY_DEFAULT;
+
+/* indices for each of the variables */
+typedef enum {
+ wlog_ind = 0,
+ clog_ind = 1,
+ hlog_ind = 2,
+ slog_ind = 3,
+ slen_ind = 4,
+ tlen_ind = 5,
+ strt_ind = 6,
+ fadt_ind = 7, /* forceAttachDict */
+ NUM_PARAMS = 8
+} varInds_t;
+
+typedef struct {
+ U32 vals[NUM_PARAMS];
+} paramValues_t;
+
+/* maximum value of parameters */
+static const U32 mintable[NUM_PARAMS] =
+ { ZSTD_WINDOWLOG_MIN, ZSTD_CHAINLOG_MIN, ZSTD_HASHLOG_MIN, ZSTD_SEARCHLOG_MIN, ZSTD_SEARCHLENGTH_MIN, ZSTD_TARGETLENGTH_MIN, ZSTD_fast, FADT_MIN };
+
+/* minimum value of parameters */
+static const U32 maxtable[NUM_PARAMS] =
+ { ZSTD_WINDOWLOG_MAX, ZSTD_CHAINLOG_MAX, ZSTD_HASHLOG_MAX, ZSTD_SEARCHLOG_MAX, ZSTD_SEARCHLENGTH_MAX, ZSTD_TARGETLENGTH_MAX, ZSTD_btultra, FADT_MAX };
+
+/* # of values parameters can take on */
+static const U32 rangetable[NUM_PARAMS] =
+ { WLOG_RANGE, CLOG_RANGE, HLOG_RANGE, SLOG_RANGE, SLEN_RANGE, TLEN_RANGE, STRT_RANGE, FADT_RANGE };
+
+/* ZSTD_cctxSetParameter() index to set */
+static const ZSTD_cParameter cctxSetParamTable[NUM_PARAMS] =
+ { ZSTD_p_windowLog, ZSTD_p_chainLog, ZSTD_p_hashLog, ZSTD_p_searchLog, ZSTD_p_minMatch, ZSTD_p_targetLength, ZSTD_p_compressionStrategy, ZSTD_p_forceAttachDict };
+
+/* names of parameters */
+static const char* g_paramNames[NUM_PARAMS] =
+ { "windowLog", "chainLog", "hashLog","searchLog", "searchLength", "targetLength", "strategy", "forceAttachDict" };
+
+/* shortened names of parameters */
+static const char* g_shortParamNames[NUM_PARAMS] =
+ { "wlog", "clog", "hlog","slog", "slen", "tlen", "strt", "fadt" };
+
+/* maps value from { 0 to rangetable[param] - 1 } to valid paramvalues */
+static U32 rangeMap(varInds_t param, int ind) {
+ ind = MAX(MIN(ind, (int)rangetable[param] - 1), 0);
+ switch(param) {
+ case tlen_ind:
+ return tlen_table[ind];
+ case fadt_ind: /* 0, 1, 2 -> -1, 0, 1 */
+ return ind - 1;
+ case wlog_ind: /* using default: triggers -Wswitch-enum */
+ case clog_ind:
+ case hlog_ind:
+ case slog_ind:
+ case slen_ind:
+ case strt_ind:
+ return mintable[param] + ind;
+ case NUM_PARAMS:
+ DISPLAY("Error, not a valid param\n ");
+ return (U32)-1;
+ }
+ return 0; /* should never happen, stop compiler warnings */
+}
+
+/* inverse of rangeMap */
+static int invRangeMap(varInds_t param, U32 value) {
+ value = MIN(MAX(mintable[param], value), maxtable[param]);
+ switch(param) {
+ case tlen_ind: /* bin search */
+ {
+ int lo = 0;
+ int hi = TLEN_RANGE;
+ while(lo < hi) {
+ int mid = (lo + hi) / 2;
+ if(tlen_table[mid] < value) {
+ lo = mid + 1;
+ } if(tlen_table[mid] == value) {
+ return mid;
+ } else {
+ hi = mid;
+ }
+ }
+ return lo;
+ }
+ case fadt_ind:
+ return (int)value + 1;
+ case wlog_ind:
+ case clog_ind:
+ case hlog_ind:
+ case slog_ind:
+ case slen_ind:
+ case strt_ind:
+ return value - mintable[param];
+ case NUM_PARAMS:
+ DISPLAY("Error, not a valid param\n ");
+ return -2;
+ }
+ return 0; /* should never happen, stop compiler warnings */
+}
+
+/* display of params */
+static void displayParamVal(FILE* f, varInds_t param, U32 value, int width) {
+ switch(param) {
+ case fadt_ind: if(width) { fprintf(f, "%*d", width, (int)value); } else { fprintf(f, "%d", (int)value); } break;
+ case strt_ind: if(width) { fprintf(f, "%*s", width, g_stratName[value]); } else { fprintf(f, "%s", g_stratName[value]); } break;
+ case wlog_ind:
+ case clog_ind:
+ case hlog_ind:
+ case slog_ind:
+ case slen_ind:
+ case tlen_ind: if(width) { fprintf(f, "%*u", width, value); } else { fprintf(f, "%u", value); } break;
+ case NUM_PARAMS:
+ DISPLAY("Error, not a valid param\n "); break;
+ }
+}
+
+
+/*-************************************
+* Benchmark Parameters/Global Variables
+**************************************/
+
+typedef BYTE U8;
+
+/* General Utility */
+static U32 g_timeLimit_s = 99999; /* about 27 hours */
+static UTIL_time_t g_time; /* to be used to compare solution finding speeds to compare to original */
static U32 g_blockSize = 0;
static U32 g_rand = 1;
+
+/* Display */
+static int g_displayLevel = 3;
+static BYTE g_silenceParams[NUM_PARAMS];
+
+/* Mode Selection */
static U32 g_singleRun = 0;
+static U32 g_optimizer = 0;
+static int g_optmode = 0;
+
+/* For cLevel Table generation */
static U32 g_target = 0;
static U32 g_noSeed = 0;
-static ZSTD_compressionParameters g_params = { 0, 0, 0, 0, 0, 0, ZSTD_greedy };
-void BMK_SetNbIterations(int nbLoops)
-{
- g_nbIterations = nbLoops;
- DISPLAY("- %u iterations -\n", g_nbIterations);
-}
+/* For optimizer */
+static paramValues_t g_params; /* Initialized at the beginning of main w/ emptyParams() function */
+static double g_ratioMultiplier = 5.;
+static U32 g_strictness = PARAM_UNSET; /* range 1 - 100, measure of how strict */
+static BMK_benchResult_t g_lvltarget;
+
+typedef enum {
+ directMap,
+ xxhashMap,
+ noMemo
+} memoTableType_t;
+
+typedef struct {
+ memoTableType_t tableType;
+ BYTE* table;
+ size_t tableLen;
+ varInds_t varArray[NUM_PARAMS];
+ size_t varLen;
+} memoTable_t;
+
+typedef struct {
+ BMK_benchResult_t result;
+ paramValues_t params;
+} winnerInfo_t;
+
+typedef struct {
+ U32 cSpeed; /* bytes / sec */
+ U32 dSpeed;
+ U32 cMem; /* bytes */
+} constraint_t;
+
+typedef struct winner_ll_node winner_ll_node;
+struct winner_ll_node {
+ winnerInfo_t res;
+ winner_ll_node* next;
+};
+
+static winner_ll_node* g_winners; /* linked list sorted ascending by cSize & cSpeed */
+
+/*
+ * Additional Global Variables (Defined Above Use)
+ * g_level_constraint
+ * g_alreadyTested
+ * g_maxTries
+ * g_clockGranularity
+ */
/*-*******************************************************
-* Private functions
+* General Util Functions
*********************************************************/
-/* accuracy in seconds only, span can be multiple years */
-static double BMK_timeSpan(time_t tStart) { return difftime(time(NULL), tStart); }
+/* nullified useless params, to ensure count stats */
+/* cleans up params for memoizing / display */
+static paramValues_t sanitizeParams(paramValues_t params)
+{
+ if (params.vals[strt_ind] == ZSTD_fast)
+ params.vals[clog_ind] = 0, params.vals[slog_ind] = 0;
+ if (params.vals[strt_ind] == ZSTD_dfast)
+ params.vals[slog_ind] = 0;
+ if (params.vals[strt_ind] != ZSTD_btopt && params.vals[strt_ind] != ZSTD_btultra && params.vals[strt_ind] != ZSTD_fast)
+ params.vals[tlen_ind] = 0;
+
+ return params;
+}
+
+static ZSTD_compressionParameters pvalsToCParams(paramValues_t p) {
+ ZSTD_compressionParameters c;
+ memset(&c, 0, sizeof(ZSTD_compressionParameters));
+ c.windowLog = p.vals[wlog_ind];
+ c.chainLog = p.vals[clog_ind];
+ c.hashLog = p.vals[hlog_ind];
+ c.searchLog = p.vals[slog_ind];
+ c.searchLength = p.vals[slen_ind];
+ c.targetLength = p.vals[tlen_ind];
+ c.strategy = p.vals[strt_ind];
+ /* no forceAttachDict */
+ return c;
+}
+
+static paramValues_t cParamsToPVals(ZSTD_compressionParameters c) {
+ paramValues_t p;
+ varInds_t i;
+ p.vals[wlog_ind] = c.windowLog;
+ p.vals[clog_ind] = c.chainLog;
+ p.vals[hlog_ind] = c.hashLog;
+ p.vals[slog_ind] = c.searchLog;
+ p.vals[slen_ind] = c.searchLength;
+ p.vals[tlen_ind] = c.targetLength;
+ p.vals[strt_ind] = c.strategy;
+
+ /* set all other params to their minimum value */
+ for(i = strt_ind + 1; i < NUM_PARAMS; i++) {
+ p.vals[i] = mintable[i];
+ }
+ return p;
+}
+
+/* equivalent of ZSTD_adjustCParams for paramValues_t */
+static paramValues_t adjustParams(paramValues_t p, const size_t maxBlockSize, const size_t dictSize) {
+ paramValues_t ot = p;
+ varInds_t i;
+ p = cParamsToPVals(ZSTD_adjustCParams(pvalsToCParams(p), maxBlockSize, dictSize));
+ if(!dictSize) { p.vals[fadt_ind] = 0; }
+ /* retain value of all other parameters */
+ for(i = strt_ind + 1; i < NUM_PARAMS; i++) {
+ p.vals[i] = ot.vals[i];
+ }
+ return p;
+}
static size_t BMK_findMaxMem(U64 requiredMem)
{
@@ -100,23 +356,25 @@ static size_t BMK_findMaxMem(U64 requiredMem)
requiredMem = (((requiredMem >> 26) + 1) << 26);
if (requiredMem > maxMemory) requiredMem = maxMemory;
- requiredMem += 2*step;
- while (!testmem) {
- requiredMem -= step;
+ requiredMem += 2 * step;
+ while (!testmem && requiredMem > 0) {
testmem = malloc ((size_t)requiredMem);
+ requiredMem -= step;
}
free (testmem);
- return (size_t) (requiredMem - step);
+ return (size_t) requiredMem;
}
+/* accuracy in seconds only, span can be multiple years */
+static U32 BMK_timeSpan(const UTIL_time_t tStart) { return (U32)(UTIL_clockSpanMicro(tStart) / 1000000ULL); }
static U32 FUZ_rotl32(U32 x, U32 r)
{
return ((x << r) | (x >> (32 - r)));
}
-U32 FUZ_rand(U32* src)
+static U32 FUZ_rand(U32* src)
{
const U32 prime1 = 2654435761U;
const U32 prime2 = 2246822519U;
@@ -128,221 +386,489 @@ U32 FUZ_rand(U32* src)
return rand32 >> 5;
}
+/* allows zeros */
+#define CLAMPCHECK(val,min,max) { \
+ if (((val)<(min)) | ((val)>(max))) { \
+ DISPLAY("INVALID PARAMETER CONSTRAINTS\n"); \
+ return 0; \
+} }
+
+static int paramValid(const paramValues_t paramTarget) {
+ U32 i;
+ for(i = 0; i < NUM_PARAMS; i++) {
+ CLAMPCHECK(paramTarget.vals[i], mintable[i], maxtable[i]);
+ }
+ return 1;
+}
-/*-*******************************************************
-* Bench functions
-*********************************************************/
-typedef struct {
- size_t cSize;
- double cSpeed; /* bytes / sec */
- double dSpeed;
-} BMK_result_t;
+static paramValues_t cParamUnsetMin(paramValues_t paramTarget) {
+ varInds_t i;
+ for(i = 0; i < NUM_PARAMS; i++) {
+ if(paramTarget.vals[i] == PARAM_UNSET) {
+ paramTarget.vals[i] = mintable[i];
+ }
+ }
+ return paramTarget;
+}
-typedef struct
-{
- const char* srcPtr;
- size_t srcSize;
- char* cPtr;
- size_t cRoom;
- size_t cSize;
- char* resPtr;
- size_t resSize;
-} blockParam_t;
-
-
-static size_t BMK_benchParam(BMK_result_t* resultPtr,
- const void* srcBuffer, size_t srcSize,
- ZSTD_CCtx* ctx,
- const ZSTD_compressionParameters cParams)
+static paramValues_t emptyParams(void) {
+ U32 i;
+ paramValues_t p;
+ for(i = 0; i < NUM_PARAMS; i++) {
+ p.vals[i] = PARAM_UNSET;
+ }
+ return p;
+}
+
+static winnerInfo_t initWinnerInfo(const paramValues_t p) {
+ winnerInfo_t w1;
+ w1.result.cSpeed = 0.;
+ w1.result.dSpeed = 0.;
+ w1.result.cMem = (size_t)-1;
+ w1.result.cSize = (size_t)-1;
+ w1.params = p;
+ return w1;
+}
+
+static paramValues_t overwriteParams(paramValues_t base, const paramValues_t mask) {
+ U32 i;
+ for(i = 0; i < NUM_PARAMS; i++) {
+ if(mask.vals[i] != PARAM_UNSET) {
+ base.vals[i] = mask.vals[i];
+ }
+ }
+ return base;
+}
+
+static void paramVaryOnce(const varInds_t paramIndex, const int amt, paramValues_t* ptr) {
+ ptr->vals[paramIndex] = rangeMap(paramIndex, invRangeMap(paramIndex, ptr->vals[paramIndex]) + amt);
+}
+
+/* varies ptr by nbChanges respecting varyParams*/
+static void paramVariation(paramValues_t* ptr, memoTable_t* mtAll, const U32 nbChanges)
{
- const size_t blockSize = g_blockSize ? g_blockSize : srcSize;
- const U32 nbBlocks = (U32) ((srcSize + (blockSize-1)) / blockSize);
- blockParam_t* const blockTable = (blockParam_t*) malloc(nbBlocks * sizeof(blockParam_t));
- const size_t maxCompressedSize = (size_t)nbBlocks * ZSTD_compressBound(blockSize);
- void* const compressedBuffer = malloc(maxCompressedSize);
- void* const resultBuffer = malloc(srcSize);
- ZSTD_parameters params;
- U32 Wlog = cParams.windowLog;
- U32 Clog = cParams.chainLog;
- U32 Hlog = cParams.hashLog;
- U32 Slog = cParams.searchLog;
- U32 Slength = cParams.searchLength;
- U32 Tlength = cParams.targetLength;
- ZSTD_strategy strat = cParams.strategy;
- char name[30] = { 0 };
- U64 crcOrig;
-
- /* init result for early exit */
- resultPtr->cSize = srcSize;
- resultPtr->cSpeed = 0.;
- resultPtr->dSpeed = 0.;
-
- /* Memory allocation & restrictions */
- snprintf(name, 30, "Sw%02uc%02uh%02us%02ul%1ut%03uS%1u", Wlog, Clog, Hlog, Slog, Slength, Tlength, strat);
- if (!compressedBuffer || !resultBuffer || !blockTable) {
- DISPLAY("\nError: not enough memory!\n");
- free(compressedBuffer);
- free(resultBuffer);
- free(blockTable);
- return 12;
- }
-
- /* Calculating input Checksum */
- crcOrig = XXH64(srcBuffer, srcSize, 0);
-
- /* Init blockTable data */
- {
+ paramValues_t p;
+ U32 validated = 0;
+ while (!validated) {
U32 i;
- size_t remaining = srcSize;
- const char* srcPtr = (const char*)srcBuffer;
- char* cPtr = (char*)compressedBuffer;
- char* resPtr = (char*)resultBuffer;
- for (i=0; i<nbBlocks; i++) {
- size_t thisBlockSize = MIN(remaining, blockSize);
- blockTable[i].srcPtr = srcPtr;
- blockTable[i].cPtr = cPtr;
- blockTable[i].resPtr = resPtr;
- blockTable[i].srcSize = thisBlockSize;
- blockTable[i].cRoom = ZSTD_compressBound(thisBlockSize);
- srcPtr += thisBlockSize;
- cPtr += blockTable[i].cRoom;
- resPtr += thisBlockSize;
- remaining -= thisBlockSize;
- } }
+ p = *ptr;
+ for (i = 0 ; i < nbChanges ; i++) {
+ const U32 changeID = (U32)FUZ_rand(&g_rand) % (mtAll[p.vals[strt_ind]].varLen << 1);
+ paramVaryOnce(mtAll[p.vals[strt_ind]].varArray[changeID >> 1], ((changeID & 1) << 1) - 1, &p);
+ }
+ validated = paramValid(p);
+ }
+ *ptr = p;
+}
- /* warmimg up memory */
- RDG_genBuffer(compressedBuffer, maxCompressedSize, 0.10, 0.10, 1);
+/* Completely random parameter selection */
+static paramValues_t randomParams(void)
+{
+ varInds_t v; paramValues_t p;
+ for(v = 0; v < NUM_PARAMS; v++) {
+ p.vals[v] = rangeMap(v, FUZ_rand(&g_rand) % rangetable[v]);
+ }
+ return p;
+}
- /* Bench */
- { U32 loopNb;
- size_t cSize = 0;
- double fastestC = 100000000., fastestD = 100000000.;
- double ratio = 0.;
- UTIL_time_t const benchStart = UTIL_getTime();
-
- DISPLAY("\r%79s\r", "");
- memset(&params, 0, sizeof(params));
- params.cParams = cParams;
- for (loopNb = 1; loopNb <= g_nbIterations; loopNb++) {
- int nbLoops;
- U32 blockNb;
- UTIL_time_t roundStart;
- U64 roundClock;
-
- { U64 const benchTime = UTIL_clockSpanMicro(benchStart);
- if (benchTime > g_maxParamTime) break; }
-
- /* Compression */
- DISPLAY("\r%1u-%s : %9u ->", loopNb, name, (U32)srcSize);
- memset(compressedBuffer, 0xE5, maxCompressedSize);
-
- nbLoops = 0;
- UTIL_waitForNextTick();
- roundStart = UTIL_getTime();
- while (UTIL_clockSpanMicro(roundStart) < TIMELOOP) {
- for (blockNb=0; blockNb<nbBlocks; blockNb++)
- blockTable[blockNb].cSize = ZSTD_compress_advanced(ctx,
- blockTable[blockNb].cPtr, blockTable[blockNb].cRoom,
- blockTable[blockNb].srcPtr, blockTable[blockNb].srcSize,
- NULL, 0,
- params);
- nbLoops++;
+static U64 g_clockGranularity = 100000000ULL;
+
+static void findClockGranularity(void) {
+ UTIL_time_t clockStart = UTIL_getTime();
+ U64 el1 = 0, el2 = 0;
+ int i = 0;
+ do {
+ el1 = el2;
+ el2 = UTIL_clockSpanNano(clockStart);
+ if(el1 < el2) {
+ U64 iv = el2 - el1;
+ if(g_clockGranularity > iv) {
+ g_clockGranularity = iv;
+ i = 0;
+ } else {
+ i++;
}
- roundClock = UTIL_clockSpanMicro(roundStart);
-
- cSize = 0;
- for (blockNb=0; blockNb<nbBlocks; blockNb++)
- cSize += blockTable[blockNb].cSize;
- ratio = (double)srcSize / (double)cSize;
- if ((double)roundClock < fastestC * SEC_TO_MICRO * nbLoops) fastestC = ((double)roundClock / SEC_TO_MICRO) / nbLoops;
- DISPLAY("\r");
- DISPLAY("%1u-%s : %9u ->", loopNb, name, (U32)srcSize);
- DISPLAY(" %9u (%4.3f),%7.1f MB/s", (U32)cSize, ratio, (double)srcSize / fastestC / 1000000.);
- resultPtr->cSize = cSize;
- resultPtr->cSpeed = (double)srcSize / fastestC;
-
-#if 1
- /* Decompression */
- memset(resultBuffer, 0xD6, srcSize);
-
- nbLoops = 0;
- UTIL_waitForNextTick();
- roundStart = UTIL_getTime();
- for ( ; UTIL_clockSpanMicro(roundStart) < TIMELOOP; nbLoops++) {
- for (blockNb=0; blockNb<nbBlocks; blockNb++)
- blockTable[blockNb].resSize = ZSTD_decompress(blockTable[blockNb].resPtr, blockTable[blockNb].srcSize,
- blockTable[blockNb].cPtr, blockTable[blockNb].cSize);
+ }
+ } while(i < 10);
+ DEBUGOUTPUT("Granularity: %llu\n", (unsigned long long)g_clockGranularity);
+}
+
+/*-************************************
+* Optimizer Util Functions
+**************************************/
+
+/* checks results are feasible */
+static int feasible(const BMK_benchResult_t results, const constraint_t target) {
+ return (results.cSpeed >= target.cSpeed)
+ && (results.dSpeed >= target.dSpeed)
+ && (results.cMem <= target.cMem)
+ && (!g_optmode || results.cSize <= g_lvltarget.cSize);
+}
+
+/* hill climbing value for part 1 */
+/* Scoring here is a linear reward for all set constraints normalized between 0 to 1
+ * (with 0 at 0 and 1 being fully fulfilling the constraint), summed with a logarithmic
+ * bonus to exceeding the constraint value. We also give linear ratio for compression ratio.
+ * The constant factors are experimental.
+ */
+static double resultScore(const BMK_benchResult_t res, const size_t srcSize, const constraint_t target) {
+ double cs = 0., ds = 0., rt, cm = 0.;
+ const double r1 = 1, r2 = 0.1, rtr = 0.5;
+ double ret;
+ if(target.cSpeed) { cs = res.cSpeed / (double)target.cSpeed; }
+ if(target.dSpeed) { ds = res.dSpeed / (double)target.dSpeed; }
+ if(target.cMem != (U32)-1) { cm = (double)target.cMem / res.cMem; }
+ rt = ((double)srcSize / res.cSize);
+
+ ret = (MIN(1, cs) + MIN(1, ds) + MIN(1, cm))*r1 + rt * rtr +
+ (MAX(0, log(cs))+ MAX(0, log(ds))+ MAX(0, log(cm))) * r2;
+
+ return ret;
+}
+
+/* calculates normalized squared euclidean distance of result1 if it is in the first quadrant relative to lvlRes */
+static double resultDistLvl(const BMK_benchResult_t result1, const BMK_benchResult_t lvlRes) {
+ double normalizedCSpeedGain1 = (result1.cSpeed / lvlRes.cSpeed) - 1;
+ double normalizedRatioGain1 = ((double)lvlRes.cSize / result1.cSize) - 1;
+ if(normalizedRatioGain1 < 0 || normalizedCSpeedGain1 < 0) {
+ return 0.0;
+ }
+ return normalizedRatioGain1 * g_ratioMultiplier + normalizedCSpeedGain1;
+}
+
+/* return true if r2 strictly better than r1 */
+static int compareResultLT(const BMK_benchResult_t result1, const BMK_benchResult_t result2, const constraint_t target, size_t srcSize) {
+ if(feasible(result1, target) && feasible(result2, target)) {
+ if(g_optmode) {
+ return resultDistLvl(result1, g_lvltarget) < resultDistLvl(result2, g_lvltarget);
+ } else {
+ return (result1.cSize > result2.cSize) || (result1.cSize == result2.cSize && result2.cSpeed > result1.cSpeed)
+ || (result1.cSize == result2.cSize && result2.cSpeed == result1.cSpeed && result2.dSpeed > result1.dSpeed);
+ }
+ }
+ return feasible(result2, target) || (!feasible(result1, target) && (resultScore(result1, srcSize, target) < resultScore(result2, srcSize, target)));
+}
+
+static constraint_t relaxTarget(constraint_t target) {
+ target.cMem = (U32)-1;
+ target.cSpeed *= ((double)g_strictness) / 100;
+ target.dSpeed *= ((double)g_strictness) / 100;
+ return target;
+}
+
+static void optimizerAdjustInput(paramValues_t* pc, const size_t maxBlockSize) {
+ varInds_t v;
+ for(v = 0; v < NUM_PARAMS; v++) {
+ if(pc->vals[v] != PARAM_UNSET) {
+ U32 newval = MIN(MAX(pc->vals[v], mintable[v]), maxtable[v]);
+ if(newval != pc->vals[v]) {
+ pc->vals[v] = newval;
+ DISPLAY("Warning: parameter %s not in valid range, adjusting to ", g_paramNames[v]); displayParamVal(stderr, v, newval, 0); DISPLAY("\n");
}
- roundClock = UTIL_clockSpanMicro(roundStart);
-
- if ((double)roundClock < fastestD * SEC_TO_MICRO * nbLoops) fastestD = ((double)roundClock / SEC_TO_MICRO) / nbLoops;
- DISPLAY("\r");
- DISPLAY("%1u-%s : %9u -> ", loopNb, name, (U32)srcSize);
- DISPLAY("%9u (%4.3f),%7.1f MB/s, ", (U32)cSize, ratio, (double)srcSize / fastestC / 1000000.);
- DISPLAY("%7.1f MB/s", (double)srcSize / fastestD / 1000000.);
- resultPtr->dSpeed = (double)srcSize / fastestD;
-
- /* CRC Checking */
- { U64 const crcCheck = XXH64(resultBuffer, srcSize, 0);
- if (crcOrig!=crcCheck) {
- unsigned u;
- unsigned eBlockSize = (unsigned)(MIN(65536*2, blockSize));
- DISPLAY("\n!!! WARNING !!! Invalid Checksum : %x != %x\n", (unsigned)crcOrig, (unsigned)crcCheck);
- for (u=0; u<srcSize; u++) {
- if (((const BYTE*)srcBuffer)[u] != ((BYTE*)resultBuffer)[u]) {
- printf("Decoding error at pos %u (block %u, pos %u) \n", u, u / eBlockSize, u % eBlockSize);
- break;
- } }
- break;
- } }
-#endif
- } }
+ }
+ }
- /* End cleaning */
- DISPLAY("\r");
- free(compressedBuffer);
- free(resultBuffer);
- return 0;
+ if(pc->vals[wlog_ind] != PARAM_UNSET) {
+
+ U32 sshb = maxBlockSize > 1 ? ZSTD_highbit32((U32)(maxBlockSize-1)) + 1 : 1;
+ /* edge case of highBit not working for 0 */
+
+ if(maxBlockSize < (1ULL << 31) && sshb + 1 < pc->vals[wlog_ind]) {
+ U32 adjust = MAX(mintable[wlog_ind], sshb);
+ if(adjust != pc->vals[wlog_ind]) {
+ pc->vals[wlog_ind] = adjust;
+ DISPLAY("Warning: windowLog larger than src/block size, adjusted to %u\n", pc->vals[wlog_ind]);
+ }
+ }
+ }
+
+ if(pc->vals[wlog_ind] != PARAM_UNSET && pc->vals[clog_ind] != PARAM_UNSET) {
+ U32 maxclog;
+ if(pc->vals[strt_ind] == PARAM_UNSET || pc->vals[strt_ind] >= (U32)ZSTD_btlazy2) {
+ maxclog = pc->vals[wlog_ind] + 1;
+ } else {
+ maxclog = pc->vals[wlog_ind];
+ }
+
+ if(pc->vals[clog_ind] > maxclog) {
+ pc->vals[clog_ind] = maxclog;
+ DISPLAY("Warning: chainlog too much larger than windowLog size, adjusted to %u\n", pc->vals[clog_ind]);
+ }
+ }
+
+ if(pc->vals[wlog_ind] != PARAM_UNSET && pc->vals[hlog_ind] != PARAM_UNSET) {
+ if(pc->vals[wlog_ind] + 1 < pc->vals[hlog_ind]) {
+ pc->vals[hlog_ind] = pc->vals[wlog_ind] + 1;
+ DISPLAY("Warning: hashlog too much larger than windowLog size, adjusted to %u\n", pc->vals[hlog_ind]);
+ }
+ }
+
+ if(pc->vals[slog_ind] != PARAM_UNSET && pc->vals[clog_ind] != PARAM_UNSET) {
+ if(pc->vals[slog_ind] > pc->vals[clog_ind]) {
+ pc->vals[clog_ind] = pc->vals[slog_ind];
+ DISPLAY("Warning: searchLog larger than chainLog, adjusted to %u\n", pc->vals[slog_ind]);
+ }
+ }
}
+static int redundantParams(const paramValues_t paramValues, const constraint_t target, const size_t maxBlockSize) {
+ return
+ (ZSTD_estimateCStreamSize_usingCParams(pvalsToCParams(paramValues)) > (size_t)target.cMem) /* Uses too much memory */
+ || ((1ULL << (paramValues.vals[wlog_ind] - 1)) >= maxBlockSize && paramValues.vals[wlog_ind] != mintable[wlog_ind]) /* wlog too much bigger than src size */
+ || (paramValues.vals[clog_ind] > (paramValues.vals[wlog_ind] + (paramValues.vals[strt_ind] > ZSTD_btlazy2))) /* chainLog larger than windowLog*/
+ || (paramValues.vals[slog_ind] > paramValues.vals[clog_ind]) /* searchLog larger than chainLog */
+ || (paramValues.vals[hlog_ind] > paramValues.vals[wlog_ind] + 1); /* hashLog larger than windowLog + 1 */
-const char* g_stratName[ZSTD_btultra+1] = {
- "(none) ", "ZSTD_fast ", "ZSTD_dfast ",
- "ZSTD_greedy ", "ZSTD_lazy ", "ZSTD_lazy2 ",
- "ZSTD_btlazy2 ", "ZSTD_btopt ", "ZSTD_btultra "};
+}
+
+/*-************************************
+* Display Functions
+**************************************/
-static void BMK_printWinner(FILE* f, U32 cLevel, BMK_result_t result, ZSTD_compressionParameters params, size_t srcSize)
+static void BMK_translateAdvancedParams(FILE* f, const paramValues_t params) {
+ varInds_t v;
+ int first = 1;
+ fprintf(f,"--zstd=");
+ for (v = 0; v < NUM_PARAMS; v++) {
+ if (g_silenceParams[v]) { continue; }
+ if (!first) { fprintf(f, ","); }
+ fprintf(f,"%s=", g_paramNames[v]);
+
+ if (v == strt_ind) { fprintf(f,"%u", params.vals[v]); }
+ else { displayParamVal(f, v, params.vals[v], 0); }
+ first = 0;
+ }
+ fprintf(f, "\n");
+}
+
+static void BMK_displayOneResult(FILE* f, winnerInfo_t res, const size_t srcSize)
{
- DISPLAY("\r%79s\r", "");
- fprintf(f," {%3u,%3u,%3u,%3u,%3u,%3u, %s }, ",
- params.windowLog, params.chainLog, params.hashLog, params.searchLog, params.searchLength,
- params.targetLength, g_stratName[(U32)(params.strategy)]);
- fprintf(f,
- "/* level %2u */ /* R:%5.3f at %5.1f MB/s - %5.1f MB/s */\n",
- cLevel, (double)srcSize / result.cSize, result.cSpeed / 1000000., result.dSpeed / 1000000.);
+ varInds_t v;
+ int first = 1;
+ res.params = cParamUnsetMin(res.params);
+ fprintf(f, " {");
+ for (v = 0; v < NUM_PARAMS; v++) {
+ if (g_silenceParams[v]) { continue; }
+ if (!first) { fprintf(f, ","); }
+ displayParamVal(f, v, res.params.vals[v], 3);
+ first = 0;
+ }
+
+ { double const ratio = res.result.cSize ?
+ (double)srcSize / res.result.cSize : 0;
+ double const cSpeedMBps = (double)res.result.cSpeed / MB_UNIT;
+ double const dSpeedMBps = (double)res.result.dSpeed / MB_UNIT;
+
+ fprintf(f, " }, /* R:%5.3f at %5.1f MB/s - %5.1f MB/s */\n",
+ ratio, cSpeedMBps, dSpeedMBps);
+ }
}
+/* Writes to f the results of a parameter benchmark */
+/* when used with --optimize, will only print results better than previously discovered */
+static void BMK_printWinner(FILE* f, const int cLevel, const BMK_benchResult_t result, const paramValues_t params, const size_t srcSize)
+{
+ char lvlstr[15] = "Custom Level";
+ winnerInfo_t w;
+ w.params = params;
+ w.result = result;
-static double g_cSpeedTarget[NB_LEVELS_TRACKED] = { 0. }; /* NB_LEVELS_TRACKED : checked at main() */
+ fprintf(f, "\r%79s\r", "");
-typedef struct {
- BMK_result_t result;
- ZSTD_compressionParameters params;
-} winnerInfo_t;
+ if(cLevel != CUSTOM_LEVEL) {
+ snprintf(lvlstr, 15, " Level %2d ", cLevel);
+ }
+
+ if(TIMED) {
+ const U64 time = UTIL_clockSpanNano(g_time);
+ const U64 minutes = time / (60ULL * TIMELOOP_NANOSEC);
+ fprintf(f, "%1lu:%2lu:%05.2f - ", (unsigned long) minutes / 60,(unsigned long) minutes % 60, (double)(time - minutes * TIMELOOP_NANOSEC * 60ULL)/TIMELOOP_NANOSEC);
+ }
+
+ fprintf(f, "/* %s */ ", lvlstr);
+ BMK_displayOneResult(f, w, srcSize);
+}
-static void BMK_printWinners2(FILE* f, const winnerInfo_t* winners, size_t srcSize)
+/* comparison function: */
+/* strictly better, strictly worse, equal, speed-side adv, size-side adv */
+#define WORSE_RESULT 0
+#define BETTER_RESULT 1
+#define ERROR_RESULT 2
+
+#define SPEED_RESULT 4
+#define SIZE_RESULT 5
+/* maybe have epsilon-eq to limit table size? */
+static int speedSizeCompare(const BMK_benchResult_t r1, const BMK_benchResult_t r2) {
+ if(r1.cSpeed < r2.cSpeed) {
+ if(r1.cSize >= r2.cSize) {
+ return BETTER_RESULT;
+ }
+ return SPEED_RESULT; /* r2 is smaller but not faster. */
+ } else {
+ if(r1.cSize <= r2.cSize) {
+ return WORSE_RESULT;
+ }
+ return SIZE_RESULT; /* r2 is faster but not smaller */
+ }
+}
+
+/* 0 for insertion, 1 for no insert */
+/* maintain invariant speedSizeCompare(n, n->next) = SPEED_RESULT */
+static int insertWinner(const winnerInfo_t w, const constraint_t targetConstraints) {
+ BMK_benchResult_t r = w.result;
+ winner_ll_node* cur_node = g_winners;
+ /* first node to insert */
+ if(!feasible(r, targetConstraints)) {
+ return 1;
+ }
+
+ if(g_winners == NULL) {
+ winner_ll_node* first_node = malloc(sizeof(winner_ll_node));
+ if(first_node == NULL) {
+ return 1;
+ }
+ first_node->next = NULL;
+ first_node->res = w;
+ g_winners = first_node;
+ return 0;
+ }
+
+ while(cur_node->next != NULL) {
+ switch(speedSizeCompare(cur_node->res.result, r)) {
+ case WORSE_RESULT:
+ {
+ return 1; /* never insert if better */
+ }
+ case BETTER_RESULT:
+ {
+ winner_ll_node* tmp;
+ cur_node->res = cur_node->next->res;
+ tmp = cur_node->next;
+ cur_node->next = cur_node->next->next;
+ free(tmp);
+ break;
+ }
+ case SIZE_RESULT:
+ {
+ cur_node = cur_node->next;
+ break;
+ }
+ case SPEED_RESULT: /* insert after first size result, then return */
+ {
+ winner_ll_node* newnode = malloc(sizeof(winner_ll_node));
+ if(newnode == NULL) {
+ return 1;
+ }
+ newnode->res = cur_node->res;
+ cur_node->res = w;
+ newnode->next = cur_node->next;
+ cur_node->next = newnode;
+ return 0;
+ }
+ }
+
+ }
+
+ assert(cur_node->next == NULL);
+ switch(speedSizeCompare(cur_node->res.result, r)) {
+ case WORSE_RESULT:
+ {
+ return 1; /* never insert if better */
+ }
+ case BETTER_RESULT:
+ {
+ cur_node->res = w;
+ return 0;
+ }
+ case SIZE_RESULT:
+ {
+ winner_ll_node* newnode = malloc(sizeof(winner_ll_node));
+ if(newnode == NULL) {
+ return 1;
+ }
+ newnode->res = w;
+ newnode->next = NULL;
+ cur_node->next = newnode;
+ return 0;
+ }
+ case SPEED_RESULT: /* insert before first size result, then return */
+ {
+ winner_ll_node* newnode = malloc(sizeof(winner_ll_node));
+ if(newnode == NULL) {
+ return 1;
+ }
+ newnode->res = cur_node->res;
+ cur_node->res = w;
+ newnode->next = cur_node->next;
+ cur_node->next = newnode;
+ return 0;
+ }
+ default:
+ return 1;
+ }
+}
+
+static void BMK_printWinnerOpt(FILE* f, const U32 cLevel, const BMK_benchResult_t result, const paramValues_t params, const constraint_t targetConstraints, const size_t srcSize)
+{
+ /* global winner used for constraints */
+ /* cSize, cSpeed, dSpeed, cMem */
+ static winnerInfo_t g_winner = { { (size_t)-1LL, 0, 0, (size_t)-1LL }, { { PARAM_UNSET, PARAM_UNSET, PARAM_UNSET, PARAM_UNSET, PARAM_UNSET, PARAM_UNSET, PARAM_UNSET, PARAM_UNSET } } };
+ if(DEBUG || compareResultLT(g_winner.result, result, targetConstraints, srcSize) || g_displayLevel >= 4) {
+ if(DEBUG && compareResultLT(g_winner.result, result, targetConstraints, srcSize)) {
+ DISPLAY("New Winner: \n");
+ }
+
+ if(g_displayLevel >= 2) { BMK_printWinner(f, cLevel, result, params, srcSize); }
+
+ if(compareResultLT(g_winner.result, result, targetConstraints, srcSize)) {
+ if(g_displayLevel >= 1) { BMK_translateAdvancedParams(f, params); }
+ g_winner.result = result;
+ g_winner.params = params;
+ }
+ }
+
+ if(g_optmode && g_optimizer && (DEBUG || g_displayLevel == 3)) {
+ winnerInfo_t w;
+ winner_ll_node* n;
+ w.result = result;
+ w.params = params;
+ insertWinner(w, targetConstraints);
+
+ if(!DEBUG) { fprintf(f, "\033c"); }
+ fprintf(f, "\n");
+
+ /* the table */
+ fprintf(f, "================================\n");
+ for(n = g_winners; n != NULL; n = n->next) {
+ BMK_displayOneResult(f, n->res, srcSize);
+ }
+ fprintf(f, "================================\n");
+ fprintf(f, "Level Bounds: R: > %.3f AND C: < %.1f MB/s \n\n",
+ (double)srcSize / g_lvltarget.cSize, (double)g_lvltarget.cSpeed / MB_UNIT);
+
+
+ fprintf(f, "Overall Winner: \n");
+ BMK_displayOneResult(f, g_winner, srcSize);
+ BMK_translateAdvancedParams(f, g_winner.params);
+
+ fprintf(f, "Latest BMK: \n");\
+ BMK_displayOneResult(f, w, srcSize);
+ }
+}
+
+static void BMK_printWinners2(FILE* f, const winnerInfo_t* winners, const size_t srcSize)
{
int cLevel;
fprintf(f, "\n /* Proposed configurations : */ \n");
fprintf(f, " /* W, C, H, S, L, T, strat */ \n");
- for (cLevel=0; cLevel <= ZSTD_maxCLevel(); cLevel++)
+ for (cLevel=0; cLevel <= NB_LEVELS_TRACKED; cLevel++)
BMK_printWinner(f, cLevel, winners[cLevel].result, winners[cLevel].params, srcSize);
}
-static void BMK_printWinners(FILE* f, const winnerInfo_t* winners, size_t srcSize)
+static void BMK_printWinners(FILE* f, const winnerInfo_t* winners, const size_t srcSize)
{
fseek(f, 0, SEEK_SET);
BMK_printWinners2(f, winners, srcSize);
@@ -350,41 +876,837 @@ static void BMK_printWinners(FILE* f, const winnerInfo_t* winners, size_t srcSiz
BMK_printWinners2(stdout, winners, srcSize);
}
-static int BMK_seed(winnerInfo_t* winners, const ZSTD_compressionParameters params,
- const void* srcBuffer, size_t srcSize,
- ZSTD_CCtx* ctx)
+
+/*-*******************************************************
+* Functions to Benchmark
+*********************************************************/
+
+typedef struct {
+ ZSTD_CCtx* cctx;
+ const void* dictBuffer;
+ size_t dictBufferSize;
+ int cLevel;
+ const paramValues_t* comprParams;
+} BMK_initCCtxArgs;
+
+static size_t local_initCCtx(void* payload) {
+ const BMK_initCCtxArgs* ag = (const BMK_initCCtxArgs*)payload;
+ varInds_t i;
+ ZSTD_CCtx_reset(ag->cctx);
+ ZSTD_CCtx_resetParameters(ag->cctx);
+ ZSTD_CCtx_setParameter(ag->cctx, ZSTD_p_compressionLevel, ag->cLevel);
+
+ for(i = 0; i < NUM_PARAMS; i++) {
+ if(ag->comprParams->vals[i] != PARAM_UNSET)
+ ZSTD_CCtx_setParameter(ag->cctx, cctxSetParamTable[i], ag->comprParams->vals[i]);
+ }
+ ZSTD_CCtx_loadDictionary(ag->cctx, ag->dictBuffer, ag->dictBufferSize);
+
+ return 0;
+}
+
+typedef struct {
+ ZSTD_DCtx* dctx;
+ const void* dictBuffer;
+ size_t dictBufferSize;
+} BMK_initDCtxArgs;
+
+static size_t local_initDCtx(void* payload) {
+ const BMK_initDCtxArgs* ag = (const BMK_initDCtxArgs*)payload;
+ ZSTD_DCtx_reset(ag->dctx);
+ ZSTD_DCtx_loadDictionary(ag->dctx, ag->dictBuffer, ag->dictBufferSize);
+ return 0;
+}
+
+/* additional argument is just the context */
+static size_t local_defaultCompress(
+ const void* srcBuffer, size_t srcSize,
+ void* dstBuffer, size_t dstSize,
+ void* addArgs) {
+ size_t moreToFlush = 1;
+ ZSTD_CCtx* ctx = (ZSTD_CCtx*)addArgs;
+ ZSTD_inBuffer in;
+ ZSTD_outBuffer out;
+ in.src = srcBuffer;
+ in.size = srcSize;
+ in.pos = 0;
+ out.dst = dstBuffer;
+ out.size = dstSize;
+ out.pos = 0;
+ assert(dstSize == ZSTD_compressBound(srcSize)); /* specific to this version, which is only used in paramgrill */
+ while (moreToFlush) {
+ if(out.pos == out.size) {
+ return (size_t)-ZSTD_error_dstSize_tooSmall;
+ }
+ moreToFlush = ZSTD_compress_generic(ctx, &out, &in, ZSTD_e_end);
+ if (ZSTD_isError(moreToFlush)) {
+ return moreToFlush;
+ }
+ }
+ return out.pos;
+}
+
+/* additional argument is just the context */
+static size_t local_defaultDecompress(
+ const void* srcBuffer, size_t srcSize,
+ void* dstBuffer, size_t dstSize,
+ void* addArgs) {
+ size_t moreToFlush = 1;
+ ZSTD_DCtx* dctx = (ZSTD_DCtx*)addArgs;
+ ZSTD_inBuffer in;
+ ZSTD_outBuffer out;
+ in.src = srcBuffer;
+ in.size = srcSize;
+ in.pos = 0;
+ out.dst = dstBuffer;
+ out.size = dstSize;
+ out.pos = 0;
+ while (moreToFlush) {
+ if(out.pos == out.size) {
+ return (size_t)-ZSTD_error_dstSize_tooSmall;
+ }
+ moreToFlush = ZSTD_decompress_generic(dctx,
+ &out, &in);
+ if (ZSTD_isError(moreToFlush)) {
+ return moreToFlush;
+ }
+ }
+ return out.pos;
+
+}
+
+/*-************************************
+* Data Initialization Functions
+**************************************/
+
+typedef struct {
+ void* srcBuffer;
+ size_t srcSize;
+ const void** srcPtrs;
+ size_t* srcSizes;
+ void** dstPtrs;
+ size_t* dstCapacities;
+ size_t* dstSizes;
+ void** resPtrs;
+ size_t* resSizes;
+ size_t nbBlocks;
+ size_t maxBlockSize;
+} buffers_t;
+
+typedef struct {
+ size_t dictSize;
+ void* dictBuffer;
+ ZSTD_CCtx* cctx;
+ ZSTD_DCtx* dctx;
+} contexts_t;
+
+static void freeNonSrcBuffers(const buffers_t b) {
+ free(b.srcPtrs);
+ free(b.srcSizes);
+
+ if(b.dstPtrs != NULL) {
+ free(b.dstPtrs[0]);
+ }
+ free(b.dstPtrs);
+ free(b.dstCapacities);
+ free(b.dstSizes);
+
+ if(b.resPtrs != NULL) {
+ free(b.resPtrs[0]);
+ }
+ free(b.resPtrs);
+ free(b.resSizes);
+}
+
+static void freeBuffers(const buffers_t b) {
+ if(b.srcPtrs != NULL) {
+ free(b.srcBuffer);
+ }
+ freeNonSrcBuffers(b);
+}
+
+/* srcBuffer will be freed by freeBuffers now */
+static int createBuffersFromMemory(buffers_t* buff, void * srcBuffer, const size_t nbFiles,
+ const size_t* fileSizes)
{
- BMK_result_t testResult;
+ size_t pos = 0, n, blockSize;
+ U32 maxNbBlocks, blockNb = 0;
+ buff->srcSize = 0;
+ for(n = 0; n < nbFiles; n++) {
+ buff->srcSize += fileSizes[n];
+ }
+
+ if(buff->srcSize == 0) {
+ DISPLAY("No data to bench\n");
+ return 1;
+ }
+
+ blockSize = g_blockSize ? g_blockSize : buff->srcSize;
+ maxNbBlocks = (U32) ((buff->srcSize + (blockSize-1)) / blockSize) + (U32)nbFiles;
+
+ buff->srcPtrs = (const void**)calloc(maxNbBlocks, sizeof(void*));
+ buff->srcSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
+
+ buff->dstPtrs = (void**)calloc(maxNbBlocks, sizeof(void*));
+ buff->dstCapacities = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
+ buff->dstSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
+
+ buff->resPtrs = (void**)calloc(maxNbBlocks, sizeof(void*));
+ buff->resSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
+
+ if(!buff->srcPtrs || !buff->srcSizes || !buff->dstPtrs || !buff->dstCapacities || !buff->dstSizes || !buff->resPtrs || !buff->resSizes) {
+ DISPLAY("alloc error\n");
+ freeNonSrcBuffers(*buff);
+ return 1;
+ }
+
+ buff->srcBuffer = srcBuffer;
+ buff->srcPtrs[0] = (const void*)buff->srcBuffer;
+ buff->dstPtrs[0] = malloc(ZSTD_compressBound(buff->srcSize) + (maxNbBlocks * 1024));
+ buff->resPtrs[0] = malloc(buff->srcSize);
+
+ if(!buff->dstPtrs[0] || !buff->resPtrs[0]) {
+ DISPLAY("alloc error\n");
+ freeNonSrcBuffers(*buff);
+ return 1;
+ }
+
+ for(n = 0; n < nbFiles; n++) {
+ size_t pos_end = pos + fileSizes[n];
+ for(; pos < pos_end; blockNb++) {
+ buff->srcPtrs[blockNb] = (const void*)((char*)srcBuffer + pos);
+ buff->srcSizes[blockNb] = blockSize;
+ pos += blockSize;
+ }
+
+ if(fileSizes[n] > 0) { buff->srcSizes[blockNb - 1] = ((fileSizes[n] - 1) % blockSize) + 1; }
+ pos = pos_end;
+ }
+
+ buff->dstCapacities[0] = ZSTD_compressBound(buff->srcSizes[0]);
+ buff->dstSizes[0] = buff->dstCapacities[0];
+ buff->resSizes[0] = buff->srcSizes[0];
+ buff->maxBlockSize = buff->srcSizes[0];
+
+ for(n = 1; n < blockNb; n++) {
+ buff->dstPtrs[n] = ((char*)buff->dstPtrs[n-1]) + buff->dstCapacities[n-1];
+ buff->resPtrs[n] = ((char*)buff->resPtrs[n-1]) + buff->resSizes[n-1];
+ buff->dstCapacities[n] = ZSTD_compressBound(buff->srcSizes[n]);
+ buff->dstSizes[n] = buff->dstCapacities[n];
+ buff->resSizes[n] = buff->srcSizes[n];
+
+ buff->maxBlockSize = MAX(buff->maxBlockSize, buff->srcSizes[n]);
+ }
+
+ buff->nbBlocks = blockNb;
+
+ return 0;
+}
+
+/* allocates buffer's arguments. returns success / failuere */
+static int createBuffers(buffers_t* buff, const char* const * const fileNamesTable,
+ size_t nbFiles) {
+ size_t pos = 0;
+ size_t n;
+ size_t totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, (U32)nbFiles);
+ size_t benchedSize = MIN(BMK_findMaxMem(totalSizeToLoad * 3) / 3, totalSizeToLoad);
+ size_t* fileSizes = calloc(sizeof(size_t), nbFiles);
+ void* srcBuffer = NULL;
+ int ret = 0;
+
+ if(!totalSizeToLoad || !benchedSize) {
+ ret = 1;
+ DISPLAY("Nothing to Bench\n");
+ goto _cleanUp;
+ }
+
+ srcBuffer = malloc(benchedSize);
+
+ if(!fileSizes || !srcBuffer) {
+ ret = 1;
+ goto _cleanUp;
+ }
+
+ for(n = 0; n < nbFiles; n++) {
+ FILE* f;
+ U64 fileSize = UTIL_getFileSize(fileNamesTable[n]);
+ if (UTIL_isDirectory(fileNamesTable[n])) {
+ DISPLAY("Ignoring %s directory... \n", fileNamesTable[n]);
+ continue;
+ }
+ if (fileSize == UTIL_FILESIZE_UNKNOWN) {
+ DISPLAY("Cannot evaluate size of %s, ignoring ... \n", fileNamesTable[n]);
+ continue;
+ }
+ f = fopen(fileNamesTable[n], "rb");
+ if (f==NULL) {
+ DISPLAY("impossible to open file %s\n", fileNamesTable[n]);
+ fclose(f);
+ ret = 10;
+ goto _cleanUp;
+ }
+
+ DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[n]);
+
+ if (fileSize + pos > benchedSize) fileSize = benchedSize - pos, nbFiles=n; /* buffer too small - stop after this file */
+ {
+ char* buffer = (char*)(srcBuffer);
+ size_t const readSize = fread((buffer)+pos, 1, (size_t)fileSize, f);
+ fclose(f);
+ if (readSize != (size_t)fileSize) {
+ DISPLAY("could not read %s", fileNamesTable[n]);
+ ret = 1;
+ goto _cleanUp;
+ }
+
+ fileSizes[n] = readSize;
+ pos += readSize;
+ }
+ }
+
+ ret = createBuffersFromMemory(buff, srcBuffer, nbFiles, fileSizes);
+
+_cleanUp:
+ if(ret) { free(srcBuffer); }
+ free(fileSizes);
+ return ret;
+}
+
+static void freeContexts(const contexts_t ctx) {
+ free(ctx.dictBuffer);
+ ZSTD_freeCCtx(ctx.cctx);
+ ZSTD_freeDCtx(ctx.dctx);
+}
+
+static int createContexts(contexts_t* ctx, const char* dictFileName) {
+ FILE* f;
+ size_t readSize;
+ ctx->cctx = ZSTD_createCCtx();
+ ctx->dctx = ZSTD_createDCtx();
+ assert(ctx->cctx != NULL);
+ assert(ctx->dctx != NULL);
+
+ if(dictFileName == NULL) {
+ ctx->dictSize = 0;
+ ctx->dictBuffer = NULL;
+ return 0;
+ }
+ { U64 const dictFileSize = UTIL_getFileSize(dictFileName);
+ assert(dictFileSize != UTIL_FILESIZE_UNKNOWN);
+ ctx->dictSize = dictFileSize;
+ assert((U64)ctx->dictSize == dictFileSize); /* check overflow */
+ }
+ ctx->dictBuffer = malloc(ctx->dictSize);
+
+ f = fopen(dictFileName, "rb");
+
+ if (f==NULL) {
+ DISPLAY("unable to open file\n");
+ freeContexts(*ctx);
+ return 1;
+ }
+
+ if (ctx->dictSize > 64 MB || !(ctx->dictBuffer)) {
+ DISPLAY("dictionary too large\n");
+ fclose(f);
+ freeContexts(*ctx);
+ return 1;
+ }
+ readSize = fread(ctx->dictBuffer, 1, ctx->dictSize, f);
+ fclose(f);
+ if (readSize != ctx->dictSize) {
+ DISPLAY("unable to read file\n");
+ freeContexts(*ctx);
+ return 1;
+ }
+ return 0;
+}
+
+/*-************************************
+* Optimizer Memoization Functions
+**************************************/
+
+/* return: new length */
+/* keep old array, will need if iter over strategy. */
+/* prunes useless params */
+static size_t sanitizeVarArray(varInds_t* varNew, const size_t varLength, const varInds_t* varArray, const ZSTD_strategy strat) {
+ size_t i, j = 0;
+ for(i = 0; i < varLength; i++) {
+ if( !((varArray[i] == clog_ind && strat == ZSTD_fast)
+ || (varArray[i] == slog_ind && strat == ZSTD_fast)
+ || (varArray[i] == slog_ind && strat == ZSTD_dfast)
+ || (varArray[i] == tlen_ind && strat != ZSTD_btopt && strat != ZSTD_btultra && strat != ZSTD_fast))) {
+ varNew[j] = varArray[i];
+ j++;
+ }
+ }
+ return j;
+}
+
+/* res should be NUM_PARAMS size */
+/* constructs varArray from paramValues_t style parameter */
+/* pass in using dict. */
+static size_t variableParams(const paramValues_t paramConstraints, varInds_t* res, const int usingDictionary) {
+ varInds_t i;
+ size_t j = 0;
+ for(i = 0; i < NUM_PARAMS; i++) {
+ if(paramConstraints.vals[i] == PARAM_UNSET) {
+ if(i == fadt_ind && !usingDictionary) continue; /* don't use fadt if no dictionary */
+ res[j] = i; j++;
+ }
+ }
+ return j;
+}
+
+/* length of memo table given free variables */
+static size_t memoTableLen(const varInds_t* varyParams, const size_t varyLen) {
+ size_t arrayLen = 1;
+ size_t i;
+ for(i = 0; i < varyLen; i++) {
+ if(varyParams[i] == strt_ind) continue; /* strategy separated by table */
+ arrayLen *= rangetable[varyParams[i]];
+ }
+ return arrayLen;
+}
+
+/* returns unique index in memotable of compression parameters */
+static unsigned memoTableIndDirect(const paramValues_t* ptr, const varInds_t* varyParams, const size_t varyLen) {
+ size_t i;
+ unsigned ind = 0;
+ for(i = 0; i < varyLen; i++) {
+ varInds_t v = varyParams[i];
+ if(v == strt_ind) continue; /* exclude strategy from memotable */
+ ind *= rangetable[v]; ind += (unsigned)invRangeMap(v, ptr->vals[v]);
+ }
+ return ind;
+}
+
+static size_t memoTableGet(const memoTable_t* memoTableArray, const paramValues_t p) {
+ const memoTable_t mt = memoTableArray[p.vals[strt_ind]];
+ switch(mt.tableType) {
+ case directMap:
+ return mt.table[memoTableIndDirect(&p, mt.varArray, mt.varLen)];
+ case xxhashMap:
+ return mt.table[(XXH64(&p.vals, sizeof(U32) * NUM_PARAMS, 0) >> 3) % mt.tableLen];
+ case noMemo:
+ return 0;
+ }
+ return 0; /* should never happen, stop compiler warnings */
+}
+
+static void memoTableSet(const memoTable_t* memoTableArray, const paramValues_t p, const BYTE value) {
+ const memoTable_t mt = memoTableArray[p.vals[strt_ind]];
+ switch(mt.tableType) {
+ case directMap:
+ mt.table[memoTableIndDirect(&p, mt.varArray, mt.varLen)] = value; break;
+ case xxhashMap:
+ mt.table[(XXH64(&p.vals, sizeof(U32) * NUM_PARAMS, 0) >> 3) % mt.tableLen] = value; break;
+ case noMemo:
+ break;
+ }
+}
+
+/* frees all allocated memotables */
+static void freeMemoTableArray(memoTable_t* const mtAll) {
+ int i;
+ if(mtAll == NULL) { return; }
+ for(i = 1; i <= (int)ZSTD_btultra; i++) {
+ free(mtAll[i].table);
+ }
+ free(mtAll);
+}
+
+/* inits memotables for all (including mallocs), all strategies */
+/* takes unsanitized varyParams */
+static memoTable_t* createMemoTableArray(const paramValues_t p, const varInds_t* const varyParams, const size_t varyLen, const U32 memoTableLog) {
+ memoTable_t* mtAll = (memoTable_t*)calloc(sizeof(memoTable_t),(ZSTD_btultra + 1));
+ ZSTD_strategy i, stratMin = ZSTD_fast, stratMax = ZSTD_btultra;
+
+ if(mtAll == NULL) {
+ return NULL;
+ }
+
+ for(i = 1; i <= (int)ZSTD_btultra; i++) {
+ mtAll[i].varLen = sanitizeVarArray(mtAll[i].varArray, varyLen, varyParams, i);
+ }
+
+ /* no memoization */
+ if(memoTableLog == 0) {
+ for(i = 1; i <= (int)ZSTD_btultra; i++) {
+ mtAll[i].tableType = noMemo;
+ mtAll[i].table = NULL;
+ mtAll[i].tableLen = 0;
+ }
+ return mtAll;
+ }
+
+
+ if(p.vals[strt_ind] != PARAM_UNSET) {
+ stratMin = p.vals[strt_ind];
+ stratMax = p.vals[strt_ind];
+ }
+
+
+ for(i = stratMin; i <= stratMax; i++) {
+ size_t mtl = memoTableLen(mtAll[i].varArray, mtAll[i].varLen);
+ mtAll[i].tableType = directMap;
+
+ if(memoTableLog != PARAM_UNSET && mtl > (1ULL << memoTableLog)) { /* use hash table */ /* provide some option to only use hash tables? */
+ mtAll[i].tableType = xxhashMap;
+ mtl = (1ULL << memoTableLog);
+ }
+
+ mtAll[i].table = (BYTE*)calloc(sizeof(BYTE), mtl);
+ mtAll[i].tableLen = mtl;
+
+ if(mtAll[i].table == NULL) {
+ freeMemoTableArray(mtAll);
+ return NULL;
+ }
+ }
+
+ return mtAll;
+}
+
+/* Sets pc to random unmeasured set of parameters */
+/* specifiy strategy */
+static void randomConstrainedParams(paramValues_t* pc, const memoTable_t* memoTableArray, const ZSTD_strategy st)
+{
+ size_t j;
+ const memoTable_t mt = memoTableArray[st];
+ pc->vals[strt_ind] = st;
+ for(j = 0; j < mt.tableLen; j++) {
+ int i;
+ for(i = 0; i < NUM_PARAMS; i++) {
+ varInds_t v = mt.varArray[i];
+ if(v == strt_ind) continue;
+ pc->vals[v] = rangeMap(v, FUZ_rand(&g_rand) % rangetable[v]);
+ }
+
+ if(!(memoTableGet(memoTableArray, *pc))) break; /* only pick unpicked params. */
+ }
+}
+
+/*-************************************
+* Benchmarking Functions
+**************************************/
+
+/* Replicate functionality of benchMemAdvanced, but with pre-split src / dst buffers */
+/* The purpose is so that sufficient information is returned so that a decompression call to benchMemInvertible is possible */
+/* BMK_benchMemAdvanced(srcBuffer,srcSize, dstBuffer, dstSize, fileSizes, nbFiles, 0, &cParams, dictBuffer, dictSize, ctx, dctx, 0, "File", &adv); */
+/* nbSeconds used in same way as in BMK_advancedParams_t */
+/* if in decodeOnly, then srcPtr's will be compressed blocks, and uncompressedBlocks will be written to dstPtrs */
+/* dictionary nullable, nothing else though. */
+/* note : it would be better if this function was in bench.c, sharing code with benchMemAdvanced(), since it's technically a part of it */
+static BMK_benchOutcome_t
+BMK_benchMemInvertible( buffers_t buf, contexts_t ctx,
+ int cLevel, const paramValues_t* comprParams,
+ BMK_mode_t mode, unsigned nbSeconds)
+{
+ U32 i;
+ BMK_benchResult_t bResult;
+ const void *const *const srcPtrs = (const void *const *const)buf.srcPtrs;
+ size_t const *const srcSizes = buf.srcSizes;
+ void** const dstPtrs = buf.dstPtrs;
+ size_t const *const dstCapacities = buf.dstCapacities;
+ size_t* const dstSizes = buf.dstSizes;
+ void** const resPtrs = buf.resPtrs;
+ size_t const *const resSizes = buf.resSizes;
+ const void* dictBuffer = ctx.dictBuffer;
+ const size_t dictBufferSize = ctx.dictSize;
+ const size_t nbBlocks = buf.nbBlocks;
+ const size_t srcSize = buf.srcSize;
+ ZSTD_CCtx* cctx = ctx.cctx;
+ ZSTD_DCtx* dctx = ctx.dctx;
+
+ /* init */
+ memset(&bResult, 0, sizeof(bResult));
+
+ /* warmimg up memory */
+ for (i = 0; i < buf.nbBlocks; i++) {
+ if (mode != BMK_decodeOnly) {
+ RDG_genBuffer(dstPtrs[i], dstCapacities[i], 0.10, 0.50, 1);
+ } else {
+ RDG_genBuffer(resPtrs[i], resSizes[i], 0.10, 0.50, 1);
+ }
+ }
+
+ /* Bench */
+ {
+ /* init args */
+ int compressionCompleted = (mode == BMK_decodeOnly);
+ int decompressionCompleted = (mode == BMK_compressOnly);
+ BMK_timedFnState_t* timeStateCompress = BMK_createTimedFnState(nbSeconds * 1000, 1000);
+ BMK_timedFnState_t* timeStateDecompress = BMK_createTimedFnState(nbSeconds * 1000, 1000);
+ BMK_initCCtxArgs cctxprep;
+ BMK_initDCtxArgs dctxprep;
+ cctxprep.cctx = cctx;
+ cctxprep.dictBuffer = dictBuffer;
+ cctxprep.dictBufferSize = dictBufferSize;
+ cctxprep.cLevel = cLevel;
+ cctxprep.comprParams = comprParams;
+ dctxprep.dctx = dctx;
+ dctxprep.dictBuffer = dictBuffer;
+ dctxprep.dictBufferSize = dictBufferSize;
+
+ assert(timeStateCompress != NULL);
+ assert(timeStateDecompress != NULL);
+ while(!compressionCompleted) {
+ BMK_runOutcome_t const cOutcome = BMK_benchTimedFn(timeStateCompress,
+ &local_defaultCompress, cctx,
+ &local_initCCtx, &cctxprep,
+ nbBlocks,
+ srcPtrs, srcSizes,
+ dstPtrs, dstCapacities,
+ dstSizes);
+
+ if (!BMK_isSuccessful_runOutcome(cOutcome)) {
+ BMK_benchOutcome_t bOut;
+ memset(&bOut, 0, sizeof(bOut));
+ bOut.tag = 1; /* should rather be a function or a constant */
+ BMK_freeTimedFnState(timeStateCompress);
+ BMK_freeTimedFnState(timeStateDecompress);
+ return bOut;
+ }
+ { BMK_runTime_t const rResult = BMK_extract_runTime(cOutcome);
+ bResult.cSpeed = (srcSize * TIMELOOP_NANOSEC) / rResult.nanoSecPerRun;
+ bResult.cSize = rResult.sumOfReturn;
+ }
+ compressionCompleted = BMK_isCompleted_TimedFn(timeStateCompress);
+ }
+
+ while (!decompressionCompleted) {
+ BMK_runOutcome_t const dOutcome = BMK_benchTimedFn(timeStateDecompress,
+ &local_defaultDecompress, dctx,
+ &local_initDCtx, &dctxprep,
+ nbBlocks,
+ (const void* const*)dstPtrs, dstSizes,
+ resPtrs, resSizes,
+ NULL);
+
+ if (!BMK_isSuccessful_runOutcome(dOutcome)) {
+ BMK_benchOutcome_t bOut;
+ memset(&bOut, 0, sizeof(bOut));
+ bOut.tag = 1; /* should rather be a function or a constant */
+ BMK_freeTimedFnState(timeStateCompress);
+ BMK_freeTimedFnState(timeStateDecompress);
+ return bOut;
+ }
+ { BMK_runTime_t const rResult = BMK_extract_runTime(dOutcome);
+ bResult.dSpeed = (srcSize * TIMELOOP_NANOSEC) / rResult.nanoSecPerRun;
+ }
+ decompressionCompleted = BMK_isCompleted_TimedFn(timeStateDecompress);
+ }
+
+ BMK_freeTimedFnState(timeStateCompress);
+ BMK_freeTimedFnState(timeStateDecompress);
+ }
+
+ /* Bench */
+ bResult.cMem = (1 << (comprParams->vals[wlog_ind])) + ZSTD_sizeof_CCtx(cctx);
+
+ { BMK_benchOutcome_t bOut;
+ bOut.tag = 0;
+ bOut.internal_never_use_directly = bResult; /* should be a function */
+ return bOut;
+ }
+}
+
+static int BMK_benchParam ( BMK_benchResult_t* resultPtr,
+ buffers_t buf, contexts_t ctx,
+ paramValues_t cParams)
+{
+ BMK_benchOutcome_t const outcome = BMK_benchMemInvertible(buf, ctx,
+ BASE_CLEVEL, &cParams,
+ BMK_both, 3);
+ int const success = BMK_isSuccessful_benchOutcome(outcome);
+ if (!success) return 1;
+ *resultPtr = BMK_extract_benchResult(outcome);
+ return 0;
+}
+
+
+#define CBENCHMARK(conditional, resultvar, tmpret, mode, sec) { \
+ if(conditional) { \
+ BMK_benchOutcome_t const outcome = BMK_benchMemInvertible(buf, ctx, BASE_CLEVEL, &cParams, mode, sec); \
+ if (!BMK_isSuccessful_benchOutcome(outcome)) { \
+ DEBUGOUTPUT("Benchmarking failed\n"); \
+ return ERROR_RESULT; \
+ } \
+ { BMK_benchResult_t const tmpResult = BMK_extract_benchResult(outcome); \
+ if (mode != BMK_decodeOnly) { \
+ resultvar.cSpeed = tmpResult.cSpeed; \
+ resultvar.cSize = tmpResult.cSize; \
+ resultvar.cMem = tmpResult.cMem; \
+ } \
+ if (mode != BMK_compressOnly) { resultvar.dSpeed = tmpResult.dSpeed; } \
+ } } \
+}
+
+/* Benchmarking which stops when we are sufficiently sure the solution is infeasible / worse than the winner */
+#define VARIANCE 1.2
+static int allBench(BMK_benchResult_t* resultPtr,
+ const buffers_t buf, const contexts_t ctx,
+ const paramValues_t cParams,
+ const constraint_t target,
+ BMK_benchResult_t* winnerResult, int feas)
+{
+ BMK_benchResult_t benchres;
+ double uncertaintyConstantC = 3., uncertaintyConstantD = 3.;
+ double winnerRS;
+
+ BMK_benchOutcome_t const outcome = BMK_benchMemInvertible(buf, ctx, BASE_CLEVEL, &cParams, BMK_both, 2);
+ if (!BMK_isSuccessful_benchOutcome(outcome)) {
+ DEBUGOUTPUT("Benchmarking failed \n");
+ return ERROR_RESULT;
+ }
+ benchres = BMK_extract_benchResult(outcome);
+
+ winnerRS = resultScore(*winnerResult, buf.srcSize, target);
+ DEBUGOUTPUT("WinnerScore: %f \n ", winnerRS);
+
+ *resultPtr = benchres;
+
+ /* anything with worse ratio in feas is definitely worse, discard */
+ if(feas && benchres.cSize < winnerResult->cSize && !g_optmode) {
+ return WORSE_RESULT;
+ }
+
+ /* calculate uncertainty in compression / decompression runs */
+ if (benchres.cSpeed) {
+ U64 const loopDurationC = (((U64)buf.srcSize * TIMELOOP_NANOSEC) / benchres.cSpeed);
+ uncertaintyConstantC = ((loopDurationC + (double)(2 * g_clockGranularity))/loopDurationC);
+ }
+
+ if (benchres.dSpeed) {
+ U64 const loopDurationD = (((U64)buf.srcSize * TIMELOOP_NANOSEC) / benchres.dSpeed);
+ uncertaintyConstantD = ((loopDurationD + (double)(2 * g_clockGranularity))/loopDurationD);
+ }
+
+ /* optimistic assumption of benchres */
+ { BMK_benchResult_t resultMax = benchres;
+ resultMax.cSpeed *= uncertaintyConstantC * VARIANCE;
+ resultMax.dSpeed *= uncertaintyConstantD * VARIANCE;
+
+ /* disregard infeasible results in feas mode */
+ /* disregard if resultMax < winner in infeas mode */
+ if((feas && !feasible(resultMax, target)) ||
+ (!feas && (winnerRS > resultScore(resultMax, buf.srcSize, target)))) {
+ return WORSE_RESULT;
+ }
+ }
+
+ /* compare by resultScore when in infeas */
+ /* compare by compareResultLT when in feas */
+ if((!feas && (resultScore(benchres, buf.srcSize, target) > resultScore(*winnerResult, buf.srcSize, target))) ||
+ (feas && (compareResultLT(*winnerResult, benchres, target, buf.srcSize))) ) {
+ return BETTER_RESULT;
+ } else {
+ return WORSE_RESULT;
+ }
+}
+
+
+#define INFEASIBLE_THRESHOLD 200
+/* Memoized benchmarking, won't benchmark anything which has already been benchmarked before. */
+static int benchMemo(BMK_benchResult_t* resultPtr,
+ const buffers_t buf, const contexts_t ctx,
+ const paramValues_t cParams,
+ const constraint_t target,
+ BMK_benchResult_t* winnerResult, memoTable_t* const memoTableArray,
+ const int feas) {
+ static int bmcount = 0;
+ int res;
+
+ if ( memoTableGet(memoTableArray, cParams) >= INFEASIBLE_THRESHOLD
+ || redundantParams(cParams, target, buf.maxBlockSize) ) {
+ return WORSE_RESULT;
+ }
+
+ res = allBench(resultPtr, buf, ctx, cParams, target, winnerResult, feas);
+
+ if(DEBUG && !(bmcount % 250)) {
+ DISPLAY("Count: %d\n", bmcount);
+ bmcount++;
+ }
+ BMK_printWinnerOpt(stdout, CUSTOM_LEVEL, *resultPtr, cParams, target, buf.srcSize);
+
+ if(res == BETTER_RESULT || feas) {
+ memoTableSet(memoTableArray, cParams, 255); /* what happens if collisions are frequent */
+ }
+ return res;
+}
+
+
+typedef struct {
+ U64 cSpeed_min;
+ U64 dSpeed_min;
+ U32 windowLog_max;
+ ZSTD_strategy strategy_max;
+} level_constraints_t;
+
+static level_constraints_t g_level_constraint[NB_LEVELS_TRACKED+1];
+
+static void BMK_init_level_constraints(int bytePerSec_level1)
+{
+ assert(NB_LEVELS_TRACKED >= ZSTD_maxCLevel());
+ memset(g_level_constraint, 0, sizeof(g_level_constraint));
+ g_level_constraint[1].cSpeed_min = bytePerSec_level1;
+ g_level_constraint[1].dSpeed_min = 0.;
+ g_level_constraint[1].windowLog_max = 19;
+ g_level_constraint[1].strategy_max = ZSTD_fast;
+
+ /* establish speed objectives (relative to level 1) */
+ { int l;
+ for (l=2; l<=NB_LEVELS_TRACKED; l++) {
+ g_level_constraint[l].cSpeed_min = (g_level_constraint[l-1].cSpeed_min * 49) / 64;
+ g_level_constraint[l].dSpeed_min = 0.;
+ g_level_constraint[l].windowLog_max = (l<20) ? 23 : l+5; /* only --ultra levels >= 20 can use windowlog > 23 */
+ g_level_constraint[l].strategy_max = (l<19) ? ZSTD_btopt : ZSTD_btultra; /* level 19 is allowed to use btultra */
+ } }
+}
+
+static int BMK_seed(winnerInfo_t* winners, const paramValues_t params,
+ const buffers_t buf, const contexts_t ctx)
+{
+ BMK_benchResult_t testResult;
int better = 0;
int cLevel;
- BMK_benchParam(&testResult, srcBuffer, srcSize, ctx, params);
+ BMK_benchParam(&testResult, buf, ctx, params);
+
- for (cLevel = 1; cLevel <= ZSTD_maxCLevel(); cLevel++) {
- if (testResult.cSpeed < g_cSpeedTarget[cLevel])
+ for (cLevel = 1; cLevel <= NB_LEVELS_TRACKED; cLevel++) {
+ if (testResult.cSpeed < g_level_constraint[cLevel].cSpeed_min)
+ continue; /* not fast enough for this level */
+ if (testResult.dSpeed < g_level_constraint[cLevel].dSpeed_min)
continue; /* not fast enough for this level */
+ if (params.vals[wlog_ind] > g_level_constraint[cLevel].windowLog_max)
+ continue; /* too much memory for this level */
+ if (params.vals[strt_ind] > g_level_constraint[cLevel].strategy_max)
+ continue; /* forbidden strategy for this level */
if (winners[cLevel].result.cSize==0) {
/* first solution for this cLevel */
winners[cLevel].result = testResult;
winners[cLevel].params = params;
- BMK_printWinner(stdout, cLevel, testResult, params, srcSize);
+ BMK_printWinner(stdout, cLevel, testResult, params, buf.srcSize);
better = 1;
continue;
}
if ((double)testResult.cSize <= ((double)winners[cLevel].result.cSize * (1. + (0.02 / cLevel))) ) {
/* Validate solution is "good enough" */
- double W_ratio = (double)srcSize / testResult.cSize;
- double O_ratio = (double)srcSize / winners[cLevel].result.cSize;
+ double W_ratio = (double)buf.srcSize / testResult.cSize;
+ double O_ratio = (double)buf.srcSize / winners[cLevel].result.cSize;
double W_ratioNote = log (W_ratio);
double O_ratioNote = log (O_ratio);
- size_t W_DMemUsed = (1 << params.windowLog) + (16 KB);
- size_t O_DMemUsed = (1 << winners[cLevel].params.windowLog) + (16 KB);
+ size_t W_DMemUsed = (1 << params.vals[wlog_ind]) + (16 KB);
+ size_t O_DMemUsed = (1 << winners[cLevel].params.vals[wlog_ind]) + (16 KB);
double W_DMemUsed_note = W_ratioNote * ( 40 + 9*cLevel) - log((double)W_DMemUsed);
double O_DMemUsed_note = O_ratioNote * ( 40 + 9*cLevel) - log((double)O_DMemUsed);
- size_t W_CMemUsed = (1 << params.windowLog) + ZSTD_estimateCCtxSize_usingCParams(params);
- size_t O_CMemUsed = (1 << winners[cLevel].params.windowLog) + ZSTD_estimateCCtxSize_usingCParams(winners[cLevel].params);
+ size_t W_CMemUsed = (1 << params.vals[wlog_ind]) + ZSTD_estimateCCtxSize_usingCParams(pvalsToCParams(params));
+ size_t O_CMemUsed = (1 << winners[cLevel].params.vals[wlog_ind]) + ZSTD_estimateCCtxSize_usingCParams(pvalsToCParams(winners[cLevel].params));
double W_CMemUsed_note = W_ratioNote * ( 50 + 13*cLevel) - log((double)W_CMemUsed);
double O_CMemUsed_note = O_ratioNote * ( 50 + 13*cLevel) - log((double)O_CMemUsed);
@@ -414,16 +1736,16 @@ static int BMK_seed(winnerInfo_t* winners, const ZSTD_compressionParameters para
/* too large compression speed difference for the compression benefit */
if (W_ratio > O_ratio)
DISPLAY ("Compression Speed : %5.3f @ %4.1f MB/s vs %5.3f @ %4.1f MB/s : not enough for level %i\n",
- W_ratio, testResult.cSpeed / 1000000,
- O_ratio, winners[cLevel].result.cSpeed / 1000000., cLevel);
+ W_ratio, (double)testResult.cSpeed / MB_UNIT,
+ O_ratio, (double)winners[cLevel].result.cSpeed / MB_UNIT, cLevel);
continue;
}
if (W_DSpeed_note < O_DSpeed_note ) {
/* too large decompression speed difference for the compression benefit */
if (W_ratio > O_ratio)
DISPLAY ("Decompression Speed : %5.3f @ %4.1f MB/s vs %5.3f @ %4.1f MB/s : not enough for level %i\n",
- W_ratio, testResult.dSpeed / 1000000.,
- O_ratio, winners[cLevel].result.dSpeed / 1000000., cLevel);
+ W_ratio, (double)testResult.dSpeed / MB_UNIT,
+ O_ratio, (double)winners[cLevel].result.dSpeed / MB_UNIT, cLevel);
continue;
}
@@ -432,7 +1754,7 @@ static int BMK_seed(winnerInfo_t* winners, const ZSTD_compressionParameters para
winners[cLevel].result = testResult;
winners[cLevel].params = params;
- BMK_printWinner(stdout, cLevel, testResult, params, srcSize);
+ BMK_printWinner(stdout, cLevel, testResult, params, buf.srcSize);
better = 1;
} }
@@ -440,409 +1762,683 @@ static int BMK_seed(winnerInfo_t* winners, const ZSTD_compressionParameters para
return better;
}
-
-/* nullified useless params, to ensure count stats */
-static ZSTD_compressionParameters* sanitizeParams(ZSTD_compressionParameters params)
-{
- g_params = params;
- if (params.strategy == ZSTD_fast)
- g_params.chainLog = 0, g_params.searchLog = 0;
- if (params.strategy == ZSTD_dfast)
- g_params.searchLog = 0;
- if (params.strategy != ZSTD_btopt && params.strategy != ZSTD_btultra)
- g_params.targetLength = 0;
- return &g_params;
-}
-
-
-static void paramVariation(ZSTD_compressionParameters* ptr)
-{
- ZSTD_compressionParameters p;
- U32 validated = 0;
- while (!validated) {
- U32 nbChanges = (FUZ_rand(&g_rand) & 3) + 1;
- p = *ptr;
- for ( ; nbChanges ; nbChanges--) {
- const U32 changeID = FUZ_rand(&g_rand) % 14;
- switch(changeID)
- {
- case 0:
- p.chainLog++; break;
- case 1:
- p.chainLog--; break;
- case 2:
- p.hashLog++; break;
- case 3:
- p.hashLog--; break;
- case 4:
- p.searchLog++; break;
- case 5:
- p.searchLog--; break;
- case 6:
- p.windowLog++; break;
- case 7:
- p.windowLog--; break;
- case 8:
- p.searchLength++; break;
- case 9:
- p.searchLength--; break;
- case 10:
- p.strategy = (ZSTD_strategy)(((U32)p.strategy)+1); break;
- case 11:
- p.strategy = (ZSTD_strategy)(((U32)p.strategy)-1); break;
- case 12:
- p.targetLength *= 1 + ((double)(FUZ_rand(&g_rand)&255)) / 256.; break;
- case 13:
- p.targetLength /= 1 + ((double)(FUZ_rand(&g_rand)&255)) / 256.; break;
- }
- }
- validated = !ZSTD_isError(ZSTD_checkCParams(p));
- }
- *ptr = p;
-}
-
+/*-************************************
+* Compression Level Table Generation Functions
+**************************************/
#define PARAMTABLELOG 25
#define PARAMTABLESIZE (1<<PARAMTABLELOG)
#define PARAMTABLEMASK (PARAMTABLESIZE-1)
static BYTE g_alreadyTested[PARAMTABLESIZE] = {0}; /* init to zero */
-#define NB_TESTS_PLAYED(p) \
- g_alreadyTested[(XXH64(sanitizeParams(p), sizeof(p), 0) >> 3) & PARAMTABLEMASK]
-
+static BYTE* NB_TESTS_PLAYED(paramValues_t p) {
+ ZSTD_compressionParameters p2 = pvalsToCParams(sanitizeParams(p));
+ return &g_alreadyTested[(XXH64((void*)&p2, sizeof(p2), 0) >> 3) & PARAMTABLEMASK];
+}
static void playAround(FILE* f, winnerInfo_t* winners,
- ZSTD_compressionParameters params,
- const void* srcBuffer, size_t srcSize,
- ZSTD_CCtx* ctx)
+ paramValues_t p,
+ const buffers_t buf, const contexts_t ctx)
{
- int nbVariations = 0;
+ int nbVariations = 0, i;
UTIL_time_t const clockStart = UTIL_getTime();
while (UTIL_clockSpanMicro(clockStart) < g_maxVariationTime) {
- ZSTD_compressionParameters p = params;
+ BYTE* b;
if (nbVariations++ > g_maxNbVariations) break;
- paramVariation(&p);
+
+ do { for(i = 0; i < 4; i++) { paramVaryOnce(FUZ_rand(&g_rand) % (strt_ind + 1), ((FUZ_rand(&g_rand) & 1) << 1) - 1, &p); } }
+ while(!paramValid(p));
/* exclude faster if already played params */
- if (FUZ_rand(&g_rand) & ((1 << NB_TESTS_PLAYED(p))-1))
+ if (FUZ_rand(&g_rand) & ((1 << *NB_TESTS_PLAYED(p))-1))
continue;
/* test */
- NB_TESTS_PLAYED(p)++;
- if (!BMK_seed(winners, p, srcBuffer, srcSize, ctx)) continue;
+ b = NB_TESTS_PLAYED(p);
+ (*b)++;
+ if (!BMK_seed(winners, p, buf, ctx)) continue;
/* improvement found => search more */
- BMK_printWinners(f, winners, srcSize);
- playAround(f, winners, p, srcBuffer, srcSize, ctx);
+ BMK_printWinners(f, winners, buf.srcSize);
+ playAround(f, winners, p, buf, ctx);
}
}
-
-static ZSTD_compressionParameters randomParams(void)
-{
- ZSTD_compressionParameters p;
- U32 validated = 0;
- while (!validated) {
- /* totally random entry */
- p.chainLog = (FUZ_rand(&g_rand) % (ZSTD_CHAINLOG_MAX+1 - ZSTD_CHAINLOG_MIN)) + ZSTD_CHAINLOG_MIN;
- p.hashLog = (FUZ_rand(&g_rand) % (ZSTD_HASHLOG_MAX+1 - ZSTD_HASHLOG_MIN)) + ZSTD_HASHLOG_MIN;
- p.searchLog = (FUZ_rand(&g_rand) % (ZSTD_SEARCHLOG_MAX+1 - ZSTD_SEARCHLOG_MIN)) + ZSTD_SEARCHLOG_MIN;
- p.windowLog = (FUZ_rand(&g_rand) % (ZSTD_WINDOWLOG_MAX+1 - ZSTD_WINDOWLOG_MIN)) + ZSTD_WINDOWLOG_MIN;
- p.searchLength=(FUZ_rand(&g_rand) % (ZSTD_SEARCHLENGTH_MAX+1 - ZSTD_SEARCHLENGTH_MIN)) + ZSTD_SEARCHLENGTH_MIN;
- p.targetLength=(FUZ_rand(&g_rand) % (512)) + ZSTD_TARGETLENGTH_MIN;
- p.strategy = (ZSTD_strategy) (FUZ_rand(&g_rand) % (ZSTD_btultra +1));
- validated = !ZSTD_isError(ZSTD_checkCParams(p));
- }
- return p;
-}
-
static void BMK_selectRandomStart(
FILE* f, winnerInfo_t* winners,
- const void* srcBuffer, size_t srcSize,
- ZSTD_CCtx* ctx)
+ const buffers_t buf, const contexts_t ctx)
{
- U32 const id = (FUZ_rand(&g_rand) % (ZSTD_maxCLevel()+1));
- if ((id==0) || (winners[id].params.windowLog==0)) {
- /* totally random entry */
- ZSTD_compressionParameters const p = ZSTD_adjustCParams(randomParams(), srcSize, 0);
- playAround(f, winners, p, srcBuffer, srcSize, ctx);
+ U32 const id = FUZ_rand(&g_rand) % (NB_LEVELS_TRACKED+1);
+ if ((id==0) || (winners[id].params.vals[wlog_ind]==0)) {
+ /* use some random entry */
+ paramValues_t const p = adjustParams(cParamsToPVals(pvalsToCParams(randomParams())), /* defaults nonCompression parameters */
+ buf.srcSize, 0);
+ playAround(f, winners, p, buf, ctx);
+ } else {
+ playAround(f, winners, winners[id].params, buf, ctx);
}
- else
- playAround(f, winners, winners[id].params, srcBuffer, srcSize, ctx);
}
-
-static void BMK_benchMem(void* srcBuffer, size_t srcSize)
+static void BMK_benchFullTable(const buffers_t buf, const contexts_t ctx)
{
- ZSTD_CCtx* const ctx = ZSTD_createCCtx();
- ZSTD_compressionParameters params;
- winnerInfo_t winners[NB_LEVELS_TRACKED];
+ paramValues_t params;
+ winnerInfo_t winners[NB_LEVELS_TRACKED+1];
const char* const rfName = "grillResults.txt";
FILE* const f = fopen(rfName, "w");
- const size_t blockSize = g_blockSize ? g_blockSize : srcSize;
/* init */
- if (ctx==NULL) { DISPLAY("ZSTD_createCCtx() failed \n"); exit(1); }
+ assert(g_singleRun==0);
memset(winners, 0, sizeof(winners));
if (f==NULL) { DISPLAY("error opening %s \n", rfName); exit(1); }
- if (g_singleRun) {
- BMK_result_t testResult;
- g_params = ZSTD_adjustCParams(g_params, srcSize, 0);
- BMK_benchParam(&testResult, srcBuffer, srcSize, ctx, g_params);
- DISPLAY("\n");
- return;
- }
-
- if (g_target)
- g_cSpeedTarget[1] = g_target * 1000000;
- else {
+ if (g_target) {
+ BMK_init_level_constraints(g_target * MB_UNIT);
+ } else {
/* baseline config for level 1 */
- BMK_result_t testResult;
- params = ZSTD_getCParams(1, blockSize, 0);
- BMK_benchParam(&testResult, srcBuffer, srcSize, ctx, params);
- g_cSpeedTarget[1] = (testResult.cSpeed * 31) / 32;
- }
-
- /* establish speed objectives (relative to level 1) */
- { int i;
- for (i=2; i<=ZSTD_maxCLevel(); i++)
- g_cSpeedTarget[i] = (g_cSpeedTarget[i-1] * 25) / 32;
+ paramValues_t const l1params = cParamsToPVals(ZSTD_getCParams(1, buf.maxBlockSize, ctx.dictSize));
+ BMK_benchResult_t testResult;
+ BMK_benchParam(&testResult, buf, ctx, l1params);
+ BMK_init_level_constraints((int)((testResult.cSpeed * 31) / 32));
}
/* populate initial solution */
{ const int maxSeeds = g_noSeed ? 1 : ZSTD_maxCLevel();
int i;
for (i=0; i<=maxSeeds; i++) {
- params = ZSTD_getCParams(i, blockSize, 0);
- BMK_seed(winners, params, srcBuffer, srcSize, ctx);
+ params = cParamsToPVals(ZSTD_getCParams(i, buf.maxBlockSize, 0));
+ BMK_seed(winners, params, buf, ctx);
} }
- BMK_printWinners(f, winners, srcSize);
+ BMK_printWinners(f, winners, buf.srcSize);
/* start tests */
- { const time_t grillStart = time(NULL);
+ { const UTIL_time_t grillStart = UTIL_getTime();
do {
- BMK_selectRandomStart(f, winners, srcBuffer, srcSize, ctx);
- } while (BMK_timeSpan(grillStart) < g_grillDuration_s);
+ BMK_selectRandomStart(f, winners, buf, ctx);
+ } while (BMK_timeSpan(grillStart) < g_timeLimit_s);
}
/* end summary */
- BMK_printWinners(f, winners, srcSize);
+ BMK_printWinners(f, winners, buf.srcSize);
DISPLAY("grillParams operations completed \n");
/* clean up*/
fclose(f);
- ZSTD_freeCCtx(ctx);
}
-static int benchSample(void)
+/*-************************************
+* Single Benchmark Functions
+**************************************/
+
+static int benchOnce(const buffers_t buf, const contexts_t ctx, const int cLevel) {
+ BMK_benchResult_t testResult;
+ g_params = adjustParams(overwriteParams(cParamsToPVals(ZSTD_getCParams(cLevel, buf.maxBlockSize, ctx.dictSize)), g_params), buf.maxBlockSize, ctx.dictSize);
+
+ if (BMK_benchParam(&testResult, buf, ctx, g_params)) {
+ DISPLAY("Error during benchmarking\n");
+ return 1;
+ }
+
+ BMK_printWinner(stdout, CUSTOM_LEVEL, testResult, g_params, buf.srcSize);
+
+ return 0;
+}
+
+static int benchSample(double compressibility, int cLevel)
{
- void* origBuff;
- size_t const benchedSize = sampleSize;
- const char* const name = "Sample 10MiB";
+ const char* const name = "Sample 10MB";
+ size_t const benchedSize = 10 MB;
+ void* const srcBuffer = malloc(benchedSize);
+ int ret = 0;
- /* Allocation */
- origBuff = malloc(benchedSize);
- if (!origBuff) { DISPLAY("\nError: not enough memory!\n"); return 12; }
+ buffers_t buf;
+ contexts_t ctx;
- /* Fill buffer */
- RDG_genBuffer(origBuff, benchedSize, g_compressibility, 0.0, 0);
+ if(srcBuffer == NULL) {
+ DISPLAY("Out of Memory\n");
+ return 2;
+ }
+
+ RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
+
+ if(createBuffersFromMemory(&buf, srcBuffer, 1, &benchedSize)) {
+ DISPLAY("Buffer Creation Error\n");
+ free(srcBuffer);
+ return 3;
+ }
+
+ if(createContexts(&ctx, NULL)) {
+ DISPLAY("Context Creation Error\n");
+ freeBuffers(buf);
+ return 1;
+ }
/* bench */
DISPLAY("\r%79s\r", "");
- DISPLAY("using %s %i%%: \n", name, (int)(g_compressibility*100));
- BMK_benchMem(origBuff, benchedSize);
+ DISPLAY("using %s %i%%: \n", name, (int)(compressibility*100));
- free(origBuff);
- return 0;
-}
+ if(g_singleRun) {
+ ret = benchOnce(buf, ctx, cLevel);
+ } else {
+ BMK_benchFullTable(buf, ctx);
+ }
+ freeBuffers(buf);
+ freeContexts(ctx);
-int benchFiles(const char** fileNamesTable, int nbFiles)
+ return ret;
+}
+
+/* benchFiles() :
+ * note: while this function takes a table of filenames,
+ * in practice, only the first filename will be used */
+static int benchFiles(const char** fileNamesTable, int nbFiles,
+ const char* dictFileName, int cLevel)
{
- int fileIdx=0;
-
- /* Loop for each file */
- while (fileIdx<nbFiles) {
- const char* const inFileName = fileNamesTable[fileIdx++];
- FILE* const inFile = fopen( inFileName, "rb" );
- U64 const inFileSize = UTIL_getFileSize(inFileName);
- size_t benchedSize;
- void* origBuff;
-
- /* Check file existence */
- if (inFile==NULL) {
- DISPLAY( "Pb opening %s\n", inFileName);
- return 11;
- }
- if (inFileSize == UTIL_FILESIZE_UNKNOWN) {
- DISPLAY("Pb evaluatin size of %s \n", inFileName);
- fclose(inFile);
- return 11;
- }
+ buffers_t buf;
+ contexts_t ctx;
+ int ret = 0;
- /* Memory allocation */
- benchedSize = BMK_findMaxMem(inFileSize*3) / 3;
- if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
- if (benchedSize < inFileSize)
- DISPLAY("Not enough memory for '%s' full size; testing %i MB only...\n", inFileName, (int)(benchedSize>>20));
- origBuff = malloc(benchedSize);
- if (origBuff==NULL) {
- DISPLAY("\nError: not enough memory!\n");
- fclose(inFile);
- return 12;
- }
+ if (createBuffers(&buf, fileNamesTable, nbFiles)) {
+ DISPLAY("unable to load files\n");
+ return 1;
+ }
- /* Fill input buffer */
- DISPLAY("Loading %s... \r", inFileName);
- { size_t const readSize = fread(origBuff, 1, benchedSize, inFile);
- fclose(inFile);
- if(readSize != benchedSize) {
- DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
- free(origBuff);
- return 13;
- } }
+ if (createContexts(&ctx, dictFileName)) {
+ DISPLAY("unable to load dictionary\n");
+ freeBuffers(buf);
+ return 2;
+ }
- /* bench */
- DISPLAY("\r%79s\r", "");
- DISPLAY("using %s : \n", inFileName);
- BMK_benchMem(origBuff, benchedSize);
+ DISPLAY("\r%79s\r", "");
+ if (nbFiles == 1) {
+ DISPLAY("using %s : \n", fileNamesTable[0]);
+ } else {
+ DISPLAY("using %d Files : \n", nbFiles);
+ }
- /* clean */
- free(origBuff);
+ if (g_singleRun) {
+ ret = benchOnce(buf, ctx, cLevel);
+ } else {
+ BMK_benchFullTable(buf, ctx);
}
- return 0;
+ freeBuffers(buf);
+ freeContexts(ctx);
+ return ret;
}
-static void BMK_translateAdvancedParams(ZSTD_compressionParameters params)
+/*-************************************
+* Local Optimization Functions
+**************************************/
+
+/* One iteration of hill climbing. Specifically, it first tries all
+ * valid parameter configurations w/ manhattan distance 1 and picks the best one
+ * failing that, it progressively tries candidates further and further away (up to #dim + 2)
+ * if it finds a candidate exceeding winnerInfo, it will repeat. Otherwise, it will stop the
+ * current stage of hill climbing.
+ * Each iteration of hill climbing proceeds in 2 'phases'. Phase 1 climbs according to
+ * the resultScore function, which is effectively a linear increase in reward until it reaches
+ * the constraint-satisfying value, it which point any excess results in only logarithmic reward.
+ * This aims to find some constraint-satisfying point.
+ * Phase 2 optimizes in accordance with what the original function sets out to maximize, with
+ * all feasible solutions valued over all infeasible solutions.
+ */
+
+/* sanitize all params here.
+ * all generation after random should be sanitized. (maybe sanitize random)
+ */
+static winnerInfo_t climbOnce(const constraint_t target,
+ memoTable_t* mtAll,
+ const buffers_t buf, const contexts_t ctx,
+ const paramValues_t init)
{
- DISPLAY("--zstd=windowLog=%u,chainLog=%u,hashLog=%u,searchLog=%u,searchLength=%u,targetLength=%u,strategy=%u \n",
- params.windowLog, params.chainLog, params.hashLog, params.searchLog, params.searchLength, params.targetLength, (U32)(params.strategy));
+ /*
+ * cparam - currently considered 'center'
+ * candidate - params to benchmark/results
+ * winner - best option found so far.
+ */
+ paramValues_t cparam = init;
+ winnerInfo_t candidateInfo, winnerInfo;
+ int better = 1;
+ int feas = 0;
+
+ winnerInfo = initWinnerInfo(init);
+ candidateInfo = winnerInfo;
+
+ { winnerInfo_t bestFeasible1 = initWinnerInfo(cparam);
+ DEBUGOUTPUT("Climb Part 1\n");
+ while(better) {
+ int offset;
+ size_t i, dist;
+ const size_t varLen = mtAll[cparam.vals[strt_ind]].varLen;
+ better = 0;
+ DEBUGOUTPUT("Start\n");
+ cparam = winnerInfo.params;
+ candidateInfo.params = cparam;
+ /* all dist-1 candidates */
+ for (i = 0; i < varLen; i++) {
+ for (offset = -1; offset <= 1; offset += 2) {
+ CHECKTIME(winnerInfo);
+ candidateInfo.params = cparam;
+ paramVaryOnce(mtAll[cparam.vals[strt_ind]].varArray[i], offset, &candidateInfo.params);
+
+ if(paramValid(candidateInfo.params)) {
+ int res;
+ res = benchMemo(&candidateInfo.result, buf, ctx,
+ sanitizeParams(candidateInfo.params), target, &winnerInfo.result, mtAll, feas);
+ DEBUGOUTPUT("Res: %d\n", res);
+ if(res == BETTER_RESULT) { /* synonymous with better when called w/ infeasibleBM */
+ winnerInfo = candidateInfo;
+ better = 1;
+ if(compareResultLT(bestFeasible1.result, winnerInfo.result, target, buf.srcSize)) {
+ bestFeasible1 = winnerInfo;
+ }
+ }
+ }
+ }
+ } /* for (i = 0; i < varLen; i++) */
+
+ if(better) {
+ continue;
+ }
+
+ for(dist = 2; dist < varLen + 2; dist++) { /* varLen is # dimensions */
+ for(i = 0; i < (1 << varLen) / varLen + 2; i++) {
+ int res;
+ CHECKTIME(winnerInfo);
+ candidateInfo.params = cparam;
+ /* param error checking already done here */
+ paramVariation(&candidateInfo.params, mtAll, (U32)dist);
+
+ res = benchMemo(&candidateInfo.result,
+ buf, ctx,
+ sanitizeParams(candidateInfo.params), target,
+ &winnerInfo.result, mtAll, feas);
+ DEBUGOUTPUT("Res: %d\n", res);
+ if (res == BETTER_RESULT) { /* synonymous with better in this case*/
+ winnerInfo = candidateInfo;
+ better = 1;
+ if (compareResultLT(bestFeasible1.result, winnerInfo.result, target, buf.srcSize)) {
+ bestFeasible1 = winnerInfo;
+ }
+ break;
+ }
+ }
+
+ if (better) {
+ break;
+ }
+ } /* for(dist = 2; dist < varLen + 2; dist++) */
+
+ if (!better) { /* infeas -> feas -> stop */
+ if (feas) return winnerInfo;
+ feas = 1;
+ better = 1;
+ winnerInfo = bestFeasible1; /* note with change, bestFeasible may not necessarily be feasible, but if one has been benchmarked, it will be. */
+ DEBUGOUTPUT("Climb Part 2\n");
+ }
+ }
+ winnerInfo = bestFeasible1;
+ }
+
+ return winnerInfo;
}
-/* optimizeForSize():
- * targetSpeed : expressed in MB/s */
-int optimizeForSize(const char* inFileName, U32 targetSpeed)
+/* Optimizes for a fixed strategy */
+
+/* flexible parameters: iterations of failed climbing (or if we do non-random, maybe this is when everything is close to visitied)
+ weight more on visit for bad results, less on good results/more on later results / ones with more failures.
+ allocate memoTable here.
+ */
+static winnerInfo_t optimizeFixedStrategy(
+ const buffers_t buf, const contexts_t ctx,
+ const constraint_t target, paramValues_t paramTarget,
+ const ZSTD_strategy strat,
+ memoTable_t* memoTableArray, const int tries) {
+ int i = 0;
+
+ paramValues_t init;
+ winnerInfo_t winnerInfo, candidateInfo;
+ winnerInfo = initWinnerInfo(emptyParams());
+ /* so climb is given the right fixed strategy */
+ paramTarget.vals[strt_ind] = strat;
+ /* to pass ZSTD_checkCParams */
+ paramTarget = cParamUnsetMin(paramTarget);
+
+ init = paramTarget;
+
+ for(i = 0; i < tries; i++) {
+ DEBUGOUTPUT("Restart\n");
+ do { randomConstrainedParams(&init, memoTableArray, strat); } while(redundantParams(init, target, buf.maxBlockSize));
+ candidateInfo = climbOnce(target, memoTableArray, buf, ctx, init);
+ if(compareResultLT(winnerInfo.result, candidateInfo.result, target, buf.srcSize)) {
+ winnerInfo = candidateInfo;
+ BMK_printWinnerOpt(stdout, CUSTOM_LEVEL, winnerInfo.result, winnerInfo.params, target, buf.srcSize);
+ i = 0;
+ continue;
+ }
+ CHECKTIME(winnerInfo);
+ i++;
+ }
+ return winnerInfo;
+}
+
+/* goes best, best-1, best+1, best-2, ... */
+/* return 0 if nothing remaining */
+static int nextStrategy(const int currentStrategy, const int bestStrategy) {
+ if(bestStrategy <= currentStrategy) {
+ int candidate = 2 * bestStrategy - currentStrategy - 1;
+ if(candidate < 1) {
+ candidate = currentStrategy + 1;
+ if(candidate > (int)ZSTD_btultra) {
+ return 0;
+ } else {
+ return candidate;
+ }
+ } else {
+ return candidate;
+ }
+ } else { /* bestStrategy >= currentStrategy */
+ int candidate = 2 * bestStrategy - currentStrategy;
+ if(candidate > (int)ZSTD_btultra) {
+ candidate = currentStrategy - 1;
+ if(candidate < 1) {
+ return 0;
+ } else {
+ return candidate;
+ }
+ } else {
+ return candidate;
+ }
+ }
+}
+
+/* experiment with playing with this and decay value */
+
+/* main fn called when using --optimize */
+/* Does strategy selection by benchmarking default compression levels
+ * then optimizes by strategy, starting with the best one and moving
+ * progressively moving further away by number
+ * args:
+ * fileNamesTable - list of files to benchmark
+ * nbFiles - length of fileNamesTable
+ * dictFileName - name of dictionary file if one, else NULL
+ * target - performance constraints (cSpeed, dSpeed, cMem)
+ * paramTarget - parameter constraints (i.e. restriction search space to where strategy = ZSTD_fast)
+ * cLevel - compression level to exceed (all solutions must be > lvl in cSpeed + ratio)
+ */
+
+static int g_maxTries = 5;
+#define TRY_DECAY 1
+
+static int optimizeForSize(const char* const * const fileNamesTable, const size_t nbFiles, const char* dictFileName, constraint_t target, paramValues_t paramTarget,
+ const int cLevelOpt, const int cLevelRun, const U32 memoTableLog)
{
- FILE* const inFile = fopen( inFileName, "rb" );
- U64 const inFileSize = UTIL_getFileSize(inFileName);
- size_t benchedSize = BMK_findMaxMem(inFileSize*3) / 3;
- void* origBuff;
-
- /* Init */
- if (inFile==NULL) { DISPLAY( "Pb opening %s\n", inFileName); return 11; }
- if (inFileSize == UTIL_FILESIZE_UNKNOWN) {
- DISPLAY("Pb evaluatin size of %s \n", inFileName);
- fclose(inFile);
- return 11;
- }
-
- /* Memory allocation & restrictions */
- if ((U64)benchedSize > inFileSize) benchedSize = (size_t)inFileSize;
- if (benchedSize < inFileSize) {
- DISPLAY("Not enough memory for '%s' \n", inFileName);
- fclose(inFile);
- return 11;
- }
-
- /* Alloc */
- origBuff = malloc(benchedSize);
- if(!origBuff) {
- DISPLAY("\nError: not enough memory!\n");
- fclose(inFile);
- return 12;
- }
-
- /* Fill input buffer */
- DISPLAY("Loading %s... \r", inFileName);
- { size_t const readSize = fread(origBuff, 1, benchedSize, inFile);
- fclose(inFile);
- if(readSize != benchedSize) {
- DISPLAY("\nError: problem reading file '%s' !! \n", inFileName);
- free(origBuff);
- return 13;
- } }
+ varInds_t varArray [NUM_PARAMS];
+ int ret = 0;
+ const size_t varLen = variableParams(paramTarget, varArray, dictFileName != NULL);
+ winnerInfo_t winner = initWinnerInfo(emptyParams());
+ memoTable_t* allMT = NULL;
+ paramValues_t paramBase;
+ contexts_t ctx;
+ buffers_t buf;
+ g_time = UTIL_getTime();
+
+ if(createBuffers(&buf, fileNamesTable, nbFiles)) {
+ DISPLAY("unable to load files\n");
+ return 1;
+ }
+
+ if(createContexts(&ctx, dictFileName)) {
+ DISPLAY("unable to load dictionary\n");
+ freeBuffers(buf);
+ return 2;
+ }
+
+ if(nbFiles == 1) {
+ DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[0]);
+ } else {
+ DISPLAYLEVEL(2, "Loading %lu Files... \r", (unsigned long)nbFiles);
+ }
+
+ /* sanitize paramTarget */
+ optimizerAdjustInput(&paramTarget, buf.maxBlockSize);
+ paramBase = cParamUnsetMin(paramTarget);
+
+ allMT = createMemoTableArray(paramTarget, varArray, varLen, memoTableLog);
+
+ if (!allMT) {
+ DISPLAY("MemoTable Init Error\n");
+ ret = 2;
+ goto _cleanUp;
+ }
+
+ /* default strictnesses */
+ if (g_strictness == PARAM_UNSET) {
+ if(g_optmode) {
+ g_strictness = 100;
+ } else {
+ g_strictness = 90;
+ }
+ } else {
+ if(0 >= g_strictness || g_strictness > 100) {
+ DISPLAY("Strictness Outside of Bounds\n");
+ ret = 4;
+ goto _cleanUp;
+ }
+ }
+
+ /* use level'ing mode instead of normal target mode */
+ if (g_optmode) {
+ winner.params = cParamsToPVals(ZSTD_getCParams(cLevelOpt, buf.maxBlockSize, ctx.dictSize));
+ if(BMK_benchParam(&winner.result, buf, ctx, winner.params)) {
+ ret = 3;
+ goto _cleanUp;
+ }
+
+ g_lvltarget = winner.result;
+ g_lvltarget.cSpeed *= ((double)g_strictness) / 100;
+ g_lvltarget.dSpeed *= ((double)g_strictness) / 100;
+ g_lvltarget.cSize /= ((double)g_strictness) / 100;
+
+ target.cSpeed = (U32)g_lvltarget.cSpeed;
+ target.dSpeed = (U32)g_lvltarget.dSpeed;
+
+ BMK_printWinnerOpt(stdout, cLevelOpt, winner.result, winner.params, target, buf.srcSize);
+ }
+
+ /* Don't want it to return anything worse than the best known result */
+ if (g_singleRun) {
+ BMK_benchResult_t res;
+ g_params = adjustParams(overwriteParams(cParamsToPVals(ZSTD_getCParams(cLevelRun, buf.maxBlockSize, ctx.dictSize)), g_params), buf.maxBlockSize, ctx.dictSize);
+ if (BMK_benchParam(&res, buf, ctx, g_params)) {
+ ret = 45;
+ goto _cleanUp;
+ }
+ if(compareResultLT(winner.result, res, relaxTarget(target), buf.srcSize)) {
+ winner.result = res;
+ winner.params = g_params;
+ }
+ }
/* bench */
- DISPLAY("\r%79s\r", "");
- DISPLAY("optimizing for %s - limit speed %u MB/s \n", inFileName, targetSpeed);
- targetSpeed *= 1000000;
+ DISPLAYLEVEL(2, "\r%79s\r", "");
+ if(nbFiles == 1) {
+ DISPLAYLEVEL(2, "optimizing for %s", fileNamesTable[0]);
+ } else {
+ DISPLAYLEVEL(2, "optimizing for %lu Files", (unsigned long)nbFiles);
+ }
+
+ if(target.cSpeed != 0) { DISPLAYLEVEL(2," - limit compression speed %u MB/s", target.cSpeed >> 20); }
+ if(target.dSpeed != 0) { DISPLAYLEVEL(2, " - limit decompression speed %u MB/s", target.dSpeed >> 20); }
+ if(target.cMem != (U32)-1) { DISPLAYLEVEL(2, " - limit memory %u MB", target.cMem >> 20); }
- { ZSTD_CCtx* const ctx = ZSTD_createCCtx();
- winnerInfo_t winner;
- BMK_result_t candidate;
- const size_t blockSize = g_blockSize ? g_blockSize : benchedSize;
+ DISPLAYLEVEL(2, "\n");
+ findClockGranularity();
- /* init */
- if (ctx==NULL) { DISPLAY("\n ZSTD_createCCtx error \n"); free(origBuff); return 14;}
- memset(&winner, 0, sizeof(winner));
- winner.result.cSize = (size_t)(-1);
+ { paramValues_t CParams;
/* find best solution from default params */
- { const int maxSeeds = g_noSeed ? 1 : ZSTD_maxCLevel();
- int i;
- for (i=1; i<=maxSeeds; i++) {
- ZSTD_compressionParameters const CParams = ZSTD_getCParams(i, blockSize, 0);
- BMK_benchParam(&candidate, origBuff, benchedSize, ctx, CParams);
- if (candidate.cSpeed < targetSpeed)
- break;
- if ( (candidate.cSize < winner.result.cSize)
- | ((candidate.cSize == winner.result.cSize) & (candidate.cSpeed > winner.result.cSpeed)) )
- {
- winner.params = CParams;
- winner.result = candidate;
- BMK_printWinner(stdout, i, winner.result, winner.params, benchedSize);
- } }
+ {
+ /* strategy selection */
+ const int maxSeeds = g_noSeed ? 1 : ZSTD_maxCLevel();
+ DEBUGOUTPUT("Strategy Selection\n");
+ if(paramTarget.vals[strt_ind] == PARAM_UNSET) {
+ BMK_benchResult_t candidate;
+ int i;
+ for (i=1; i<=maxSeeds; i++) {
+ int ec;
+ CParams = overwriteParams(cParamsToPVals(ZSTD_getCParams(i, buf.maxBlockSize, ctx.dictSize)), paramTarget);
+ ec = BMK_benchParam(&candidate, buf, ctx, CParams);
+ BMK_printWinnerOpt(stdout, i, candidate, CParams, target, buf.srcSize);
+
+ if(!ec && compareResultLT(winner.result, candidate, relaxTarget(target), buf.srcSize)) {
+ winner.result = candidate;
+ winner.params = CParams;
+ }
+
+ CHECKTIMEGT(ret, 0, _displayCleanUp); /* if pass time limit, stop */
+ /* if the current params are too slow, just stop. */
+ if(target.cSpeed > candidate.cSpeed * 3 / 2) { break; }
+ }
+
+ BMK_printWinnerOpt(stdout, CUSTOM_LEVEL, winner.result, winner.params, target, buf.srcSize);
+ }
}
- BMK_printWinner(stdout, 99, winner.result, winner.params, benchedSize);
- BMK_translateAdvancedParams(winner.params);
-
- /* start tests */
- { time_t const grillStart = time(NULL);
- do {
- ZSTD_compressionParameters params = winner.params;
- paramVariation(&params);
- if ((FUZ_rand(&g_rand) & 31) == 3) params = randomParams(); /* totally random config to improve search space */
- params = ZSTD_adjustCParams(params, blockSize, 0);
-
- /* exclude faster if already played set of params */
- if (FUZ_rand(&g_rand) & ((1 << NB_TESTS_PLAYED(params))-1)) continue;
-
- /* test */
- NB_TESTS_PLAYED(params)++;
- BMK_benchParam(&candidate, origBuff, benchedSize, ctx, params);
-
- /* improvement found => new winner */
- if ( (candidate.cSpeed > targetSpeed)
- & ( (candidate.cSize < winner.result.cSize)
- | ((candidate.cSize == winner.result.cSize) & (candidate.cSpeed > winner.result.cSpeed)) ) )
+
+ DEBUGOUTPUT("Real Opt\n");
+ /* start 'real' optimization */
+ {
+ int bestStrategy = (int)winner.params.vals[strt_ind];
+ if(paramTarget.vals[strt_ind] == PARAM_UNSET) {
+ int st = bestStrategy;
+ int tries = g_maxTries;
+
{
- winner.params = params;
- winner.result = candidate;
- BMK_printWinner(stdout, 99, winner.result, winner.params, benchedSize);
- BMK_translateAdvancedParams(winner.params);
+ /* one iterations of hill climbing with the level-defined parameters. */
+ winnerInfo_t w1 = climbOnce(target, allMT, buf, ctx, winner.params);
+ if(compareResultLT(winner.result, w1.result, target, buf.srcSize)) {
+ winner = w1;
+ }
+ CHECKTIMEGT(ret, 0, _displayCleanUp);
+ }
+
+ while(st && tries > 0) {
+ winnerInfo_t wc;
+ DEBUGOUTPUT("StrategySwitch: %s\n", g_stratName[st]);
+
+ wc = optimizeFixedStrategy(buf, ctx, target, paramBase, st, allMT, tries);
+
+ if(compareResultLT(winner.result, wc.result, target, buf.srcSize)) {
+ winner = wc;
+ tries = g_maxTries;
+ bestStrategy = st;
+ } else {
+ st = nextStrategy(st, bestStrategy);
+ tries -= TRY_DECAY;
+ }
+ CHECKTIMEGT(ret, 0, _displayCleanUp);
}
- } while (BMK_timeSpan(grillStart) < g_grillDuration_s);
+ } else {
+ winner = optimizeFixedStrategy(buf, ctx, target, paramBase, paramTarget.vals[strt_ind], allMT, g_maxTries);
+ }
+
}
+ /* no solution found */
+ if(winner.result.cSize == (size_t)-1) {
+ ret = 1;
+ DISPLAY("No feasible solution found\n");
+ goto _cleanUp;
+ }
/* end summary */
- BMK_printWinner(stdout, 99, winner.result, winner.params, benchedSize);
- DISPLAY("grillParams size - optimizer completed \n");
+_displayCleanUp:
+ if(g_displayLevel >= 0) { BMK_displayOneResult(stdout, winner, buf.srcSize); }
+ BMK_translateAdvancedParams(stdout, winner.params);
+ DISPLAYLEVEL(1, "grillParams size - optimizer completed \n");
- /* clean up*/
- ZSTD_freeCCtx(ctx);
}
+_cleanUp:
+ freeContexts(ctx);
+ freeBuffers(buf);
+ freeMemoTableArray(allMT);
+ return ret;
+}
- free(origBuff);
- return 0;
+/*-************************************
+* CLI parsing functions
+**************************************/
+
+/** longCommandWArg() :
+ * check if *stringPtr is the same as longCommand.
+ * If yes, @return 1 and advances *stringPtr to the position which immediately follows longCommand.
+ * @return 0 and doesn't modify *stringPtr otherwise.
+ * from zstdcli.c
+ */
+static unsigned longCommandWArg(const char** stringPtr, const char* longCommand)
+{
+ size_t const comSize = strlen(longCommand);
+ int const result = !strncmp(*stringPtr, longCommand, comSize);
+ if (result) *stringPtr += comSize;
+ return result;
+}
+
+static void errorOut(const char* msg)
+{
+ DISPLAY("%s \n", msg); exit(1);
}
+/*! readU32FromChar() :
+ * @return : unsigned integer value read from input in `char` format.
+ * allows and interprets K, KB, KiB, M, MB and MiB suffix.
+ * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
+ * Note : function will exit() program if digit sequence overflows */
+static unsigned readU32FromChar(const char** stringPtr)
+{
+ const char errorMsg[] = "error: numeric value too large";
+ unsigned sign = 1;
+ unsigned result = 0;
+ if(**stringPtr == '-') { sign = (unsigned)-1; (*stringPtr)++; }
+ while ((**stringPtr >='0') && (**stringPtr <='9')) {
+ unsigned const max = (((unsigned)(-1)) / 10) - 1;
+ if (result > max) errorOut(errorMsg);
+ result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
+ }
+ if ((**stringPtr=='K') || (**stringPtr=='M')) {
+ unsigned const maxK = ((unsigned)(-1)) >> 10;
+ if (result > maxK) errorOut(errorMsg);
+ result <<= 10;
+ if (**stringPtr=='M') {
+ if (result > maxK) errorOut(errorMsg);
+ result <<= 10;
+ }
+ (*stringPtr)++; /* skip `K` or `M` */
+ if (**stringPtr=='i') (*stringPtr)++;
+ if (**stringPtr=='B') (*stringPtr)++;
+ }
+ return result * sign;
+}
+
+static double readDoubleFromChar(const char** stringPtr)
+{
+ double result = 0, divide = 10;
+ while ((**stringPtr >='0') && (**stringPtr <='9')) {
+ result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
+ }
+ if(**stringPtr!='.') {
+ return result;
+ }
+ (*stringPtr)++;
+ while ((**stringPtr >='0') && (**stringPtr <='9')) {
+ result += (double)(**stringPtr - '0') / divide, divide *= 10, (*stringPtr)++ ;
+ }
+ return result;
+}
static int usage(const char* exename)
{
@@ -857,12 +2453,16 @@ static int usage(const char* exename)
static int usage_advanced(void)
{
DISPLAY( "\nAdvanced options :\n");
- DISPLAY( " -T# : set level 1 speed objective \n");
- DISPLAY( " -B# : cut input into blocks of size # (default : single block) \n");
- DISPLAY( " -i# : iteration loops [1-9](default : %i) \n", NBLOOPS);
- DISPLAY( " -O# : find Optimized parameters for # MB/s compression speed (default : 0) \n");
- DISPLAY( " -S : Single run \n");
- DISPLAY( " -P# : generated sample compressibility (default : %.1f%%) \n", COMPRESSIBILITY_DEFAULT * 100);
+ DISPLAY( " -T# : set level 1 speed objective \n");
+ DISPLAY( " -B# : cut input into blocks of size # (default : single block) \n");
+ DISPLAY( " --optimize= : same as -O with more verbose syntax (see README.md)\n");
+ DISPLAY( " -S : Single run \n");
+ DISPLAY( " --zstd : Single run, parameter selection same as zstdcli \n");
+ DISPLAY( " -P# : generated sample compressibility (default : %.1f%%) \n", COMPRESSIBILITY_DEFAULT * 100);
+ DISPLAY( " -t# : Caps runtime of operation in seconds (default : %u seconds (%.1f hours)) \n", g_timeLimit_s, (double)g_timeLimit_s / 3600);
+ DISPLAY( " -v : Prints Benchmarking output\n");
+ DISPLAY( " -D : Next argument dictionary file\n");
+ DISPLAY( " -s : Seperate Files\n");
return 0;
}
@@ -873,37 +2473,135 @@ static int badusage(const char* exename)
return 1;
}
+#define PARSE_SUB_ARGS(stringLong, stringShort, variable) { if (longCommandWArg(&argument, stringLong) || longCommandWArg(&argument, stringShort)) { variable = readU32FromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; } }
+/* 1 if successful parse, 0 otherwise */
+static int parse_params(const char** argptr, paramValues_t* pv) {
+ int matched = 0;
+ const char* argOrig = *argptr;
+ varInds_t v;
+ for(v = 0; v < NUM_PARAMS; v++) {
+ if(longCommandWArg(argptr,g_shortParamNames[v]) || longCommandWArg(argptr, g_paramNames[v])) {
+ if(**argptr == '=') {
+ (*argptr)++;
+ pv->vals[v] = readU32FromChar(argptr);
+ matched = 1;
+ break;
+ }
+ }
+ /* reset and try again */
+ *argptr = argOrig;
+ }
+ return matched;
+}
+
+/*-************************************
+* Main
+**************************************/
+
int main(int argc, const char** argv)
{
int i,
filenamesStart=0,
result;
const char* exename=argv[0];
- const char* input_filename=0;
- U32 optimizer = 0;
+ const char* input_filename = NULL;
+ const char* dictFileName = NULL;
U32 main_pause = 0;
- U32 targetSpeed = 0;
+ int cLevelOpt = 0, cLevelRun = 0;
+ int seperateFiles = 0;
+ double compressibility = COMPRESSIBILITY_DEFAULT;
+ U32 memoTableLog = PARAM_UNSET;
+ constraint_t target = { 0, 0, (U32)-1 };
- /* checks */
- if (NB_LEVELS_TRACKED <= ZSTD_maxCLevel()) {
- DISPLAY("Error : NB_LEVELS_TRACKED <= ZSTD_maxCLevel() \n");
- exit(1);
- }
+ paramValues_t paramTarget = emptyParams();
+ g_params = emptyParams();
- /* Welcome message */
- DISPLAY(WELCOME_MESSAGE);
-
- if (argc<1) { badusage(exename); return 1; }
+ assert(argc>=1); /* for exename */
for(i=1; i<argc; i++) {
const char* argument = argv[i];
-
- if(!argument) continue; /* Protection if argument empty */
+ DEBUGOUTPUT("%d: %s\n", i, argument);
+ assert(argument != NULL);
if(!strcmp(argument,"--no-seed")) { g_noSeed = 1; continue; }
+ if (longCommandWArg(&argument, "--optimize=")) {
+ g_optimizer = 1;
+ for ( ; ;) {
+ if(parse_params(&argument, &paramTarget)) { if(argument[0] == ',') { argument++; continue; } else break; }
+ PARSE_SUB_ARGS("compressionSpeed=" , "cSpeed=", target.cSpeed);
+ PARSE_SUB_ARGS("decompressionSpeed=", "dSpeed=", target.dSpeed);
+ PARSE_SUB_ARGS("compressionMemory=" , "cMem=", target.cMem);
+ PARSE_SUB_ARGS("strict=", "stc=", g_strictness);
+ PARSE_SUB_ARGS("maxTries=", "tries=", g_maxTries);
+ PARSE_SUB_ARGS("memoLimitLog=", "memLog=", memoTableLog);
+ if (longCommandWArg(&argument, "level=") || longCommandWArg(&argument, "lvl=")) { cLevelOpt = readU32FromChar(&argument); g_optmode = 1; if (argument[0]==',') { argument++; continue; } else break; }
+ if (longCommandWArg(&argument, "speedForRatio=") || longCommandWArg(&argument, "speedRatio=")) { g_ratioMultiplier = readDoubleFromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
+
+ DISPLAY("invalid optimization parameter \n");
+ return 1;
+ }
+
+ if (argument[0] != 0) {
+ DISPLAY("invalid --optimize= format\n");
+ return 1; /* check the end of string */
+ }
+ continue;
+ } else if (longCommandWArg(&argument, "--zstd=")) {
/* Decode command (note : aggregated commands are allowed) */
- if (argument[0]=='-') {
+ g_singleRun = 1;
+ for ( ; ;) {
+ if(parse_params(&argument, &g_params)) { if(argument[0] == ',') { argument++; continue; } else break; }
+ if (longCommandWArg(&argument, "level=") || longCommandWArg(&argument, "lvl=")) { cLevelRun = readU32FromChar(&argument); g_params = emptyParams(); if (argument[0]==',') { argument++; continue; } else break; }
+
+ DISPLAY("invalid compression parameter \n");
+ return 1;
+ }
+
+ if (argument[0] != 0) {
+ DISPLAY("invalid --zstd= format\n");
+ return 1; /* check the end of string */
+ }
+ continue;
+ /* if not return, success */
+
+ } else if (longCommandWArg(&argument, "--display=")) {
+ /* Decode command (note : aggregated commands are allowed) */
+ memset(g_silenceParams, 1, sizeof(g_silenceParams));
+ for ( ; ;) {
+ int found = 0;
+ varInds_t v;
+ for(v = 0; v < NUM_PARAMS; v++) {
+ if(longCommandWArg(&argument, g_shortParamNames[v]) || longCommandWArg(&argument, g_paramNames[v])) {
+ g_silenceParams[v] = 0;
+ found = 1;
+ }
+ }
+ if(longCommandWArg(&argument, "compressionParameters") || longCommandWArg(&argument, "cParams")) {
+ for(v = 0; v <= strt_ind; v++) {
+ g_silenceParams[v] = 0;
+ }
+ found = 1;
+ }
+
+
+ if(found) {
+ if(argument[0]==',') {
+ continue;
+ } else {
+ break;
+ }
+ }
+ DISPLAY("invalid parameter name parameter \n");
+ return 1;
+ }
+
+ if (argument[0] != 0) {
+ DISPLAY("invalid --display format\n");
+ return 1; /* check the end of string */
+ }
+ continue;
+ } else if (argument[0]=='-') {
argument++;
while (argument[0]!=0) {
@@ -917,114 +2615,110 @@ int main(int argc, const char** argv)
/* Pause at the end (hidden option) */
case 'p': main_pause = 1; argument++; break;
- /* Modify Nb Iterations */
- case 'i':
- argument++;
- if ((argument[0] >='0') & (argument[0] <='9'))
- g_nbIterations = *argument++ - '0';
- break;
-
/* Sample compressibility (when no file provided) */
case 'P':
argument++;
- { U32 proba32 = 0;
- while ((argument[0]>= '0') & (argument[0]<= '9'))
- proba32 = (proba32*10) + (*argument++ - '0');
- g_compressibility = (double)proba32 / 100.;
+ { U32 const proba32 = readU32FromChar(&argument);
+ compressibility = (double)proba32 / 100.;
}
break;
- case 'O':
- argument++;
- optimizer=1;
- targetSpeed = 0;
- while ((*argument >= '0') & (*argument <= '9'))
- targetSpeed = (targetSpeed*10) + (*argument++ - '0');
- break;
-
/* Run Single conf */
case 'S':
g_singleRun = 1;
argument++;
- g_params = ZSTD_getCParams(2, g_blockSize, 0);
for ( ; ; ) {
switch(*argument)
{
case 'w':
- g_params.windowLog = 0;
argument++;
- while ((*argument>= '0') && (*argument<='9'))
- g_params.windowLog *= 10, g_params.windowLog += *argument++ - '0';
+ g_params.vals[wlog_ind] = readU32FromChar(&argument);
continue;
case 'c':
- g_params.chainLog = 0;
argument++;
- while ((*argument>= '0') && (*argument<='9'))
- g_params.chainLog *= 10, g_params.chainLog += *argument++ - '0';
+ g_params.vals[clog_ind] = readU32FromChar(&argument);
continue;
case 'h':
- g_params.hashLog = 0;
argument++;
- while ((*argument>= '0') && (*argument<='9'))
- g_params.hashLog *= 10, g_params.hashLog += *argument++ - '0';
+ g_params.vals[hlog_ind] = readU32FromChar(&argument);
continue;
case 's':
- g_params.searchLog = 0;
argument++;
- while ((*argument>= '0') && (*argument<='9'))
- g_params.searchLog *= 10, g_params.searchLog += *argument++ - '0';
+ g_params.vals[slog_ind] = readU32FromChar(&argument);
continue;
case 'l': /* search length */
- g_params.searchLength = 0;
argument++;
- while ((*argument>= '0') && (*argument<='9'))
- g_params.searchLength *= 10, g_params.searchLength += *argument++ - '0';
+ g_params.vals[slen_ind] = readU32FromChar(&argument);
continue;
case 't': /* target length */
- g_params.targetLength = 0;
argument++;
- while ((*argument>= '0') && (*argument<='9'))
- g_params.targetLength *= 10, g_params.targetLength += *argument++ - '0';
+ g_params.vals[tlen_ind] = readU32FromChar(&argument);
continue;
case 'S': /* strategy */
argument++;
- while ((*argument>= '0') && (*argument<='9'))
- g_params.strategy = (ZSTD_strategy)(*argument++ - '0');
+ g_params.vals[strt_ind] = readU32FromChar(&argument);
+ continue;
+ case 'f': /* forceAttachDict */
+ argument++;
+ g_params.vals[fadt_ind] = readU32FromChar(&argument);
continue;
case 'L':
- { int cLevel = 0;
- argument++;
- while ((*argument>= '0') && (*argument<='9'))
- cLevel *= 10, cLevel += *argument++ - '0';
- g_params = ZSTD_getCParams(cLevel, g_blockSize, 0);
+ { argument++;
+ cLevelRun = readU32FromChar(&argument);
+ g_params = emptyParams();
continue;
}
default : ;
}
break;
}
+
break;
/* target level1 speed objective, in MB/s */
case 'T':
argument++;
- g_target = 0;
- while ((*argument >= '0') && (*argument <= '9'))
- g_target = (g_target*10) + (*argument++ - '0');
+ g_target = readU32FromChar(&argument);
break;
/* cut input into blocks */
case 'B':
- g_blockSize = 0;
argument++;
- while ((*argument >='0') & (*argument <='9'))
- g_blockSize = (g_blockSize*10) + (*argument++ - '0');
- if (*argument=='K') g_blockSize<<=10, argument++; /* allows using KB notation */
- if (*argument=='M') g_blockSize<<=20, argument++;
- if (*argument=='B') argument++;
+ g_blockSize = readU32FromChar(&argument);
DISPLAY("using %u KB block size \n", g_blockSize>>10);
break;
+ /* caps runtime (in seconds) */
+ case 't':
+ argument++;
+ g_timeLimit_s = readU32FromChar(&argument);
+ break;
+
+ case 's':
+ argument++;
+ seperateFiles = 1;
+ break;
+
+ case 'q':
+ while (argument[0] == 'q') { argument++; g_displayLevel--; }
+ break;
+
+ case 'v':
+ while (argument[0] == 'v') { argument++; g_displayLevel++; }
+ break;
+
+ /* load dictionary file (only applicable for optimizer rn) */
+ case 'D':
+ if(i == argc - 1) { /* last argument, return error. */
+ DISPLAY("Dictionary file expected but not given : %d\n", i);
+ return 1;
+ } else {
+ i++;
+ dictFileName = argv[i];
+ argument += strlen(argument);
+ }
+ break;
+
/* Unknown command */
default : return badusage(exename);
}
@@ -1036,13 +2730,34 @@ int main(int argc, const char** argv)
if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
}
- if (filenamesStart==0)
- result = benchSample();
- else {
- if (optimizer)
- result = optimizeForSize(input_filename, targetSpeed);
- else
- result = benchFiles(argv+filenamesStart, argc-filenamesStart);
+ /* Welcome message */
+ DISPLAYLEVEL(2, WELCOME_MESSAGE);
+
+ if (filenamesStart==0) {
+ if (g_optimizer) {
+ DISPLAY("Optimizer Expects File\n");
+ return 1;
+ } else {
+ result = benchSample(compressibility, cLevelRun);
+ }
+ } else {
+ if(seperateFiles) {
+ for(i = 0; i < argc - filenamesStart; i++) {
+ if (g_optimizer) {
+ result = optimizeForSize(argv+filenamesStart + i, 1, dictFileName, target, paramTarget, cLevelOpt, cLevelRun, memoTableLog);
+ if(result) { DISPLAY("Error on File %d", i); return result; }
+ } else {
+ result = benchFiles(argv+filenamesStart + i, 1, dictFileName, cLevelRun);
+ if(result) { DISPLAY("Error on File %d", i); return result; }
+ }
+ }
+ } else {
+ if (g_optimizer) {
+ result = optimizeForSize(argv+filenamesStart, argc-filenamesStart, dictFileName, target, paramTarget, cLevelOpt, cLevelRun, memoTableLog);
+ } else {
+ result = benchFiles(argv+filenamesStart, argc-filenamesStart, dictFileName, cLevelRun);
+ }
+ }
}
if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; }
diff --git a/tests/playTests.sh b/tests/playTests.sh
index 41d8263b6a674..b86a0dc40cb20 100755
--- a/tests/playTests.sh
+++ b/tests/playTests.sh
@@ -48,6 +48,12 @@ fileRoundTripTest() {
$DIFF -q tmp.md5.1 tmp.md5.2
}
+truncateLastByte() {
+ dd bs=1 count=$(($(wc -c < "$1") - 1)) if="$1" status=none
+}
+
+UNAME=$(uname)
+
isTerminal=false
if [ -t 0 ] && [ -t 1 ]
then
@@ -56,7 +62,10 @@ fi
isWindows=false
INTOVOID="/dev/null"
-DEVDEVICE="/dev/zero"
+case "$UNAME" in
+ GNU) DEVDEVICE="/dev/random" ;;
+ *) DEVDEVICE="/dev/zero" ;;
+esac
case "$OS" in
Windows*)
isWindows=true
@@ -65,10 +74,10 @@ case "$OS" in
;;
esac
-UNAME=$(uname)
case "$UNAME" in
Darwin) MD5SUM="md5 -r" ;;
FreeBSD) MD5SUM="gmd5sum" ;;
+ OpenBSD) MD5SUM="md5" ;;
*) MD5SUM="md5sum" ;;
esac
@@ -94,6 +103,7 @@ else
fi
+
$ECHO "\n===> simple tests "
./datagen > tmp
@@ -103,10 +113,15 @@ $ECHO "test : basic decompression"
$ZSTD -df tmp.zst # trivial decompression case (overwrites tmp)
$ECHO "test : too large compression level => auto-fix"
$ZSTD -99 -f tmp # too large compression level, automatic sized down
+$ZSTD -5000000000 -f tmp && die "too large numeric value : must fail"
$ECHO "test : --fast aka negative compression levels"
$ZSTD --fast -f tmp # == -1
$ZSTD --fast=3 -f tmp # == -3
-$ZSTD --fast=200000 -f tmp # == no compression
+$ZSTD --fast=200000 -f tmp # too low compression level, automatic fixed
+$ZSTD --fast=5000000000 -f tmp && die "too large numeric value : must fail"
+$ZSTD -c --fast=0 tmp > $INTOVOID && die "--fast must not accept value 0"
+$ECHO "test : too large numeric argument"
+$ZSTD --fast=9999999999 -f tmp && die "should have refused numeric value"
$ECHO "test : compress to stdout"
$ZSTD tmp -c > tmpCompressed
$ZSTD tmp --stdout > tmpCompressed # long command format
@@ -165,6 +180,8 @@ chmod 400 tmpro.zst
$ZSTD -q tmpro && die "should have refused to overwrite read-only file"
$ZSTD -q -f tmpro
rm -f tmpro tmpro.zst
+
+
$ECHO "test : file removal"
$ZSTD -f --rm tmp
test ! -f tmp # tmp should no longer be present
@@ -175,12 +192,23 @@ $ECHO hello > tmp
$ZSTD tmp -f -o "$DEVDEVICE" 2>tmplog > "$INTOVOID"
grep -v "Refusing to remove non-regular file" tmplog
rm -f tmplog
-$ZSTD tmp -f -o "$INTONULL" 2>&1 | grep -v "Refusing to remove non-regular file"
+$ZSTD tmp -f -o "$INTOVOID" 2>&1 | grep -v "Refusing to remove non-regular file"
$ECHO "test : --rm on stdin"
$ECHO a | $ZSTD --rm > $INTOVOID # --rm should remain silent
rm tmp
$ZSTD -f tmp && die "tmp not present : should have failed"
test ! -f tmp.zst # tmp.zst should not be created
+$ECHO "test : -d -f do not delete destination when source is not present"
+touch tmp # create destination file
+$ZSTD -d -f tmp.zst && die "attempt to decompress a non existing file"
+test -f tmp # destination file should still be present
+$ECHO "test : -f do not delete destination when source is not present"
+rm tmp # erase source file
+touch tmp.zst # create destination file
+$ZSTD -f tmp && die "attempt to compress a non existing file"
+test -f tmp.zst # destination file should still be present
+rm tmp*
+
$ECHO "test : compress multiple files"
$ECHO hello > tmp1
@@ -258,7 +286,7 @@ rm ./*.tmp ./*.zstd
$ECHO "frame concatenation tests completed"
-if [ "$isWindows" = false ] && [ "$UNAME" != 'SunOS' ] ; then
+if [ "$isWindows" = false ] && [ "$UNAME" != 'SunOS' ] && [ "$UNAME" != "OpenBSD" ] ; then
$ECHO "\n**** flush write error test **** "
$ECHO "$ECHO foo | $ZSTD > /dev/full"
@@ -395,28 +423,54 @@ $ECHO "Hello World" > tmp
$ZSTD --train-legacy -q tmp && die "Dictionary training should fail : not enough input source"
./datagen -P0 -g10M > tmp
$ZSTD --train-legacy -q tmp && die "Dictionary training should fail : source is pure noise"
-rm tmp*
+$ECHO "- Test -o before --train"
+rm -f tmpDict dictionary
+$ZSTD -o tmpDict --train *.c ../programs/*.c
+test -f tmpDict
+$ZSTD --train *.c ../programs/*.c
+test -f dictionary
+rm tmp* dictionary
-$ECHO "\n===> cover dictionary builder : advanced options "
+$ECHO "\n===> fastCover dictionary builder : advanced options "
TESTFILE=../programs/zstdcli.c
./datagen > tmpDict
$ECHO "- Create first dictionary"
-$ZSTD --train-cover=k=46,d=8 *.c ../programs/*.c -o tmpDict
+$ZSTD --train-fastcover=k=46,d=8,f=15,split=80 *.c ../programs/*.c -o tmpDict
cp $TESTFILE tmp
$ZSTD -f tmp -D tmpDict
$ZSTD -d tmp.zst -D tmpDict -fo result
$DIFF $TESTFILE result
$ECHO "- Create second (different) dictionary"
-$ZSTD --train-cover=k=56,d=8 *.c ../programs/*.c ../programs/*.h -o tmpDictC
+$ZSTD --train-fastcover=k=56,d=8 *.c ../programs/*.c ../programs/*.h -o tmpDictC
$ZSTD -d tmp.zst -D tmpDictC -fo result && die "wrong dictionary not detected!"
$ECHO "- Create dictionary with short dictID"
-$ZSTD --train-cover=k=46,d=8 *.c ../programs/*.c --dictID=1 -o tmpDict1
+$ZSTD --train-fastcover=k=46,d=8,f=15,split=80 *.c ../programs/*.c --dictID=1 -o tmpDict1
cmp tmpDict tmpDict1 && die "dictionaries should have different ID !"
$ECHO "- Create dictionary with size limit"
-$ZSTD --train-cover=steps=8 *.c ../programs/*.c -o tmpDict2 --maxdict=4K
-rm tmp*
+$ZSTD --train-fastcover=steps=8 *.c ../programs/*.c -o tmpDict2 --maxdict=4K
+$ECHO "- Compare size of dictionary from 90% training samples with 80% training samples"
+$ZSTD --train-fastcover=split=90 -r *.c ../programs/*.c
+$ZSTD --train-fastcover=split=80 -r *.c ../programs/*.c
+$ECHO "- Create dictionary using all samples for both training and testing"
+$ZSTD --train-fastcover=split=100 -r *.c ../programs/*.c
+$ECHO "- Create dictionary using f=16"
+$ZSTD --train-fastcover=f=16 -r *.c ../programs/*.c
+$ECHO "- Create dictionary using accel=2"
+$ZSTD --train-fastcover=accel=2 -r *.c ../programs/*.c
+$ECHO "- Create dictionary using accel=10"
+$ZSTD --train-fastcover=accel=10 -r *.c ../programs/*.c
+$ECHO "- Create dictionary with multithreading"
+$ZSTD --train-fastcover -T4 -r *.c ../programs/*.c
+$ECHO "- Test -o before --train-fastcover"
+rm -f tmpDict dictionary
+$ZSTD -o tmpDict --train-fastcover *.c ../programs/*.c
+test -f tmpDict
+$ZSTD --train-fastcover *.c ../programs/*.c
+test -f dictionary
+rm tmp* dictionary
+
$ECHO "\n===> legacy dictionary builder "
@@ -436,7 +490,13 @@ $ZSTD --train-legacy -s5 *.c ../programs/*.c --dictID=1 -o tmpDict1
cmp tmpDict tmpDict1 && die "dictionaries should have different ID !"
$ECHO "- Create dictionary with size limit"
$ZSTD --train-legacy -s9 *.c ../programs/*.c -o tmpDict2 --maxdict=4K
-rm tmp*
+$ECHO "- Test -o before --train-legacy"
+rm -f tmpDict dictionary
+$ZSTD -o tmpDict --train-legacy *.c ../programs/*.c
+test -f tmpDict
+$ZSTD --train-legacy *.c ../programs/*.c
+test -f dictionary
+rm tmp* dictionary
$ECHO "\n===> integrity tests "
@@ -482,6 +542,12 @@ $ZSTD -bi0 --fast tmp1
$ECHO "with recursive and quiet modes"
$ZSTD -rqi1b1e2 tmp1
+$ECHO "\n===> zstd compatibility tests "
+
+./datagen > tmp
+rm -f tmp.zst
+$ZSTD --format=zstd -f tmp
+test -f tmp.zst
$ECHO "\n===> gzip compatibility tests "
@@ -513,12 +579,18 @@ if [ $GZIPMODE -eq 1 ]; then
$ZSTD -f --format=gzip tmp
$ZSTD -f tmp
cat tmp.gz tmp.zst tmp.gz tmp.zst | $ZSTD -d -f -o tmp
- head -c -1 tmp.gz | $ZSTD -t > $INTOVOID && die "incomplete frame not detected !"
+ truncateLastByte tmp.gz | $ZSTD -t > $INTOVOID && die "incomplete frame not detected !"
rm tmp*
else
$ECHO "gzip mode not supported"
fi
+if [ $GZIPMODE -eq 1 ]; then
+ ./datagen > tmp
+ rm -f tmp.zst
+ $ZSTD --format=gzip --format=zstd -f tmp
+ test -f tmp.zst
+fi
$ECHO "\n===> xz compatibility tests "
@@ -527,16 +599,16 @@ $ZSTD --format=xz -V || LZMAMODE=0
if [ $LZMAMODE -eq 1 ]; then
$ECHO "xz support detected"
XZEXE=1
- xz -V && lzma -V || XZEXE=0
+ xz -Q -V && lzma -Q -V || XZEXE=0
if [ $XZEXE -eq 1 ]; then
$ECHO "Testing zstd xz and lzma support"
./datagen > tmp
$ZSTD --format=lzma -f tmp
$ZSTD --format=xz -f tmp
- xz -t -v tmp.xz
- xz -t -v tmp.lzma
- xz -f -k tmp
- lzma -f -k --lzma1 tmp
+ xz -Q -t -v tmp.xz
+ xz -Q -t -v tmp.lzma
+ xz -Q -f -k tmp
+ lzma -Q -f -k --lzma1 tmp
$ZSTD -d -f -v tmp.xz
$ZSTD -d -f -v tmp.lzma
rm tmp*
@@ -548,13 +620,13 @@ if [ $LZMAMODE -eq 1 ]; then
$ECHO "Testing xz and lzma symlinks"
./datagen > tmp
./xz tmp
- xz -d tmp.xz
+ xz -Q -d tmp.xz
./lzma tmp
- lzma -d tmp.lzma
+ lzma -Q -d tmp.lzma
$ECHO "Testing unxz and unlzma symlinks"
- xz tmp
+ xz -Q tmp
./xz -d tmp.xz
- lzma tmp
+ lzma -Q tmp
./lzma -d tmp.lzma
rm xz unxz lzma unlzma
rm tmp*
@@ -574,8 +646,8 @@ if [ $LZMAMODE -eq 1 ]; then
$ZSTD -f --format=lzma tmp
$ZSTD -f tmp
cat tmp.xz tmp.lzma tmp.zst tmp.lzma tmp.xz tmp.zst | $ZSTD -d -f -o tmp
- head -c -1 tmp.xz | $ZSTD -t > $INTOVOID && die "incomplete frame not detected !"
- head -c -1 tmp.lzma | $ZSTD -t > $INTOVOID && die "incomplete frame not detected !"
+ truncateLastByte tmp.xz | $ZSTD -t > $INTOVOID && die "incomplete frame not detected !"
+ truncateLastByte tmp.lzma | $ZSTD -t > $INTOVOID && die "incomplete frame not detected !"
rm tmp*
else
$ECHO "xz mode not supported"
@@ -611,12 +683,29 @@ if [ $LZ4MODE -eq 1 ]; then
$ZSTD -f --format=lz4 tmp
$ZSTD -f tmp
cat tmp.lz4 tmp.zst tmp.lz4 tmp.zst | $ZSTD -d -f -o tmp
- head -c -1 tmp.lz4 | $ZSTD -t > $INTOVOID && die "incomplete frame not detected !"
+ truncateLastByte tmp.lz4 | $ZSTD -t > $INTOVOID && die "incomplete frame not detected !"
rm tmp*
else
$ECHO "lz4 mode not supported"
fi
+$ECHO "\n===> suffix list test"
+
+! $ZSTD -d tmp.abc 2> tmplg
+
+if [ $GZIPMODE -ne 1 ]; then
+ grep ".gz" tmplg > $INTOVOID && die "Unsupported suffix listed"
+fi
+
+if [ $LZMAMODE -ne 1 ]; then
+ grep ".lzma" tmplg > $INTOVOID && die "Unsupported suffix listed"
+ grep ".xz" tmplg > $INTOVOID && die "Unsupported suffix listed"
+fi
+
+if [ $LZ4MODE -ne 1 ]; then
+ grep ".lz4" tmplg > $INTOVOID && die "Unsupported suffix listed"
+fi
+
$ECHO "\n===> zstd round-trip tests "
roundTripTest
@@ -650,6 +739,25 @@ then
$ECHO "\n===> zstdmt long distance matching round-trip tests "
roundTripTest -g8M "3 --long=24 -T2"
+
+ $ECHO "\n===> ovLog tests "
+ ./datagen -g2MB > tmp
+ refSize=$($ZSTD tmp -6 -c --zstd=wlog=18 | wc -c)
+ ov9Size=$($ZSTD tmp -6 -c --zstd=wlog=18,ovlog=9 | wc -c)
+ ov0Size=$($ZSTD tmp -6 -c --zstd=wlog=18,ovlog=0 | wc -c)
+ if [ $refSize -eq $ov9Size ]; then
+ echo ov9Size should be different from refSize
+ exit 1
+ fi
+ if [ $refSize -eq $ov0Size ]; then
+ echo ov0Size should be different from refSize
+ exit 1
+ fi
+ if [ $ov9Size -ge $ov0Size ]; then
+ echo ov9Size=$ov9Size should be smaller than ov0Size=$ov0Size
+ exit 1
+ fi
+
else
$ECHO "\n===> no multithreading, skipping zstdmt tests "
fi
@@ -673,23 +781,32 @@ $ZSTD -l *.zst
$ZSTD -lv *.zst
$ECHO "\n===> zstd --list/-l error detection tests "
-! $ZSTD -l tmp1 tmp1.zst
-! $ZSTD --list tmp*
-! $ZSTD -lv tmp1*
-! $ZSTD --list -v tmp2 tmp12.zst
+$ZSTD -l tmp1 tmp1.zst && die "-l must fail on non-zstd file"
+$ZSTD --list tmp* && die "-l must fail on non-zstd file"
+$ZSTD -lv tmp1* && die "-l must fail on non-zstd file"
+$ZSTD --list -v tmp2 tmp12.zst && die "-l must fail on non-zstd file"
+
+$ECHO "\n===> zstd --list/-l errors when presented with stdin / no files"
+$ZSTD -l && die "-l must fail on empty list of files"
+$ZSTD -l - && die "-l does not work on stdin"
+$ZSTD -l < tmp1.zst && die "-l does not work on stdin"
+$ZSTD -l - < tmp1.zst && die "-l does not work on stdin"
+$ZSTD -l - tmp1.zst && die "-l does not work on stdin"
+$ZSTD -l - tmp1.zst < tmp1.zst && die "-l does not work on stdin"
+$ZSTD -l tmp1.zst < tmp2.zst # this will check tmp1.zst, but not tmp2.zst, which is not an error : zstd simply doesn't read stdin in this case. It must not error just because stdin is not a tty
$ECHO "\n===> zstd --list/-l test with null files "
./datagen -g0 > tmp5
$ZSTD tmp5
$ZSTD -l tmp5.zst
-! $ZSTD -l tmp5*
+$ZSTD -l tmp5* && die "-l must fail on non-zstd file"
$ZSTD -lv tmp5.zst | grep "Decompressed Size: 0.00 KB (0 B)" # check that 0 size is present in header
-! $ZSTD -lv tmp5*
+$ZSTD -lv tmp5* && die "-l must fail on non-zstd file"
$ECHO "\n===> zstd --list/-l test with no content size field "
./datagen -g513K | $ZSTD > tmp6.zst
$ZSTD -l tmp6.zst
-! $ZSTD -lv tmp6.zst | grep "Decompressed Size:" # must NOT be present in header
+$ZSTD -lv tmp6.zst | grep "Decompressed Size:" && die "Field :Decompressed Size: should not be available in this compressed file"
$ECHO "\n===> zstd --list/-l test with no checksum "
$ZSTD -f --no-check tmp1
@@ -709,11 +826,22 @@ roundTripTest -g1M -P50 "1 --single-thread --long=29" " --long=28 --memory=512MB
roundTripTest -g1M -P50 "1 --single-thread --long=29" " --zstd=wlog=28 --memory=512MB"
+$ECHO "\n===> adaptive mode "
+roundTripTest -g270000000 " --adapt"
+roundTripTest -g27000000 " --adapt=min=1,max=4"
+$ECHO "===> test: --adapt must fail on incoherent bounds "
+./datagen > tmp
+$ZSTD -f -vv --adapt=min=10,max=9 tmp && die "--adapt must fail on incoherent bounds"
+
+
if [ "$1" != "--test-large-data" ]; then
$ECHO "Skipping large data tests"
exit 0
fi
+
+#############################################################################
+
$ECHO "\n===> large files tests "
roundTripTest -g270000000 1
@@ -768,4 +896,37 @@ else
$ECHO "\n**** no multithreading, skipping zstdmt tests **** "
fi
-rm tmp*
+
+$ECHO "\n===> cover dictionary builder : advanced options "
+
+TESTFILE=../programs/zstdcli.c
+./datagen > tmpDict
+$ECHO "- Create first dictionary"
+$ZSTD --train-cover=k=46,d=8,split=80 *.c ../programs/*.c -o tmpDict
+cp $TESTFILE tmp
+$ZSTD -f tmp -D tmpDict
+$ZSTD -d tmp.zst -D tmpDict -fo result
+$DIFF $TESTFILE result
+$ECHO "- Create second (different) dictionary"
+$ZSTD --train-cover=k=56,d=8 *.c ../programs/*.c ../programs/*.h -o tmpDictC
+$ZSTD -d tmp.zst -D tmpDictC -fo result && die "wrong dictionary not detected!"
+$ECHO "- Create dictionary with short dictID"
+$ZSTD --train-cover=k=46,d=8,split=80 *.c ../programs/*.c --dictID=1 -o tmpDict1
+cmp tmpDict tmpDict1 && die "dictionaries should have different ID !"
+$ECHO "- Create dictionary with size limit"
+$ZSTD --train-cover=steps=8 *.c ../programs/*.c -o tmpDict2 --maxdict=4K
+$ECHO "- Compare size of dictionary from 90% training samples with 80% training samples"
+$ZSTD --train-cover=split=90 -r *.c ../programs/*.c
+$ZSTD --train-cover=split=80 -r *.c ../programs/*.c
+$ECHO "- Create dictionary using all samples for both training and testing"
+$ZSTD --train-cover=split=100 -r *.c ../programs/*.c
+$ECHO "- Test -o before --train-cover"
+rm -f tmpDict dictionary
+$ZSTD -o tmpDict --train-cover *.c ../programs/*.c
+test -f tmpDict
+$ZSTD --train-cover *.c ../programs/*.c
+test -f dictionary
+rm -f tmp* dictionary
+
+
+rm -f tmp*
diff --git a/tests/poolTests.c b/tests/poolTests.c
index 00ee830154c99..9661b5299e523 100644
--- a/tests/poolTests.c
+++ b/tests/poolTests.c
@@ -15,11 +15,11 @@
#include <stddef.h>
#include <stdio.h>
-#define ASSERT_TRUE(p) \
- do { \
- if (!(p)) { \
- return 1; \
- } \
+#define ASSERT_TRUE(p) \
+ do { \
+ if (!(p)) { \
+ return 1; \
+ } \
} while (0)
#define ASSERT_FALSE(p) ASSERT_TRUE(!(p))
#define ASSERT_EQ(lhs, rhs) ASSERT_TRUE((lhs) == (rhs))
@@ -32,10 +32,10 @@ struct data {
void fn(void *opaque) {
struct data *data = (struct data *)opaque;
- pthread_mutex_lock(&data->mutex);
+ ZSTD_pthread_mutex_lock(&data->mutex);
data->data[data->i] = data->i;
++data->i;
- pthread_mutex_unlock(&data->mutex);
+ ZSTD_pthread_mutex_unlock(&data->mutex);
}
int testOrder(size_t numThreads, size_t queueSize) {
@@ -43,25 +43,26 @@ int testOrder(size_t numThreads, size_t queueSize) {
POOL_ctx *ctx = POOL_create(numThreads, queueSize);
ASSERT_TRUE(ctx);
data.i = 0;
- pthread_mutex_init(&data.mutex, NULL);
- {
- size_t i;
+ ZSTD_pthread_mutex_init(&data.mutex, NULL);
+ { size_t i;
for (i = 0; i < 16; ++i) {
POOL_add(ctx, &fn, &data);
}
}
POOL_free(ctx);
ASSERT_EQ(16, data.i);
- {
- size_t i;
+ { size_t i;
for (i = 0; i < data.i; ++i) {
ASSERT_EQ(i, data.data[i]);
}
}
- pthread_mutex_destroy(&data.mutex);
+ ZSTD_pthread_mutex_destroy(&data.mutex);
return 0;
}
+
+/* --- test deadlocks --- */
+
void waitFn(void *opaque) {
(void)opaque;
UTIL_sleepMilli(1);
@@ -72,8 +73,7 @@ int testWait(size_t numThreads, size_t queueSize) {
struct data data;
POOL_ctx *ctx = POOL_create(numThreads, queueSize);
ASSERT_TRUE(ctx);
- {
- size_t i;
+ { size_t i;
for (i = 0; i < 16; ++i) {
POOL_add(ctx, &waitFn, &data);
}
@@ -82,25 +82,178 @@ int testWait(size_t numThreads, size_t queueSize) {
return 0;
}
+
+/* --- test POOL_resize() --- */
+
+typedef struct {
+ ZSTD_pthread_mutex_t mut;
+ int val;
+ int max;
+ ZSTD_pthread_cond_t cond;
+} poolTest_t;
+
+void waitLongFn(void *opaque) {
+ poolTest_t* test = (poolTest_t*) opaque;
+ UTIL_sleepMilli(10);
+ ZSTD_pthread_mutex_lock(&test->mut);
+ test->val = test->val + 1;
+ if (test->val == test->max)
+ ZSTD_pthread_cond_signal(&test->cond);
+ ZSTD_pthread_mutex_unlock(&test->mut);
+}
+
+static int testThreadReduction_internal(POOL_ctx* ctx, poolTest_t test)
+{
+ int const nbWaits = 16;
+ UTIL_time_t startTime;
+ U64 time4threads, time2threads;
+
+ test.val = 0;
+ test.max = nbWaits;
+
+ startTime = UTIL_getTime();
+ { int i;
+ for (i=0; i<nbWaits; i++)
+ POOL_add(ctx, &waitLongFn, &test);
+ }
+ ZSTD_pthread_mutex_lock(&test.mut);
+ ZSTD_pthread_cond_wait(&test.cond, &test.mut);
+ ASSERT_EQ(test.val, nbWaits);
+ ZSTD_pthread_mutex_unlock(&test.mut);
+ time4threads = UTIL_clockSpanNano(startTime);
+
+ ASSERT_EQ( POOL_resize(ctx, 2/*nbThreads*/) , 0 );
+ test.val = 0;
+ startTime = UTIL_getTime();
+ { int i;
+ for (i=0; i<nbWaits; i++)
+ POOL_add(ctx, &waitLongFn, &test);
+ }
+ ZSTD_pthread_mutex_lock(&test.mut);
+ ZSTD_pthread_cond_wait(&test.cond, &test.mut);
+ ASSERT_EQ(test.val, nbWaits);
+ ZSTD_pthread_mutex_unlock(&test.mut);
+ time2threads = UTIL_clockSpanNano(startTime);
+
+ if (time4threads >= time2threads) return 1; /* check 4 threads were effectively faster than 2 */
+ return 0;
+}
+
+static int testThreadReduction(void) {
+ int result;
+ poolTest_t test;
+ POOL_ctx* const ctx = POOL_create(4 /*nbThreads*/, 2 /*queueSize*/);
+
+ ASSERT_TRUE(ctx);
+
+ memset(&test, 0, sizeof(test));
+ ASSERT_FALSE( ZSTD_pthread_mutex_init(&test.mut, NULL) );
+ ASSERT_FALSE( ZSTD_pthread_cond_init(&test.cond, NULL) );
+
+ result = testThreadReduction_internal(ctx, test);
+
+ ZSTD_pthread_mutex_destroy(&test.mut);
+ ZSTD_pthread_cond_destroy(&test.cond);
+ POOL_free(ctx);
+
+ return result;
+}
+
+
+/* --- test abrupt ending --- */
+
+typedef struct {
+ ZSTD_pthread_mutex_t mut;
+ int val;
+} abruptEndCanary_t;
+
+void waitIncFn(void *opaque) {
+ abruptEndCanary_t* test = (abruptEndCanary_t*) opaque;
+ UTIL_sleepMilli(10);
+ ZSTD_pthread_mutex_lock(&test->mut);
+ test->val = test->val + 1;
+ ZSTD_pthread_mutex_unlock(&test->mut);
+}
+
+static int testAbruptEnding_internal(abruptEndCanary_t test)
+{
+ int const nbWaits = 16;
+
+ POOL_ctx* const ctx = POOL_create(3 /*numThreads*/, nbWaits /*queueSize*/);
+ ASSERT_TRUE(ctx);
+ test.val = 0;
+
+ { int i;
+ for (i=0; i<nbWaits; i++)
+ POOL_add(ctx, &waitIncFn, &test); /* all jobs pushed into queue */
+ }
+ ASSERT_EQ( POOL_resize(ctx, 1 /*numThreads*/) , 0 ); /* downsize numThreads, to try to break end condition */
+
+ POOL_free(ctx); /* must finish all jobs in queue before giving back control */
+ ASSERT_EQ(test.val, nbWaits);
+ return 0;
+}
+
+static int testAbruptEnding(void) {
+ int result;
+ abruptEndCanary_t test;
+
+ memset(&test, 0, sizeof(test));
+ ASSERT_FALSE( ZSTD_pthread_mutex_init(&test.mut, NULL) );
+
+ result = testAbruptEnding_internal(test);
+
+ ZSTD_pthread_mutex_destroy(&test.mut);
+ return result;
+}
+
+
+
+/* --- test launcher --- */
+
int main(int argc, const char **argv) {
size_t numThreads;
+ (void)argc;
+ (void)argv;
+
+ if (POOL_create(0, 1)) { /* should not be possible */
+ printf("FAIL: should not create POOL with 0 threads\n");
+ return 1;
+ }
+
for (numThreads = 1; numThreads <= 4; ++numThreads) {
size_t queueSize;
for (queueSize = 0; queueSize <= 2; ++queueSize) {
+ printf("queueSize==%u, numThreads=%u \n",
+ (unsigned)queueSize, (unsigned)numThreads);
if (testOrder(numThreads, queueSize)) {
printf("FAIL: testOrder\n");
return 1;
}
+ printf("SUCCESS: testOrder\n");
if (testWait(numThreads, queueSize)) {
printf("FAIL: testWait\n");
return 1;
}
+ printf("SUCCESS: testWait\n");
}
}
- printf("PASS: testOrder\n");
- (void)argc;
- (void)argv;
- return (POOL_create(0, 1)) ? printf("FAIL: testInvalid\n"), 1
- : printf("PASS: testInvalid\n"), 0;
+
+ if (testThreadReduction()) {
+ printf("FAIL: thread reduction not effective \n");
+ return 1;
+ } else {
+ printf("SUCCESS: thread reduction effective (slower execution) \n");
+ }
+
+ if (testAbruptEnding()) {
+ printf("FAIL: jobs in queue not completed on early end \n");
+ return 1;
+ } else {
+ printf("SUCCESS: all jobs in queue completed on early end \n");
+ }
+
+ printf("PASS: all POOL tests\n");
+
return 0;
}
diff --git a/tests/rateLimiter.py b/tests/rateLimiter.py
new file mode 100755
index 0000000000000..da0baf01464ff
--- /dev/null
+++ b/tests/rateLimiter.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python3
+
+# ################################################################
+# Copyright (c) 2018-present, Facebook, Inc.
+# All rights reserved.
+#
+# This source code is licensed under both the BSD-style license (found in the
+# LICENSE file in the root directory of this source tree) and the GPLv2 (found
+# in the COPYING file in the root directory of this source tree).
+# ##########################################################################
+
+# Rate limiter, replacement for pv
+# this rate limiter does not "catch up" after a blocking period
+# Limitations:
+# - only accepts limit speed in MB/s
+
+import sys
+import time
+
+MB = 1024 * 1024
+rate = float(sys.argv[1]) * MB
+start = time.time()
+total_read = 0
+
+# sys.stderr.close() # remove error message, for Ctrl+C
+
+try:
+ buf = " "
+ while len(buf):
+ now = time.time()
+ to_read = max(int(rate * (now - start)), 1)
+ max_buf_size = 1 * MB
+ to_read = min(to_read, max_buf_size)
+ start = now
+
+ buf = sys.stdin.buffer.read(to_read)
+ sys.stdout.buffer.write(buf)
+
+except (KeyboardInterrupt, BrokenPipeError) as e:
+ pass
diff --git a/tests/roundTripCrash.c b/tests/roundTripCrash.c
index 7d937fceebc01..90afcd4b2a8be 100644
--- a/tests/roundTripCrash.c
+++ b/tests/roundTripCrash.c
@@ -212,7 +212,7 @@ static void loadFile(void* buffer, const char* fileName, size_t fileSize)
static void fileCheck(const char* fileName, int testCCtxParams)
{
size_t const fileSize = getFileSize(fileName);
- void* buffer = malloc(fileSize);
+ void* const buffer = malloc(fileSize + !fileSize /* avoid 0 */);
if (!buffer) {
fprintf(stderr, "not enough memory \n");
exit(4);
diff --git a/tests/symbols.c b/tests/symbols.c
index c0bed2e5d96e9..b370821314647 100644
--- a/tests/symbols.c
+++ b/tests/symbols.c
@@ -144,6 +144,8 @@ static const void *symbols[] = {
/* zdict.h: advanced functions */
&ZDICT_trainFromBuffer_cover,
&ZDICT_optimizeTrainFromBuffer_cover,
+ &ZDICT_trainFromBuffer_fastCover,
+ &ZDICT_optimizeTrainFromBuffer_fastCover,
&ZDICT_finalizeDictionary,
&ZDICT_trainFromBuffer_legacy,
&ZDICT_addEntropyTablesFromBuffer,
diff --git a/tests/test-zstd-versions.py b/tests/test-zstd-versions.py
index f2deac1f28da0..8e88b869b0d28 100755
--- a/tests/test-zstd-versions.py
+++ b/tests/test-zstd-versions.py
@@ -213,7 +213,7 @@ if __name__ == '__main__':
print('Retrieve all release tags :')
os.chdir(clone_dir)
alltags = get_git_tags() + [head]
- tags = [t for t in alltags if t >= 'v0.4.0']
+ tags = [t for t in alltags if t >= 'v0.5.0']
print(tags)
# Build all release zstd
diff --git a/tests/zstreamtest.c b/tests/zstreamtest.c
index b94f282f5802c..f47451a3c3d50 100644
--- a/tests/zstreamtest.c
+++ b/tests/zstreamtest.c
@@ -10,8 +10,8 @@
/*-************************************
-* Compiler specific
-**************************************/
+ * Compiler specific
+ **************************************/
#ifdef _MSC_VER /* Visual Studio */
# define _CRT_SECURE_NO_WARNINGS /* fgets */
# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
@@ -20,8 +20,8 @@
/*-************************************
-* Includes
-**************************************/
+ * Includes
+ **************************************/
#include <stdlib.h> /* free */
#include <stdio.h> /* fgets, sscanf */
#include <string.h> /* strcmp */
@@ -40,8 +40,8 @@
/*-************************************
-* Constants
-**************************************/
+ * Constants
+ **************************************/
#define KB *(1U<<10)
#define MB *(1U<<20)
#define GB *(1U<<30)
@@ -54,8 +54,8 @@ static const U32 prime32 = 2654435761U;
/*-************************************
-* Display Macros
-**************************************/
+ * Display Macros
+ **************************************/
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { \
DISPLAY(__VA_ARGS__); \
@@ -74,8 +74,8 @@ static U64 g_clockTime = 0;
/*-*******************************************************
-* Fuzzer functions
-*********************************************************/
+ * Check macros
+ *********************************************************/
#undef MIN
#undef MAX
#define MIN(a,b) ((a)<(b)?(a):(b))
@@ -84,7 +84,7 @@ static U64 g_clockTime = 0;
@return : a 27 bits random value, from a 32-bits `seed`.
`seed` is also modified */
#define FUZ_rotl32(x,r) ((x << r) | (x >> (32 - r)))
-unsigned int FUZ_rand(unsigned int* seedPtr)
+static unsigned int FUZ_rand(unsigned int* seedPtr)
{
static const U32 prime2 = 2246822519U;
U32 rand32 = *seedPtr;
@@ -110,10 +110,24 @@ unsigned int FUZ_rand(unsigned int* seedPtr)
#f, ZSTD_getErrorName(err)); \
}
+#define CHECK_RET(ret, cond, ...) { \
+ if (cond) { \
+ DISPLAY("Error %llu => ", (unsigned long long)ret); \
+ DISPLAY(__VA_ARGS__); \
+ DISPLAY(" (line %u)\n", __LINE__); \
+ return ret; \
+} }
+
+#define CHECK_RET_Z(f) { \
+ size_t const err = f; \
+ CHECK_RET(err, ZSTD_isError(err), "%s : %s ", \
+ #f, ZSTD_getErrorName(err)); \
+}
+
/*======================================================
-* Basic Unit tests
-======================================================*/
+ * Basic Unit tests
+ *======================================================*/
typedef struct {
void* start;
@@ -121,34 +135,34 @@ typedef struct {
size_t filled;
} buffer_t;
-static const buffer_t g_nullBuffer = { NULL, 0 , 0 };
+static const buffer_t kBuffNull = { NULL, 0 , 0 };
+
+static void FUZ_freeDictionary(buffer_t dict)
+{
+ free(dict.start);
+}
static buffer_t FUZ_createDictionary(const void* src, size_t srcSize, size_t blockSize, size_t requestedDictSize)
{
- buffer_t dict = { NULL, 0, 0 };
+ buffer_t dict = kBuffNull;
size_t const nbBlocks = (srcSize + (blockSize-1)) / blockSize;
- size_t* const blockSizes = (size_t*) malloc(nbBlocks * sizeof(size_t));
- if (!blockSizes) return dict;
+ size_t* const blockSizes = (size_t*)malloc(nbBlocks * sizeof(size_t));
+ if (!blockSizes) return kBuffNull;
dict.start = malloc(requestedDictSize);
- if (!dict.start) { free(blockSizes); return dict; }
+ if (!dict.start) { free(blockSizes); return kBuffNull; }
{ size_t nb;
for (nb=0; nb<nbBlocks-1; nb++) blockSizes[nb] = blockSize;
blockSizes[nbBlocks-1] = srcSize - (blockSize * (nbBlocks-1));
}
{ size_t const dictSize = ZDICT_trainFromBuffer(dict.start, requestedDictSize, src, blockSizes, (unsigned)nbBlocks);
free(blockSizes);
- if (ZDICT_isError(dictSize)) { free(dict.start); return g_nullBuffer; }
+ if (ZDICT_isError(dictSize)) { FUZ_freeDictionary(dict); return kBuffNull; }
dict.size = requestedDictSize;
dict.filled = dictSize;
- return dict; /* how to return dictSize ? */
+ return dict;
}
}
-static void FUZ_freeDictionary(buffer_t dict)
-{
- free(dict.start);
-}
-
/* Round trips data and updates xxh with the decompressed data produced */
static size_t SEQ_roundTrip(ZSTD_CCtx* cctx, ZSTD_DCtx* dctx,
XXH64_state_t* xxh, void* data, size_t size,
@@ -207,6 +221,42 @@ static size_t SEQ_generateRoundTrip(ZSTD_CCtx* cctx, ZSTD_DCtx* dctx,
return 0;
}
+static size_t getCCtxParams(ZSTD_CCtx* zc, ZSTD_parameters* savedParams)
+{
+ unsigned value;
+ CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_p_windowLog, &savedParams->cParams.windowLog));
+ CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_p_hashLog, &savedParams->cParams.hashLog));
+ CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_p_chainLog, &savedParams->cParams.chainLog));
+ CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_p_searchLog, &savedParams->cParams.searchLog));
+ CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_p_minMatch, &savedParams->cParams.searchLength));
+ CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_p_targetLength, &savedParams->cParams.targetLength));
+ CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_p_compressionStrategy, &value));
+ savedParams->cParams.strategy = value;
+
+ CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_p_checksumFlag, &savedParams->fParams.checksumFlag));
+ CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_p_contentSizeFlag, &savedParams->fParams.contentSizeFlag));
+ CHECK_RET_Z(ZSTD_CCtx_getParameter(zc, ZSTD_p_dictIDFlag, &value));
+ savedParams->fParams.noDictIDFlag = !value;
+ return 0;
+}
+
+static U32 badParameters(ZSTD_CCtx* zc, ZSTD_parameters const savedParams)
+{
+ ZSTD_parameters params;
+ if (ZSTD_isError(getCCtxParams(zc, &params))) return 10;
+ CHECK_RET(1, params.cParams.windowLog != savedParams.cParams.windowLog, "windowLog");
+ CHECK_RET(2, params.cParams.hashLog != savedParams.cParams.hashLog, "hashLog");
+ CHECK_RET(3, params.cParams.chainLog != savedParams.cParams.chainLog, "chainLog");
+ CHECK_RET(4, params.cParams.searchLog != savedParams.cParams.searchLog, "searchLog");
+ CHECK_RET(5, params.cParams.searchLength != savedParams.cParams.searchLength, "searchLength");
+ CHECK_RET(6, params.cParams.targetLength != savedParams.cParams.targetLength, "targetLength");
+
+ CHECK_RET(7, params.fParams.checksumFlag != savedParams.fParams.checksumFlag, "checksumFlag");
+ CHECK_RET(8, params.fParams.contentSizeFlag != savedParams.fParams.contentSizeFlag, "contentSizeFlag");
+ CHECK_RET(9, params.fParams.noDictIDFlag != savedParams.fParams.noDictIDFlag, "noDictIDFlag");
+ return 0;
+}
+
static int basicUnitTests(U32 seed, double compressibility)
{
size_t const CNBufferSize = COMPRESSIBLE_NOISE_LENGTH;
@@ -226,7 +276,7 @@ static int basicUnitTests(U32 seed, double compressibility)
ZSTD_inBuffer inBuff, inBuff2;
ZSTD_outBuffer outBuff;
- buffer_t dictionary = g_nullBuffer;
+ buffer_t dictionary = kBuffNull;
size_t const dictSize = 128 KB;
unsigned dictID = 0;
@@ -383,11 +433,12 @@ static int basicUnitTests(U32 seed, double compressibility)
inBuff.pos = 0;
outBuff.pos = 0;
while (r) { /* skippable frame */
- size_t const inSize = FUZ_rand(&coreSeed) & 15;
- size_t const outSize = FUZ_rand(&coreSeed) & 15;
+ size_t const inSize = (FUZ_rand(&coreSeed) & 15) + 1;
+ size_t const outSize = (FUZ_rand(&coreSeed) & 15) + 1;
inBuff.size = inBuff.pos + inSize;
outBuff.size = outBuff.pos + outSize;
r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
+ if (ZSTD_isError(r)) DISPLAYLEVEL(4, "ZSTD_decompressStream on skippable frame error : %s \n", ZSTD_getErrorName(r));
if (ZSTD_isError(r)) goto _output_error;
}
/* normal frame */
@@ -395,14 +446,17 @@ static int basicUnitTests(U32 seed, double compressibility)
r=1;
while (r) {
size_t const inSize = FUZ_rand(&coreSeed) & 15;
- size_t const outSize = FUZ_rand(&coreSeed) & 15;
+ size_t const outSize = (FUZ_rand(&coreSeed) & 15) + (!inSize); /* avoid having both sizes at 0 => would trigger a no_forward_progress error */
inBuff.size = inBuff.pos + inSize;
outBuff.size = outBuff.pos + outSize;
r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
+ if (ZSTD_isError(r)) DISPLAYLEVEL(4, "ZSTD_decompressStream error : %s \n", ZSTD_getErrorName(r));
if (ZSTD_isError(r)) goto _output_error;
}
}
+ if (outBuff.pos != CNBufferSize) DISPLAYLEVEL(4, "outBuff.pos != CNBufferSize : should have regenerated same amount ! \n");
if (outBuff.pos != CNBufferSize) goto _output_error; /* should regenerate the same amount */
+ if (inBuff.pos != cSize) DISPLAYLEVEL(4, "inBuff.pos != cSize : should have real all input ! \n");
if (inBuff.pos != cSize) goto _output_error; /* should have read the entire frame */
DISPLAYLEVEL(3, "OK \n");
@@ -414,6 +468,30 @@ static int basicUnitTests(U32 seed, double compressibility)
} }
DISPLAYLEVEL(3, "OK \n");
+ /* Decompression forward progress */
+ DISPLAYLEVEL(3, "test%3i : generate error when ZSTD_decompressStream() doesn't progress : ", testNb++);
+ { /* skippable frame */
+ size_t r = 0;
+ int decNb = 0;
+ int const maxDec = 100;
+ inBuff.src = compressedBuffer;
+ inBuff.size = cSize;
+ inBuff.pos = 0;
+
+ outBuff.dst = decodedBuffer;
+ outBuff.pos = 0;
+ outBuff.size = CNBufferSize-1; /* 1 byte missing */
+
+ for (decNb=0; decNb<maxDec; decNb++) {
+ if (r==0) ZSTD_initDStream_usingDict(zd, CNBuffer, dictSize);
+ r = ZSTD_decompressStream(zd, &outBuff, &inBuff);
+ if (ZSTD_isError(r)) break;
+ }
+ if (!ZSTD_isError(r)) DISPLAYLEVEL(4, "ZSTD_decompressStream should have triggered a no_forward_progress error \n");
+ if (!ZSTD_isError(r)) goto _output_error; /* should have triggered no_forward_progress error */
+ }
+ DISPLAYLEVEL(3, "OK \n");
+
/* _srcSize compression test */
DISPLAYLEVEL(3, "test%3i : compress_srcSize %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
ZSTD_initCStream_srcSize(zc, 1, CNBufferSize);
@@ -460,6 +538,21 @@ static int basicUnitTests(U32 seed, double compressibility)
DISPLAYLEVEL(3, "OK (error detected : %s) \n", ZSTD_getErrorName(r));
}
+ DISPLAYLEVEL(3, "test%3i : wrong srcSize !contentSizeFlag : %u bytes : ", testNb++, COMPRESSIBLE_NOISE_LENGTH-1);
+ { ZSTD_parameters params = ZSTD_getParams(1, CNBufferSize, 0);
+ params.fParams.contentSizeFlag = 0;
+ CHECK_Z(ZSTD_initCStream_advanced(zc, NULL, 0, params, CNBufferSize - MIN(CNBufferSize, 200 KB)));
+ outBuff.dst = (char*)compressedBuffer;
+ outBuff.size = compressedBufferSize;
+ outBuff.pos = 0;
+ inBuff.src = CNBuffer;
+ inBuff.size = CNBufferSize;
+ inBuff.pos = 0;
+ { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
+ if (ZSTD_getErrorCode(r) != ZSTD_error_srcSize_wrong) goto _output_error; /* must fail : wrong srcSize */
+ DISPLAYLEVEL(3, "OK (error detected : %s) \n", ZSTD_getErrorName(r));
+ } }
+
/* Complex context re-use scenario */
DISPLAYLEVEL(3, "test%3i : context re-use : ", testNb++);
ZSTD_freeCStream(zc);
@@ -507,7 +600,6 @@ static int basicUnitTests(U32 seed, double compressibility)
size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict);
DISPLAYLEVEL(5, "ZSTD_initCStream_usingCDict result : %u ", (U32)initError);
if (ZSTD_isError(initError)) goto _output_error;
- cSize = 0;
outBuff.dst = compressedBuffer;
outBuff.size = compressedBufferSize;
outBuff.pos = 0;
@@ -591,6 +683,8 @@ static int basicUnitTests(U32 seed, double compressibility)
for (size = 512; size <= maxSize; size <<= 1) {
U64 const crcOrig = XXH64(CNBuffer, size, 0);
ZSTD_CCtx* const cctx = ZSTD_createCCtx();
+ ZSTD_parameters savedParams;
+ getCCtxParams(cctx, &savedParams);
outBuff.dst = compressedBuffer;
outBuff.size = compressedBufferSize;
outBuff.pos = 0;
@@ -599,6 +693,7 @@ static int basicUnitTests(U32 seed, double compressibility)
inBuff.pos = 0;
CHECK_Z(ZSTD_CCtx_refCDict(cctx, cdict));
CHECK_Z(ZSTD_compress_generic(cctx, &outBuff, &inBuff, ZSTD_e_end));
+ CHECK(badParameters(cctx, savedParams), "Bad CCtx params");
if (inBuff.pos != inBuff.size) goto _output_error;
{ ZSTD_outBuffer decOut = {decodedBuffer, size, 0};
ZSTD_inBuffer decIn = {outBuff.dst, outBuff.pos, 0};
@@ -622,7 +717,6 @@ static int basicUnitTests(U32 seed, double compressibility)
ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, ZSTD_dlm_byRef, ZSTD_dct_auto, cParams, ZSTD_defaultCMem);
size_t const initError = ZSTD_initCStream_usingCDict_advanced(zc, cdict, fParams, CNBufferSize);
if (ZSTD_isError(initError)) goto _output_error;
- cSize = 0;
outBuff.dst = compressedBuffer;
outBuff.size = compressedBufferSize;
outBuff.pos = 0;
@@ -748,7 +842,12 @@ static int basicUnitTests(U32 seed, double compressibility)
/* Basic multithreading compression test */
DISPLAYLEVEL(3, "test%3i : compress %u bytes with multiple threads : ", testNb++, COMPRESSIBLE_NOISE_LENGTH);
{ ZSTD_parameters const params = ZSTD_getParams(1, 0, 0);
+ unsigned jobSize;
+ CHECK_Z( ZSTDMT_getMTCtxParameter(mtctx, ZSTDMT_p_jobSize, &jobSize));
+ CHECK(jobSize != 0, "job size non-zero");
CHECK_Z( ZSTDMT_initCStream_advanced(mtctx, CNBuffer, dictSize, params, CNBufferSize) );
+ CHECK_Z( ZSTDMT_getMTCtxParameter(mtctx, ZSTDMT_p_jobSize, &jobSize));
+ CHECK(jobSize != 0, "job size non-zero");
}
outBuff.dst = compressedBuffer;
outBuff.size = compressedBufferSize;
@@ -868,6 +967,26 @@ static int basicUnitTests(U32 seed, double compressibility)
}
DISPLAYLEVEL(3, "OK \n");
+ DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_srcSize sets requestedParams : ", testNb++);
+ { unsigned level;
+ CHECK_Z(ZSTD_initCStream_srcSize(zc, 11, ZSTD_CONTENTSIZE_UNKNOWN));
+ CHECK_Z(ZSTD_CCtx_getParameter(zc, ZSTD_p_compressionLevel, &level));
+ CHECK(level != 11, "Compression level does not match");
+ ZSTD_resetCStream(zc, ZSTD_CONTENTSIZE_UNKNOWN);
+ CHECK_Z(ZSTD_CCtx_getParameter(zc, ZSTD_p_compressionLevel, &level));
+ CHECK(level != 11, "Compression level does not match");
+ }
+ DISPLAYLEVEL(3, "OK \n");
+
+ DISPLAYLEVEL(3, "test%3i : ZSTD_initCStream_advanced sets requestedParams : ", testNb++);
+ { ZSTD_parameters const params = ZSTD_getParams(9, 0, 0);
+ CHECK_Z(ZSTD_initCStream_advanced(zc, NULL, 0, params, ZSTD_CONTENTSIZE_UNKNOWN));
+ CHECK(badParameters(zc, params), "Compression parameters do not match");
+ ZSTD_resetCStream(zc, ZSTD_CONTENTSIZE_UNKNOWN);
+ CHECK(badParameters(zc, params), "Compression parameters do not match");
+ }
+ DISPLAYLEVEL(3, "OK \n");
+
/* Overlen overwriting window data bug */
DISPLAYLEVEL(3, "test%3i : wildcopy doesn't overwrite potential match data : ", testNb++);
{ /* This test has a window size of 1024 bytes and consists of 3 blocks:
@@ -901,6 +1020,97 @@ static int basicUnitTests(U32 seed, double compressibility)
}
DISPLAYLEVEL(3, "OK \n");
+ DISPLAYLEVEL(3, "test%3i : dictionary + uncompressible block + reusing tables checks offset table validity: ", testNb++);
+ { ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(
+ dictionary.start, dictionary.filled,
+ ZSTD_dlm_byRef, ZSTD_dct_fullDict,
+ ZSTD_getCParams(3, 0, dictionary.filled),
+ ZSTD_defaultCMem);
+ const size_t inbufsize = 2 * 128 * 1024; /* 2 blocks */
+ const size_t outbufsize = ZSTD_compressBound(inbufsize);
+ size_t inbufpos = 0;
+ size_t cursegmentlen;
+ BYTE *inbuf = (BYTE *)malloc(inbufsize);
+ BYTE *outbuf = (BYTE *)malloc(outbufsize);
+ BYTE *checkbuf = (BYTE *)malloc(inbufsize);
+ size_t ret;
+
+ CHECK(cdict == NULL, "failed to alloc cdict");
+ CHECK(inbuf == NULL, "failed to alloc input buffer");
+
+ /* first block is uncompressible */
+ cursegmentlen = 128 * 1024;
+ RDG_genBuffer(inbuf + inbufpos, cursegmentlen, 0., 0., seed);
+ inbufpos += cursegmentlen;
+
+ /* second block is compressible */
+ cursegmentlen = 128 * 1024 - 256;
+ RDG_genBuffer(inbuf + inbufpos, cursegmentlen, 0.05, 0., seed);
+ inbufpos += cursegmentlen;
+
+ /* and includes a very long backref */
+ cursegmentlen = 128;
+ memcpy(inbuf + inbufpos, dictionary.start + 256, cursegmentlen);
+ inbufpos += cursegmentlen;
+
+ /* and includes a very long backref */
+ cursegmentlen = 128;
+ memcpy(inbuf + inbufpos, dictionary.start + 128, cursegmentlen);
+ inbufpos += cursegmentlen;
+
+ ret = ZSTD_compress_usingCDict(zc, outbuf, outbufsize, inbuf, inbufpos, cdict);
+ CHECK_Z(ret);
+
+ ret = ZSTD_decompress_usingDict(zd, checkbuf, inbufsize, outbuf, ret, dictionary.start, dictionary.filled);
+ CHECK_Z(ret);
+
+ CHECK(memcmp(inbuf, checkbuf, inbufpos), "start and finish buffers don't match");
+
+ ZSTD_freeCDict(cdict);
+ free(inbuf);
+ free(outbuf);
+ free(checkbuf);
+ }
+ DISPLAYLEVEL(3, "OK \n");
+
+ DISPLAYLEVEL(3, "test%3i : dictionary + small blocks + reusing tables checks offset table validity: ", testNb++);
+ { ZSTD_CDict* const cdict = ZSTD_createCDict_advanced(
+ dictionary.start, dictionary.filled,
+ ZSTD_dlm_byRef, ZSTD_dct_fullDict,
+ ZSTD_getCParams(3, 0, dictionary.filled),
+ ZSTD_defaultCMem);
+ ZSTD_outBuffer out = {compressedBuffer, compressedBufferSize, 0};
+ int remainingInput = 256 * 1024;
+ int offset;
+
+ ZSTD_CCtx_reset(zc);
+ CHECK_Z(ZSTD_CCtx_resetParameters(zc));
+ CHECK_Z(ZSTD_CCtx_refCDict(zc, cdict));
+ CHECK_Z(ZSTD_CCtx_setParameter(zc, ZSTD_p_checksumFlag, 1));
+ /* Write a bunch of 6 byte blocks */
+ while (remainingInput > 0) {
+ char testBuffer[6] = "\xAA\xAA\xAA\xAA\xAA\xAA";
+ const size_t kSmallBlockSize = sizeof(testBuffer);
+ ZSTD_inBuffer in = {testBuffer, kSmallBlockSize, 0};
+
+ CHECK_Z(ZSTD_compress_generic(zc, &out, &in, ZSTD_e_flush));
+ CHECK(in.pos != in.size, "input not fully consumed");
+ remainingInput -= kSmallBlockSize;
+ }
+ /* Write several very long offset matches into the dictionary */
+ for (offset = 1024; offset >= 0; offset -= 128) {
+ ZSTD_inBuffer in = {dictionary.start + offset, 128, 0};
+ ZSTD_EndDirective flush = offset > 0 ? ZSTD_e_continue : ZSTD_e_end;
+ CHECK_Z(ZSTD_compress_generic(zc, &out, &in, flush));
+ CHECK(in.pos != in.size, "input not fully consumed");
+ }
+ /* Ensure decompression works */
+ CHECK_Z(ZSTD_decompress_usingDict(zd, decodedBuffer, CNBufferSize, out.dst, out.pos, dictionary.start, dictionary.filled));
+
+ ZSTD_freeCDict(cdict);
+ }
+ DISPLAYLEVEL(3, "OK \n");
+
_end:
FUZ_freeDictionary(dictionary);
ZSTD_freeCStream(zc);
@@ -1216,8 +1426,9 @@ _output_error:
}
-/* Multi-threading version of fuzzer Tests */
-static int fuzzerTests_MT(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests)
+/* fuzzing ZSTDMT_* interface */
+static int fuzzerTests_MT(U32 seed, U32 nbTests, unsigned startTest,
+ double compressibility, int bigTests)
{
const U32 maxSrcLog = bigTests ? 24 : 22;
static const U32 maxSampleLog = 19;
@@ -1491,7 +1702,7 @@ _output_error:
* Otherwise, sets the param in zc. */
static size_t setCCtxParameter(ZSTD_CCtx* zc, ZSTD_CCtx_params* cctxParams,
ZSTD_cParameter param, unsigned value,
- U32 useOpaqueAPI)
+ int useOpaqueAPI)
{
if (useOpaqueAPI) {
return ZSTD_CCtxParam_setParameter(cctxParams, param, value);
@@ -1501,7 +1712,8 @@ static size_t setCCtxParameter(ZSTD_CCtx* zc, ZSTD_CCtx_params* cctxParams,
}
/* Tests for ZSTD_compress_generic() API */
-static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double compressibility, int bigTests, U32 const useOpaqueAPI)
+static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest,
+ double compressibility, int bigTests)
{
U32 const maxSrcLog = bigTests ? 24 : 22;
static const U32 maxSampleLog = 19;
@@ -1554,12 +1766,14 @@ static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double
/* test loop */
for ( ; (testNb <= nbTests) || (UTIL_clockSpanMicro(startClock) < g_clockTime) ; testNb++ ) {
U32 lseed;
+ int opaqueAPI;
const BYTE* srcBuffer;
size_t totalTestSize, totalGenSize, cSize;
XXH64_state_t xxhState;
U64 crcOrig;
U32 resetAllowed = 1;
size_t maxTestSize;
+ ZSTD_parameters savedParams;
/* init */
if (nbTests >= testNb) { DISPLAYUPDATE(2, "\r%6u/%6u ", testNb, nbTests); }
@@ -1567,6 +1781,7 @@ static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double
FUZ_rand(&coreSeed);
lseed = coreSeed ^ prime32;
DISPLAYLEVEL(5, " *** Test %u *** \n", testNb);
+ opaqueAPI = FUZ_rand(&lseed) & 1;
/* states full reset (deliberately not synchronized) */
/* some issues can only happen when reusing states */
@@ -1574,13 +1789,13 @@ static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double
DISPLAYLEVEL(5, "Creating new context \n");
ZSTD_freeCCtx(zc);
zc = ZSTD_createCCtx();
- CHECK(zc==NULL, "ZSTD_createCCtx allocation error");
- resetAllowed=0;
+ CHECK(zc == NULL, "ZSTD_createCCtx allocation error");
+ resetAllowed = 0;
}
if ((FUZ_rand(&lseed) & 0xFF) == 132) {
ZSTD_freeDStream(zd);
zd = ZSTD_createDStream();
- CHECK(zd==NULL, "ZSTD_createDStream allocation error");
+ CHECK(zd == NULL, "ZSTD_createDStream allocation error");
ZSTD_initDStream_usingDict(zd, NULL, 0); /* ensure at least one init */
}
@@ -1602,11 +1817,14 @@ static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double
/* compression init */
CHECK_Z( ZSTD_CCtx_loadDictionary(zc, NULL, 0) ); /* cancel previous dict /*/
if ((FUZ_rand(&lseed)&1) /* at beginning, to keep same nb of rand */
- && oldTestLog /* at least one test happened */ && resetAllowed) {
+ && oldTestLog /* at least one test happened */
+ && resetAllowed) {
+ /* just set a compression level */
maxTestSize = FUZ_randomLength(&lseed, oldTestLog+2);
if (maxTestSize >= srcBufferSize) maxTestSize = srcBufferSize-1;
{ int const compressionLevel = (FUZ_rand(&lseed) % 5) + 1;
- CHECK_Z (setCCtxParameter(zc, cctxParams, ZSTD_p_compressionLevel, compressionLevel, useOpaqueAPI) );
+ DISPLAYLEVEL(5, "t%u : compression level : %i \n", testNb, compressionLevel);
+ CHECK_Z (setCCtxParameter(zc, cctxParams, ZSTD_p_compressionLevel, compressionLevel, opaqueAPI) );
}
} else {
U32 const testLog = FUZ_rand(&lseed) % maxSrcLog;
@@ -1628,7 +1846,10 @@ static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double
}
{ U64 const pledgedSrcSize = (FUZ_rand(&lseed) & 3) ? ZSTD_CONTENTSIZE_UNKNOWN : maxTestSize;
ZSTD_compressionParameters cParams = ZSTD_getCParams(cLevel, pledgedSrcSize, dictSize);
- static const U32 windowLogMax = 24;
+ const U32 windowLogMax = bigTests ? 24 : 20;
+ const U32 searchLogMax = bigTests ? 15 : 13;
+ if (dictSize)
+ DISPLAYLEVEL(5, "t%u: with dictionary of size : %zu \n", testNb, dictSize);
/* mess with compression parameters */
cParams.windowLog += (FUZ_rand(&lseed) & 3) - 1;
@@ -1636,68 +1857,70 @@ static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double
cParams.hashLog += (FUZ_rand(&lseed) & 3) - 1;
cParams.chainLog += (FUZ_rand(&lseed) & 3) - 1;
cParams.searchLog += (FUZ_rand(&lseed) & 3) - 1;
+ cParams.searchLog = MIN(searchLogMax, cParams.searchLog);
cParams.searchLength += (FUZ_rand(&lseed) & 3) - 1;
cParams.targetLength = (U32)((cParams.targetLength + 1 ) * (0.5 + ((double)(FUZ_rand(&lseed) & 127) / 128)));
- cParams = ZSTD_adjustCParams(cParams, 0, 0);
+ cParams = ZSTD_adjustCParams(cParams, pledgedSrcSize, dictSize);
if (FUZ_rand(&lseed) & 1) {
DISPLAYLEVEL(5, "t%u: windowLog : %u \n", testNb, cParams.windowLog);
- CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_windowLog, cParams.windowLog, useOpaqueAPI) );
+ CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_windowLog, cParams.windowLog, opaqueAPI) );
assert(cParams.windowLog >= ZSTD_WINDOWLOG_MIN); /* guaranteed by ZSTD_adjustCParams() */
windowLogMalus = (cParams.windowLog - ZSTD_WINDOWLOG_MIN) / 5;
}
if (FUZ_rand(&lseed) & 1) {
DISPLAYLEVEL(5, "t%u: hashLog : %u \n", testNb, cParams.hashLog);
- CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_hashLog, cParams.hashLog, useOpaqueAPI) );
+ CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_hashLog, cParams.hashLog, opaqueAPI) );
}
if (FUZ_rand(&lseed) & 1) {
DISPLAYLEVEL(5, "t%u: chainLog : %u \n", testNb, cParams.chainLog);
- CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_chainLog, cParams.chainLog, useOpaqueAPI) );
+ CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_chainLog, cParams.chainLog, opaqueAPI) );
}
- if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_searchLog, cParams.searchLog, useOpaqueAPI) );
- if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_minMatch, cParams.searchLength, useOpaqueAPI) );
- if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_targetLength, cParams.targetLength, useOpaqueAPI) );
+ if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_searchLog, cParams.searchLog, opaqueAPI) );
+ if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_minMatch, cParams.searchLength, opaqueAPI) );
+ if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_targetLength, cParams.targetLength, opaqueAPI) );
/* mess with long distance matching parameters */
if (bigTests) {
- if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_enableLongDistanceMatching, FUZ_rand(&lseed) & 63, useOpaqueAPI) );
- if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmHashLog, FUZ_randomClampedLength(&lseed, ZSTD_HASHLOG_MIN, 23), useOpaqueAPI) );
- if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmMinMatch, FUZ_randomClampedLength(&lseed, ZSTD_LDM_MINMATCH_MIN, ZSTD_LDM_MINMATCH_MAX), useOpaqueAPI) );
- if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmBucketSizeLog, FUZ_randomClampedLength(&lseed, 0, ZSTD_LDM_BUCKETSIZELOG_MAX), useOpaqueAPI) );
- if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmHashEveryLog, FUZ_randomClampedLength(&lseed, 0, ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN), useOpaqueAPI) );
+ if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_enableLongDistanceMatching, FUZ_rand(&lseed) & 63, opaqueAPI) );
+ if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmHashLog, FUZ_randomClampedLength(&lseed, ZSTD_HASHLOG_MIN, 23), opaqueAPI) );
+ if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmMinMatch, FUZ_randomClampedLength(&lseed, ZSTD_LDM_MINMATCH_MIN, ZSTD_LDM_MINMATCH_MAX), opaqueAPI) );
+ if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmBucketSizeLog, FUZ_randomClampedLength(&lseed, 0, ZSTD_LDM_BUCKETSIZELOG_MAX), opaqueAPI) );
+ if (FUZ_rand(&lseed) & 3) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_ldmHashEveryLog, FUZ_randomClampedLength(&lseed, 0, ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN), opaqueAPI) );
}
/* mess with frame parameters */
if (FUZ_rand(&lseed) & 1) {
U32 const checksumFlag = FUZ_rand(&lseed) & 1;
DISPLAYLEVEL(5, "t%u: frame checksum : %u \n", testNb, checksumFlag);
- CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_checksumFlag, checksumFlag, useOpaqueAPI) );
+ CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_checksumFlag, checksumFlag, opaqueAPI) );
}
- if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_dictIDFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
- if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_contentSizeFlag, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
+ if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_dictIDFlag, FUZ_rand(&lseed) & 1, opaqueAPI) );
+ if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_contentSizeFlag, FUZ_rand(&lseed) & 1, opaqueAPI) );
if (FUZ_rand(&lseed) & 1) {
DISPLAYLEVEL(5, "t%u: pledgedSrcSize : %u \n", testNb, (U32)pledgedSrcSize);
CHECK_Z( ZSTD_CCtx_setPledgedSrcSize(zc, pledgedSrcSize) );
}
- /* multi-threading parameters */
- { U32 const nbThreadsCandidate = (FUZ_rand(&lseed) & 4) + 1;
+ /* multi-threading parameters. Only adjust ocassionally for small tests. */
+ if (bigTests || (FUZ_rand(&lseed) & 0xF) == 0xF) {
+ U32 const nbThreadsCandidate = (FUZ_rand(&lseed) & 4) + 1;
U32 const nbThreadsAdjusted = (windowLogMalus < nbThreadsCandidate) ? nbThreadsCandidate - windowLogMalus : 1;
U32 const nbThreads = MIN(nbThreadsAdjusted, nbThreadsMax);
DISPLAYLEVEL(5, "t%u: nbThreads : %u \n", testNb, nbThreads);
- CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_nbWorkers, nbThreads, useOpaqueAPI) );
+ CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_nbWorkers, nbThreads, opaqueAPI) );
if (nbThreads > 1) {
U32 const jobLog = FUZ_rand(&lseed) % (testLog+1);
- CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_overlapSizeLog, FUZ_rand(&lseed) % 10, useOpaqueAPI) );
- CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_jobSize, (U32)FUZ_rLogLength(&lseed, jobLog), useOpaqueAPI) );
+ CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_overlapSizeLog, FUZ_rand(&lseed) % 10, opaqueAPI) );
+ CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_jobSize, (U32)FUZ_rLogLength(&lseed, jobLog), opaqueAPI) );
}
}
- if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_forceMaxWindow, FUZ_rand(&lseed) & 1, useOpaqueAPI) );
+ if (FUZ_rand(&lseed) & 1) CHECK_Z( setCCtxParameter(zc, cctxParams, ZSTD_p_forceMaxWindow, FUZ_rand(&lseed) & 1, opaqueAPI) );
/* Apply parameters */
- if (useOpaqueAPI) {
- DISPLAYLEVEL(6," t%u: applying CCtxParams \n", testNb);
+ if (opaqueAPI) {
+ DISPLAYLEVEL(5, "t%u: applying CCtxParams \n", testNb);
CHECK_Z (ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams) );
}
@@ -1709,7 +1932,7 @@ static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double
}
if (dict && dictSize) {
/* test that compression parameters are rejected (correctly) after loading a non-NULL dictionary */
- if (useOpaqueAPI) {
+ if (opaqueAPI) {
size_t const setError = ZSTD_CCtx_setParametersUsingCCtxParams(zc, cctxParams);
CHECK(!ZSTD_isError(setError), "ZSTD_CCtx_setParametersUsingCCtxParams should have failed");
} else {
@@ -1722,6 +1945,8 @@ static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double
}
} }
+ CHECK_Z(getCCtxParams(zc, &savedParams));
+
/* multi-segments compression test */
XXH64_reset(&xxhState, 0);
{ ZSTD_outBuffer outBuff = { cBuffer, cBufferSize, 0 } ;
@@ -1761,15 +1986,18 @@ static int fuzzerTests_newAPI(U32 seed, U32 nbTests, unsigned startTest, double
} }
crcOrig = XXH64_digest(&xxhState);
cSize = outBuff.pos;
- DISPLAYLEVEL(5, "Frame completed : %u bytes \n", (U32)cSize);
+ DISPLAYLEVEL(5, "Frame completed : %zu bytes \n", cSize);
}
+ CHECK(badParameters(zc, savedParams), "CCtx params are wrong");
+
/* multi - fragments decompression test */
if (!dictSize /* don't reset if dictionary : could be different */ && (FUZ_rand(&lseed) & 1)) {
DISPLAYLEVEL(5, "resetting DCtx (dict:%08X) \n", (U32)(size_t)dict);
CHECK_Z( ZSTD_resetDStream(zd) );
} else {
- DISPLAYLEVEL(5, "using dict of size %u \n", (U32)dictSize);
+ if (dictSize)
+ DISPLAYLEVEL(5, "using dictionary of size %zu \n", dictSize);
CHECK_Z( ZSTD_initDStream_usingDict(zd, dict, dictSize) );
}
{ size_t decompressionResult = 1;
@@ -1853,7 +2081,7 @@ _output_error:
/*-*******************************************************
* Command line
*********************************************************/
-int FUZ_usage(const char* programName)
+static int FUZ_usage(const char* programName)
{
DISPLAY( "Usage :\n");
DISPLAY( " %s [args]\n", programName);
@@ -1883,7 +2111,6 @@ int main(int argc, const char** argv)
int bigTests = (sizeof(size_t) == 8);
e_api selected_api = simple_api;
const char* const programName = argv[0];
- U32 useOpaqueAPI = 0;
int argNb;
/* Check command line */
@@ -1896,7 +2123,6 @@ int main(int argc, const char** argv)
if (!strcmp(argument, "--mt")) { selected_api=mt_api; testNb += !testNb; continue; }
if (!strcmp(argument, "--newapi")) { selected_api=advanced_api; testNb += !testNb; continue; }
- if (!strcmp(argument, "--opaqueapi")) { selected_api=advanced_api; testNb += !testNb; useOpaqueAPI = 1; continue; }
if (!strcmp(argument, "--no-big-tests")) { bigTests=0; continue; }
argument++;
@@ -2012,7 +2238,7 @@ int main(int argc, const char** argv)
result = fuzzerTests_MT(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
break;
case advanced_api :
- result = fuzzerTests_newAPI(seed, nbTests, testNb, ((double)proba) / 100, bigTests, useOpaqueAPI);
+ result = fuzzerTests_newAPI(seed, nbTests, testNb, ((double)proba) / 100, bigTests);
break;
default :
assert(0); /* impossible */