blob: c5ae3277855f08c6037bbbe5eea52441c8bed2e3 [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
59/*
60 * native public static long elapsedRealtimeNano();
61 */
62int64_t elapsedRealtimeNano()
63{
Elliott Hughes9b828ad2015-07-30 08:47:35 -070064#if defined(__ANDROID__)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080065 static int s_fd = -1;
66
67 if (s_fd == -1) {
68 int fd = open("/dev/alarm", O_RDONLY);
69 if (android_atomic_cmpxchg(-1, fd, &s_fd)) {
70 close(fd);
71 }
72 }
73
Elliott Hughesad19af72016-04-04 16:06:53 -070074 struct timespec ts;
75 if (ioctl(s_fd, ANDROID_ALARM_GET_TIME(ANDROID_ALARM_ELAPSED_REALTIME), &ts) == 0) {
76 return seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080077 }
Nick Pellyaf1e7b72012-07-19 09:17:24 -070078
Greg Hackmann64289762013-12-16 17:04:32 -080079 // /dev/alarm doesn't exist, fallback to CLOCK_BOOTTIME
Elliott Hughesad19af72016-04-04 16:06:53 -070080 if (clock_gettime(CLOCK_BOOTTIME, &ts) == 0) {
81 return seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
Greg Hackmann64289762013-12-16 17:04:32 -080082 }
83
Nick Pellyaf1e7b72012-07-19 09:17:24 -070084 // XXX: there was an error, probably because the driver didn't
85 // exist ... this should return
86 // a real error, like an exception!
Elliott Hughesad19af72016-04-04 16:06:53 -070087 return systemTime(SYSTEM_TIME_MONOTONIC);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080088#else
Nick Pellyaf1e7b72012-07-19 09:17:24 -070089 return systemTime(SYSTEM_TIME_MONOTONIC);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080090#endif
91}
92
93}; // namespace android