blob: a556d172374208fedac023a43cb390627cff48ea [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>
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080022
Chih-Hung Hsieh1a5fd9c2016-06-10 11:07:21 -070023constexpr auto KB = 1024;
Elliott Hughesb28e4902014-03-11 11:19:06 -070024
25#define AT_COMMON_SIZES \
26 Arg(1)->Arg(2)->Arg(3)->Arg(4)->Arg(8)->Arg(16)->Arg(32)->Arg(64)->Arg(512)-> \
27 Arg(1*KB)->Arg(4*KB)->Arg(8*KB)->Arg(16*KB)->Arg(64*KB)
28
Elliott Hughes47dc7c92014-12-01 13:12:18 -080029template <typename Fn>
Elliott Hughes281e06b2016-02-17 10:23:52 -080030void ReadWriteTest(benchmark::State& state, Fn f, bool buffered) {
31 size_t chunk_size = state.range_x();
32
Elliott Hughesb28e4902014-03-11 11:19:06 -070033 FILE* fp = fopen("/dev/zero", "rw");
Elliott Hughes8c4994b2015-01-20 18:09:05 -080034 __fsetlocking(fp, FSETLOCKING_BYCALLER);
Elliott Hughesb28e4902014-03-11 11:19:06 -070035 char* buf = new char[chunk_size];
Elliott Hughesb28e4902014-03-11 11:19:06 -070036
Elliott Hughes47dc7c92014-12-01 13:12:18 -080037 if (!buffered) {
38 setvbuf(fp, 0, _IONBF, 0);
39 }
40
Elliott Hughes281e06b2016-02-17 10:23:52 -080041 while (state.KeepRunning()) {
Elliott Hughes47dc7c92014-12-01 13:12:18 -080042 f(buf, chunk_size, 1, fp);
Elliott Hughesb28e4902014-03-11 11:19:06 -070043 }
44
Elliott Hughes281e06b2016-02-17 10:23:52 -080045 state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(chunk_size));
Elliott Hughesb28e4902014-03-11 11:19:06 -070046 delete[] buf;
47 fclose(fp);
48}
Elliott Hughes47dc7c92014-12-01 13:12:18 -080049
Elliott Hughes281e06b2016-02-17 10:23:52 -080050void BM_stdio_fread(benchmark::State& state) {
51 ReadWriteTest(state, fread, true);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080052}
Elliott Hughes281e06b2016-02-17 10:23:52 -080053BENCHMARK(BM_stdio_fread)->AT_COMMON_SIZES;
Elliott Hughesb28e4902014-03-11 11:19:06 -070054
Elliott Hughes281e06b2016-02-17 10:23:52 -080055void BM_stdio_fwrite(benchmark::State& state) {
56 ReadWriteTest(state, fwrite, true);
Elliott Hughesb28e4902014-03-11 11:19:06 -070057}
Elliott Hughes281e06b2016-02-17 10:23:52 -080058BENCHMARK(BM_stdio_fwrite)->AT_COMMON_SIZES;
Elliott Hughes47dc7c92014-12-01 13:12:18 -080059
Elliott Hughes281e06b2016-02-17 10:23:52 -080060void BM_stdio_fread_unbuffered(benchmark::State& state) {
61 ReadWriteTest(state, fread, false);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080062}
Elliott Hughes281e06b2016-02-17 10:23:52 -080063BENCHMARK(BM_stdio_fread_unbuffered)->AT_COMMON_SIZES;
Elliott Hughes47dc7c92014-12-01 13:12:18 -080064
Elliott Hughes281e06b2016-02-17 10:23:52 -080065void BM_stdio_fwrite_unbuffered(benchmark::State& state) {
66 ReadWriteTest(state, fwrite, false);
Elliott Hughes47dc7c92014-12-01 13:12:18 -080067}
Elliott Hughes281e06b2016-02-17 10:23:52 -080068BENCHMARK(BM_stdio_fwrite_unbuffered)->AT_COMMON_SIZES;
Elliott Hughes1cf32f82015-01-16 17:08:31 -080069
Elliott Hughes281e06b2016-02-17 10:23:52 -080070static void FopenFgetsFclose(benchmark::State& state, bool no_locking) {
Elliott Hughes1cf32f82015-01-16 17:08:31 -080071 char buf[1024];
Elliott Hughes281e06b2016-02-17 10:23:52 -080072 while (state.KeepRunning()) {
Elliott Hughes1cf32f82015-01-16 17:08:31 -080073 FILE* fp = fopen("/proc/version", "re");
Elliott Hughes8c4994b2015-01-20 18:09:05 -080074 if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
Elliott Hughesc2173732015-05-13 13:18:04 -070075 if (fgets(buf, sizeof(buf), fp) == nullptr) abort();
Elliott Hughes1cf32f82015-01-16 17:08:31 -080076 fclose(fp);
77 }
78}
Elliott Hughes8c4994b2015-01-20 18:09:05 -080079
Elliott Hughes281e06b2016-02-17 10:23:52 -080080static void BM_stdio_fopen_fgets_fclose_locking(benchmark::State& state) {
81 FopenFgetsFclose(state, false);
Elliott Hughes8c4994b2015-01-20 18:09:05 -080082}
Elliott Hughes281e06b2016-02-17 10:23:52 -080083BENCHMARK(BM_stdio_fopen_fgets_fclose_locking);
Elliott Hughes8c4994b2015-01-20 18:09:05 -080084
Elliott Hughes281e06b2016-02-17 10:23:52 -080085void BM_stdio_fopen_fgets_fclose_no_locking(benchmark::State& state) {
86 FopenFgetsFclose(state, true);
Elliott Hughes8c4994b2015-01-20 18:09:05 -080087}
Elliott Hughes281e06b2016-02-17 10:23:52 -080088BENCHMARK(BM_stdio_fopen_fgets_fclose_no_locking);