1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2014 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker *
4*8d67ca89SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8d67ca89SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8d67ca89SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8d67ca89SAndroid Build Coastguard Worker *
8*8d67ca89SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8d67ca89SAndroid Build Coastguard Worker *
10*8d67ca89SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8d67ca89SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8d67ca89SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8d67ca89SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8d67ca89SAndroid Build Coastguard Worker * limitations under the License.
15*8d67ca89SAndroid Build Coastguard Worker */
16*8d67ca89SAndroid Build Coastguard Worker
17*8d67ca89SAndroid Build Coastguard Worker #include <err.h>
18*8d67ca89SAndroid Build Coastguard Worker #include <inttypes.h>
19*8d67ca89SAndroid Build Coastguard Worker #include <stdio.h>
20*8d67ca89SAndroid Build Coastguard Worker #include <stdio_ext.h>
21*8d67ca89SAndroid Build Coastguard Worker #include <stdlib.h>
22*8d67ca89SAndroid Build Coastguard Worker
23*8d67ca89SAndroid Build Coastguard Worker #include <android-base/file.h>
24*8d67ca89SAndroid Build Coastguard Worker #include <benchmark/benchmark.h>
25*8d67ca89SAndroid Build Coastguard Worker #include "util.h"
26*8d67ca89SAndroid Build Coastguard Worker
FillFile(TemporaryFile & tf)27*8d67ca89SAndroid Build Coastguard Worker static void FillFile(TemporaryFile& tf) {
28*8d67ca89SAndroid Build Coastguard Worker char line[256];
29*8d67ca89SAndroid Build Coastguard Worker memset(line, 'x', sizeof(line));
30*8d67ca89SAndroid Build Coastguard Worker line[sizeof(line) - 1] = '\0';
31*8d67ca89SAndroid Build Coastguard Worker
32*8d67ca89SAndroid Build Coastguard Worker FILE* fp = fopen(tf.path, "we");
33*8d67ca89SAndroid Build Coastguard Worker for (size_t i = 0; i < 4096; ++i) fputs(line, fp);
34*8d67ca89SAndroid Build Coastguard Worker fclose(fp);
35*8d67ca89SAndroid Build Coastguard Worker }
36*8d67ca89SAndroid Build Coastguard Worker
37*8d67ca89SAndroid Build Coastguard Worker template <typename Fn>
ReadWriteTest(benchmark::State & state,Fn f,bool buffered)38*8d67ca89SAndroid Build Coastguard Worker void ReadWriteTest(benchmark::State& state, Fn f, bool buffered) {
39*8d67ca89SAndroid Build Coastguard Worker size_t chunk_size = state.range(0);
40*8d67ca89SAndroid Build Coastguard Worker
41*8d67ca89SAndroid Build Coastguard Worker FILE* fp = fopen("/dev/zero", "r+e");
42*8d67ca89SAndroid Build Coastguard Worker __fsetlocking(fp, FSETLOCKING_BYCALLER);
43*8d67ca89SAndroid Build Coastguard Worker char* buf = new char[chunk_size];
44*8d67ca89SAndroid Build Coastguard Worker
45*8d67ca89SAndroid Build Coastguard Worker if (!buffered) {
46*8d67ca89SAndroid Build Coastguard Worker setvbuf(fp, nullptr, _IONBF, 0);
47*8d67ca89SAndroid Build Coastguard Worker }
48*8d67ca89SAndroid Build Coastguard Worker
49*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
50*8d67ca89SAndroid Build Coastguard Worker if (f(buf, chunk_size, 1, fp) != 1) {
51*8d67ca89SAndroid Build Coastguard Worker errx(1, "ERROR: op of %zu bytes failed.", chunk_size);
52*8d67ca89SAndroid Build Coastguard Worker }
53*8d67ca89SAndroid Build Coastguard Worker }
54*8d67ca89SAndroid Build Coastguard Worker
55*8d67ca89SAndroid Build Coastguard Worker state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(chunk_size));
56*8d67ca89SAndroid Build Coastguard Worker delete[] buf;
57*8d67ca89SAndroid Build Coastguard Worker fclose(fp);
58*8d67ca89SAndroid Build Coastguard Worker }
59*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_fread(benchmark::State & state)60*8d67ca89SAndroid Build Coastguard Worker void BM_stdio_fread(benchmark::State& state) {
61*8d67ca89SAndroid Build Coastguard Worker ReadWriteTest(state, fread, true);
62*8d67ca89SAndroid Build Coastguard Worker }
63*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK_WITH_ARG(BM_stdio_fread, "AT_COMMON_SIZES");
64*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_fwrite(benchmark::State & state)65*8d67ca89SAndroid Build Coastguard Worker void BM_stdio_fwrite(benchmark::State& state) {
66*8d67ca89SAndroid Build Coastguard Worker ReadWriteTest(state, fwrite, true);
67*8d67ca89SAndroid Build Coastguard Worker }
68*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK_WITH_ARG(BM_stdio_fwrite, "AT_COMMON_SIZES");
69*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_fread_unbuffered(benchmark::State & state)70*8d67ca89SAndroid Build Coastguard Worker void BM_stdio_fread_unbuffered(benchmark::State& state) {
71*8d67ca89SAndroid Build Coastguard Worker ReadWriteTest(state, fread, false);
72*8d67ca89SAndroid Build Coastguard Worker }
73*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK_WITH_ARG(BM_stdio_fread_unbuffered, "AT_COMMON_SIZES");
74*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_fwrite_unbuffered(benchmark::State & state)75*8d67ca89SAndroid Build Coastguard Worker void BM_stdio_fwrite_unbuffered(benchmark::State& state) {
76*8d67ca89SAndroid Build Coastguard Worker ReadWriteTest(state, fwrite, false);
77*8d67ca89SAndroid Build Coastguard Worker }
78*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK_WITH_ARG(BM_stdio_fwrite_unbuffered, "AT_COMMON_SIZES");
79*8d67ca89SAndroid Build Coastguard Worker
80*8d67ca89SAndroid Build Coastguard Worker #if !defined(__GLIBC__)
FopenFgetlnFclose(benchmark::State & state,bool no_locking)81*8d67ca89SAndroid Build Coastguard Worker static void FopenFgetlnFclose(benchmark::State& state, bool no_locking) {
82*8d67ca89SAndroid Build Coastguard Worker TemporaryFile tf;
83*8d67ca89SAndroid Build Coastguard Worker FillFile(tf);
84*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
85*8d67ca89SAndroid Build Coastguard Worker FILE* fp = fopen(tf.path, "re");
86*8d67ca89SAndroid Build Coastguard Worker if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
87*8d67ca89SAndroid Build Coastguard Worker size_t length;
88*8d67ca89SAndroid Build Coastguard Worker while (fgetln(fp, &length) != nullptr) {
89*8d67ca89SAndroid Build Coastguard Worker }
90*8d67ca89SAndroid Build Coastguard Worker fclose(fp);
91*8d67ca89SAndroid Build Coastguard Worker }
92*8d67ca89SAndroid Build Coastguard Worker }
93*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_fopen_fgetln_fclose_locking(benchmark::State & state)94*8d67ca89SAndroid Build Coastguard Worker static void BM_stdio_fopen_fgetln_fclose_locking(benchmark::State& state) {
95*8d67ca89SAndroid Build Coastguard Worker FopenFgetlnFclose(state, false);
96*8d67ca89SAndroid Build Coastguard Worker }
97*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_stdio_fopen_fgetln_fclose_locking);
98*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_fopen_fgetln_fclose_no_locking(benchmark::State & state)99*8d67ca89SAndroid Build Coastguard Worker void BM_stdio_fopen_fgetln_fclose_no_locking(benchmark::State& state) {
100*8d67ca89SAndroid Build Coastguard Worker FopenFgetlnFclose(state, true);
101*8d67ca89SAndroid Build Coastguard Worker }
102*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_stdio_fopen_fgetln_fclose_no_locking);
103*8d67ca89SAndroid Build Coastguard Worker #endif
104*8d67ca89SAndroid Build Coastguard Worker
FopenFgetsFclose(benchmark::State & state,bool no_locking)105*8d67ca89SAndroid Build Coastguard Worker static void FopenFgetsFclose(benchmark::State& state, bool no_locking) {
106*8d67ca89SAndroid Build Coastguard Worker TemporaryFile tf;
107*8d67ca89SAndroid Build Coastguard Worker FillFile(tf);
108*8d67ca89SAndroid Build Coastguard Worker char buf[BUFSIZ];
109*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
110*8d67ca89SAndroid Build Coastguard Worker FILE* fp = fopen(tf.path, "re");
111*8d67ca89SAndroid Build Coastguard Worker if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
112*8d67ca89SAndroid Build Coastguard Worker while (fgets(buf, sizeof(buf), fp) != nullptr) {
113*8d67ca89SAndroid Build Coastguard Worker }
114*8d67ca89SAndroid Build Coastguard Worker fclose(fp);
115*8d67ca89SAndroid Build Coastguard Worker }
116*8d67ca89SAndroid Build Coastguard Worker }
117*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_fopen_fgets_fclose_locking(benchmark::State & state)118*8d67ca89SAndroid Build Coastguard Worker static void BM_stdio_fopen_fgets_fclose_locking(benchmark::State& state) {
119*8d67ca89SAndroid Build Coastguard Worker FopenFgetsFclose(state, false);
120*8d67ca89SAndroid Build Coastguard Worker }
121*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_stdio_fopen_fgets_fclose_locking);
122*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_fopen_fgets_fclose_no_locking(benchmark::State & state)123*8d67ca89SAndroid Build Coastguard Worker void BM_stdio_fopen_fgets_fclose_no_locking(benchmark::State& state) {
124*8d67ca89SAndroid Build Coastguard Worker FopenFgetsFclose(state, true);
125*8d67ca89SAndroid Build Coastguard Worker }
126*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_stdio_fopen_fgets_fclose_no_locking);
127*8d67ca89SAndroid Build Coastguard Worker
FopenGetlineFclose(benchmark::State & state,bool no_locking)128*8d67ca89SAndroid Build Coastguard Worker static void FopenGetlineFclose(benchmark::State& state, bool no_locking) {
129*8d67ca89SAndroid Build Coastguard Worker TemporaryFile tf;
130*8d67ca89SAndroid Build Coastguard Worker FillFile(tf);
131*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
132*8d67ca89SAndroid Build Coastguard Worker FILE* fp = fopen(tf.path, "re");
133*8d67ca89SAndroid Build Coastguard Worker if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
134*8d67ca89SAndroid Build Coastguard Worker char* line = nullptr;
135*8d67ca89SAndroid Build Coastguard Worker size_t n = 0;
136*8d67ca89SAndroid Build Coastguard Worker while (getline(&line, &n, fp) != -1) {
137*8d67ca89SAndroid Build Coastguard Worker }
138*8d67ca89SAndroid Build Coastguard Worker free(line);
139*8d67ca89SAndroid Build Coastguard Worker fclose(fp);
140*8d67ca89SAndroid Build Coastguard Worker }
141*8d67ca89SAndroid Build Coastguard Worker }
142*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_fopen_getline_fclose_locking(benchmark::State & state)143*8d67ca89SAndroid Build Coastguard Worker static void BM_stdio_fopen_getline_fclose_locking(benchmark::State& state) {
144*8d67ca89SAndroid Build Coastguard Worker FopenGetlineFclose(state, false);
145*8d67ca89SAndroid Build Coastguard Worker }
146*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_stdio_fopen_getline_fclose_locking);
147*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_fopen_getline_fclose_no_locking(benchmark::State & state)148*8d67ca89SAndroid Build Coastguard Worker void BM_stdio_fopen_getline_fclose_no_locking(benchmark::State& state) {
149*8d67ca89SAndroid Build Coastguard Worker FopenGetlineFclose(state, true);
150*8d67ca89SAndroid Build Coastguard Worker }
151*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_stdio_fopen_getline_fclose_no_locking);
152*8d67ca89SAndroid Build Coastguard Worker
FopenFgetcFclose(benchmark::State & state,bool no_locking)153*8d67ca89SAndroid Build Coastguard Worker static void FopenFgetcFclose(benchmark::State& state, bool no_locking) {
154*8d67ca89SAndroid Build Coastguard Worker size_t nbytes = state.range(0);
155*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
156*8d67ca89SAndroid Build Coastguard Worker FILE* fp = fopen("/dev/zero", "re");
157*8d67ca89SAndroid Build Coastguard Worker if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
158*8d67ca89SAndroid Build Coastguard Worker for (size_t i = 0; i < nbytes; ++i) {
159*8d67ca89SAndroid Build Coastguard Worker benchmark::DoNotOptimize(fgetc(fp));
160*8d67ca89SAndroid Build Coastguard Worker }
161*8d67ca89SAndroid Build Coastguard Worker fclose(fp);
162*8d67ca89SAndroid Build Coastguard Worker }
163*8d67ca89SAndroid Build Coastguard Worker }
164*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_fopen_fgetc_fclose_locking(benchmark::State & state)165*8d67ca89SAndroid Build Coastguard Worker static void BM_stdio_fopen_fgetc_fclose_locking(benchmark::State& state) {
166*8d67ca89SAndroid Build Coastguard Worker FopenFgetcFclose(state, false);
167*8d67ca89SAndroid Build Coastguard Worker }
168*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK_WITH_ARG(BM_stdio_fopen_fgetc_fclose_locking, "1024");
169*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_fopen_fgetc_fclose_no_locking(benchmark::State & state)170*8d67ca89SAndroid Build Coastguard Worker void BM_stdio_fopen_fgetc_fclose_no_locking(benchmark::State& state) {
171*8d67ca89SAndroid Build Coastguard Worker FopenFgetcFclose(state, true);
172*8d67ca89SAndroid Build Coastguard Worker }
173*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK_WITH_ARG(BM_stdio_fopen_fgetc_fclose_no_locking, "1024");
174*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_printf_literal(benchmark::State & state)175*8d67ca89SAndroid Build Coastguard Worker static void BM_stdio_printf_literal(benchmark::State& state) {
176*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
177*8d67ca89SAndroid Build Coastguard Worker char buf[BUFSIZ];
178*8d67ca89SAndroid Build Coastguard Worker snprintf(buf, sizeof(buf), "this is just a literal string with no format specifiers");
179*8d67ca89SAndroid Build Coastguard Worker }
180*8d67ca89SAndroid Build Coastguard Worker }
181*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_stdio_printf_literal);
182*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_printf_s(benchmark::State & state)183*8d67ca89SAndroid Build Coastguard Worker static void BM_stdio_printf_s(benchmark::State& state) {
184*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
185*8d67ca89SAndroid Build Coastguard Worker char buf[BUFSIZ];
186*8d67ca89SAndroid Build Coastguard Worker snprintf(buf, sizeof(buf), "this is a more typical error message with detail: %s",
187*8d67ca89SAndroid Build Coastguard Worker "No such file or directory");
188*8d67ca89SAndroid Build Coastguard Worker }
189*8d67ca89SAndroid Build Coastguard Worker }
190*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_stdio_printf_s);
191*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_printf_d(benchmark::State & state)192*8d67ca89SAndroid Build Coastguard Worker static void BM_stdio_printf_d(benchmark::State& state) {
193*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
194*8d67ca89SAndroid Build Coastguard Worker char buf[BUFSIZ];
195*8d67ca89SAndroid Build Coastguard Worker snprintf(buf, sizeof(buf), "this is a more typical error message with detail: %d", 123456);
196*8d67ca89SAndroid Build Coastguard Worker }
197*8d67ca89SAndroid Build Coastguard Worker }
198*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_stdio_printf_d);
199*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_printf_1$s(benchmark::State & state)200*8d67ca89SAndroid Build Coastguard Worker static void BM_stdio_printf_1$s(benchmark::State& state) {
201*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
202*8d67ca89SAndroid Build Coastguard Worker char buf[BUFSIZ];
203*8d67ca89SAndroid Build Coastguard Worker snprintf(buf, sizeof(buf), "this is a more typical error message with detail: %1$s",
204*8d67ca89SAndroid Build Coastguard Worker "No such file or directory");
205*8d67ca89SAndroid Build Coastguard Worker }
206*8d67ca89SAndroid Build Coastguard Worker }
207*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_stdio_printf_1$s);
208*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_scanf_s(benchmark::State & state)209*8d67ca89SAndroid Build Coastguard Worker static void BM_stdio_scanf_s(benchmark::State& state) {
210*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
211*8d67ca89SAndroid Build Coastguard Worker char s[BUFSIZ];
212*8d67ca89SAndroid Build Coastguard Worker if (sscanf("file /etc/passwd", "file %s", s) != 1) abort();
213*8d67ca89SAndroid Build Coastguard Worker }
214*8d67ca89SAndroid Build Coastguard Worker }
215*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_stdio_scanf_s);
216*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_scanf_d(benchmark::State & state)217*8d67ca89SAndroid Build Coastguard Worker static void BM_stdio_scanf_d(benchmark::State& state) {
218*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
219*8d67ca89SAndroid Build Coastguard Worker int i;
220*8d67ca89SAndroid Build Coastguard Worker if (sscanf("size 12345", "size %d", &i) != 1) abort();
221*8d67ca89SAndroid Build Coastguard Worker }
222*8d67ca89SAndroid Build Coastguard Worker }
223*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_stdio_scanf_d);
224*8d67ca89SAndroid Build Coastguard Worker
225*8d67ca89SAndroid Build Coastguard Worker // Parsing maps is a common use of sscanf with a relatively complex format string.
BM_stdio_scanf_maps(benchmark::State & state)226*8d67ca89SAndroid Build Coastguard Worker static void BM_stdio_scanf_maps(benchmark::State& state) {
227*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
228*8d67ca89SAndroid Build Coastguard Worker uintptr_t start;
229*8d67ca89SAndroid Build Coastguard Worker uintptr_t end;
230*8d67ca89SAndroid Build Coastguard Worker uintptr_t offset;
231*8d67ca89SAndroid Build Coastguard Worker char permissions[5];
232*8d67ca89SAndroid Build Coastguard Worker int name_pos;
233*8d67ca89SAndroid Build Coastguard Worker if (sscanf("6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so",
234*8d67ca89SAndroid Build Coastguard Worker "%" PRIxPTR "-%" PRIxPTR " %4s %" PRIxPTR " %*x:%*x %*d %n",
235*8d67ca89SAndroid Build Coastguard Worker &start, &end, permissions, &offset, &name_pos) != 4) abort();
236*8d67ca89SAndroid Build Coastguard Worker }
237*8d67ca89SAndroid Build Coastguard Worker }
238*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_stdio_scanf_maps);
239*8d67ca89SAndroid Build Coastguard Worker
240*8d67ca89SAndroid Build Coastguard Worker // Hard-coded equivalent of the maps sscanf from libunwindstack/Maps.cpp for a baseline.
ParseMap(const char * line,const char *,uintptr_t * start,uintptr_t * end,char * permissions,uintptr_t * offset,int * name_pos)241*8d67ca89SAndroid Build Coastguard Worker static int ParseMap(const char* line, const char* /*fmt*/, uintptr_t* start, uintptr_t* end,
242*8d67ca89SAndroid Build Coastguard Worker char* permissions, uintptr_t* offset, int* name_pos) __attribute__((noinline)) {
243*8d67ca89SAndroid Build Coastguard Worker char* str;
244*8d67ca89SAndroid Build Coastguard Worker const char* old_str = line;
245*8d67ca89SAndroid Build Coastguard Worker
246*8d67ca89SAndroid Build Coastguard Worker // "%" PRIxPTR "-"
247*8d67ca89SAndroid Build Coastguard Worker *start = strtoul(old_str, &str, 16);
248*8d67ca89SAndroid Build Coastguard Worker if (old_str == str || *str++ != '-') return 0;
249*8d67ca89SAndroid Build Coastguard Worker
250*8d67ca89SAndroid Build Coastguard Worker // "%" PRIxPTR " "
251*8d67ca89SAndroid Build Coastguard Worker old_str = str;
252*8d67ca89SAndroid Build Coastguard Worker *end = strtoul(old_str, &str, 16);
253*8d67ca89SAndroid Build Coastguard Worker if (old_str == str || !std::isspace(*str++)) return 0;
254*8d67ca89SAndroid Build Coastguard Worker while (std::isspace(*str)) str++;
255*8d67ca89SAndroid Build Coastguard Worker
256*8d67ca89SAndroid Build Coastguard Worker // "%4s "
257*8d67ca89SAndroid Build Coastguard Worker if (*str == '\0') return 0;
258*8d67ca89SAndroid Build Coastguard Worker permissions[0] = *str;
259*8d67ca89SAndroid Build Coastguard Worker str++;
260*8d67ca89SAndroid Build Coastguard Worker permissions[1] = *str;
261*8d67ca89SAndroid Build Coastguard Worker str++;
262*8d67ca89SAndroid Build Coastguard Worker permissions[2] = *str;
263*8d67ca89SAndroid Build Coastguard Worker str++;
264*8d67ca89SAndroid Build Coastguard Worker permissions[3] = *str;
265*8d67ca89SAndroid Build Coastguard Worker str++;
266*8d67ca89SAndroid Build Coastguard Worker permissions[4] = 0;
267*8d67ca89SAndroid Build Coastguard Worker if (!std::isspace(*str++)) return 0;
268*8d67ca89SAndroid Build Coastguard Worker
269*8d67ca89SAndroid Build Coastguard Worker // "%" PRIxPTR " "
270*8d67ca89SAndroid Build Coastguard Worker old_str = str;
271*8d67ca89SAndroid Build Coastguard Worker *offset = strtoul(old_str, &str, 16);
272*8d67ca89SAndroid Build Coastguard Worker if (old_str == str || !std::isspace(*str)) return 0;
273*8d67ca89SAndroid Build Coastguard Worker
274*8d67ca89SAndroid Build Coastguard Worker // "%*x:%*x "
275*8d67ca89SAndroid Build Coastguard Worker old_str = str;
276*8d67ca89SAndroid Build Coastguard Worker (void)strtoul(old_str, &str, 16);
277*8d67ca89SAndroid Build Coastguard Worker if (old_str == str || *str++ != ':') return 0;
278*8d67ca89SAndroid Build Coastguard Worker if (std::isspace(*str)) return 0;
279*8d67ca89SAndroid Build Coastguard Worker old_str = str;
280*8d67ca89SAndroid Build Coastguard Worker (void)strtoul(str, &str, 16);
281*8d67ca89SAndroid Build Coastguard Worker if (old_str == str || !std::isspace(*str++)) return 0;
282*8d67ca89SAndroid Build Coastguard Worker
283*8d67ca89SAndroid Build Coastguard Worker // "%*d "
284*8d67ca89SAndroid Build Coastguard Worker old_str = str;
285*8d67ca89SAndroid Build Coastguard Worker (void)strtoul(old_str, &str, 10);
286*8d67ca89SAndroid Build Coastguard Worker if (old_str == str || (!std::isspace(*str) && *str != '\0')) return 0;
287*8d67ca89SAndroid Build Coastguard Worker while (std::isspace(*str)) str++;
288*8d67ca89SAndroid Build Coastguard Worker
289*8d67ca89SAndroid Build Coastguard Worker // "%n"
290*8d67ca89SAndroid Build Coastguard Worker *name_pos = (str - line);
291*8d67ca89SAndroid Build Coastguard Worker return 4;
292*8d67ca89SAndroid Build Coastguard Worker }
293*8d67ca89SAndroid Build Coastguard Worker
BM_stdio_scanf_maps_baseline(benchmark::State & state)294*8d67ca89SAndroid Build Coastguard Worker static void BM_stdio_scanf_maps_baseline(benchmark::State& state) {
295*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
296*8d67ca89SAndroid Build Coastguard Worker uintptr_t start;
297*8d67ca89SAndroid Build Coastguard Worker uintptr_t end;
298*8d67ca89SAndroid Build Coastguard Worker uintptr_t offset;
299*8d67ca89SAndroid Build Coastguard Worker char permissions[5];
300*8d67ca89SAndroid Build Coastguard Worker int name_pos;
301*8d67ca89SAndroid Build Coastguard Worker if (ParseMap("6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so",
302*8d67ca89SAndroid Build Coastguard Worker "%" PRIxPTR "-%" PRIxPTR " %4s %" PRIxPTR " %*x:%*x %*d %n",
303*8d67ca89SAndroid Build Coastguard Worker &start, &end, permissions, &offset, &name_pos) != 4) abort();
304*8d67ca89SAndroid Build Coastguard Worker }
305*8d67ca89SAndroid Build Coastguard Worker }
306*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_stdio_scanf_maps_baseline);
307