Vladimir Marko | 41b175a | 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 | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_TIME_UTILS_H_ |
| 18 | #define ART_LIBARTBASE_BASE_TIME_UTILS_H_ |
Vladimir Marko | 41b175a | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 19 | |
Andreas Gampe | 2388ae5 | 2019-05-09 16:26:08 -0700 | [diff] [blame] | 20 | #ifdef _WIN32 |
| 21 | #include <stdio.h> // Needed for correct macro definitions. |
| 22 | #endif |
| 23 | |
Vladimir Marko | 41b175a | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 24 | #include <time.h> |
| 25 | |
Andreas Gampe | 2388ae5 | 2019-05-09 16:26:08 -0700 | [diff] [blame] | 26 | #include <cstdint> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 27 | #include <string> |
| 28 | |
Vladimir Marko | 41b175a | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 29 | namespace art { |
| 30 | |
| 31 | enum TimeUnit { |
| 32 | kTimeUnitNanosecond, |
| 33 | kTimeUnitMicrosecond, |
| 34 | kTimeUnitMillisecond, |
| 35 | kTimeUnitSecond, |
| 36 | }; |
| 37 | |
Eric Holk | 62b7558 | 2021-03-03 15:14:58 -0800 | [diff] [blame] | 38 | // Constants for common time periods. |
| 39 | constexpr unsigned int kOneMinuteInSeconds = 60; |
| 40 | constexpr unsigned int kOneHourInSeconds = 60 * kOneMinuteInSeconds; |
| 41 | |
Vladimir Marko | 41b175a | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 42 | // Returns a human-readable time string which prints every nanosecond while trying to limit the |
| 43 | // number of trailing zeros. Prints using the largest human readable unit up to a second. |
| 44 | // e.g. "1ms", "1.000000001s", "1.001us" |
| 45 | std::string PrettyDuration(uint64_t nano_duration, size_t max_fraction_digits = 3); |
| 46 | |
| 47 | // Format a nanosecond time to specified units. |
| 48 | std::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit, |
| 49 | size_t max_fraction_digits); |
| 50 | |
| 51 | // Get the appropriate unit for a nanosecond duration. |
| 52 | TimeUnit GetAppropriateTimeUnit(uint64_t nano_duration); |
| 53 | |
| 54 | // Get the divisor to convert from a nanoseconds to a time unit. |
| 55 | uint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit); |
| 56 | |
| 57 | // Returns the current date in ISO yyyy-mm-dd hh:mm:ss format. |
| 58 | std::string GetIsoDate(); |
| 59 | |
| 60 | // Returns the monotonic time since some unspecified starting point in milliseconds. |
| 61 | uint64_t MilliTime(); |
| 62 | |
| 63 | // Returns the monotonic time since some unspecified starting point in microseconds. |
| 64 | uint64_t MicroTime(); |
| 65 | |
| 66 | // Returns the monotonic time since some unspecified starting point in nanoseconds. |
| 67 | uint64_t NanoTime(); |
| 68 | |
| 69 | // Returns the thread-specific CPU-time clock in nanoseconds or -1 if unavailable. |
| 70 | uint64_t ThreadCpuNanoTime(); |
| 71 | |
Andreas Gampe | c560fc0 | 2014-07-16 09:57:39 -0700 | [diff] [blame] | 72 | // Returns the process CPU-time clock in nanoseconds or -1 if unavailable. |
| 73 | uint64_t ProcessCpuNanoTime(); |
| 74 | |
Vladimir Marko | 41b175a | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 75 | // Converts the given number of nanoseconds to milliseconds. |
Hans Boehm | ca83438 | 2020-08-26 21:41:13 -0700 | [diff] [blame] | 76 | static constexpr uint64_t NsToMs(uint64_t ns) { |
Vladimir Marko | 41b175a | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 77 | return ns / 1000 / 1000; |
| 78 | } |
| 79 | |
Lokesh Gidra | 7f04738 | 2021-06-17 16:28:46 -0700 | [diff] [blame^] | 80 | // Converts the given number of nanoseconds to microseconds. |
| 81 | static constexpr uint64_t NsToUs(uint64_t ns) { |
| 82 | return ns / 1000; |
| 83 | } |
| 84 | |
Vladimir Marko | 41b175a | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 85 | // Converts the given number of milliseconds to nanoseconds |
Hans Boehm | ca83438 | 2020-08-26 21:41:13 -0700 | [diff] [blame] | 86 | static constexpr uint64_t MsToNs(uint64_t ms) { |
Mathieu Chartier | 3b532d7 | 2015-06-05 13:21:05 -0700 | [diff] [blame] | 87 | return ms * 1000 * 1000; |
Vladimir Marko | 41b175a | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 90 | // Converts the given number of milliseconds to microseconds |
Hans Boehm | ca83438 | 2020-08-26 21:41:13 -0700 | [diff] [blame] | 91 | static constexpr uint64_t MsToUs(uint64_t ms) { |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 92 | return ms * 1000; |
| 93 | } |
| 94 | |
Hans Boehm | ca83438 | 2020-08-26 21:41:13 -0700 | [diff] [blame] | 95 | static constexpr uint64_t UsToNs(uint64_t us) { |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 96 | return us * 1000; |
| 97 | } |
| 98 | |
Eric Holk | 0b986f7 | 2021-01-20 22:24:06 +0000 | [diff] [blame] | 99 | static constexpr uint64_t SecondsToMs(uint64_t seconds) { |
| 100 | return seconds * 1000; |
| 101 | } |
| 102 | |
Hans Boehm | ca83438 | 2020-08-26 21:41:13 -0700 | [diff] [blame] | 103 | static constexpr time_t SaturatedTimeT(int64_t secs) { |
| 104 | if (sizeof(time_t) < sizeof(int64_t)) { |
| 105 | return static_cast<time_t>(std::min(secs, |
| 106 | static_cast<int64_t>(std::numeric_limits<time_t>::max()))); |
| 107 | } else { |
| 108 | return secs; |
| 109 | } |
| 110 | } |
| 111 | |
Vladimir Marko | 41b175a | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 112 | #if defined(__APPLE__) |
Dan Willemsen | 0fc1c9a | 2016-10-26 10:13:15 -0700 | [diff] [blame] | 113 | #ifndef CLOCK_REALTIME |
| 114 | // No clocks to specify on OS/X < 10.12, fake value to pass to routines that require a clock. |
Vladimir Marko | 41b175a | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 115 | #define CLOCK_REALTIME 0xebadf00d |
| 116 | #endif |
Dan Willemsen | 0fc1c9a | 2016-10-26 10:13:15 -0700 | [diff] [blame] | 117 | #endif |
Vladimir Marko | 41b175a | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 118 | |
| 119 | // Sleep for the given number of nanoseconds, a bad way to handle contention. |
| 120 | void NanoSleep(uint64_t ns); |
| 121 | |
| 122 | // Initialize a timespec to either a relative time (ms,ns), or to the absolute |
| 123 | // time corresponding to the indicated clock value plus the supplied offset. |
| 124 | void InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts); |
| 125 | |
| 126 | } // namespace art |
| 127 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 128 | #endif // ART_LIBARTBASE_BASE_TIME_UTILS_H_ |