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 | |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 17 | #include "time_utils.h" |
| 18 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 19 | #include <inttypes.h> |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | |
Hans Boehm | fe76a62 | 2016-03-28 14:36:23 -0700 | [diff] [blame] | 22 | #include <limits> |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 23 | #include <sstream> |
| 24 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 25 | #include "android-base/stringprintf.h" |
| 26 | |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 27 | #include "logging.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 28 | |
Vladimir Marko | ce39200 | 2015-05-26 19:57:30 +0100 | [diff] [blame] | 29 | #if defined(__APPLE__) |
| 30 | #include <sys/time.h> |
| 31 | #endif |
| 32 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 33 | namespace art { |
| 34 | |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 35 | namespace { |
| 36 | |
| 37 | #if !defined(__linux__) |
| 38 | int GetTimeOfDay(struct timeval* tv, struct timezone* tz) { |
| 39 | #ifdef _WIN32 |
| 40 | return mingw_gettimeofday(tv, tz); |
| 41 | #else |
| 42 | return gettimeofday(tv, tz); |
| 43 | #endif |
| 44 | } |
| 45 | #endif |
| 46 | |
| 47 | } // namespace |
| 48 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 49 | using android::base::StringPrintf; |
| 50 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 51 | std::string PrettyDuration(uint64_t nano_duration, size_t max_fraction_digits) { |
| 52 | if (nano_duration == 0) { |
| 53 | return "0"; |
| 54 | } else { |
| 55 | return FormatDuration(nano_duration, GetAppropriateTimeUnit(nano_duration), |
| 56 | max_fraction_digits); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | TimeUnit GetAppropriateTimeUnit(uint64_t nano_duration) { |
| 61 | const uint64_t one_sec = 1000 * 1000 * 1000; |
| 62 | const uint64_t one_ms = 1000 * 1000; |
| 63 | const uint64_t one_us = 1000; |
| 64 | if (nano_duration >= one_sec) { |
| 65 | return kTimeUnitSecond; |
| 66 | } else if (nano_duration >= one_ms) { |
| 67 | return kTimeUnitMillisecond; |
| 68 | } else if (nano_duration >= one_us) { |
| 69 | return kTimeUnitMicrosecond; |
| 70 | } else { |
| 71 | return kTimeUnitNanosecond; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | uint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit) { |
| 76 | const uint64_t one_sec = 1000 * 1000 * 1000; |
| 77 | const uint64_t one_ms = 1000 * 1000; |
| 78 | const uint64_t one_us = 1000; |
| 79 | |
| 80 | switch (time_unit) { |
| 81 | case kTimeUnitSecond: |
| 82 | return one_sec; |
| 83 | case kTimeUnitMillisecond: |
| 84 | return one_ms; |
| 85 | case kTimeUnitMicrosecond: |
| 86 | return one_us; |
| 87 | case kTimeUnitNanosecond: |
| 88 | return 1; |
| 89 | } |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | std::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit, |
| 94 | size_t max_fraction_digits) { |
| 95 | const char* unit = nullptr; |
| 96 | uint64_t divisor = GetNsToTimeUnitDivisor(time_unit); |
| 97 | switch (time_unit) { |
| 98 | case kTimeUnitSecond: |
| 99 | unit = "s"; |
| 100 | break; |
| 101 | case kTimeUnitMillisecond: |
| 102 | unit = "ms"; |
| 103 | break; |
| 104 | case kTimeUnitMicrosecond: |
| 105 | unit = "us"; |
| 106 | break; |
| 107 | case kTimeUnitNanosecond: |
| 108 | unit = "ns"; |
| 109 | break; |
| 110 | } |
| 111 | const uint64_t whole_part = nano_duration / divisor; |
| 112 | uint64_t fractional_part = nano_duration % divisor; |
| 113 | if (fractional_part == 0) { |
| 114 | return StringPrintf("%" PRIu64 "%s", whole_part, unit); |
| 115 | } else { |
| 116 | static constexpr size_t kMaxDigits = 30; |
| 117 | size_t avail_digits = kMaxDigits; |
| 118 | char fraction_buffer[kMaxDigits]; |
| 119 | char* ptr = fraction_buffer; |
| 120 | uint64_t multiplier = 10; |
| 121 | // This infinite loops if fractional part is 0. |
| 122 | while (avail_digits > 1 && fractional_part * multiplier < divisor) { |
| 123 | multiplier *= 10; |
| 124 | *ptr++ = '0'; |
| 125 | avail_digits--; |
| 126 | } |
| 127 | snprintf(ptr, avail_digits, "%" PRIu64, fractional_part); |
| 128 | fraction_buffer[std::min(kMaxDigits - 1, max_fraction_digits)] = '\0'; |
| 129 | return StringPrintf("%" PRIu64 ".%s%s", whole_part, fraction_buffer, unit); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | std::string GetIsoDate() { |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 134 | tm tmbuf; |
Elliott Hughes | 43d7c65 | 2020-07-24 13:22:37 -0700 | [diff] [blame] | 135 | int ns; |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 136 | #ifdef _WIN32 |
Elliott Hughes | 43d7c65 | 2020-07-24 13:22:37 -0700 | [diff] [blame] | 137 | time_t now = time(nullptr); |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 138 | localtime_s(&tmbuf, &now); |
Elliott Hughes | 43d7c65 | 2020-07-24 13:22:37 -0700 | [diff] [blame] | 139 | ns = 0; |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 140 | #else |
Jiyong Park | ef01e76 | 2021-01-22 10:40:16 +0900 | [diff] [blame] | 141 | if (__builtin_available(macOS 10.12, *)) { |
| 142 | timespec now; |
| 143 | clock_gettime(CLOCK_REALTIME, &now); |
| 144 | localtime_r(&now.tv_sec, &tmbuf); |
| 145 | ns = now.tv_nsec; |
| 146 | } else { |
| 147 | time_t now = time(nullptr); |
| 148 | localtime_r(&now, &tmbuf); |
| 149 | ns = 0; |
| 150 | } |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 151 | #endif |
Elliott Hughes | 43d7c65 | 2020-07-24 13:22:37 -0700 | [diff] [blame] | 152 | char zone[16] = {}; |
Jiyong Park | ef01e76 | 2021-01-22 10:40:16 +0900 | [diff] [blame] | 153 | strftime(zone, sizeof(zone), "%z", &tmbuf); |
Elliott Hughes | 43d7c65 | 2020-07-24 13:22:37 -0700 | [diff] [blame] | 154 | return StringPrintf("%04d-%02d-%02d %02d:%02d:%02d.%09d%s", |
Jiyong Park | ef01e76 | 2021-01-22 10:40:16 +0900 | [diff] [blame] | 155 | tmbuf.tm_year + 1900, tmbuf.tm_mon+1, tmbuf.tm_mday, |
| 156 | tmbuf.tm_hour, tmbuf.tm_min, tmbuf.tm_sec, ns, zone); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | uint64_t MilliTime() { |
| 160 | #if defined(__linux__) |
| 161 | timespec now; |
| 162 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 163 | 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] | 164 | #else |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 165 | timeval now; |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 166 | GetTimeOfDay(&now, nullptr); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 167 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000) + now.tv_usec / UINT64_C(1000); |
| 168 | #endif |
| 169 | } |
| 170 | |
| 171 | uint64_t MicroTime() { |
| 172 | #if defined(__linux__) |
| 173 | timespec now; |
| 174 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 175 | 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] | 176 | #else |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 177 | timeval now; |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 178 | GetTimeOfDay(&now, nullptr); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 179 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000) + now.tv_usec; |
| 180 | #endif |
| 181 | } |
| 182 | |
| 183 | uint64_t NanoTime() { |
| 184 | #if defined(__linux__) |
| 185 | timespec now; |
| 186 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 187 | 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] | 188 | #else |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 189 | timeval now; |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 190 | GetTimeOfDay(&now, nullptr); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 191 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_usec * UINT64_C(1000); |
| 192 | #endif |
| 193 | } |
| 194 | |
| 195 | uint64_t ThreadCpuNanoTime() { |
| 196 | #if defined(__linux__) |
| 197 | timespec now; |
| 198 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now); |
| 199 | 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] | 200 | #else |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 201 | UNIMPLEMENTED(WARNING); |
| 202 | return -1; |
| 203 | #endif |
| 204 | } |
| 205 | |
Andreas Gampe | c560fc0 | 2014-07-16 09:57:39 -0700 | [diff] [blame] | 206 | uint64_t ProcessCpuNanoTime() { |
| 207 | #if defined(__linux__) |
| 208 | timespec now; |
| 209 | clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &now); |
| 210 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec; |
| 211 | #else |
Roland Levillain | 891fdcf | 2018-12-17 15:16:56 +0000 | [diff] [blame] | 212 | // We cannot use clock_gettime() here. Return the process wall clock time |
| 213 | // (using art::NanoTime, which relies on gettimeofday()) as approximation of |
| 214 | // the process CPU time instead. |
| 215 | // |
| 216 | // Note: clock_gettime() is available from macOS 10.12 (Darwin 16), but we try |
| 217 | // to keep things simple here. |
| 218 | return NanoTime(); |
Andreas Gampe | c560fc0 | 2014-07-16 09:57:39 -0700 | [diff] [blame] | 219 | #endif |
| 220 | } |
| 221 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 222 | void NanoSleep(uint64_t ns) { |
| 223 | timespec tm; |
Hans Boehm | ca83438 | 2020-08-26 21:41:13 -0700 | [diff] [blame] | 224 | tm.tv_sec = SaturatedTimeT(ns / MsToNs(1000)); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 225 | tm.tv_nsec = ns - static_cast<uint64_t>(tm.tv_sec) * MsToNs(1000); |
| 226 | nanosleep(&tm, nullptr); |
| 227 | } |
| 228 | |
| 229 | 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] | 230 | if (absolute) { |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 231 | #if defined(__linux__) |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 232 | clock_gettime(clock, ts); |
| 233 | #else |
| 234 | UNUSED(clock); |
| 235 | timeval tv; |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 236 | GetTimeOfDay(&tv, nullptr); |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 237 | ts->tv_sec = tv.tv_sec; |
| 238 | ts->tv_nsec = tv.tv_usec * 1000; |
| 239 | #endif |
| 240 | } else { |
| 241 | ts->tv_sec = 0; |
| 242 | ts->tv_nsec = 0; |
| 243 | } |
Narayan Kamath | 93e8edd | 2015-12-03 14:11:46 +0000 | [diff] [blame] | 244 | |
| 245 | int64_t end_sec = ts->tv_sec + ms / 1000; |
Hans Boehm | fe76a62 | 2016-03-28 14:36:23 -0700 | [diff] [blame] | 246 | constexpr int32_t int32_max = std::numeric_limits<int32_t>::max(); |
| 247 | if (UNLIKELY(end_sec >= int32_max)) { |
| 248 | // Either ms was intended to denote an infinite timeout, or we have a |
| 249 | // problem. The former generally uses the largest possible millisecond |
| 250 | // or nanosecond value. Log only in the latter case. |
| 251 | constexpr int64_t int64_max = std::numeric_limits<int64_t>::max(); |
| 252 | if (ms != int64_max && ms != int64_max / (1000 * 1000)) { |
| 253 | LOG(INFO) << "Note: end time exceeds INT32_MAX: " << end_sec; |
| 254 | } |
| 255 | end_sec = int32_max - 1; // Allow for increment below. |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 256 | } |
Narayan Kamath | 93e8edd | 2015-12-03 14:11:46 +0000 | [diff] [blame] | 257 | ts->tv_sec = end_sec; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 258 | ts->tv_nsec = (ts->tv_nsec + (ms % 1000) * 1000000) + ns; |
| 259 | |
| 260 | // Catch rollover. |
| 261 | if (ts->tv_nsec >= 1000000000L) { |
| 262 | ts->tv_sec++; |
| 263 | ts->tv_nsec -= 1000000000L; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | } // namespace art |