]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/zstd/tests/paramgrill.c
Update to Zstandard 1.3.8
[FreeBSD/FreeBSD.git] / sys / contrib / zstd / tests / paramgrill.c
1 /*
2  * Copyright (c) 2015-present, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
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.
9  */
10
11
12 /*-************************************
13 *  Dependencies
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 */
20 #include <assert.h>
21
22 #include "mem.h"
23 #define ZSTD_STATIC_LINKING_ONLY   /* ZSTD_parameters, ZSTD_estimateCCtxSize */
24 #include "zstd.h"
25 #include "datagen.h"
26 #include "xxhash.h"
27 #include "benchfn.h"
28 #include "benchzstd.h"
29 #include "zstd_errors.h"
30 #include "zstd_internal.h"     /* should not be needed */
31
32
33 /*-************************************
34 *  Constants
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
39
40 #define TIMELOOP_NANOSEC      (1*1000000000ULL) /* 1 second */
41 #define NB_LEVELS_TRACKED 22   /* ensured being >= ZSTD_maxCLevel() in BMK_init_level_constraints() */
42
43 static const size_t maxMemory = (sizeof(size_t)==4)  ?  (2 GB - 64 MB) : (size_t)(1ULL << ((sizeof(size_t)*8)-31));
44
45 #define COMPRESSIBILITY_DEFAULT 0.50
46
47 static const U64 g_maxVariationTime = 60 * SEC_TO_MICRO;
48 static const int g_maxNbVariations = 64;
49
50
51 /*-************************************
52 *  Macros
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__); }
57
58 #define TIMED 0
59 #ifndef DEBUG
60 #  define DEBUG 0
61 #endif
62
63 #undef MIN
64 #undef MAX
65 #define MIN(a,b)   ( (a) < (b) ? (a) : (b) )
66 #define MAX(a,b)   ( (a) > (b) ? (a) : (b) )
67 #define CUSTOM_LEVEL 99
68 #define BASE_CLEVEL 1
69
70 #define FADT_MIN 0
71 #define FADT_MAX ((U32)-1)
72
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)
78 #define TLEN_RANGE  17
79 #define STRT_RANGE (ZSTD_STRATEGY_MAX - ZSTD_STRATEGY_MIN + 1)
80 #define FADT_RANGE   3
81
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; } }
84
85 #define PARAM_UNSET ((U32)-2) /* can't be -1 b/c fadt uses -1 */
86
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 ",
91                 "ZSTD_btultra2"};
92
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 };
94
95
96 /*-************************************
97 *  Setup for Adding new params
98 **************************************/
99
100 /* indices for each of the variables */
101 typedef enum {
102     wlog_ind = 0,
103     clog_ind = 1,
104     hlog_ind = 2,
105     slog_ind = 3,
106     mml_ind  = 4,
107     tlen_ind = 5,
108     strt_ind = 6,
109     fadt_ind = 7, /* forceAttachDict */
110     NUM_PARAMS = 8
111 } varInds_t;
112
113 typedef struct {
114     U32 vals[NUM_PARAMS];
115 } paramValues_t;
116
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 };
120
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 };
124
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 };
128
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 };
132
133 /* names of parameters */
134 static const char* g_paramNames[NUM_PARAMS] =
135         { "windowLog", "chainLog", "hashLog","searchLog", "minMatch", "targetLength", "strategy", "forceAttachDict" };
136
137 /* shortened names of parameters */
138 static const char* g_shortParamNames[NUM_PARAMS] =
139         { "wlog", "clog", "hlog", "slog", "mml", "tlen", "strat", "fadt" };
140
141 /* maps value from { 0 to rangetable[param] - 1 } to valid paramvalues */
142 static U32 rangeMap(varInds_t param, int ind)
143 {
144     ind = MAX(MIN(ind, (int)rangetable[param] - 1), 0);
145     switch(param) {
146         case wlog_ind: /* using default: triggers -Wswitch-enum */
147         case clog_ind:
148         case hlog_ind:
149         case slog_ind:
150         case mml_ind:
151         case strt_ind:
152             return mintable[param] + ind;
153         case tlen_ind:
154             return tlen_table[ind];
155         case fadt_ind: /* 0, 1, 2 -> -1, 0, 1 */
156             return ind - 1;
157         case NUM_PARAMS:
158         default:;
159     }
160     DISPLAY("Error, not a valid param\n ");
161     assert(0);
162     return (U32)-1;
163 }
164
165 /* inverse of rangeMap */
166 static int invRangeMap(varInds_t param, U32 value)
167 {
168     value = MIN(MAX(mintable[param], value), maxtable[param]);
169     switch(param) {
170         case wlog_ind:
171         case clog_ind:
172         case hlog_ind:
173         case slog_ind:
174         case mml_ind:
175         case strt_ind:
176             return value - mintable[param];
177         case tlen_ind: /* bin search */
178         {
179             int lo = 0;
180             int hi = TLEN_RANGE;
181             while(lo < hi) {
182                 int mid = (lo + hi) / 2;
183                 if(tlen_table[mid] < value) {
184                     lo = mid + 1;
185                 } if(tlen_table[mid] == value) {
186                     return mid;
187                 } else {
188                     hi = mid;
189                 }
190             }
191             return lo;
192         }
193         case fadt_ind:
194             return (int)value + 1;
195         case NUM_PARAMS:
196         default:;
197     }
198     DISPLAY("Error, not a valid param\n ");
199     assert(0);
200     return -2;
201 }
202
203 /* display of params */
204 static void displayParamVal(FILE* f, varInds_t param, unsigned value, int width)
205 {
206     switch(param) {
207         case wlog_ind:
208         case clog_ind:
209         case hlog_ind:
210         case slog_ind:
211         case mml_ind:
212         case tlen_ind:
213             if(width) {
214                 fprintf(f, "%*u", width, value);
215             } else {
216                 fprintf(f, "%u", value);
217             }
218             break;
219         case strt_ind:
220             if(width) {
221                 fprintf(f, "%*s", width, g_stratName[value]);
222             } else {
223                 fprintf(f, "%s", g_stratName[value]);
224             }
225             break;
226         case fadt_ind:   /* force attach dict */
227             if(width) {
228                 fprintf(f, "%*d", width, (int)value);
229             } else {
230                 fprintf(f, "%d", (int)value);
231             }
232             break;
233         case NUM_PARAMS:
234         default:
235             DISPLAY("Error, not a valid param\n ");
236             assert(0);
237             break;
238     }
239 }
240
241
242 /*-************************************
243 *  Benchmark Parameters/Global Variables
244 **************************************/
245
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;
251
252 /* Display */
253 static int g_displayLevel = 3;
254 static BYTE g_silenceParams[NUM_PARAMS];   /* can selectively silence some params when displaying them */
255
256 /* Mode Selection */
257 static U32 g_singleRun = 0;
258 static U32 g_optimizer = 0;
259 static int g_optmode = 0;
260
261 /* For cLevel Table generation */
262 static U32 g_target = 0;
263 static U32 g_noSeed = 0;
264
265 /* For optimizer */
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;
270
271 typedef enum {
272     directMap,
273     xxhashMap,
274     noMemo
275 } memoTableType_t;
276
277 typedef struct {
278     memoTableType_t tableType;
279     BYTE* table;
280     size_t tableLen;
281     varInds_t varArray[NUM_PARAMS];
282     size_t varLen;
283 } memoTable_t;
284
285 typedef struct {
286     BMK_benchResult_t result;
287     paramValues_t params;
288 } winnerInfo_t;
289
290 typedef struct {
291     U32 cSpeed;  /* bytes / sec */
292     U32 dSpeed;
293     U32 cMem;    /* bytes */
294 } constraint_t;
295
296 typedef struct winner_ll_node winner_ll_node;
297 struct winner_ll_node {
298     winnerInfo_t res;
299     winner_ll_node* next;
300 };
301
302 static winner_ll_node* g_winners; /* linked list sorted ascending by cSize & cSpeed */
303
304 /*
305  * Additional Global Variables (Defined Above Use)
306  * g_level_constraint
307  * g_alreadyTested
308  * g_maxTries
309  * g_clockGranularity
310  */
311
312
313 /*-*******************************************************
314 *  General Util Functions
315 *********************************************************/
316
317 /* nullified useless params, to ensure count stats */
318 /* cleans up params for memoizing / display */
319 static paramValues_t sanitizeParams(paramValues_t params)
320 {
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;
327
328     return params;
329 }
330
331 static ZSTD_compressionParameters pvalsToCParams(paramValues_t p)
332 {
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 */
343     return c;
344 }
345
346 static paramValues_t cParamsToPVals(ZSTD_compressionParameters c)
347 {
348     paramValues_t p;
349     varInds_t i;
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;
357
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];
361     }
362     return p;
363 }
364
365 /* equivalent of ZSTD_adjustCParams for paramValues_t */
366 static paramValues_t
367 adjustParams(paramValues_t p, const size_t maxBlockSize, const size_t dictSize)
368 {
369     paramValues_t ot = p;
370     varInds_t i;
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];
376     }
377     return p;
378 }
379
380 static size_t BMK_findMaxMem(U64 requiredMem)
381 {
382     size_t const step = 64 MB;
383     void* testmem = NULL;
384
385     requiredMem = (((requiredMem >> 26) + 1) << 26);
386     if (requiredMem > maxMemory) requiredMem = maxMemory;
387
388     requiredMem += 2 * step;
389     while (!testmem && requiredMem > 0) {
390         testmem = malloc ((size_t)requiredMem);
391         requiredMem -= step;
392     }
393
394     free (testmem);
395     return (size_t) requiredMem;
396 }
397
398 /* accuracy in seconds only, span can be multiple years */
399 static U32 BMK_timeSpan_s(const UTIL_time_t tStart)
400 {
401     return (U32)(UTIL_clockSpanMicro(tStart) / 1000000ULL);
402 }
403
404 static U32 FUZ_rotl32(U32 x, U32 r)
405 {
406     return ((x << r) | (x >> (32 - r)));
407 }
408
409 static U32 FUZ_rand(U32* src)
410 {
411     const U32 prime1 = 2654435761U;
412     const U32 prime2 = 2246822519U;
413     U32 rand32 = *src;
414     rand32 *= prime1;
415     rand32 += prime2;
416     rand32  = FUZ_rotl32(rand32, 13);
417     *src = rand32;
418     return rand32 >> 5;
419 }
420
421 #define BOUNDCHECK(val,min,max) {                     \
422     if (((val)<(min)) | ((val)>(max))) {              \
423         DISPLAY("INVALID PARAMETER CONSTRAINTS\n");   \
424         return 0;                                     \
425 }   }
426
427 static int paramValid(const paramValues_t paramTarget)
428 {
429     U32 i;
430     for(i = 0; i < NUM_PARAMS; i++) {
431         BOUNDCHECK(paramTarget.vals[i], mintable[i], maxtable[i]);
432     }
433     return 1;
434 }
435
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)
441 {
442     varInds_t vi;
443     for (vi = 0; vi < NUM_PARAMS; vi++) {
444         if (paramTarget.vals[vi] == PARAM_UNSET) {
445             paramTarget.vals[vi] = mintable[vi];
446         }
447     }
448     return paramTarget;
449 }
450
451 static paramValues_t emptyParams(void)
452 {
453     U32 i;
454     paramValues_t p;
455     for(i = 0; i < NUM_PARAMS; i++) {
456         p.vals[i] = PARAM_UNSET;
457     }
458     return p;
459 }
460
461 static winnerInfo_t initWinnerInfo(const paramValues_t p)
462 {
463     winnerInfo_t w1;
464     w1.result.cSpeed = 0.;
465     w1.result.dSpeed = 0.;
466     w1.result.cMem = (size_t)-1;
467     w1.result.cSize = (size_t)-1;
468     w1.params = p;
469     return w1;
470 }
471
472 static paramValues_t
473 overwriteParams(paramValues_t base, const paramValues_t mask)
474 {
475     U32 i;
476     for(i = 0; i < NUM_PARAMS; i++) {
477         if(mask.vals[i] != PARAM_UNSET) {
478             base.vals[i] = mask.vals[i];
479         }
480     }
481     return base;
482 }
483
484 static void
485 paramVaryOnce(const varInds_t paramIndex, const int amt, paramValues_t* ptr)
486 {
487     ptr->vals[paramIndex] = rangeMap(paramIndex,
488                                      invRangeMap(paramIndex, ptr->vals[paramIndex]) + amt);
489 }
490
491 /* varies ptr by nbChanges respecting varyParams*/
492 static void
493 paramVariation(paramValues_t* ptr, memoTable_t* mtAll, const U32 nbChanges)
494 {
495     paramValues_t p;
496     U32 validated = 0;
497     while (!validated) {
498         U32 i;
499         p = *ptr;
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);
503         }
504         validated = paramValid(p);
505     }
506     *ptr = p;
507 }
508
509 /* Completely random parameter selection */
510 static paramValues_t randomParams(void)
511 {
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]);
515     }
516     return p;
517 }
518
519 static U64 g_clockGranularity = 100000000ULL;
520
521 static void init_clockGranularity(void)
522 {
523     UTIL_time_t const clockStart = UTIL_getTime();
524     U64 el1 = 0, el2 = 0;
525     int i = 0;
526     do {
527         el1 = el2;
528         el2 = UTIL_clockSpanNano(clockStart);
529         if(el1 < el2) {
530             U64 iv = el2 - el1;
531             if(g_clockGranularity > iv) {
532                 g_clockGranularity = iv;
533                 i = 0;
534             } else {
535                 i++;
536             }
537         }
538     } while(i < 10);
539     DEBUGOUTPUT("Granularity: %llu\n", (unsigned long long)g_clockGranularity);
540 }
541
542 /*-************************************
543 *  Optimizer Util Functions
544 **************************************/
545
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);
552 }
553
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.
559  */
560 static double
561 resultScore(const BMK_benchResult_t res, const size_t srcSize, const constraint_t target)
562 {
563     double cs = 0., ds = 0., rt, cm = 0.;
564     const double r1 = 1, r2 = 0.1, rtr = 0.5;
565     double ret;
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);
570
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;
573
574     return ret;
575 }
576
577 /* calculates normalized squared euclidean distance of result1 if it is in the first quadrant relative to lvlRes */
578 static double
579 resultDistLvl(const BMK_benchResult_t result1, const BMK_benchResult_t lvlRes)
580 {
581     double normalizedCSpeedGain1 = (result1.cSpeed / lvlRes.cSpeed) - 1;
582     double normalizedRatioGain1 = ((double)lvlRes.cSize / result1.cSize) - 1;
583     if(normalizedRatioGain1 < 0 || normalizedCSpeedGain1 < 0) {
584         return 0.0;
585     }
586     return normalizedRatioGain1 * g_ratioMultiplier + normalizedCSpeedGain1;
587 }
588
589 /* return true if r2 strictly better than r1 */
590 static int
591 compareResultLT(const BMK_benchResult_t result1, const BMK_benchResult_t result2, const constraint_t target, size_t srcSize)
592 {
593     if(feasible(result1, target) && feasible(result2, target)) {
594         if(g_optmode) {
595             return resultDistLvl(result1, g_lvltarget) < resultDistLvl(result2, g_lvltarget);
596         } else {
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);
600         }
601     }
602     return feasible(result2, target)
603         || (!feasible(result1, target)
604             && (resultScore(result1, srcSize, target) < resultScore(result2, srcSize, target)));
605 }
606
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;
611     return target;
612 }
613
614 static void optimizerAdjustInput(paramValues_t* pc, const size_t maxBlockSize)
615 {
616     varInds_t v;
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 ",
623                         g_paramNames[v]);
624                 displayParamVal(stderr, v, newval, 0); DISPLAY("\n");
625             }
626         }
627     }
628
629     if(pc->vals[wlog_ind] != PARAM_UNSET) {
630
631         U32 sshb = maxBlockSize > 1 ? ZSTD_highbit32((U32)(maxBlockSize-1)) + 1 : 1;
632         /* edge case of highBit not working for 0 */
633
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]);
640             }
641         }
642     }
643
644     if(pc->vals[wlog_ind] != PARAM_UNSET && pc->vals[clog_ind] != PARAM_UNSET) {
645         U32 maxclog;
646         if(pc->vals[strt_ind] == PARAM_UNSET || pc->vals[strt_ind] >= (U32)ZSTD_btlazy2) {
647             maxclog = pc->vals[wlog_ind] + 1;
648         } else {
649             maxclog = pc->vals[wlog_ind];
650         }
651
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]);
656         }
657     }
658
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]);
664         }
665     }
666
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]);
672         }
673     }
674 }
675
676 static int
677 redundantParams(const paramValues_t paramValues, const constraint_t target, const size_t maxBlockSize)
678 {
679     return
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 */
685 }
686
687
688 /*-************************************
689 *  Display Functions
690 **************************************/
691
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 */
697 static void
698 BMK_paramValues_into_commandLine(FILE* f, const paramValues_t params)
699 {
700     varInds_t v;
701     int first = 1;
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]);
707
708         if (v == strt_ind) { fprintf(f,"%u", (unsigned)params.vals[v]); }
709         else { displayParamVal(f, v, params.vals[v], 0); }
710         first = 0;
711     }
712     fprintf(f, "\n");
713 }
714
715
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
721
722 #define SPEED_RESULT 4
723 #define SIZE_RESULT 5
724 /* maybe have epsilon-eq to limit table size? */
725 static int
726 speedSizeCompare(const BMK_benchResult_t r1, const BMK_benchResult_t r2)
727 {
728     if(r1.cSpeed < r2.cSpeed) {
729         if(r1.cSize >= r2.cSize) {
730             return BETTER_RESULT;
731         }
732         return SPEED_RESULT; /* r2 is smaller but not faster. */
733     } else {
734         if(r1.cSize <= r2.cSize) {
735             return WORSE_RESULT;
736         }
737         return SIZE_RESULT; /* r2 is faster but not smaller */
738     }
739 }
740
741 /* 0 for insertion, 1 for no insert */
742 /* maintain invariant speedSizeCompare(n, n->next) = SPEED_RESULT */
743 static int
744 insertWinner(const winnerInfo_t w, const constraint_t targetConstraints)
745 {
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)) {
750         return 1;
751     }
752
753     if(g_winners == NULL) {
754         winner_ll_node* first_node = malloc(sizeof(winner_ll_node));
755         if(first_node == NULL) {
756             return 1;
757         }
758         first_node->next = NULL;
759         first_node->res = w;
760         g_winners = first_node;
761         return 0;
762     }
763
764     while(cur_node->next != NULL) {
765         switch(speedSizeCompare(cur_node->res.result, r)) {
766             case WORSE_RESULT:
767             {
768                 return 1; /* never insert if better */
769             }
770             case BETTER_RESULT:
771             {
772                 winner_ll_node* tmp;
773                 cur_node->res = cur_node->next->res;
774                 tmp = cur_node->next;
775                 cur_node->next = cur_node->next->next;
776                 free(tmp);
777                 break;
778             }
779             case SIZE_RESULT:
780             {
781                 cur_node = cur_node->next;
782                 break;
783             }
784             case SPEED_RESULT: /* insert after first size result, then return */
785             {
786                 winner_ll_node* newnode = malloc(sizeof(winner_ll_node));
787                 if(newnode == NULL) {
788                     return 1;
789                 }
790                 newnode->res = cur_node->res;
791                 cur_node->res = w;
792                 newnode->next = cur_node->next;
793                 cur_node->next = newnode;
794                 return 0;
795             }
796         }
797
798     }
799
800     assert(cur_node->next == NULL);
801     switch(speedSizeCompare(cur_node->res.result, r)) {
802         case WORSE_RESULT:
803         {
804             return 1; /* never insert if better */
805         }
806         case BETTER_RESULT:
807         {
808             cur_node->res = w;
809             return 0;
810         }
811         case SIZE_RESULT:
812         {
813             winner_ll_node* newnode = malloc(sizeof(winner_ll_node));
814             if(newnode == NULL) {
815                 return 1;
816             }
817             newnode->res = w;
818             newnode->next = NULL;
819             cur_node->next = newnode;
820             return 0;
821         }
822         case SPEED_RESULT: /* insert before first size result, then return */
823         {
824             winner_ll_node* newnode = malloc(sizeof(winner_ll_node));
825             if(newnode == NULL) {
826                 return 1;
827             }
828             newnode->res = cur_node->res;
829             cur_node->res = w;
830             newnode->next = cur_node->next;
831             cur_node->next = newnode;
832             return 0;
833         }
834         default:
835             return 1;
836     }
837 }
838
839 static void
840 BMK_displayOneResult(FILE* f, winnerInfo_t res, const size_t srcSize)
841 {
842     varInds_t v;
843     int first = 1;
844     res.params = cParamUnsetMin(res.params);
845     fprintf(f, "    {");
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);
850         first = 0;
851     }
852
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;
857
858         fprintf(f, " },     /* R:%5.3f at %5.1f MB/s - %5.1f MB/s */\n",
859                             ratio, cSpeedMBps, dSpeedMBps);
860     }
861 }
862
863 /* Writes to f the results of a parameter benchmark */
864 /* when used with --optimize, will only print results better than previously discovered */
865 static void
866 BMK_printWinner(FILE* f, const int cLevel, const BMK_benchResult_t result, const paramValues_t params, const size_t srcSize)
867 {
868     char lvlstr[15] = "Custom Level";
869     winnerInfo_t w;
870     w.params = params;
871     w.result = result;
872
873     fprintf(f, "\r%79s\r", "");
874
875     if(cLevel != CUSTOM_LEVEL) {
876         snprintf(lvlstr, 15, "  Level %2d  ", cLevel);
877     }
878
879     if(TIMED) {
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 );
887     }
888
889     fprintf(f, "/* %s */   ", lvlstr);
890     BMK_displayOneResult(f, w, srcSize);
891 }
892
893 static void
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)
895 {
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 } }
900                                    };
901     if ( DEBUG
902       || compareResultLT(g_winner.result, result, targetConstraints, srcSize)
903       || g_displayLevel >= 4) {
904         if ( DEBUG
905           && compareResultLT(g_winner.result, result, targetConstraints, srcSize)) {
906             DISPLAY("New Winner: \n");
907         }
908
909         if(g_displayLevel >= 2) {
910             BMK_printWinner(f, cLevel, result, params, srcSize);
911         }
912
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;
917         }
918     }
919
920     if(g_optmode && g_optimizer && (DEBUG || g_displayLevel == 3)) {
921         winnerInfo_t w;
922         winner_ll_node* n;
923         w.result = result;
924         w.params = params;
925         insertWinner(w, targetConstraints);
926
927         if(!DEBUG) { fprintf(f, "\033c"); }
928         fprintf(f, "\n");
929
930         /* the table */
931         fprintf(f, "================================\n");
932         for(n = g_winners; n != NULL; n = n->next) {
933             BMK_displayOneResult(f, n->res, srcSize);
934         }
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);
938
939
940         fprintf(f, "Overall Winner: \n");
941         BMK_displayOneResult(f, g_winner, srcSize);
942         BMK_paramValues_into_commandLine(f, g_winner.params);
943
944         fprintf(f, "Latest BMK: \n");\
945         BMK_displayOneResult(f, w, srcSize);
946     }
947 }
948
949
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.
954  */
955 static void
956 BMK_print_cLevelEntry(FILE* f, const int cLevel,
957                       paramValues_t params,
958                       const BMK_benchResult_t result, const size_t srcSize)
959 {
960     varInds_t v;
961     int first = 1;
962
963     assert(cLevel >= 0);
964     assert(cLevel <= NB_LEVELS_TRACKED);
965     params = cParamUnsetMin(params);
966
967     fprintf(f, "   {");
968     /* print cParams.
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);
973         first = 0;
974     }
975     /* print comment */
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;
980
981         fprintf(f, " },   /* level %2i:  R=%5.3f at %5.1f MB/s - %5.1f MB/s */\n",
982                              cLevel, ratio, cSpeedMBps, dSpeedMBps);
983     }
984 }
985
986
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.
992  */
993 static void
994 BMK_print_cLevelTable(FILE* f, const winnerInfo_t* winners, const size_t srcSize)
995 {
996     int cLevel;
997
998     fprintf(f, "\n /* Proposed configurations : */ \n");
999     fprintf(f, "   /* W,  C,  H,  S,  L,  T, strat */ \n");
1000
1001     for (cLevel=0; cLevel <= NB_LEVELS_TRACKED; cLevel++)
1002         BMK_print_cLevelEntry(f,
1003                               cLevel, winners[cLevel].params,
1004                               winners[cLevel].result, srcSize);
1005 }
1006
1007
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.
1014  */
1015 static void
1016 BMK_saveAndPrint_cLevelTable(FILE* const f,
1017                        const winnerInfo_t* winners,
1018                        const size_t srcSize)
1019 {
1020     fseek(f, 0, SEEK_SET);
1021     BMK_print_cLevelTable(f, winners, srcSize);
1022     fflush(f);
1023     BMK_print_cLevelTable(stdout, winners, srcSize);
1024 }
1025
1026
1027 /*-*******************************************************
1028 *  Functions to Benchmark
1029 *********************************************************/
1030
1031 typedef struct {
1032     ZSTD_CCtx* cctx;
1033     const void* dictBuffer;
1034     size_t dictBufferSize;
1035     int cLevel;
1036     const paramValues_t* comprParams;
1037 } BMK_initCCtxArgs;
1038
1039 static size_t local_initCCtx(void* payload) {
1040     const BMK_initCCtxArgs* ag = (const BMK_initCCtxArgs*)payload;
1041     varInds_t i;
1042     ZSTD_CCtx_reset(ag->cctx, ZSTD_reset_session_and_parameters);
1043     ZSTD_CCtx_setParameter(ag->cctx, ZSTD_c_compressionLevel, ag->cLevel);
1044
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]);
1048     }
1049     ZSTD_CCtx_loadDictionary(ag->cctx, ag->dictBuffer, ag->dictBufferSize);
1050
1051     return 0;
1052 }
1053
1054 typedef struct {
1055     ZSTD_DCtx* dctx;
1056     const void* dictBuffer;
1057     size_t dictBufferSize;
1058 } BMK_initDCtxArgs;
1059
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);
1064     return 0;
1065 }
1066
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,
1071                             void* addArgs)
1072 {
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);
1076 }
1077
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,
1082     void* addArgs) {
1083     size_t moreToFlush = 1;
1084     ZSTD_DCtx* dctx = (ZSTD_DCtx*)addArgs;
1085     ZSTD_inBuffer in;
1086     ZSTD_outBuffer out;
1087     in.src = srcBuffer;
1088     in.size = srcSize;
1089     in.pos = 0;
1090     out.dst = dstBuffer;
1091     out.size = dstSize;
1092     out.pos = 0;
1093     while (moreToFlush) {
1094         if(out.pos == out.size) {
1095             return (size_t)-ZSTD_error_dstSize_tooSmall;
1096         }
1097         moreToFlush = ZSTD_decompressStream(dctx,
1098                             &out, &in);
1099         if (ZSTD_isError(moreToFlush)) {
1100             return moreToFlush;
1101         }
1102     }
1103     return out.pos;
1104
1105 }
1106
1107 /*-************************************
1108 *  Data Initialization Functions
1109 **************************************/
1110
1111 typedef struct {
1112     void* srcBuffer;
1113     size_t srcSize;
1114     const void** srcPtrs;
1115     size_t* srcSizes;
1116     void** dstPtrs;
1117     size_t* dstCapacities;
1118     size_t* dstSizes;
1119     void** resPtrs;
1120     size_t* resSizes;
1121     size_t nbBlocks;
1122     size_t maxBlockSize;
1123 } buffers_t;
1124
1125 typedef struct {
1126     size_t dictSize;
1127     void* dictBuffer;
1128     ZSTD_CCtx* cctx;
1129     ZSTD_DCtx* dctx;
1130 } contexts_t;
1131
1132 static void freeNonSrcBuffers(const buffers_t b) {
1133     free(b.srcPtrs);
1134     free(b.srcSizes);
1135
1136     if(b.dstPtrs != NULL) {
1137         free(b.dstPtrs[0]);
1138     }
1139     free(b.dstPtrs);
1140     free(b.dstCapacities);
1141     free(b.dstSizes);
1142
1143     if(b.resPtrs != NULL) {
1144         free(b.resPtrs[0]);
1145     }
1146     free(b.resPtrs);
1147     free(b.resSizes);
1148 }
1149
1150 static void freeBuffers(const buffers_t b) {
1151     if(b.srcPtrs != NULL) {
1152         free(b.srcBuffer);
1153     }
1154     freeNonSrcBuffers(b);
1155 }
1156
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)
1160 {
1161     size_t pos = 0, n, blockSize;
1162     U32 maxNbBlocks, blockNb = 0;
1163     buff->srcSize = 0;
1164     for(n = 0; n < nbFiles; n++) {
1165         buff->srcSize += fileSizes[n];
1166     }
1167
1168     if(buff->srcSize == 0) {
1169         DISPLAY("No data to bench\n");
1170         return 1;
1171     }
1172
1173     blockSize = g_blockSize ? g_blockSize : buff->srcSize;
1174     maxNbBlocks = (U32) ((buff->srcSize + (blockSize-1)) / blockSize) + (U32)nbFiles;
1175
1176     buff->srcPtrs = (const void**)calloc(maxNbBlocks, sizeof(void*));
1177     buff->srcSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
1178
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));
1182
1183     buff->resPtrs = (void**)calloc(maxNbBlocks, sizeof(void*));
1184     buff->resSizes = (size_t*)malloc(maxNbBlocks * sizeof(size_t));
1185
1186     if(!buff->srcPtrs || !buff->srcSizes || !buff->dstPtrs || !buff->dstCapacities || !buff->dstSizes || !buff->resPtrs || !buff->resSizes) {
1187         DISPLAY("alloc error\n");
1188         freeNonSrcBuffers(*buff);
1189         return 1;
1190     }
1191
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);
1196
1197     if(!buff->dstPtrs[0] || !buff->resPtrs[0]) {
1198         DISPLAY("alloc error\n");
1199         freeNonSrcBuffers(*buff);
1200         return 1;
1201     }
1202
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;
1208             pos += blockSize;
1209         }
1210
1211         if(fileSizes[n] > 0) { buff->srcSizes[blockNb - 1] = ((fileSizes[n] - 1) % blockSize) + 1; }
1212         pos = pos_end;
1213     }
1214
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];
1219
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];
1226
1227         buff->maxBlockSize = MAX(buff->maxBlockSize, buff->srcSizes[n]);
1228     }
1229
1230     buff->nbBlocks = blockNb;
1231
1232     return 0;
1233 }
1234
1235 /* allocates buffer's arguments. returns success / failuere */
1236 static int createBuffers(buffers_t* buff, const char* const * const fileNamesTable,
1237                           size_t nbFiles) {
1238     size_t pos = 0;
1239     size_t n;
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;
1244     int ret = 0;
1245
1246     if(!totalSizeToLoad || !benchedSize) {
1247         ret = 1;
1248         DISPLAY("Nothing to Bench\n");
1249         goto _cleanUp;
1250     }
1251
1252     srcBuffer = malloc(benchedSize);
1253
1254     if(!fileSizes || !srcBuffer) {
1255         ret = 1;
1256         goto _cleanUp;
1257     }
1258
1259     for(n = 0; n < nbFiles; n++) {
1260         FILE* f;
1261         U64 fileSize = UTIL_getFileSize(fileNamesTable[n]);
1262         if (UTIL_isDirectory(fileNamesTable[n])) {
1263             DISPLAY("Ignoring %s directory...       \n", fileNamesTable[n]);
1264             continue;
1265         }
1266         if (fileSize == UTIL_FILESIZE_UNKNOWN) {
1267             DISPLAY("Cannot evaluate size of %s, ignoring ... \n", fileNamesTable[n]);
1268             continue;
1269         }
1270         f = fopen(fileNamesTable[n], "rb");
1271         if (f==NULL) {
1272             DISPLAY("impossible to open file %s\n", fileNamesTable[n]);
1273             fclose(f);
1274             ret = 10;
1275             goto _cleanUp;
1276         }
1277
1278         DISPLAYLEVEL(2, "Loading %s...       \r", fileNamesTable[n]);
1279
1280         if (fileSize + pos > benchedSize) fileSize = benchedSize - pos, nbFiles=n;   /* buffer too small - stop after this file */
1281         {
1282             char* buffer = (char*)(srcBuffer);
1283             size_t const readSize = fread((buffer)+pos, 1, (size_t)fileSize, f);
1284             fclose(f);
1285             if (readSize != (size_t)fileSize) {
1286                 DISPLAY("could not read %s", fileNamesTable[n]);
1287                 ret = 1;
1288                 goto _cleanUp;
1289             }
1290
1291             fileSizes[n] = readSize;
1292             pos += readSize;
1293         }
1294     }
1295
1296     ret = createBuffersFromMemory(buff, srcBuffer, nbFiles, fileSizes);
1297
1298 _cleanUp:
1299     if(ret) { free(srcBuffer); }
1300     free(fileSizes);
1301     return ret;
1302 }
1303
1304 static void freeContexts(const contexts_t ctx) {
1305     free(ctx.dictBuffer);
1306     ZSTD_freeCCtx(ctx.cctx);
1307     ZSTD_freeDCtx(ctx.dctx);
1308 }
1309
1310 static int createContexts(contexts_t* ctx, const char* dictFileName) {
1311     FILE* f;
1312     size_t readSize;
1313     ctx->cctx = ZSTD_createCCtx();
1314     ctx->dctx = ZSTD_createDCtx();
1315     assert(ctx->cctx != NULL);
1316     assert(ctx->dctx != NULL);
1317
1318     if(dictFileName == NULL) {
1319         ctx->dictSize = 0;
1320         ctx->dictBuffer = NULL;
1321         return 0;
1322     }
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 */
1327     }
1328     ctx->dictBuffer = malloc(ctx->dictSize);
1329
1330     f = fopen(dictFileName, "rb");
1331
1332     if (f==NULL) {
1333         DISPLAY("unable to open file\n");
1334         freeContexts(*ctx);
1335         return 1;
1336     }
1337
1338     if (ctx->dictSize > 64 MB || !(ctx->dictBuffer)) {
1339         DISPLAY("dictionary too large\n");
1340         fclose(f);
1341         freeContexts(*ctx);
1342         return 1;
1343     }
1344     readSize = fread(ctx->dictBuffer, 1, ctx->dictSize, f);
1345     fclose(f);
1346     if (readSize != ctx->dictSize) {
1347         DISPLAY("unable to read file\n");
1348         freeContexts(*ctx);
1349         return 1;
1350     }
1351     return 0;
1352 }
1353
1354 /*-************************************
1355 *  Optimizer Memoization Functions
1356 **************************************/
1357
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) {
1362     size_t i, j = 0;
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];
1369             j++;
1370         }
1371     }
1372     return j;
1373 }
1374
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) {
1379     varInds_t i;
1380     size_t j = 0;
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 */
1384             res[j] = i; j++;
1385         }
1386     }
1387     return j;
1388 }
1389
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;
1393     size_t i;
1394     for(i = 0; i < varyLen; i++) {
1395         if(varyParams[i] == strt_ind) continue; /* strategy separated by table */
1396         arrayLen *= rangetable[varyParams[i]];
1397     }
1398     return arrayLen;
1399 }
1400
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) {
1403     size_t i;
1404     unsigned ind = 0;
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]);
1409     }
1410     return ind;
1411 }
1412
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) {
1416         case directMap:
1417             return mt.table[memoTableIndDirect(&p, mt.varArray, mt.varLen)];
1418         case xxhashMap:
1419             return mt.table[(XXH64(&p.vals, sizeof(U32) * NUM_PARAMS, 0) >> 3) % mt.tableLen];
1420         case noMemo:
1421             return 0;
1422     }
1423     return 0; /* should never happen, stop compiler warnings */
1424 }
1425
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) {
1429         case directMap:
1430             mt.table[memoTableIndDirect(&p, mt.varArray, mt.varLen)] = value; break;
1431         case xxhashMap:
1432             mt.table[(XXH64(&p.vals, sizeof(U32) * NUM_PARAMS, 0) >> 3) % mt.tableLen] = value; break;
1433         case noMemo:
1434             break;
1435     }
1436 }
1437
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) {
1442     int i;
1443     if(mtAll == NULL) { return; }
1444     for(i = 1; i <= (int)ZSTD_STRATEGY_MAX; i++) {
1445         free(mtAll[i].table);
1446     }
1447     free(mtAll);
1448 }
1449
1450 /* inits memotables for all (including mallocs), all strategies */
1451 /* takes unsanitized varyParams */
1452 static memoTable_t*
1453 createMemoTableArray(const paramValues_t p,
1454                      const varInds_t* const varyParams,
1455                      const size_t varyLen,
1456                      const U32 memoTableLog)
1457 {
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;
1460
1461     if(mtAll == NULL) {
1462         return NULL;
1463     }
1464
1465     for(i = 1; i <= (int)ZSTD_STRATEGY_MAX; i++) {
1466         mtAll[i].varLen = sanitizeVarArray(mtAll[i].varArray, varyLen, varyParams, i);
1467     }
1468
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;
1475         }
1476         return mtAll;
1477     }
1478
1479
1480     if(p.vals[strt_ind] != PARAM_UNSET) {
1481         stratMin = p.vals[strt_ind];
1482         stratMax = p.vals[strt_ind];
1483     }
1484
1485
1486     for(i = stratMin; i <= stratMax; i++) {
1487         size_t mtl = memoTableLen(mtAll[i].varArray, mtAll[i].varLen);
1488         mtAll[i].tableType = directMap;
1489
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);
1493         }
1494
1495         mtAll[i].table = (BYTE*)calloc(sizeof(BYTE), mtl);
1496         mtAll[i].tableLen = mtl;
1497
1498         if(mtAll[i].table == NULL) {
1499             freeMemoTableArray(mtAll);
1500             return NULL;
1501         }
1502     }
1503
1504     return mtAll;
1505 }
1506
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)
1510 {
1511     size_t j;
1512     const memoTable_t mt = memoTableArray[st];
1513     pc->vals[strt_ind] = st;
1514     for(j = 0; j < mt.tableLen; j++) {
1515         int i;
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]);
1520         }
1521
1522         if(!(memoTableGet(memoTableArray, *pc))) break; /* only pick unpicked params. */
1523     }
1524 }
1525
1526 /*-************************************
1527 *  Benchmarking Functions
1528 **************************************/
1529
1530 static void display_params_tested(paramValues_t cParams)
1531 {
1532     varInds_t vi;
1533     DISPLAYLEVEL(3, "\r testing :");
1534     for (vi=0; vi < NUM_PARAMS; vi++) {
1535         DISPLAYLEVEL(3, "%3u,", (unsigned)cParams.vals[vi]);
1536     }
1537     DISPLAYLEVEL(3, "\b    \r");
1538 }
1539
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)
1552 {
1553     U32 i;
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;
1568
1569     /* init */
1570     display_params_tested(*comprParams);
1571     memset(&bResult, 0, sizeof(bResult));
1572
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);
1577         } else {
1578             RDG_genBuffer(resPtrs[i], resSizes[i], 0.10, 0.50, 1);
1579         }
1580     }
1581
1582     /* Bench */
1583     {
1584         /* init args */
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;
1592
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;
1604
1605         cctxprep.cctx = cctx;
1606         cctxprep.dictBuffer = dictBuffer;
1607         cctxprep.dictBufferSize = dictBufferSize;
1608         cctxprep.cLevel = cLevel;
1609         cctxprep.comprParams = comprParams;
1610
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;
1622
1623         dctxprep.dctx = dctx;
1624         dctxprep.dictBuffer = dictBuffer;
1625         dctxprep.dictBufferSize = dictBufferSize;
1626
1627         assert(timeStateCompress != NULL);
1628         assert(timeStateDecompress != NULL);
1629         while(!compressionCompleted) {
1630             BMK_runOutcome_t const cOutcome = BMK_benchTimedFn(timeStateCompress, cbp);
1631
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);
1638                 return bOut;
1639             }
1640             {   BMK_runTime_t const rResult = BMK_extract_runTime(cOutcome);
1641                 bResult.cSpeed = (srcSize * TIMELOOP_NANOSEC) / rResult.nanoSecPerRun;
1642                 bResult.cSize = rResult.sumOfReturn;
1643             }
1644             compressionCompleted = BMK_isCompleted_TimedFn(timeStateCompress);
1645         }
1646
1647         while (!decompressionCompleted) {
1648             BMK_runOutcome_t const dOutcome = BMK_benchTimedFn(timeStateDecompress, dbp);
1649
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);
1656                 return bOut;
1657             }
1658             {   BMK_runTime_t const rResult = BMK_extract_runTime(dOutcome);
1659                 bResult.dSpeed = (srcSize * TIMELOOP_NANOSEC) / rResult.nanoSecPerRun;
1660             }
1661             decompressionCompleted = BMK_isCompleted_TimedFn(timeStateDecompress);
1662         }
1663
1664         BMK_freeTimedFnState(timeStateCompress);
1665         BMK_freeTimedFnState(timeStateDecompress);
1666     }
1667
1668    /* Bench */
1669     bResult.cMem = (1 << (comprParams->vals[wlog_ind])) + ZSTD_sizeof_CCtx(cctx);
1670
1671     {   BMK_benchOutcome_t bOut;
1672         bOut.tag = 0;
1673         bOut.internal_never_use_directly = bResult;  /* should be a function */
1674         return bOut;
1675     }
1676 }
1677
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)
1685 {
1686     BMK_benchOutcome_t const outcome = BMK_benchMemInvertible(buf, ctx,
1687                                                         BASE_CLEVEL, &cParams,
1688                                                         BMK_both, 3);
1689     if (!BMK_isSuccessful_benchOutcome(outcome)) return 1;
1690     *resultPtr = BMK_extract_benchResult(outcome);
1691     return 0;
1692 }
1693
1694
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)
1702 {
1703     BMK_benchResult_t benchres;
1704     double uncertaintyConstantC = 3., uncertaintyConstantD = 3.;
1705     double winnerRS;
1706
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;
1711     }
1712     benchres = BMK_extract_benchResult(outcome);
1713
1714     winnerRS = resultScore(*winnerResult, buf.srcSize, target);
1715     DEBUGOUTPUT("WinnerScore: %f \n ", winnerRS);
1716
1717     *resultPtr = benchres;
1718
1719     /* anything with worse ratio in feas is definitely worse, discard */
1720     if(feas && benchres.cSize < winnerResult->cSize && !g_optmode) {
1721         return WORSE_RESULT;
1722     }
1723
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);
1728     }
1729
1730     if (benchres.dSpeed) {
1731         U64 const loopDurationD = (((U64)buf.srcSize * TIMELOOP_NANOSEC) / benchres.dSpeed);
1732         uncertaintyConstantD = ((loopDurationD + (double)(2 * g_clockGranularity))/loopDurationD);
1733     }
1734
1735     /* optimistic assumption of benchres */
1736     {   BMK_benchResult_t resultMax = benchres;
1737         resultMax.cSpeed *= uncertaintyConstantC * VARIANCE;
1738         resultMax.dSpeed *= uncertaintyConstantD * VARIANCE;
1739
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;
1745         }
1746     }
1747
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;
1753     } else {
1754         return WORSE_RESULT;
1755     }
1756 }
1757
1758
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,
1766                 const int feas) {
1767     static int bmcount = 0;
1768     int res;
1769
1770     if ( memoTableGet(memoTableArray, cParams) >= INFEASIBLE_THRESHOLD
1771       || redundantParams(cParams, target, buf.maxBlockSize) ) {
1772         return WORSE_RESULT;
1773     }
1774
1775     res = allBench(resultPtr, buf, ctx, cParams, target, winnerResult, feas);
1776
1777     if(DEBUG && !(bmcount % 250)) {
1778         DISPLAY("Count: %d\n", bmcount);
1779         bmcount++;
1780     }
1781     BMK_printWinnerOpt(stdout, CUSTOM_LEVEL, *resultPtr, cParams, target, buf.srcSize);
1782
1783     if(res == BETTER_RESULT || feas) {
1784         memoTableSet(memoTableArray, cParams, 255); /* what happens if collisions are frequent */
1785     }
1786     return res;
1787 }
1788
1789
1790 typedef struct {
1791     U64 cSpeed_min;
1792     U64 dSpeed_min;
1793     U32 windowLog_max;
1794     ZSTD_strategy strategy_max;
1795 } level_constraints_t;
1796
1797 static level_constraints_t g_level_constraint[NB_LEVELS_TRACKED+1];
1798
1799 static void BMK_init_level_constraints(int bytePerSec_level1)
1800 {
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;
1807
1808     /* establish speed objectives (relative to level 1) */
1809     {   int l;
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;
1815     }   }
1816 }
1817
1818 static int BMK_seed(winnerInfo_t* winners,
1819                     const paramValues_t params,
1820                     const buffers_t buf,
1821                     const contexts_t ctx)
1822 {
1823     BMK_benchResult_t testResult;
1824     int better = 0;
1825     int cLevel;
1826
1827     BMK_benchParam(&testResult, buf, ctx, params);
1828
1829     for (cLevel = 1; cLevel <= NB_LEVELS_TRACKED; cLevel++) {
1830
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);
1844             better = 1;
1845             continue;
1846         }
1847
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);
1858
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);
1863
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);
1866
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);
1869
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);
1876                 continue;
1877             }
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,
1884                          cLevel);
1885                 continue;
1886             }
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,
1893                          cLevel);
1894                 continue;
1895             }
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,
1902                          cLevel);
1903                 continue;
1904             }
1905
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);
1909
1910             winners[cLevel].result = testResult;
1911             winners[cLevel].params = params;
1912             BMK_print_cLevelEntry(stdout, cLevel, params, testResult, buf.srcSize);
1913
1914             better = 1;
1915     }   }
1916
1917     return better;
1918 }
1919
1920 /*-************************************
1921 *  Compression Level Table Generation Functions
1922 **************************************/
1923
1924 #define PARAMTABLELOG   25
1925 #define PARAMTABLESIZE (1<<PARAMTABLELOG)
1926 #define PARAMTABLEMASK (PARAMTABLESIZE-1)
1927 static BYTE g_alreadyTested[PARAMTABLESIZE] = {0};   /* init to zero */
1928
1929 static BYTE* NB_TESTS_PLAYED(paramValues_t p)
1930 {
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];
1934 }
1935
1936 static void playAround(FILE* f,
1937                        winnerInfo_t* winners,
1938                        paramValues_t p,
1939                        const buffers_t buf, const contexts_t ctx)
1940 {
1941     int nbVariations = 0;
1942     UTIL_time_t const clockStart = UTIL_getTime();
1943
1944     while (UTIL_clockSpanMicro(clockStart) < g_maxVariationTime) {
1945         if (nbVariations++ > g_maxNbVariations) break;
1946
1947         do {
1948             int i;
1949             for(i = 0; i < 4; i++) {
1950                 paramVaryOnce(FUZ_rand(&g_rand) % (strt_ind + 1),
1951                               ((FUZ_rand(&g_rand) & 1) << 1) - 1,
1952                               &p);
1953             }
1954         } while (!paramValid(p));
1955
1956         /* exclude faster if already played params */
1957         if (FUZ_rand(&g_rand) & ((1 << *NB_TESTS_PLAYED(p))-1))
1958             continue;
1959
1960         /* test */
1961         {   BYTE* const b = NB_TESTS_PLAYED(p);
1962             (*b)++;
1963         }
1964         if (!BMK_seed(winners, p, buf, ctx)) continue;
1965
1966         /* improvement found => search more */
1967         BMK_saveAndPrint_cLevelTable(f, winners, buf.srcSize);
1968         playAround(f, winners, p, buf, ctx);
1969     }
1970
1971 }
1972
1973 static void
1974 BMK_selectRandomStart( FILE* f,
1975                        winnerInfo_t* winners,
1976                        const buffers_t buf, const contexts_t ctx)
1977 {
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 */
1982                                              buf.srcSize, 0);
1983         playAround(f, winners, p, buf, ctx);
1984     } else {
1985         playAround(f, winners, winners[id].params, buf, ctx);
1986     }
1987 }
1988
1989
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.
1996  */
1997 static void BMK_generate_cLevelTable(const buffers_t buf, const contexts_t ctx)
1998 {
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");
2003
2004     /* init */
2005     assert(g_singleRun==0);
2006     memset(winners, 0, sizeof(winners));
2007     if (f==NULL) { DISPLAY("error opening %s \n", rfName); exit(1); }
2008
2009     if (g_target) {
2010         BMK_init_level_constraints(g_target * MB_UNIT);
2011     } else {
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));
2017     }
2018
2019     /* populate initial solution */
2020     {   const int maxSeeds = g_noSeed ? 1 : ZSTD_maxCLevel();
2021         int i;
2022         for (i=0; i<=maxSeeds; i++) {
2023             params = cParamsToPVals(ZSTD_getCParams(i, buf.maxBlockSize, 0));
2024             BMK_seed(winners, params, buf, ctx);
2025     }   }
2026     BMK_saveAndPrint_cLevelTable(f, winners, buf.srcSize);
2027
2028     /* start tests */
2029     {   const UTIL_time_t grillStart = UTIL_getTime();
2030         do {
2031             BMK_selectRandomStart(f, winners, buf, ctx);
2032         } while (BMK_timeSpan_s(grillStart) < g_timeLimit_s);
2033     }
2034
2035     /* end summary */
2036     BMK_saveAndPrint_cLevelTable(f, winners, buf.srcSize);
2037     DISPLAY("grillParams operations completed \n");
2038
2039     /* clean up*/
2040     fclose(f);
2041 }
2042
2043
2044 /*-************************************
2045 *  Single Benchmark Functions
2046 **************************************/
2047
2048 static int
2049 benchOnce(const buffers_t buf, const contexts_t ctx, const int cLevel)
2050 {
2051     BMK_benchResult_t testResult;
2052     g_params = adjustParams(overwriteParams(cParamsToPVals(ZSTD_getCParams(cLevel, buf.maxBlockSize, ctx.dictSize)), g_params), buf.maxBlockSize, ctx.dictSize);
2053
2054     if (BMK_benchParam(&testResult, buf, ctx, g_params)) {
2055         DISPLAY("Error during benchmarking\n");
2056         return 1;
2057     }
2058
2059     BMK_printWinner(stdout, CUSTOM_LEVEL, testResult, g_params, buf.srcSize);
2060
2061     return 0;
2062 }
2063
2064 static int benchSample(double compressibility, int cLevel)
2065 {
2066     const char* const name = "Sample 10MB";
2067     size_t const benchedSize = 10 MB;
2068     void* const srcBuffer = malloc(benchedSize);
2069     int ret = 0;
2070
2071     buffers_t buf;
2072     contexts_t ctx;
2073
2074     if(srcBuffer == NULL) {
2075         DISPLAY("Out of Memory\n");
2076         return 2;
2077     }
2078
2079     RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
2080
2081     if(createBuffersFromMemory(&buf, srcBuffer, 1, &benchedSize)) {
2082         DISPLAY("Buffer Creation Error\n");
2083         free(srcBuffer);
2084         return 3;
2085     }
2086
2087     if(createContexts(&ctx, NULL)) {
2088         DISPLAY("Context Creation Error\n");
2089         freeBuffers(buf);
2090         return 1;
2091     }
2092
2093     /* bench */
2094     DISPLAY("\r%79s\r", "");
2095     DISPLAY("using %s %i%%: \n", name, (int)(compressibility*100));
2096
2097     if(g_singleRun) {
2098         ret = benchOnce(buf, ctx, cLevel);
2099     } else {
2100         BMK_generate_cLevelTable(buf, ctx);
2101     }
2102
2103     freeBuffers(buf);
2104     freeContexts(ctx);
2105
2106     return ret;
2107 }
2108
2109 /* benchFiles() :
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)
2114 {
2115     buffers_t buf;
2116     contexts_t ctx;
2117     int ret = 0;
2118
2119     if (createBuffers(&buf, fileNamesTable, nbFiles)) {
2120         DISPLAY("unable to load files\n");
2121         return 1;
2122     }
2123
2124     if (createContexts(&ctx, dictFileName)) {
2125         DISPLAY("unable to load dictionary\n");
2126         freeBuffers(buf);
2127         return 2;
2128     }
2129
2130     DISPLAY("\r%79s\r", "");
2131     if (nbFiles == 1) {
2132         DISPLAY("using %s : \n", fileNamesTable[0]);
2133     } else {
2134         DISPLAY("using %d Files : \n", nbFiles);
2135     }
2136
2137     if (g_singleRun) {
2138         ret = benchOnce(buf, ctx, cLevel);
2139     } else {
2140         BMK_generate_cLevelTable(buf, ctx);
2141     }
2142
2143     freeBuffers(buf);
2144     freeContexts(ctx);
2145     return ret;
2146 }
2147
2148
2149 /*-************************************
2150 *  Local Optimization Functions
2151 **************************************/
2152
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.
2164  */
2165
2166 /* sanitize all params here.
2167  * all generation after random should be sanitized. (maybe sanitize random)
2168  */
2169 static winnerInfo_t climbOnce(const constraint_t target,
2170                 memoTable_t* mtAll,
2171                 const buffers_t buf, const contexts_t ctx,
2172                 const paramValues_t init)
2173 {
2174     /*
2175      * cparam - currently considered 'center'
2176      * candidate - params to benchmark/results
2177      * winner - best option found so far.
2178      */
2179     paramValues_t cparam = init;
2180     winnerInfo_t candidateInfo, winnerInfo;
2181     int better = 1;
2182     int feas = 0;
2183
2184     winnerInfo = initWinnerInfo(init);
2185     candidateInfo = winnerInfo;
2186
2187     {   winnerInfo_t bestFeasible1 = initWinnerInfo(cparam);
2188         DEBUGOUTPUT("Climb Part 1\n");
2189         while(better) {
2190             int offset;
2191             size_t i, dist;
2192             const size_t varLen = mtAll[cparam.vals[strt_ind]].varLen;
2193             better = 0;
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);
2203
2204                     if(paramValid(candidateInfo.params)) {
2205                         int res;
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;
2211                             better = 1;
2212                             if(compareResultLT(bestFeasible1.result, winnerInfo.result, target, buf.srcSize)) {
2213                                 bestFeasible1 = winnerInfo;
2214                             }
2215                         }
2216                     }
2217                 }  /* for (offset = -1; offset <= 1; offset += 2) */
2218             }   /* for (i = 0; i < varLen; i++) */
2219
2220             if(better) {
2221                 continue;
2222             }
2223
2224             for (dist = 2; dist < varLen + 2; dist++) { /* varLen is # dimensions */
2225                 for (i = 0; i < (1 << varLen) / varLen + 2; i++) {
2226                     int res;
2227                     CHECKTIME(winnerInfo);
2228                     candidateInfo.params = cparam;
2229                     /* param error checking already done here */
2230                     paramVariation(&candidateInfo.params, mtAll, (U32)dist);
2231
2232                     res = benchMemo(&candidateInfo.result,
2233                                 buf, ctx,
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;
2239                         better = 1;
2240                         if (compareResultLT(bestFeasible1.result, winnerInfo.result, target, buf.srcSize)) {
2241                             bestFeasible1 = winnerInfo;
2242                         }
2243                         break;
2244                     }
2245                 }
2246
2247                 if (better) {
2248                     break;
2249                 }
2250             }   /* for(dist = 2; dist < varLen + 2; dist++) */
2251
2252             if (!better) { /* infeas -> feas -> stop */
2253                 if (feas) return winnerInfo;
2254                 feas = 1;
2255                 better = 1;
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");
2258             }
2259         }
2260         winnerInfo = bestFeasible1;
2261     }
2262
2263     return winnerInfo;
2264 }
2265
2266 /* Optimizes for a fixed strategy */
2267
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.
2271  */
2272 static winnerInfo_t
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)
2277 {
2278     int i = 0;
2279
2280     paramValues_t init;
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);
2287
2288     init = paramTarget;
2289
2290     for(i = 0; i < tries; i++) {
2291         DEBUGOUTPUT("Restart\n");
2292         do {
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);
2299             i = 0;
2300             continue;
2301         }
2302         CHECKTIME(winnerInfo);
2303         i++;
2304     }
2305     return winnerInfo;
2306 }
2307
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)
2311 {
2312     if(bestStrategy <= currentStrategy) {
2313         int candidate = 2 * bestStrategy - currentStrategy - 1;
2314         if(candidate < 1) {
2315             candidate = currentStrategy + 1;
2316             if(candidate > (int)ZSTD_STRATEGY_MAX) {
2317                 return 0;
2318             } else {
2319                 return candidate;
2320             }
2321         } else {
2322             return candidate;
2323         }
2324     } else { /* bestStrategy >= currentStrategy */
2325         int candidate = 2 * bestStrategy - currentStrategy;
2326         if(candidate > (int)ZSTD_STRATEGY_MAX) {
2327             candidate = currentStrategy - 1;
2328             if(candidate < 1) {
2329                 return 0;
2330             } else {
2331                 return candidate;
2332             }
2333         } else {
2334             return candidate;
2335         }
2336     }
2337 }
2338
2339 /* experiment with playing with this and decay value */
2340
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
2345  * args:
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)
2352  */
2353
2354 static int g_maxTries = 5;
2355 #define TRY_DECAY 1
2356
2357 static int
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)
2363 {
2364     varInds_t varArray [NUM_PARAMS];
2365     int ret = 0;
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;
2370     contexts_t ctx;
2371     buffers_t buf;
2372     g_time = UTIL_getTime();
2373
2374     if (createBuffers(&buf, fileNamesTable, nbFiles)) {
2375         DISPLAY("unable to load files\n");
2376         return 1;
2377     }
2378
2379     if (createContexts(&ctx, dictFileName)) {
2380         DISPLAY("unable to load dictionary\n");
2381         freeBuffers(buf);
2382         return 2;
2383     }
2384
2385     if (nbFiles == 1) {
2386         DISPLAYLEVEL(2, "Loading %s...       \r", fileNamesTable[0]);
2387     } else {
2388         DISPLAYLEVEL(2, "Loading %lu Files...       \r", (unsigned long)nbFiles);
2389     }
2390
2391     /* sanitize paramTarget */
2392     optimizerAdjustInput(&paramTarget, buf.maxBlockSize);
2393     paramBase = cParamUnsetMin(paramTarget);
2394
2395     allMT = createMemoTableArray(paramTarget, varArray, varLen, memoTableLog);
2396
2397     if (!allMT) {
2398         DISPLAY("MemoTable Init Error\n");
2399         ret = 2;
2400         goto _cleanUp;
2401     }
2402
2403     /* default strictnesses */
2404     if (g_strictness == PARAM_UNSET) {
2405         if(g_optmode) {
2406             g_strictness = 100;
2407         } else {
2408             g_strictness = 90;
2409         }
2410     } else {
2411         if(0 >= g_strictness || g_strictness > 100) {
2412             DISPLAY("Strictness Outside of Bounds\n");
2413             ret = 4;
2414             goto _cleanUp;
2415         }
2416     }
2417
2418     /* use level'ing mode instead of normal target mode */
2419     if (g_optmode) {
2420         winner.params = cParamsToPVals(ZSTD_getCParams(cLevelOpt, buf.maxBlockSize, ctx.dictSize));
2421         if(BMK_benchParam(&winner.result, buf, ctx, winner.params)) {
2422             ret = 3;
2423             goto _cleanUp;
2424         }
2425
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;
2430
2431         target.cSpeed = (U32)g_lvltarget.cSpeed;
2432         target.dSpeed = (U32)g_lvltarget.dSpeed;
2433
2434         BMK_printWinnerOpt(stdout, cLevelOpt, winner.result, winner.params, target, buf.srcSize);
2435     }
2436
2437     /* Don't want it to return anything worse than the best known result */
2438     if (g_singleRun) {
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)) {
2442             ret = 45;
2443             goto _cleanUp;
2444         }
2445         if(compareResultLT(winner.result, res, relaxTarget(target), buf.srcSize)) {
2446             winner.result = res;
2447             winner.params = g_params;
2448         }
2449     }
2450
2451     /* bench */
2452     DISPLAYLEVEL(2, "\r%79s\r", "");
2453     if(nbFiles == 1) {
2454         DISPLAYLEVEL(2, "optimizing for %s", fileNamesTable[0]);
2455     } else {
2456         DISPLAYLEVEL(2, "optimizing for %lu Files", (unsigned long)nbFiles);
2457     }
2458
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)); }
2462
2463     DISPLAYLEVEL(2, "\n");
2464     init_clockGranularity();
2465
2466     {   paramValues_t CParams;
2467
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;
2473                 int i;
2474                 for (i=1; i<=maxSeeds; i++) {
2475                     int ec;
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);
2479
2480                     if(!ec && compareResultLT(winner.result, candidate, relaxTarget(target), buf.srcSize)) {
2481                         winner.result = candidate;
2482                         winner.params = CParams;
2483                     }
2484
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; }
2488                 }
2489
2490                 BMK_printWinnerOpt(stdout, CUSTOM_LEVEL, winner.result, winner.params, target, buf.srcSize);
2491             }
2492         }
2493
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;
2500
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)) {
2504                         winner = w1;
2505                     }
2506                     CHECKTIMEGT(ret, 0, _displayCleanUp);
2507                 }
2508
2509                 while(st && tries > 0) {
2510                     winnerInfo_t wc;
2511                     DEBUGOUTPUT("StrategySwitch: %s\n", g_stratName[st]);
2512
2513                     wc = optimizeFixedStrategy(buf, ctx, target, paramBase, st, allMT, tries);
2514
2515                     if(compareResultLT(winner.result, wc.result, target, buf.srcSize)) {
2516                         winner = wc;
2517                         tries = g_maxTries;
2518                         bestStrategy = st;
2519                     } else {
2520                         st = nextStrategy(st, bestStrategy);
2521                         tries -= TRY_DECAY;
2522                     }
2523                     CHECKTIMEGT(ret, 0, _displayCleanUp);
2524                 }
2525             } else {
2526                 winner = optimizeFixedStrategy(buf, ctx, target, paramBase, paramTarget.vals[strt_ind], allMT, g_maxTries);
2527             }
2528
2529         }
2530
2531         /* no solution found */
2532         if(winner.result.cSize == (size_t)-1) {
2533             ret = 1;
2534             DISPLAY("No feasible solution found\n");
2535             goto _cleanUp;
2536         }
2537
2538         /* end summary */
2539 _displayCleanUp:
2540         if (g_displayLevel >= 0) {
2541             BMK_displayOneResult(stdout, winner, buf.srcSize);
2542         }
2543         BMK_paramValues_into_commandLine(stdout, winner.params);
2544         DISPLAYLEVEL(1, "grillParams size - optimizer completed \n");
2545     }
2546
2547 _cleanUp:
2548     freeContexts(ctx);
2549     freeBuffers(buf);
2550     freeMemoTableArray(allMT);
2551     return ret;
2552 }
2553
2554 /*-************************************
2555 *  CLI parsing functions
2556 **************************************/
2557
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.
2562  * from zstdcli.c
2563  */
2564 static unsigned longCommandWArg(const char** stringPtr, const char* longCommand)
2565 {
2566     size_t const comSize = strlen(longCommand);
2567     int const result = !strncmp(*stringPtr, longCommand, comSize);
2568     if (result) *stringPtr += comSize;
2569     return result;
2570 }
2571
2572 static void errorOut(const char* msg)
2573 {
2574     DISPLAY("%s \n", msg); exit(1);
2575 }
2576
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)
2583 {
2584     const char errorMsg[] = "error: numeric value too large";
2585     unsigned sign = 1;
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)++ ;
2592     }
2593     if ((**stringPtr=='K') || (**stringPtr=='M')) {
2594         unsigned const maxK = ((unsigned)(-1)) >> 10;
2595         if (result > maxK) errorOut(errorMsg);
2596         result <<= 10;
2597         if (**stringPtr=='M') {
2598             if (result > maxK) errorOut(errorMsg);
2599             result <<= 10;
2600         }
2601         (*stringPtr)++;  /* skip `K` or `M` */
2602         if (**stringPtr=='i') (*stringPtr)++;
2603         if (**stringPtr=='B') (*stringPtr)++;
2604     }
2605     return result * sign;
2606 }
2607
2608 static double readDoubleFromChar(const char** stringPtr)
2609 {
2610     double result = 0, divide = 10;
2611     while ((**stringPtr >='0') && (**stringPtr <='9')) {
2612         result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
2613     }
2614     if(**stringPtr!='.') {
2615         return result;
2616     }
2617     (*stringPtr)++;
2618     while ((**stringPtr >='0') && (**stringPtr <='9')) {
2619         result += (double)(**stringPtr - '0') / divide, divide *= 10, (*stringPtr)++ ;
2620     }
2621     return result;
2622 }
2623
2624 static int usage(const char* exename)
2625 {
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");
2631     return 0;
2632 }
2633
2634 static int usage_advanced(void)
2635 {
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");
2648     return 0;
2649 }
2650
2651 static int badusage(const char* exename)
2652 {
2653     DISPLAY("Wrong parameters\n");
2654     usage(exename);
2655     return 1;
2656 }
2657
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;                         \
2664           } else break;                                     \
2665 }   }
2666
2667 /* 1 if successful parse, 0 otherwise */
2668 static int parse_params(const char** argptr, paramValues_t* pv) {
2669     int matched = 0;
2670     const char* argOrig = *argptr;
2671     varInds_t v;
2672     for(v = 0; v < NUM_PARAMS; v++) {
2673         if ( longCommandWArg(argptr,g_shortParamNames[v])
2674           || longCommandWArg(argptr, g_paramNames[v]) ) {
2675             if(**argptr == '=') {
2676                 (*argptr)++;
2677                 pv->vals[v] = readU32FromChar(argptr);
2678                 matched = 1;
2679                 break;
2680             }
2681         }
2682         /* reset and try again */
2683         *argptr = argOrig;
2684     }
2685     return matched;
2686 }
2687
2688 /*-************************************
2689 *  Main
2690 **************************************/
2691
2692 int main(int argc, const char** argv)
2693 {
2694     int i,
2695         filenamesStart=0,
2696         result;
2697     const char* exename=argv[0];
2698     const char* input_filename = NULL;
2699     const char* dictFileName = NULL;
2700     U32 main_pause = 0;
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 };
2706
2707     paramValues_t paramTarget = emptyParams();
2708     g_params = emptyParams();
2709
2710     assert(argc>=1);   /* for exename */
2711
2712     for(i=1; i<argc; i++) {
2713         const char* argument = argv[i];
2714         DEBUGOUTPUT("%d: %s\n", i, argument);
2715         assert(argument != NULL);
2716
2717         if(!strcmp(argument,"--no-seed")) { g_noSeed = 1; continue; }
2718
2719         if (longCommandWArg(&argument, "--optimize=")) {
2720             g_optimizer = 1;
2721             for ( ; ;) {
2722                 if(parse_params(&argument, &paramTarget)) { 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; }
2731
2732                 DISPLAY("invalid optimization parameter \n");
2733                 return 1;
2734             }
2735
2736             if (argument[0] != 0) {
2737                 DISPLAY("invalid --optimize= format\n");
2738                 return 1; /* check the end of string */
2739             }
2740             continue;
2741         } else if (longCommandWArg(&argument, "--zstd=")) {
2742         /* Decode command (note : aggregated commands are allowed) */
2743             g_singleRun = 1;
2744             for ( ; ;) {
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; }
2747
2748                 DISPLAY("invalid compression parameter \n");
2749                 return 1;
2750             }
2751
2752             if (argument[0] != 0) {
2753                 DISPLAY("invalid --zstd= format\n");
2754                 return 1; /* check the end of string */
2755             }
2756             continue;
2757             /* if not return, success */
2758
2759         } else if (longCommandWArg(&argument, "--display=")) {
2760             /* Decode command (note : aggregated commands are allowed) */
2761             memset(g_silenceParams, 1, sizeof(g_silenceParams));
2762             for ( ; ;) {
2763                 int found = 0;
2764                 varInds_t v;
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;
2768                         found = 1;
2769                     }
2770                 }
2771                 if(longCommandWArg(&argument, "compressionParameters") || longCommandWArg(&argument, "cParams")) {
2772                     for(v = 0; v <= strt_ind; v++) {
2773                         g_silenceParams[v] = 0;
2774                     }
2775                     found = 1;
2776                 }
2777
2778
2779                 if(found) {
2780                     if(argument[0]==',') {
2781                         continue;
2782                     } else {
2783                         break;
2784                     }
2785                 }
2786                 DISPLAY("invalid parameter name parameter \n");
2787                 return 1;
2788             }
2789
2790             if (argument[0] != 0) {
2791                 DISPLAY("invalid --display format\n");
2792                 return 1; /* check the end of string */
2793             }
2794             continue;
2795         } else if (argument[0]=='-') {
2796             argument++;
2797
2798             while (argument[0]!=0) {
2799
2800                 switch(argument[0])
2801                 {
2802                     /* Display help on usage */
2803                 case 'h' :
2804                 case 'H': usage(exename); usage_advanced(); return 0;
2805
2806                     /* Pause at the end (hidden option) */
2807                 case 'p': main_pause = 1; argument++; break;
2808
2809                     /* Sample compressibility (when no file provided) */
2810                 case 'P':
2811                     argument++;
2812                     {   U32 const proba32 = readU32FromChar(&argument);
2813                         compressibility = (double)proba32 / 100.;
2814                     }
2815                     break;
2816
2817                     /* Run Single conf */
2818                 case 'S':
2819                     g_singleRun = 1;
2820                     argument++;
2821                     for ( ; ; ) {
2822                         switch(*argument)
2823                         {
2824                         case 'w':
2825                             argument++;
2826                             g_params.vals[wlog_ind] = readU32FromChar(&argument);
2827                             continue;
2828                         case 'c':
2829                             argument++;
2830                             g_params.vals[clog_ind] = readU32FromChar(&argument);
2831                             continue;
2832                         case 'h':
2833                             argument++;
2834                             g_params.vals[hlog_ind] = readU32FromChar(&argument);
2835                             continue;
2836                         case 's':
2837                             argument++;
2838                             g_params.vals[slog_ind] = readU32FromChar(&argument);
2839                             continue;
2840                         case 'l':  /* search length */
2841                             argument++;
2842                             g_params.vals[mml_ind] = readU32FromChar(&argument);
2843                             continue;
2844                         case 't':  /* target length */
2845                             argument++;
2846                             g_params.vals[tlen_ind] = readU32FromChar(&argument);
2847                             continue;
2848                         case 'S':  /* strategy */
2849                             argument++;
2850                             g_params.vals[strt_ind] = readU32FromChar(&argument);
2851                             continue;
2852                         case 'f':  /* forceAttachDict */
2853                             argument++;
2854                             g_params.vals[fadt_ind] = readU32FromChar(&argument);
2855                             continue;
2856                         case 'L':
2857                             {   argument++;
2858                                 cLevelRun = readU32FromChar(&argument);
2859                                 g_params = emptyParams();
2860                                 continue;
2861                             }
2862                         default : ;
2863                         }
2864                         break;
2865                     }
2866
2867                     break;
2868
2869                     /* target level1 speed objective, in MB/s */
2870                 case 'T':
2871                     argument++;
2872                     g_target = readU32FromChar(&argument);
2873                     break;
2874
2875                     /* cut input into blocks */
2876                 case 'B':
2877                     argument++;
2878                     g_blockSize = readU32FromChar(&argument);
2879                     DISPLAY("using %u KB block size \n", (unsigned)(g_blockSize>>10));
2880                     break;
2881
2882                     /* caps runtime (in seconds) */
2883                 case 't':
2884                     argument++;
2885                     g_timeLimit_s = readU32FromChar(&argument);
2886                     break;
2887
2888                 case 's':
2889                     argument++;
2890                     seperateFiles = 1;
2891                     break;
2892
2893                 case 'q':
2894                     while (argument[0] == 'q') { argument++; g_displayLevel--; }
2895                     break;
2896
2897                 case 'v':
2898                     while (argument[0] == 'v') { argument++; g_displayLevel++; }
2899                     break;
2900
2901                 /* load dictionary file (only applicable for optimizer rn) */
2902                 case 'D':
2903                     if(i == argc - 1) { /* last argument, return error. */
2904                         DISPLAY("Dictionary file expected but not given : %d\n", i);
2905                         return 1;
2906                     } else {
2907                         i++;
2908                         dictFileName = argv[i];
2909                         argument += strlen(argument);
2910                     }
2911                     break;
2912
2913                     /* Unknown command */
2914                 default : return badusage(exename);
2915                 }
2916             }
2917             continue;
2918         }   /* if (argument[0]=='-') */
2919
2920         /* first provided filename is input */
2921         if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
2922     }
2923
2924     /* Welcome message */
2925     DISPLAYLEVEL(2, WELCOME_MESSAGE);
2926
2927     if (filenamesStart==0) {
2928         if (g_optimizer) {
2929             DISPLAY("Optimizer Expects File\n");
2930             return 1;
2931         } else {
2932             result = benchSample(compressibility, cLevelRun);
2933         }
2934     } else {
2935         if(seperateFiles) {
2936             for(i = 0; i < argc - filenamesStart; i++) {
2937                 if (g_optimizer) {
2938                     result = optimizeForSize(argv+filenamesStart + i, 1, dictFileName, target, paramTarget, cLevelOpt, cLevelRun, memoTableLog);
2939                     if(result) { DISPLAY("Error on File %d", i); return result; }
2940                 } else {
2941                     result = benchFiles(argv+filenamesStart + i, 1, dictFileName, cLevelRun);
2942                     if(result) { DISPLAY("Error on File %d", i); return result; }
2943                 }
2944             }
2945         } else {
2946             if (g_optimizer) {
2947                 result = optimizeForSize(argv+filenamesStart, argc-filenamesStart, dictFileName, target, paramTarget, cLevelOpt, cLevelRun, memoTableLog);
2948             } else {
2949                 result = benchFiles(argv+filenamesStart, argc-filenamesStart, dictFileName, cLevelRun);
2950             }
2951         }
2952     }
2953
2954     if (main_pause) { int unused; printf("press enter...\n"); unused = getchar(); (void)unused; }
2955
2956     return result;
2957 }