| 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 | { | 
 | 42 |     int64_t when = systemTime(SYSTEM_TIME_MONOTONIC); | 
 | 43 |     return (int64_t) nanoseconds_to_milliseconds(when); | 
 | 44 | } | 
 | 45 |  | 
 | 46 | /* | 
 | 47 |  * native public static long elapsedRealtime(); | 
 | 48 |  */ | 
 | 49 | int64_t elapsedRealtime() | 
 | 50 | { | 
| Nick Pelly | af1e7b7 | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 51 | 	return nanoseconds_to_milliseconds(elapsedRealtimeNano()); | 
 | 52 | } | 
 | 53 |  | 
 | 54 | /* | 
 | 55 |  * native public static long elapsedRealtimeNano(); | 
 | 56 |  */ | 
 | 57 | int64_t elapsedRealtimeNano() | 
 | 58 | { | 
| Greg Hackmann | e94c92c | 2016-04-25 10:39:55 -0700 | [diff] [blame] | 59 | #if defined(__linux__) | 
| Elliott Hughes | ad19af7 | 2016-04-04 16:06:53 -0700 | [diff] [blame] | 60 |     struct timespec ts; | 
| Greg Hackmann | e94c92c | 2016-04-25 10:39:55 -0700 | [diff] [blame] | 61 |     int err = clock_gettime(CLOCK_BOOTTIME, &ts); | 
 | 62 |     if (CC_UNLIKELY(err)) { | 
 | 63 |         // This should never happen, but just in case ... | 
 | 64 |         ALOGE("clock_gettime(CLOCK_BOOTTIME) failed: %s", strerror(errno)); | 
 | 65 |         return 0; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 |     } | 
| Nick Pelly | af1e7b7 | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 67 |  | 
| Greg Hackmann | e94c92c | 2016-04-25 10:39:55 -0700 | [diff] [blame] | 68 |     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] | 69 | #else | 
| Nick Pelly | af1e7b7 | 2012-07-19 09:17:24 -0700 | [diff] [blame] | 70 |     return systemTime(SYSTEM_TIME_MONOTONIC); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | #endif | 
 | 72 | } | 
 | 73 |  | 
 | 74 | }; // namespace android |