The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 22 | #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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include "JNIHelp.h" |
| 29 | #include "jni.h" |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 30 | #include "core_jni_helpers.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include <sys/time.h> |
| 33 | #include <time.h> |
| 34 | |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 35 | #include <utils/SystemClock.h> |
| 36 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | namespace android { |
| 38 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | /* |
| 40 | * native public static long uptimeMillis(); |
| 41 | */ |
| 42 | static 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 | */ |
| 51 | static 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 | */ |
| 60 | static jlong android_os_SystemClock_currentThreadTimeMillis(JNIEnv* env, |
| 61 | jobject clazz) |
| 62 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | struct timespec tm; |
| 64 | |
| 65 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tm); |
| 66 | |
| 67 | return tm.tv_sec * 1000LL + tm.tv_nsec / 1000000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* |
Romain Guy | 648bee1 | 2011-07-20 18:47:17 -0700 | [diff] [blame] | 71 | * native public static long currentThreadTimeMicro(); |
| 72 | */ |
| 73 | static jlong android_os_SystemClock_currentThreadTimeMicro(JNIEnv* env, |
| 74 | jobject clazz) |
| 75 | { |
Romain Guy | 648bee1 | 2011-07-20 18:47:17 -0700 | [diff] [blame] | 76 | struct timespec tm; |
| 77 | |
| 78 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tm); |
| 79 | |
| 80 | return tm.tv_sec * 1000000LL + tm.tv_nsec / 1000; |
Romain Guy | 648bee1 | 2011-07-20 18:47:17 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* |
| 84 | * native public static long currentTimeMicro(); |
| 85 | */ |
| 86 | static jlong android_os_SystemClock_currentTimeMicro(JNIEnv* env, |
| 87 | jobject clazz) |
| 88 | { |
| 89 | struct timeval tv; |
| 90 | |
| 91 | gettimeofday(&tv, NULL); |
| 92 | return tv.tv_sec * 1000000LL + tv.tv_usec; |
| 93 | } |
| 94 | |
| 95 | /* |
Nick Pelly | 95f1158 | 2012-07-19 10:22:18 -0700 | [diff] [blame] | 96 | * public static native long elapsedRealtimeNano(); |
| 97 | */ |
| 98 | static jlong android_os_SystemClock_elapsedRealtimeNano(JNIEnv* env, |
| 99 | jobject clazz) |
| 100 | { |
| 101 | return (jlong)elapsedRealtimeNano(); |
| 102 | } |
| 103 | |
| 104 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | * JNI registration. |
| 106 | */ |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame^] | 107 | static const JNINativeMethod gMethods[] = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | /* name, signature, funcPtr */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | { "uptimeMillis", "()J", |
| 110 | (void*) android_os_SystemClock_uptimeMillis }, |
| 111 | { "elapsedRealtime", "()J", |
| 112 | (void*) android_os_SystemClock_elapsedRealtime }, |
| 113 | { "currentThreadTimeMillis", "()J", |
| 114 | (void*) android_os_SystemClock_currentThreadTimeMillis }, |
Romain Guy | 648bee1 | 2011-07-20 18:47:17 -0700 | [diff] [blame] | 115 | { "currentThreadTimeMicro", "()J", |
| 116 | (void*) android_os_SystemClock_currentThreadTimeMicro }, |
| 117 | { "currentTimeMicro", "()J", |
| 118 | (void*) android_os_SystemClock_currentTimeMicro }, |
Jeff Sharkey | dd78d46 | 2012-09-26 18:45:28 -0700 | [diff] [blame] | 119 | { "elapsedRealtimeNanos", "()J", |
Nick Pelly | 95f1158 | 2012-07-19 10:22:18 -0700 | [diff] [blame] | 120 | (void*) android_os_SystemClock_elapsedRealtimeNano }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | }; |
| 122 | int register_android_os_SystemClock(JNIEnv* env) |
| 123 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 124 | return RegisterMethodsOrDie(env, "android/os/SystemClock", gMethods, NELEM(gMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | }; // namespace android |
| 128 | |