1 /*
2  * Copyright (C) 2020 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/proto/async_track_set_tracker.h"
18 
19 #include "src/trace_processor/importers/common/args_tracker.h"
20 #include "src/trace_processor/importers/common/global_args_tracker.h"
21 #include "src/trace_processor/importers/common/track_tracker.h"
22 #include "src/trace_processor/types/trace_processor_context.h"
23 #include "test/gtest_and_gmock.h"
24 
25 namespace perfetto {
26 namespace trace_processor {
27 
28 class AsyncTrackSetTrackerUnittest : public testing::Test {
29  public:
AsyncTrackSetTrackerUnittest()30   AsyncTrackSetTrackerUnittest() {
31     context_.storage.reset(new TraceStorage());
32     context_.global_args_tracker.reset(new GlobalArgsTracker(&context_));
33     context_.args_tracker.reset(new ArgsTracker(&context_));
34     context_.track_tracker.reset(new TrackTracker(&context_));
35     context_.async_track_set_tracker.reset(new AsyncTrackSetTracker(&context_));
36 
37     storage_ = context_.storage.get();
38     tracker_ = context_.async_track_set_tracker.get();
39 
40     unnestable_id_ = tracker_->CreateUnnestableTrackSetForTesting(
41         1, storage_->InternString("test"));
42     legacy_unnestable_id_ =
43         tracker_->InternAndroidSet(2, storage_->InternString("test"));
44   }
45 
46  protected:
47   TraceStorage* storage_ = nullptr;
48   AsyncTrackSetTracker* tracker_ = nullptr;
49 
50   AsyncTrackSetTracker::TrackSetId unnestable_id_;
51   AsyncTrackSetTracker::TrackSetId legacy_unnestable_id_;
52 
53  private:
54   TraceProcessorContext context_;
55 };
56 
57 namespace {
58 
TEST_F(AsyncTrackSetTrackerUnittest,Smoke)59 TEST_F(AsyncTrackSetTrackerUnittest, Smoke) {
60   auto set_id = tracker_->InternAndroidSet(1, storage_->InternString("test"));
61 
62   auto begin = tracker_->Begin(set_id, 1);
63   auto end = tracker_->End(set_id, 1);
64 
65   ASSERT_EQ(begin, end);
66 
67   uint32_t row = *storage_->process_track_table().id().IndexOf(begin);
68   ASSERT_EQ(storage_->process_track_table().upid()[row], 1u);
69   ASSERT_EQ(storage_->process_track_table().name()[row],
70             storage_->InternString("test"));
71 }
72 
TEST_F(AsyncTrackSetTrackerUnittest,EndFirst)73 TEST_F(AsyncTrackSetTrackerUnittest, EndFirst) {
74   auto end = tracker_->End(unnestable_id_, 1);
75 
76   uint32_t row = *storage_->process_track_table().id().IndexOf(end);
77   ASSERT_EQ(storage_->process_track_table().upid()[row], 1u);
78   ASSERT_EQ(storage_->process_track_table().name()[row],
79             storage_->InternString("test"));
80 }
81 
TEST_F(AsyncTrackSetTrackerUnittest,LegacySaturating)82 TEST_F(AsyncTrackSetTrackerUnittest, LegacySaturating) {
83   auto begin = tracker_->Begin(legacy_unnestable_id_, 1);
84   auto begin_2 = tracker_->Begin(legacy_unnestable_id_, 1);
85 
86   ASSERT_EQ(begin, begin_2);
87 }
88 
TEST_F(AsyncTrackSetTrackerUnittest,Unnestable)89 TEST_F(AsyncTrackSetTrackerUnittest, Unnestable) {
90   auto begin = tracker_->Begin(unnestable_id_, 1);
91   auto end = tracker_->End(unnestable_id_, 1);
92   auto begin_2 = tracker_->Begin(unnestable_id_, 1);
93 
94   ASSERT_EQ(begin, end);
95   ASSERT_EQ(begin, begin_2);
96 }
97 
TEST_F(AsyncTrackSetTrackerUnittest,UnnestableMultipleEndAfterBegin)98 TEST_F(AsyncTrackSetTrackerUnittest, UnnestableMultipleEndAfterBegin) {
99   auto begin = tracker_->Begin(unnestable_id_, 1);
100   auto end = tracker_->End(unnestable_id_, 1);
101   auto end_2 = tracker_->End(unnestable_id_, 1);
102 
103   ASSERT_EQ(begin, end);
104   ASSERT_EQ(end, end_2);
105 }
106 
TEST_F(AsyncTrackSetTrackerUnittest,OnlyScoped)107 TEST_F(AsyncTrackSetTrackerUnittest, OnlyScoped) {
108   TrackId a = tracker_->Scoped(unnestable_id_, 100, 10);
109   TrackId b = tracker_->Scoped(unnestable_id_, 105, 2);
110   TrackId c = tracker_->Scoped(unnestable_id_, 107, 3);
111   TrackId d = tracker_->Scoped(unnestable_id_, 110, 5);
112 
113   ASSERT_NE(a, b);
114   ASSERT_EQ(b, c);
115   ASSERT_EQ(a, d);
116 }
117 
TEST_F(AsyncTrackSetTrackerUnittest,MixScopedAndBeginEnd)118 TEST_F(AsyncTrackSetTrackerUnittest, MixScopedAndBeginEnd) {
119   TrackId a = tracker_->Scoped(unnestable_id_, 100, 10);
120 
121   TrackId begin = tracker_->Begin(unnestable_id_, 777);
122   TrackId end = tracker_->End(unnestable_id_, 777);
123 
124   TrackId b = tracker_->Scoped(unnestable_id_, 105, 2);
125 
126   ASSERT_NE(a, begin);
127   ASSERT_NE(b, begin);
128   ASSERT_EQ(begin, end);
129 }
130 
TEST_F(AsyncTrackSetTrackerUnittest,DifferentTracksInterleave)131 TEST_F(AsyncTrackSetTrackerUnittest, DifferentTracksInterleave) {
132   TrackId b1 = tracker_->Begin(unnestable_id_, 666);
133   TrackId b2 = tracker_->Begin(legacy_unnestable_id_, 777);
134   TrackId e1 = tracker_->End(unnestable_id_, 666);
135   TrackId e2 = tracker_->End(legacy_unnestable_id_, 777);
136 
137   ASSERT_EQ(b1, e1);
138   ASSERT_EQ(b2, e2);
139   ASSERT_NE(b1, b2);
140 }
141 
TEST_F(AsyncTrackSetTrackerUnittest,DifferentCookieInterleave)142 TEST_F(AsyncTrackSetTrackerUnittest, DifferentCookieInterleave) {
143   TrackId b1 = tracker_->Begin(legacy_unnestable_id_, 666);
144   TrackId b2 = tracker_->Begin(legacy_unnestable_id_, 777);
145   TrackId e1 = tracker_->End(legacy_unnestable_id_, 666);
146   TrackId e2 = tracker_->End(legacy_unnestable_id_, 777);
147 
148   ASSERT_EQ(b1, e1);
149   ASSERT_EQ(b2, e2);
150   ASSERT_NE(b1, b2);
151 }
152 
TEST_F(AsyncTrackSetTrackerUnittest,DifferentCookieSequential)153 TEST_F(AsyncTrackSetTrackerUnittest, DifferentCookieSequential) {
154   TrackId b1 = tracker_->Begin(legacy_unnestable_id_, 666);
155   TrackId e1 = tracker_->End(legacy_unnestable_id_, 666);
156   TrackId b2 = tracker_->Begin(legacy_unnestable_id_, 777);
157   TrackId e2 = tracker_->End(legacy_unnestable_id_, 777);
158 
159   ASSERT_EQ(b1, e1);
160   ASSERT_EQ(b1, b2);
161   ASSERT_EQ(b2, e2);
162 }
163 
164 }  // namespace
165 }  // namespace trace_processor
166 }  // namespace perfetto
167