• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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/ftrace/binder_tracker.h"
18 
19 #include "perfetto/base/logging.h"
20 #include "src/trace_processor/importers/common/args_tracker.h"
21 #include "src/trace_processor/importers/common/args_translation_table.h"
22 #include "src/trace_processor/importers/common/event_tracker.h"
23 #include "src/trace_processor/importers/common/flow_tracker.h"
24 #include "src/trace_processor/importers/common/process_tracker.h"
25 #include "src/trace_processor/importers/common/slice_tracker.h"
26 #include "src/trace_processor/importers/common/slice_translation_table.h"
27 #include "src/trace_processor/importers/common/track_tracker.h"
28 #include "src/trace_processor/storage/trace_storage.h"
29 #include "test/gtest_and_gmock.h"
30 
31 namespace perfetto {
32 namespace trace_processor {
33 namespace {
34 constexpr int kOneWay = 0x01;
35 
36 class BinderTrackerTest : public ::testing::Test {
37  public:
BinderTrackerTest()38   BinderTrackerTest() {
39     context.storage.reset(new TraceStorage());
40     context.global_args_tracker.reset(
41         new GlobalArgsTracker(context.storage.get()));
42     context.args_tracker.reset(new ArgsTracker(&context));
43     context.args_translation_table.reset(
44         new ArgsTranslationTable(context.storage.get()));
45     context.slice_tracker.reset(new SliceTracker(&context));
46     context.slice_translation_table.reset(
47         new SliceTranslationTable(context.storage.get()));
48     context.process_tracker.reset(new ProcessTracker(&context));
49     context.track_tracker.reset(new TrackTracker(&context));
50     context.flow_tracker.reset(new FlowTracker(&context));
51     binder_tracker = BinderTracker::GetOrCreate(&context);
52   }
53 
54  protected:
55   TraceProcessorContext context;
56   BinderTracker* binder_tracker;
57 };
58 
TEST_F(BinderTrackerTest,RequestReply)59 TEST_F(BinderTrackerTest, RequestReply) {
60   int64_t req_ts = 100;
61   int64_t req_recv_ts = 105;
62   int64_t rep_ts = 150;
63   int64_t rep_recv_ts = 155;
64 
65   uint32_t req_tid = 5;
66   uint32_t rep_tid = 10;
67 
68   int32_t req_transaction_id = 1234;
69   int32_t rep_transaction_id = 5678;
70 
71   binder_tracker->Transaction(req_ts, req_tid, req_transaction_id, 9, rep_tid,
72                               rep_tid, false, 0, kNullStringId);
73   binder_tracker->TransactionReceived(req_recv_ts, rep_tid, req_transaction_id);
74 
75   binder_tracker->Transaction(rep_ts, rep_tid, rep_transaction_id, 99, req_tid,
76                               req_tid, true, 0, kNullStringId);
77   binder_tracker->TransactionReceived(rep_recv_ts, req_tid, rep_transaction_id);
78 
79   const auto& thread = context.storage->thread_table();
80   const auto& track = context.storage->thread_track_table();
81   const auto& slice = context.storage->slice_table();
82   const auto& flow = context.storage->flow_table();
83   ASSERT_EQ(slice.row_count(), 2u);
84 
85   auto tid_for_slice = [&](uint32_t row) {
86     TrackId track_id = slice.track_id()[row];
87     UniqueTid utid = track.utid()[*track.id().IndexOf(track_id)];
88     return thread.tid()[utid];
89   };
90 
91   ASSERT_EQ(slice.ts()[0], req_ts);
92   ASSERT_EQ(slice.dur()[0], rep_recv_ts - req_ts);
93   ASSERT_EQ(tid_for_slice(0), req_tid);
94 
95   ASSERT_EQ(slice.ts()[1], req_recv_ts);
96   ASSERT_EQ(slice.dur()[1], rep_ts - req_recv_ts);
97   ASSERT_EQ(tid_for_slice(1), rep_tid);
98 
99   ASSERT_EQ(flow.row_count(), 1u);
100   ASSERT_EQ(flow.slice_out()[0], slice.id()[0]);
101   ASSERT_EQ(flow.slice_in()[0], slice.id()[1]);
102 }
103 
TEST_F(BinderTrackerTest,Oneway)104 TEST_F(BinderTrackerTest, Oneway) {
105   int64_t sen_ts = 100;
106   int64_t rec_ts = 150;
107 
108   uint32_t sen_tid = 5;
109   uint32_t rec_tid = 10;
110 
111   int32_t transaction_id = 1234;
112 
113   binder_tracker->Transaction(sen_ts, sen_tid, transaction_id, 9, rec_tid,
114                               rec_tid, false, kOneWay, kNullStringId);
115   binder_tracker->TransactionReceived(rec_ts, rec_tid, transaction_id);
116 
117   const auto& thread = context.storage->thread_table();
118   const auto& track = context.storage->thread_track_table();
119   const auto& slice = context.storage->slice_table();
120   const auto& flow = context.storage->flow_table();
121   ASSERT_EQ(slice.row_count(), 2u);
122 
123   auto tid_for_slice = [&](uint32_t row) {
124     TrackId track_id = slice.track_id()[row];
125     UniqueTid utid = track.utid()[*track.id().IndexOf(track_id)];
126     return thread.tid()[utid];
127   };
128 
129   ASSERT_EQ(slice.ts()[0], sen_ts);
130   ASSERT_EQ(slice.dur()[0], 0);
131   ASSERT_EQ(tid_for_slice(0), sen_tid);
132 
133   ASSERT_EQ(slice.ts()[1], rec_ts);
134   ASSERT_EQ(slice.dur()[1], 0);
135   ASSERT_EQ(tid_for_slice(1), rec_tid);
136 
137   ASSERT_EQ(flow.row_count(), 1u);
138   ASSERT_EQ(flow.slice_out()[0], slice.id()[0]);
139   ASSERT_EQ(flow.slice_in()[0], slice.id()[1]);
140 }
141 
142 }  // namespace
143 }  // namespace trace_processor
144 }  // namespace perfetto
145