| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2005 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 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #include <utils/Timers.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 |  | 
| Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 19 | #include <limits.h> | 
| Elliott Hughes | 842e1cc | 2020-05-27 12:24:30 -0700 | [diff] [blame] | 20 | #include <stdlib.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include <time.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 |  | 
| Elliott Hughes | 842e1cc | 2020-05-27 12:24:30 -0700 | [diff] [blame] | 23 | #include <android-base/macros.h> | 
| Elliott Hughes | 4139da6 | 2021-05-18 13:10:07 -0700 | [diff] [blame] | 24 | #include <utils/Log.h> | 
| Elliott Hughes | 842e1cc | 2020-05-27 12:24:30 -0700 | [diff] [blame] | 25 |  | 
|  | 26 | static constexpr size_t clock_id_max = 5; | 
|  | 27 |  | 
|  | 28 | static void checkClockId(int clock) { | 
| Elliott Hughes | 4139da6 | 2021-05-18 13:10:07 -0700 | [diff] [blame] | 29 | LOG_ALWAYS_FATAL_IF(clock < 0 || clock >= clock_id_max, "invalid clock id"); | 
| Elliott Hughes | 842e1cc | 2020-05-27 12:24:30 -0700 | [diff] [blame] | 30 | } | 
|  | 31 |  | 
| Brett Chabot | 1af6acc | 2019-09-17 13:23:59 -0700 | [diff] [blame] | 32 | #if defined(__linux__) | 
| Elliott Hughes | 842e1cc | 2020-05-27 12:24:30 -0700 | [diff] [blame] | 33 | nsecs_t systemTime(int clock) { | 
|  | 34 | checkClockId(clock); | 
|  | 35 | static constexpr clockid_t clocks[] = {CLOCK_REALTIME, CLOCK_MONOTONIC, | 
|  | 36 | CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID, | 
|  | 37 | CLOCK_BOOTTIME}; | 
|  | 38 | static_assert(clock_id_max == arraysize(clocks)); | 
|  | 39 | timespec t = {}; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | clock_gettime(clocks[clock], &t); | 
|  | 41 | return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec; | 
| Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 42 | } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | #else | 
| Elliott Hughes | 842e1cc | 2020-05-27 12:24:30 -0700 | [diff] [blame] | 44 | nsecs_t systemTime(int clock) { | 
|  | 45 | // TODO: is this ever called with anything but REALTIME on mac/windows? | 
|  | 46 | checkClockId(clock); | 
|  | 47 |  | 
| Narayan Kamath | ea65964 | 2014-04-10 18:40:55 +0100 | [diff] [blame] | 48 | // Clock support varies widely across hosts. Mac OS doesn't support | 
| Elliott Hughes | 842e1cc | 2020-05-27 12:24:30 -0700 | [diff] [blame] | 49 | // CLOCK_BOOTTIME (and doesn't even have clock_gettime until 10.12). | 
|  | 50 | // Windows is windows. | 
|  | 51 | timeval t = {}; | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 52 | gettimeofday(&t, nullptr); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | } | 
| Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 55 | #endif | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 |  | 
| Elliott Hughes | 4139da6 | 2021-05-18 13:10:07 -0700 | [diff] [blame] | 57 | int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime) { | 
|  | 58 | if (timeoutTime <= referenceTime) return 0; | 
|  | 59 |  | 
|  | 60 | uint64_t timeoutDelay = uint64_t(timeoutTime - referenceTime); | 
|  | 61 | if (timeoutDelay > uint64_t((INT_MAX - 1) * 1000000LL)) return -1; | 
|  | 62 | return (timeoutDelay + 999999LL) / 1000000LL; | 
| Jeff Brown | 43550ee | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 63 | } |