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