blob: 70cc662aff189b2830d89ef2b625c616c77c8eca [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
Colin Crossdaa6b822021-12-15 14:26:43 -080025// Musl doesn't define __NR_gettimeofday, __NR_clock_gettime32, __NR_gettimeofday_time32 or
26// __NR_clock_getres on 32-bit architectures.
27#if !defined(__NR_gettimeofday)
28#define __NR_gettimeofday __NR_gettimeofday_time32
29#endif
30#if !defined(__NR_clock_gettime)
31#define __NR_clock_gettime __NR_clock_gettime32
32#endif
33#if !defined(__NR_gettimeofday)
34#define __NR_gettimeofday __NR_gettimeofday_time32
35#endif
36#if !defined(__NR_clock_getres)
37#define __NR_clock_getres __NR_clock_getres_time32
38#endif
39
Elliott Hughes281e06b2016-02-17 10:23:52 -080040static void BM_time_clock_gettime(benchmark::State& state) {
Mark Salyzyn6ffa10f2017-11-07 13:47:48 -080041 // CLOCK_MONOTONIC is required supported in vdso
Elliott Hughes625993d2014-07-15 16:53:13 -070042 timespec t;
Elliott Hughes281e06b2016-02-17 10:23:52 -080043 while (state.KeepRunning()) {
Elliott Hughes7634db52014-06-09 18:35:21 -070044 clock_gettime(CLOCK_MONOTONIC, &t);
45 }
Elliott Hughes7634db52014-06-09 18:35:21 -070046}
Anders Lewisa7b0f882017-07-24 20:01:13 -070047BIONIC_BENCHMARK(BM_time_clock_gettime);
Elliott Hughes625993d2014-07-15 16:53:13 -070048
Elliott Hughes281e06b2016-02-17 10:23:52 -080049static void BM_time_clock_gettime_syscall(benchmark::State& state) {
Mark Salyzyn6ffa10f2017-11-07 13:47:48 -080050 // CLOCK_MONOTONIC is required supported in vdso
Elliott Hughes625993d2014-07-15 16:53:13 -070051 timespec t;
Elliott Hughes281e06b2016-02-17 10:23:52 -080052 while (state.KeepRunning()) {
Elliott Hughes625993d2014-07-15 16:53:13 -070053 syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &t);
54 }
Elliott Hughes625993d2014-07-15 16:53:13 -070055}
Anders Lewisa7b0f882017-07-24 20:01:13 -070056BIONIC_BENCHMARK(BM_time_clock_gettime_syscall);
Elliott Hughes625993d2014-07-15 16:53:13 -070057
Mark Salyzyn6ffa10f2017-11-07 13:47:48 -080058static void BM_time_clock_gettime_MONOTONIC_COARSE(benchmark::State& state) {
59 // CLOCK_MONOTONIC_COARSE is required supported in vdso
60 timespec t;
61 while (state.KeepRunning()) {
62 clock_gettime(CLOCK_MONOTONIC_COARSE, &t);
63 }
64}
65BIONIC_BENCHMARK(BM_time_clock_gettime_MONOTONIC_COARSE);
66
67static void BM_time_clock_gettime_MONOTONIC_RAW(benchmark::State& state) {
68 // CLOCK_MONOTONIC_RAW is required supported in vdso
69 timespec t;
70 while (state.KeepRunning()) {
71 clock_gettime(CLOCK_MONOTONIC_RAW, &t);
72 }
73}
74BIONIC_BENCHMARK(BM_time_clock_gettime_MONOTONIC_RAW);
75
76static void BM_time_clock_gettime_REALTIME(benchmark::State& state) {
77 // CLOCK_REALTIME is required supported in vdso
78 timespec t;
79 while (state.KeepRunning()) {
80 clock_gettime(CLOCK_REALTIME, &t);
81 }
82}
83BIONIC_BENCHMARK(BM_time_clock_gettime_REALTIME);
84
85static void BM_time_clock_gettime_REALTIME_COARSE(benchmark::State& state) {
86 // CLOCK_REALTIME_COARSE is required supported in vdso
87 timespec t;
88 while (state.KeepRunning()) {
89 clock_gettime(CLOCK_REALTIME_COARSE, &t);
90 }
91}
92BIONIC_BENCHMARK(BM_time_clock_gettime_REALTIME_COARSE);
93
94static void BM_time_clock_gettime_BOOTTIME(benchmark::State& state) {
95 // CLOCK_BOOTTIME is optionally supported in vdso
96 timespec t;
97 while (state.KeepRunning()) {
98 clock_gettime(CLOCK_BOOTTIME, &t);
99 }
100}
101BIONIC_BENCHMARK(BM_time_clock_gettime_BOOTTIME);
102
Mark Salyzyn6f9c35d2017-11-07 13:48:46 -0800103static void BM_time_clock_getres(benchmark::State& state) {
104 // CLOCK_MONOTONIC is required supported in vdso
105 timespec t;
106 while (state.KeepRunning()) {
107 clock_getres(CLOCK_MONOTONIC, &t);
108 }
109}
110BIONIC_BENCHMARK(BM_time_clock_getres);
111
112static void BM_time_clock_getres_syscall(benchmark::State& state) {
113 // CLOCK_MONOTONIC is required supported in vdso
114 timespec t;
115 while (state.KeepRunning()) {
116 syscall(__NR_clock_getres, CLOCK_MONOTONIC, &t);
117 }
118}
119BIONIC_BENCHMARK(BM_time_clock_getres_syscall);
120
121static void BM_time_clock_getres_MONOTONIC_COARSE(benchmark::State& state) {
122 // CLOCK_MONOTONIC_COARSE is required supported in vdso
123 timespec t;
124 while (state.KeepRunning()) {
125 clock_getres(CLOCK_MONOTONIC_COARSE, &t);
126 }
127}
128BIONIC_BENCHMARK(BM_time_clock_getres_MONOTONIC_COARSE);
129
130static void BM_time_clock_getres_MONOTONIC_RAW(benchmark::State& state) {
131 // CLOCK_MONOTONIC_RAW is required supported in vdso
132 timespec t;
133 while (state.KeepRunning()) {
134 clock_getres(CLOCK_MONOTONIC_RAW, &t);
135 }
136}
137BIONIC_BENCHMARK(BM_time_clock_getres_MONOTONIC_RAW);
138
139static void BM_time_clock_getres_REALTIME(benchmark::State& state) {
140 // CLOCK_REALTIME is required supported in vdso
141 timespec t;
142 while (state.KeepRunning()) {
143 clock_getres(CLOCK_REALTIME, &t);
144 }
145}
146BIONIC_BENCHMARK(BM_time_clock_getres_REALTIME);
147
148static void BM_time_clock_getres_REALTIME_COARSE(benchmark::State& state) {
149 // CLOCK_REALTIME_COARSE is required supported in vdso
150 timespec t;
151 while (state.KeepRunning()) {
152 clock_getres(CLOCK_REALTIME_COARSE, &t);
153 }
154}
155BIONIC_BENCHMARK(BM_time_clock_getres_REALTIME_COARSE);
156
157static void BM_time_clock_getres_BOOTTIME(benchmark::State& state) {
158 // CLOCK_BOOTTIME is optionally supported in vdso
159 timespec t;
160 while (state.KeepRunning()) {
161 clock_getres(CLOCK_BOOTTIME, &t);
162 }
163}
164BIONIC_BENCHMARK(BM_time_clock_getres_BOOTTIME);
165
Elliott Hughes281e06b2016-02-17 10:23:52 -0800166static void BM_time_gettimeofday(benchmark::State& state) {
Elliott Hughes625993d2014-07-15 16:53:13 -0700167 timeval tv;
Elliott Hughes281e06b2016-02-17 10:23:52 -0800168 while (state.KeepRunning()) {
Elliott Hughesea877162017-01-11 14:34:16 -0800169 gettimeofday(&tv, 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_gettimeofday);
Elliott Hughes625993d2014-07-15 16:53:13 -0700173
Elliott Hughes281e06b2016-02-17 10:23:52 -0800174void BM_time_gettimeofday_syscall(benchmark::State& state) {
Elliott Hughes625993d2014-07-15 16:53:13 -0700175 timeval tv;
Elliott Hughes281e06b2016-02-17 10:23:52 -0800176 while (state.KeepRunning()) {
Elliott Hughesea877162017-01-11 14:34:16 -0800177 syscall(__NR_gettimeofday, &tv, nullptr);
Elliott Hughes625993d2014-07-15 16:53:13 -0700178 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700179}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700180BIONIC_BENCHMARK(BM_time_gettimeofday_syscall);
Elliott Hughes625993d2014-07-15 16:53:13 -0700181
Elliott Hughes281e06b2016-02-17 10:23:52 -0800182void BM_time_time(benchmark::State& state) {
183 while (state.KeepRunning()) {
Elliott Hughesea877162017-01-11 14:34:16 -0800184 time(nullptr);
Elliott Hughes625993d2014-07-15 16:53:13 -0700185 }
Elliott Hughes625993d2014-07-15 16:53:13 -0700186}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700187BIONIC_BENCHMARK(BM_time_time);
Elliott Hughesea877162017-01-11 14:34:16 -0800188
189void BM_time_localtime(benchmark::State& state) {
190 time_t t = time(nullptr);
191 while (state.KeepRunning()) {
192 localtime(&t);
193 }
194}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700195BIONIC_BENCHMARK(BM_time_localtime);
Elliott Hughesea877162017-01-11 14:34:16 -0800196
197void BM_time_localtime_r(benchmark::State& state) {
198 time_t t = time(nullptr);
199 while (state.KeepRunning()) {
200 struct tm tm;
201 localtime_r(&t, &tm);
202 }
203}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700204BIONIC_BENCHMARK(BM_time_localtime_r);
Elliott Hughese4d5efe2021-11-18 17:57:38 -0800205
206void BM_time_strftime(benchmark::State& state) {
207 char buf[128];
208 time_t t = 0;
209 struct tm* tm = gmtime(&t);
210 while (state.KeepRunning()) {
211 strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
212 }
213}
214BIONIC_BENCHMARK(BM_time_strftime);