blob: 1fca2b2932e2fcf60b02b64f8e91bf27a3cfe577 [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
Elliott Hughes9b828ad2015-07-30 08:47:35 -070022#if defined(__ANDROID__)
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>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080032#include <string.h>
33
34#include <utils/SystemClock.h>
35#include <utils/Timers.h>
36
37#define LOG_TAG "SystemClock"
Mathias Agopian9eb2a3b2013-05-06 20:20:50 -070038#include <utils/Log.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080039
40namespace android {
41
42/*
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080043 * native public static long uptimeMillis();
44 */
45int64_t uptimeMillis()
46{
47 int64_t when = systemTime(SYSTEM_TIME_MONOTONIC);
48 return (int64_t) nanoseconds_to_milliseconds(when);
49}
50
51/*
52 * native public static long elapsedRealtime();
53 */
54int64_t elapsedRealtime()
55{
Nick Pellyaf1e7b72012-07-19 09:17:24 -070056 return nanoseconds_to_milliseconds(elapsedRealtimeNano());
57}
58
Ben Chengad0a9592012-09-14 14:45:34 -070059#define METHOD_CLOCK_GETTIME 0
60#define METHOD_IOCTL 1
61#define METHOD_SYSTEMTIME 2
62
Ben Cheng5cd11812013-09-23 22:39:15 -070063/*
64 * To debug/verify the timestamps returned by the kernel, change
65 * DEBUG_TIMESTAMP to 1 and call the timestamp routine from a single thread
66 * in the test program. b/10899829
67 */
68#define DEBUG_TIMESTAMP 0
69
Elliott Hughes22a40e62014-12-04 14:17:26 -080070#if DEBUG_TIMESTAMP && defined(__arm__)
Ben Chengad0a9592012-09-14 14:45:34 -070071static inline void checkTimeStamps(int64_t timestamp,
72 int64_t volatile *prevTimestampPtr,
73 int volatile *prevMethodPtr,
74 int curMethod)
75{
76 /*
77 * Disable the check for SDK since the prebuilt toolchain doesn't contain
78 * gettid, and int64_t is different on the ARM platform
79 * (ie long vs long long).
80 */
Ben Chengad0a9592012-09-14 14:45:34 -070081 int64_t prevTimestamp = *prevTimestampPtr;
82 int prevMethod = *prevMethodPtr;
83
84 if (timestamp < prevTimestamp) {
Mark Salyzyn5bed8032014-04-30 11:10:46 -070085 static const char *gettime_method_names[] = {
86 "clock_gettime",
87 "ioctl",
88 "systemTime",
89 };
90
Ben Chengad0a9592012-09-14 14:45:34 -070091 ALOGW("time going backwards: prev %lld(%s) vs now %lld(%s), tid=%d",
92 prevTimestamp, gettime_method_names[prevMethod],
93 timestamp, gettime_method_names[curMethod],
94 gettid());
95 }
96 // NOTE - not atomic and may generate spurious warnings if the 64-bit
97 // write is interrupted or not observed as a whole.
98 *prevTimestampPtr = timestamp;
99 *prevMethodPtr = curMethod;
Ben Chengad0a9592012-09-14 14:45:34 -0700100}
Ben Cheng5cd11812013-09-23 22:39:15 -0700101#else
102#define checkTimeStamps(timestamp, prevTimestampPtr, prevMethodPtr, curMethod)
103#endif
Ben Chengad0a9592012-09-14 14:45:34 -0700104
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700105/*
106 * native public static long elapsedRealtimeNano();
107 */
108int64_t elapsedRealtimeNano()
109{
Elliott Hughes9b828ad2015-07-30 08:47:35 -0700110#if defined(__ANDROID__)
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700111 struct timespec ts;
Ben Chengf4722b52012-09-19 14:53:10 -0700112 int result;
Ben Chengad0a9592012-09-14 14:45:34 -0700113 int64_t timestamp;
Ben Cheng5cd11812013-09-23 22:39:15 -0700114#if DEBUG_TIMESTAMP
Ben Chengad0a9592012-09-14 14:45:34 -0700115 static volatile int64_t prevTimestamp;
116 static volatile int prevMethod;
Ben Cheng5cd11812013-09-23 22:39:15 -0700117#endif
Ben Chengad0a9592012-09-14 14:45:34 -0700118
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800119 static int s_fd = -1;
120
121 if (s_fd == -1) {
122 int fd = open("/dev/alarm", O_RDONLY);
123 if (android_atomic_cmpxchg(-1, fd, &s_fd)) {
124 close(fd);
125 }
126 }
127
Ben Chengad0a9592012-09-14 14:45:34 -0700128 result = ioctl(s_fd,
129 ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME), &ts);
130
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800131 if (result == 0) {
Ben Chengad0a9592012-09-14 14:45:34 -0700132 timestamp = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
133 checkTimeStamps(timestamp, &prevTimestamp, &prevMethod, METHOD_IOCTL);
134 return timestamp;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800135 }
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700136
Greg Hackmann64289762013-12-16 17:04:32 -0800137 // /dev/alarm doesn't exist, fallback to CLOCK_BOOTTIME
138 result = clock_gettime(CLOCK_BOOTTIME, &ts);
139 if (result == 0) {
140 timestamp = seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
141 checkTimeStamps(timestamp, &prevTimestamp, &prevMethod,
142 METHOD_CLOCK_GETTIME);
143 return timestamp;
144 }
145
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700146 // XXX: there was an error, probably because the driver didn't
147 // exist ... this should return
148 // a real error, like an exception!
Ben Chengad0a9592012-09-14 14:45:34 -0700149 timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
150 checkTimeStamps(timestamp, &prevTimestamp, &prevMethod,
151 METHOD_SYSTEMTIME);
152 return timestamp;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800153#else
Nick Pellyaf1e7b72012-07-19 09:17:24 -0700154 return systemTime(SYSTEM_TIME_MONOTONIC);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800155#endif
156}
157
158}; // namespace android