blob: 69c867cfafeea5378a3abe7fb84697bdd045a52c [file] [log] [blame]
Vladimir Marko41b175a2015-05-19 18:08:00 +01001/*
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 Sehrc431b9d2018-03-02 12:01:51 -080017#ifndef ART_LIBARTBASE_BASE_TIME_UTILS_H_
18#define ART_LIBARTBASE_BASE_TIME_UTILS_H_
Vladimir Marko41b175a2015-05-19 18:08:00 +010019
Andreas Gampe2388ae52019-05-09 16:26:08 -070020#ifdef _WIN32
21#include <stdio.h> // Needed for correct macro definitions.
22#endif
23
Vladimir Marko41b175a2015-05-19 18:08:00 +010024#include <time.h>
25
Andreas Gampe2388ae52019-05-09 16:26:08 -070026#include <cstdint>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include <string>
28
Vladimir Marko41b175a2015-05-19 18:08:00 +010029namespace art {
30
31enum TimeUnit {
32 kTimeUnitNanosecond,
33 kTimeUnitMicrosecond,
34 kTimeUnitMillisecond,
35 kTimeUnitSecond,
36};
37
38// Returns a human-readable time string which prints every nanosecond while trying to limit the
39// number of trailing zeros. Prints using the largest human readable unit up to a second.
40// e.g. "1ms", "1.000000001s", "1.001us"
41std::string PrettyDuration(uint64_t nano_duration, size_t max_fraction_digits = 3);
42
43// Format a nanosecond time to specified units.
44std::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit,
45 size_t max_fraction_digits);
46
47// Get the appropriate unit for a nanosecond duration.
48TimeUnit GetAppropriateTimeUnit(uint64_t nano_duration);
49
50// Get the divisor to convert from a nanoseconds to a time unit.
51uint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit);
52
53// Returns the current date in ISO yyyy-mm-dd hh:mm:ss format.
54std::string GetIsoDate();
55
56// Returns the monotonic time since some unspecified starting point in milliseconds.
57uint64_t MilliTime();
58
59// Returns the monotonic time since some unspecified starting point in microseconds.
60uint64_t MicroTime();
61
62// Returns the monotonic time since some unspecified starting point in nanoseconds.
63uint64_t NanoTime();
64
65// Returns the thread-specific CPU-time clock in nanoseconds or -1 if unavailable.
66uint64_t ThreadCpuNanoTime();
67
Andreas Gampec560fc02014-07-16 09:57:39 -070068// Returns the process CPU-time clock in nanoseconds or -1 if unavailable.
69uint64_t ProcessCpuNanoTime();
70
Vladimir Marko41b175a2015-05-19 18:08:00 +010071// Converts the given number of nanoseconds to milliseconds.
72static constexpr inline uint64_t NsToMs(uint64_t ns) {
73 return ns / 1000 / 1000;
74}
75
76// Converts the given number of milliseconds to nanoseconds
Mathieu Chartier3b532d72015-06-05 13:21:05 -070077static constexpr inline uint64_t MsToNs(uint64_t ms) {
78 return ms * 1000 * 1000;
Vladimir Marko41b175a2015-05-19 18:08:00 +010079}
80
Ian Rogers55256cb2017-12-21 17:07:11 -080081// Converts the given number of milliseconds to microseconds
82static constexpr inline uint64_t MsToUs(uint64_t ms) {
83 return ms * 1000;
84}
85
86static constexpr inline uint64_t UsToNs(uint64_t us) {
87 return us * 1000;
88}
89
Vladimir Marko41b175a2015-05-19 18:08:00 +010090#if defined(__APPLE__)
Dan Willemsen0fc1c9a2016-10-26 10:13:15 -070091#ifndef CLOCK_REALTIME
92// No clocks to specify on OS/X < 10.12, fake value to pass to routines that require a clock.
Vladimir Marko41b175a2015-05-19 18:08:00 +010093#define CLOCK_REALTIME 0xebadf00d
94#endif
Dan Willemsen0fc1c9a2016-10-26 10:13:15 -070095#endif
Vladimir Marko41b175a2015-05-19 18:08:00 +010096
97// Sleep for the given number of nanoseconds, a bad way to handle contention.
98void NanoSleep(uint64_t ns);
99
100// Initialize a timespec to either a relative time (ms,ns), or to the absolute
101// time corresponding to the indicated clock value plus the supplied offset.
102void InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts);
103
104} // namespace art
105
David Sehrc431b9d2018-03-02 12:01:51 -0800106#endif // ART_LIBARTBASE_BASE_TIME_UTILS_H_