benchmarks: remove more boilerplate.
Many of our benchmarks are basically just "call one function with a
fixed argument". We don't need to keep repeating all the boilerplate for
that.
This also ensures we don't forget the benchmark::DoNotOptimize, which --
in addition to being a good idea in general -- specifically solves the
problem with gettid benchmark and provides a more accurate result by
removing the indirection through a function pointer.
Test: ran benchmarks
Change-Id: Id67535243678cd0d48f51cf25141e2040da9af03
diff --git a/benchmarks/unistd_benchmark.cpp b/benchmarks/unistd_benchmark.cpp
index 98e8858..d697dfd 100644
--- a/benchmarks/unistd_benchmark.cpp
+++ b/benchmarks/unistd_benchmark.cpp
@@ -20,37 +20,11 @@
#include <benchmark/benchmark.h>
#include "util.h"
-static void BM_unistd_getpid(benchmark::State& state) {
- while (state.KeepRunning()) {
- getpid();
- }
-}
-BIONIC_BENCHMARK(BM_unistd_getpid);
+BIONIC_TRIVIAL_BENCHMARK(BM_unistd_getpid, getpid());
+BIONIC_TRIVIAL_BENCHMARK(BM_unistd_getpid_syscall, syscall(__NR_getpid));
-static void BM_unistd_getpid_syscall(benchmark::State& state) {
- while (state.KeepRunning()) {
- syscall(__NR_getpid);
- }
-}
-BIONIC_BENCHMARK(BM_unistd_getpid_syscall);
-
+// TODO: glibc 2.30 added gettid() too.
#if defined(__BIONIC__)
-
-// Stop GCC optimizing out our pure function.
-/* Must not be static! */ pid_t (*gettid_fp)() = gettid;
-
-static void BM_unistd_gettid(benchmark::State& state) {
- while (state.KeepRunning()) {
- gettid_fp();
- }
-}
-BIONIC_BENCHMARK(BM_unistd_gettid);
-
+BIONIC_TRIVIAL_BENCHMARK(BM_unistd_gettid, gettid());
#endif
-
-void BM_unistd_gettid_syscall(benchmark::State& state) {
- while (state.KeepRunning()) {
- syscall(__NR_gettid);
- }
-}
-BIONIC_BENCHMARK(BM_unistd_gettid_syscall);
+BIONIC_TRIVIAL_BENCHMARK(BM_unistd_gettid_syscall, syscall(__NR_gettid));