Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 1 | /* |
| 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 Hughes | f323832 | 2023-06-30 10:23:39 -0700 | [diff] [blame] | 17 | #define _POSIX_THREAD_SAFE_FUNCTIONS // For mingw localtime_r(). |
| 18 | |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 19 | #include "time_utils.h" |
| 20 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 21 | #include <inttypes.h> |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | |
Hans Boehm | fe76a62 | 2016-03-28 14:36:23 -0700 | [diff] [blame] | 24 | #include <limits> |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 25 | #include <sstream> |
| 26 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 27 | #include "android-base/stringprintf.h" |
| 28 | |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 29 | #include "logging.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 30 | |
Vladimir Marko | ce39200 | 2015-05-26 19:57:30 +0100 | [diff] [blame] | 31 | #if defined(__APPLE__) |
| 32 | #include <sys/time.h> |
| 33 | #endif |
| 34 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 35 | namespace art { |
| 36 | |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 37 | namespace { |
| 38 | |
| 39 | #if !defined(__linux__) |
| 40 | int 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 Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 51 | using android::base::StringPrintf; |
| 52 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 53 | std::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 | |
| 62 | TimeUnit 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 | |
| 77 | uint64_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 | |
| 95 | std::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 | |
| 135 | std::string GetIsoDate() { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 136 | tm tmbuf; |
Elliott Hughes | 43d7c65 | 2020-07-24 13:22:37 -0700 | [diff] [blame] | 137 | int ns; |
Jiyong Park | ef01e76 | 2021-01-22 10:40:16 +0900 | [diff] [blame] | 138 | 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 Hughes | 43d7c65 | 2020-07-24 13:22:37 -0700 | [diff] [blame] | 148 | char zone[16] = {}; |
Jiyong Park | ef01e76 | 2021-01-22 10:40:16 +0900 | [diff] [blame] | 149 | strftime(zone, sizeof(zone), "%z", &tmbuf); |
Elliott Hughes | 43d7c65 | 2020-07-24 13:22:37 -0700 | [diff] [blame] | 150 | return StringPrintf("%04d-%02d-%02d %02d:%02d:%02d.%09d%s", |
Jiyong Park | ef01e76 | 2021-01-22 10:40:16 +0900 | [diff] [blame] | 151 | tmbuf.tm_year + 1900, tmbuf.tm_mon+1, tmbuf.tm_mday, |
| 152 | tmbuf.tm_hour, tmbuf.tm_min, tmbuf.tm_sec, ns, zone); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | uint64_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 Levillain | 891fdcf | 2018-12-17 15:16:56 +0000 | [diff] [blame] | 160 | #else |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 161 | timeval now; |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 162 | GetTimeOfDay(&now, nullptr); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 163 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000) + now.tv_usec / UINT64_C(1000); |
| 164 | #endif |
| 165 | } |
| 166 | |
| 167 | uint64_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 Levillain | 891fdcf | 2018-12-17 15:16:56 +0000 | [diff] [blame] | 172 | #else |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 173 | timeval now; |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 174 | GetTimeOfDay(&now, nullptr); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 175 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000) + now.tv_usec; |
| 176 | #endif |
| 177 | } |
| 178 | |
| 179 | uint64_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 Levillain | 891fdcf | 2018-12-17 15:16:56 +0000 | [diff] [blame] | 184 | #else |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 185 | timeval now; |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 186 | GetTimeOfDay(&now, nullptr); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 187 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_usec * UINT64_C(1000); |
| 188 | #endif |
| 189 | } |
| 190 | |
| 191 | uint64_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 Levillain | 891fdcf | 2018-12-17 15:16:56 +0000 | [diff] [blame] | 196 | #else |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 197 | UNIMPLEMENTED(WARNING); |
| 198 | return -1; |
| 199 | #endif |
| 200 | } |
| 201 | |
Andreas Gampe | c560fc0 | 2014-07-16 09:57:39 -0700 | [diff] [blame] | 202 | uint64_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 Levillain | 891fdcf | 2018-12-17 15:16:56 +0000 | [diff] [blame] | 208 | // 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 Gampe | c560fc0 | 2014-07-16 09:57:39 -0700 | [diff] [blame] | 215 | #endif |
| 216 | } |
| 217 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 218 | void NanoSleep(uint64_t ns) { |
| 219 | timespec tm; |
Hans Boehm | ca83438 | 2020-08-26 21:41:13 -0700 | [diff] [blame] | 220 | tm.tv_sec = SaturatedTimeT(ns / MsToNs(1000)); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 221 | tm.tv_nsec = ns - static_cast<uint64_t>(tm.tv_sec) * MsToNs(1000); |
| 222 | nanosleep(&tm, nullptr); |
| 223 | } |
| 224 | |
| 225 | void InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts) { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 226 | if (absolute) { |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 227 | #if defined(__linux__) |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 228 | clock_gettime(clock, ts); |
| 229 | #else |
| 230 | UNUSED(clock); |
| 231 | timeval tv; |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 232 | GetTimeOfDay(&tv, nullptr); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 233 | 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 Kamath | 93e8edd | 2015-12-03 14:11:46 +0000 | [diff] [blame] | 240 | |
| 241 | int64_t end_sec = ts->tv_sec + ms / 1000; |
Hans Boehm | fe76a62 | 2016-03-28 14:36:23 -0700 | [diff] [blame] | 242 | 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 Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 252 | } |
Narayan Kamath | 93e8edd | 2015-12-03 14:11:46 +0000 | [diff] [blame] | 253 | ts->tv_sec = end_sec; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 254 | 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 |