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 <sys/time.h> 23 #include <limits.h> 24 #include <fcntl.h> 25 #include <errno.h> 26 #include <string.h> 27 28 #include <nativehelper/JNIHelp.h> 29 #include "jni.h" 30 #include "core_jni_helpers.h" 31 32 #include <utils/SystemClock.h> 33 #include <utils/Timers.h> 34 35 namespace android { 36 37 static_assert(std::is_same<int64_t, jlong>::value, "jlong isn't an int64_t"); 38 static_assert(std::is_same<decltype(uptimeMillis()), int64_t>::value, 39 "uptimeMillis signature change, expected int64_t return value"); 40 static_assert(std::is_same<decltype(uptimeNanos()), int64_t>::value, 41 "uptimeNanos signature change, expected int64_t return value"); 42 static_assert(std::is_same<decltype(elapsedRealtime()), int64_t>::value, 43 "elapsedRealtime signature change, expected int64_t return value"); 44 static_assert(std::is_same<decltype(elapsedRealtimeNano()), int64_t>::value, 45 "elapsedRealtimeNano signature change, expected int64_t return value"); 46 47 /* 48 * native public static long currentThreadTimeMillis(); 49 */ 50 static jlong android_os_SystemClock_currentThreadTimeMillis() 51 { 52 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_THREAD)); 53 } 54 55 /* 56 * native public static long currentThreadTimeMicro(); 57 */ 58 static jlong android_os_SystemClock_currentThreadTimeMicro() 59 { 60 return nanoseconds_to_microseconds(systemTime(SYSTEM_TIME_THREAD)); 61 } 62 63 /* 64 * native public static long currentTimeMicro(); 65 */ 66 static jlong android_os_SystemClock_currentTimeMicro() 67 { 68 struct timeval tv; 69 70 gettimeofday(&tv, NULL); 71 return tv.tv_sec * 1000000LL + tv.tv_usec; 72 } 73 74 /* 75 * JNI registration. 76 */ 77 static const JNINativeMethod gMethods[] = { 78 // All of these are @CriticalNative, so we can defer directly to SystemClock.h for 79 // some of these 80 { "uptimeMillis", "()J", (void*) uptimeMillis }, 81 { "uptimeNanos", "()J", (void*) uptimeNanos }, 82 { "elapsedRealtime", "()J", (void*) elapsedRealtime }, 83 { "elapsedRealtimeNanos", "()J", (void*) elapsedRealtimeNano }, 84 85 // SystemClock doesn't have an implementation for these that we can directly call 86 { "currentThreadTimeMillis", "()J", 87 (void*) android_os_SystemClock_currentThreadTimeMillis }, 88 { "currentThreadTimeMicro", "()J", 89 (void*) android_os_SystemClock_currentThreadTimeMicro }, 90 { "currentTimeMicro", "()J", 91 (void*) android_os_SystemClock_currentTimeMicro }, 92 }; 93 int register_android_os_SystemClock(JNIEnv* env) 94 { 95 return RegisterMethodsOrDie(env, "android/os/SystemClock", gMethods, NELEM(gMethods)); 96 } 97 98 }; // namespace android 99 100