blob: a1ca60e8a7af8e622c2f8a95040c6d1ce44b2b88 [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
Elliott Hughesb28e4902014-03-11 11:19:06 -070017#include <stdio.h>
Elliott Hughes8c4994b2015-01-20 18:09:05 -080018#include <stdio_ext.h>
Elliott Hughesc2173732015-05-13 13:18:04 -070019#include <stdlib.h>
Elliott Hughesb28e4902014-03-11 11:19:06 -070020
Elliott Hughes281e06b2016-02-17 10:23:52 -080021#include <benchmark/benchmark.h>
Anders Lewisa7b0f882017-07-24 20:01:13 -070022#include "util.h"
Elliott Hughesb28e4902014-03-11 11:19:06 -070023
Elliott Hughes47dc7c92014-12-01 13:12:18 -080024template <typename Fn>
Elliott Hughes281e06b2016-02-17 10:23:52 -080025void ReadWriteTest(benchmark::State& state, Fn f, bool buffered) {
Martijn Coenenbe763d82016-11-14 14:16:08 +010026 size_t chunk_size = state.range(0);
Elliott Hughes281e06b2016-02-17 10:23:52 -080027
Elliott Hughesb28e4902014-03-11 11:19:06 -070028 FILE* fp = fopen("/dev/zero", "rw");
Elliott Hughes8c4994b2015-01-20 18:09:05 -080029 __fsetlocking(fp, FSETLOCKING_BYCALLER);
Elliott Hughesb28e4902014-03-11 11:19:06 -070030 char* buf = new char[chunk_size];
Elliott Hughesb28e4902014-03-11 11:19:06 -070031
Elliott Hughes47dc7c92014-12-01 13:12:18 -080032 if (!buffered) {
33 setvbuf(fp, 0, _IONBF, 0);
34 }
35
Elliott Hughes281e06b2016-02-17 10:23:52 -080036 while (state.KeepRunning()) {
Elliott Hughes47dc7c92014-12-01 13:12:18 -080037 f(buf, chunk_size, 1, fp);
Elliott Hughesb28e4902014-03-11 11:19:06 -070038 }
39
Elliott Hughes281e06b2016-02-17 10:23:52 -080040 state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(chunk_size));
Elliott Hughesb28e4902014-03-11 11:19:06 -070041 delete[] buf;
42 fclose(fp);
43}
Elliott Hughes47dc7c92014-12-01 13:12:18 -080044
Elliott Hughes281e06b2016-02-17 10:23:52 -080045void BM_stdio_fread(benchmark::State& state) {
46 ReadWriteTest(state, fread, true);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080047}
Anders Lewisa7b0f882017-07-24 20:01:13 -070048BIONIC_BENCHMARK(BM_stdio_fread);
Elliott Hughesb28e4902014-03-11 11:19:06 -070049
Elliott Hughes281e06b2016-02-17 10:23:52 -080050void BM_stdio_fwrite(benchmark::State& state) {
51 ReadWriteTest(state, fwrite, true);
Elliott Hughesb28e4902014-03-11 11:19:06 -070052}
Anders Lewisa7b0f882017-07-24 20:01:13 -070053BIONIC_BENCHMARK(BM_stdio_fwrite);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080054
Elliott Hughes281e06b2016-02-17 10:23:52 -080055void BM_stdio_fread_unbuffered(benchmark::State& state) {
56 ReadWriteTest(state, fread, false);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080057}
Anders Lewisa7b0f882017-07-24 20:01:13 -070058BIONIC_BENCHMARK(BM_stdio_fread_unbuffered);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080059
Elliott Hughes281e06b2016-02-17 10:23:52 -080060void BM_stdio_fwrite_unbuffered(benchmark::State& state) {
61 ReadWriteTest(state, fwrite, false);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080062}
Anders Lewisa7b0f882017-07-24 20:01:13 -070063BIONIC_BENCHMARK(BM_stdio_fwrite_unbuffered);
Elliott Hughes1cf32f82015-01-16 17:08:31 -080064
Elliott Hughes281e06b2016-02-17 10:23:52 -080065static void FopenFgetsFclose(benchmark::State& state, bool no_locking) {
Anders Lewisac4f4b42017-08-08 18:29:51 -070066 size_t nbytes = state.range(0);
67 char buf[nbytes];
Elliott Hughes281e06b2016-02-17 10:23:52 -080068 while (state.KeepRunning()) {
Anders Lewisac4f4b42017-08-08 18:29:51 -070069 FILE* fp = fopen("/dev/zero", "re");
Elliott Hughes8c4994b2015-01-20 18:09:05 -080070 if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
Elliott Hughesc2173732015-05-13 13:18:04 -070071 if (fgets(buf, sizeof(buf), fp) == nullptr) abort();
Elliott Hughes1cf32f82015-01-16 17:08:31 -080072 fclose(fp);
73 }
74}
Elliott Hughes8c4994b2015-01-20 18:09:05 -080075
Elliott Hughes281e06b2016-02-17 10:23:52 -080076static void BM_stdio_fopen_fgets_fclose_locking(benchmark::State& state) {
77 FopenFgetsFclose(state, false);
Elliott Hughes8c4994b2015-01-20 18:09:05 -080078}
Anders Lewisa7b0f882017-07-24 20:01:13 -070079BIONIC_BENCHMARK(BM_stdio_fopen_fgets_fclose_locking);
Elliott Hughes8c4994b2015-01-20 18:09:05 -080080
Elliott Hughes281e06b2016-02-17 10:23:52 -080081void BM_stdio_fopen_fgets_fclose_no_locking(benchmark::State& state) {
82 FopenFgetsFclose(state, true);
Elliott Hughes8c4994b2015-01-20 18:09:05 -080083}
Anders Lewisa7b0f882017-07-24 20:01:13 -070084BIONIC_BENCHMARK(BM_stdio_fopen_fgets_fclose_no_locking);
Anders Lewisac4f4b42017-08-08 18:29:51 -070085
86static void FopenFgetcFclose(benchmark::State& state, bool no_locking) {
87 size_t nbytes = state.range(0);
88 while (state.KeepRunning()) {
89 FILE* fp = fopen("/dev/zero", "re");
90 if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
91 volatile int c __attribute__((unused));
92 for (size_t i = 0; i < nbytes; ++i) {
93 c = fgetc(fp);
94 }
95 fclose(fp);
96 }
97}
98
99static void BM_stdio_fopen_fgetc_fclose_locking(benchmark::State& state) {
100 FopenFgetcFclose(state, false);
101}
102BIONIC_BENCHMARK(BM_stdio_fopen_fgetc_fclose_locking);
103
104void BM_stdio_fopen_fgetc_fclose_no_locking(benchmark::State& state) {
105 FopenFgetcFclose(state, true);
106}
107BIONIC_BENCHMARK(BM_stdio_fopen_fgetc_fclose_no_locking);
108