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