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 | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 29 | FILE* fp = fopen("/dev/zero", "rw"); |
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 | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 38 | f(buf, chunk_size, 1, fp); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 41 | state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(chunk_size)); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 42 | delete[] buf; |
| 43 | fclose(fp); |
| 44 | } |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 45 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 46 | void BM_stdio_fread(benchmark::State& state) { |
| 47 | ReadWriteTest(state, fread, true); |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 48 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 49 | BIONIC_BENCHMARK(BM_stdio_fread); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 50 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 51 | void BM_stdio_fwrite(benchmark::State& state) { |
| 52 | ReadWriteTest(state, fwrite, true); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 53 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 54 | BIONIC_BENCHMARK(BM_stdio_fwrite); |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 55 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 56 | void BM_stdio_fread_unbuffered(benchmark::State& state) { |
| 57 | ReadWriteTest(state, fread, false); |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 58 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 59 | BIONIC_BENCHMARK(BM_stdio_fread_unbuffered); |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 60 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 61 | void BM_stdio_fwrite_unbuffered(benchmark::State& state) { |
| 62 | ReadWriteTest(state, fwrite, false); |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 63 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 64 | BIONIC_BENCHMARK(BM_stdio_fwrite_unbuffered); |
Elliott Hughes | 1cf32f8 | 2015-01-16 17:08:31 -0800 | [diff] [blame] | 65 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 66 | static void FopenFgetsFclose(benchmark::State& state, bool no_locking) { |
Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 67 | size_t nbytes = state.range(0); |
| 68 | char buf[nbytes]; |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 69 | while (state.KeepRunning()) { |
Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 70 | FILE* fp = fopen("/dev/zero", "re"); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 71 | if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER); |
Anders Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 72 | if (fgets(buf, sizeof(buf), fp) == nullptr) { |
| 73 | errx(1, "ERROR: fgets of %zu bytes failed.", nbytes); |
| 74 | } |
Elliott Hughes | 1cf32f8 | 2015-01-16 17:08:31 -0800 | [diff] [blame] | 75 | fclose(fp); |
| 76 | } |
| 77 | } |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 78 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 79 | static void BM_stdio_fopen_fgets_fclose_locking(benchmark::State& state) { |
| 80 | FopenFgetsFclose(state, false); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 81 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 82 | BIONIC_BENCHMARK(BM_stdio_fopen_fgets_fclose_locking); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 83 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 84 | void BM_stdio_fopen_fgets_fclose_no_locking(benchmark::State& state) { |
| 85 | FopenFgetsFclose(state, true); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame] | 86 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 87 | BIONIC_BENCHMARK(BM_stdio_fopen_fgets_fclose_no_locking); |
Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 88 | |
| 89 | static void FopenFgetcFclose(benchmark::State& state, bool no_locking) { |
| 90 | size_t nbytes = state.range(0); |
| 91 | while (state.KeepRunning()) { |
| 92 | FILE* fp = fopen("/dev/zero", "re"); |
| 93 | if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER); |
| 94 | volatile int c __attribute__((unused)); |
| 95 | for (size_t i = 0; i < nbytes; ++i) { |
| 96 | c = fgetc(fp); |
| 97 | } |
| 98 | fclose(fp); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | static void BM_stdio_fopen_fgetc_fclose_locking(benchmark::State& state) { |
| 103 | FopenFgetcFclose(state, false); |
| 104 | } |
| 105 | BIONIC_BENCHMARK(BM_stdio_fopen_fgetc_fclose_locking); |
| 106 | |
| 107 | void BM_stdio_fopen_fgetc_fclose_no_locking(benchmark::State& state) { |
| 108 | FopenFgetcFclose(state, true); |
| 109 | } |
| 110 | BIONIC_BENCHMARK(BM_stdio_fopen_fgetc_fclose_no_locking); |
| 111 | |