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