• 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 startRealtimeMs = android::elapsedRealtime();
33     auto startRealtimeNs = android::elapsedRealtimeNano();
34 
35     ASSERT_GT(startUptimeMs, 0)
36             << "uptimeMillis() reported an impossible uptime";
37     ASSERT_GE(startRealtimeMs, startUptimeMs)
38             << "elapsedRealtime() thinks we've suspended for negative time";
39     ASSERT_GE(startRealtimeNs, startUptimeMs * MS_IN_NS)
40             << "elapsedRealtimeNano() thinks we've suspended for negative time";
41 
42     ASSERT_GE(startRealtimeNs, startRealtimeMs * MS_IN_NS)
43             << "elapsedRealtime() and elapsedRealtimeNano() are inconsistent";
44     ASSERT_LT(startRealtimeNs, (startRealtimeMs + SLACK_MS) * MS_IN_NS)
45             << "elapsedRealtime() and elapsedRealtimeNano() are inconsistent";
46 
47     timespec ts;
48     ts.tv_sec = 0;
49     ts.tv_nsec = SLEEP_MS * MS_IN_NS;
50     auto nanosleepErr = TEMP_FAILURE_RETRY(nanosleep(&ts, nullptr));
51     ASSERT_EQ(nanosleepErr, 0) << "nanosleep() failed: " << strerror(errno);
52 
53     auto endUptimeMs = android::uptimeMillis();
54     auto endRealtimeMs = android::elapsedRealtime();
55     auto endRealtimeNs = android::elapsedRealtimeNano();
56 
57     EXPECT_GE(endUptimeMs - startUptimeMs, SLEEP_MS)
58             << "uptimeMillis() advanced too little after nanosleep()";
59     EXPECT_LT(endUptimeMs - startUptimeMs, SLEEP_MS + SLACK_MS)
60             << "uptimeMillis() advanced too much after nanosleep()";
61     EXPECT_GE(endRealtimeMs - startRealtimeMs, SLEEP_MS)
62             << "elapsedRealtime() advanced too little after nanosleep()";
63     EXPECT_LT(endRealtimeMs - startRealtimeMs, SLEEP_MS + SLACK_MS)
64             << "elapsedRealtime() advanced too much after nanosleep()";
65     EXPECT_GE(endRealtimeNs - startRealtimeNs, SLEEP_NS)
66             << "elapsedRealtimeNano() advanced too little after nanosleep()";
67     EXPECT_LT(endRealtimeNs - startRealtimeNs, SLEEP_NS + SLACK_NS)
68             << "elapsedRealtimeNano() advanced too much after nanosleep()";
69 
70     EXPECT_GE(endRealtimeNs, endRealtimeMs * MS_IN_NS)
71             << "elapsedRealtime() and elapsedRealtimeNano() are inconsistent after nanosleep()";
72     EXPECT_LT(endRealtimeNs, (endRealtimeMs + SLACK_MS) * MS_IN_NS)
73             << "elapsedRealtime() and elapsedRealtimeNano() are inconsistent after nanosleep()";
74 }
75