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 | |
Mathias Agopian | 22dbf39 | 2017-02-28 15:06:51 -0800 | [diff] [blame] | 22 | #define LOG_TAG "SystemClock" |
| 23 | |
| 24 | #include <utils/SystemClock.h> |
| 25 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <string.h> |
Greg Hackmann | e94c92c | 2016-04-25 10:39:55 -0700 | [diff] [blame] | 27 | #include <errno.h> |
Jiyong Park | bab1658 | 2017-09-06 12:21:40 +0900 | [diff] [blame] | 28 | #include <time.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Greg Hackmann | e94c92c | 2016-04-25 10:39:55 -0700 | [diff] [blame] | 30 | #include <cutils/compiler.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
Mathias Agopian | 22dbf39 | 2017-02-28 15:06:51 -0800 | [diff] [blame] | 32 | #include <utils/Timers.h> |
Mathias Agopian | 9eb2a3b | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 33 | #include <utils/Log.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | /* |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | * native public static long uptimeMillis(); |
| 39 | */ |
| 40 | int64_t uptimeMillis() |
| 41 | { |
Suprabh Shukla | a55ef96 | 2020-08-05 00:01:36 -0700 | [diff] [blame] | 42 | return nanoseconds_to_milliseconds(uptimeNanos()); |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * public static native long uptimeNanos(); |
| 47 | */ |
| 48 | int64_t uptimeNanos() |
| 49 | { |
| 50 | return systemTime(SYSTEM_TIME_MONOTONIC); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /* |
| 54 | * native public static long elapsedRealtime(); |
| 55 | */ |
| 56 | int64_t elapsedRealtime() |
| 57 | { |
Nick Pelly | af1e7b7 | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 58 | return nanoseconds_to_milliseconds(elapsedRealtimeNano()); |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * native public static long elapsedRealtimeNano(); |
| 63 | */ |
| 64 | int64_t elapsedRealtimeNano() |
| 65 | { |
Greg Hackmann | e94c92c | 2016-04-25 10:39:55 -0700 | [diff] [blame] | 66 | #if defined(__linux__) |
Elliott Hughes | ad19af7 | 2016-04-04 16:06:53 -0700 | [diff] [blame] | 67 | struct timespec ts; |
Greg Hackmann | e94c92c | 2016-04-25 10:39:55 -0700 | [diff] [blame] | 68 | int err = clock_gettime(CLOCK_BOOTTIME, &ts); |
| 69 | if (CC_UNLIKELY(err)) { |
| 70 | // This should never happen, but just in case ... |
| 71 | ALOGE("clock_gettime(CLOCK_BOOTTIME) failed: %s", strerror(errno)); |
| 72 | return 0; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | } |
Nick Pelly | af1e7b7 | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 74 | |
Greg Hackmann | e94c92c | 2016-04-25 10:39:55 -0700 | [diff] [blame] | 75 | 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] | 76 | #else |
Nick Pelly | af1e7b7 | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 77 | return systemTime(SYSTEM_TIME_MONOTONIC); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | #endif |
| 79 | } |
| 80 | |
| 81 | }; // namespace android |