1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2015 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_LIBARTBASE_BASE_TIME_UTILS_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBARTBASE_BASE_TIME_UTILS_H_
19*795d594fSAndroid Build Coastguard Worker
20*795d594fSAndroid Build Coastguard Worker #ifdef _WIN32
21*795d594fSAndroid Build Coastguard Worker #include <stdio.h> // Needed for correct macro definitions.
22*795d594fSAndroid Build Coastguard Worker #endif
23*795d594fSAndroid Build Coastguard Worker
24*795d594fSAndroid Build Coastguard Worker #include <time.h>
25*795d594fSAndroid Build Coastguard Worker
26*795d594fSAndroid Build Coastguard Worker #include <cstdint>
27*795d594fSAndroid Build Coastguard Worker #include <string>
28*795d594fSAndroid Build Coastguard Worker
29*795d594fSAndroid Build Coastguard Worker namespace art {
30*795d594fSAndroid Build Coastguard Worker
31*795d594fSAndroid Build Coastguard Worker enum TimeUnit {
32*795d594fSAndroid Build Coastguard Worker kTimeUnitNanosecond,
33*795d594fSAndroid Build Coastguard Worker kTimeUnitMicrosecond,
34*795d594fSAndroid Build Coastguard Worker kTimeUnitMillisecond,
35*795d594fSAndroid Build Coastguard Worker kTimeUnitSecond,
36*795d594fSAndroid Build Coastguard Worker };
37*795d594fSAndroid Build Coastguard Worker
38*795d594fSAndroid Build Coastguard Worker // Constants for common time periods.
39*795d594fSAndroid Build Coastguard Worker constexpr unsigned int kOneMinuteInSeconds = 60;
40*795d594fSAndroid Build Coastguard Worker constexpr unsigned int kOneHourInSeconds = 60 * kOneMinuteInSeconds;
41*795d594fSAndroid Build Coastguard Worker
42*795d594fSAndroid Build Coastguard Worker // Returns a human-readable time string which prints every nanosecond while trying to limit the
43*795d594fSAndroid Build Coastguard Worker // number of trailing zeros. Prints using the largest human readable unit up to a second.
44*795d594fSAndroid Build Coastguard Worker // e.g. "1ms", "1.000000001s", "1.001us"
45*795d594fSAndroid Build Coastguard Worker std::string PrettyDuration(uint64_t nano_duration, size_t max_fraction_digits = 3);
46*795d594fSAndroid Build Coastguard Worker
47*795d594fSAndroid Build Coastguard Worker // Format a nanosecond time to specified units.
48*795d594fSAndroid Build Coastguard Worker std::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit,
49*795d594fSAndroid Build Coastguard Worker size_t max_fraction_digits);
50*795d594fSAndroid Build Coastguard Worker
51*795d594fSAndroid Build Coastguard Worker // Get the appropriate unit for a nanosecond duration.
52*795d594fSAndroid Build Coastguard Worker TimeUnit GetAppropriateTimeUnit(uint64_t nano_duration);
53*795d594fSAndroid Build Coastguard Worker
54*795d594fSAndroid Build Coastguard Worker // Get the divisor to convert from a nanoseconds to a time unit.
55*795d594fSAndroid Build Coastguard Worker uint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit);
56*795d594fSAndroid Build Coastguard Worker
57*795d594fSAndroid Build Coastguard Worker // Returns the current date in ISO yyyy-mm-dd hh:mm:ss format.
58*795d594fSAndroid Build Coastguard Worker std::string GetIsoDate();
59*795d594fSAndroid Build Coastguard Worker
60*795d594fSAndroid Build Coastguard Worker // Returns the monotonic time since some unspecified starting point in milliseconds.
61*795d594fSAndroid Build Coastguard Worker uint64_t MilliTime();
62*795d594fSAndroid Build Coastguard Worker
63*795d594fSAndroid Build Coastguard Worker // Returns the monotonic time since some unspecified starting point in microseconds.
64*795d594fSAndroid Build Coastguard Worker uint64_t MicroTime();
65*795d594fSAndroid Build Coastguard Worker
66*795d594fSAndroid Build Coastguard Worker // Returns the monotonic time since some unspecified starting point in nanoseconds.
67*795d594fSAndroid Build Coastguard Worker uint64_t NanoTime();
68*795d594fSAndroid Build Coastguard Worker
69*795d594fSAndroid Build Coastguard Worker // Returns the thread-specific CPU-time clock in nanoseconds or -1 if unavailable.
70*795d594fSAndroid Build Coastguard Worker uint64_t ThreadCpuNanoTime();
71*795d594fSAndroid Build Coastguard Worker
72*795d594fSAndroid Build Coastguard Worker // Returns the process CPU-time clock in nanoseconds or -1 if unavailable.
73*795d594fSAndroid Build Coastguard Worker uint64_t ProcessCpuNanoTime();
74*795d594fSAndroid Build Coastguard Worker
75*795d594fSAndroid Build Coastguard Worker // Converts the given number of nanoseconds to milliseconds.
NsToMs(uint64_t ns)76*795d594fSAndroid Build Coastguard Worker static constexpr uint64_t NsToMs(uint64_t ns) {
77*795d594fSAndroid Build Coastguard Worker return ns / 1000 / 1000;
78*795d594fSAndroid Build Coastguard Worker }
79*795d594fSAndroid Build Coastguard Worker
80*795d594fSAndroid Build Coastguard Worker // Converts the given number of nanoseconds to microseconds.
NsToUs(uint64_t ns)81*795d594fSAndroid Build Coastguard Worker static constexpr uint64_t NsToUs(uint64_t ns) {
82*795d594fSAndroid Build Coastguard Worker return ns / 1000;
83*795d594fSAndroid Build Coastguard Worker }
84*795d594fSAndroid Build Coastguard Worker
85*795d594fSAndroid Build Coastguard Worker // Converts the given number of milliseconds to nanoseconds
MsToNs(uint64_t ms)86*795d594fSAndroid Build Coastguard Worker static constexpr uint64_t MsToNs(uint64_t ms) {
87*795d594fSAndroid Build Coastguard Worker return ms * 1000 * 1000;
88*795d594fSAndroid Build Coastguard Worker }
89*795d594fSAndroid Build Coastguard Worker
90*795d594fSAndroid Build Coastguard Worker // Converts the given number of milliseconds to microseconds
MsToUs(uint64_t ms)91*795d594fSAndroid Build Coastguard Worker static constexpr uint64_t MsToUs(uint64_t ms) {
92*795d594fSAndroid Build Coastguard Worker return ms * 1000;
93*795d594fSAndroid Build Coastguard Worker }
94*795d594fSAndroid Build Coastguard Worker
UsToNs(uint64_t us)95*795d594fSAndroid Build Coastguard Worker static constexpr uint64_t UsToNs(uint64_t us) {
96*795d594fSAndroid Build Coastguard Worker return us * 1000;
97*795d594fSAndroid Build Coastguard Worker }
98*795d594fSAndroid Build Coastguard Worker
SecondsToMs(uint64_t seconds)99*795d594fSAndroid Build Coastguard Worker static constexpr uint64_t SecondsToMs(uint64_t seconds) {
100*795d594fSAndroid Build Coastguard Worker return seconds * 1000;
101*795d594fSAndroid Build Coastguard Worker }
102*795d594fSAndroid Build Coastguard Worker
SaturatedTimeT(int64_t secs)103*795d594fSAndroid Build Coastguard Worker static constexpr time_t SaturatedTimeT(int64_t secs) {
104*795d594fSAndroid Build Coastguard Worker if (sizeof(time_t) < sizeof(int64_t)) {
105*795d594fSAndroid Build Coastguard Worker return static_cast<time_t>(std::min(secs,
106*795d594fSAndroid Build Coastguard Worker static_cast<int64_t>(std::numeric_limits<time_t>::max())));
107*795d594fSAndroid Build Coastguard Worker } else {
108*795d594fSAndroid Build Coastguard Worker return secs;
109*795d594fSAndroid Build Coastguard Worker }
110*795d594fSAndroid Build Coastguard Worker }
111*795d594fSAndroid Build Coastguard Worker
112*795d594fSAndroid Build Coastguard Worker #if defined(__APPLE__)
113*795d594fSAndroid Build Coastguard Worker #ifndef CLOCK_REALTIME
114*795d594fSAndroid Build Coastguard Worker // No clocks to specify on OS/X < 10.12, fake value to pass to routines that require a clock.
115*795d594fSAndroid Build Coastguard Worker #define CLOCK_REALTIME 0xebadf00d
116*795d594fSAndroid Build Coastguard Worker #endif
117*795d594fSAndroid Build Coastguard Worker #endif
118*795d594fSAndroid Build Coastguard Worker
119*795d594fSAndroid Build Coastguard Worker // Sleep for the given number of nanoseconds, a bad way to handle contention.
120*795d594fSAndroid Build Coastguard Worker void NanoSleep(uint64_t ns);
121*795d594fSAndroid Build Coastguard Worker
122*795d594fSAndroid Build Coastguard Worker // Initialize a timespec to either a relative time (ms,ns), or to the absolute
123*795d594fSAndroid Build Coastguard Worker // time corresponding to the indicated clock value plus the supplied offset.
124*795d594fSAndroid Build Coastguard Worker void InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts);
125*795d594fSAndroid Build Coastguard Worker
126*795d594fSAndroid Build Coastguard Worker } // namespace art
127*795d594fSAndroid Build Coastguard Worker
128*795d594fSAndroid Build Coastguard Worker #endif // ART_LIBARTBASE_BASE_TIME_UTILS_H_
129