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,TrivialToAbsTime)63 TEST_F(ClockConverterTest, TrivialToAbsTime) {
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_.ToAbsTime(10).ok());
71 EXPECT_EQ(cc_.ToAbsTime(10).value(), "1970-01-01T00:00:00.000000020");
72 }
73
TEST_F(ClockConverterTest,Monotonic)74 TEST_F(ClockConverterTest, Monotonic) {
75 {
76 tables::ClockSnapshotTable::Row rows;
77 rows.ts = 10;
78 rows.clock_id = kMonotonic;
79 rows.clock_value = 10;
80 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
81 }
82 {
83 tables::ClockSnapshotTable::Row rows;
84 rows.ts = 20;
85 rows.clock_id = kMonotonic;
86 rows.clock_value = 10;
87 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
88 }
89 {
90 tables::ClockSnapshotTable::Row rows;
91 rows.ts = 30;
92 rows.clock_id = kMonotonic;
93 rows.clock_value = 20;
94 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
95 }
96 {
97 tables::ClockSnapshotTable::Row rows;
98 rows.ts = 40;
99 rows.clock_id = kMonotonic;
100 rows.clock_value = 20;
101 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
102 }
103
104 EXPECT_EQ(cc_.ToMonotonic(15).value(), 10);
105 EXPECT_EQ(cc_.ToMonotonic(25).value(), 15);
106 EXPECT_EQ(cc_.ToMonotonic(35).value(), 20);
107 EXPECT_EQ(cc_.ToMonotonic(45).value(), 25);
108 }
109
TEST_F(ClockConverterTest,AbsTime)110 TEST_F(ClockConverterTest, AbsTime) {
111 // We will add 3 snapshots for real time clock, and the last snapshot will be
112 // earlier then the second one.
113 {
114 tables::ClockSnapshotTable::Row rows;
115 rows.ts = 10;
116 rows.clock_id = kReal;
117 rows.clock_value = 0;
118 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
119 }
120 {
121 tables::ClockSnapshotTable::Row rows;
122 rows.ts = 20;
123 rows.clock_id = kReal;
124 rows.clock_value = 1652904000000000000;
125 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
126 }
127 {
128 tables::ClockSnapshotTable::Row rows;
129 rows.ts = 30;
130 rows.clock_id = kReal;
131 rows.clock_value = 1652904000000000000 - 5;
132 context_.storage->mutable_clock_snapshot_table()->Insert(rows);
133 }
134
135 EXPECT_EQ(cc_.ToAbsTime(15).value(), "1970-01-01T00:00:00.000000005");
136 EXPECT_EQ(cc_.ToAbsTime(25).value(), "2022-05-18T19:59:59.999999995");
137 EXPECT_EQ(cc_.ToAbsTime(35).value(), "2022-05-18T20:00:00.000000000");
138 }
139
140 } // namespace
141 } // namespace trace_processor
142 } // namespace perfetto
143