time_android.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2018 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. namespace base {
  6. // static
  7. TimeTicks TimeTicks::FromUptimeMillis(int64_t uptime_millis_value) {
  8. // The implementation of the SystemClock.uptimeMillis() in AOSP uses the same
  9. // clock as base::TimeTicks::Now(): clock_gettime(CLOCK_MONOTONIC), see in
  10. // platform/system/code:
  11. // 1. libutils/SystemClock.cpp
  12. // 2. libutils/Timers.cpp
  13. //
  14. // We are not aware of any motivations for Android OEMs to modify the AOSP
  15. // implementation of either uptimeMillis() or clock_gettime(CLOCK_MONOTONIC),
  16. // so we assume that there are no such customizations.
  17. //
  18. // Under these assumptions the conversion is as safe as copying the value of
  19. // base::TimeTicks::Now() with a loss of sub-millisecond precision.
  20. return TimeTicks(uptime_millis_value * Time::kMicrosecondsPerMillisecond);
  21. }
  22. // This file is included on chromeos_ash because it needs to interpret
  23. // UptimeMillis values from the Android container.
  24. #if BUILDFLAG(IS_ANDROID)
  25. // static
  26. TimeTicks TimeTicks::FromJavaNanoTime(int64_t nano_time_value) {
  27. // The implementation of the System.nanoTime() in AOSP uses the same
  28. // clock as UptimeMillis() and base::TimeTicks::Now():
  29. // clock_gettime(CLOCK_MONOTONIC), see ojluni/src/main/native/System.c in
  30. // AOSP.
  31. //
  32. // From Android documentation on android.os.SystemClock:
  33. // [uptimeMillis()] is the basis for most interval timing such as
  34. // Thread.sleep(millls), Object.wait(millis), and System.nanoTime().
  35. //
  36. // We are not aware of any motivations for Android OEMs to modify the AOSP
  37. // implementation of either uptimeMillis(), nanoTime, or
  38. // clock_gettime(CLOCK_MONOTONIC), so we assume that there are no such
  39. // customizations.
  40. //
  41. // Under these assumptions the conversion is as safe as copying the value of
  42. // base::TimeTicks::Now() without the (theoretical) sub-microsecond
  43. // resolution.
  44. return TimeTicks(nano_time_value / Time::kNanosecondsPerMicrosecond);
  45. }
  46. jlong TimeTicks::ToUptimeMillis() const {
  47. // See FromUptimeMillis. UptimeMillis and TimeTicks use the same clock source,
  48. // and only differ in resolution.
  49. return us_ / Time::kMicrosecondsPerMillisecond;
  50. }
  51. jlong TimeTicks::ToUptimeMicros() const {
  52. // Same as ToUptimeMillis but maintains sub-millisecond precision.
  53. return us_;
  54. }
  55. #endif // BUILDFLAG(IS_ANDROID)
  56. } // namespace base