blob: ac8da8871fedff089843088dad86a14f816a4413 [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"
Mathias Agopian9eb2a3b2013-05-06 20:20:50 -070039#include <utils/Log.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080040
41namespace android {
42
43/*
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080044 * native public static long uptimeMillis();
45 */
46int64_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 */
55int64_t elapsedRealtime()
56{
Nick Pellyaf1e7b72012-07-19 09:17:24 -070057 return nanoseconds_to_milliseconds(elapsedRealtimeNano());
58}
59
Ben Chengad0a9592012-09-14 14:45:34 -070060#define METHOD_CLOCK_GETTIME 0
61#define METHOD_IOCTL 1
62#define METHOD_SYSTEMTIME 2
63
Ben Cheng5cd11812013-09-23 22:39:15 -070064/*
65 * To debug/verify the timestamps returned by the kernel, change
66 * DEBUG_TIMESTAMP to 1 and call the timestamp routine from a single thread
67 * in the test program. b/10899829
68 */
69#define DEBUG_TIMESTAMP 0
70
Ben Chengad0a9592012-09-14 14:45:34 -070071static const char *gettime_method_names[] = {
72 "clock_gettime",
73 "ioctl",
74 "systemTime",
75};
76
Ben Cheng5cd11812013-09-23 22:39:15 -070077#if DEBUG_TIMESTAMP
Ben Chengad0a9592012-09-14 14:45:34 -070078static inline void checkTimeStamps(int64_t timestamp,
79 int64_t volatile *prevTimestampPtr,
80 int volatile *prevMethodPtr,
81 int curMethod)
82{
83 /*
84 * Disable the check for SDK since the prebuilt toolchain doesn't contain
85 * gettid, and int64_t is different on the ARM platform
86 * (ie long vs long long).
87 */
88#ifdef ARCH_ARM
89 int64_t prevTimestamp = *prevTimestampPtr;
90 int prevMethod = *prevMethodPtr;
91
92 if (timestamp < prevTimestamp) {
93 ALOGW("time going backwards: prev %lld(%s) vs now %lld(%s), tid=%d",
94 prevTimestamp, gettime_method_names[prevMethod],
95 timestamp, gettime_method_names[curMethod],
96 gettid());
97 }
98 // NOTE - not atomic and may generate spurious warnings if the 64-bit
99 // write is interrupted or not observed as a whole.
100 *prevTimestampPtr = timestamp;
101 *prevMethodPtr = curMethod;
102#endif
103}
Ben Cheng5cd11812013-09-23 22:39:15 -0700104#else
105#define checkTimeStamps(timestamp, prevTimestampPtr, prevMethodPtr, curMethod)
106#endif
Ben Chengad0a9592012-09-14 14:45:34 -0700107
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700108/*
109 * native public static long elapsedRealtimeNano();
110 */
111int64_t elapsedRealtimeNano()
112{
Kenny Rootdafff0b2011-02-16 10:13:53 -0800113#ifdef HAVE_ANDROID_OS
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700114 struct timespec ts;
Ben Chengf4722b52012-09-19 14:53:10 -0700115 int result;
Ben Chengad0a9592012-09-14 14:45:34 -0700116 int64_t timestamp;
Ben Cheng5cd11812013-09-23 22:39:15 -0700117#if DEBUG_TIMESTAMP
Ben Chengad0a9592012-09-14 14:45:34 -0700118 static volatile int64_t prevTimestamp;
119 static volatile int prevMethod;
Ben Cheng5cd11812013-09-23 22:39:15 -0700120#endif
Ben Chengad0a9592012-09-14 14:45:34 -0700121
Ben Chengf4722b52012-09-19 14:53:10 -0700122#if 0
123 /*
124 * b/7100774
125 * clock_gettime appears to have clock skews and can sometimes return
126 * backwards values. Disable its use until we find out what's wrong.
127 */
128 result = clock_gettime(CLOCK_BOOTTIME, &ts);
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700129 if (result == 0) {
Ben Chengad0a9592012-09-14 14:45:34 -0700130 timestamp = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
131 checkTimeStamps(timestamp, &prevTimestamp, &prevMethod,
132 METHOD_CLOCK_GETTIME);
133 return timestamp;
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700134 }
Ben Chengf4722b52012-09-19 14:53:10 -0700135#endif
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700136
137 // CLOCK_BOOTTIME doesn't exist, fallback to /dev/alarm
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800138 static int s_fd = -1;
139
140 if (s_fd == -1) {
141 int fd = open("/dev/alarm", O_RDONLY);
142 if (android_atomic_cmpxchg(-1, fd, &s_fd)) {
143 close(fd);
144 }
145 }
146
Ben Chengad0a9592012-09-14 14:45:34 -0700147 result = ioctl(s_fd,
148 ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME), &ts);
149
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800150 if (result == 0) {
Ben Chengad0a9592012-09-14 14:45:34 -0700151 timestamp = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
152 checkTimeStamps(timestamp, &prevTimestamp, &prevMethod, METHOD_IOCTL);
153 return timestamp;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800154 }
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700155
156 // XXX: there was an error, probably because the driver didn't
157 // exist ... this should return
158 // a real error, like an exception!
Ben Chengad0a9592012-09-14 14:45:34 -0700159 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
160 checkTimeStamps(timestamp, &prevTimestamp, &prevMethod,
161 METHOD_SYSTEMTIME);
162 return timestamp;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800163#else
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700164 return systemTime(SYSTEM_TIME_MONOTONIC);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800165#endif
166}
167
168}; // namespace android