blob: a765e3efe22d48d113ce2339821e7e724904ef6a [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
Mark Salyzyn6f9c35d2017-11-07 13:48:46 -080088static 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}
95BIONIC_BENCHMARK(BM_time_clock_getres);
96
97static 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}
104BIONIC_BENCHMARK(BM_time_clock_getres_syscall);
105
106static 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}
113BIONIC_BENCHMARK(BM_time_clock_getres_MONOTONIC_COARSE);
114
115static 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}
122BIONIC_BENCHMARK(BM_time_clock_getres_MONOTONIC_RAW);
123
124static 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}
131BIONIC_BENCHMARK(BM_time_clock_getres_REALTIME);
132
133static 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}
140BIONIC_BENCHMARK(BM_time_clock_getres_REALTIME_COARSE);
141
142static 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}
149BIONIC_BENCHMARK(BM_time_clock_getres_BOOTTIME);
150
Elliott Hughes281e06b2016-02-17 10:23:52 -0800151static void BM_time_gettimeofday(benchmark::State& state) {
Elliott Hughes625993d2014-07-15 16:53:13 -0700152 timeval tv;
Elliott Hughes281e06b2016-02-17 10:23:52 -0800153 while (state.KeepRunning()) {
Elliott Hughesea877162017-01-11 14:34:16 -0800154 gettimeofday(&tv, nullptr);
Elliott Hughes625993d2014-07-15 16:53:13 -0700155 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700156}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700157BIONIC_BENCHMARK(BM_time_gettimeofday);
Elliott Hughes625993d2014-07-15 16:53:13 -0700158
Elliott Hughes281e06b2016-02-17 10:23:52 -0800159void BM_time_gettimeofday_syscall(benchmark::State& state) {
Elliott Hughes625993d2014-07-15 16:53:13 -0700160 timeval tv;
Elliott Hughes281e06b2016-02-17 10:23:52 -0800161 while (state.KeepRunning()) {
Elliott Hughesea877162017-01-11 14:34:16 -0800162 syscall(__NR_gettimeofday, &tv, nullptr);
Elliott Hughes625993d2014-07-15 16:53:13 -0700163 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700164}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700165BIONIC_BENCHMARK(BM_time_gettimeofday_syscall);
Elliott Hughes625993d2014-07-15 16:53:13 -0700166
Elliott Hughes281e06b2016-02-17 10:23:52 -0800167void BM_time_time(benchmark::State& state) {
168 while (state.KeepRunning()) {
Elliott Hughesea877162017-01-11 14:34:16 -0800169 time(nullptr);
Elliott Hughes625993d2014-07-15 16:53:13 -0700170 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700171}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700172BIONIC_BENCHMARK(BM_time_time);
Elliott Hughesea877162017-01-11 14:34:16 -0800173
174void BM_time_localtime(benchmark::State& state) {
175 time_t t = time(nullptr);
176 while (state.KeepRunning()) {
177 localtime(&t);
178 }
179}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700180BIONIC_BENCHMARK(BM_time_localtime);
Elliott Hughesea877162017-01-11 14:34:16 -0800181
182void 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 Lewisa7b0f882017-07-24 20:01:13 -0700189BIONIC_BENCHMARK(BM_time_localtime_r);
Elliott Hughese4d5efe2021-11-18 17:57:38 -0800190
191void BM_time_strftime(benchmark::State& state) {
192 char buf[128];
193 time_t t = 0;
194 struct tm* tm = gmtime(&t);
195 while (state.KeepRunning()) {
196 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
197 }
198}
199BIONIC_BENCHMARK(BM_time_strftime);