More benchmarks.
Add a hand-rolled maps line parser as something to compare our realistic
sscanf benchmark against. Also add benchmarks for the ato*/strto* family.
This patch doesn't fix the tests, which seem to have been broken by
the recent google-benchmark upgrade despite the benchmarks themselves
all working just fine. To me that's a final strike against these tests
which are hard to maintain and not obviously useful, but we can worry
about what to do with them -- whether to just delete them or to try to
turn them into tests that actually have some value -- in a separate CL.
Bug: N/A
Test: ran benchmarks
Change-Id: I6c9a77ece98d624baeb049b360876ef5c35ea7f2
diff --git a/benchmarks/Android.bp b/benchmarks/Android.bp
index c7c8a29..fd3d85a 100644
--- a/benchmarks/Android.bp
+++ b/benchmarks/Android.bp
@@ -22,11 +22,13 @@
"-Wall",
"-Wextra",
"-Werror",
+ "-Wno-gcc-compat",
"-Wunused",
],
srcs: [
"bionic_benchmarks.cpp",
"atomic_benchmark.cpp",
+ "inttypes_benchmark.cpp",
"math_benchmark.cpp",
"property_benchmark.cpp",
"pthread_benchmark.cpp",
diff --git a/benchmarks/inttypes_benchmark.cpp b/benchmarks/inttypes_benchmark.cpp
new file mode 100644
index 0000000..f123eb8
--- /dev/null
+++ b/benchmarks/inttypes_benchmark.cpp
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <inttypes.h>
+
+#include <benchmark/benchmark.h>
+#include "util.h"
+
+void BM_inttypes_strtoimax(benchmark::State& state) {
+ while (state.KeepRunning()) {
+ strtoimax(" -123", nullptr, 0);
+ }
+}
+BIONIC_BENCHMARK(BM_inttypes_strtoimax);
+
+void BM_inttypes_strtoumax(benchmark::State& state) {
+ while (state.KeepRunning()) {
+ strtoumax(" -123", nullptr, 0);
+ }
+}
+BIONIC_BENCHMARK(BM_inttypes_strtoumax);
diff --git a/benchmarks/stdio_benchmark.cpp b/benchmarks/stdio_benchmark.cpp
index 872725d..965a3ae 100644
--- a/benchmarks/stdio_benchmark.cpp
+++ b/benchmarks/stdio_benchmark.cpp
@@ -223,6 +223,7 @@
}
BIONIC_BENCHMARK(BM_stdio_scanf_d);
+// Parsing maps is a common use of sscanf with a relatively complex format string.
static void BM_stdio_scanf_maps(benchmark::State& state) {
while (state.KeepRunning()) {
uintptr_t start;
@@ -236,3 +237,71 @@
}
}
BIONIC_BENCHMARK(BM_stdio_scanf_maps);
+
+// Hard-coded equivalent of the maps sscanf from libunwindstack/Maps.cpp for a baseline.
+static int ParseMap(const char* line, const char* /*fmt*/, uintptr_t* start, uintptr_t* end,
+ char* permissions, uintptr_t* offset, int* name_pos) __attribute__((noinline)) {
+ char* str;
+ const char* old_str = line;
+
+ // "%" PRIxPTR "-"
+ *start = strtoul(old_str, &str, 16);
+ if (old_str == str || *str++ != '-') return 0;
+
+ // "%" PRIxPTR " "
+ old_str = str;
+ *end = strtoul(old_str, &str, 16);
+ if (old_str == str || !std::isspace(*str++)) return 0;
+ while (std::isspace(*str)) str++;
+
+ // "%4s "
+ if (*str == '\0') return 0;
+ permissions[0] = *str;
+ str++;
+ permissions[1] = *str;
+ str++;
+ permissions[2] = *str;
+ str++;
+ permissions[3] = *str;
+ str++;
+ permissions[4] = 0;
+ if (!std::isspace(*str++)) return 0;
+
+ // "%" PRIxPTR " "
+ old_str = str;
+ *offset = strtoul(old_str, &str, 16);
+ if (old_str == str || !std::isspace(*str)) return 0;
+
+ // "%*x:%*x "
+ old_str = str;
+ (void)strtoul(old_str, &str, 16);
+ if (old_str == str || *str++ != ':') return 0;
+ if (std::isspace(*str)) return 0;
+ old_str = str;
+ (void)strtoul(str, &str, 16);
+ if (old_str == str || !std::isspace(*str++)) return 0;
+
+ // "%*d "
+ old_str = str;
+ (void)strtoul(old_str, &str, 10);
+ if (old_str == str || (!std::isspace(*str) && *str != '\0')) return 0;
+ while (std::isspace(*str)) str++;
+
+ // "%n"
+ *name_pos = (str - line);
+ return 4;
+}
+
+static void BM_stdio_scanf_maps_baseline(benchmark::State& state) {
+ while (state.KeepRunning()) {
+ uintptr_t start;
+ uintptr_t end;
+ uintptr_t offset;
+ char permissions[5];
+ int name_pos;
+ if (ParseMap("6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so",
+ "%" PRIxPTR "-%" PRIxPTR " %4s %" PRIxPTR " %*x:%*x %*d %n",
+ &start, &end, permissions, &offset, &name_pos) != 4) abort();
+ }
+}
+BIONIC_BENCHMARK(BM_stdio_scanf_maps_baseline);
diff --git a/benchmarks/stdlib_benchmark.cpp b/benchmarks/stdlib_benchmark.cpp
index 0bee683..24773de 100644
--- a/benchmarks/stdlib_benchmark.cpp
+++ b/benchmarks/stdlib_benchmark.cpp
@@ -118,3 +118,45 @@
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(500000));
}
BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc, "0");
+
+void BM_stdlib_atoi(benchmark::State& state) {
+ while (state.KeepRunning()) {
+ benchmark::DoNotOptimize(atoi(" -123"));
+ }
+}
+BIONIC_BENCHMARK(BM_stdlib_atoi);
+
+void BM_stdlib_atol(benchmark::State& state) {
+ while (state.KeepRunning()) {
+ benchmark::DoNotOptimize(atol(" -123"));
+ }
+}
+BIONIC_BENCHMARK(BM_stdlib_atol);
+
+void BM_stdlib_strtol(benchmark::State& state) {
+ while (state.KeepRunning()) {
+ benchmark::DoNotOptimize(strtol(" -123", nullptr, 0));
+ }
+}
+BIONIC_BENCHMARK(BM_stdlib_strtol);
+
+void BM_stdlib_strtoll(benchmark::State& state) {
+ while (state.KeepRunning()) {
+ benchmark::DoNotOptimize(strtoll(" -123", nullptr, 0));
+ }
+}
+BIONIC_BENCHMARK(BM_stdlib_strtoll);
+
+void BM_stdlib_strtoul(benchmark::State& state) {
+ while (state.KeepRunning()) {
+ benchmark::DoNotOptimize(strtoul(" -123", nullptr, 0));
+ }
+}
+BIONIC_BENCHMARK(BM_stdlib_strtoul);
+
+void BM_stdlib_strtoull(benchmark::State& state) {
+ while (state.KeepRunning()) {
+ benchmark::DoNotOptimize(strtoull(" -123", nullptr, 0));
+ }
+}
+BIONIC_BENCHMARK(BM_stdlib_strtoull);