diff --git a/libutils/SystemClock.cpp b/libutils/SystemClock.cpp index 73ec1be96..9c71141f2 100644 --- a/libutils/SystemClock.cpp +++ b/libutils/SystemClock.cpp @@ -39,8 +39,15 @@ namespace android { */ int64_t uptimeMillis() { - int64_t when = systemTime(SYSTEM_TIME_MONOTONIC); - return (int64_t) nanoseconds_to_milliseconds(when); + return nanoseconds_to_milliseconds(uptimeNanos()); +} + +/* + * public static native long uptimeNanos(); + */ +int64_t uptimeNanos() +{ + return systemTime(SYSTEM_TIME_MONOTONIC); } /* diff --git a/libutils/SystemClock_test.cpp b/libutils/SystemClock_test.cpp index 5ad060b04..7449dad77 100644 --- a/libutils/SystemClock_test.cpp +++ b/libutils/SystemClock_test.cpp @@ -29,16 +29,24 @@ static const int64_t SLACK_NS = SLACK_MS * MS_IN_NS; TEST(SystemClock, SystemClock) { auto startUptimeMs = android::uptimeMillis(); + auto startUptimeNs = android::uptimeNanos(); auto startRealtimeMs = android::elapsedRealtime(); auto startRealtimeNs = android::elapsedRealtimeNano(); ASSERT_GT(startUptimeMs, 0) << "uptimeMillis() reported an impossible uptime"; + ASSERT_GT(startUptimeNs, 0) + << "uptimeNanos() reported an impossible uptime"; ASSERT_GE(startRealtimeMs, startUptimeMs) << "elapsedRealtime() thinks we've suspended for negative time"; - ASSERT_GE(startRealtimeNs, startUptimeMs * MS_IN_NS) + ASSERT_GE(startRealtimeNs, startUptimeNs) << "elapsedRealtimeNano() thinks we've suspended for negative time"; + ASSERT_GE(startUptimeNs, startUptimeMs * MS_IN_NS) + << "uptimeMillis() and uptimeNanos() are inconsistent"; + ASSERT_LT(startUptimeNs, (startUptimeMs + SLACK_MS) * MS_IN_NS) + << "uptimeMillis() and uptimeNanos() are inconsistent"; + ASSERT_GE(startRealtimeNs, startRealtimeMs * MS_IN_NS) << "elapsedRealtime() and elapsedRealtimeNano() are inconsistent"; ASSERT_LT(startRealtimeNs, (startRealtimeMs + SLACK_MS) * MS_IN_NS) @@ -51,6 +59,7 @@ TEST(SystemClock, SystemClock) { ASSERT_EQ(nanosleepErr, 0) << "nanosleep() failed: " << strerror(errno); auto endUptimeMs = android::uptimeMillis(); + auto endUptimeNs = android::uptimeNanos(); auto endRealtimeMs = android::elapsedRealtime(); auto endRealtimeNs = android::elapsedRealtimeNano(); @@ -58,6 +67,10 @@ TEST(SystemClock, SystemClock) { << "uptimeMillis() advanced too little after nanosleep()"; EXPECT_LT(endUptimeMs - startUptimeMs, SLEEP_MS + SLACK_MS) << "uptimeMillis() advanced too much after nanosleep()"; + EXPECT_GE(endUptimeNs - startUptimeNs, SLEEP_NS) + << "uptimeNanos() advanced too little after nanosleep()"; + EXPECT_LT(endUptimeNs - startUptimeNs, SLEEP_NS + SLACK_NS) + << "uptimeNanos() advanced too much after nanosleep()"; EXPECT_GE(endRealtimeMs - startRealtimeMs, SLEEP_MS) << "elapsedRealtime() advanced too little after nanosleep()"; EXPECT_LT(endRealtimeMs - startRealtimeMs, SLEEP_MS + SLACK_MS) @@ -67,6 +80,11 @@ TEST(SystemClock, SystemClock) { EXPECT_LT(endRealtimeNs - startRealtimeNs, SLEEP_NS + SLACK_NS) << "elapsedRealtimeNano() advanced too much after nanosleep()"; + EXPECT_GE(endUptimeNs, endUptimeMs * MS_IN_NS) + << "uptimeMillis() and uptimeNanos() are inconsistent after nanosleep()"; + EXPECT_LT(endUptimeNs, (endUptimeMs + SLACK_MS) * MS_IN_NS) + << "uptimeMillis() and uptimeNanos() are inconsistent after nanosleep()"; + EXPECT_GE(endRealtimeNs, endRealtimeMs * MS_IN_NS) << "elapsedRealtime() and elapsedRealtimeNano() are inconsistent after nanosleep()"; EXPECT_LT(endRealtimeNs, (endRealtimeMs + SLACK_MS) * MS_IN_NS) diff --git a/libutils/include/utils/SystemClock.h b/libutils/include/utils/SystemClock.h index f816fbaf7..892104cb8 100644 --- a/libutils/include/utils/SystemClock.h +++ b/libutils/include/utils/SystemClock.h @@ -23,6 +23,7 @@ namespace android { int64_t uptimeMillis(); +int64_t uptimeNanos(); int64_t elapsedRealtime(); int64_t elapsedRealtimeNano();