xref: /freebsd/sys/contrib/zstd/programs/benchfn.h (revision 5ff13fbc199bdf5f0572845351c68ee5ca828e71)
1a0483764SConrad Meyer /*
2*5ff13fbcSAllan Jude  * Copyright (c) Yann Collet, Facebook, Inc.
3a0483764SConrad Meyer  * All rights reserved.
4a0483764SConrad Meyer  *
5a0483764SConrad Meyer  * This source code is licensed under both the BSD-style license (found in the
6a0483764SConrad Meyer  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7a0483764SConrad Meyer  * in the COPYING file in the root directory of this source tree).
8a0483764SConrad Meyer  * You may select, at your option, one of the above-listed licenses.
9a0483764SConrad Meyer  */
10a0483764SConrad Meyer 
11a0483764SConrad Meyer 
12a0483764SConrad Meyer /* benchfn :
13a0483764SConrad Meyer  * benchmark any function on a set of input
14a0483764SConrad Meyer  * providing result in nanoSecPerRun
15a0483764SConrad Meyer  * or detecting and returning an error
16a0483764SConrad Meyer  */
17a0483764SConrad Meyer 
18a0483764SConrad Meyer #if defined (__cplusplus)
19a0483764SConrad Meyer extern "C" {
20a0483764SConrad Meyer #endif
21a0483764SConrad Meyer 
22a0483764SConrad Meyer #ifndef BENCH_FN_H_23876
23a0483764SConrad Meyer #define BENCH_FN_H_23876
24a0483764SConrad Meyer 
25a0483764SConrad Meyer /* ===  Dependencies  === */
26a0483764SConrad Meyer #include <stddef.h>   /* size_t */
27a0483764SConrad Meyer 
28a0483764SConrad Meyer 
29a0483764SConrad Meyer /* ====  Benchmark any function, iterated on a set of blocks  ==== */
30a0483764SConrad Meyer 
31a0483764SConrad Meyer /* BMK_runTime_t: valid result return type */
32a0483764SConrad Meyer 
33a0483764SConrad Meyer typedef struct {
342b9c00cbSConrad Meyer     double nanoSecPerRun;  /* time per iteration (over all blocks) */
35a0483764SConrad Meyer     size_t sumOfReturn;         /* sum of return values */
36a0483764SConrad Meyer } BMK_runTime_t;
37a0483764SConrad Meyer 
38a0483764SConrad Meyer 
39a0483764SConrad Meyer /* BMK_runOutcome_t:
40a0483764SConrad Meyer  * type expressing the outcome of a benchmark run by BMK_benchFunction(),
41a0483764SConrad Meyer  * which can be either valid or invalid.
42a0483764SConrad Meyer  * benchmark outcome can be invalid if errorFn is provided.
43a0483764SConrad Meyer  * BMK_runOutcome_t must be considered "opaque" : never access its members directly.
44a0483764SConrad Meyer  * Instead, use its assigned methods :
45a0483764SConrad Meyer  * BMK_isSuccessful_runOutcome, BMK_extract_runTime, BMK_extract_errorResult.
46a0483764SConrad Meyer  * The structure is only described here to allow its allocation on stack. */
47a0483764SConrad Meyer 
48a0483764SConrad Meyer typedef struct {
49a0483764SConrad Meyer     BMK_runTime_t internal_never_ever_use_directly;
50a0483764SConrad Meyer     size_t error_result_never_ever_use_directly;
51a0483764SConrad Meyer     int error_tag_never_ever_use_directly;
52a0483764SConrad Meyer } BMK_runOutcome_t;
53a0483764SConrad Meyer 
54a0483764SConrad Meyer 
55a0483764SConrad Meyer /* prototypes for benchmarked functions */
56a0483764SConrad Meyer typedef size_t (*BMK_benchFn_t)(const void* src, size_t srcSize, void* dst, size_t dstCapacity, void* customPayload);
57a0483764SConrad Meyer typedef size_t (*BMK_initFn_t)(void* initPayload);
58a0483764SConrad Meyer typedef unsigned (*BMK_errorFn_t)(size_t);
59a0483764SConrad Meyer 
60a0483764SConrad Meyer 
612b9c00cbSConrad Meyer /* BMK_benchFunction() parameters are provided via the following structure.
622b9c00cbSConrad Meyer  * A structure is preferable for readability,
632b9c00cbSConrad Meyer  * as the number of parameters required is fairly large.
64a0483764SConrad Meyer  * No initializer is provided, because it doesn't make sense to provide some "default" :
652b9c00cbSConrad Meyer  * all parameters must be specified by the caller.
662b9c00cbSConrad Meyer  * optional parameters are labelled explicitly, and accept value NULL when not used */
67a0483764SConrad Meyer typedef struct {
68a0483764SConrad Meyer     BMK_benchFn_t benchFn;    /* the function to benchmark, over the set of blocks */
69a0483764SConrad Meyer     void* benchPayload;       /* pass custom parameters to benchFn  :
70a0483764SConrad Meyer                                * (*benchFn)(srcBuffers[i], srcSizes[i], dstBuffers[i], dstCapacities[i], benchPayload) */
71a0483764SConrad Meyer     BMK_initFn_t initFn;      /* (*initFn)(initPayload) is run once per run, at the beginning. */
72a0483764SConrad Meyer     void* initPayload;        /* Both arguments can be NULL, in which case nothing is run. */
73a0483764SConrad Meyer     BMK_errorFn_t errorFn;    /* errorFn will check each return value of benchFn over each block, to determine if it failed or not.
74a0483764SConrad Meyer                                * errorFn can be NULL, in which case no check is performed.
75a0483764SConrad Meyer                                * errorFn must return 0 when benchFn was successful, and >= 1 if it detects an error.
76a0483764SConrad Meyer                                * Execution is stopped as soon as an error is detected.
77a0483764SConrad Meyer                                * the triggering return value can be retrieved using BMK_extract_errorResult(). */
78a0483764SConrad Meyer     size_t blockCount;        /* number of blocks to operate benchFn on.
79a0483764SConrad Meyer                                * It's also the size of all array parameters :
80a0483764SConrad Meyer                                * srcBuffers, srcSizes, dstBuffers, dstCapacities, blockResults */
812b9c00cbSConrad Meyer     const void *const * srcBuffers; /* read-only array of buffers to be operated on by benchFn */
822b9c00cbSConrad Meyer     const size_t* srcSizes;   /* read-only array containing sizes of srcBuffers */
832b9c00cbSConrad Meyer     void *const * dstBuffers; /* array of buffers to be written into by benchFn. This array is not optional, it must be provided even if unused by benchfn. */
842b9c00cbSConrad Meyer     const size_t* dstCapacities; /* read-only array containing capacities of dstBuffers. This array must be present. */
85a0483764SConrad Meyer     size_t* blockResults;     /* Optional: store the return value of benchFn for each block. Use NULL if this result is not requested. */
86a0483764SConrad Meyer } BMK_benchParams_t;
87a0483764SConrad Meyer 
88a0483764SConrad Meyer 
89a0483764SConrad Meyer /* BMK_benchFunction() :
90a0483764SConrad Meyer  * This function benchmarks benchFn and initFn, providing a result.
91a0483764SConrad Meyer  *
92a0483764SConrad Meyer  * params : see description of BMK_benchParams_t above.
93a0483764SConrad Meyer  * nbLoops: defines number of times benchFn is run over the full set of blocks.
94a0483764SConrad Meyer  *          Minimum value is 1. A 0 is interpreted as a 1.
95a0483764SConrad Meyer  *
96a0483764SConrad Meyer  * @return: can express either an error or a successful result.
97a0483764SConrad Meyer  *          Use BMK_isSuccessful_runOutcome() to check if benchmark was successful.
98a0483764SConrad Meyer  *          If yes, extract the result with BMK_extract_runTime(),
99a0483764SConrad Meyer  *          it will contain :
100a0483764SConrad Meyer  *              .sumOfReturn : the sum of all return values of benchFn through all of blocks
101a0483764SConrad Meyer  *              .nanoSecPerRun : time per run of benchFn + (time for initFn / nbLoops)
102a0483764SConrad Meyer  *          .sumOfReturn is generally intended for functions which return a # of bytes written into dstBuffer,
103a0483764SConrad Meyer  *              in which case, this value will be the total amount of bytes written into dstBuffer.
104a0483764SConrad Meyer  *
105a0483764SConrad Meyer  * blockResults : when provided (!= NULL), and when benchmark is successful,
106a0483764SConrad Meyer  *                params.blockResults contains all return values of `benchFn` over all blocks.
107a0483764SConrad Meyer  *                when provided (!= NULL), and when benchmark failed,
108a0483764SConrad Meyer  *                params.blockResults contains return values of `benchFn` over all blocks preceding and including the failed block.
109a0483764SConrad Meyer  */
110a0483764SConrad Meyer BMK_runOutcome_t BMK_benchFunction(BMK_benchParams_t params, unsigned nbLoops);
111a0483764SConrad Meyer 
112a0483764SConrad Meyer 
113a0483764SConrad Meyer 
114a0483764SConrad Meyer /* check first if the benchmark was successful or not */
115a0483764SConrad Meyer int BMK_isSuccessful_runOutcome(BMK_runOutcome_t outcome);
116a0483764SConrad Meyer 
117a0483764SConrad Meyer /* If the benchmark was successful, extract the result.
118a0483764SConrad Meyer  * note : this function will abort() program execution if benchmark failed !
119a0483764SConrad Meyer  *        always check if benchmark was successful first !
120a0483764SConrad Meyer  */
121a0483764SConrad Meyer BMK_runTime_t BMK_extract_runTime(BMK_runOutcome_t outcome);
122a0483764SConrad Meyer 
123a0483764SConrad Meyer /* when benchmark failed, it means one invocation of `benchFn` failed.
124a0483764SConrad Meyer  * The failure was detected by `errorFn`, operating on return values of `benchFn`.
125a0483764SConrad Meyer  * Returns the faulty return value.
126a0483764SConrad Meyer  * note : this function will abort() program execution if benchmark did not failed.
127a0483764SConrad Meyer  *        always check if benchmark failed first !
128a0483764SConrad Meyer  */
129a0483764SConrad Meyer size_t BMK_extract_errorResult(BMK_runOutcome_t outcome);
130a0483764SConrad Meyer 
131a0483764SConrad Meyer 
132a0483764SConrad Meyer 
133a0483764SConrad Meyer /* ====  Benchmark any function, returning intermediate results  ==== */
134a0483764SConrad Meyer 
135a0483764SConrad Meyer /* state information tracking benchmark session */
136a0483764SConrad Meyer typedef struct BMK_timedFnState_s BMK_timedFnState_t;
137a0483764SConrad Meyer 
138a0483764SConrad Meyer /* BMK_benchTimedFn() :
139a0483764SConrad Meyer  * Similar to BMK_benchFunction(), most arguments being identical.
140a0483764SConrad Meyer  * Automatically determines `nbLoops` so that each result is regularly produced at interval of about run_ms.
141a0483764SConrad Meyer  * Note : minimum `nbLoops` is 1, therefore a run may last more than run_ms, and possibly even more than total_ms.
142a0483764SConrad Meyer  * Usage - initialize timedFnState, select benchmark duration (total_ms) and each measurement duration (run_ms)
143a0483764SConrad Meyer  *         call BMK_benchTimedFn() repetitively, each measurement is supposed to last about run_ms
144a0483764SConrad Meyer  *         Check if total time budget is spent or exceeded, using BMK_isCompleted_TimedFn()
145a0483764SConrad Meyer  */
146a0483764SConrad Meyer BMK_runOutcome_t BMK_benchTimedFn(BMK_timedFnState_t* timedFnState,
147a0483764SConrad Meyer                                   BMK_benchParams_t params);
148a0483764SConrad Meyer 
149a0483764SConrad Meyer /* Tells if duration of all benchmark runs has exceeded total_ms
150a0483764SConrad Meyer  */
151a0483764SConrad Meyer int BMK_isCompleted_TimedFn(const BMK_timedFnState_t* timedFnState);
152a0483764SConrad Meyer 
153a0483764SConrad Meyer /* BMK_createTimedFnState() and BMK_resetTimedFnState() :
154a0483764SConrad Meyer  * Create/Set BMK_timedFnState_t for next benchmark session,
155a0483764SConrad Meyer  * which shall last a minimum of total_ms milliseconds,
156a0483764SConrad Meyer  * producing intermediate results, paced at interval of (approximately) run_ms.
157a0483764SConrad Meyer  */
158a0483764SConrad Meyer BMK_timedFnState_t* BMK_createTimedFnState(unsigned total_ms, unsigned run_ms);
159a0483764SConrad Meyer void BMK_resetTimedFnState(BMK_timedFnState_t* timedFnState, unsigned total_ms, unsigned run_ms);
160a0483764SConrad Meyer void BMK_freeTimedFnState(BMK_timedFnState_t* state);
161a0483764SConrad Meyer 
162a0483764SConrad Meyer 
1632b9c00cbSConrad Meyer /* BMK_timedFnState_shell and BMK_initStatic_timedFnState() :
1642b9c00cbSConrad Meyer  * Makes it possible to statically allocate a BMK_timedFnState_t on stack.
1652b9c00cbSConrad Meyer  * BMK_timedFnState_shell is only there to allocate space,
1662b9c00cbSConrad Meyer  * never ever access its members.
1672b9c00cbSConrad Meyer  * BMK_timedFnState_t() actually accepts any buffer.
1682b9c00cbSConrad Meyer  * It will check if provided buffer is large enough and is correctly aligned,
1692b9c00cbSConrad Meyer  * and will return NULL if conditions are not respected.
1702b9c00cbSConrad Meyer  */
1712b9c00cbSConrad Meyer #define BMK_TIMEDFNSTATE_SIZE 64
1722b9c00cbSConrad Meyer typedef union {
1732b9c00cbSConrad Meyer     char never_access_space[BMK_TIMEDFNSTATE_SIZE];
1742b9c00cbSConrad Meyer     long long alignment_enforcer;  /* must be aligned on 8-bytes boundaries */
1752b9c00cbSConrad Meyer } BMK_timedFnState_shell;
1762b9c00cbSConrad Meyer BMK_timedFnState_t* BMK_initStatic_timedFnState(void* buffer, size_t size, unsigned total_ms, unsigned run_ms);
1772b9c00cbSConrad Meyer 
178a0483764SConrad Meyer 
179a0483764SConrad Meyer #endif   /* BENCH_FN_H_23876 */
180a0483764SConrad Meyer 
181a0483764SConrad Meyer #if defined (__cplusplus)
182a0483764SConrad Meyer }
183a0483764SConrad Meyer #endif
184