1 /*
2 * Copyright (C) 2019 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 "src/trace_processor/clock_tracker.h"
18 #include "perfetto/base/optional.h"
19 #include "src/trace_processor/trace_processor_context.h"
20 #include "src/trace_processor/trace_storage.h"
21
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24
25 namespace perfetto {
26 namespace trace_processor {
27 namespace {
28
29 using ::testing::NiceMock;
30 class MockTraceStorage : public TraceStorage {
31 public:
MockTraceStorage()32 MockTraceStorage() : TraceStorage() {}
33 };
34
TEST(ClockTrackerTest,ClockDomainConversions)35 TEST(ClockTrackerTest, ClockDomainConversions) {
36 TraceProcessorContext context;
37 context.storage.reset(new NiceMock<MockTraceStorage>());
38 ClockTracker ct(&context);
39
40 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 0), base::nullopt);
41
42 ct.SyncClocks(ClockDomain::kRealTime, 10, 10010);
43 ct.SyncClocks(ClockDomain::kRealTime, 20, 20220);
44 ct.SyncClocks(ClockDomain::kRealTime, 30, 30030);
45 ct.SyncClocks(ClockDomain::kMonotonic, 1000, 100000);
46
47 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 0), 10000);
48 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 1), 10001);
49 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 9), 10009);
50 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 10), 10010);
51 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 11), 10011);
52 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 19), 10019);
53 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 20), 20220);
54 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 21), 20221);
55 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 29), 20229);
56 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 30), 30030);
57 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 40), 30040);
58
59 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kMonotonic, 0), 100000 - 1000);
60 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kMonotonic, 999), 100000 - 1);
61 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kMonotonic, 1000), 100000);
62 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kMonotonic, 1e6),
63 static_cast<int64_t>(100000 - 1000 + 1e6));
64 }
65
TEST(ClockTrackerTest,RealTimeClockMovingBackwards)66 TEST(ClockTrackerTest, RealTimeClockMovingBackwards) {
67 TraceProcessorContext context;
68 ClockTracker ct(&context);
69
70 ct.SyncClocks(ClockDomain::kRealTime, 10, 10010);
71 ct.SyncClocks(ClockDomain::kRealTime, 20, 10020);
72 ct.SyncClocks(ClockDomain::kRealTime, 40, 30040);
73 ct.SyncClocks(ClockDomain::kRealTime, 30, 40030);
74
75 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 11), 10011);
76 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 29), 10029);
77 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 30), 40030);
78 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 40), 40040);
79
80 ct.SyncClocks(ClockDomain::kRealTime, 50, 50000);
81 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 55), 50005);
82
83 ct.SyncClocks(ClockDomain::kRealTime, 11, 60011);
84 EXPECT_EQ(ct.ToTraceTime(ClockDomain::kRealTime, 20), 60020);
85 }
86
87 } // namespace
88 } // namespace trace_processor
89 } // namespace perfetto
90