| 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) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 26 | timespec t; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 27 | while (state.KeepRunning()) { | 
| Elliott Hughes | 7634db5 | 2014-06-09 18:35:21 -0700 | [diff] [blame] | 28 | clock_gettime(CLOCK_MONOTONIC, &t); | 
|  | 29 | } | 
| Elliott Hughes | 7634db5 | 2014-06-09 18:35:21 -0700 | [diff] [blame] | 30 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 31 | BIONIC_BENCHMARK(BM_time_clock_gettime); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 32 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 33 | static void BM_time_clock_gettime_syscall(benchmark::State& state) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 34 | timespec t; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 35 | while (state.KeepRunning()) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 36 | syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &t); | 
|  | 37 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 38 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 39 | BIONIC_BENCHMARK(BM_time_clock_gettime_syscall); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 40 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 41 | static void BM_time_gettimeofday(benchmark::State& state) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 42 | timeval tv; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 43 | while (state.KeepRunning()) { | 
| Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 44 | gettimeofday(&tv, nullptr); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 45 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 46 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 47 | BIONIC_BENCHMARK(BM_time_gettimeofday); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 48 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 49 | void BM_time_gettimeofday_syscall(benchmark::State& state) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 50 | timeval tv; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 51 | while (state.KeepRunning()) { | 
| Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 52 | syscall(__NR_gettimeofday, &tv, nullptr); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 53 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 54 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 55 | BIONIC_BENCHMARK(BM_time_gettimeofday_syscall); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 56 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 57 | void BM_time_time(benchmark::State& state) { | 
|  | 58 | while (state.KeepRunning()) { | 
| Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 59 | time(nullptr); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 60 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 61 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 62 | BIONIC_BENCHMARK(BM_time_time); | 
| Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 63 |  | 
|  | 64 | void BM_time_localtime(benchmark::State& state) { | 
|  | 65 | time_t t = time(nullptr); | 
|  | 66 | while (state.KeepRunning()) { | 
|  | 67 | localtime(&t); | 
|  | 68 | } | 
|  | 69 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 70 | BIONIC_BENCHMARK(BM_time_localtime); | 
| Elliott Hughes | ea87716 | 2017-01-11 14:34:16 -0800 | [diff] [blame] | 71 |  | 
|  | 72 | void BM_time_localtime_r(benchmark::State& state) { | 
|  | 73 | time_t t = time(nullptr); | 
|  | 74 | while (state.KeepRunning()) { | 
|  | 75 | struct tm tm; | 
|  | 76 | localtime_r(&t, &tm); | 
|  | 77 | } | 
|  | 78 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 79 | BIONIC_BENCHMARK(BM_time_localtime_r); |