benchfn.h 8.4 KB

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