blob: 9c71141f2d5a4d93ce84ab10be6f4e1773fa0921 [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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 Agopian22dbf392017-02-28 15:06:51 -080022#define LOG_TAG "SystemClock"
23
24#include <utils/SystemClock.h>
25
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080026#include <string.h>
Greg Hackmanne94c92c2016-04-25 10:39:55 -070027#include <errno.h>
Jiyong Parkbab16582017-09-06 12:21:40 +090028#include <time.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080029
Greg Hackmanne94c92c2016-04-25 10:39:55 -070030#include <cutils/compiler.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080031
Mathias Agopian22dbf392017-02-28 15:06:51 -080032#include <utils/Timers.h>
Mathias Agopian9eb2a3b2013-05-06 20:20:50 -070033#include <utils/Log.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080034
35namespace android {
36
37/*
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080038 * native public static long uptimeMillis();
39 */
40int64_t uptimeMillis()
41{
Suprabh Shuklaa55ef962020-08-05 00:01:36 -070042 return nanoseconds_to_milliseconds(uptimeNanos());
43}
44
45/*
46 * public static native long uptimeNanos();
47 */
48int64_t uptimeNanos()
49{
50 return systemTime(SYSTEM_TIME_MONOTONIC);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080051}
52
53/*
54 * native public static long elapsedRealtime();
55 */
56int64_t elapsedRealtime()
57{
Nick Pellyaf1e7b72012-07-19 09:17:24 -070058 return nanoseconds_to_milliseconds(elapsedRealtimeNano());
59}
60
61/*
62 * native public static long elapsedRealtimeNano();
63 */
64int64_t elapsedRealtimeNano()
65{
Greg Hackmanne94c92c2016-04-25 10:39:55 -070066#if defined(__linux__)
Elliott Hughesad19af72016-04-04 16:06:53 -070067 struct timespec ts;
Greg Hackmanne94c92c2016-04-25 10:39:55 -070068 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 Projectcbb10112009-03-03 19:31:44 -080073 }
Nick Pellyaf1e7b72012-07-19 09:17:24 -070074
Greg Hackmanne94c92c2016-04-25 10:39:55 -070075 return seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080076#else
Nick Pellyaf1e7b72012-07-19 09:17:24 -070077 return systemTime(SYSTEM_TIME_MONOTONIC);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080078#endif
79}
80
81}; // namespace android