blob: b26e0ef1b19d460fd4555b21c135fc57b58bb16e [file] [log] [blame]
Vladimir Marko80afd022015-05-19 18:08:00 +01001/*
2 * Copyright (C) 2015 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 Hughesf3238322023-06-30 10:23:39 -070017#define _POSIX_THREAD_SAFE_FUNCTIONS // For mingw localtime_r().
18
David Sehr10db8fe2018-07-18 11:01:20 -070019#include "time_utils.h"
20
Vladimir Marko80afd022015-05-19 18:08:00 +010021#include <inttypes.h>
David Sehr10db8fe2018-07-18 11:01:20 -070022#include <stdio.h>
23
Hans Boehmfe76a622016-03-28 14:36:23 -070024#include <limits>
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include <sstream>
26
Andreas Gampe46ee31b2016-12-14 10:11:49 -080027#include "android-base/stringprintf.h"
28
David Sehr1979c642018-04-26 14:41:18 -070029#include "logging.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010030
Vladimir Markoce392002015-05-26 19:57:30 +010031#if defined(__APPLE__)
32#include <sys/time.h>
33#endif
34
Vladimir Marko80afd022015-05-19 18:08:00 +010035namespace art {
36
David Sehr10db8fe2018-07-18 11:01:20 -070037namespace {
38
39#if !defined(__linux__)
40int GetTimeOfDay(struct timeval* tv, struct timezone* tz) {
41#ifdef _WIN32
42 return mingw_gettimeofday(tv, tz);
43#else
44 return gettimeofday(tv, tz);
45#endif
46}
47#endif
48
49} // namespace
50
Andreas Gampe46ee31b2016-12-14 10:11:49 -080051using android::base::StringPrintf;
52
Vladimir Marko80afd022015-05-19 18:08:00 +010053std::string PrettyDuration(uint64_t nano_duration, size_t max_fraction_digits) {
54 if (nano_duration == 0) {
55 return "0";
56 } else {
57 return FormatDuration(nano_duration, GetAppropriateTimeUnit(nano_duration),
58 max_fraction_digits);
59 }
60}
61
62TimeUnit GetAppropriateTimeUnit(uint64_t nano_duration) {
63 const uint64_t one_sec = 1000 * 1000 * 1000;
64 const uint64_t one_ms = 1000 * 1000;
65 const uint64_t one_us = 1000;
66 if (nano_duration >= one_sec) {
67 return kTimeUnitSecond;
68 } else if (nano_duration >= one_ms) {
69 return kTimeUnitMillisecond;
70 } else if (nano_duration >= one_us) {
71 return kTimeUnitMicrosecond;
72 } else {
73 return kTimeUnitNanosecond;
74 }
75}
76
77uint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit) {
78 const uint64_t one_sec = 1000 * 1000 * 1000;
79 const uint64_t one_ms = 1000 * 1000;
80 const uint64_t one_us = 1000;
81
82 switch (time_unit) {
83 case kTimeUnitSecond:
84 return one_sec;
85 case kTimeUnitMillisecond:
86 return one_ms;
87 case kTimeUnitMicrosecond:
88 return one_us;
89 case kTimeUnitNanosecond:
90 return 1;
91 }
92 return 0;
93}
94
95std::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit,
96 size_t max_fraction_digits) {
97 const char* unit = nullptr;
98 uint64_t divisor = GetNsToTimeUnitDivisor(time_unit);
99 switch (time_unit) {
100 case kTimeUnitSecond:
101 unit = "s";
102 break;
103 case kTimeUnitMillisecond:
104 unit = "ms";
105 break;
106 case kTimeUnitMicrosecond:
107 unit = "us";
108 break;
109 case kTimeUnitNanosecond:
110 unit = "ns";
111 break;
112 }
113 const uint64_t whole_part = nano_duration / divisor;
114 uint64_t fractional_part = nano_duration % divisor;
115 if (fractional_part == 0) {
116 return StringPrintf("%" PRIu64 "%s", whole_part, unit);
117 } else {
118 static constexpr size_t kMaxDigits = 30;
119 size_t avail_digits = kMaxDigits;
120 char fraction_buffer[kMaxDigits];
121 char* ptr = fraction_buffer;
122 uint64_t multiplier = 10;
123 // This infinite loops if fractional part is 0.
124 while (avail_digits > 1 && fractional_part * multiplier < divisor) {
125 multiplier *= 10;
126 *ptr++ = '0';
127 avail_digits--;
128 }
129 snprintf(ptr, avail_digits, "%" PRIu64, fractional_part);
130 fraction_buffer[std::min(kMaxDigits - 1, max_fraction_digits)] = '\0';
131 return StringPrintf("%" PRIu64 ".%s%s", whole_part, fraction_buffer, unit);
132 }
133}
134
135std::string GetIsoDate() {
Vladimir Marko80afd022015-05-19 18:08:00 +0100136 tm tmbuf;
Elliott Hughes43d7c652020-07-24 13:22:37 -0700137 int ns;
Jiyong Parkef01e762021-01-22 10:40:16 +0900138 if (__builtin_available(macOS 10.12, *)) {
139 timespec now;
140 clock_gettime(CLOCK_REALTIME, &now);
141 localtime_r(&now.tv_sec, &tmbuf);
142 ns = now.tv_nsec;
143 } else {
144 time_t now = time(nullptr);
145 localtime_r(&now, &tmbuf);
146 ns = 0;
147 }
Elliott Hughes43d7c652020-07-24 13:22:37 -0700148 char zone[16] = {};
Jiyong Parkef01e762021-01-22 10:40:16 +0900149 strftime(zone, sizeof(zone), "%z", &tmbuf);
Elliott Hughes43d7c652020-07-24 13:22:37 -0700150 return StringPrintf("%04d-%02d-%02d %02d:%02d:%02d.%09d%s",
Jiyong Parkef01e762021-01-22 10:40:16 +0900151 tmbuf.tm_year + 1900, tmbuf.tm_mon+1, tmbuf.tm_mday,
152 tmbuf.tm_hour, tmbuf.tm_min, tmbuf.tm_sec, ns, zone);
Vladimir Marko80afd022015-05-19 18:08:00 +0100153}
154
155uint64_t MilliTime() {
156#if defined(__linux__)
157 timespec now;
158 clock_gettime(CLOCK_MONOTONIC, &now);
159 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000) + now.tv_nsec / UINT64_C(1000000);
Roland Levillain891fdcf2018-12-17 15:16:56 +0000160#else
Vladimir Marko80afd022015-05-19 18:08:00 +0100161 timeval now;
David Sehr10db8fe2018-07-18 11:01:20 -0700162 GetTimeOfDay(&now, nullptr);
Vladimir Marko80afd022015-05-19 18:08:00 +0100163 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000) + now.tv_usec / UINT64_C(1000);
164#endif
165}
166
167uint64_t MicroTime() {
168#if defined(__linux__)
169 timespec now;
170 clock_gettime(CLOCK_MONOTONIC, &now);
171 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000) + now.tv_nsec / UINT64_C(1000);
Roland Levillain891fdcf2018-12-17 15:16:56 +0000172#else
Vladimir Marko80afd022015-05-19 18:08:00 +0100173 timeval now;
David Sehr10db8fe2018-07-18 11:01:20 -0700174 GetTimeOfDay(&now, nullptr);
Vladimir Marko80afd022015-05-19 18:08:00 +0100175 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000) + now.tv_usec;
176#endif
177}
178
179uint64_t NanoTime() {
180#if defined(__linux__)
181 timespec now;
182 clock_gettime(CLOCK_MONOTONIC, &now);
183 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
Roland Levillain891fdcf2018-12-17 15:16:56 +0000184#else
Vladimir Marko80afd022015-05-19 18:08:00 +0100185 timeval now;
David Sehr10db8fe2018-07-18 11:01:20 -0700186 GetTimeOfDay(&now, nullptr);
Vladimir Marko80afd022015-05-19 18:08:00 +0100187 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_usec * UINT64_C(1000);
188#endif
189}
190
191uint64_t ThreadCpuNanoTime() {
192#if defined(__linux__)
193 timespec now;
194 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now);
195 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
Roland Levillain891fdcf2018-12-17 15:16:56 +0000196#else
Vladimir Marko80afd022015-05-19 18:08:00 +0100197 UNIMPLEMENTED(WARNING);
198 return -1;
199#endif
200}
201
Andreas Gampec560fc02014-07-16 09:57:39 -0700202uint64_t ProcessCpuNanoTime() {
203#if defined(__linux__)
204 timespec now;
205 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &now);
206 return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec;
207#else
Roland Levillain891fdcf2018-12-17 15:16:56 +0000208 // We cannot use clock_gettime() here. Return the process wall clock time
209 // (using art::NanoTime, which relies on gettimeofday()) as approximation of
210 // the process CPU time instead.
211 //
212 // Note: clock_gettime() is available from macOS 10.12 (Darwin 16), but we try
213 // to keep things simple here.
214 return NanoTime();
Andreas Gampec560fc02014-07-16 09:57:39 -0700215#endif
216}
217
Vladimir Marko80afd022015-05-19 18:08:00 +0100218void NanoSleep(uint64_t ns) {
219 timespec tm;
Hans Boehmca834382020-08-26 21:41:13 -0700220 tm.tv_sec = SaturatedTimeT(ns / MsToNs(1000));
Vladimir Marko80afd022015-05-19 18:08:00 +0100221 tm.tv_nsec = ns - static_cast<uint64_t>(tm.tv_sec) * MsToNs(1000);
222 nanosleep(&tm, nullptr);
223}
224
225void InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts) {
Vladimir Marko80afd022015-05-19 18:08:00 +0100226 if (absolute) {
David Sehr10db8fe2018-07-18 11:01:20 -0700227#if defined(__linux__)
Vladimir Marko80afd022015-05-19 18:08:00 +0100228 clock_gettime(clock, ts);
229#else
230 UNUSED(clock);
231 timeval tv;
David Sehr10db8fe2018-07-18 11:01:20 -0700232 GetTimeOfDay(&tv, nullptr);
Vladimir Marko80afd022015-05-19 18:08:00 +0100233 ts->tv_sec = tv.tv_sec;
234 ts->tv_nsec = tv.tv_usec * 1000;
235#endif
236 } else {
237 ts->tv_sec = 0;
238 ts->tv_nsec = 0;
239 }
Narayan Kamath93e8edd2015-12-03 14:11:46 +0000240
241 int64_t end_sec = ts->tv_sec + ms / 1000;
Hans Boehmfe76a622016-03-28 14:36:23 -0700242 constexpr int32_t int32_max = std::numeric_limits<int32_t>::max();
243 if (UNLIKELY(end_sec >= int32_max)) {
244 // Either ms was intended to denote an infinite timeout, or we have a
245 // problem. The former generally uses the largest possible millisecond
246 // or nanosecond value. Log only in the latter case.
247 constexpr int64_t int64_max = std::numeric_limits<int64_t>::max();
248 if (ms != int64_max && ms != int64_max / (1000 * 1000)) {
249 LOG(INFO) << "Note: end time exceeds INT32_MAX: " << end_sec;
250 }
251 end_sec = int32_max - 1; // Allow for increment below.
Vladimir Marko80afd022015-05-19 18:08:00 +0100252 }
Narayan Kamath93e8edd2015-12-03 14:11:46 +0000253 ts->tv_sec = end_sec;
Vladimir Marko80afd022015-05-19 18:08:00 +0100254 ts->tv_nsec = (ts->tv_nsec + (ms % 1000) * 1000000) + ns;
255
256 // Catch rollover.
257 if (ts->tv_nsec >= 1000000000L) {
258 ts->tv_sec++;
259 ts->tv_nsec -= 1000000000L;
260 }
261}
262
263} // namespace art