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 | |
| 17 | #ifndef ART_RUNTIME_BASE_TIME_UTILS_H_ |
| 18 | #define ART_RUNTIME_BASE_TIME_UTILS_H_ |
| 19 | |
| 20 | #include <stdint.h> |
Vladimir Marko | 41b175a | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 21 | #include <time.h> |
| 22 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
Vladimir Marko | 41b175a | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 25 | #include "base/macros.h" |
| 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | enum TimeUnit { |
| 30 | kTimeUnitNanosecond, |
| 31 | kTimeUnitMicrosecond, |
| 32 | kTimeUnitMillisecond, |
| 33 | kTimeUnitSecond, |
| 34 | }; |
| 35 | |
| 36 | // Returns a human-readable time string which prints every nanosecond while trying to limit the |
| 37 | // number of trailing zeros. Prints using the largest human readable unit up to a second. |
| 38 | // e.g. "1ms", "1.000000001s", "1.001us" |
| 39 | std::string PrettyDuration(uint64_t nano_duration, size_t max_fraction_digits = 3); |
| 40 | |
| 41 | // Format a nanosecond time to specified units. |
| 42 | std::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit, |
| 43 | size_t max_fraction_digits); |
| 44 | |
| 45 | // Get the appropriate unit for a nanosecond duration. |
| 46 | TimeUnit GetAppropriateTimeUnit(uint64_t nano_duration); |
| 47 | |
| 48 | // Get the divisor to convert from a nanoseconds to a time unit. |
| 49 | uint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit); |
| 50 | |
| 51 | // Returns the current date in ISO yyyy-mm-dd hh:mm:ss format. |
| 52 | std::string GetIsoDate(); |
| 53 | |
| 54 | // Returns the monotonic time since some unspecified starting point in milliseconds. |
| 55 | uint64_t MilliTime(); |
| 56 | |
| 57 | // Returns the monotonic time since some unspecified starting point in microseconds. |
| 58 | uint64_t MicroTime(); |
| 59 | |
| 60 | // Returns the monotonic time since some unspecified starting point in nanoseconds. |
| 61 | uint64_t NanoTime(); |
| 62 | |
| 63 | // Returns the thread-specific CPU-time clock in nanoseconds or -1 if unavailable. |
| 64 | uint64_t ThreadCpuNanoTime(); |
| 65 | |
Andreas Gampe | c560fc0 | 2014-07-16 09:57:39 -0700 | [diff] [blame] | 66 | // Returns the process CPU-time clock in nanoseconds or -1 if unavailable. |
| 67 | uint64_t ProcessCpuNanoTime(); |
| 68 | |
Vladimir Marko | 41b175a | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 69 | // Converts the given number of nanoseconds to milliseconds. |
| 70 | static constexpr inline uint64_t NsToMs(uint64_t ns) { |
| 71 | return ns / 1000 / 1000; |
| 72 | } |
| 73 | |
| 74 | // Converts the given number of milliseconds to nanoseconds |
Mathieu Chartier | 3b532d7 | 2015-06-05 13:21:05 -0700 | [diff] [blame] | 75 | static constexpr inline uint64_t MsToNs(uint64_t ms) { |
| 76 | return ms * 1000 * 1000; |
Vladimir Marko | 41b175a | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | #if defined(__APPLE__) |
Dan Willemsen | 0fc1c9a | 2016-10-26 10:13:15 -0700 | [diff] [blame] | 80 | #ifndef CLOCK_REALTIME |
| 81 | // 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] | 82 | #define CLOCK_REALTIME 0xebadf00d |
| 83 | #endif |
Dan Willemsen | 0fc1c9a | 2016-10-26 10:13:15 -0700 | [diff] [blame] | 84 | #endif |
Vladimir Marko | 41b175a | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 85 | |
| 86 | // Sleep for the given number of nanoseconds, a bad way to handle contention. |
| 87 | void NanoSleep(uint64_t ns); |
| 88 | |
| 89 | // Initialize a timespec to either a relative time (ms,ns), or to the absolute |
| 90 | // time corresponding to the indicated clock value plus the supplied offset. |
| 91 | void InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts); |
| 92 | |
| 93 | } // namespace art |
| 94 | |
| 95 | #endif // ART_RUNTIME_BASE_TIME_UTILS_H_ |