blob: 7449dad7737a027d1e71d9effec163d547742dbd [file] [log] [blame]
Greg Hackmann340079d2016-04-25 15:57:44 -07001/*
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
22static const auto MS_IN_NS = 1000000;
23
24static const int64_t SLEEP_MS = 500;
25static const int64_t SLEEP_NS = SLEEP_MS * MS_IN_NS;
26// Conservatively assume that we might be descheduled for up to 50 ms
27static const int64_t SLACK_MS = 50;
28static const int64_t SLACK_NS = SLACK_MS * MS_IN_NS;
29
30TEST(SystemClock, SystemClock) {
31 auto startUptimeMs = android::uptimeMillis();
Suprabh Shuklaa55ef962020-08-05 00:01:36 -070032 auto startUptimeNs = android::uptimeNanos();
Greg Hackmann340079d2016-04-25 15:57:44 -070033 auto startRealtimeMs = android::elapsedRealtime();
34 auto startRealtimeNs = android::elapsedRealtimeNano();
35
36 ASSERT_GT(startUptimeMs, 0)
37 << "uptimeMillis() reported an impossible uptime";
Suprabh Shuklaa55ef962020-08-05 00:01:36 -070038 ASSERT_GT(startUptimeNs, 0)
39 << "uptimeNanos() reported an impossible uptime";
Greg Hackmann340079d2016-04-25 15:57:44 -070040 ASSERT_GE(startRealtimeMs, startUptimeMs)
41 << "elapsedRealtime() thinks we've suspended for negative time";
Suprabh Shuklaa55ef962020-08-05 00:01:36 -070042 ASSERT_GE(startRealtimeNs, startUptimeNs)
Greg Hackmann340079d2016-04-25 15:57:44 -070043 << "elapsedRealtimeNano() thinks we've suspended for negative time";
44
Suprabh Shuklaa55ef962020-08-05 00:01:36 -070045 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
Greg Hackmann340079d2016-04-25 15:57:44 -070050 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();
Suprabh Shuklaa55ef962020-08-05 00:01:36 -070062 auto endUptimeNs = android::uptimeNanos();
Greg Hackmann340079d2016-04-25 15:57:44 -070063 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()";
Suprabh Shuklaa55ef962020-08-05 00:01:36 -070070 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()";
Greg Hackmann340079d2016-04-25 15:57:44 -070074 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
Suprabh Shuklaa55ef962020-08-05 00:01:36 -070083 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
Greg Hackmann340079d2016-04-25 15:57:44 -070088 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}