Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 17 | #include <stdio.h> |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 18 | #include <stdio_ext.h> |
Elliott Hughes | c217373 | 2015-05-13 13:18:04 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 20 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 21 | #include <benchmark/benchmark.h> |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 22 | #include "util.h" |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 23 | |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 24 | template <typename Fn> |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 25 | void ReadWriteTest(benchmark::State& state, Fn f, bool buffered) { |
Martijn Coenen | be763d8 | 2016-11-14 14:16:08 +0100 | [diff] [blame] | 26 | size_t chunk_size = state.range(0); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 27 | |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 28 | FILE* fp = fopen("/dev/zero", "rw"); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 29 | __fsetlocking(fp, FSETLOCKING_BYCALLER); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 30 | char* buf = new char[chunk_size]; |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 31 | |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 32 | if (!buffered) { |
| 33 | setvbuf(fp, 0, _IONBF, 0); |
| 34 | } |
| 35 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 36 | while (state.KeepRunning()) { |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 37 | f(buf, chunk_size, 1, fp); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 40 | state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(chunk_size)); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 41 | delete[] buf; |
| 42 | fclose(fp); |
| 43 | } |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 44 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 45 | void BM_stdio_fread(benchmark::State& state) { |
| 46 | ReadWriteTest(state, fread, true); |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 47 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 48 | BIONIC_BENCHMARK(BM_stdio_fread); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 50 | void BM_stdio_fwrite(benchmark::State& state) { |
| 51 | ReadWriteTest(state, fwrite, true); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 52 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 53 | BIONIC_BENCHMARK(BM_stdio_fwrite); |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 54 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 55 | void BM_stdio_fread_unbuffered(benchmark::State& state) { |
| 56 | ReadWriteTest(state, fread, false); |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 57 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 58 | BIONIC_BENCHMARK(BM_stdio_fread_unbuffered); |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 59 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 60 | void BM_stdio_fwrite_unbuffered(benchmark::State& state) { |
| 61 | ReadWriteTest(state, fwrite, false); |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 62 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 63 | BIONIC_BENCHMARK(BM_stdio_fwrite_unbuffered); |
Elliott Hughes | 1cf32f8 | 2015-01-16 17:08:31 -0800 | [diff] [blame] | 64 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 65 | static void FopenFgetsFclose(benchmark::State& state, bool no_locking) { |
Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame^] | 66 | size_t nbytes = state.range(0); |
| 67 | char buf[nbytes]; |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 68 | while (state.KeepRunning()) { |
Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame^] | 69 | FILE* fp = fopen("/dev/zero", "re"); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 70 | if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER); |
Elliott Hughes | c217373 | 2015-05-13 13:18:04 -0700 | [diff] [blame] | 71 | if (fgets(buf, sizeof(buf), fp) == nullptr) abort(); |
Elliott Hughes | 1cf32f8 | 2015-01-16 17:08:31 -0800 | [diff] [blame] | 72 | fclose(fp); |
| 73 | } |
| 74 | } |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 75 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 76 | static void BM_stdio_fopen_fgets_fclose_locking(benchmark::State& state) { |
| 77 | FopenFgetsFclose(state, false); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 78 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 79 | BIONIC_BENCHMARK(BM_stdio_fopen_fgets_fclose_locking); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 80 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 81 | void BM_stdio_fopen_fgets_fclose_no_locking(benchmark::State& state) { |
| 82 | FopenFgetsFclose(state, true); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 83 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 84 | BIONIC_BENCHMARK(BM_stdio_fopen_fgets_fclose_no_locking); |
Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame^] | 85 | |
| 86 | static 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 | |
| 99 | static void BM_stdio_fopen_fgetc_fclose_locking(benchmark::State& state) { |
| 100 | FopenFgetcFclose(state, false); |
| 101 | } |
| 102 | BIONIC_BENCHMARK(BM_stdio_fopen_fgetc_fclose_locking); |
| 103 | |
| 104 | void BM_stdio_fopen_fgetc_fclose_no_locking(benchmark::State& state) { |
| 105 | FopenFgetcFclose(state, true); |
| 106 | } |
| 107 | BIONIC_BENCHMARK(BM_stdio_fopen_fgetc_fclose_no_locking); |
| 108 | |