blob: 28fc351452c179b980c5e80350d4128119326756 [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 <sys/time.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080027#include <string.h>
Greg Hackmanne94c92c2016-04-25 10:39:55 -070028#include <errno.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{
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