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" |
| 39 | #include "utils/Log.h" |
| 40 | |
| 41 | namespace android { |
| 42 | |
| 43 | /* |
| 44 | * Set the current time. This only works when running as root. |
| 45 | */ |
| 46 | int setCurrentTimeMillis(int64_t millis) |
| 47 | { |
| 48 | #if WIN32 |
| 49 | // not implemented |
| 50 | return -1; |
| 51 | #else |
| 52 | struct timeval tv; |
Kenny Root | af1cf07 | 2011-02-16 10:13:53 -0800 | [diff] [blame] | 53 | #ifdef HAVE_ANDROID_OS |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | struct timespec ts; |
| 55 | int fd; |
| 56 | int res; |
| 57 | #endif |
| 58 | int ret = 0; |
| 59 | |
| 60 | if (millis <= 0 || millis / 1000LL >= INT_MAX) { |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | tv.tv_sec = (time_t) (millis / 1000LL); |
| 65 | tv.tv_usec = (suseconds_t) ((millis % 1000LL) * 1000LL); |
| 66 | |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 67 | ALOGD("Setting time of day to sec=%d\n", (int) tv.tv_sec); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
Kenny Root | af1cf07 | 2011-02-16 10:13:53 -0800 | [diff] [blame] | 69 | #ifdef HAVE_ANDROID_OS |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | fd = open("/dev/alarm", O_RDWR); |
| 71 | if(fd < 0) { |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 72 | ALOGW("Unable to open alarm driver: %s\n", strerror(errno)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | return -1; |
| 74 | } |
| 75 | ts.tv_sec = tv.tv_sec; |
| 76 | ts.tv_nsec = tv.tv_usec * 1000; |
| 77 | res = ioctl(fd, ANDROID_ALARM_SET_RTC, &ts); |
| 78 | if(res < 0) { |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 79 | ALOGW("Unable to set rtc to %ld: %s\n", tv.tv_sec, strerror(errno)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | ret = -1; |
| 81 | } |
| 82 | close(fd); |
| 83 | #else |
| 84 | if (settimeofday(&tv, NULL) != 0) { |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 85 | ALOGW("Unable to set clock to %d.%d: %s\n", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | (int) tv.tv_sec, (int) tv.tv_usec, strerror(errno)); |
| 87 | ret = -1; |
| 88 | } |
| 89 | #endif |
| 90 | |
| 91 | return ret; |
| 92 | #endif // WIN32 |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * native public static long uptimeMillis(); |
| 97 | */ |
| 98 | int64_t uptimeMillis() |
| 99 | { |
| 100 | int64_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
| 101 | return (int64_t) nanoseconds_to_milliseconds(when); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * native public static long elapsedRealtime(); |
| 106 | */ |
| 107 | int64_t elapsedRealtime() |
| 108 | { |
Nick Pelly | 074cd0c | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 109 | return nanoseconds_to_milliseconds(elapsedRealtimeNano()); |
| 110 | } |
| 111 | |
Ben Cheng | 8dc7c6e | 2012-09-14 14:45:34 -0700 | [diff] [blame] | 112 | #define METHOD_CLOCK_GETTIME 0 |
| 113 | #define METHOD_IOCTL 1 |
| 114 | #define METHOD_SYSTEMTIME 2 |
| 115 | |
| 116 | static const char *gettime_method_names[] = { |
| 117 | "clock_gettime", |
| 118 | "ioctl", |
| 119 | "systemTime", |
| 120 | }; |
| 121 | |
| 122 | static inline void checkTimeStamps(int64_t timestamp, |
| 123 | int64_t volatile *prevTimestampPtr, |
| 124 | int volatile *prevMethodPtr, |
| 125 | int curMethod) |
| 126 | { |
| 127 | /* |
| 128 | * Disable the check for SDK since the prebuilt toolchain doesn't contain |
| 129 | * gettid, and int64_t is different on the ARM platform |
| 130 | * (ie long vs long long). |
| 131 | */ |
| 132 | #ifdef ARCH_ARM |
| 133 | int64_t prevTimestamp = *prevTimestampPtr; |
| 134 | int prevMethod = *prevMethodPtr; |
| 135 | |
| 136 | if (timestamp < prevTimestamp) { |
| 137 | ALOGW("time going backwards: prev %lld(%s) vs now %lld(%s), tid=%d", |
| 138 | prevTimestamp, gettime_method_names[prevMethod], |
| 139 | timestamp, gettime_method_names[curMethod], |
| 140 | gettid()); |
| 141 | } |
| 142 | // NOTE - not atomic and may generate spurious warnings if the 64-bit |
| 143 | // write is interrupted or not observed as a whole. |
| 144 | *prevTimestampPtr = timestamp; |
| 145 | *prevMethodPtr = curMethod; |
| 146 | #endif |
| 147 | } |
| 148 | |
Nick Pelly | 074cd0c | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 149 | /* |
| 150 | * native public static long elapsedRealtimeNano(); |
| 151 | */ |
| 152 | int64_t elapsedRealtimeNano() |
| 153 | { |
Kenny Root | af1cf07 | 2011-02-16 10:13:53 -0800 | [diff] [blame] | 154 | #ifdef HAVE_ANDROID_OS |
Nick Pelly | 074cd0c | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 155 | struct timespec ts; |
| 156 | int result = clock_gettime(CLOCK_BOOTTIME, &ts); |
Ben Cheng | 8dc7c6e | 2012-09-14 14:45:34 -0700 | [diff] [blame] | 157 | int64_t timestamp; |
| 158 | static volatile int64_t prevTimestamp; |
| 159 | static volatile int prevMethod; |
| 160 | |
Nick Pelly | 074cd0c | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 161 | if (result == 0) { |
Ben Cheng | 8dc7c6e | 2012-09-14 14:45:34 -0700 | [diff] [blame] | 162 | timestamp = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec; |
| 163 | checkTimeStamps(timestamp, &prevTimestamp, &prevMethod, |
| 164 | METHOD_CLOCK_GETTIME); |
| 165 | return timestamp; |
Nick Pelly | 074cd0c | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // CLOCK_BOOTTIME doesn't exist, fallback to /dev/alarm |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | static int s_fd = -1; |
| 170 | |
| 171 | if (s_fd == -1) { |
| 172 | int fd = open("/dev/alarm", O_RDONLY); |
| 173 | if (android_atomic_cmpxchg(-1, fd, &s_fd)) { |
| 174 | close(fd); |
| 175 | } |
| 176 | } |
| 177 | |
Ben Cheng | 8dc7c6e | 2012-09-14 14:45:34 -0700 | [diff] [blame] | 178 | result = ioctl(s_fd, |
| 179 | ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME), &ts); |
| 180 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | if (result == 0) { |
Ben Cheng | 8dc7c6e | 2012-09-14 14:45:34 -0700 | [diff] [blame] | 182 | timestamp = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec; |
| 183 | checkTimeStamps(timestamp, &prevTimestamp, &prevMethod, METHOD_IOCTL); |
| 184 | return timestamp; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | } |
Nick Pelly | 074cd0c | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 186 | |
| 187 | // XXX: there was an error, probably because the driver didn't |
| 188 | // exist ... this should return |
| 189 | // a real error, like an exception! |
Ben Cheng | 8dc7c6e | 2012-09-14 14:45:34 -0700 | [diff] [blame] | 190 | timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 191 | checkTimeStamps(timestamp, &prevTimestamp, &prevMethod, |
| 192 | METHOD_SYSTEMTIME); |
| 193 | return timestamp; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | #else |
Nick Pelly | 074cd0c | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 195 | return systemTime(SYSTEM_TIME_MONOTONIC); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | #endif |
| 197 | } |
| 198 | |
| 199 | }; // namespace android |