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