xref: /aosp_15_r20/external/cronet/base/time/time_fuchsia.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 "base/time/time.h"
6 
7 #include <threads.h>
8 #include <zircon/syscalls.h>
9 #include <zircon/threads.h>
10 
11 #include "base/check_op.h"
12 #include "base/fuchsia/fuchsia_logging.h"
13 #include "base/time/time_override.h"
14 
15 namespace base {
16 
17 // Time -----------------------------------------------------------------------
18 
19 namespace subtle {
TimeNowIgnoringOverride()20 Time TimeNowIgnoringOverride() {
21   timespec ts;
22   int status = timespec_get(&ts, TIME_UTC);
23   CHECK(status != 0);
24   return Time::FromTimeSpec(ts);
25 }
26 
TimeNowFromSystemTimeIgnoringOverride()27 Time TimeNowFromSystemTimeIgnoringOverride() {
28   // Just use TimeNowIgnoringOverride() because it returns the system time.
29   return TimeNowIgnoringOverride();
30 }
31 }  // namespace subtle
32 
33 // TimeTicks ------------------------------------------------------------------
34 
35 namespace subtle {
TimeTicksNowIgnoringOverride()36 TimeTicks TimeTicksNowIgnoringOverride() {
37   const zx_time_t nanos_since_boot = zx_clock_get_monotonic();
38   CHECK_NE(0, nanos_since_boot);
39   return TimeTicks::FromZxTime(nanos_since_boot);
40 }
41 }  // namespace subtle
42 
43 // static
FromZxDuration(zx_duration_t nanos)44 TimeDelta TimeDelta::FromZxDuration(zx_duration_t nanos) {
45   return Nanoseconds(nanos);
46 }
47 
ToZxDuration() const48 zx_duration_t TimeDelta::ToZxDuration() const {
49   return InNanoseconds();
50 }
51 
52 // static
FromZxTime(zx_time_t nanos_since_unix_epoch)53 Time Time::FromZxTime(zx_time_t nanos_since_unix_epoch) {
54   return UnixEpoch() + Nanoseconds(nanos_since_unix_epoch);
55 }
56 
ToZxTime() const57 zx_time_t Time::ToZxTime() const {
58   return (*this - UnixEpoch()).InNanoseconds();
59 }
60 
61 // static
GetClock()62 TimeTicks::Clock TimeTicks::GetClock() {
63   return Clock::FUCHSIA_ZX_CLOCK_MONOTONIC;
64 }
65 
66 // static
IsHighResolution()67 bool TimeTicks::IsHighResolution() {
68   return true;
69 }
70 
71 // static
IsConsistentAcrossProcesses()72 bool TimeTicks::IsConsistentAcrossProcesses() {
73   return true;
74 }
75 
76 // static
FromZxTime(zx_time_t nanos_since_boot)77 TimeTicks TimeTicks::FromZxTime(zx_time_t nanos_since_boot) {
78   return TimeTicks() + Nanoseconds(nanos_since_boot);
79 }
80 
ToZxTime() const81 zx_time_t TimeTicks::ToZxTime() const {
82   return (*this - TimeTicks()).InNanoseconds();
83 }
84 
85 // ThreadTicks ----------------------------------------------------------------
86 
87 namespace subtle {
ThreadTicksNowIgnoringOverride()88 ThreadTicks ThreadTicksNowIgnoringOverride() {
89   zx_info_thread_stats_t info;
90   zx_status_t status = zx_object_get_info(thrd_get_zx_handle(thrd_current()),
91                                           ZX_INFO_THREAD_STATS, &info,
92                                           sizeof(info), nullptr, nullptr);
93   ZX_CHECK(status == ZX_OK, status);
94   return ThreadTicks() + Nanoseconds(info.total_runtime);
95 }
96 }  // namespace subtle
97 
98 }  // namespace base
99