1 // Copyright 2017 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "partition_alloc/partition_alloc_base/time/time.h" 6 7 #include <threads.h> 8 #include <zircon/syscalls.h> 9 #include <zircon/threads.h> 10 11 #include "partition_alloc/partition_alloc_base/check.h" 12 #include "partition_alloc/partition_alloc_base/time/time_override.h" 13 14 namespace partition_alloc::internal::base { 15 16 // Time ----------------------------------------------------------------------- 17 18 namespace subtle { TimeNowIgnoringOverride()19Time TimeNowIgnoringOverride() { 20 timespec ts; 21 int status = timespec_get(&ts, TIME_UTC); 22 PA_BASE_CHECK(status != 0); 23 return Time::FromTimeSpec(ts); 24 } 25 TimeNowFromSystemTimeIgnoringOverride()26Time TimeNowFromSystemTimeIgnoringOverride() { 27 // Just use TimeNowIgnoringOverride() because it returns the system time. 28 return TimeNowIgnoringOverride(); 29 } 30 } // namespace subtle 31 32 // TimeTicks ------------------------------------------------------------------ 33 34 namespace subtle { TimeTicksNowIgnoringOverride()35TimeTicks TimeTicksNowIgnoringOverride() { 36 const zx_time_t nanos_since_boot = zx_clock_get_monotonic(); 37 PA_BASE_CHECK(0 != nanos_since_boot); 38 return TimeTicks::FromZxTime(nanos_since_boot); 39 } 40 } // namespace subtle 41 42 // static FromZxDuration(zx_duration_t nanos)43TimeDelta TimeDelta::FromZxDuration(zx_duration_t nanos) { 44 return Nanoseconds(nanos); 45 } 46 ToZxDuration() const47zx_duration_t TimeDelta::ToZxDuration() const { 48 return InNanoseconds(); 49 } 50 51 // static FromZxTime(zx_time_t nanos_since_unix_epoch)52Time Time::FromZxTime(zx_time_t nanos_since_unix_epoch) { 53 return UnixEpoch() + Nanoseconds(nanos_since_unix_epoch); 54 } 55 ToZxTime() const56zx_time_t Time::ToZxTime() const { 57 return (*this - UnixEpoch()).InNanoseconds(); 58 } 59 60 // static GetClock()61TimeTicks::Clock TimeTicks::GetClock() { 62 return Clock::FUCHSIA_ZX_CLOCK_MONOTONIC; 63 } 64 65 // static IsHighResolution()66bool TimeTicks::IsHighResolution() { 67 return true; 68 } 69 70 // static IsConsistentAcrossProcesses()71bool TimeTicks::IsConsistentAcrossProcesses() { 72 return true; 73 } 74 75 // static FromZxTime(zx_time_t nanos_since_boot)76TimeTicks TimeTicks::FromZxTime(zx_time_t nanos_since_boot) { 77 return TimeTicks() + Nanoseconds(nanos_since_boot); 78 } 79 ToZxTime() const80zx_time_t TimeTicks::ToZxTime() const { 81 return (*this - TimeTicks()).InNanoseconds(); 82 } 83 84 // ThreadTicks ---------------------------------------------------------------- 85 86 namespace subtle { ThreadTicksNowIgnoringOverride()87ThreadTicks ThreadTicksNowIgnoringOverride() { 88 zx_info_thread_stats_t info; 89 zx_status_t status = zx_object_get_info(thrd_get_zx_handle(thrd_current()), 90 ZX_INFO_THREAD_STATS, &info, 91 sizeof(info), nullptr, nullptr); 92 PA_BASE_CHECK(status == ZX_OK); 93 return ThreadTicks() + Nanoseconds(info.total_runtime); 94 } 95 } // namespace subtle 96 97 } // namespace partition_alloc::internal::base 98