blob: 965e32c0fd802520af2e689f4cc6a17c30a4619f [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
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080022#include <sys/time.h>
23#include <limits.h>
24#include <fcntl.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080025#include <string.h>
Greg Hackmanne94c92c2016-04-25 10:39:55 -070026#include <errno.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080027
Greg Hackmanne94c92c2016-04-25 10:39:55 -070028#include <cutils/compiler.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080029#include <utils/SystemClock.h>
30#include <utils/Timers.h>
31
32#define LOG_TAG "SystemClock"
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{
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 */
49int64_t elapsedRealtime()
50{
Nick Pellyaf1e7b72012-07-19 09:17:24 -070051 return nanoseconds_to_milliseconds(elapsedRealtimeNano());
52}
53
54/*
55 * native public static long elapsedRealtimeNano();
56 */
57int64_t elapsedRealtimeNano()
58{
Greg Hackmanne94c92c2016-04-25 10:39:55 -070059#if defined(__linux__)
Elliott Hughesad19af72016-04-04 16:06:53 -070060 struct timespec ts;
Greg Hackmanne94c92c2016-04-25 10:39:55 -070061 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 Projectcbb10112009-03-03 19:31:44 -080066 }
Nick Pellyaf1e7b72012-07-19 09:17:24 -070067
Greg Hackmanne94c92c2016-04-25 10:39:55 -070068 return seconds_to_nanoseconds(ts.tv_sec) + ts.tv_nsec;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080069#else
Nick Pellyaf1e7b72012-07-19 09:17:24 -070070 return systemTime(SYSTEM_TIME_MONOTONIC);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080071#endif
72}
73
74}; // namespace android