| Elliott Hughes | 4a05bef | 2013-03-11 17:17:02 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2013 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 | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 17 | #include <sys/syscall.h> | 
| Elliott Hughes | 212e0e3 | 2014-12-01 16:43:51 -0800 | [diff] [blame] | 18 | #include <sys/time.h> | 
| Elliott Hughes | 4a05bef | 2013-03-11 17:17:02 -0700 | [diff] [blame] | 19 | #include <time.h> | 
| Dan Albert | 3fe1515 | 2015-08-11 16:46:26 -0700 | [diff] [blame] | 20 | #include <unistd.h> | 
| Elliott Hughes | 4a05bef | 2013-03-11 17:17:02 -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" | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 24 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 25 | static void BM_time_clock_gettime(benchmark::State& state) { | 
| Mark Salyzyn | 6ffa10f | 2017-11-07 13:47:48 -0800 | [diff] [blame] | 26 | // CLOCK_MONOTONIC is required supported in vdso | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 27 | timespec t; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 28 | while (state.KeepRunning()) { | 
| Elliott Hughes | 7634db5 | 2014-06-09 18:35:21 -0700 | [diff] [blame] | 29 | clock_gettime(CLOCK_MONOTONIC, &t); | 
|  | 30 | } | 
| Elliott Hughes | 7634db5 | 2014-06-09 18:35:21 -0700 | [diff] [blame] | 31 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 32 | BIONIC_BENCHMARK(BM_time_clock_gettime); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 33 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 34 | static void BM_time_clock_gettime_syscall(benchmark::State& state) { | 
| Mark Salyzyn | 6ffa10f | 2017-11-07 13:47:48 -0800 | [diff] [blame] | 35 | // CLOCK_MONOTONIC is required supported in vdso | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 36 | timespec t; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 37 | while (state.KeepRunning()) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 38 | syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &t); | 
|  | 39 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 40 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 41 | BIONIC_BENCHMARK(BM_time_clock_gettime_syscall); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 42 |  | 
| Mark Salyzyn | 6ffa10f | 2017-11-07 13:47:48 -0800 | [diff] [blame] | 43 | static void BM_time_clock_gettime_MONOTONIC_COARSE(benchmark::State& state) { | 
|  | 44 | // CLOCK_MONOTONIC_COARSE is required supported in vdso | 
|  | 45 | timespec t; | 
|  | 46 | while (state.KeepRunning()) { | 
|  | 47 | clock_gettime(CLOCK_MONOTONIC_COARSE, &t); | 
|  | 48 | } | 
|  | 49 | } | 
|  | 50 | BIONIC_BENCHMARK(BM_time_clock_gettime_MONOTONIC_COARSE); | 
|  | 51 |  | 
|  | 52 | static void BM_time_clock_gettime_MONOTONIC_RAW(benchmark::State& state) { | 
|  | 53 | // CLOCK_MONOTONIC_RAW is required supported in vdso | 
|  | 54 | timespec t; | 
|  | 55 | while (state.KeepRunning()) { | 
|  | 56 | clock_gettime(CLOCK_MONOTONIC_RAW, &t); | 
|  | 57 | } | 
|  | 58 | } | 
|  | 59 | BIONIC_BENCHMARK(BM_time_clock_gettime_MONOTONIC_RAW); | 
|  | 60 |  | 
|  | 61 | static void BM_time_clock_gettime_REALTIME(benchmark::State& state) { | 
|  | 62 | // CLOCK_REALTIME is required supported in vdso | 
|  | 63 | timespec t; | 
|  | 64 | while (state.KeepRunning()) { | 
|  | 65 | clock_gettime(CLOCK_REALTIME, &t); | 
|  | 66 | } | 
|  | 67 | } | 
|  | 68 | BIONIC_BENCHMARK(BM_time_clock_gettime_REALTIME); | 
|  | 69 |  | 
|  | 70 | static void BM_time_clock_gettime_REALTIME_COARSE(benchmark::State& state) { | 
|  | 71 | // CLOCK_REALTIME_COARSE is required supported in vdso | 
|  | 72 | timespec t; | 
|  | 73 | while (state.KeepRunning()) { | 
|  | 74 | clock_gettime(CLOCK_REALTIME_COARSE, &t); | 
|  | 75 | } | 
|  | 76 | } | 
|  | 77 | BIONIC_BENCHMARK(BM_time_clock_gettime_REALTIME_COARSE); | 
|  | 78 |  | 
|  | 79 | static void BM_time_clock_gettime_BOOTTIME(benchmark::State& state) { | 
|  | 80 | // CLOCK_BOOTTIME is optionally supported in vdso | 
|  | 81 | timespec t; | 
|  | 82 | while (state.KeepRunning()) { | 
|  | 83 | clock_gettime(CLOCK_BOOTTIME, &t); | 
|  | 84 | } | 
|  | 85 | } | 
|  | 86 | BIONIC_BENCHMARK(BM_time_clock_gettime_BOOTTIME); | 
|  | 87 |  | 
| Mark Salyzyn | 6f9c35d | 2017-11-07 13:48:46 -0800 | [diff] [blame] | 88 | static void BM_time_clock_getres(benchmark::State& state) { | 
|  | 89 | // CLOCK_MONOTONIC is required supported in vdso | 
|  | 90 | timespec t; | 
|  | 91 | while (state.KeepRunning()) { | 
|  | 92 | clock_getres(CLOCK_MONOTONIC, &t); | 
|  | 93 | } | 
|  | 94 | } | 
|  | 95 | BIONIC_BENCHMARK(BM_time_clock_getres); | 
|  | 96 |  | 
|  | 97 | static void BM_time_clock_getres_syscall(benchmark::State& state) { | 
|  | 98 | // CLOCK_MONOTONIC is required supported in vdso | 
|  | 99 | timespec t; | 
|  | 100 | while (state.KeepRunning()) { | 
|  | 101 | syscall(__NR_clock_getres, CLOCK_MONOTONIC, &t); | 
|  | 102 | } | 
|  | 103 | } | 
|  | 104 | BIONIC_BENCHMARK(BM_time_clock_getres_syscall); | 
|  | 105 |  | 
|  | 106 | static void BM_time_clock_getres_MONOTONIC_COARSE(benchmark::State& state) { | 
|  | 107 | // CLOCK_MONOTONIC_COARSE is required supported in vdso | 
|  | 108 | timespec t; | 
|  | 109 | while (state.KeepRunning()) { | 
|  | 110 | clock_getres(CLOCK_MONOTONIC_COARSE, &t); | 
|  | 111 | } | 
|  | 112 | } | 
|  | 113 | BIONIC_BENCHMARK(BM_time_clock_getres_MONOTONIC_COARSE); | 
|  | 114 |  | 
|  | 115 | static void BM_time_clock_getres_MONOTONIC_RAW(benchmark::State& state) { | 
|  | 116 | // CLOCK_MONOTONIC_RAW is required supported in vdso | 
|  | 117 | timespec t; | 
|  | 118 | while (state.KeepRunning()) { | 
|  | 119 | clock_getres(CLOCK_MONOTONIC_RAW, &t); | 
|  | 120 | } | 
|  | 121 | } | 
|  | 122 | BIONIC_BENCHMARK(BM_time_clock_getres_MONOTONIC_RAW); | 
|  | 123 |  | 
|  | 124 | static void BM_time_clock_getres_REALTIME(benchmark::State& state) { | 
|  | 125 | // CLOCK_REALTIME is required supported in vdso | 
|  | 126 | timespec t; | 
|  | 127 | while (state.KeepRunning()) { | 
|  | 128 | clock_getres(CLOCK_REALTIME, &t); | 
|  | 129 | } | 
|  | 130 | } | 
|  | 131 | BIONIC_BENCHMARK(BM_time_clock_getres_REALTIME); | 
|  | 132 |  | 
|  | 133 | static void BM_time_clock_getres_REALTIME_COARSE(benchmark::State& state) { | 
|  | 134 | // CLOCK_REALTIME_COARSE is required supported in vdso | 
|  | 135 | timespec t; | 
|  | 136 | while (state.KeepRunning()) { | 
|  | 137 | clock_getres(CLOCK_REALTIME_COARSE, &t); | 
|  | 138 | } | 
|  | 139 | } | 
|  | 140 | BIONIC_BENCHMARK(BM_time_clock_getres_REALTIME_COARSE); | 
|  | 141 |  | 
|  | 142 | static void BM_time_clock_getres_BOOTTIME(benchmark::State& state) { | 
|  | 143 | // CLOCK_BOOTTIME is optionally supported in vdso | 
|  | 144 | timespec t; | 
|  | 145 | while (state.KeepRunning()) { | 
|  | 146 | clock_getres(CLOCK_BOOTTIME, &t); | 
|  | 147 | } | 
|  | 148 | } | 
|  | 149 | BIONIC_BENCHMARK(BM_time_clock_getres_BOOTTIME); | 
|  | 150 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 151 | static void BM_time_gettimeofday(benchmark::State& state) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 152 | timeval tv; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 153 | while (state.KeepRunning()) { | 
| Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 154 | gettimeofday(&tv, nullptr); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 155 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 156 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 157 | BIONIC_BENCHMARK(BM_time_gettimeofday); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 158 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 159 | void BM_time_gettimeofday_syscall(benchmark::State& state) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 160 | timeval tv; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 161 | while (state.KeepRunning()) { | 
| Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 162 | syscall(__NR_gettimeofday, &tv, nullptr); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 163 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 164 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 165 | BIONIC_BENCHMARK(BM_time_gettimeofday_syscall); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 166 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 167 | void BM_time_time(benchmark::State& state) { | 
|  | 168 | while (state.KeepRunning()) { | 
| Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 169 | time(nullptr); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 170 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 171 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 172 | BIONIC_BENCHMARK(BM_time_time); | 
| Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 173 |  | 
|  | 174 | void BM_time_localtime(benchmark::State& state) { | 
|  | 175 | time_t t = time(nullptr); | 
|  | 176 | while (state.KeepRunning()) { | 
|  | 177 | localtime(&t); | 
|  | 178 | } | 
|  | 179 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 180 | BIONIC_BENCHMARK(BM_time_localtime); | 
| Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 181 |  | 
|  | 182 | void BM_time_localtime_r(benchmark::State& state) { | 
|  | 183 | time_t t = time(nullptr); | 
|  | 184 | while (state.KeepRunning()) { | 
|  | 185 | struct tm tm; | 
|  | 186 | localtime_r(&t, &tm); | 
|  | 187 | } | 
|  | 188 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 189 | BIONIC_BENCHMARK(BM_time_localtime_r); |