The Android Open Source Project | cbb1011 | 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 | |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 22 | #if defined(__ANDROID__) |
The Android Open Source Project | cbb1011 | 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> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include <string.h> |
| 33 | |
| 34 | #include <utils/SystemClock.h> |
| 35 | #include <utils/Timers.h> |
| 36 | |
| 37 | #define LOG_TAG "SystemClock" |
Mathias Agopian | 9eb2a3b | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 38 | #include <utils/Log.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
| 40 | namespace android { |
| 41 | |
| 42 | /* |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | * native public static long uptimeMillis(); |
| 44 | */ |
| 45 | int64_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 | */ |
| 54 | int64_t elapsedRealtime() |
| 55 | { |
Nick Pelly | af1e7b7 | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 56 | return nanoseconds_to_milliseconds(elapsedRealtimeNano()); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * native public static long elapsedRealtimeNano(); |
| 61 | */ |
| 62 | int64_t elapsedRealtimeNano() |
| 63 | { |
Elliott Hughes | 9b828ad | 2015-07-30 08:47:35 -0700 | [diff] [blame] | 64 | #if defined(__ANDROID__) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | 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 Hughes | ad19af7 | 2016-04-04 16:06:53 -0700 | [diff] [blame^] | 74 | 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 Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | } |
Nick Pelly | af1e7b7 | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 78 | |
Greg Hackmann | 6428976 | 2013-12-16 17:04:32 -0800 | [diff] [blame] | 79 | // /dev/alarm doesn't exist, fallback to CLOCK_BOOTTIME |
Elliott Hughes | ad19af7 | 2016-04-04 16:06:53 -0700 | [diff] [blame^] | 80 | if (clock_gettime(CLOCK_BOOTTIME, &ts) == 0) { |
| 81 | return seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec; |
Greg Hackmann | 6428976 | 2013-12-16 17:04:32 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Nick Pelly | af1e7b7 | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 84 | // XXX: there was an error, probably because the driver didn't |
| 85 | // exist ... this should return |
| 86 | // a real error, like an exception! |
Elliott Hughes | ad19af7 | 2016-04-04 16:06:53 -0700 | [diff] [blame^] | 87 | return systemTime(SYSTEM_TIME_MONOTONIC); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | #else |
Nick Pelly | af1e7b7 | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 89 | return systemTime(SYSTEM_TIME_MONOTONIC); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | #endif |
| 91 | } |
| 92 | |
| 93 | }; // namespace android |