blob: 1d456057507e2cce4fcd43eab47b8141fe4d6af6 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include "JNIHelp.h"
29#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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032#include <sys/time.h>
33#include <time.h>
34
Mathias Agopian1f5762e2013-05-06 20:20:34 -070035#include <utils/SystemClock.h>
36
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037namespace android {
38
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039/*
40 * native public static long uptimeMillis();
41 */
42static jlong android_os_SystemClock_uptimeMillis(JNIEnv* env,
43 jobject clazz)
44{
45 return (jlong)uptimeMillis();
46}
47
48/*
49 * native public static long elapsedRealtime();
50 */
51static jlong android_os_SystemClock_elapsedRealtime(JNIEnv* env,
52 jobject clazz)
53{
54 return (jlong)elapsedRealtime();
55}
56
57/*
58 * native public static long currentThreadTimeMillis();
59 */
60static jlong android_os_SystemClock_currentThreadTimeMillis(JNIEnv* env,
61 jobject clazz)
62{
63#if defined(HAVE_POSIX_CLOCKS)
64 struct timespec tm;
65
66 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tm);
67
68 return tm.tv_sec * 1000LL + tm.tv_nsec / 1000000;
69#else
70 struct timeval tv;
71
72 gettimeofday(&tv, NULL);
73 return tv.tv_sec * 1000LL + tv.tv_usec / 1000;
74#endif
75}
76
77/*
Romain Guy648bee12011-07-20 18:47:17 -070078 * native public static long currentThreadTimeMicro();
79 */
80static jlong android_os_SystemClock_currentThreadTimeMicro(JNIEnv* env,
81 jobject clazz)
82{
83#if defined(HAVE_POSIX_CLOCKS)
84 struct timespec tm;
85
86 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tm);
87
88 return tm.tv_sec * 1000000LL + tm.tv_nsec / 1000;
89#else
90 struct timeval tv;
91
92 gettimeofday(&tv, NULL);
93 return tv.tv_sec * 1000000LL + tv.tv_nsec / 1000;
94#endif
95}
96
97/*
98 * native public static long currentTimeMicro();
99 */
100static jlong android_os_SystemClock_currentTimeMicro(JNIEnv* env,
101 jobject clazz)
102{
103 struct timeval tv;
104
105 gettimeofday(&tv, NULL);
106 return tv.tv_sec * 1000000LL + tv.tv_usec;
107}
108
109/*
Nick Pelly95f11582012-07-19 10:22:18 -0700110 * public static native long elapsedRealtimeNano();
111 */
112static jlong android_os_SystemClock_elapsedRealtimeNano(JNIEnv* env,
113 jobject clazz)
114{
115 return (jlong)elapsedRealtimeNano();
116}
117
118/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 * JNI registration.
120 */
121static JNINativeMethod gMethods[] = {
122 /* name, signature, funcPtr */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 { "uptimeMillis", "()J",
124 (void*) android_os_SystemClock_uptimeMillis },
125 { "elapsedRealtime", "()J",
126 (void*) android_os_SystemClock_elapsedRealtime },
127 { "currentThreadTimeMillis", "()J",
128 (void*) android_os_SystemClock_currentThreadTimeMillis },
Romain Guy648bee12011-07-20 18:47:17 -0700129 { "currentThreadTimeMicro", "()J",
130 (void*) android_os_SystemClock_currentThreadTimeMicro },
131 { "currentTimeMicro", "()J",
132 (void*) android_os_SystemClock_currentTimeMicro },
Jeff Sharkeydd78d462012-09-26 18:45:28 -0700133 { "elapsedRealtimeNanos", "()J",
Nick Pelly95f11582012-07-19 10:22:18 -0700134 (void*) android_os_SystemClock_elapsedRealtimeNano },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135};
136int register_android_os_SystemClock(JNIEnv* env)
137{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800138 return RegisterMethodsOrDie(env, "android/os/SystemClock", gMethods, NELEM(gMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139}
140
141}; // namespace android
142