blob: ec2d82e0111d466ee08183eb1e99e3fac0a770ef [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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 Rootdafff0b2011-02-16 10:13:53 -080022#ifdef HAVE_ANDROID_OS
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080023#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
41namespace android {
42
43/*
44 * Set the current time. This only works when running as root.
45 */
46int setCurrentTimeMillis(int64_t millis)
47{
48#if WIN32
49 // not implemented
50 return -1;
51#else
52 struct timeval tv;
Kenny Rootdafff0b2011-02-16 10:13:53 -080053#ifdef HAVE_ANDROID_OS
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080054 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 Blockeb095332011-12-20 16:23:08 +000067 ALOGD("Setting time of day to sec=%d\n", (int) tv.tv_sec);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080068
Kenny Rootdafff0b2011-02-16 10:13:53 -080069#ifdef HAVE_ANDROID_OS
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080070 fd = open("/dev/alarm", O_RDWR);
71 if(fd < 0) {
Steve Block61d341b2012-01-05 23:22:43 +000072 ALOGW("Unable to open alarm driver: %s\n", strerror(errno));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080073 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 Block61d341b2012-01-05 23:22:43 +000079 ALOGW("Unable to set rtc to %ld: %s\n", tv.tv_sec, strerror(errno));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080080 ret = -1;
81 }
82 close(fd);
83#else
84 if (settimeofday(&tv, NULL) != 0) {
Steve Block61d341b2012-01-05 23:22:43 +000085 ALOGW("Unable to set clock to %d.%d: %s\n",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080086 (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 */
98int64_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 */
107int64_t elapsedRealtime()
108{
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700109 return nanoseconds_to_milliseconds(elapsedRealtimeNano());
110}
111
Ben Chengad0a9592012-09-14 14:45:34 -0700112#define METHOD_CLOCK_GETTIME 0
113#define METHOD_IOCTL 1
114#define METHOD_SYSTEMTIME 2
115
116static const char *gettime_method_names[] = {
117 "clock_gettime",
118 "ioctl",
119 "systemTime",
120};
121
122static 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 Pellyaf1e7b72012-07-19 09:17:24 -0700149/*
150 * native public static long elapsedRealtimeNano();
151 */
152int64_t elapsedRealtimeNano()
153{
Kenny Rootdafff0b2011-02-16 10:13:53 -0800154#ifdef HAVE_ANDROID_OS
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700155 struct timespec ts;
Ben Chengf4722b52012-09-19 14:53:10 -0700156 int result;
Ben Chengad0a9592012-09-14 14:45:34 -0700157 int64_t timestamp;
158 static volatile int64_t prevTimestamp;
159 static volatile int prevMethod;
160
Ben Chengf4722b52012-09-19 14:53:10 -0700161#if 0
162 /*
163 * b/7100774
164 * clock_gettime appears to have clock skews and can sometimes return
165 * backwards values. Disable its use until we find out what's wrong.
166 */
167 result = clock_gettime(CLOCK_BOOTTIME, &ts);
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700168 if (result == 0) {
Ben Chengad0a9592012-09-14 14:45:34 -0700169 timestamp = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
170 checkTimeStamps(timestamp, &prevTimestamp, &prevMethod,
171 METHOD_CLOCK_GETTIME);
172 return timestamp;
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700173 }
Ben Chengf4722b52012-09-19 14:53:10 -0700174#endif
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700175
176 // CLOCK_BOOTTIME doesn't exist, fallback to /dev/alarm
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800177 static int s_fd = -1;
178
179 if (s_fd == -1) {
180 int fd = open("/dev/alarm", O_RDONLY);
181 if (android_atomic_cmpxchg(-1, fd, &s_fd)) {
182 close(fd);
183 }
184 }
185
Ben Chengad0a9592012-09-14 14:45:34 -0700186 result = ioctl(s_fd,
187 ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME), &ts);
188
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800189 if (result == 0) {
Ben Chengad0a9592012-09-14 14:45:34 -0700190 timestamp = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
191 checkTimeStamps(timestamp, &prevTimestamp, &prevMethod, METHOD_IOCTL);
192 return timestamp;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800193 }
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700194
195 // XXX: there was an error, probably because the driver didn't
196 // exist ... this should return
197 // a real error, like an exception!
Ben Chengad0a9592012-09-14 14:45:34 -0700198 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
199 checkTimeStamps(timestamp, &prevTimestamp, &prevMethod,
200 METHOD_SYSTEMTIME);
201 return timestamp;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800202#else
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700203 return systemTime(SYSTEM_TIME_MONOTONIC);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800204#endif
205}
206
207}; // namespace android