SystemClock.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (C) 2008 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * System clock functions.
  18. */
  19. #define LOG_TAG "SystemClock"
  20. #include <utils/SystemClock.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <time.h>
  24. #include <cutils/compiler.h>
  25. #include <utils/Timers.h>
  26. #include <utils/Log.h>
  27. namespace android {
  28. /*
  29. * native public static long uptimeMillis();
  30. */
  31. int64_t uptimeMillis()
  32. {
  33. int64_t when = systemTime(SYSTEM_TIME_MONOTONIC);
  34. return (int64_t) nanoseconds_to_milliseconds(when);
  35. }
  36. /*
  37. * native public static long elapsedRealtime();
  38. */
  39. int64_t elapsedRealtime()
  40. {
  41. return nanoseconds_to_milliseconds(elapsedRealtimeNano());
  42. }
  43. /*
  44. * native public static long elapsedRealtimeNano();
  45. */
  46. int64_t elapsedRealtimeNano()
  47. {
  48. #if defined(__linux__)
  49. struct timespec ts;
  50. int err = clock_gettime(CLOCK_BOOTTIME, &ts);
  51. if (CC_UNLIKELY(err)) {
  52. // This should never happen, but just in case ...
  53. ALOGE("clock_gettime(CLOCK_BOOTTIME) failed: %s", strerror(errno));
  54. return 0;
  55. }
  56. return seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
  57. #else
  58. return systemTime(SYSTEM_TIME_MONOTONIC);
  59. #endif
  60. }
  61. }; // namespace android