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 22 #include "JNIHelp.h" 23 #include "jni.h" 24 #include "android_runtime/AndroidRuntime.h" 25 26 #include "utils/SystemClock.h" 27 28 #include <sys/time.h> 29 #include <time.h> 30 31 namespace android { 32 33 /* 34 * native public static void setCurrentTimeMillis(long millis) 35 * 36 * Set the current time. This only works when running as root. 37 */ 38 static jboolean android_os_SystemClock_setCurrentTimeMillis(JNIEnv* env, 39 jobject clazz, jlong millis) 40 { 41 return (setCurrentTimeMillis(millis) == 0); 42 } 43 44 /* 45 * native public static long uptimeMillis(); 46 */ 47 static jlong android_os_SystemClock_uptimeMillis(JNIEnv* env, 48 jobject clazz) 49 { 50 return (jlong)uptimeMillis(); 51 } 52 53 /* 54 * native public static long elapsedRealtime(); 55 */ 56 static jlong android_os_SystemClock_elapsedRealtime(JNIEnv* env, 57 jobject clazz) 58 { 59 return (jlong)elapsedRealtime(); 60 } 61 62 /* 63 * native public static long currentThreadTimeMillis(); 64 */ 65 static jlong android_os_SystemClock_currentThreadTimeMillis(JNIEnv* env, 66 jobject clazz) 67 { 68 #if defined(HAVE_POSIX_CLOCKS) 69 struct timespec tm; 70 71 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tm); 72 73 return tm.tv_sec * 1000LL + tm.tv_nsec / 1000000; 74 #else 75 struct timeval tv; 76 77 gettimeofday(&tv, NULL); 78 return tv.tv_sec * 1000LL + tv.tv_usec / 1000; 79 #endif 80 } 81 82 /* 83 * native public static long currentThreadTimeMicro(); 84 */ 85 static jlong android_os_SystemClock_currentThreadTimeMicro(JNIEnv* env, 86 jobject clazz) 87 { 88 #if defined(HAVE_POSIX_CLOCKS) 89 struct timespec tm; 90 91 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tm); 92 93 return tm.tv_sec * 1000000LL + tm.tv_nsec / 1000; 94 #else 95 struct timeval tv; 96 97 gettimeofday(&tv, NULL); 98 return tv.tv_sec * 1000000LL + tv.tv_nsec / 1000; 99 #endif 100 } 101 102 /* 103 * native public static long currentTimeMicro(); 104 */ 105 static jlong android_os_SystemClock_currentTimeMicro(JNIEnv* env, 106 jobject clazz) 107 { 108 struct timeval tv; 109 110 gettimeofday(&tv, NULL); 111 return tv.tv_sec * 1000000LL + tv.tv_usec; 112 } 113 114 /* 115 * public static native long elapsedRealtimeNano(); 116 */ 117 static jlong android_os_SystemClock_elapsedRealtimeNano(JNIEnv* env, 118 jobject clazz) 119 { 120 return (jlong)elapsedRealtimeNano(); 121 } 122 123 /* 124 * JNI registration. 125 */ 126 static JNINativeMethod gMethods[] = { 127 /* name, signature, funcPtr */ 128 { "setCurrentTimeMillis", "(J)Z", 129 (void*) android_os_SystemClock_setCurrentTimeMillis }, 130 { "uptimeMillis", "()J", 131 (void*) android_os_SystemClock_uptimeMillis }, 132 { "elapsedRealtime", "()J", 133 (void*) android_os_SystemClock_elapsedRealtime }, 134 { "currentThreadTimeMillis", "()J", 135 (void*) android_os_SystemClock_currentThreadTimeMillis }, 136 { "currentThreadTimeMicro", "()J", 137 (void*) android_os_SystemClock_currentThreadTimeMicro }, 138 { "currentTimeMicro", "()J", 139 (void*) android_os_SystemClock_currentTimeMicro }, 140 { "elapsedRealtimeNanos", "()J", 141 (void*) android_os_SystemClock_elapsedRealtimeNano }, 142 }; 143 int register_android_os_SystemClock(JNIEnv* env) 144 { 145 return AndroidRuntime::registerNativeMethods(env, 146 "android/os/SystemClock", gMethods, NELEM(gMethods)); 147 } 148 149 }; // namespace android 150 151