• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LIBARTBASE_BASE_TIME_UTILS_H_
18 #define ART_LIBARTBASE_BASE_TIME_UTILS_H_
19 
20 #ifdef _WIN32
21 #include <stdio.h>  // Needed for correct macro definitions.
22 #endif
23 
24 #include <time.h>
25 
26 #include <cstdint>
27 #include <string>
28 
29 #include "android-base/logging.h"
30 
31 namespace art {
32 
33 enum TimeUnit {
34   kTimeUnitNanosecond,
35   kTimeUnitMicrosecond,
36   kTimeUnitMillisecond,
37   kTimeUnitSecond,
38 };
39 
40 // Constants for common time periods.
41 constexpr unsigned int kOneMinuteInSeconds = 60;
42 constexpr unsigned int kOneHourInSeconds = 60 * kOneMinuteInSeconds;
43 
44 // Returns a human-readable time string which prints every nanosecond while trying to limit the
45 // number of trailing zeros. Prints using the largest human readable unit up to a second.
46 // e.g. "1ms", "1.000000001s", "1.001us"
47 std::string PrettyDuration(uint64_t nano_duration, size_t max_fraction_digits = 3);
48 
49 // Format a nanosecond time to specified units.
50 std::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit,
51                            size_t max_fraction_digits);
52 
53 // Get the appropriate unit for a nanosecond duration.
54 TimeUnit GetAppropriateTimeUnit(uint64_t nano_duration);
55 
56 // Get the divisor to convert from a nanoseconds to a time unit.
57 uint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit);
58 
59 // Returns the current date in ISO yyyy-mm-dd hh:mm:ss format.
60 std::string GetIsoDate();
61 
62 // Returns the monotonic time since some unspecified starting point in milliseconds.
63 uint64_t MilliTime();
64 
65 // Returns the monotonic time since some unspecified starting point in microseconds.
66 uint64_t MicroTime();
67 
68 // Returns the monotonic time since some unspecified starting point in nanoseconds.
69 uint64_t NanoTime();
70 
71 // Returns the thread-specific CPU-time clock in nanoseconds or -1 if unavailable.
72 uint64_t ThreadCpuNanoTime();
73 
74 // Returns the process CPU-time clock in nanoseconds or -1 if unavailable.
75 uint64_t ProcessCpuNanoTime();
76 
77 // Converts the given number of nanoseconds to milliseconds.
NsToMs(uint64_t ns)78 static constexpr uint64_t NsToMs(uint64_t ns) {
79   return ns / 1000 / 1000;
80 }
81 
82 // Converts the given number of nanoseconds to microseconds.
NsToUs(uint64_t ns)83 static constexpr uint64_t NsToUs(uint64_t ns) {
84   return ns / 1000;
85 }
86 
87 // Converts the given number of milliseconds to nanoseconds
MsToNs(uint64_t ms)88 static constexpr uint64_t MsToNs(uint64_t ms) {
89   return ms * 1000 * 1000;
90 }
91 
92 // Converts the given number of milliseconds to microseconds
MsToUs(uint64_t ms)93 static constexpr uint64_t MsToUs(uint64_t ms) {
94   return ms * 1000;
95 }
96 
UsToNs(uint64_t us)97 static constexpr uint64_t UsToNs(uint64_t us) {
98   return us * 1000;
99 }
100 
SecondsToMs(uint64_t seconds)101 static constexpr uint64_t SecondsToMs(uint64_t seconds) {
102   return seconds * 1000;
103 }
104 
SaturatedTimeT(int64_t secs)105 static constexpr time_t SaturatedTimeT(int64_t secs) {
106   if (sizeof(time_t) < sizeof(int64_t)) {
107     return static_cast<time_t>(std::min(secs,
108                                         static_cast<int64_t>(std::numeric_limits<time_t>::max())));
109   } else {
110     return secs;
111   }
112 }
113 
114 #if defined(__APPLE__)
115 #ifndef CLOCK_REALTIME
116 // No clocks to specify on OS/X < 10.12, fake value to pass to routines that require a clock.
117 #define CLOCK_REALTIME 0xebadf00d
118 #endif
119 #endif
120 
121 // Sleep for the given number of nanoseconds, a bad way to handle contention.
122 void NanoSleep(uint64_t ns);
123 
124 // Initialize a timespec to either a relative time (ms,ns), or to the absolute
125 // time corresponding to the indicated clock value plus the supplied offset.
126 void InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts);
127 
128 // Converts `timespec` to nanoseconds. The return value can be negative, which should be interpreted
129 // as a time before the epoch.
TimeSpecToNs(timespec ts)130 static constexpr int64_t TimeSpecToNs(timespec ts) {
131   DCHECK_GE(ts.tv_nsec, 0);  // According to POSIX.
132   return static_cast<int64_t>(ts.tv_sec) * INT64_C(1000000000) + ts.tv_nsec;
133 }
134 
135 }  // namespace art
136 
137 #endif  // ART_LIBARTBASE_BASE_TIME_UTILS_H_
138