#ifndef INCLUDE_PERFETTO_BASE_TIME_H_
#define INCLUDE_PERFETTO_BASE_TIME_H_
#include <time.h>
#include <chrono>
#include <optional>
#include <string>
#include "perfetto/base/build_config.h"
#include "perfetto/base/logging.h"
#if PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
#include <mach/mach_init.h>
#include <mach/mach_port.h>
#include <mach/mach_time.h>
#include <mach/thread_act.h>
#endif
#if PERFETTO_BUILDFLAG(PERFETTO_OS_WASM)
#include <emscripten/emscripten.h>
#endif
#if PERFETTO_BUILDFLAG(PERFETTO_ARCH_CPU_X86_64)
#if PERFETTO_BUILDFLAG(PERFETTO_COMPILER_MSVC)
#include <intrin.h>
#endif
#endif
namespace perfetto {
namespace base {
TimeSeconds;
TimeMillis;
TimeNanos;
inline TimeNanos FromPosixTimespec(const struct timespec& ts) { … }
void SleepMicroseconds(unsigned interval_us);
void InitializeTime();
#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
TimeNanos GetWallTimeNs();
TimeNanos GetThreadCPUTimeNs();
inline TimeNanos GetWallTimeRawNs() {
return GetWallTimeNs();
}
inline TimeNanos GetBootTimeNs() {
return GetWallTimeNs();
}
#elif PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
inline TimeNanos GetWallTimeNs() {
auto init_timebase_info = []() -> mach_timebase_info_data_t {
mach_timebase_info_data_t timebase_info;
mach_timebase_info(&timebase_info);
return timebase_info;
};
static mach_timebase_info_data_t timebase_info = init_timebase_info();
uint64_t mach_time = mach_absolute_time();
if (timebase_info.numer == timebase_info.denom) {
return TimeNanos(mach_time);
}
uint64_t nanoseconds = mach_time / timebase_info.denom;
const uint64_t mach_time_remainder = mach_time % timebase_info.denom;
PERFETTO_CHECK(!__builtin_umulll_overflow(nanoseconds, timebase_info.numer,
&nanoseconds));
uint64_t least_significant_nanoseconds =
(mach_time_remainder * timebase_info.numer) / timebase_info.denom;
PERFETTO_CHECK(!__builtin_uaddll_overflow(
nanoseconds, least_significant_nanoseconds, &nanoseconds));
return TimeNanos(nanoseconds);
}
inline TimeNanos GetWallTimeRawNs() {
return GetWallTimeNs();
}
inline TimeNanos GetBootTimeNs() {
return GetWallTimeNs();
}
#if __MAC_OS_X_VERSION_MIN_REQUIRED < 101200
inline TimeNanos GetThreadCPUTimeNs() {
mach_port_t this_thread = mach_thread_self();
mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
thread_basic_info_data_t info{};
kern_return_t kr =
thread_info(this_thread, THREAD_BASIC_INFO,
reinterpret_cast<thread_info_t>(&info), &count);
mach_port_deallocate(mach_task_self(), this_thread);
if (kr != KERN_SUCCESS) {
PERFETTO_DFATAL("Failed to get CPU time.");
return TimeNanos(0);
}
return TimeNanos(info.user_time.seconds * 1000000000LL +
info.user_time.microseconds * 1000LL +
info.system_time.seconds * 1000000000LL +
info.system_time.microseconds * 1000LL);
}
#else
inline TimeNanos GetThreadCPUTimeNs() {
struct timespec ts = {};
PERFETTO_CHECK(clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0);
return FromPosixTimespec(ts);
}
#endif
#elif PERFETTO_BUILDFLAG(PERFETTO_OS_WASM)
inline TimeNanos GetWallTimeNs() {
return TimeNanos(static_cast<uint64_t>(emscripten_get_now()) * 1000000);
}
inline TimeNanos GetWallTimeRawNs() {
return GetWallTimeNs();
}
inline TimeNanos GetThreadCPUTimeNs() {
return TimeNanos(0);
}
inline TimeNanos GetBootTimeNs() {
return GetWallTimeNs();
}
#elif PERFETTO_BUILDFLAG(PERFETTO_OS_NACL)
inline TimeNanos GetWallTimeNs() {
return TimeNanos(0);
}
inline TimeNanos GetWallTimeRawNs() {
return TimeNanos(0);
}
inline TimeNanos GetThreadCPUTimeNs() {
return TimeNanos(0);
}
inline TimeNanos GetBootTimeNs() {
return TimeNanos(0);
}
#else
constexpr clockid_t kWallTimeClockSource = …;
inline TimeNanos GetTimeInternalNs(clockid_t clk_id) { … }
inline TimeNanos GetBootTimeNs() { … }
inline TimeNanos GetWallTimeNs() { … }
inline TimeNanos GetWallTimeRawNs() { … }
inline TimeNanos GetThreadCPUTimeNs() { … }
#endif
inline TimeSeconds GetBootTimeS() { … }
inline TimeMillis GetBootTimeMs() { … }
inline TimeMillis GetWallTimeMs() { … }
inline TimeSeconds GetWallTimeS() { … }
inline struct timespec ToPosixTimespec(TimeMillis time) { … }
std::string GetTimeFmt(const std::string& fmt);
inline int64_t TimeGm(struct tm* tms) { … }
inline int64_t MkTime(int year, int month, int day, int h, int m, int s) { … }
#if PERFETTO_BUILDFLAG(PERFETTO_ARCH_CPU_X86_64)
inline uint64_t Rdtsc() { … }
#endif
std::optional<int32_t> GetTimezoneOffsetMins();
}
}
#endif