diff options
author | 2018-03-02 12:01:51 -0800 | |
---|---|---|
committer | 2018-03-05 13:58:20 -0800 | |
commit | c431b9dc4b23cc950eb313695258df5d89f53b22 (patch) | |
tree | 422273559c3ae52caff0c6b1cf1a62a8312f0e26 /libartbase/base/time_utils.h | |
parent | f46f46cf5bd32788d5252b7107628a66594a5e98 (diff) |
Move most of runtime/base to libartbase/base
Enforce the layering that code in runtime/base should not depend on
runtime by separating it into libartbase. Some of the code in
runtime/base depends on the Runtime class, so it cannot be moved yet.
Also, some of the tests depend on CommonRuntimeTest, which itself needs
to be factored (in a subsequent CL).
Bug: 22322814
Test: make -j 50 checkbuild
make -j 50 test-art-host
Change-Id: I8b096c1e2542f829eb456b4b057c71421b77d7e2
Diffstat (limited to 'libartbase/base/time_utils.h')
-rw-r--r-- | libartbase/base/time_utils.h | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/libartbase/base/time_utils.h b/libartbase/base/time_utils.h new file mode 100644 index 0000000000..811af5d682 --- /dev/null +++ b/libartbase/base/time_utils.h @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ART_LIBARTBASE_BASE_TIME_UTILS_H_ +#define ART_LIBARTBASE_BASE_TIME_UTILS_H_ + +#include <stdint.h> +#include <time.h> + +#include <string> + +#include "base/macros.h" + +namespace art { + +enum TimeUnit { + kTimeUnitNanosecond, + kTimeUnitMicrosecond, + kTimeUnitMillisecond, + kTimeUnitSecond, +}; + +// Returns a human-readable time string which prints every nanosecond while trying to limit the +// number of trailing zeros. Prints using the largest human readable unit up to a second. +// e.g. "1ms", "1.000000001s", "1.001us" +std::string PrettyDuration(uint64_t nano_duration, size_t max_fraction_digits = 3); + +// Format a nanosecond time to specified units. +std::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit, + size_t max_fraction_digits); + +// Get the appropriate unit for a nanosecond duration. +TimeUnit GetAppropriateTimeUnit(uint64_t nano_duration); + +// Get the divisor to convert from a nanoseconds to a time unit. +uint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit); + +// Returns the current date in ISO yyyy-mm-dd hh:mm:ss format. +std::string GetIsoDate(); + +// Returns the monotonic time since some unspecified starting point in milliseconds. +uint64_t MilliTime(); + +// Returns the monotonic time since some unspecified starting point in microseconds. +uint64_t MicroTime(); + +// Returns the monotonic time since some unspecified starting point in nanoseconds. +uint64_t NanoTime(); + +// Returns the thread-specific CPU-time clock in nanoseconds or -1 if unavailable. +uint64_t ThreadCpuNanoTime(); + +// Returns the process CPU-time clock in nanoseconds or -1 if unavailable. +uint64_t ProcessCpuNanoTime(); + +// Converts the given number of nanoseconds to milliseconds. +static constexpr inline uint64_t NsToMs(uint64_t ns) { + return ns / 1000 / 1000; +} + +// Converts the given number of milliseconds to nanoseconds +static constexpr inline uint64_t MsToNs(uint64_t ms) { + return ms * 1000 * 1000; +} + +// Converts the given number of milliseconds to microseconds +static constexpr inline uint64_t MsToUs(uint64_t ms) { + return ms * 1000; +} + +static constexpr inline uint64_t UsToNs(uint64_t us) { + return us * 1000; +} + +#if defined(__APPLE__) +#ifndef CLOCK_REALTIME +// No clocks to specify on OS/X < 10.12, fake value to pass to routines that require a clock. +#define CLOCK_REALTIME 0xebadf00d +#endif +#endif + +// Sleep for the given number of nanoseconds, a bad way to handle contention. +void NanoSleep(uint64_t ns); + +// Initialize a timespec to either a relative time (ms,ns), or to the absolute +// time corresponding to the indicated clock value plus the supplied offset. +void InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts); + +} // namespace art + +#endif // ART_LIBARTBASE_BASE_TIME_UTILS_H_ |