benchfn.h 8.5 KB

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