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