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