blob: 03f3f290357b761f2900049958c3ddc572f54b56 [file] [log] [blame]
Elliott Hughesb28e4902014-03-11 11:19:06 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Anders Lewisa98a5fb2017-08-09 16:52:19 -070017#include <err.h>
Elliott Hughes3b644e92017-12-11 14:43:52 -080018#include <inttypes.h>
Elliott Hughesb28e4902014-03-11 11:19:06 -070019#include <stdio.h>
Elliott Hughes8c4994b2015-01-20 18:09:05 -080020#include <stdio_ext.h>
Elliott Hughesc2173732015-05-13 13:18:04 -070021#include <stdlib.h>
Elliott Hughesb28e4902014-03-11 11:19:06 -070022
Mark Salyzynba1a7232018-11-14 15:19:53 -080023#include <android-base/file.h>
Elliott Hughes281e06b2016-02-17 10:23:52 -080024#include <benchmark/benchmark.h>
Anders Lewisa7b0f882017-07-24 20:01:13 -070025#include "util.h"
Elliott Hughesb28e4902014-03-11 11:19:06 -070026
Elliott Hughes938bece2017-08-23 09:52:50 -070027static void FillFile(TemporaryFile& tf) {
28 char line[256];
29 memset(line, 'x', sizeof(line));
30 line[sizeof(line) - 1] = '\0';
31
Elliott Hughes5cec3772018-01-19 15:45:23 -080032 FILE* fp = fopen(tf.path, "we");
Elliott Hughes938bece2017-08-23 09:52:50 -070033 for (size_t i = 0; i < 4096; ++i) fputs(line, fp);
34 fclose(fp);
35}
36
Elliott Hughes47dc7c92014-12-01 13:12:18 -080037template <typename Fn>
Elliott Hughes281e06b2016-02-17 10:23:52 -080038void ReadWriteTest(benchmark::State& state, Fn f, bool buffered) {
Martijn Coenenbe763d82016-11-14 14:16:08 +010039 size_t chunk_size = state.range(0);
Elliott Hughes281e06b2016-02-17 10:23:52 -080040
Elliott Hughesd706fe52017-08-22 15:26:07 -070041 FILE* fp = fopen("/dev/zero", "r+e");
Elliott Hughes8c4994b2015-01-20 18:09:05 -080042 __fsetlocking(fp, FSETLOCKING_BYCALLER);
Elliott Hughesb28e4902014-03-11 11:19:06 -070043 char* buf = new char[chunk_size];
Elliott Hughesb28e4902014-03-11 11:19:06 -070044
Elliott Hughes47dc7c92014-12-01 13:12:18 -080045 if (!buffered) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070046 setvbuf(fp, nullptr, _IONBF, 0);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080047 }
48
Elliott Hughes281e06b2016-02-17 10:23:52 -080049 while (state.KeepRunning()) {
Elliott Hughesd706fe52017-08-22 15:26:07 -070050 if (f(buf, chunk_size, 1, fp) != 1) {
51 errx(1, "ERROR: op of %zu bytes failed.", chunk_size);
52 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070053 }
54
Elliott Hughes281e06b2016-02-17 10:23:52 -080055 state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(chunk_size));
Elliott Hughesb28e4902014-03-11 11:19:06 -070056 delete[] buf;
57 fclose(fp);
58}
Elliott Hughes47dc7c92014-12-01 13:12:18 -080059
Elliott Hughes281e06b2016-02-17 10:23:52 -080060void BM_stdio_fread(benchmark::State& state) {
61 ReadWriteTest(state, fread, true);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080062}
Christopher Ferris858e3362017-11-30 08:53:15 -080063BIONIC_BENCHMARK_WITH_ARG(BM_stdio_fread, "AT_COMMON_SIZES");
Elliott Hughesb28e4902014-03-11 11:19:06 -070064
Elliott Hughes281e06b2016-02-17 10:23:52 -080065void BM_stdio_fwrite(benchmark::State& state) {
66 ReadWriteTest(state, fwrite, true);
Elliott Hughesb28e4902014-03-11 11:19:06 -070067}
Christopher Ferris858e3362017-11-30 08:53:15 -080068BIONIC_BENCHMARK_WITH_ARG(BM_stdio_fwrite, "AT_COMMON_SIZES");
Elliott Hughes47dc7c92014-12-01 13:12:18 -080069
Elliott Hughes281e06b2016-02-17 10:23:52 -080070void BM_stdio_fread_unbuffered(benchmark::State& state) {
71 ReadWriteTest(state, fread, false);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080072}
Christopher Ferris858e3362017-11-30 08:53:15 -080073BIONIC_BENCHMARK_WITH_ARG(BM_stdio_fread_unbuffered, "AT_COMMON_SIZES");
Elliott Hughes47dc7c92014-12-01 13:12:18 -080074
Elliott Hughes281e06b2016-02-17 10:23:52 -080075void BM_stdio_fwrite_unbuffered(benchmark::State& state) {
76 ReadWriteTest(state, fwrite, false);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080077}
Christopher Ferris858e3362017-11-30 08:53:15 -080078BIONIC_BENCHMARK_WITH_ARG(BM_stdio_fwrite_unbuffered, "AT_COMMON_SIZES");
Elliott Hughes1cf32f82015-01-16 17:08:31 -080079
Elliott Hughes938bece2017-08-23 09:52:50 -070080#if !defined(__GLIBC__)
81static void FopenFgetlnFclose(benchmark::State& state, bool no_locking) {
82 TemporaryFile tf;
83 FillFile(tf);
Elliott Hughes281e06b2016-02-17 10:23:52 -080084 while (state.KeepRunning()) {
Elliott Hughes938bece2017-08-23 09:52:50 -070085 FILE* fp = fopen(tf.path, "re");
Elliott Hughes8c4994b2015-01-20 18:09:05 -080086 if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
Elliott Hughes938bece2017-08-23 09:52:50 -070087 size_t length;
88 while (fgetln(fp, &length) != nullptr) {
89 }
90 fclose(fp);
91 }
92}
93
94static void BM_stdio_fopen_fgetln_fclose_locking(benchmark::State& state) {
95 FopenFgetlnFclose(state, false);
96}
97BIONIC_BENCHMARK(BM_stdio_fopen_fgetln_fclose_locking);
98
99void BM_stdio_fopen_fgetln_fclose_no_locking(benchmark::State& state) {
100 FopenFgetlnFclose(state, true);
101}
102BIONIC_BENCHMARK(BM_stdio_fopen_fgetln_fclose_no_locking);
103#endif
104
105static void FopenFgetsFclose(benchmark::State& state, bool no_locking) {
106 TemporaryFile tf;
107 FillFile(tf);
108 char buf[BUFSIZ];
109 while (state.KeepRunning()) {
110 FILE* fp = fopen(tf.path, "re");
111 if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
112 while (fgets(buf, sizeof(buf), fp) != nullptr) {
Anders Lewisa98a5fb2017-08-09 16:52:19 -0700113 }
Elliott Hughes1cf32f82015-01-16 17:08:31 -0800114 fclose(fp);
115 }
116}
Elliott Hughes8c4994b2015-01-20 18:09:05 -0800117
Elliott Hughes281e06b2016-02-17 10:23:52 -0800118static void BM_stdio_fopen_fgets_fclose_locking(benchmark::State& state) {
119 FopenFgetsFclose(state, false);
Elliott Hughes8c4994b2015-01-20 18:09:05 -0800120}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700121BIONIC_BENCHMARK(BM_stdio_fopen_fgets_fclose_locking);
Elliott Hughes8c4994b2015-01-20 18:09:05 -0800122
Elliott Hughes281e06b2016-02-17 10:23:52 -0800123void BM_stdio_fopen_fgets_fclose_no_locking(benchmark::State& state) {
124 FopenFgetsFclose(state, true);
Elliott Hughes8c4994b2015-01-20 18:09:05 -0800125}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700126BIONIC_BENCHMARK(BM_stdio_fopen_fgets_fclose_no_locking);
Anders Lewisac4f4b42017-08-08 18:29:51 -0700127
Elliott Hughes938bece2017-08-23 09:52:50 -0700128static void FopenGetlineFclose(benchmark::State& state, bool no_locking) {
129 TemporaryFile tf;
130 FillFile(tf);
131 while (state.KeepRunning()) {
132 FILE* fp = fopen(tf.path, "re");
133 if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
134 char* line = nullptr;
135 size_t n = 0;
136 while (getline(&line, &n, fp) != -1) {
137 }
138 free(line);
139 fclose(fp);
140 }
141}
142
143static void BM_stdio_fopen_getline_fclose_locking(benchmark::State& state) {
144 FopenGetlineFclose(state, false);
145}
146BIONIC_BENCHMARK(BM_stdio_fopen_getline_fclose_locking);
147
148void BM_stdio_fopen_getline_fclose_no_locking(benchmark::State& state) {
149 FopenGetlineFclose(state, true);
150}
151BIONIC_BENCHMARK(BM_stdio_fopen_getline_fclose_no_locking);
152
Anders Lewisac4f4b42017-08-08 18:29:51 -0700153static void FopenFgetcFclose(benchmark::State& state, bool no_locking) {
154 size_t nbytes = state.range(0);
155 while (state.KeepRunning()) {
156 FILE* fp = fopen("/dev/zero", "re");
157 if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
Anders Lewisac4f4b42017-08-08 18:29:51 -0700158 for (size_t i = 0; i < nbytes; ++i) {
Elliott Hughese8693e72020-10-22 13:43:59 -0700159 benchmark::DoNotOptimize(fgetc(fp));
Anders Lewisac4f4b42017-08-08 18:29:51 -0700160 }
161 fclose(fp);
162 }
163}
164
165static void BM_stdio_fopen_fgetc_fclose_locking(benchmark::State& state) {
166 FopenFgetcFclose(state, false);
167}
Christopher Ferris858e3362017-11-30 08:53:15 -0800168BIONIC_BENCHMARK_WITH_ARG(BM_stdio_fopen_fgetc_fclose_locking, "1024");
Anders Lewisac4f4b42017-08-08 18:29:51 -0700169
170void BM_stdio_fopen_fgetc_fclose_no_locking(benchmark::State& state) {
171 FopenFgetcFclose(state, true);
172}
Christopher Ferris858e3362017-11-30 08:53:15 -0800173BIONIC_BENCHMARK_WITH_ARG(BM_stdio_fopen_fgetc_fclose_no_locking, "1024");
Elliott Hughes92805992017-10-26 15:41:49 -0700174
175static void BM_stdio_printf_literal(benchmark::State& state) {
176 while (state.KeepRunning()) {
177 char buf[BUFSIZ];
178 snprintf(buf, sizeof(buf), "this is just a literal string with no format specifiers");
179 }
180}
181BIONIC_BENCHMARK(BM_stdio_printf_literal);
182
183static void BM_stdio_printf_s(benchmark::State& state) {
184 while (state.KeepRunning()) {
185 char buf[BUFSIZ];
186 snprintf(buf, sizeof(buf), "this is a more typical error message with detail: %s",
187 "No such file or directory");
188 }
189}
190BIONIC_BENCHMARK(BM_stdio_printf_s);
191
192static void BM_stdio_printf_d(benchmark::State& state) {
193 while (state.KeepRunning()) {
194 char buf[BUFSIZ];
195 snprintf(buf, sizeof(buf), "this is a more typical error message with detail: %d", 123456);
196 }
197}
198BIONIC_BENCHMARK(BM_stdio_printf_d);
Elliott Hughes5305a4d2017-11-03 14:00:37 -0700199
200static void BM_stdio_printf_1$s(benchmark::State& state) {
201 while (state.KeepRunning()) {
202 char buf[BUFSIZ];
203 snprintf(buf, sizeof(buf), "this is a more typical error message with detail: %1$s",
204 "No such file or directory");
205 }
206}
207BIONIC_BENCHMARK(BM_stdio_printf_1$s);
Elliott Hughes3b644e92017-12-11 14:43:52 -0800208
209static void BM_stdio_scanf_s(benchmark::State& state) {
210 while (state.KeepRunning()) {
211 char s[BUFSIZ];
212 if (sscanf("file /etc/passwd", "file %s", s) != 1) abort();
213 }
214}
215BIONIC_BENCHMARK(BM_stdio_scanf_s);
216
217static void BM_stdio_scanf_d(benchmark::State& state) {
218 while (state.KeepRunning()) {
219 int i;
220 if (sscanf("size 12345", "size %d", &i) != 1) abort();
221 }
222}
223BIONIC_BENCHMARK(BM_stdio_scanf_d);
224
Elliott Hughes7063a832017-12-19 08:55:40 -0800225// Parsing maps is a common use of sscanf with a relatively complex format string.
Elliott Hughes3b644e92017-12-11 14:43:52 -0800226static void BM_stdio_scanf_maps(benchmark::State& state) {
227 while (state.KeepRunning()) {
228 uintptr_t start;
229 uintptr_t end;
230 uintptr_t offset;
231 char permissions[5];
232 int name_pos;
233 if (sscanf("6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so",
234 "%" PRIxPTR "-%" PRIxPTR " %4s %" PRIxPTR " %*x:%*x %*d %n",
235 &start, &end, permissions, &offset, &name_pos) != 4) abort();
236 }
237}
238BIONIC_BENCHMARK(BM_stdio_scanf_maps);
Elliott Hughes7063a832017-12-19 08:55:40 -0800239
240// Hard-coded equivalent of the maps sscanf from libunwindstack/Maps.cpp for a baseline.
241static int ParseMap(const char* line, const char* /*fmt*/, uintptr_t* start, uintptr_t* end,
242 char* permissions, uintptr_t* offset, int* name_pos) __attribute__((noinline)) {
243 char* str;
244 const char* old_str = line;
245
246 // "%" PRIxPTR "-"
247 *start = strtoul(old_str, &str, 16);
248 if (old_str == str || *str++ != '-') return 0;
249
250 // "%" PRIxPTR " "
251 old_str = str;
252 *end = strtoul(old_str, &str, 16);
253 if (old_str == str || !std::isspace(*str++)) return 0;
254 while (std::isspace(*str)) str++;
255
256 // "%4s "
257 if (*str == '\0') return 0;
258 permissions[0] = *str;
259 str++;
260 permissions[1] = *str;
261 str++;
262 permissions[2] = *str;
263 str++;
264 permissions[3] = *str;
265 str++;
266 permissions[4] = 0;
267 if (!std::isspace(*str++)) return 0;
268
269 // "%" PRIxPTR " "
270 old_str = str;
271 *offset = strtoul(old_str, &str, 16);
272 if (old_str == str || !std::isspace(*str)) return 0;
273
274 // "%*x:%*x "
275 old_str = str;
276 (void)strtoul(old_str, &str, 16);
277 if (old_str == str || *str++ != ':') return 0;
278 if (std::isspace(*str)) return 0;
279 old_str = str;
280 (void)strtoul(str, &str, 16);
281 if (old_str == str || !std::isspace(*str++)) return 0;
282
283 // "%*d "
284 old_str = str;
285 (void)strtoul(old_str, &str, 10);
286 if (old_str == str || (!std::isspace(*str) && *str != '\0')) return 0;
287 while (std::isspace(*str)) str++;
288
289 // "%n"
290 *name_pos = (str - line);
291 return 4;
292}
293
294static void BM_stdio_scanf_maps_baseline(benchmark::State& state) {
295 while (state.KeepRunning()) {
296 uintptr_t start;
297 uintptr_t end;
298 uintptr_t offset;
299 char permissions[5];
300 int name_pos;
301 if (ParseMap("6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so",
302 "%" PRIxPTR "-%" PRIxPTR " %4s %" PRIxPTR " %*x:%*x %*d %n",
303 &start, &end, permissions, &offset, &name_pos) != 4) abort();
304 }
305}
306BIONIC_BENCHMARK(BM_stdio_scanf_maps_baseline);