The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 18 | /* |
| 19 | * System clock functions. |
| 20 | */ |
| 21 | |
Kenny Root | af1cf07 | 2011-02-16 10:13:53 -0800 | [diff] [blame] | 22 | #ifdef HAVE_ANDROID_OS |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <linux/ioctl.h> |
| 24 | #include <linux/rtc.h> |
| 25 | #include <utils/Atomic.h> |
| 26 | #include <linux/android_alarm.h> |
| 27 | #endif |
| 28 | |
| 29 | #include <sys/time.h> |
| 30 | #include <limits.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <errno.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #include <utils/SystemClock.h> |
| 36 | #include <utils/Timers.h> |
| 37 | |
| 38 | #define LOG_TAG "SystemClock" |
Mathias Agopian | 002e1e5 | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 39 | #include <utils/Log.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
| 41 | namespace android { |
| 42 | |
| 43 | /* |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | * native public static long uptimeMillis(); |
| 45 | */ |
| 46 | int64_t uptimeMillis() |
| 47 | { |
| 48 | int64_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
| 49 | return (int64_t) nanoseconds_to_milliseconds(when); |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * native public static long elapsedRealtime(); |
| 54 | */ |
| 55 | int64_t elapsedRealtime() |
| 56 | { |
Nick Pelly | 074cd0c | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 57 | return nanoseconds_to_milliseconds(elapsedRealtimeNano()); |
| 58 | } |
| 59 | |
Ben Cheng | 8dc7c6e | 2012-09-14 14:45:34 -0700 | [diff] [blame] | 60 | #define METHOD_CLOCK_GETTIME 0 |
| 61 | #define METHOD_IOCTL 1 |
| 62 | #define METHOD_SYSTEMTIME 2 |
| 63 | |
| 64 | static const char *gettime_method_names[] = { |
| 65 | "clock_gettime", |
| 66 | "ioctl", |
| 67 | "systemTime", |
| 68 | }; |
| 69 | |
| 70 | static inline void checkTimeStamps(int64_t timestamp, |
| 71 | int64_t volatile *prevTimestampPtr, |
| 72 | int volatile *prevMethodPtr, |
| 73 | int curMethod) |
| 74 | { |
| 75 | /* |
| 76 | * Disable the check for SDK since the prebuilt toolchain doesn't contain |
| 77 | * gettid, and int64_t is different on the ARM platform |
| 78 | * (ie long vs long long). |
| 79 | */ |
| 80 | #ifdef ARCH_ARM |
| 81 | int64_t prevTimestamp = *prevTimestampPtr; |
| 82 | int prevMethod = *prevMethodPtr; |
| 83 | |
| 84 | if (timestamp < prevTimestamp) { |
| 85 | ALOGW("time going backwards: prev %lld(%s) vs now %lld(%s), tid=%d", |
| 86 | prevTimestamp, gettime_method_names[prevMethod], |
| 87 | timestamp, gettime_method_names[curMethod], |
| 88 | gettid()); |
| 89 | } |
| 90 | // NOTE - not atomic and may generate spurious warnings if the 64-bit |
| 91 | // write is interrupted or not observed as a whole. |
| 92 | *prevTimestampPtr = timestamp; |
| 93 | *prevMethodPtr = curMethod; |
| 94 | #endif |
| 95 | } |
| 96 | |
Nick Pelly | 074cd0c | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 97 | /* |
| 98 | * native public static long elapsedRealtimeNano(); |
| 99 | */ |
| 100 | int64_t elapsedRealtimeNano() |
| 101 | { |
Kenny Root | af1cf07 | 2011-02-16 10:13:53 -0800 | [diff] [blame] | 102 | #ifdef HAVE_ANDROID_OS |
Nick Pelly | 074cd0c | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 103 | struct timespec ts; |
Ben Cheng | 971f7e9 | 2012-09-19 14:53:10 -0700 | [diff] [blame] | 104 | int result; |
Ben Cheng | 8dc7c6e | 2012-09-14 14:45:34 -0700 | [diff] [blame] | 105 | int64_t timestamp; |
| 106 | static volatile int64_t prevTimestamp; |
| 107 | static volatile int prevMethod; |
| 108 | |
Ben Cheng | 971f7e9 | 2012-09-19 14:53:10 -0700 | [diff] [blame] | 109 | #if 0 |
| 110 | /* |
| 111 | * b/7100774 |
| 112 | * clock_gettime appears to have clock skews and can sometimes return |
| 113 | * backwards values. Disable its use until we find out what's wrong. |
| 114 | */ |
| 115 | result = clock_gettime(CLOCK_BOOTTIME, &ts); |
Nick Pelly | 074cd0c | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 116 | if (result == 0) { |
Ben Cheng | 8dc7c6e | 2012-09-14 14:45:34 -0700 | [diff] [blame] | 117 | timestamp = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec; |
| 118 | checkTimeStamps(timestamp, &prevTimestamp, &prevMethod, |
| 119 | METHOD_CLOCK_GETTIME); |
| 120 | return timestamp; |
Nick Pelly | 074cd0c | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 121 | } |
Ben Cheng | 971f7e9 | 2012-09-19 14:53:10 -0700 | [diff] [blame] | 122 | #endif |
Nick Pelly | 074cd0c | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 123 | |
| 124 | // CLOCK_BOOTTIME doesn't exist, fallback to /dev/alarm |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | static int s_fd = -1; |
| 126 | |
| 127 | if (s_fd == -1) { |
| 128 | int fd = open("/dev/alarm", O_RDONLY); |
| 129 | if (android_atomic_cmpxchg(-1, fd, &s_fd)) { |
| 130 | close(fd); |
| 131 | } |
| 132 | } |
| 133 | |
Ben Cheng | 8dc7c6e | 2012-09-14 14:45:34 -0700 | [diff] [blame] | 134 | result = ioctl(s_fd, |
| 135 | ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME), &ts); |
| 136 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | if (result == 0) { |
Ben Cheng | 8dc7c6e | 2012-09-14 14:45:34 -0700 | [diff] [blame] | 138 | timestamp = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec; |
| 139 | checkTimeStamps(timestamp, &prevTimestamp, &prevMethod, METHOD_IOCTL); |
| 140 | return timestamp; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | } |
Nick Pelly | 074cd0c | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 142 | |
| 143 | // XXX: there was an error, probably because the driver didn't |
| 144 | // exist ... this should return |
| 145 | // a real error, like an exception! |
Ben Cheng | 8dc7c6e | 2012-09-14 14:45:34 -0700 | [diff] [blame] | 146 | timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 147 | checkTimeStamps(timestamp, &prevTimestamp, &prevMethod, |
| 148 | METHOD_SYSTEMTIME); |
| 149 | return timestamp; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | #else |
Nick Pelly | 074cd0c | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 151 | return systemTime(SYSTEM_TIME_MONOTONIC); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | #endif |
| 153 | } |
| 154 | |
| 155 | }; // namespace android |