blob: 3f5e0f13866b6555522014c063a05c21f6a337db [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 Hughesb28e4902014-03-11 11:19:06 -070018#include <stdio.h>
Elliott Hughes8c4994b2015-01-20 18:09:05 -080019#include <stdio_ext.h>
Elliott Hughesc2173732015-05-13 13:18:04 -070020#include <stdlib.h>
Elliott Hughesb28e4902014-03-11 11:19:06 -070021
Elliott Hughes281e06b2016-02-17 10:23:52 -080022#include <benchmark/benchmark.h>
Anders Lewisa7b0f882017-07-24 20:01:13 -070023#include "util.h"
Elliott Hughesb28e4902014-03-11 11:19:06 -070024
Elliott Hughes47dc7c92014-12-01 13:12:18 -080025template <typename Fn>
Elliott Hughes281e06b2016-02-17 10:23:52 -080026void ReadWriteTest(benchmark::State& state, Fn f, bool buffered) {
Martijn Coenenbe763d82016-11-14 14:16:08 +010027 size_t chunk_size = state.range(0);
Elliott Hughes281e06b2016-02-17 10:23:52 -080028
Elliott Hughesd706fe52017-08-22 15:26:07 -070029 FILE* fp = fopen("/dev/zero", "r+e");
Elliott Hughes8c4994b2015-01-20 18:09:05 -080030 __fsetlocking(fp, FSETLOCKING_BYCALLER);
Elliott Hughesb28e4902014-03-11 11:19:06 -070031 char* buf = new char[chunk_size];
Elliott Hughesb28e4902014-03-11 11:19:06 -070032
Elliott Hughes47dc7c92014-12-01 13:12:18 -080033 if (!buffered) {
34 setvbuf(fp, 0, _IONBF, 0);
35 }
36
Elliott Hughes281e06b2016-02-17 10:23:52 -080037 while (state.KeepRunning()) {
Elliott Hughesd706fe52017-08-22 15:26:07 -070038 if (f(buf, chunk_size, 1, fp) != 1) {
39 errx(1, "ERROR: op of %zu bytes failed.", chunk_size);
40 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070041 }
42
Elliott Hughes281e06b2016-02-17 10:23:52 -080043 state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(chunk_size));
Elliott Hughesb28e4902014-03-11 11:19:06 -070044 delete[] buf;
45 fclose(fp);
46}
Elliott Hughes47dc7c92014-12-01 13:12:18 -080047
Elliott Hughes281e06b2016-02-17 10:23:52 -080048void BM_stdio_fread(benchmark::State& state) {
49 ReadWriteTest(state, fread, true);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080050}
Anders Lewisa7b0f882017-07-24 20:01:13 -070051BIONIC_BENCHMARK(BM_stdio_fread);
Elliott Hughesb28e4902014-03-11 11:19:06 -070052
Elliott Hughes281e06b2016-02-17 10:23:52 -080053void BM_stdio_fwrite(benchmark::State& state) {
54 ReadWriteTest(state, fwrite, true);
Elliott Hughesb28e4902014-03-11 11:19:06 -070055}
Anders Lewisa7b0f882017-07-24 20:01:13 -070056BIONIC_BENCHMARK(BM_stdio_fwrite);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080057
Elliott Hughes281e06b2016-02-17 10:23:52 -080058void BM_stdio_fread_unbuffered(benchmark::State& state) {
59 ReadWriteTest(state, fread, false);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080060}
Anders Lewisa7b0f882017-07-24 20:01:13 -070061BIONIC_BENCHMARK(BM_stdio_fread_unbuffered);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080062
Elliott Hughes281e06b2016-02-17 10:23:52 -080063void BM_stdio_fwrite_unbuffered(benchmark::State& state) {
64 ReadWriteTest(state, fwrite, false);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080065}
Anders Lewisa7b0f882017-07-24 20:01:13 -070066BIONIC_BENCHMARK(BM_stdio_fwrite_unbuffered);
Elliott Hughes1cf32f82015-01-16 17:08:31 -080067
Elliott Hughes281e06b2016-02-17 10:23:52 -080068static void FopenFgetsFclose(benchmark::State& state, bool no_locking) {
Anders Lewisac4f4b42017-08-08 18:29:51 -070069 size_t nbytes = state.range(0);
70 char buf[nbytes];
Elliott Hughes281e06b2016-02-17 10:23:52 -080071 while (state.KeepRunning()) {
Anders Lewisac4f4b42017-08-08 18:29:51 -070072 FILE* fp = fopen("/dev/zero", "re");
Elliott Hughes8c4994b2015-01-20 18:09:05 -080073 if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
Anders Lewisa98a5fb2017-08-09 16:52:19 -070074 if (fgets(buf, sizeof(buf), fp) == nullptr) {
Elliott Hughesd706fe52017-08-22 15:26:07 -070075 errx(1, "ERROR: fgets of %zu bytes failed.", nbytes);
Anders Lewisa98a5fb2017-08-09 16:52:19 -070076 }
Elliott Hughes1cf32f82015-01-16 17:08:31 -080077 fclose(fp);
78 }
79}
Elliott Hughes8c4994b2015-01-20 18:09:05 -080080
Elliott Hughes281e06b2016-02-17 10:23:52 -080081static void BM_stdio_fopen_fgets_fclose_locking(benchmark::State& state) {
82 FopenFgetsFclose(state, false);
Elliott Hughes8c4994b2015-01-20 18:09:05 -080083}
Anders Lewisa7b0f882017-07-24 20:01:13 -070084BIONIC_BENCHMARK(BM_stdio_fopen_fgets_fclose_locking);
Elliott Hughes8c4994b2015-01-20 18:09:05 -080085
Elliott Hughes281e06b2016-02-17 10:23:52 -080086void BM_stdio_fopen_fgets_fclose_no_locking(benchmark::State& state) {
87 FopenFgetsFclose(state, true);
Elliott Hughes8c4994b2015-01-20 18:09:05 -080088}
Anders Lewisa7b0f882017-07-24 20:01:13 -070089BIONIC_BENCHMARK(BM_stdio_fopen_fgets_fclose_no_locking);
Anders Lewisac4f4b42017-08-08 18:29:51 -070090
91static void FopenFgetcFclose(benchmark::State& state, bool no_locking) {
92 size_t nbytes = state.range(0);
93 while (state.KeepRunning()) {
94 FILE* fp = fopen("/dev/zero", "re");
95 if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
96 volatile int c __attribute__((unused));
97 for (size_t i = 0; i < nbytes; ++i) {
98 c = fgetc(fp);
99 }
100 fclose(fp);
101 }
102}
103
104static void BM_stdio_fopen_fgetc_fclose_locking(benchmark::State& state) {
105 FopenFgetcFclose(state, false);
106}
107BIONIC_BENCHMARK(BM_stdio_fopen_fgetc_fclose_locking);
108
109void BM_stdio_fopen_fgetc_fclose_no_locking(benchmark::State& state) {
110 FopenFgetcFclose(state, true);
111}
112BIONIC_BENCHMARK(BM_stdio_fopen_fgetc_fclose_no_locking);