blob: b1f600053b9bcd96201f5584b5a9e29351b7011d [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Agopian1f5762e2013-05-06 20:20:34 -070022#include <sys/time.h>
23#include <limits.h>
24#include <fcntl.h>
25#include <errno.h>
26#include <string.h>
27
Steven Moreland2279b252017-07-19 09:50:45 -070028#include <nativehelper/JNIHelp.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include "jni.h"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080030#include "core_jni_helpers.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
Mathias Agopian1f5762e2013-05-06 20:20:34 -070032#include <utils/SystemClock.h>
Jerome Gaillarde83f80d2019-07-01 11:04:51 +010033#include <utils/Timers.h>
Mathias Agopian1f5762e2013-05-06 20:20:34 -070034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035namespace android {
36
John Reck9481c9e2016-10-03 14:55:00 -070037static_assert(std::is_same<int64_t, jlong>::value, "jlong isn't an int64_t");
38static_assert(std::is_same<decltype(uptimeMillis()), int64_t>::value,
39 "uptimeMillis signature change, expected int64_t return value");
40static_assert(std::is_same<decltype(elapsedRealtime()), int64_t>::value,
41 "uptimeMillis signature change, expected int64_t return value");
42static_assert(std::is_same<decltype(elapsedRealtimeNano()), int64_t>::value,
43 "uptimeMillis signature change, expected int64_t return value");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
45/*
46 * native public static long currentThreadTimeMillis();
47 */
John Reck9481c9e2016-10-03 14:55:00 -070048static jlong android_os_SystemClock_currentThreadTimeMillis()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049{
Jerome Gaillarde83f80d2019-07-01 11:04:51 +010050 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_THREAD));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051}
52
53/*
Romain Guy648bee12011-07-20 18:47:17 -070054 * native public static long currentThreadTimeMicro();
55 */
John Reck9481c9e2016-10-03 14:55:00 -070056static jlong android_os_SystemClock_currentThreadTimeMicro()
Romain Guy648bee12011-07-20 18:47:17 -070057{
Jerome Gaillarde83f80d2019-07-01 11:04:51 +010058 return nanoseconds_to_microseconds(systemTime(SYSTEM_TIME_THREAD));
Romain Guy648bee12011-07-20 18:47:17 -070059}
60
61/*
62 * native public static long currentTimeMicro();
63 */
John Reck9481c9e2016-10-03 14:55:00 -070064static jlong android_os_SystemClock_currentTimeMicro()
Romain Guy648bee12011-07-20 18:47:17 -070065{
66 struct timeval tv;
67
68 gettimeofday(&tv, NULL);
69 return tv.tv_sec * 1000000LL + tv.tv_usec;
70}
71
72/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 * JNI registration.
74 */
Daniel Micay76f6a862015-09-19 17:31:01 -040075static const JNINativeMethod gMethods[] = {
John Reck9481c9e2016-10-03 14:55:00 -070076 // All of these are @CriticalNative, so we can defer directly to SystemClock.h for
77 // some of these
78 { "uptimeMillis", "()J", (void*) uptimeMillis },
79 { "elapsedRealtime", "()J", (void*) elapsedRealtime },
80 { "elapsedRealtimeNanos", "()J", (void*) elapsedRealtimeNano },
81
82 // SystemClock doesn't have an implementation for these that we can directly call
83 { "currentThreadTimeMillis", "()J",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 (void*) android_os_SystemClock_currentThreadTimeMillis },
John Reck9481c9e2016-10-03 14:55:00 -070085 { "currentThreadTimeMicro", "()J",
Romain Guy648bee12011-07-20 18:47:17 -070086 (void*) android_os_SystemClock_currentThreadTimeMicro },
John Reck9481c9e2016-10-03 14:55:00 -070087 { "currentTimeMicro", "()J",
Romain Guy648bee12011-07-20 18:47:17 -070088 (void*) android_os_SystemClock_currentTimeMicro },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089};
90int register_android_os_SystemClock(JNIEnv* env)
91{
Andreas Gampeed6b9df2014-11-20 22:02:20 -080092 return RegisterMethodsOrDie(env, "android/os/SystemClock", gMethods, NELEM(gMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093}
94
95}; // namespace android
96