1 /*
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 
17 #include <stdio.h>
18 #include <stdio_ext.h>
19 #include <stdlib.h>
20 
21 #include <benchmark/benchmark.h>
22 
23 constexpr auto KB = 1024;
24 
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 
29 template <typename Fn>
ReadWriteTest(benchmark::State & state,Fn f,bool buffered)30 void ReadWriteTest(benchmark::State& state, Fn f, bool buffered) {
31   size_t chunk_size = state.range(0);
32 
33   FILE* fp = fopen("/dev/zero", "rw");
34   __fsetlocking(fp, FSETLOCKING_BYCALLER);
35   char* buf = new char[chunk_size];
36 
37   if (!buffered) {
38     setvbuf(fp, 0, _IONBF, 0);
39   }
40 
41   while (state.KeepRunning()) {
42     f(buf, chunk_size, 1, fp);
43   }
44 
45   state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(chunk_size));
46   delete[] buf;
47   fclose(fp);
48 }
49 
BM_stdio_fread(benchmark::State & state)50 void BM_stdio_fread(benchmark::State& state) {
51   ReadWriteTest(state, fread, true);
52 }
53 BENCHMARK(BM_stdio_fread)->AT_COMMON_SIZES;
54 
BM_stdio_fwrite(benchmark::State & state)55 void BM_stdio_fwrite(benchmark::State& state) {
56   ReadWriteTest(state, fwrite, true);
57 }
58 BENCHMARK(BM_stdio_fwrite)->AT_COMMON_SIZES;
59 
BM_stdio_fread_unbuffered(benchmark::State & state)60 void BM_stdio_fread_unbuffered(benchmark::State& state) {
61   ReadWriteTest(state, fread, false);
62 }
63 BENCHMARK(BM_stdio_fread_unbuffered)->AT_COMMON_SIZES;
64 
BM_stdio_fwrite_unbuffered(benchmark::State & state)65 void BM_stdio_fwrite_unbuffered(benchmark::State& state) {
66   ReadWriteTest(state, fwrite, false);
67 }
68 BENCHMARK(BM_stdio_fwrite_unbuffered)->AT_COMMON_SIZES;
69 
FopenFgetsFclose(benchmark::State & state,bool no_locking)70 static void FopenFgetsFclose(benchmark::State& state, bool no_locking) {
71   char buf[1024];
72   while (state.KeepRunning()) {
73     FILE* fp = fopen("/proc/version", "re");
74     if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER);
75     if (fgets(buf, sizeof(buf), fp) == nullptr) abort();
76     fclose(fp);
77   }
78 }
79 
BM_stdio_fopen_fgets_fclose_locking(benchmark::State & state)80 static void BM_stdio_fopen_fgets_fclose_locking(benchmark::State& state) {
81   FopenFgetsFclose(state, false);
82 }
83 BENCHMARK(BM_stdio_fopen_fgets_fclose_locking);
84 
BM_stdio_fopen_fgets_fclose_no_locking(benchmark::State & state)85 void BM_stdio_fopen_fgets_fclose_no_locking(benchmark::State& state) {
86   FopenFgetsFclose(state, true);
87 }
88 BENCHMARK(BM_stdio_fopen_fgets_fclose_no_locking);
89