• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 #include <unistd.h>
18 #include <utils/SystemClock.h>
19 
20 #include <gtest/gtest.h>
21 
22 static const auto MS_IN_NS = 1000000;
23 
24 static const int64_t SLEEP_MS = 500;
25 static const int64_t SLEEP_NS = SLEEP_MS * MS_IN_NS;
26 // Conservatively assume that we might be descheduled for up to 50 ms
27 static const int64_t SLACK_MS = 50;
28 static const int64_t SLACK_NS = SLACK_MS * MS_IN_NS;
29 
TEST(SystemClock,SystemClock)30 TEST(SystemClock, SystemClock) {
31     auto startUptimeMs = android::uptimeMillis();
32     auto startUptimeNs = android::uptimeNanos();
33     auto startRealtimeMs = android::elapsedRealtime();
34     auto startRealtimeNs = android::elapsedRealtimeNano();
35 
36     ASSERT_GT(startUptimeMs, 0)
37             << "uptimeMillis() reported an impossible uptime";
38     ASSERT_GT(startUptimeNs, 0)
39             << "uptimeNanos() reported an impossible uptime";
40     ASSERT_GE(startRealtimeMs, startUptimeMs)
41             << "elapsedRealtime() thinks we've suspended for negative time";
42     ASSERT_GE(startRealtimeNs, startUptimeNs)
43             << "elapsedRealtimeNano() thinks we've suspended for negative time";
44 
45     ASSERT_GE(startUptimeNs, startUptimeMs * MS_IN_NS)
46             << "uptimeMillis() and uptimeNanos() are inconsistent";
47     ASSERT_LT(startUptimeNs, (startUptimeMs + SLACK_MS) * MS_IN_NS)
48             << "uptimeMillis() and uptimeNanos() are inconsistent";
49 
50     ASSERT_GE(startRealtimeNs, startRealtimeMs * MS_IN_NS)
51             << "elapsedRealtime() and elapsedRealtimeNano() are inconsistent";
52     ASSERT_LT(startRealtimeNs, (startRealtimeMs + SLACK_MS) * MS_IN_NS)
53             << "elapsedRealtime() and elapsedRealtimeNano() are inconsistent";
54 
55     timespec ts;
56     ts.tv_sec = 0;
57     ts.tv_nsec = SLEEP_MS * MS_IN_NS;
58     auto nanosleepErr = TEMP_FAILURE_RETRY(nanosleep(&ts, nullptr));
59     ASSERT_EQ(nanosleepErr, 0) << "nanosleep() failed: " << strerror(errno);
60 
61     auto endUptimeMs = android::uptimeMillis();
62     auto endUptimeNs = android::uptimeNanos();
63     auto endRealtimeMs = android::elapsedRealtime();
64     auto endRealtimeNs = android::elapsedRealtimeNano();
65 
66     EXPECT_GE(endUptimeMs - startUptimeMs, SLEEP_MS)
67             << "uptimeMillis() advanced too little after nanosleep()";
68     EXPECT_LT(endUptimeMs - startUptimeMs, SLEEP_MS + SLACK_MS)
69             << "uptimeMillis() advanced too much after nanosleep()";
70     EXPECT_GE(endUptimeNs - startUptimeNs, SLEEP_NS)
71             << "uptimeNanos() advanced too little after nanosleep()";
72     EXPECT_LT(endUptimeNs - startUptimeNs, SLEEP_NS + SLACK_NS)
73             << "uptimeNanos() advanced too much after nanosleep()";
74     EXPECT_GE(endRealtimeMs - startRealtimeMs, SLEEP_MS)
75             << "elapsedRealtime() advanced too little after nanosleep()";
76     EXPECT_LT(endRealtimeMs - startRealtimeMs, SLEEP_MS + SLACK_MS)
77             << "elapsedRealtime() advanced too much after nanosleep()";
78     EXPECT_GE(endRealtimeNs - startRealtimeNs, SLEEP_NS)
79             << "elapsedRealtimeNano() advanced too little after nanosleep()";
80     EXPECT_LT(endRealtimeNs - startRealtimeNs, SLEEP_NS + SLACK_NS)
81             << "elapsedRealtimeNano() advanced too much after nanosleep()";
82 
83     EXPECT_GE(endUptimeNs, endUptimeMs * MS_IN_NS)
84             << "uptimeMillis() and uptimeNanos() are inconsistent after nanosleep()";
85     EXPECT_LT(endUptimeNs, (endUptimeMs + SLACK_MS) * MS_IN_NS)
86             << "uptimeMillis() and uptimeNanos() are inconsistent after nanosleep()";
87 
88     EXPECT_GE(endRealtimeNs, endRealtimeMs * MS_IN_NS)
89             << "elapsedRealtime() and elapsedRealtimeNano() are inconsistent after nanosleep()";
90     EXPECT_LT(endRealtimeNs, (endRealtimeMs + SLACK_MS) * MS_IN_NS)
91             << "elapsedRealtime() and elapsedRealtimeNano() are inconsistent after nanosleep()";
92 }
93