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 #include <stdint.h>
21 #include <stdio.h>  // Needed for correct _WIN32 build.
22 #include <time.h>
23 
24 #include <string>
25 
26 #include "macros.h"
27 
28 namespace art {
29 
30 enum TimeUnit {
31   kTimeUnitNanosecond,
32   kTimeUnitMicrosecond,
33   kTimeUnitMillisecond,
34   kTimeUnitSecond,
35 };
36 
37 // Returns a human-readable time string which prints every nanosecond while trying to limit the
38 // number of trailing zeros. Prints using the largest human readable unit up to a second.
39 // e.g. "1ms", "1.000000001s", "1.001us"
40 std::string PrettyDuration(uint64_t nano_duration, size_t max_fraction_digits = 3);
41 
42 // Format a nanosecond time to specified units.
43 std::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit,
44                            size_t max_fraction_digits);
45 
46 // Get the appropriate unit for a nanosecond duration.
47 TimeUnit GetAppropriateTimeUnit(uint64_t nano_duration);
48 
49 // Get the divisor to convert from a nanoseconds to a time unit.
50 uint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit);
51 
52 // Returns the current date in ISO yyyy-mm-dd hh:mm:ss format.
53 std::string GetIsoDate();
54 
55 // Returns the monotonic time since some unspecified starting point in milliseconds.
56 uint64_t MilliTime();
57 
58 // Returns the monotonic time since some unspecified starting point in microseconds.
59 uint64_t MicroTime();
60 
61 // Returns the monotonic time since some unspecified starting point in nanoseconds.
62 uint64_t NanoTime();
63 
64 // Returns the thread-specific CPU-time clock in nanoseconds or -1 if unavailable.
65 uint64_t ThreadCpuNanoTime();
66 
67 // Returns the process CPU-time clock in nanoseconds or -1 if unavailable.
68 uint64_t ProcessCpuNanoTime();
69 
70 // Converts the given number of nanoseconds to milliseconds.
NsToMs(uint64_t ns)71 static constexpr inline uint64_t NsToMs(uint64_t ns) {
72   return ns / 1000 / 1000;
73 }
74 
75 // Converts the given number of milliseconds to nanoseconds
MsToNs(uint64_t ms)76 static constexpr inline uint64_t MsToNs(uint64_t ms) {
77   return ms * 1000 * 1000;
78 }
79 
80 // Converts the given number of milliseconds to microseconds
MsToUs(uint64_t ms)81 static constexpr inline uint64_t MsToUs(uint64_t ms) {
82   return ms * 1000;
83 }
84 
UsToNs(uint64_t us)85 static constexpr inline uint64_t UsToNs(uint64_t us) {
86   return us * 1000;
87 }
88 
89 #if defined(__APPLE__)
90 #ifndef CLOCK_REALTIME
91 // No clocks to specify on OS/X < 10.12, fake value to pass to routines that require a clock.
92 #define CLOCK_REALTIME 0xebadf00d
93 #endif
94 #endif
95 
96 // Sleep for the given number of nanoseconds, a bad way to handle contention.
97 void NanoSleep(uint64_t ns);
98 
99 // Initialize a timespec to either a relative time (ms,ns), or to the absolute
100 // time corresponding to the indicated clock value plus the supplied offset.
101 void InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts);
102 
103 }  // namespace art
104 
105 #endif  // ART_LIBARTBASE_BASE_TIME_UTILS_H_
106