blob: f44c2802fc823a6b984a0c9a13e3075a8574b699 [file] [log] [blame]
Elliott Hughes4a05bef2013-03-11 17:17:02 -07001/*
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 Hughes625993d2014-07-15 16:53:13 -070017#include <sys/syscall.h>
Elliott Hughes212e0e32014-12-01 16:43:51 -080018#include <sys/time.h>
Elliott Hughes4a05bef2013-03-11 17:17:02 -070019#include <time.h>
Dan Albert3fe15152015-08-11 16:46:26 -070020#include <unistd.h>
Elliott Hughes4a05bef2013-03-11 17:17:02 -070021
Elliott Hughes281e06b2016-02-17 10:23:52 -080022#include <benchmark/benchmark.h>
Anders Lewisa7b0f882017-07-24 20:01:13 -070023#include "util.h"
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080024
Elliott Hughes281e06b2016-02-17 10:23:52 -080025static void BM_time_clock_gettime(benchmark::State& state) {
Mark Salyzyn6ffa10f2017-11-07 13:47:48 -080026 // CLOCK_MONOTONIC is required supported in vdso
Elliott Hughes625993d2014-07-15 16:53:13 -070027 timespec t;
Elliott Hughes281e06b2016-02-17 10:23:52 -080028 while (state.KeepRunning()) {
Elliott Hughes7634db52014-06-09 18:35:21 -070029 clock_gettime(CLOCK_MONOTONIC, &t);
30 }
Elliott Hughes7634db52014-06-09 18:35:21 -070031}
Anders Lewisa7b0f882017-07-24 20:01:13 -070032BIONIC_BENCHMARK(BM_time_clock_gettime);
Elliott Hughes625993d2014-07-15 16:53:13 -070033
Elliott Hughes281e06b2016-02-17 10:23:52 -080034static void BM_time_clock_gettime_syscall(benchmark::State& state) {
Mark Salyzyn6ffa10f2017-11-07 13:47:48 -080035 // CLOCK_MONOTONIC is required supported in vdso
Elliott Hughes625993d2014-07-15 16:53:13 -070036 timespec t;
Elliott Hughes281e06b2016-02-17 10:23:52 -080037 while (state.KeepRunning()) {
Elliott Hughes625993d2014-07-15 16:53:13 -070038 syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &t);
39 }
Elliott Hughes625993d2014-07-15 16:53:13 -070040}
Anders Lewisa7b0f882017-07-24 20:01:13 -070041BIONIC_BENCHMARK(BM_time_clock_gettime_syscall);
Elliott Hughes625993d2014-07-15 16:53:13 -070042
Mark Salyzyn6ffa10f2017-11-07 13:47:48 -080043static 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}
50BIONIC_BENCHMARK(BM_time_clock_gettime_MONOTONIC_COARSE);
51
52static 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}
59BIONIC_BENCHMARK(BM_time_clock_gettime_MONOTONIC_RAW);
60
61static 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}
68BIONIC_BENCHMARK(BM_time_clock_gettime_REALTIME);
69
70static 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}
77BIONIC_BENCHMARK(BM_time_clock_gettime_REALTIME_COARSE);
78
79static 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}
86BIONIC_BENCHMARK(BM_time_clock_gettime_BOOTTIME);
87
Elliott Hughes281e06b2016-02-17 10:23:52 -080088static void BM_time_gettimeofday(benchmark::State& state) {
Elliott Hughes625993d2014-07-15 16:53:13 -070089 timeval tv;
Elliott Hughes281e06b2016-02-17 10:23:52 -080090 while (state.KeepRunning()) {
Elliott Hughesea877162017-01-11 14:34:16 -080091 gettimeofday(&tv, nullptr);
Elliott Hughes625993d2014-07-15 16:53:13 -070092 }
Elliott Hughes625993d2014-07-15 16:53:13 -070093}
Anders Lewisa7b0f882017-07-24 20:01:13 -070094BIONIC_BENCHMARK(BM_time_gettimeofday);
Elliott Hughes625993d2014-07-15 16:53:13 -070095
Elliott Hughes281e06b2016-02-17 10:23:52 -080096void BM_time_gettimeofday_syscall(benchmark::State& state) {
Elliott Hughes625993d2014-07-15 16:53:13 -070097 timeval tv;
Elliott Hughes281e06b2016-02-17 10:23:52 -080098 while (state.KeepRunning()) {
Elliott Hughesea877162017-01-11 14:34:16 -080099 syscall(__NR_gettimeofday, &tv, nullptr);
Elliott Hughes625993d2014-07-15 16:53:13 -0700100 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700101}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700102BIONIC_BENCHMARK(BM_time_gettimeofday_syscall);
Elliott Hughes625993d2014-07-15 16:53:13 -0700103
Elliott Hughes281e06b2016-02-17 10:23:52 -0800104void BM_time_time(benchmark::State& state) {
105 while (state.KeepRunning()) {
Elliott Hughesea877162017-01-11 14:34:16 -0800106 time(nullptr);
Elliott Hughes625993d2014-07-15 16:53:13 -0700107 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700108}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700109BIONIC_BENCHMARK(BM_time_time);
Elliott Hughesea877162017-01-11 14:34:16 -0800110
111void BM_time_localtime(benchmark::State& state) {
112 time_t t = time(nullptr);
113 while (state.KeepRunning()) {
114 localtime(&t);
115 }
116}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700117BIONIC_BENCHMARK(BM_time_localtime);
Elliott Hughesea877162017-01-11 14:34:16 -0800118
119void BM_time_localtime_r(benchmark::State& state) {
120 time_t t = time(nullptr);
121 while (state.KeepRunning()) {
122 struct tm tm;
123 localtime_r(&t, &tm);
124 }
125}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700126BIONIC_BENCHMARK(BM_time_localtime_r);