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 | |
John Reck | 9481c9e | 2016-10-03 14:55:00 -0700 | [diff] [blame^] | 39 | static_assert(std::is_same<int64_t, jlong>::value, "jlong isn't an int64_t"); |
| 40 | static_assert(std::is_same<decltype(uptimeMillis()), int64_t>::value, |
| 41 | "uptimeMillis signature change, expected int64_t return value"); |
| 42 | static_assert(std::is_same<decltype(elapsedRealtime()), int64_t>::value, |
| 43 | "uptimeMillis signature change, expected int64_t return value"); |
| 44 | static_assert(std::is_same<decltype(elapsedRealtimeNano()), int64_t>::value, |
| 45 | "uptimeMillis signature change, expected int64_t return value"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * native public static long currentThreadTimeMillis(); |
| 49 | */ |
John Reck | 9481c9e | 2016-10-03 14:55:00 -0700 | [diff] [blame^] | 50 | static jlong android_os_SystemClock_currentThreadTimeMillis() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | struct timespec tm; |
| 53 | |
| 54 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tm); |
| 55 | |
| 56 | return tm.tv_sec * 1000LL + tm.tv_nsec / 1000000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* |
Romain Guy | 648bee1 | 2011-07-20 18:47:17 -0700 | [diff] [blame] | 60 | * native public static long currentThreadTimeMicro(); |
| 61 | */ |
John Reck | 9481c9e | 2016-10-03 14:55:00 -0700 | [diff] [blame^] | 62 | static jlong android_os_SystemClock_currentThreadTimeMicro() |
Romain Guy | 648bee1 | 2011-07-20 18:47:17 -0700 | [diff] [blame] | 63 | { |
Romain Guy | 648bee1 | 2011-07-20 18:47:17 -0700 | [diff] [blame] | 64 | struct timespec tm; |
| 65 | |
| 66 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tm); |
| 67 | |
| 68 | return tm.tv_sec * 1000000LL + tm.tv_nsec / 1000; |
Romain Guy | 648bee1 | 2011-07-20 18:47:17 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* |
| 72 | * native public static long currentTimeMicro(); |
| 73 | */ |
John Reck | 9481c9e | 2016-10-03 14:55:00 -0700 | [diff] [blame^] | 74 | static jlong android_os_SystemClock_currentTimeMicro() |
Romain Guy | 648bee1 | 2011-07-20 18:47:17 -0700 | [diff] [blame] | 75 | { |
| 76 | struct timeval tv; |
| 77 | |
| 78 | gettimeofday(&tv, NULL); |
| 79 | return tv.tv_sec * 1000000LL + tv.tv_usec; |
| 80 | } |
| 81 | |
| 82 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | * JNI registration. |
| 84 | */ |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 85 | static const JNINativeMethod gMethods[] = { |
John Reck | 9481c9e | 2016-10-03 14:55:00 -0700 | [diff] [blame^] | 86 | // All of these are @CriticalNative, so we can defer directly to SystemClock.h for |
| 87 | // some of these |
| 88 | { "uptimeMillis", "()J", (void*) uptimeMillis }, |
| 89 | { "elapsedRealtime", "()J", (void*) elapsedRealtime }, |
| 90 | { "elapsedRealtimeNanos", "()J", (void*) elapsedRealtimeNano }, |
| 91 | |
| 92 | // SystemClock doesn't have an implementation for these that we can directly call |
| 93 | { "currentThreadTimeMillis", "()J", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | (void*) android_os_SystemClock_currentThreadTimeMillis }, |
John Reck | 9481c9e | 2016-10-03 14:55:00 -0700 | [diff] [blame^] | 95 | { "currentThreadTimeMicro", "()J", |
Romain Guy | 648bee1 | 2011-07-20 18:47:17 -0700 | [diff] [blame] | 96 | (void*) android_os_SystemClock_currentThreadTimeMicro }, |
John Reck | 9481c9e | 2016-10-03 14:55:00 -0700 | [diff] [blame^] | 97 | { "currentTimeMicro", "()J", |
Romain Guy | 648bee1 | 2011-07-20 18:47:17 -0700 | [diff] [blame] | 98 | (void*) android_os_SystemClock_currentTimeMicro }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | }; |
| 100 | int register_android_os_SystemClock(JNIEnv* env) |
| 101 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 102 | return RegisterMethodsOrDie(env, "android/os/SystemClock", gMethods, NELEM(gMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | }; // namespace android |
| 106 | |