Merge "Add uptimeNanos to SystemClock" am: 52c8422ea1 am: c7a2225665 am: ba7efc63cc am: 11a0227d2a am: e2269017ad

Original change: https://android-review.googlesource.com/c/platform/system/core/+/1394287

Change-Id: I4594a62d40bee62362f72d0aed1f01e40d337256
This commit is contained in:
Treehugger Robot 2020-08-08 02:33:21 +00:00 committed by Automerger Merge Worker
commit 388a02c78a
3 changed files with 29 additions and 3 deletions

View file

@ -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);
}
/*

View file

@ -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)

View file

@ -23,6 +23,7 @@
namespace android {
int64_t uptimeMillis();
int64_t uptimeNanos();
int64_t elapsedRealtime();
int64_t elapsedRealtimeNano();