xref: /xnu-10002.81.5/tests/benchmark/helpers.h (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions #ifndef BENCHMARK_PERF_HELPERS_H
2*5e3eaea3SApple OSS Distributions #define BENCHMARK_PERF_HELPERS_H
3*5e3eaea3SApple OSS Distributions 
4*5e3eaea3SApple OSS Distributions /*
5*5e3eaea3SApple OSS Distributions  * Utility functions and constants used by perf tests.
6*5e3eaea3SApple OSS Distributions  */
7*5e3eaea3SApple OSS Distributions #include <inttypes.h>
8*5e3eaea3SApple OSS Distributions #include <time.h>
9*5e3eaea3SApple OSS Distributions #include <stdbool.h>
10*5e3eaea3SApple OSS Distributions 
11*5e3eaea3SApple OSS Distributions /*
12*5e3eaea3SApple OSS Distributions  * mmap an anonymous chunk of memory.
13*5e3eaea3SApple OSS Distributions  */
14*5e3eaea3SApple OSS Distributions unsigned char *mmap_buffer(size_t size);
15*5e3eaea3SApple OSS Distributions /*
16*5e3eaea3SApple OSS Distributions  * Returns a - b in microseconds.
17*5e3eaea3SApple OSS Distributions  * NB: a must be >= b
18*5e3eaea3SApple OSS Distributions  */
19*5e3eaea3SApple OSS Distributions uint64_t timespec_difference_us(const struct timespec* a, const struct timespec* b);
20*5e3eaea3SApple OSS Distributions /*
21*5e3eaea3SApple OSS Distributions  * Print the message to stdout along with the current time.
22*5e3eaea3SApple OSS Distributions  * Also flushes stdout so that the log can help detect hangs. Don't call
23*5e3eaea3SApple OSS Distributions  * this function from within the measured portion of the benchmark as it will
24*5e3eaea3SApple OSS Distributions  * pollute your measurement.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * NB: Will only log if verbose == true.
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions void benchmark_log(bool verbose, const char *restrict fmt, ...) __attribute__((format(printf, 2, 3)));
29*5e3eaea3SApple OSS Distributions 
30*5e3eaea3SApple OSS Distributions static const uint64_t kNumMicrosecondsInSecond = 1000UL * 1000;
31*5e3eaea3SApple OSS Distributions static const uint64_t kNumNanosecondsInMicrosecond = 1000UL;
32*5e3eaea3SApple OSS Distributions static const uint64_t kNumNanosecondsInSecond = kNumNanosecondsInMicrosecond * kNumMicrosecondsInSecond;
33*5e3eaea3SApple OSS Distributions /* Get a (wall-time) timestamp in nanoseconds */
34*5e3eaea3SApple OSS Distributions #define current_timestamp_ns() (clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW));
35*5e3eaea3SApple OSS Distributions 
36*5e3eaea3SApple OSS Distributions unsigned int get_ncpu(void);
37*5e3eaea3SApple OSS Distributions 
38*5e3eaea3SApple OSS Distributions #endif /* !defined(BENCHMARK_PERF_HELPERS_H) */
39