Skip to content

Commit 95f1158

Browse files
author
Nick Pelly
committed
Introduce SystemClock#elapsedRealtimeNano.
Change-Id: I47e1b14d45c5321f959d46e1805f86aafd72f5d4
1 parent 824582d commit 95f1158

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

api/current.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16342,6 +16342,7 @@ package android.os {
1634216342
public final class SystemClock {
1634316343
method public static long currentThreadTimeMillis();
1634416344
method public static long elapsedRealtime();
16345+
method public static long elapsedRealtimeNano();
1634516346
method public static boolean setCurrentTimeMillis(long);
1634616347
method public static void sleep(long);
1634716348
method public static long uptimeMillis();

core/java/android/os/SystemClock.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@
4646
* such as {@link Thread#sleep(long) Thread.sleep(millls)},
4747
* {@link Object#wait(long) Object.wait(millis)}, and
4848
* {@link System#nanoTime System.nanoTime()}. This clock is guaranteed
49-
* to be monotonic, and is the recommended basis for the general purpose
50-
* interval timing of user interface events, performance measurements,
51-
* and anything else that does not need to measure elapsed time during
52-
* device sleep. Most methods that accept a timestamp value expect the
53-
* {@link #uptimeMillis} clock.
49+
* to be monotonic, and is suitable for interval timing when the
50+
* interval does not span device sleep. Most methods that accept a
51+
* timestamp value currently expect the {@link #uptimeMillis} clock.
52+
*
53+
* <li> <p> {@link #elapsedRealtime} and {@link #elapsedRealtimeNano}
54+
* return the time since the system was booted, and include deep sleep.
55+
* This clock is guaranteed to be monotonic, and continues to tick even
56+
* when the CPU is in power saving modes, so is the recommend basis
57+
* for general purpose interval timing.
5458
*
55-
* <li> <p> {@link #elapsedRealtime} is counted in milliseconds since the
56-
* system was booted, including deep sleep. This clock should be used
57-
* when measuring time intervals that may span periods of system sleep.
5859
* </ul>
5960
*
6061
* There are several mechanisms for controlling the timing of events:
@@ -150,7 +151,14 @@ public static void sleep(long ms)
150151
* @return elapsed milliseconds since boot.
151152
*/
152153
native public static long elapsedRealtime();
153-
154+
155+
/**
156+
* Returns nanoseconds since boot, including time spent in sleep.
157+
*
158+
* @return elapsed nanoseconds since boot.
159+
*/
160+
public static native long elapsedRealtimeNano();
161+
154162
/**
155163
* Returns milliseconds running in the current thread.
156164
*

core/jni/android_os_SystemClock.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ static jlong android_os_SystemClock_currentTimeMicro(JNIEnv* env,
111111
return tv.tv_sec * 1000000LL + tv.tv_usec;
112112
}
113113

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+
114123
/*
115124
* JNI registration.
116125
*/
@@ -128,6 +137,8 @@ static JNINativeMethod gMethods[] = {
128137
(void*) android_os_SystemClock_currentThreadTimeMicro },
129138
{ "currentTimeMicro", "()J",
130139
(void*) android_os_SystemClock_currentTimeMicro },
140+
{ "elapsedRealtimeNano", "()J",
141+
(void*) android_os_SystemClock_elapsedRealtimeNano },
131142
};
132143
int register_android_os_SystemClock(JNIEnv* env)
133144
{

0 commit comments

Comments
 (0)