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