2 * Copyright (c) 2015-present, Yann Collet, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
12 /*-************************************
14 **************************************/
15 #include "util.h" /* Ensure platform.h is compiled first; also : compiler options, UTIL_GetFileSize */
16 #include <stdlib.h> /* malloc */
17 #include <stdio.h> /* fprintf, fopen, ftello64 */
18 #include <string.h> /* strcmp */
19 #include <math.h> /* log */
23 #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_parameters, ZSTD_estimateCCtxSize */
28 #include "benchzstd.h"
29 #include "zstd_errors.h"
30 #include "zstd_internal.h" /* should not be needed */
33 /*-************************************
35 **************************************/
36 #define PROGRAM_DESCRIPTION "ZSTD parameters tester"
37 #define AUTHOR "Yann Collet"
38 #define WELCOME_MESSAGE "*** %s %s %i-bits, by %s ***\n", PROGRAM_DESCRIPTION, ZSTD_VERSION_STRING, (int)(sizeof(void*)*8), AUTHOR
40 #define TIMELOOP_NANOSEC (1*1000000000ULL) /* 1 second */
41 #define NB_LEVELS_TRACKED 22 /* ensured being >= ZSTD_maxCLevel() in BMK_init_level_constraints() */
43 static const size_t maxMemory = (sizeof(size_t)==4) ? (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31));
45 #define COMPRESSIBILITY_DEFAULT 0.50
47 static const U64 g_maxVariationTime = 60 * SEC_TO_MICRO;
48 static const int g_maxNbVariations = 64;
51 /*-************************************
53 **************************************/
54 #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
55 #define DISPLAYLEVEL(n, ...) if(g_displayLevel >= n) { fprintf(stderr, __VA_ARGS__); }
56 #define DEBUGOUTPUT(...) { if (DEBUG) DISPLAY(__VA_ARGS__); }
65 #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
66 #define MAX(a,b) ( (a) > (b) ? (a) : (b) )
67 #define CUSTOM_LEVEL 99
71 #define FADT_MAX ((U32)-1)
73 #define WLOG_RANGE (ZSTD_WINDOWLOG_MAX - ZSTD_WINDOWLOG_MIN + 1)
74 #define CLOG_RANGE (ZSTD_CHAINLOG_MAX - ZSTD_CHAINLOG_MIN + 1)
75 #define HLOG_RANGE (ZSTD_HASHLOG_MAX - ZSTD_HASHLOG_MIN + 1)
76 #define SLOG_RANGE (ZSTD_SEARCHLOG_MAX - ZSTD_SEARCHLOG_MIN + 1)
77 #define MML_RANGE (ZSTD_MINMATCH_MAX - ZSTD_MINMATCH_MIN + 1)
79 #define STRT_RANGE (ZSTD_STRATEGY_MAX - ZSTD_STRATEGY_MIN + 1)
82 #define CHECKTIME(r) { if(BMK_timeSpan_s(g_time) > g_timeLimit_s) { DEBUGOUTPUT("Time Limit Reached\n"); return r; } }
83 #define CHECKTIMEGT(ret, val, _gototag) { if(BMK_timeSpan_s(g_time) > g_timeLimit_s) { DEBUGOUTPUT("Time Limit Reached\n"); ret = val; goto _gototag; } }
85 #define PARAM_UNSET ((U32)-2) /* can't be -1 b/c fadt uses -1 */
87 static const char* g_stratName[ZSTD_STRATEGY_MAX+1] = {
88 "(none) ", "ZSTD_fast ", "ZSTD_dfast ",
89 "ZSTD_greedy ", "ZSTD_lazy ", "ZSTD_lazy2 ",
90 "ZSTD_btlazy2 ", "ZSTD_btopt ", "ZSTD_btultra ",
93 static const U32 tlen_table[TLEN_RANGE] = { 0, 1, 2, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 256, 512, 999 };
96 /*-************************************
97 * Setup for Adding new params
98 **************************************/
100 /* indices for each of the variables */
109 fadt_ind = 7, /* forceAttachDict */
114 U32 vals[NUM_PARAMS];
117 /* minimum value of parameters */
118 static const U32 mintable[NUM_PARAMS] =
119 { ZSTD_WINDOWLOG_MIN, ZSTD_CHAINLOG_MIN, ZSTD_HASHLOG_MIN, ZSTD_SEARCHLOG_MIN, ZSTD_MINMATCH_MIN, ZSTD_TARGETLENGTH_MIN, ZSTD_STRATEGY_MIN, FADT_MIN };
121 /* maximum value of parameters */
122 static const U32 maxtable[NUM_PARAMS] =
123 { ZSTD_WINDOWLOG_MAX, ZSTD_CHAINLOG_MAX, ZSTD_HASHLOG_MAX, ZSTD_SEARCHLOG_MAX, ZSTD_MINMATCH_MAX, ZSTD_TARGETLENGTH_MAX, ZSTD_STRATEGY_MAX, FADT_MAX };
125 /* # of values parameters can take on */
126 static const U32 rangetable[NUM_PARAMS] =
127 { WLOG_RANGE, CLOG_RANGE, HLOG_RANGE, SLOG_RANGE, MML_RANGE, TLEN_RANGE, STRT_RANGE, FADT_RANGE };
129 /* ZSTD_cctxSetParameter() index to set */
130 static const ZSTD_cParameter cctxSetParamTable[NUM_PARAMS] =
131 { ZSTD_c_windowLog, ZSTD_c_chainLog, ZSTD_c_hashLog, ZSTD_c_searchLog, ZSTD_c_minMatch, ZSTD_c_targetLength, ZSTD_c_strategy, ZSTD_c_forceAttachDict };
133 /* names of parameters */
134 static const char* g_paramNames[NUM_PARAMS] =
135 { "windowLog", "chainLog", "hashLog","searchLog", "minMatch", "targetLength", "strategy", "forceAttachDict" };
137 /* shortened names of parameters */
138 static const char* g_shortParamNames[NUM_PARAMS] =
139 { "wlog", "clog", "hlog", "slog", "mml", "tlen", "strat", "fadt" };
141 /* maps value from { 0 to rangetable[param] - 1 } to valid paramvalues */
142 static U32 rangeMap(varInds_t param, int ind)
144 ind = MAX(MIN(ind, (int)rangetable[param] - 1), 0);
146 case wlog_ind: /* using default: triggers -Wswitch-enum */
152 return mintable[param] + ind;
154 return tlen_table[ind];
155 case fadt_ind: /* 0, 1, 2 -> -1, 0, 1 */
160 DISPLAY("Error, not a valid param\n ");
165 /* inverse of rangeMap */
166 static int invRangeMap(varInds_t param, U32 value)
168 value = MIN(MAX(mintable[param], value), maxtable[param]);
176 return value - mintable[param];
177 case tlen_ind: /* bin search */
182 int mid = (lo + hi) / 2;
183 if(tlen_table[mid] < value) {
185 } if(tlen_table[mid] == value) {
194 return (int)value + 1;
198 DISPLAY("Error, not a valid param\n ");
203 /* display of params */
204 static void displayParamVal(FILE* f, varInds_t param, unsigned value, int width)
214 fprintf(f, "%*u", width, value);
216 fprintf(f, "%u", value);
221 fprintf(f, "%*s", width, g_stratName[value]);
223 fprintf(f, "%s", g_stratName[value]);
226 case fadt_ind: /* force attach dict */
228 fprintf(f, "%*d", width, (int)value);
230 fprintf(f, "%d", (int)value);
235 DISPLAY("Error, not a valid param\n ");
242 /*-************************************
243 * Benchmark Parameters/Global Variables
244 **************************************/
246 /* General Utility */
247 static U32 g_timeLimit_s = 99999; /* about 27 hours */
248 static UTIL_time_t g_time; /* to be used to compare solution finding speeds to compare to original */
249 static U32 g_blockSize = 0;
250 static U32 g_rand = 1;
253 static int g_displayLevel = 3;
254 static BYTE g_silenceParams[NUM_PARAMS]; /* can selectively silence some params when displaying them */
257 static U32 g_singleRun = 0;
258 static U32 g_optimizer = 0;
259 static int g_optmode = 0;
261 /* For cLevel Table generation */
262 static U32 g_target = 0;
263 static U32 g_noSeed = 0;
266 static paramValues_t g_params; /* Initialized at the beginning of main w/ emptyParams() function */
267 static double g_ratioMultiplier = 5.;
268 static U32 g_strictness = PARAM_UNSET; /* range 1 - 100, measure of how strict */
269 static BMK_benchResult_t g_lvltarget;
278 memoTableType_t tableType;
281 varInds_t varArray[NUM_PARAMS];
286 BMK_benchResult_t result;
287 paramValues_t params;
291 U32 cSpeed; /* bytes / sec */
293 U32 cMem; /* bytes */
296 typedef struct winner_ll_node winner_ll_node;
297 struct winner_ll_node {
299 winner_ll_node* next;
302 static winner_ll_node* g_winners; /* linked list sorted ascending by cSize & cSpeed */
305 * Additional Global Variables (Defined Above Use)
313 /*-*******************************************************
314 * General Util Functions
315 *********************************************************/
317 /* nullified useless params, to ensure count stats */
318 /* cleans up params for memoizing / display */
319 static paramValues_t sanitizeParams(paramValues_t params)
321 if (params.vals[strt_ind] == ZSTD_fast)
322 params.vals[clog_ind] = 0, params.vals[slog_ind] = 0;
323 if (params.vals[strt_ind] == ZSTD_dfast)
324 params.vals[slog_ind] = 0;
325 if ( (params.vals[strt_ind] < ZSTD_btopt) && (params.vals[strt_ind] != ZSTD_fast) )
326 params.vals[tlen_ind] = 0;
331 static ZSTD_compressionParameters pvalsToCParams(paramValues_t p)
333 ZSTD_compressionParameters c;
334 memset(&c, 0, sizeof(ZSTD_compressionParameters));
335 c.windowLog = p.vals[wlog_ind];
336 c.chainLog = p.vals[clog_ind];
337 c.hashLog = p.vals[hlog_ind];
338 c.searchLog = p.vals[slog_ind];
339 c.minMatch = p.vals[mml_ind];
340 c.targetLength = p.vals[tlen_ind];
341 c.strategy = p.vals[strt_ind];
342 /* no forceAttachDict */
346 static paramValues_t cParamsToPVals(ZSTD_compressionParameters c)
350 p.vals[wlog_ind] = c.windowLog;
351 p.vals[clog_ind] = c.chainLog;
352 p.vals[hlog_ind] = c.hashLog;
353 p.vals[slog_ind] = c.searchLog;
354 p.vals[mml_ind] = c.minMatch;
355 p.vals[tlen_ind] = c.targetLength;
356 p.vals[strt_ind] = c.strategy;
358 /* set all other params to their minimum value */
359 for (i = strt_ind + 1; i < NUM_PARAMS; i++) {
360 p.vals[i] = mintable[i];
365 /* equivalent of ZSTD_adjustCParams for paramValues_t */
367 adjustParams(paramValues_t p, const size_t maxBlockSize, const size_t dictSize)
369 paramValues_t ot = p;
371 p = cParamsToPVals(ZSTD_adjustCParams(pvalsToCParams(p), maxBlockSize, dictSize));
372 if (!dictSize) { p.vals[fadt_ind] = 0; }
373 /* retain value of all other parameters */
374 for(i = strt_ind + 1; i < NUM_PARAMS; i++) {
375 p.vals[i] = ot.vals[i];
380 static size_t BMK_findMaxMem(U64 requiredMem)
382 size_t const step = 64 MB;
383 void* testmem = NULL;
385 requiredMem = (((requiredMem >> 26) + 1) << 26);
386 if (requiredMem > maxMemory) requiredMem = maxMemory;
388 requiredMem += 2 * step;
389 while (!testmem && requiredMem > 0) {
390 testmem = malloc ((size_t)requiredMem);
395 return (size_t) requiredMem;
398 /* accuracy in seconds only, span can be multiple years */
399 static U32 BMK_timeSpan_s(const UTIL_time_t tStart)
401 return (U32)(UTIL_clockSpanMicro(tStart) / 1000000ULL);
404 static U32 FUZ_rotl32(U32 x, U32 r)
406 return ((x << r) | (x >> (32 - r)));
409 static U32 FUZ_rand(U32* src)
411 const U32 prime1 = 2654435761U;
412 const U32 prime2 = 2246822519U;
416 rand32 = FUZ_rotl32(rand32, 13);
421 #define BOUNDCHECK(val,min,max) { \
422 if (((val)<(min)) | ((val)>(max))) { \
423 DISPLAY("INVALID PARAMETER CONSTRAINTS\n"); \
427 static int paramValid(const paramValues_t paramTarget)
430 for(i = 0; i < NUM_PARAMS; i++) {
431 BOUNDCHECK(paramTarget.vals[i], mintable[i], maxtable[i]);
436 /* cParamUnsetMin() :
437 * if any parameter in paramTarget is not yet set,
438 * it will receive its corresponding minimal value.
439 * This function never fails */
440 static paramValues_t cParamUnsetMin(paramValues_t paramTarget)
443 for (vi = 0; vi < NUM_PARAMS; vi++) {
444 if (paramTarget.vals[vi] == PARAM_UNSET) {
445 paramTarget.vals[vi] = mintable[vi];
451 static paramValues_t emptyParams(void)
455 for(i = 0; i < NUM_PARAMS; i++) {
456 p.vals[i] = PARAM_UNSET;
461 static winnerInfo_t initWinnerInfo(const paramValues_t p)
464 w1.result.cSpeed = 0.;
465 w1.result.dSpeed = 0.;
466 w1.result.cMem = (size_t)-1;
467 w1.result.cSize = (size_t)-1;
473 overwriteParams(paramValues_t base, const paramValues_t mask)
476 for(i = 0; i < NUM_PARAMS; i++) {
477 if(mask.vals[i] != PARAM_UNSET) {
478 base.vals[i] = mask.vals[i];
485 paramVaryOnce(const varInds_t paramIndex, const int amt, paramValues_t* ptr)
487 ptr->vals[paramIndex] = rangeMap(paramIndex,
488 invRangeMap(paramIndex, ptr->vals[paramIndex]) + amt);
491 /* varies ptr by nbChanges respecting varyParams*/
493 paramVariation(paramValues_t* ptr, memoTable_t* mtAll, const U32 nbChanges)
500 for (i = 0 ; i < nbChanges ; i++) {
501 const U32 changeID = (U32)FUZ_rand(&g_rand) % (mtAll[p.vals[strt_ind]].varLen << 1);
502 paramVaryOnce(mtAll[p.vals[strt_ind]].varArray[changeID >> 1], ((changeID & 1) << 1) - 1, &p);
504 validated = paramValid(p);
509 /* Completely random parameter selection */
510 static paramValues_t randomParams(void)
512 varInds_t v; paramValues_t p;
513 for(v = 0; v < NUM_PARAMS; v++) {
514 p.vals[v] = rangeMap(v, FUZ_rand(&g_rand) % rangetable[v]);
519 static U64 g_clockGranularity = 100000000ULL;
521 static void init_clockGranularity(void)
523 UTIL_time_t const clockStart = UTIL_getTime();
524 U64 el1 = 0, el2 = 0;
528 el2 = UTIL_clockSpanNano(clockStart);
531 if(g_clockGranularity > iv) {
532 g_clockGranularity = iv;
539 DEBUGOUTPUT("Granularity: %llu\n", (unsigned long long)g_clockGranularity);
542 /*-************************************
543 * Optimizer Util Functions
544 **************************************/
546 /* checks results are feasible */
547 static int feasible(const BMK_benchResult_t results, const constraint_t target) {
548 return (results.cSpeed >= target.cSpeed)
549 && (results.dSpeed >= target.dSpeed)
550 && (results.cMem <= target.cMem)
551 && (!g_optmode || results.cSize <= g_lvltarget.cSize);
554 /* hill climbing value for part 1 */
555 /* Scoring here is a linear reward for all set constraints normalized between 0 to 1
556 * (with 0 at 0 and 1 being fully fulfilling the constraint), summed with a logarithmic
557 * bonus to exceeding the constraint value. We also give linear ratio for compression ratio.
558 * The constant factors are experimental.
561 resultScore(const BMK_benchResult_t res, const size_t srcSize, const constraint_t target)
563 double cs = 0., ds = 0., rt, cm = 0.;
564 const double r1 = 1, r2 = 0.1, rtr = 0.5;
566 if(target.cSpeed) { cs = res.cSpeed / (double)target.cSpeed; }
567 if(target.dSpeed) { ds = res.dSpeed / (double)target.dSpeed; }
568 if(target.cMem != (U32)-1) { cm = (double)target.cMem / res.cMem; }
569 rt = ((double)srcSize / res.cSize);
571 ret = (MIN(1, cs) + MIN(1, ds) + MIN(1, cm))*r1 + rt * rtr +
572 (MAX(0, log(cs))+ MAX(0, log(ds))+ MAX(0, log(cm))) * r2;
577 /* calculates normalized squared euclidean distance of result1 if it is in the first quadrant relative to lvlRes */
579 resultDistLvl(const BMK_benchResult_t result1, const BMK_benchResult_t lvlRes)
581 double normalizedCSpeedGain1 = (result1.cSpeed / lvlRes.cSpeed) - 1;
582 double normalizedRatioGain1 = ((double)lvlRes.cSize / result1.cSize) - 1;
583 if(normalizedRatioGain1 < 0 || normalizedCSpeedGain1 < 0) {
586 return normalizedRatioGain1 * g_ratioMultiplier + normalizedCSpeedGain1;
589 /* return true if r2 strictly better than r1 */
591 compareResultLT(const BMK_benchResult_t result1, const BMK_benchResult_t result2, const constraint_t target, size_t srcSize)
593 if(feasible(result1, target) && feasible(result2, target)) {
595 return resultDistLvl(result1, g_lvltarget) < resultDistLvl(result2, g_lvltarget);
597 return (result1.cSize > result2.cSize)
598 || (result1.cSize == result2.cSize && result2.cSpeed > result1.cSpeed)
599 || (result1.cSize == result2.cSize && result2.cSpeed == result1.cSpeed && result2.dSpeed > result1.dSpeed);
602 return feasible(result2, target)
603 || (!feasible(result1, target)
604 && (resultScore(result1, srcSize, target) < resultScore(result2, srcSize, target)));
607 static constraint_t relaxTarget(constraint_t target) {
608 target.cMem = (U32)-1;
609 target.cSpeed *= ((double)g_strictness) / 100;
610 target.dSpeed *= ((double)g_strictness) / 100;
614 static void optimizerAdjustInput(paramValues_t* pc, const size_t maxBlockSize)
617 for(v = 0; v < NUM_PARAMS; v++) {
618 if(pc->vals[v] != PARAM_UNSET) {
619 U32 newval = MIN(MAX(pc->vals[v], mintable[v]), maxtable[v]);
620 if(newval != pc->vals[v]) {
621 pc->vals[v] = newval;
622 DISPLAY("Warning: parameter %s not in valid range, adjusting to ",
624 displayParamVal(stderr, v, newval, 0); DISPLAY("\n");
629 if(pc->vals[wlog_ind] != PARAM_UNSET) {
631 U32 sshb = maxBlockSize > 1 ? ZSTD_highbit32((U32)(maxBlockSize-1)) + 1 : 1;
632 /* edge case of highBit not working for 0 */
634 if(maxBlockSize < (1ULL << 31) && sshb + 1 < pc->vals[wlog_ind]) {
635 U32 adjust = MAX(mintable[wlog_ind], sshb);
636 if(adjust != pc->vals[wlog_ind]) {
637 pc->vals[wlog_ind] = adjust;
638 DISPLAY("Warning: windowLog larger than src/block size, adjusted to %u\n",
639 (unsigned)pc->vals[wlog_ind]);
644 if(pc->vals[wlog_ind] != PARAM_UNSET && pc->vals[clog_ind] != PARAM_UNSET) {
646 if(pc->vals[strt_ind] == PARAM_UNSET || pc->vals[strt_ind] >= (U32)ZSTD_btlazy2) {
647 maxclog = pc->vals[wlog_ind] + 1;
649 maxclog = pc->vals[wlog_ind];
652 if(pc->vals[clog_ind] > maxclog) {
653 pc->vals[clog_ind] = maxclog;
654 DISPLAY("Warning: chainlog too much larger than windowLog size, adjusted to %u\n",
655 (unsigned)pc->vals[clog_ind]);
659 if(pc->vals[wlog_ind] != PARAM_UNSET && pc->vals[hlog_ind] != PARAM_UNSET) {
660 if(pc->vals[wlog_ind] + 1 < pc->vals[hlog_ind]) {
661 pc->vals[hlog_ind] = pc->vals[wlog_ind] + 1;
662 DISPLAY("Warning: hashlog too much larger than windowLog size, adjusted to %u\n",
663 (unsigned)pc->vals[hlog_ind]);
667 if(pc->vals[slog_ind] != PARAM_UNSET && pc->vals[clog_ind] != PARAM_UNSET) {
668 if(pc->vals[slog_ind] > pc->vals[clog_ind]) {
669 pc->vals[clog_ind] = pc->vals[slog_ind];
670 DISPLAY("Warning: searchLog larger than chainLog, adjusted to %u\n",
671 (unsigned)pc->vals[slog_ind]);
677 redundantParams(const paramValues_t paramValues, const constraint_t target, const size_t maxBlockSize)
680 (ZSTD_estimateCStreamSize_usingCParams(pvalsToCParams(paramValues)) > (size_t)target.cMem) /* Uses too much memory */
681 || ((1ULL << (paramValues.vals[wlog_ind] - 1)) >= maxBlockSize && paramValues.vals[wlog_ind] != mintable[wlog_ind]) /* wlog too much bigger than src size */
682 || (paramValues.vals[clog_ind] > (paramValues.vals[wlog_ind] + (paramValues.vals[strt_ind] > ZSTD_btlazy2))) /* chainLog larger than windowLog*/
683 || (paramValues.vals[slog_ind] > paramValues.vals[clog_ind]) /* searchLog larger than chainLog */
684 || (paramValues.vals[hlog_ind] > paramValues.vals[wlog_ind] + 1); /* hashLog larger than windowLog + 1 */
688 /*-************************************
690 **************************************/
692 /* BMK_paramValues_into_commandLine() :
693 * transform a set of parameters paramValues_t
694 * into a command line compatible with `zstd` syntax
695 * and writes it into FILE* f.
696 * f must be already opened and writable */
698 BMK_paramValues_into_commandLine(FILE* f, const paramValues_t params)
702 fprintf(f,"--zstd=");
703 for (v = 0; v < NUM_PARAMS; v++) {
704 if (g_silenceParams[v]) { continue; }
705 if (!first) { fprintf(f, ","); }
706 fprintf(f,"%s=", g_paramNames[v]);
708 if (v == strt_ind) { fprintf(f,"%u", (unsigned)params.vals[v]); }
709 else { displayParamVal(f, v, params.vals[v], 0); }
716 /* comparison function: */
717 /* strictly better, strictly worse, equal, speed-side adv, size-side adv */
718 #define WORSE_RESULT 0
719 #define BETTER_RESULT 1
720 #define ERROR_RESULT 2
722 #define SPEED_RESULT 4
723 #define SIZE_RESULT 5
724 /* maybe have epsilon-eq to limit table size? */
726 speedSizeCompare(const BMK_benchResult_t r1, const BMK_benchResult_t r2)
728 if(r1.cSpeed < r2.cSpeed) {
729 if(r1.cSize >= r2.cSize) {
730 return BETTER_RESULT;
732 return SPEED_RESULT; /* r2 is smaller but not faster. */
734 if(r1.cSize <= r2.cSize) {
737 return SIZE_RESULT; /* r2 is faster but not smaller */
741 /* 0 for insertion, 1 for no insert */
742 /* maintain invariant speedSizeCompare(n, n->next) = SPEED_RESULT */
744 insertWinner(const winnerInfo_t w, const constraint_t targetConstraints)
746 BMK_benchResult_t r = w.result;
747 winner_ll_node* cur_node = g_winners;
748 /* first node to insert */
749 if(!feasible(r, targetConstraints)) {
753 if(g_winners == NULL) {
754 winner_ll_node* first_node = malloc(sizeof(winner_ll_node));
755 if(first_node == NULL) {
758 first_node->next = NULL;
760 g_winners = first_node;
764 while(cur_node->next != NULL) {
765 switch(speedSizeCompare(cur_node->res.result, r)) {
768 return 1; /* never insert if better */
773 cur_node->res = cur_node->next->res;
774 tmp = cur_node->next;
775 cur_node->next = cur_node->next->next;
781 cur_node = cur_node->next;
784 case SPEED_RESULT: /* insert after first size result, then return */
786 winner_ll_node* newnode = malloc(sizeof(winner_ll_node));
787 if(newnode == NULL) {
790 newnode->res = cur_node->res;
792 newnode->next = cur_node->next;
793 cur_node->next = newnode;
800 assert(cur_node->next == NULL);
801 switch(speedSizeCompare(cur_node->res.result, r)) {
804 return 1; /* never insert if better */
813 winner_ll_node* newnode = malloc(sizeof(winner_ll_node));
814 if(newnode == NULL) {
818 newnode->next = NULL;
819 cur_node->next = newnode;
822 case SPEED_RESULT: /* insert before first size result, then return */
824 winner_ll_node* newnode = malloc(sizeof(winner_ll_node));
825 if(newnode == NULL) {
828 newnode->res = cur_node->res;
830 newnode->next = cur_node->next;
831 cur_node->next = newnode;
840 BMK_displayOneResult(FILE* f, winnerInfo_t res, const size_t srcSize)
844 res.params = cParamUnsetMin(res.params);
846 for (v = 0; v < NUM_PARAMS; v++) {
847 if (g_silenceParams[v]) { continue; }
848 if (!first) { fprintf(f, ","); }
849 displayParamVal(f, v, res.params.vals[v], 3);
853 { double const ratio = res.result.cSize ?
854 (double)srcSize / res.result.cSize : 0;
855 double const cSpeedMBps = (double)res.result.cSpeed / MB_UNIT;
856 double const dSpeedMBps = (double)res.result.dSpeed / MB_UNIT;
858 fprintf(f, " }, /* R:%5.3f at %5.1f MB/s - %5.1f MB/s */\n",
859 ratio, cSpeedMBps, dSpeedMBps);
863 /* Writes to f the results of a parameter benchmark */
864 /* when used with --optimize, will only print results better than previously discovered */
866 BMK_printWinner(FILE* f, const int cLevel, const BMK_benchResult_t result, const paramValues_t params, const size_t srcSize)
868 char lvlstr[15] = "Custom Level";
873 fprintf(f, "\r%79s\r", "");
875 if(cLevel != CUSTOM_LEVEL) {
876 snprintf(lvlstr, 15, " Level %2d ", cLevel);
880 const U64 mn_in_ns = 60ULL * TIMELOOP_NANOSEC;
881 const U64 time_ns = UTIL_clockSpanNano(g_time);
882 const U64 minutes = time_ns / mn_in_ns;
883 fprintf(f, "%1lu:%2lu:%05.2f - ",
884 (unsigned long) minutes / 60,
885 (unsigned long) minutes % 60,
886 (double)(time_ns - (minutes * mn_in_ns)) / TIMELOOP_NANOSEC );
889 fprintf(f, "/* %s */ ", lvlstr);
890 BMK_displayOneResult(f, w, srcSize);
894 BMK_printWinnerOpt(FILE* f, const U32 cLevel, const BMK_benchResult_t result, const paramValues_t params, const constraint_t targetConstraints, const size_t srcSize)
896 /* global winner used for constraints */
897 /* cSize, cSpeed, dSpeed, cMem */
898 static winnerInfo_t g_winner = { { (size_t)-1LL, 0, 0, (size_t)-1LL },
899 { { PARAM_UNSET, PARAM_UNSET, PARAM_UNSET, PARAM_UNSET, PARAM_UNSET, PARAM_UNSET, PARAM_UNSET, PARAM_UNSET } }
902 || compareResultLT(g_winner.result, result, targetConstraints, srcSize)
903 || g_displayLevel >= 4) {
905 && compareResultLT(g_winner.result, result, targetConstraints, srcSize)) {
906 DISPLAY("New Winner: \n");
909 if(g_displayLevel >= 2) {
910 BMK_printWinner(f, cLevel, result, params, srcSize);
913 if(compareResultLT(g_winner.result, result, targetConstraints, srcSize)) {
914 if(g_displayLevel >= 1) { BMK_paramValues_into_commandLine(f, params); }
915 g_winner.result = result;
916 g_winner.params = params;
920 if(g_optmode && g_optimizer && (DEBUG || g_displayLevel == 3)) {
925 insertWinner(w, targetConstraints);
927 if(!DEBUG) { fprintf(f, "\033c"); }
931 fprintf(f, "================================\n");
932 for(n = g_winners; n != NULL; n = n->next) {
933 BMK_displayOneResult(f, n->res, srcSize);
935 fprintf(f, "================================\n");
936 fprintf(f, "Level Bounds: R: > %.3f AND C: < %.1f MB/s \n\n",
937 (double)srcSize / g_lvltarget.cSize, (double)g_lvltarget.cSpeed / MB_UNIT);
940 fprintf(f, "Overall Winner: \n");
941 BMK_displayOneResult(f, g_winner, srcSize);
942 BMK_paramValues_into_commandLine(f, g_winner.params);
944 fprintf(f, "Latest BMK: \n");\
945 BMK_displayOneResult(f, w, srcSize);
950 /* BMK_print_cLevelEntry() :
951 * Writes one cLevelTable entry, for one level.
952 * f must exist, be already opened, and be seekable.
953 * this function cannot error.
956 BMK_print_cLevelEntry(FILE* f, const int cLevel,
957 paramValues_t params,
958 const BMK_benchResult_t result, const size_t srcSize)
964 assert(cLevel <= NB_LEVELS_TRACKED);
965 params = cParamUnsetMin(params);
969 * assumption : all cParams are present and in order in the following range */
970 for (v = 0; v <= strt_ind; v++) {
971 if (!first) { fprintf(f, ","); }
972 displayParamVal(f, v, params.vals[v], 3);
976 { double const ratio = result.cSize ?
977 (double)srcSize / result.cSize : 0;
978 double const cSpeedMBps = (double)result.cSpeed / MB_UNIT;
979 double const dSpeedMBps = (double)result.dSpeed / MB_UNIT;
981 fprintf(f, " }, /* level %2i: R=%5.3f at %5.1f MB/s - %5.1f MB/s */\n",
982 cLevel, ratio, cSpeedMBps, dSpeedMBps);
987 /* BMK_print_cLevelTable() :
988 * print candidate compression table into proposed FILE* f.
989 * f must exist, be already opened, and be seekable.
990 * winners must be a table of NB_LEVELS_TRACKED+1 elements winnerInfo_t, all entries presumed initialized
991 * this function cannot error.
994 BMK_print_cLevelTable(FILE* f, const winnerInfo_t* winners, const size_t srcSize)
998 fprintf(f, "\n /* Proposed configurations : */ \n");
999 fprintf(f, " /* W, C, H, S, L, T, strat */ \n");
1001 for (cLevel=0; cLevel <= NB_LEVELS_TRACKED; cLevel++)
1002 BMK_print_cLevelEntry(f,
1003 cLevel, winners[cLevel].params,
1004 winners[cLevel].result, srcSize);
1008 /* BMK_saveAndPrint_cLevelTable() :
1009 * save candidate compression table into FILE* f,
1010 * and then to stdout.
1011 * f must exist, be already opened, and be seekable.
1012 * winners must be a table of NB_LEVELS_TRACKED+1 elements winnerInfo_t, all entries presumed initialized
1013 * this function cannot error.
1016 BMK_saveAndPrint_cLevelTable(FILE* const f,
1017 const winnerInfo_t* winners,
1018 const size_t srcSize)
1020 fseek(f, 0, SEEK_SET);
1021 BMK_print_cLevelTable(f, winners, srcSize);
1023 BMK_print_cLevelTable(stdout, winners, srcSize);
1027 /*-*******************************************************
1028 * Functions to Benchmark
1029 *********************************************************/
1033 const void* dictBuffer;
1034 size_t dictBufferSize;
1036 const paramValues_t* comprParams;
1039 static size_t local_initCCtx(void* payload) {
1040 const BMK_initCCtxArgs* ag = (const BMK_initCCtxArgs*)payload;
1042 ZSTD_CCtx_reset(ag->cctx, ZSTD_reset_session_and_parameters);
1043 ZSTD_CCtx_setParameter(ag->cctx, ZSTD_c_compressionLevel, ag->cLevel);
1045 for(i = 0; i < NUM_PARAMS; i++) {
1046 if(ag->comprParams->vals[i] != PARAM_UNSET)
1047 ZSTD_CCtx_setParameter(ag->cctx, cctxSetParamTable[i], ag->comprParams->vals[i]);
1049 ZSTD_CCtx_loadDictionary(ag->cctx, ag->dictBuffer, ag->dictBufferSize);
1056 const void* dictBuffer;
1057 size_t dictBufferSize;
1060 static size_t local_initDCtx(void* payload) {
1061 const BMK_initDCtxArgs* ag = (const BMK_initDCtxArgs*)payload;
1062 ZSTD_DCtx_reset(ag->dctx, ZSTD_reset_session_and_parameters);
1063 ZSTD_DCtx_loadDictionary(ag->dctx, ag->dictBuffer, ag->dictBufferSize);
1067 /* additional argument is just the context */
1068 static size_t local_defaultCompress(
1069 const void* srcBuffer, size_t srcSize,
1070 void* dstBuffer, size_t dstSize,
1073 ZSTD_CCtx* cctx = (ZSTD_CCtx*)addArgs;
1074 assert(dstSize == ZSTD_compressBound(srcSize)); /* specific to this version, which is only used in paramgrill */
1075 return ZSTD_compress2(cctx, dstBuffer, dstSize, srcBuffer, srcSize);
1078 /* additional argument is just the context */
1079 static size_t local_defaultDecompress(
1080 const void* srcBuffer, size_t srcSize,
1081 void* dstBuffer, size_t dstSize,
1083 size_t moreToFlush = 1;
1084 ZSTD_DCtx* dctx = (ZSTD_DCtx*)addArgs;
1090 out.dst = dstBuffer;
1093 while (moreToFlush) {
1094 if(out.pos == out.size) {
1095 return (size_t)-ZSTD_error_dstSize_tooSmall;
1097 moreToFlush = ZSTD_decompressStream(dctx,
1099 if (ZSTD_isError(moreToFlush)) {
1107 /*-************************************
1108 * Data Initialization Functions
1109 **************************************/
1114 const void** srcPtrs;
1117 size_t* dstCapacities;
1122 size_t maxBlockSize;
1132 static void freeNonSrcBuffers(const buffers_t b) {
1136 if(b.dstPtrs != NULL) {
1140 free(b.dstCapacities);
1143 if(b.resPtrs != NULL) {
1150 static void freeBuffers(const buffers_t b) {
1151 if(b.srcPtrs != NULL) {
1154 freeNonSrcBuffers(b);
1157 /* srcBuffer will be freed by freeBuffers now */
1158 static int createBuffersFromMemory(buffers_t* buff, void * srcBuffer, const size_t nbFiles,
1159 const size_t* fileSizes)
1161 size_t pos = 0, n, blockSize;
1162 U32 maxNbBlocks, blockNb = 0;
1164 for(n = 0; n < nbFiles; n++) {
1165 buff->srcSize += fileSizes[n];
1168 if(buff->srcSize == 0) {
1169 DISPLAY("No data to bench\n");
1173 blockSize = g_blockSize ? g_blockSize : buff->srcSize;
1174 maxNbBlocks = (U32) ((buff->srcSize + (blockSize-1)) / blockSize) + (U32)nbFiles;
1176 buff->srcPtrs = (const void**)calloc(maxNbBlocks, sizeof(void*));
1177 buff->srcSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
1179 buff->dstPtrs = (void**)calloc(maxNbBlocks, sizeof(void*));
1180 buff->dstCapacities = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
1181 buff->dstSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
1183 buff->resPtrs = (void**)calloc(maxNbBlocks, sizeof(void*));
1184 buff->resSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
1186 if(!buff->srcPtrs || !buff->srcSizes || !buff->dstPtrs || !buff->dstCapacities || !buff->dstSizes || !buff->resPtrs || !buff->resSizes) {
1187 DISPLAY("alloc error\n");
1188 freeNonSrcBuffers(*buff);
1192 buff->srcBuffer = srcBuffer;
1193 buff->srcPtrs[0] = (const void*)buff->srcBuffer;
1194 buff->dstPtrs[0] = malloc(ZSTD_compressBound(buff->srcSize) + (maxNbBlocks * 1024));
1195 buff->resPtrs[0] = malloc(buff->srcSize);
1197 if(!buff->dstPtrs[0] || !buff->resPtrs[0]) {
1198 DISPLAY("alloc error\n");
1199 freeNonSrcBuffers(*buff);
1203 for(n = 0; n < nbFiles; n++) {
1204 size_t pos_end = pos + fileSizes[n];
1205 for(; pos < pos_end; blockNb++) {
1206 buff->srcPtrs[blockNb] = (const void*)((char*)srcBuffer + pos);
1207 buff->srcSizes[blockNb] = blockSize;
1211 if(fileSizes[n] > 0) { buff->srcSizes[blockNb - 1] = ((fileSizes[n] - 1) % blockSize) + 1; }
1215 buff->dstCapacities[0] = ZSTD_compressBound(buff->srcSizes[0]);
1216 buff->dstSizes[0] = buff->dstCapacities[0];
1217 buff->resSizes[0] = buff->srcSizes[0];
1218 buff->maxBlockSize = buff->srcSizes[0];
1220 for(n = 1; n < blockNb; n++) {
1221 buff->dstPtrs[n] = ((char*)buff->dstPtrs[n-1]) + buff->dstCapacities[n-1];
1222 buff->resPtrs[n] = ((char*)buff->resPtrs[n-1]) + buff->resSizes[n-1];
1223 buff->dstCapacities[n] = ZSTD_compressBound(buff->srcSizes[n]);
1224 buff->dstSizes[n] = buff->dstCapacities[n];
1225 buff->resSizes[n] = buff->srcSizes[n];
1227 buff->maxBlockSize = MAX(buff->maxBlockSize, buff->srcSizes[n]);
1230 buff->nbBlocks = blockNb;
1235 /* allocates buffer's arguments. returns success / failuere */
1236 static int createBuffers(buffers_t* buff, const char* const * const fileNamesTable,
1240 size_t totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, (U32)nbFiles);
1241 size_t benchedSize = MIN(BMK_findMaxMem(totalSizeToLoad * 3) / 3, totalSizeToLoad);
1242 size_t* fileSizes = calloc(sizeof(size_t), nbFiles);
1243 void* srcBuffer = NULL;
1246 if(!totalSizeToLoad || !benchedSize) {
1248 DISPLAY("Nothing to Bench\n");
1252 srcBuffer = malloc(benchedSize);
1254 if(!fileSizes || !srcBuffer) {
1259 for(n = 0; n < nbFiles; n++) {
1261 U64 fileSize = UTIL_getFileSize(fileNamesTable[n]);
1262 if (UTIL_isDirectory(fileNamesTable[n])) {
1263 DISPLAY("Ignoring %s directory... \n", fileNamesTable[n]);
1266 if (fileSize == UTIL_FILESIZE_UNKNOWN) {
1267 DISPLAY("Cannot evaluate size of %s, ignoring ... \n", fileNamesTable[n]);
1270 f = fopen(fileNamesTable[n], "rb");
1272 DISPLAY("impossible to open file %s\n", fileNamesTable[n]);
1278 DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[n]);
1280 if (fileSize + pos > benchedSize) fileSize = benchedSize - pos, nbFiles=n; /* buffer too small - stop after this file */
1282 char* buffer = (char*)(srcBuffer);
1283 size_t const readSize = fread((buffer)+pos, 1, (size_t)fileSize, f);
1285 if (readSize != (size_t)fileSize) {
1286 DISPLAY("could not read %s", fileNamesTable[n]);
1291 fileSizes[n] = readSize;
1296 ret = createBuffersFromMemory(buff, srcBuffer, nbFiles, fileSizes);
1299 if(ret) { free(srcBuffer); }
1304 static void freeContexts(const contexts_t ctx) {
1305 free(ctx.dictBuffer);
1306 ZSTD_freeCCtx(ctx.cctx);
1307 ZSTD_freeDCtx(ctx.dctx);
1310 static int createContexts(contexts_t* ctx, const char* dictFileName) {
1313 ctx->cctx = ZSTD_createCCtx();
1314 ctx->dctx = ZSTD_createDCtx();
1315 assert(ctx->cctx != NULL);
1316 assert(ctx->dctx != NULL);
1318 if(dictFileName == NULL) {
1320 ctx->dictBuffer = NULL;
1323 { U64 const dictFileSize = UTIL_getFileSize(dictFileName);
1324 assert(dictFileSize != UTIL_FILESIZE_UNKNOWN);
1325 ctx->dictSize = dictFileSize;
1326 assert((U64)ctx->dictSize == dictFileSize); /* check overflow */
1328 ctx->dictBuffer = malloc(ctx->dictSize);
1330 f = fopen(dictFileName, "rb");
1333 DISPLAY("unable to open file\n");
1338 if (ctx->dictSize > 64 MB || !(ctx->dictBuffer)) {
1339 DISPLAY("dictionary too large\n");
1344 readSize = fread(ctx->dictBuffer, 1, ctx->dictSize, f);
1346 if (readSize != ctx->dictSize) {
1347 DISPLAY("unable to read file\n");
1354 /*-************************************
1355 * Optimizer Memoization Functions
1356 **************************************/
1358 /* return: new length */
1359 /* keep old array, will need if iter over strategy. */
1360 /* prunes useless params */
1361 static size_t sanitizeVarArray(varInds_t* varNew, const size_t varLength, const varInds_t* varArray, const ZSTD_strategy strat) {
1363 for(i = 0; i < varLength; i++) {
1364 if( !((varArray[i] == clog_ind && strat == ZSTD_fast)
1365 || (varArray[i] == slog_ind && strat == ZSTD_fast)
1366 || (varArray[i] == slog_ind && strat == ZSTD_dfast)
1367 || (varArray[i] == tlen_ind && strat < ZSTD_btopt && strat != ZSTD_fast))) {
1368 varNew[j] = varArray[i];
1375 /* res should be NUM_PARAMS size */
1376 /* constructs varArray from paramValues_t style parameter */
1377 /* pass in using dict. */
1378 static size_t variableParams(const paramValues_t paramConstraints, varInds_t* res, const int usingDictionary) {
1381 for(i = 0; i < NUM_PARAMS; i++) {
1382 if(paramConstraints.vals[i] == PARAM_UNSET) {
1383 if(i == fadt_ind && !usingDictionary) continue; /* don't use fadt if no dictionary */
1390 /* length of memo table given free variables */
1391 static size_t memoTableLen(const varInds_t* varyParams, const size_t varyLen) {
1392 size_t arrayLen = 1;
1394 for(i = 0; i < varyLen; i++) {
1395 if(varyParams[i] == strt_ind) continue; /* strategy separated by table */
1396 arrayLen *= rangetable[varyParams[i]];
1401 /* returns unique index in memotable of compression parameters */
1402 static unsigned memoTableIndDirect(const paramValues_t* ptr, const varInds_t* varyParams, const size_t varyLen) {
1405 for(i = 0; i < varyLen; i++) {
1406 varInds_t v = varyParams[i];
1407 if(v == strt_ind) continue; /* exclude strategy from memotable */
1408 ind *= rangetable[v]; ind += (unsigned)invRangeMap(v, ptr->vals[v]);
1413 static size_t memoTableGet(const memoTable_t* memoTableArray, const paramValues_t p) {
1414 const memoTable_t mt = memoTableArray[p.vals[strt_ind]];
1415 switch(mt.tableType) {
1417 return mt.table[memoTableIndDirect(&p, mt.varArray, mt.varLen)];
1419 return mt.table[(XXH64(&p.vals, sizeof(U32) * NUM_PARAMS, 0) >> 3) % mt.tableLen];
1423 return 0; /* should never happen, stop compiler warnings */
1426 static void memoTableSet(const memoTable_t* memoTableArray, const paramValues_t p, const BYTE value) {
1427 const memoTable_t mt = memoTableArray[p.vals[strt_ind]];
1428 switch(mt.tableType) {
1430 mt.table[memoTableIndDirect(&p, mt.varArray, mt.varLen)] = value; break;
1432 mt.table[(XXH64(&p.vals, sizeof(U32) * NUM_PARAMS, 0) >> 3) % mt.tableLen] = value; break;
1438 /* frees all allocated memotables */
1439 /* secret contract :
1440 * mtAll is a table of (ZSTD_STRATEGY_MAX+1) memoTable_t */
1441 static void freeMemoTableArray(memoTable_t* const mtAll) {
1443 if(mtAll == NULL) { return; }
1444 for(i = 1; i <= (int)ZSTD_STRATEGY_MAX; i++) {
1445 free(mtAll[i].table);
1450 /* inits memotables for all (including mallocs), all strategies */
1451 /* takes unsanitized varyParams */
1453 createMemoTableArray(const paramValues_t p,
1454 const varInds_t* const varyParams,
1455 const size_t varyLen,
1456 const U32 memoTableLog)
1458 memoTable_t* const mtAll = (memoTable_t*)calloc(sizeof(memoTable_t),(ZSTD_STRATEGY_MAX + 1));
1459 ZSTD_strategy i, stratMin = ZSTD_STRATEGY_MIN, stratMax = ZSTD_STRATEGY_MAX;
1465 for(i = 1; i <= (int)ZSTD_STRATEGY_MAX; i++) {
1466 mtAll[i].varLen = sanitizeVarArray(mtAll[i].varArray, varyLen, varyParams, i);
1469 /* no memoization */
1470 if(memoTableLog == 0) {
1471 for(i = 1; i <= (int)ZSTD_STRATEGY_MAX; i++) {
1472 mtAll[i].tableType = noMemo;
1473 mtAll[i].table = NULL;
1474 mtAll[i].tableLen = 0;
1480 if(p.vals[strt_ind] != PARAM_UNSET) {
1481 stratMin = p.vals[strt_ind];
1482 stratMax = p.vals[strt_ind];
1486 for(i = stratMin; i <= stratMax; i++) {
1487 size_t mtl = memoTableLen(mtAll[i].varArray, mtAll[i].varLen);
1488 mtAll[i].tableType = directMap;
1490 if(memoTableLog != PARAM_UNSET && mtl > (1ULL << memoTableLog)) { /* use hash table */ /* provide some option to only use hash tables? */
1491 mtAll[i].tableType = xxhashMap;
1492 mtl = (1ULL << memoTableLog);
1495 mtAll[i].table = (BYTE*)calloc(sizeof(BYTE), mtl);
1496 mtAll[i].tableLen = mtl;
1498 if(mtAll[i].table == NULL) {
1499 freeMemoTableArray(mtAll);
1507 /* Sets pc to random unmeasured set of parameters */
1508 /* specifiy strategy */
1509 static void randomConstrainedParams(paramValues_t* pc, const memoTable_t* memoTableArray, const ZSTD_strategy st)
1512 const memoTable_t mt = memoTableArray[st];
1513 pc->vals[strt_ind] = st;
1514 for(j = 0; j < mt.tableLen; j++) {
1516 for(i = 0; i < NUM_PARAMS; i++) {
1517 varInds_t v = mt.varArray[i];
1518 if(v == strt_ind) continue;
1519 pc->vals[v] = rangeMap(v, FUZ_rand(&g_rand) % rangetable[v]);
1522 if(!(memoTableGet(memoTableArray, *pc))) break; /* only pick unpicked params. */
1526 /*-************************************
1527 * Benchmarking Functions
1528 **************************************/
1530 static void display_params_tested(paramValues_t cParams)
1533 DISPLAYLEVEL(3, "\r testing :");
1534 for (vi=0; vi < NUM_PARAMS; vi++) {
1535 DISPLAYLEVEL(3, "%3u,", (unsigned)cParams.vals[vi]);
1537 DISPLAYLEVEL(3, "\b \r");
1540 /* Replicate functionality of benchMemAdvanced, but with pre-split src / dst buffers */
1541 /* The purpose is so that sufficient information is returned so that a decompression call to benchMemInvertible is possible */
1542 /* BMK_benchMemAdvanced(srcBuffer,srcSize, dstBuffer, dstSize, fileSizes, nbFiles, 0, &cParams, dictBuffer, dictSize, ctx, dctx, 0, "File", &adv); */
1543 /* nbSeconds used in same way as in BMK_advancedParams_t */
1544 /* if in decodeOnly, then srcPtr's will be compressed blocks, and uncompressedBlocks will be written to dstPtrs */
1545 /* dictionary nullable, nothing else though. */
1546 /* note : it would be a lot better if this function was present in benchzstd.c,
1547 * sharing code with benchMemAdvanced(), since it's technically a part of it */
1548 static BMK_benchOutcome_t
1549 BMK_benchMemInvertible( buffers_t buf, contexts_t ctx,
1550 int cLevel, const paramValues_t* comprParams,
1551 BMK_mode_t mode, unsigned nbSeconds)
1554 BMK_benchResult_t bResult;
1555 const void *const *const srcPtrs = (const void *const *const)buf.srcPtrs;
1556 size_t const *const srcSizes = buf.srcSizes;
1557 void** const dstPtrs = buf.dstPtrs;
1558 size_t const *const dstCapacities = buf.dstCapacities;
1559 size_t* const dstSizes = buf.dstSizes;
1560 void** const resPtrs = buf.resPtrs;
1561 size_t const *const resSizes = buf.resSizes;
1562 const void* dictBuffer = ctx.dictBuffer;
1563 const size_t dictBufferSize = ctx.dictSize;
1564 const size_t nbBlocks = buf.nbBlocks;
1565 const size_t srcSize = buf.srcSize;
1566 ZSTD_CCtx* cctx = ctx.cctx;
1567 ZSTD_DCtx* dctx = ctx.dctx;
1570 display_params_tested(*comprParams);
1571 memset(&bResult, 0, sizeof(bResult));
1573 /* warmimg up memory */
1574 for (i = 0; i < buf.nbBlocks; i++) {
1575 if (mode != BMK_decodeOnly) {
1576 RDG_genBuffer(dstPtrs[i], dstCapacities[i], 0.10, 0.50, 1);
1578 RDG_genBuffer(resPtrs[i], resSizes[i], 0.10, 0.50, 1);
1585 int compressionCompleted = (mode == BMK_decodeOnly);
1586 int decompressionCompleted = (mode == BMK_compressOnly);
1587 BMK_timedFnState_t* timeStateCompress = BMK_createTimedFnState(nbSeconds * 1000, 1000);
1588 BMK_timedFnState_t* timeStateDecompress = BMK_createTimedFnState(nbSeconds * 1000, 1000);
1589 BMK_benchParams_t cbp, dbp;
1590 BMK_initCCtxArgs cctxprep;
1591 BMK_initDCtxArgs dctxprep;
1593 cbp.benchFn = local_defaultCompress;
1594 cbp.benchPayload = cctx;
1595 cbp.initFn = local_initCCtx;
1596 cbp.initPayload = &cctxprep;
1597 cbp.errorFn = ZSTD_isError;
1598 cbp.blockCount = nbBlocks;
1599 cbp.srcBuffers = srcPtrs;
1600 cbp.srcSizes = srcSizes;
1601 cbp.dstBuffers = dstPtrs;
1602 cbp.dstCapacities = dstCapacities;
1603 cbp.blockResults = dstSizes;
1605 cctxprep.cctx = cctx;
1606 cctxprep.dictBuffer = dictBuffer;
1607 cctxprep.dictBufferSize = dictBufferSize;
1608 cctxprep.cLevel = cLevel;
1609 cctxprep.comprParams = comprParams;
1611 dbp.benchFn = local_defaultDecompress;
1612 dbp.benchPayload = dctx;
1613 dbp.initFn = local_initDCtx;
1614 dbp.initPayload = &dctxprep;
1615 dbp.errorFn = ZSTD_isError;
1616 dbp.blockCount = nbBlocks;
1617 dbp.srcBuffers = (const void* const *) dstPtrs;
1618 dbp.srcSizes = dstCapacities;
1619 dbp.dstBuffers = resPtrs;
1620 dbp.dstCapacities = resSizes;
1621 dbp.blockResults = NULL;
1623 dctxprep.dctx = dctx;
1624 dctxprep.dictBuffer = dictBuffer;
1625 dctxprep.dictBufferSize = dictBufferSize;
1627 assert(timeStateCompress != NULL);
1628 assert(timeStateDecompress != NULL);
1629 while(!compressionCompleted) {
1630 BMK_runOutcome_t const cOutcome = BMK_benchTimedFn(timeStateCompress, cbp);
1632 if (!BMK_isSuccessful_runOutcome(cOutcome)) {
1633 BMK_benchOutcome_t bOut;
1634 memset(&bOut, 0, sizeof(bOut));
1635 bOut.tag = 1; /* should rather be a function or a constant */
1636 BMK_freeTimedFnState(timeStateCompress);
1637 BMK_freeTimedFnState(timeStateDecompress);
1640 { BMK_runTime_t const rResult = BMK_extract_runTime(cOutcome);
1641 bResult.cSpeed = (srcSize * TIMELOOP_NANOSEC) / rResult.nanoSecPerRun;
1642 bResult.cSize = rResult.sumOfReturn;
1644 compressionCompleted = BMK_isCompleted_TimedFn(timeStateCompress);
1647 while (!decompressionCompleted) {
1648 BMK_runOutcome_t const dOutcome = BMK_benchTimedFn(timeStateDecompress, dbp);
1650 if (!BMK_isSuccessful_runOutcome(dOutcome)) {
1651 BMK_benchOutcome_t bOut;
1652 memset(&bOut, 0, sizeof(bOut));
1653 bOut.tag = 1; /* should rather be a function or a constant */
1654 BMK_freeTimedFnState(timeStateCompress);
1655 BMK_freeTimedFnState(timeStateDecompress);
1658 { BMK_runTime_t const rResult = BMK_extract_runTime(dOutcome);
1659 bResult.dSpeed = (srcSize * TIMELOOP_NANOSEC) / rResult.nanoSecPerRun;
1661 decompressionCompleted = BMK_isCompleted_TimedFn(timeStateDecompress);
1664 BMK_freeTimedFnState(timeStateCompress);
1665 BMK_freeTimedFnState(timeStateDecompress);
1669 bResult.cMem = (1 << (comprParams->vals[wlog_ind])) + ZSTD_sizeof_CCtx(cctx);
1671 { BMK_benchOutcome_t bOut;
1673 bOut.internal_never_use_directly = bResult; /* should be a function */
1678 /* BMK_benchParam() :
1679 * benchmark a set of `cParams` over sample `buf`,
1680 * store the result in `resultPtr`.
1681 * @return : 0 if success, 1 if error */
1682 static int BMK_benchParam ( BMK_benchResult_t* resultPtr,
1683 buffers_t buf, contexts_t ctx,
1684 paramValues_t cParams)
1686 BMK_benchOutcome_t const outcome = BMK_benchMemInvertible(buf, ctx,
1687 BASE_CLEVEL, &cParams,
1689 if (!BMK_isSuccessful_benchOutcome(outcome)) return 1;
1690 *resultPtr = BMK_extract_benchResult(outcome);
1695 /* Benchmarking which stops when we are sufficiently sure the solution is infeasible / worse than the winner */
1696 #define VARIANCE 1.2
1697 static int allBench(BMK_benchResult_t* resultPtr,
1698 const buffers_t buf, const contexts_t ctx,
1699 const paramValues_t cParams,
1700 const constraint_t target,
1701 BMK_benchResult_t* winnerResult, int feas)
1703 BMK_benchResult_t benchres;
1704 double uncertaintyConstantC = 3., uncertaintyConstantD = 3.;
1707 BMK_benchOutcome_t const outcome = BMK_benchMemInvertible(buf, ctx, BASE_CLEVEL, &cParams, BMK_both, 2);
1708 if (!BMK_isSuccessful_benchOutcome(outcome)) {
1709 DEBUGOUTPUT("Benchmarking failed \n");
1710 return ERROR_RESULT;
1712 benchres = BMK_extract_benchResult(outcome);
1714 winnerRS = resultScore(*winnerResult, buf.srcSize, target);
1715 DEBUGOUTPUT("WinnerScore: %f \n ", winnerRS);
1717 *resultPtr = benchres;
1719 /* anything with worse ratio in feas is definitely worse, discard */
1720 if(feas && benchres.cSize < winnerResult->cSize && !g_optmode) {
1721 return WORSE_RESULT;
1724 /* calculate uncertainty in compression / decompression runs */
1725 if (benchres.cSpeed) {
1726 U64 const loopDurationC = (((U64)buf.srcSize * TIMELOOP_NANOSEC) / benchres.cSpeed);
1727 uncertaintyConstantC = ((loopDurationC + (double)(2 * g_clockGranularity))/loopDurationC);
1730 if (benchres.dSpeed) {
1731 U64 const loopDurationD = (((U64)buf.srcSize * TIMELOOP_NANOSEC) / benchres.dSpeed);
1732 uncertaintyConstantD = ((loopDurationD + (double)(2 * g_clockGranularity))/loopDurationD);
1735 /* optimistic assumption of benchres */
1736 { BMK_benchResult_t resultMax = benchres;
1737 resultMax.cSpeed *= uncertaintyConstantC * VARIANCE;
1738 resultMax.dSpeed *= uncertaintyConstantD * VARIANCE;
1740 /* disregard infeasible results in feas mode */
1741 /* disregard if resultMax < winner in infeas mode */
1742 if((feas && !feasible(resultMax, target)) ||
1743 (!feas && (winnerRS > resultScore(resultMax, buf.srcSize, target)))) {
1744 return WORSE_RESULT;
1748 /* compare by resultScore when in infeas */
1749 /* compare by compareResultLT when in feas */
1750 if((!feas && (resultScore(benchres, buf.srcSize, target) > resultScore(*winnerResult, buf.srcSize, target))) ||
1751 (feas && (compareResultLT(*winnerResult, benchres, target, buf.srcSize))) ) {
1752 return BETTER_RESULT;
1754 return WORSE_RESULT;
1759 #define INFEASIBLE_THRESHOLD 200
1760 /* Memoized benchmarking, won't benchmark anything which has already been benchmarked before. */
1761 static int benchMemo(BMK_benchResult_t* resultPtr,
1762 const buffers_t buf, const contexts_t ctx,
1763 const paramValues_t cParams,
1764 const constraint_t target,
1765 BMK_benchResult_t* winnerResult, memoTable_t* const memoTableArray,
1767 static int bmcount = 0;
1770 if ( memoTableGet(memoTableArray, cParams) >= INFEASIBLE_THRESHOLD
1771 || redundantParams(cParams, target, buf.maxBlockSize) ) {
1772 return WORSE_RESULT;
1775 res = allBench(resultPtr, buf, ctx, cParams, target, winnerResult, feas);
1777 if(DEBUG && !(bmcount % 250)) {
1778 DISPLAY("Count: %d\n", bmcount);
1781 BMK_printWinnerOpt(stdout, CUSTOM_LEVEL, *resultPtr, cParams, target, buf.srcSize);
1783 if(res == BETTER_RESULT || feas) {
1784 memoTableSet(memoTableArray, cParams, 255); /* what happens if collisions are frequent */
1794 ZSTD_strategy strategy_max;
1795 } level_constraints_t;
1797 static level_constraints_t g_level_constraint[NB_LEVELS_TRACKED+1];
1799 static void BMK_init_level_constraints(int bytePerSec_level1)
1801 assert(NB_LEVELS_TRACKED >= ZSTD_maxCLevel());
1802 memset(g_level_constraint, 0, sizeof(g_level_constraint));
1803 g_level_constraint[1].cSpeed_min = bytePerSec_level1;
1804 g_level_constraint[1].dSpeed_min = 0.;
1805 g_level_constraint[1].windowLog_max = 19;
1806 g_level_constraint[1].strategy_max = ZSTD_fast;
1808 /* establish speed objectives (relative to level 1) */
1810 for (l=2; l<=NB_LEVELS_TRACKED; l++) {
1811 g_level_constraint[l].cSpeed_min = (g_level_constraint[l-1].cSpeed_min * 49) / 64;
1812 g_level_constraint[l].dSpeed_min = 0.;
1813 g_level_constraint[l].windowLog_max = (l<20) ? 23 : l+5; /* only --ultra levels >= 20 can use windowlog > 23 */
1814 g_level_constraint[l].strategy_max = ZSTD_STRATEGY_MAX;
1818 static int BMK_seed(winnerInfo_t* winners,
1819 const paramValues_t params,
1820 const buffers_t buf,
1821 const contexts_t ctx)
1823 BMK_benchResult_t testResult;
1827 BMK_benchParam(&testResult, buf, ctx, params);
1829 for (cLevel = 1; cLevel <= NB_LEVELS_TRACKED; cLevel++) {
1831 if (testResult.cSpeed < g_level_constraint[cLevel].cSpeed_min)
1832 continue; /* not fast enough for this level */
1833 if (testResult.dSpeed < g_level_constraint[cLevel].dSpeed_min)
1834 continue; /* not fast enough for this level */
1835 if (params.vals[wlog_ind] > g_level_constraint[cLevel].windowLog_max)
1836 continue; /* too much memory for this level */
1837 if (params.vals[strt_ind] > g_level_constraint[cLevel].strategy_max)
1838 continue; /* forbidden strategy for this level */
1839 if (winners[cLevel].result.cSize==0) {
1840 /* first solution for this cLevel */
1841 winners[cLevel].result = testResult;
1842 winners[cLevel].params = params;
1843 BMK_print_cLevelEntry(stdout, cLevel, params, testResult, buf.srcSize);
1848 if ((double)testResult.cSize <= ((double)winners[cLevel].result.cSize * (1. + (0.02 / cLevel))) ) {
1849 /* Validate solution is "good enough" */
1850 double W_ratio = (double)buf.srcSize / testResult.cSize;
1851 double O_ratio = (double)buf.srcSize / winners[cLevel].result.cSize;
1852 double W_ratioNote = log (W_ratio);
1853 double O_ratioNote = log (O_ratio);
1854 size_t W_DMemUsed = (1 << params.vals[wlog_ind]) + (16 KB);
1855 size_t O_DMemUsed = (1 << winners[cLevel].params.vals[wlog_ind]) + (16 KB);
1856 double W_DMemUsed_note = W_ratioNote * ( 40 + 9*cLevel) - log((double)W_DMemUsed);
1857 double O_DMemUsed_note = O_ratioNote * ( 40 + 9*cLevel) - log((double)O_DMemUsed);
1859 size_t W_CMemUsed = (1 << params.vals[wlog_ind]) + ZSTD_estimateCCtxSize_usingCParams(pvalsToCParams(params));
1860 size_t O_CMemUsed = (1 << winners[cLevel].params.vals[wlog_ind]) + ZSTD_estimateCCtxSize_usingCParams(pvalsToCParams(winners[cLevel].params));
1861 double W_CMemUsed_note = W_ratioNote * ( 50 + 13*cLevel) - log((double)W_CMemUsed);
1862 double O_CMemUsed_note = O_ratioNote * ( 50 + 13*cLevel) - log((double)O_CMemUsed);
1864 double W_CSpeed_note = W_ratioNote * ( 30 + 10*cLevel) + log(testResult.cSpeed);
1865 double O_CSpeed_note = O_ratioNote * ( 30 + 10*cLevel) + log(winners[cLevel].result.cSpeed);
1867 double W_DSpeed_note = W_ratioNote * ( 20 + 2*cLevel) + log(testResult.dSpeed);
1868 double O_DSpeed_note = O_ratioNote * ( 20 + 2*cLevel) + log(winners[cLevel].result.dSpeed);
1870 if (W_DMemUsed_note < O_DMemUsed_note) {
1871 /* uses too much Decompression memory for too little benefit */
1872 if (W_ratio > O_ratio)
1873 DISPLAYLEVEL(3, "Decompression Memory : %5.3f @ %4.1f MB vs %5.3f @ %4.1f MB : not enough for level %i\n",
1874 W_ratio, (double)(W_DMemUsed) / 1024 / 1024,
1875 O_ratio, (double)(O_DMemUsed) / 1024 / 1024, cLevel);
1878 if (W_CMemUsed_note < O_CMemUsed_note) {
1879 /* uses too much memory for compression for too little benefit */
1880 if (W_ratio > O_ratio)
1881 DISPLAYLEVEL(3, "Compression Memory : %5.3f @ %4.1f MB vs %5.3f @ %4.1f MB : not enough for level %i\n",
1882 W_ratio, (double)(W_CMemUsed) / 1024 / 1024,
1883 O_ratio, (double)(O_CMemUsed) / 1024 / 1024,
1887 if (W_CSpeed_note < O_CSpeed_note ) {
1888 /* too large compression speed difference for the compression benefit */
1889 if (W_ratio > O_ratio)
1890 DISPLAYLEVEL(3, "Compression Speed : %5.3f @ %4.1f MB/s vs %5.3f @ %4.1f MB/s : not enough for level %i\n",
1891 W_ratio, (double)testResult.cSpeed / MB_UNIT,
1892 O_ratio, (double)winners[cLevel].result.cSpeed / MB_UNIT,
1896 if (W_DSpeed_note < O_DSpeed_note ) {
1897 /* too large decompression speed difference for the compression benefit */
1898 if (W_ratio > O_ratio)
1899 DISPLAYLEVEL(3, "Decompression Speed : %5.3f @ %4.1f MB/s vs %5.3f @ %4.1f MB/s : not enough for level %i\n",
1900 W_ratio, (double)testResult.dSpeed / MB_UNIT,
1901 O_ratio, (double)winners[cLevel].result.dSpeed / MB_UNIT,
1906 if (W_ratio < O_ratio)
1907 DISPLAYLEVEL(3, "Solution %4.3f selected over %4.3f at level %i, due to better secondary statistics \n",
1908 W_ratio, O_ratio, cLevel);
1910 winners[cLevel].result = testResult;
1911 winners[cLevel].params = params;
1912 BMK_print_cLevelEntry(stdout, cLevel, params, testResult, buf.srcSize);
1920 /*-************************************
1921 * Compression Level Table Generation Functions
1922 **************************************/
1924 #define PARAMTABLELOG 25
1925 #define PARAMTABLESIZE (1<<PARAMTABLELOG)
1926 #define PARAMTABLEMASK (PARAMTABLESIZE-1)
1927 static BYTE g_alreadyTested[PARAMTABLESIZE] = {0}; /* init to zero */
1929 static BYTE* NB_TESTS_PLAYED(paramValues_t p)
1931 ZSTD_compressionParameters const cParams = pvalsToCParams(sanitizeParams(p));
1932 unsigned long long const h64 = XXH64(&cParams, sizeof(cParams), 0);
1933 return &g_alreadyTested[(h64 >> 3) & PARAMTABLEMASK];
1936 static void playAround(FILE* f,
1937 winnerInfo_t* winners,
1939 const buffers_t buf, const contexts_t ctx)
1941 int nbVariations = 0;
1942 UTIL_time_t const clockStart = UTIL_getTime();
1944 while (UTIL_clockSpanMicro(clockStart) < g_maxVariationTime) {
1945 if (nbVariations++ > g_maxNbVariations) break;
1949 for(i = 0; i < 4; i++) {
1950 paramVaryOnce(FUZ_rand(&g_rand) % (strt_ind + 1),
1951 ((FUZ_rand(&g_rand) & 1) << 1) - 1,
1954 } while (!paramValid(p));
1956 /* exclude faster if already played params */
1957 if (FUZ_rand(&g_rand) & ((1 << *NB_TESTS_PLAYED(p))-1))
1961 { BYTE* const b = NB_TESTS_PLAYED(p);
1964 if (!BMK_seed(winners, p, buf, ctx)) continue;
1966 /* improvement found => search more */
1967 BMK_saveAndPrint_cLevelTable(f, winners, buf.srcSize);
1968 playAround(f, winners, p, buf, ctx);
1974 BMK_selectRandomStart( FILE* f,
1975 winnerInfo_t* winners,
1976 const buffers_t buf, const contexts_t ctx)
1978 U32 const id = FUZ_rand(&g_rand) % (NB_LEVELS_TRACKED+1);
1979 if ((id==0) || (winners[id].params.vals[wlog_ind]==0)) {
1980 /* use some random entry */
1981 paramValues_t const p = adjustParams(cParamsToPVals(pvalsToCParams(randomParams())), /* defaults nonCompression parameters */
1983 playAround(f, winners, p, buf, ctx);
1985 playAround(f, winners, winners[id].params, buf, ctx);
1990 /* BMK_generate_cLevelTable() :
1991 * test a large number of configurations
1992 * and distribute them accross compression levels according to speed conditions.
1993 * display and save all intermediate results into rfName = "grillResults.txt".
1994 * the function automatically stops after g_timeLimit_s.
1995 * this function cannot error, it directly exit() in case of problem.
1997 static void BMK_generate_cLevelTable(const buffers_t buf, const contexts_t ctx)
1999 paramValues_t params;
2000 winnerInfo_t winners[NB_LEVELS_TRACKED+1];
2001 const char* const rfName = "grillResults.txt";
2002 FILE* const f = fopen(rfName, "w");
2005 assert(g_singleRun==0);
2006 memset(winners, 0, sizeof(winners));
2007 if (f==NULL) { DISPLAY("error opening %s \n", rfName); exit(1); }
2010 BMK_init_level_constraints(g_target * MB_UNIT);
2012 /* baseline config for level 1 */
2013 paramValues_t const l1params = cParamsToPVals(ZSTD_getCParams(1, buf.maxBlockSize, ctx.dictSize));
2014 BMK_benchResult_t testResult;
2015 BMK_benchParam(&testResult, buf, ctx, l1params);
2016 BMK_init_level_constraints((int)((testResult.cSpeed * 31) / 32));
2019 /* populate initial solution */
2020 { const int maxSeeds = g_noSeed ? 1 : ZSTD_maxCLevel();
2022 for (i=0; i<=maxSeeds; i++) {
2023 params = cParamsToPVals(ZSTD_getCParams(i, buf.maxBlockSize, 0));
2024 BMK_seed(winners, params, buf, ctx);
2026 BMK_saveAndPrint_cLevelTable(f, winners, buf.srcSize);
2029 { const UTIL_time_t grillStart = UTIL_getTime();
2031 BMK_selectRandomStart(f, winners, buf, ctx);
2032 } while (BMK_timeSpan_s(grillStart) < g_timeLimit_s);
2036 BMK_saveAndPrint_cLevelTable(f, winners, buf.srcSize);
2037 DISPLAY("grillParams operations completed \n");
2044 /*-************************************
2045 * Single Benchmark Functions
2046 **************************************/
2049 benchOnce(const buffers_t buf, const contexts_t ctx, const int cLevel)
2051 BMK_benchResult_t testResult;
2052 g_params = adjustParams(overwriteParams(cParamsToPVals(ZSTD_getCParams(cLevel, buf.maxBlockSize, ctx.dictSize)), g_params), buf.maxBlockSize, ctx.dictSize);
2054 if (BMK_benchParam(&testResult, buf, ctx, g_params)) {
2055 DISPLAY("Error during benchmarking\n");
2059 BMK_printWinner(stdout, CUSTOM_LEVEL, testResult, g_params, buf.srcSize);
2064 static int benchSample(double compressibility, int cLevel)
2066 const char* const name = "Sample 10MB";
2067 size_t const benchedSize = 10 MB;
2068 void* const srcBuffer = malloc(benchedSize);
2074 if(srcBuffer == NULL) {
2075 DISPLAY("Out of Memory\n");
2079 RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
2081 if(createBuffersFromMemory(&buf, srcBuffer, 1, &benchedSize)) {
2082 DISPLAY("Buffer Creation Error\n");
2087 if(createContexts(&ctx, NULL)) {
2088 DISPLAY("Context Creation Error\n");
2094 DISPLAY("\r%79s\r", "");
2095 DISPLAY("using %s %i%%: \n", name, (int)(compressibility*100));
2098 ret = benchOnce(buf, ctx, cLevel);
2100 BMK_generate_cLevelTable(buf, ctx);
2110 * note: while this function takes a table of filenames,
2111 * in practice, only the first filename will be used */
2112 static int benchFiles(const char** fileNamesTable, int nbFiles,
2113 const char* dictFileName, int cLevel)
2119 if (createBuffers(&buf, fileNamesTable, nbFiles)) {
2120 DISPLAY("unable to load files\n");
2124 if (createContexts(&ctx, dictFileName)) {
2125 DISPLAY("unable to load dictionary\n");
2130 DISPLAY("\r%79s\r", "");
2132 DISPLAY("using %s : \n", fileNamesTable[0]);
2134 DISPLAY("using %d Files : \n", nbFiles);
2138 ret = benchOnce(buf, ctx, cLevel);
2140 BMK_generate_cLevelTable(buf, ctx);
2149 /*-************************************
2150 * Local Optimization Functions
2151 **************************************/
2153 /* One iteration of hill climbing. Specifically, it first tries all
2154 * valid parameter configurations w/ manhattan distance 1 and picks the best one
2155 * failing that, it progressively tries candidates further and further away (up to #dim + 2)
2156 * if it finds a candidate exceeding winnerInfo, it will repeat. Otherwise, it will stop the
2157 * current stage of hill climbing.
2158 * Each iteration of hill climbing proceeds in 2 'phases'. Phase 1 climbs according to
2159 * the resultScore function, which is effectively a linear increase in reward until it reaches
2160 * the constraint-satisfying value, it which point any excess results in only logarithmic reward.
2161 * This aims to find some constraint-satisfying point.
2162 * Phase 2 optimizes in accordance with what the original function sets out to maximize, with
2163 * all feasible solutions valued over all infeasible solutions.
2166 /* sanitize all params here.
2167 * all generation after random should be sanitized. (maybe sanitize random)
2169 static winnerInfo_t climbOnce(const constraint_t target,
2171 const buffers_t buf, const contexts_t ctx,
2172 const paramValues_t init)
2175 * cparam - currently considered 'center'
2176 * candidate - params to benchmark/results
2177 * winner - best option found so far.
2179 paramValues_t cparam = init;
2180 winnerInfo_t candidateInfo, winnerInfo;
2184 winnerInfo = initWinnerInfo(init);
2185 candidateInfo = winnerInfo;
2187 { winnerInfo_t bestFeasible1 = initWinnerInfo(cparam);
2188 DEBUGOUTPUT("Climb Part 1\n");
2192 const size_t varLen = mtAll[cparam.vals[strt_ind]].varLen;
2194 DEBUGOUTPUT("Start\n");
2195 cparam = winnerInfo.params;
2196 candidateInfo.params = cparam;
2197 /* all dist-1 candidates */
2198 for (i = 0; i < varLen; i++) {
2199 for (offset = -1; offset <= 1; offset += 2) {
2200 CHECKTIME(winnerInfo);
2201 candidateInfo.params = cparam;
2202 paramVaryOnce(mtAll[cparam.vals[strt_ind]].varArray[i], offset, &candidateInfo.params);
2204 if(paramValid(candidateInfo.params)) {
2206 res = benchMemo(&candidateInfo.result, buf, ctx,
2207 sanitizeParams(candidateInfo.params), target, &winnerInfo.result, mtAll, feas);
2208 DEBUGOUTPUT("Res: %d\n", res);
2209 if(res == BETTER_RESULT) { /* synonymous with better when called w/ infeasibleBM */
2210 winnerInfo = candidateInfo;
2212 if(compareResultLT(bestFeasible1.result, winnerInfo.result, target, buf.srcSize)) {
2213 bestFeasible1 = winnerInfo;
2217 } /* for (offset = -1; offset <= 1; offset += 2) */
2218 } /* for (i = 0; i < varLen; i++) */
2224 for (dist = 2; dist < varLen + 2; dist++) { /* varLen is # dimensions */
2225 for (i = 0; i < (1 << varLen) / varLen + 2; i++) {
2227 CHECKTIME(winnerInfo);
2228 candidateInfo.params = cparam;
2229 /* param error checking already done here */
2230 paramVariation(&candidateInfo.params, mtAll, (U32)dist);
2232 res = benchMemo(&candidateInfo.result,
2234 sanitizeParams(candidateInfo.params), target,
2235 &winnerInfo.result, mtAll, feas);
2236 DEBUGOUTPUT("Res: %d\n", res);
2237 if (res == BETTER_RESULT) { /* synonymous with better in this case*/
2238 winnerInfo = candidateInfo;
2240 if (compareResultLT(bestFeasible1.result, winnerInfo.result, target, buf.srcSize)) {
2241 bestFeasible1 = winnerInfo;
2250 } /* for(dist = 2; dist < varLen + 2; dist++) */
2252 if (!better) { /* infeas -> feas -> stop */
2253 if (feas) return winnerInfo;
2256 winnerInfo = bestFeasible1; /* note with change, bestFeasible may not necessarily be feasible, but if one has been benchmarked, it will be. */
2257 DEBUGOUTPUT("Climb Part 2\n");
2260 winnerInfo = bestFeasible1;
2266 /* Optimizes for a fixed strategy */
2268 /* flexible parameters: iterations of failed climbing (or if we do non-random, maybe this is when everything is close to visitied)
2269 weight more on visit for bad results, less on good results/more on later results / ones with more failures.
2270 allocate memoTable here.
2273 optimizeFixedStrategy(const buffers_t buf, const contexts_t ctx,
2274 const constraint_t target, paramValues_t paramTarget,
2275 const ZSTD_strategy strat,
2276 memoTable_t* memoTableArray, const int tries)
2281 winnerInfo_t winnerInfo, candidateInfo;
2282 winnerInfo = initWinnerInfo(emptyParams());
2283 /* so climb is given the right fixed strategy */
2284 paramTarget.vals[strt_ind] = strat;
2285 /* to pass ZSTD_checkCParams */
2286 paramTarget = cParamUnsetMin(paramTarget);
2290 for(i = 0; i < tries; i++) {
2291 DEBUGOUTPUT("Restart\n");
2293 randomConstrainedParams(&init, memoTableArray, strat);
2294 } while(redundantParams(init, target, buf.maxBlockSize));
2295 candidateInfo = climbOnce(target, memoTableArray, buf, ctx, init);
2296 if (compareResultLT(winnerInfo.result, candidateInfo.result, target, buf.srcSize)) {
2297 winnerInfo = candidateInfo;
2298 BMK_printWinnerOpt(stdout, CUSTOM_LEVEL, winnerInfo.result, winnerInfo.params, target, buf.srcSize);
2302 CHECKTIME(winnerInfo);
2308 /* goes best, best-1, best+1, best-2, ... */
2309 /* return 0 if nothing remaining */
2310 static int nextStrategy(const int currentStrategy, const int bestStrategy)
2312 if(bestStrategy <= currentStrategy) {
2313 int candidate = 2 * bestStrategy - currentStrategy - 1;
2315 candidate = currentStrategy + 1;
2316 if(candidate > (int)ZSTD_STRATEGY_MAX) {
2324 } else { /* bestStrategy >= currentStrategy */
2325 int candidate = 2 * bestStrategy - currentStrategy;
2326 if(candidate > (int)ZSTD_STRATEGY_MAX) {
2327 candidate = currentStrategy - 1;
2339 /* experiment with playing with this and decay value */
2341 /* main fn called when using --optimize */
2342 /* Does strategy selection by benchmarking default compression levels
2343 * then optimizes by strategy, starting with the best one and moving
2344 * progressively moving further away by number
2346 * fileNamesTable - list of files to benchmark
2347 * nbFiles - length of fileNamesTable
2348 * dictFileName - name of dictionary file if one, else NULL
2349 * target - performance constraints (cSpeed, dSpeed, cMem)
2350 * paramTarget - parameter constraints (i.e. restriction search space to where strategy = ZSTD_fast)
2351 * cLevel - compression level to exceed (all solutions must be > lvl in cSpeed + ratio)
2354 static int g_maxTries = 5;
2358 optimizeForSize(const char* const * const fileNamesTable, const size_t nbFiles,
2359 const char* dictFileName,
2360 constraint_t target, paramValues_t paramTarget,
2361 const int cLevelOpt, const int cLevelRun,
2362 const U32 memoTableLog)
2364 varInds_t varArray [NUM_PARAMS];
2366 const size_t varLen = variableParams(paramTarget, varArray, dictFileName != NULL);
2367 winnerInfo_t winner = initWinnerInfo(emptyParams());
2368 memoTable_t* allMT = NULL;
2369 paramValues_t paramBase;
2372 g_time = UTIL_getTime();
2374 if (createBuffers(&buf, fileNamesTable, nbFiles)) {
2375 DISPLAY("unable to load files\n");
2379 if (createContexts(&ctx, dictFileName)) {
2380 DISPLAY("unable to load dictionary\n");
2386 DISPLAYLEVEL(2, "Loading %s... \r", fileNamesTable[0]);
2388 DISPLAYLEVEL(2, "Loading %lu Files... \r", (unsigned long)nbFiles);
2391 /* sanitize paramTarget */
2392 optimizerAdjustInput(¶mTarget, buf.maxBlockSize);
2393 paramBase = cParamUnsetMin(paramTarget);
2395 allMT = createMemoTableArray(paramTarget, varArray, varLen, memoTableLog);
2398 DISPLAY("MemoTable Init Error\n");
2403 /* default strictnesses */
2404 if (g_strictness == PARAM_UNSET) {
2411 if(0 >= g_strictness || g_strictness > 100) {
2412 DISPLAY("Strictness Outside of Bounds\n");
2418 /* use level'ing mode instead of normal target mode */
2420 winner.params = cParamsToPVals(ZSTD_getCParams(cLevelOpt, buf.maxBlockSize, ctx.dictSize));
2421 if(BMK_benchParam(&winner.result, buf, ctx, winner.params)) {
2426 g_lvltarget = winner.result;
2427 g_lvltarget.cSpeed *= ((double)g_strictness) / 100;
2428 g_lvltarget.dSpeed *= ((double)g_strictness) / 100;
2429 g_lvltarget.cSize /= ((double)g_strictness) / 100;
2431 target.cSpeed = (U32)g_lvltarget.cSpeed;
2432 target.dSpeed = (U32)g_lvltarget.dSpeed;
2434 BMK_printWinnerOpt(stdout, cLevelOpt, winner.result, winner.params, target, buf.srcSize);
2437 /* Don't want it to return anything worse than the best known result */
2439 BMK_benchResult_t res;
2440 g_params = adjustParams(overwriteParams(cParamsToPVals(ZSTD_getCParams(cLevelRun, buf.maxBlockSize, ctx.dictSize)), g_params), buf.maxBlockSize, ctx.dictSize);
2441 if (BMK_benchParam(&res, buf, ctx, g_params)) {
2445 if(compareResultLT(winner.result, res, relaxTarget(target), buf.srcSize)) {
2446 winner.result = res;
2447 winner.params = g_params;
2452 DISPLAYLEVEL(2, "\r%79s\r", "");
2454 DISPLAYLEVEL(2, "optimizing for %s", fileNamesTable[0]);
2456 DISPLAYLEVEL(2, "optimizing for %lu Files", (unsigned long)nbFiles);
2459 if(target.cSpeed != 0) { DISPLAYLEVEL(2," - limit compression speed %u MB/s", (unsigned)(target.cSpeed >> 20)); }
2460 if(target.dSpeed != 0) { DISPLAYLEVEL(2, " - limit decompression speed %u MB/s", (unsigned)(target.dSpeed >> 20)); }
2461 if(target.cMem != (U32)-1) { DISPLAYLEVEL(2, " - limit memory %u MB", (unsigned)(target.cMem >> 20)); }
2463 DISPLAYLEVEL(2, "\n");
2464 init_clockGranularity();
2466 { paramValues_t CParams;
2468 /* find best solution from default params */
2469 { const int maxSeeds = g_noSeed ? 1 : ZSTD_maxCLevel();
2470 DEBUGOUTPUT("Strategy Selection\n");
2471 if (paramTarget.vals[strt_ind] == PARAM_UNSET) {
2472 BMK_benchResult_t candidate;
2474 for (i=1; i<=maxSeeds; i++) {
2476 CParams = overwriteParams(cParamsToPVals(ZSTD_getCParams(i, buf.maxBlockSize, ctx.dictSize)), paramTarget);
2477 ec = BMK_benchParam(&candidate, buf, ctx, CParams);
2478 BMK_printWinnerOpt(stdout, i, candidate, CParams, target, buf.srcSize);
2480 if(!ec && compareResultLT(winner.result, candidate, relaxTarget(target), buf.srcSize)) {
2481 winner.result = candidate;
2482 winner.params = CParams;
2485 CHECKTIMEGT(ret, 0, _displayCleanUp); /* if pass time limit, stop */
2486 /* if the current params are too slow, just stop. */
2487 if(target.cSpeed > candidate.cSpeed * 3 / 2) { break; }
2490 BMK_printWinnerOpt(stdout, CUSTOM_LEVEL, winner.result, winner.params, target, buf.srcSize);
2494 DEBUGOUTPUT("Real Opt\n");
2495 /* start 'real' optimization */
2496 { int bestStrategy = (int)winner.params.vals[strt_ind];
2497 if (paramTarget.vals[strt_ind] == PARAM_UNSET) {
2498 int st = bestStrategy;
2499 int tries = g_maxTries;
2501 /* one iterations of hill climbing with the level-defined parameters. */
2502 { winnerInfo_t const w1 = climbOnce(target, allMT, buf, ctx, winner.params);
2503 if (compareResultLT(winner.result, w1.result, target, buf.srcSize)) {
2506 CHECKTIMEGT(ret, 0, _displayCleanUp);
2509 while(st && tries > 0) {
2511 DEBUGOUTPUT("StrategySwitch: %s\n", g_stratName[st]);
2513 wc = optimizeFixedStrategy(buf, ctx, target, paramBase, st, allMT, tries);
2515 if(compareResultLT(winner.result, wc.result, target, buf.srcSize)) {
2520 st = nextStrategy(st, bestStrategy);
2523 CHECKTIMEGT(ret, 0, _displayCleanUp);
2526 winner = optimizeFixedStrategy(buf, ctx, target, paramBase, paramTarget.vals[strt_ind], allMT, g_maxTries);
2531 /* no solution found */
2532 if(winner.result.cSize == (size_t)-1) {
2534 DISPLAY("No feasible solution found\n");
2540 if (g_displayLevel >= 0) {
2541 BMK_displayOneResult(stdout, winner, buf.srcSize);
2543 BMK_paramValues_into_commandLine(stdout, winner.params);
2544 DISPLAYLEVEL(1, "grillParams size - optimizer completed \n");
2550 freeMemoTableArray(allMT);
2554 /*-************************************
2555 * CLI parsing functions
2556 **************************************/
2558 /** longCommandWArg() :
2559 * check if *stringPtr is the same as longCommand.
2560 * If yes, @return 1 and advances *stringPtr to the position which immediately follows longCommand.
2561 * @return 0 and doesn't modify *stringPtr otherwise.
2564 static unsigned longCommandWArg(const char** stringPtr, const char* longCommand)
2566 size_t const comSize = strlen(longCommand);
2567 int const result = !strncmp(*stringPtr, longCommand, comSize);
2568 if (result) *stringPtr += comSize;
2572 static void errorOut(const char* msg)
2574 DISPLAY("%s \n", msg); exit(1);
2577 /*! readU32FromChar() :
2578 * @return : unsigned integer value read from input in `char` format.
2579 * allows and interprets K, KB, KiB, M, MB and MiB suffix.
2580 * Will also modify `*stringPtr`, advancing it to position where it stopped reading.
2581 * Note : function will exit() program if digit sequence overflows */
2582 static unsigned readU32FromChar(const char** stringPtr)
2584 const char errorMsg[] = "error: numeric value too large";
2586 unsigned result = 0;
2587 if(**stringPtr == '-') { sign = (unsigned)-1; (*stringPtr)++; }
2588 while ((**stringPtr >='0') && (**stringPtr <='9')) {
2589 unsigned const max = (((unsigned)(-1)) / 10) - 1;
2590 if (result > max) errorOut(errorMsg);
2591 result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
2593 if ((**stringPtr=='K') || (**stringPtr=='M')) {
2594 unsigned const maxK = ((unsigned)(-1)) >> 10;
2595 if (result > maxK) errorOut(errorMsg);
2597 if (**stringPtr=='M') {
2598 if (result > maxK) errorOut(errorMsg);
2601 (*stringPtr)++; /* skip `K` or `M` */
2602 if (**stringPtr=='i') (*stringPtr)++;
2603 if (**stringPtr=='B') (*stringPtr)++;
2605 return result * sign;
2608 static double readDoubleFromChar(const char** stringPtr)
2610 double result = 0, divide = 10;
2611 while ((**stringPtr >='0') && (**stringPtr <='9')) {
2612 result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
2614 if(**stringPtr!='.') {
2618 while ((**stringPtr >='0') && (**stringPtr <='9')) {
2619 result += (double)(**stringPtr - '0') / divide, divide *= 10, (*stringPtr)++ ;
2624 static int usage(const char* exename)
2626 DISPLAY( "Usage :\n");
2627 DISPLAY( " %s [arg] file\n", exename);
2628 DISPLAY( "Arguments :\n");
2629 DISPLAY( " file : path to the file used as reference (if none, generates a compressible sample)\n");
2630 DISPLAY( " -H/-h : Help (this text + advanced options)\n");
2634 static int usage_advanced(void)
2636 DISPLAY( "\nAdvanced options :\n");
2637 DISPLAY( " -T# : set level 1 speed objective \n");
2638 DISPLAY( " -B# : cut input into blocks of size # (default : single block) \n");
2639 DISPLAY( " --optimize= : same as -O with more verbose syntax (see README.md)\n");
2640 DISPLAY( " -S : Single run \n");
2641 DISPLAY( " --zstd : Single run, parameter selection same as zstdcli \n");
2642 DISPLAY( " -P# : generated sample compressibility (default : %.1f%%) \n", COMPRESSIBILITY_DEFAULT * 100);
2643 DISPLAY( " -t# : Caps runtime of operation in seconds (default : %u seconds (%.1f hours)) \n",
2644 (unsigned)g_timeLimit_s, (double)g_timeLimit_s / 3600);
2645 DISPLAY( " -v : Prints Benchmarking output\n");
2646 DISPLAY( " -D : Next argument dictionary file\n");
2647 DISPLAY( " -s : Seperate Files\n");
2651 static int badusage(const char* exename)
2653 DISPLAY("Wrong parameters\n");
2658 #define PARSE_SUB_ARGS(stringLong, stringShort, variable) { \
2659 if ( longCommandWArg(&argument, stringLong) \
2660 || longCommandWArg(&argument, stringShort) ) { \
2661 variable = readU32FromChar(&argument); \
2662 if (argument[0]==',') { \
2663 argument++; continue; \
2667 /* 1 if successful parse, 0 otherwise */
2668 static int parse_params(const char** argptr, paramValues_t* pv) {
2670 const char* argOrig = *argptr;
2672 for(v = 0; v < NUM_PARAMS; v++) {
2673 if ( longCommandWArg(argptr,g_shortParamNames[v])
2674 || longCommandWArg(argptr, g_paramNames[v]) ) {
2675 if(**argptr == '=') {
2677 pv->vals[v] = readU32FromChar(argptr);
2682 /* reset and try again */
2688 /*-************************************
2690 **************************************/
2692 int main(int argc, const char** argv)
2697 const char* exename=argv[0];
2698 const char* input_filename = NULL;
2699 const char* dictFileName = NULL;
2701 int cLevelOpt = 0, cLevelRun = 0;
2702 int seperateFiles = 0;
2703 double compressibility = COMPRESSIBILITY_DEFAULT;
2704 U32 memoTableLog = PARAM_UNSET;
2705 constraint_t target = { 0, 0, (U32)-1 };
2707 paramValues_t paramTarget = emptyParams();
2708 g_params = emptyParams();
2710 assert(argc>=1); /* for exename */
2712 for(i=1; i<argc; i++) {
2713 const char* argument = argv[i];
2714 DEBUGOUTPUT("%d: %s\n", i, argument);
2715 assert(argument != NULL);
2717 if(!strcmp(argument,"--no-seed")) { g_noSeed = 1; continue; }
2719 if (longCommandWArg(&argument, "--optimize=")) {
2722 if(parse_params(&argument, ¶mTarget)) { if(argument[0] == ',') { argument++; continue; } else break; }
2723 PARSE_SUB_ARGS("compressionSpeed=" , "cSpeed=", target.cSpeed);
2724 PARSE_SUB_ARGS("decompressionSpeed=", "dSpeed=", target.dSpeed);
2725 PARSE_SUB_ARGS("compressionMemory=" , "cMem=", target.cMem);
2726 PARSE_SUB_ARGS("strict=", "stc=", g_strictness);
2727 PARSE_SUB_ARGS("maxTries=", "tries=", g_maxTries);
2728 PARSE_SUB_ARGS("memoLimitLog=", "memLog=", memoTableLog);
2729 if (longCommandWArg(&argument, "level=") || longCommandWArg(&argument, "lvl=")) { cLevelOpt = readU32FromChar(&argument); g_optmode = 1; if (argument[0]==',') { argument++; continue; } else break; }
2730 if (longCommandWArg(&argument, "speedForRatio=") || longCommandWArg(&argument, "speedRatio=")) { g_ratioMultiplier = readDoubleFromChar(&argument); if (argument[0]==',') { argument++; continue; } else break; }
2732 DISPLAY("invalid optimization parameter \n");
2736 if (argument[0] != 0) {
2737 DISPLAY("invalid --optimize= format\n");
2738 return 1; /* check the end of string */
2741 } else if (longCommandWArg(&argument, "--zstd=")) {
2742 /* Decode command (note : aggregated commands are allowed) */
2745 if(parse_params(&argument, &g_params)) { if(argument[0] == ',') { argument++; continue; } else break; }
2746 if (longCommandWArg(&argument, "level=") || longCommandWArg(&argument, "lvl=")) { cLevelRun = readU32FromChar(&argument); g_params = emptyParams(); if (argument[0]==',') { argument++; continue; } else break; }
2748 DISPLAY("invalid compression parameter \n");
2752 if (argument[0] != 0) {
2753 DISPLAY("invalid --zstd= format\n");
2754 return 1; /* check the end of string */
2757 /* if not return, success */
2759 } else if (longCommandWArg(&argument, "--display=")) {
2760 /* Decode command (note : aggregated commands are allowed) */
2761 memset(g_silenceParams, 1, sizeof(g_silenceParams));
2765 for(v = 0; v < NUM_PARAMS; v++) {
2766 if(longCommandWArg(&argument, g_shortParamNames[v]) || longCommandWArg(&argument, g_paramNames[v])) {
2767 g_silenceParams[v] = 0;
2771 if(longCommandWArg(&argument, "compressionParameters") || longCommandWArg(&argument, "cParams")) {
2772 for(v = 0; v <= strt_ind; v++) {
2773 g_silenceParams[v] = 0;
2780 if(argument[0]==',') {
2786 DISPLAY("invalid parameter name parameter \n");
2790 if (argument[0] != 0) {
2791 DISPLAY("invalid --display format\n");
2792 return 1; /* check the end of string */
2795 } else if (argument[0]=='-') {
2798 while (argument[0]!=0) {
2802 /* Display help on usage */
2804 case 'H': usage(exename); usage_advanced(); return 0;
2806 /* Pause at the end (hidden option) */
2807 case 'p': main_pause = 1; argument++; break;
2809 /* Sample compressibility (when no file provided) */
2812 { U32 const proba32 = readU32FromChar(&argument);
2813 compressibility = (double)proba32 / 100.;
2817 /* Run Single conf */
2826 g_params.vals[wlog_ind] = readU32FromChar(&argument);
2830 g_params.vals[clog_ind] = readU32FromChar(&argument);
2834 g_params.vals[hlog_ind] = readU32FromChar(&argument);
2838 g_params.vals[slog_ind] = readU32FromChar(&argument);
2840 case 'l': /* search length */
2842 g_params.vals[mml_ind] = readU32FromChar(&argument);
2844 case 't': /* target length */
2846 g_params.vals[tlen_ind] = readU32FromChar(&argument);
2848 case 'S': /* strategy */
2850 g_params.vals[strt_ind] = readU32FromChar(&argument);
2852 case 'f': /* forceAttachDict */
2854 g_params.vals[fadt_ind] = readU32FromChar(&argument);
2858 cLevelRun = readU32FromChar(&argument);
2859 g_params = emptyParams();
2869 /* target level1 speed objective, in MB/s */
2872 g_target = readU32FromChar(&argument);
2875 /* cut input into blocks */
2878 g_blockSize = readU32FromChar(&argument);
2879 DISPLAY("using %u KB block size \n", (unsigned)(g_blockSize>>10));
2882 /* caps runtime (in seconds) */
2885 g_timeLimit_s = readU32FromChar(&argument);
2894 while (argument[0] == 'q') { argument++; g_displayLevel--; }
2898 while (argument[0] == 'v') { argument++; g_displayLevel++; }
2901 /* load dictionary file (only applicable for optimizer rn) */
2903 if(i == argc - 1) { /* last argument, return error. */
2904 DISPLAY("Dictionary file expected but not given : %d\n", i);
2908 dictFileName = argv[i];
2909 argument += strlen(argument);
2913 /* Unknown command */
2914 default : return badusage(exename);
2918 } /* if (argument[0]=='-') */
2920 /* first provided filename is input */
2921 if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
2924 /* Welcome message */
2925 DISPLAYLEVEL(2, WELCOME_MESSAGE);
2927 if (filenamesStart==0) {
2929 DISPLAY("Optimizer Expects File\n");
2932 result = benchSample(compressibility, cLevelRun);
2936 for(i = 0; i < argc - filenamesStart; i++) {
2938 result = optimizeForSize(argv+filenamesStart + i, 1, dictFileName, target, paramTarget, cLevelOpt, cLevelRun, memoTableLog);
2939 if(result) { DISPLAY("Error on File %d", i); return result; }
2941 result = benchFiles(argv+filenamesStart + i, 1, dictFileName, cLevelRun);
2942 if(result) { DISPLAY("Error on File %d", i); return result; }
2947 result = optimizeForSize(argv+filenamesStart, argc-filenamesStart, dictFileName, target, paramTarget, cLevelOpt, cLevelRun, memoTableLog);
2949 result = benchFiles(argv+filenamesStart, argc-filenamesStart, dictFileName, cLevelRun);
2954 if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; }