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 | |
| 17 | #include "benchmark.h" |
| 18 | |
| 19 | #include <stdio.h> |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame^] | 20 | #include <stdio_ext.h> |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 21 | |
| 22 | #define KB 1024 |
| 23 | #define MB 1024*KB |
| 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 | |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 29 | template <typename Fn> |
| 30 | static void ReadWriteTest(int iters, int chunk_size, Fn f, bool buffered) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 31 | StopBenchmarkTiming(); |
| 32 | FILE* fp = fopen("/dev/zero", "rw"); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame^] | 33 | __fsetlocking(fp, FSETLOCKING_BYCALLER); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 34 | char* buf = new char[chunk_size]; |
| 35 | StartBenchmarkTiming(); |
| 36 | |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 37 | if (!buffered) { |
| 38 | setvbuf(fp, 0, _IONBF, 0); |
| 39 | } |
| 40 | |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 41 | for (int i = 0; i < iters; ++i) { |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 42 | f(buf, chunk_size, 1, fp); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | StopBenchmarkTiming(); |
| 46 | SetBenchmarkBytesProcessed(int64_t(iters) * int64_t(chunk_size)); |
| 47 | delete[] buf; |
| 48 | fclose(fp); |
| 49 | } |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 50 | |
| 51 | static void BM_stdio_fread(int iters, int chunk_size) { |
| 52 | ReadWriteTest(iters, chunk_size, fread, true); |
| 53 | } |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 54 | BENCHMARK(BM_stdio_fread)->AT_COMMON_SIZES; |
| 55 | |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 56 | static void BM_stdio_fwrite(int iters, int chunk_size) { |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 57 | ReadWriteTest(iters, chunk_size, fwrite, true); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 58 | } |
| 59 | BENCHMARK(BM_stdio_fwrite)->AT_COMMON_SIZES; |
Elliott Hughes | 47dc7c9 | 2014-12-01 13:12:18 -0800 | [diff] [blame] | 60 | |
| 61 | static void BM_stdio_fread_unbuffered(int iters, int chunk_size) { |
| 62 | ReadWriteTest(iters, chunk_size, fread, false); |
| 63 | } |
| 64 | BENCHMARK(BM_stdio_fread_unbuffered)->AT_COMMON_SIZES; |
| 65 | |
| 66 | static void BM_stdio_fwrite_unbuffered(int iters, int chunk_size) { |
| 67 | ReadWriteTest(iters, chunk_size, fwrite, false); |
| 68 | } |
| 69 | BENCHMARK(BM_stdio_fwrite_unbuffered)->AT_COMMON_SIZES; |
Elliott Hughes | 1cf32f8 | 2015-01-16 17:08:31 -0800 | [diff] [blame] | 70 | |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame^] | 71 | static void FopenFgetsFclose(int iters, bool no_locking) { |
Elliott Hughes | 1cf32f8 | 2015-01-16 17:08:31 -0800 | [diff] [blame] | 72 | char buf[1024]; |
| 73 | for (int i = 0; i < iters; ++i) { |
| 74 | FILE* fp = fopen("/proc/version", "re"); |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame^] | 75 | if (no_locking) __fsetlocking(fp, FSETLOCKING_BYCALLER); |
Elliott Hughes | 1cf32f8 | 2015-01-16 17:08:31 -0800 | [diff] [blame] | 76 | fgets(buf, sizeof(buf), fp); |
| 77 | fclose(fp); |
| 78 | } |
| 79 | } |
Elliott Hughes | 8c4994b | 2015-01-20 18:09:05 -0800 | [diff] [blame^] | 80 | |
| 81 | static void BM_stdio_fopen_fgets_fclose_locking(int iters) { |
| 82 | FopenFgetsFclose(iters, false); |
| 83 | } |
| 84 | BENCHMARK(BM_stdio_fopen_fgets_fclose_locking); |
| 85 | |
| 86 | static void BM_stdio_fopen_fgets_fclose_no_locking(int iters) { |
| 87 | FopenFgetsFclose(iters, true); |
| 88 | } |
| 89 | BENCHMARK(BM_stdio_fopen_fgets_fclose_no_locking); |