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
17 #include <sys/syscall.h>
18 #include <sys/time.h>
19 #include <time.h>
20 #include <unistd.h>
21
22 #include <benchmark/benchmark.h>
23 #include "util.h"
24
BM_time_clock_gettime(benchmark::State & state)25 static void BM_time_clock_gettime(benchmark::State& state) {
26 // CLOCK_MONOTONIC is required supported in vdso
27 timespec t;
28 while (state.KeepRunning()) {
29 clock_gettime(CLOCK_MONOTONIC, &t);
30 }
31 }
32 BIONIC_BENCHMARK(BM_time_clock_gettime);
33
BM_time_clock_gettime_syscall(benchmark::State & state)34 static void BM_time_clock_gettime_syscall(benchmark::State& state) {
35 // CLOCK_MONOTONIC is required supported in vdso
36 timespec t;
37 while (state.KeepRunning()) {
38 syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &t);
39 }
40 }
41 BIONIC_BENCHMARK(BM_time_clock_gettime_syscall);
42
BM_time_clock_gettime_MONOTONIC_COARSE(benchmark::State & state)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
BM_time_clock_gettime_MONOTONIC_RAW(benchmark::State & state)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
BM_time_clock_gettime_REALTIME(benchmark::State & state)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
BM_time_clock_gettime_REALTIME_COARSE(benchmark::State & state)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
BM_time_clock_gettime_BOOTTIME(benchmark::State & state)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
BM_time_clock_getres(benchmark::State & state)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
BM_time_clock_getres_syscall(benchmark::State & state)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
BM_time_clock_getres_MONOTONIC_COARSE(benchmark::State & state)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
BM_time_clock_getres_MONOTONIC_RAW(benchmark::State & state)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
BM_time_clock_getres_REALTIME(benchmark::State & state)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
BM_time_clock_getres_REALTIME_COARSE(benchmark::State & state)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
BM_time_clock_getres_BOOTTIME(benchmark::State & state)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
BM_time_gettimeofday(benchmark::State & state)151 static void BM_time_gettimeofday(benchmark::State& state) {
152 timeval tv;
153 while (state.KeepRunning()) {
154 gettimeofday(&tv, nullptr);
155 }
156 }
157 BIONIC_BENCHMARK(BM_time_gettimeofday);
158
BM_time_gettimeofday_syscall(benchmark::State & state)159 void BM_time_gettimeofday_syscall(benchmark::State& state) {
160 timeval tv;
161 while (state.KeepRunning()) {
162 syscall(__NR_gettimeofday, &tv, nullptr);
163 }
164 }
165 BIONIC_BENCHMARK(BM_time_gettimeofday_syscall);
166
BM_time_time(benchmark::State & state)167 void BM_time_time(benchmark::State& state) {
168 while (state.KeepRunning()) {
169 time(nullptr);
170 }
171 }
172 BIONIC_BENCHMARK(BM_time_time);
173
BM_time_localtime(benchmark::State & state)174 void BM_time_localtime(benchmark::State& state) {
175 time_t t = time(nullptr);
176 while (state.KeepRunning()) {
177 localtime(&t);
178 }
179 }
180 BIONIC_BENCHMARK(BM_time_localtime);
181
BM_time_localtime_r(benchmark::State & state)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 }
189 BIONIC_BENCHMARK(BM_time_localtime_r);
190