time_fuchsia.cc 2.5 KB

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