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