blob: fd3f4a957644ab5295c6e7be8a4f6ee057d62b95 [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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
17//
18// Timer functions.
19//
20#include <utils/Timers.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080021
Yabin Cui4a6e5a32015-01-26 19:48:54 -080022#include <limits.h>
Elliott Hughes842e1cc2020-05-27 12:24:30 -070023#include <stdlib.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080024#include <time.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080025
Elliott Hughes842e1cc2020-05-27 12:24:30 -070026#include <android-base/macros.h>
27
28static constexpr size_t clock_id_max = 5;
29
30static void checkClockId(int clock) {
31 if (clock < 0 || clock >= clock_id_max) abort();
32}
33
Brett Chabot1af6acc2019-09-17 13:23:59 -070034#if defined(__linux__)
Elliott Hughes842e1cc2020-05-27 12:24:30 -070035nsecs_t systemTime(int clock) {
36 checkClockId(clock);
37 static constexpr clockid_t clocks[] = {CLOCK_REALTIME, CLOCK_MONOTONIC,
38 CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID,
39 CLOCK_BOOTTIME};
40 static_assert(clock_id_max == arraysize(clocks));
41 timespec t = {};
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080042 clock_gettime(clocks[clock], &t);
43 return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec;
Mark Salyzyn5bed8032014-04-30 11:10:46 -070044}
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080045#else
Elliott Hughes842e1cc2020-05-27 12:24:30 -070046nsecs_t systemTime(int clock) {
47 // TODO: is this ever called with anything but REALTIME on mac/windows?
48 checkClockId(clock);
49
Narayan Kamathea659642014-04-10 18:40:55 +010050 // Clock support varies widely across hosts. Mac OS doesn't support
Elliott Hughes842e1cc2020-05-27 12:24:30 -070051 // CLOCK_BOOTTIME (and doesn't even have clock_gettime until 10.12).
52 // Windows is windows.
53 timeval t = {};
Yi Konge1731a42018-07-16 18:11:34 -070054 gettimeofday(&t, nullptr);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080055 return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080056}
Mark Salyzyn5bed8032014-04-30 11:10:46 -070057#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080058
Jeff Brown43550ee2011-03-17 01:34:19 -070059int toMillisecondTimeoutDelay(nsecs_t referenceTime, nsecs_t timeoutTime)
60{
Mathias Agopian22dbf392017-02-28 15:06:51 -080061 nsecs_t timeoutDelayMillis;
Jeff Brown43550ee2011-03-17 01:34:19 -070062 if (timeoutTime > referenceTime) {
63 uint64_t timeoutDelay = uint64_t(timeoutTime - referenceTime);
64 if (timeoutDelay > uint64_t((INT_MAX - 1) * 1000000LL)) {
65 timeoutDelayMillis = -1;
66 } else {
67 timeoutDelayMillis = (timeoutDelay + 999999LL) / 1000000LL;
68 }
69 } else {
70 timeoutDelayMillis = 0;
71 }
Mathias Agopian22dbf392017-02-28 15:06:51 -080072 return (int)timeoutDelayMillis;
Jeff Brown43550ee2011-03-17 01:34:19 -070073}