1 /*
2 * Copyright (C) 2023 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/importers/common/clock_converter.h"
18 #include "src/trace_processor/importers/common/clock_tracker.h"
19
20 #include <random>
21
22 #include "src/trace_processor/importers/common/metadata_tracker.h"
23 #include "src/trace_processor/storage/trace_storage.h"
24 #include "src/trace_processor/types/trace_processor_context.h"
25 #include "test/gtest_and_gmock.h"
26
27 namespace perfetto {
28 namespace trace_processor {
29
30 class ClockConverterTest : public ::testing::Test {
31 public:
ClockConverterTest()32 ClockConverterTest() { context_.storage.reset(new TraceStorage()); }
33
34 TraceProcessorContext context_;
35 ClockConverter cc_{&context_};
36 };
37
38 namespace {
39
40 using ::testing::NiceMock;
41 using Clock = protos::pbzero::ClockSnapshot::Clock;
42
43 static constexpr int64_t kMonotonic =
44 protos::pbzero::BuiltinClock::BUILTIN_CLOCK_MONOTONIC;
45 static constexpr int64_t kReal = protos::pbzero::ClockSnapshot::Clock::REALTIME;
46
TEST_F(ClockConverterTest,EmptyTable)47 TEST_F(ClockConverterTest, EmptyTable) {
48 EXPECT_FALSE(cc_.ToAbsTime(10).ok());
49 EXPECT_FALSE(cc_.ToMonotonic(10).ok());
50 }
51
TEST_F(ClockConverterTest,TrivialMonotonic)52 TEST_F(ClockConverterTest, TrivialMonotonic) {
53 tables::ClockSnapshotTable::Row row;
54 row.ts = 10;
55 row.clock_id = kMonotonic;
56 row.clock_value = 20;
57 context_.storage->mutable_clock_snapshot_table()->Insert(row);
58
59 EXPECT_TRUE(cc_.ToMonotonic(10).ok());
60 EXPECT_EQ(cc_.ToMonotonic(10).value(), 20);
61 }
62
TEST_F(ClockConverterTest,TrivialToRealtime)63 TEST_F(ClockConverterTest, TrivialToRealtime) {
64 tables::ClockSnapshotTable::Row row;
65 row.ts = 10;
66 row.clock_id = kReal;
67 row.clock_value = 20;
68 context_.storage->mutable_clock_snapshot_table()->Insert(row);
69
70 EXPECT_TRUE(cc_.ToRealtime(10).ok());
71 EXPECT_EQ(cc_.ToRealtime(10).value(), 20);
72 }
73
TEST_F(ClockConverterTest,TrivialToAbsTime)74 TEST_F(ClockConverterTest, TrivialToAbsTime) {
75 tables::ClockSnapshotTable::Row row;
76 row.ts = 10;
77 row.clock_id = kReal;
78 row.clock_value = 20;
79 context_.storage->mutable_clock_snapshot_table()->Insert(row);
80
81 EXPECT_TRUE(cc_.ToAbsTime(10).ok());
82 EXPECT_EQ(cc_.ToAbsTime(10).value(), "1970-01-01T00:00:00.000000020");
83 }
84
TEST_F(ClockConverterTest,Monotonic)85 TEST_F(ClockConverterTest, Monotonic) {
86 {
87 tables::ClockSnapshotTable::Row rows;
88 rows.ts = 10;
89 rows.clock_id = kMonotonic;
90 rows.clock_value = 10;
91 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
92 }
93 {
94 tables::ClockSnapshotTable::Row rows;
95 rows.ts = 20;
96 rows.clock_id = kMonotonic;
97 rows.clock_value = 10;
98 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
99 }
100 {
101 tables::ClockSnapshotTable::Row rows;
102 rows.ts = 30;
103 rows.clock_id = kMonotonic;
104 rows.clock_value = 20;
105 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
106 }
107 {
108 tables::ClockSnapshotTable::Row rows;
109 rows.ts = 40;
110 rows.clock_id = kMonotonic;
111 rows.clock_value = 20;
112 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
113 }
114
115 EXPECT_EQ(cc_.ToMonotonic(15).value(), 10);
116 EXPECT_EQ(cc_.ToMonotonic(25).value(), 15);
117 EXPECT_EQ(cc_.ToMonotonic(35).value(), 20);
118 EXPECT_EQ(cc_.ToMonotonic(45).value(), 25);
119 }
120
TEST_F(ClockConverterTest,Realtime)121 TEST_F(ClockConverterTest, Realtime) {
122 // We will add 3 snapshots for real time clock, and the last snapshot will be
123 // earlier then the second one.
124 {
125 tables::ClockSnapshotTable::Row rows;
126 rows.ts = 10;
127 rows.clock_id = kReal;
128 rows.clock_value = 0;
129 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
130 }
131 {
132 tables::ClockSnapshotTable::Row rows;
133 rows.ts = 20;
134 rows.clock_id = kReal;
135 rows.clock_value = 10;
136 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
137 }
138 {
139 tables::ClockSnapshotTable::Row rows;
140 rows.ts = 30;
141 rows.clock_id = kReal;
142 rows.clock_value = 5;
143 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
144 }
145
146 EXPECT_EQ(cc_.ToRealtime(15).value(), 5);
147 EXPECT_EQ(cc_.ToRealtime(25).value(), 5);
148 EXPECT_EQ(cc_.ToRealtime(35).value(), 10);
149 }
150
TEST_F(ClockConverterTest,AbsTime)151 TEST_F(ClockConverterTest, AbsTime) {
152 // We will add 3 snapshots for real time clock, and the last snapshot will be
153 // earlier then the second one.
154 {
155 tables::ClockSnapshotTable::Row rows;
156 rows.ts = 10;
157 rows.clock_id = kReal;
158 rows.clock_value = 0;
159 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
160 }
161 {
162 tables::ClockSnapshotTable::Row rows;
163 rows.ts = 20;
164 rows.clock_id = kReal;
165 rows.clock_value = 1652904000000000000;
166 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
167 }
168 {
169 tables::ClockSnapshotTable::Row rows;
170 rows.ts = 30;
171 rows.clock_id = kReal;
172 rows.clock_value = 1652904000000000000 - 5;
173 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
174 }
175
176 EXPECT_EQ(cc_.ToAbsTime(15).value(), "1970-01-01T00:00:00.000000005");
177 EXPECT_EQ(cc_.ToAbsTime(25).value(), "2022-05-18T19:59:59.999999995");
178 EXPECT_EQ(cc_.ToAbsTime(35).value(), "2022-05-18T20:00:00.000000000");
179 }
180
181 } // namespace
182 } // namespace trace_processor
183 } // namespace perfetto
184