xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/common/flow_tracker.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 <limits>
18 #include <optional>
19 
20 #include <stdint.h>
21 
22 #include "src/trace_processor/importers/common/flow_tracker.h"
23 #include "src/trace_processor/importers/common/slice_tracker.h"
24 #include "src/trace_processor/storage/trace_storage.h"
25 #include "src/trace_processor/types/trace_processor_context.h"
26 
27 namespace perfetto {
28 namespace trace_processor {
29 
FlowTracker(TraceProcessorContext * context)30 FlowTracker::FlowTracker(TraceProcessorContext* context) : context_(context) {
31   name_key_id_ = context_->storage->InternString("name");
32   cat_key_id_ = context_->storage->InternString("cat");
33 }
34 
35 FlowTracker::~FlowTracker() = default;
36 
37 /* TODO: if we report a flow event earlier that a corresponding slice then
38   flow event would not be added, and it will increase "flow_no_enclosing_slice"
39   In catapult, it was possible to report a flow after an enclosing slice if
40   timestamps were equal. But because of our seqential processing of a trace
41   it is a bit tricky to make it here.
42   We suspect that this case is too rare or impossible */
Begin(TrackId track_id,FlowId flow_id)43 void FlowTracker::Begin(TrackId track_id, FlowId flow_id) {
44   std::optional<SliceId> open_slice_id =
45       context_->slice_tracker->GetTopmostSliceOnTrack(track_id);
46   if (!open_slice_id) {
47     context_->storage->IncrementStats(stats::flow_no_enclosing_slice);
48     return;
49   }
50   Begin(open_slice_id.value(), flow_id);
51 }
52 
Begin(SliceId slice_id,FlowId flow_id)53 void FlowTracker::Begin(SliceId slice_id, FlowId flow_id) {
54   auto it_and_ins = flow_to_slice_map_.Insert(flow_id, slice_id);
55   if (!it_and_ins.second) {
56     context_->storage->IncrementStats(stats::flow_duplicate_id);
57     return;
58   }
59 }
60 
Step(TrackId track_id,FlowId flow_id)61 void FlowTracker::Step(TrackId track_id, FlowId flow_id) {
62   std::optional<SliceId> open_slice_id =
63       context_->slice_tracker->GetTopmostSliceOnTrack(track_id);
64   if (!open_slice_id) {
65     context_->storage->IncrementStats(stats::flow_no_enclosing_slice);
66     return;
67   }
68   Step(open_slice_id.value(), flow_id);
69 }
70 
Step(SliceId slice_id,FlowId flow_id)71 void FlowTracker::Step(SliceId slice_id, FlowId flow_id) {
72   auto* it = flow_to_slice_map_.Find(flow_id);
73   if (!it) {
74     context_->storage->IncrementStats(stats::flow_step_without_start);
75     return;
76   }
77   SliceId slice_out_id = *it;
78   InsertFlow(flow_id, slice_out_id, slice_id);
79   *it = slice_id;
80 }
81 
End(TrackId track_id,FlowId flow_id,bool bind_enclosing_slice,bool close_flow)82 void FlowTracker::End(TrackId track_id,
83                       FlowId flow_id,
84                       bool bind_enclosing_slice,
85                       bool close_flow) {
86   if (!bind_enclosing_slice) {
87     pending_flow_ids_map_[track_id].push_back(flow_id);
88     return;
89   }
90   std::optional<SliceId> open_slice_id =
91       context_->slice_tracker->GetTopmostSliceOnTrack(track_id);
92   if (!open_slice_id) {
93     context_->storage->IncrementStats(stats::flow_no_enclosing_slice);
94     return;
95   }
96   End(open_slice_id.value(), flow_id, close_flow);
97 }
98 
End(SliceId slice_id,FlowId flow_id,bool close_flow)99 void FlowTracker::End(SliceId slice_id, FlowId flow_id, bool close_flow) {
100   auto* it = flow_to_slice_map_.Find(flow_id);
101   if (!it) {
102     context_->storage->IncrementStats(stats::flow_end_without_start);
103     return;
104   }
105   SliceId slice_out_id = *it;
106   if (close_flow)
107     flow_to_slice_map_.Erase(flow_id);
108   InsertFlow(flow_id, slice_out_id, slice_id);
109 }
110 
IsActive(FlowId flow_id) const111 bool FlowTracker::IsActive(FlowId flow_id) const {
112   return flow_to_slice_map_.Find(flow_id) != nullptr;
113 }
114 
GetFlowIdForV1Event(uint64_t source_id,StringId cat,StringId name)115 FlowId FlowTracker::GetFlowIdForV1Event(uint64_t source_id,
116                                         StringId cat,
117                                         StringId name) {
118   V1FlowId v1_flow_id = {source_id, cat, name};
119   auto* iter = v1_flow_id_to_flow_id_map_.Find(v1_flow_id);
120   if (iter)
121     return *iter;
122   FlowId new_id = v1_id_counter_++;
123   flow_id_to_v1_flow_id_map_[new_id] = v1_flow_id;
124   v1_flow_id_to_flow_id_map_[v1_flow_id] = new_id;
125   return new_id;
126 }
127 
ClosePendingEventsOnTrack(TrackId track_id,SliceId slice_id)128 void FlowTracker::ClosePendingEventsOnTrack(TrackId track_id,
129                                             SliceId slice_id) {
130   auto* iter = pending_flow_ids_map_.Find(track_id);
131   if (!iter)
132     return;
133 
134   for (FlowId flow_id : *iter) {
135     SliceId slice_out_id = flow_to_slice_map_[flow_id];
136     InsertFlow(flow_id, slice_out_id, slice_id);
137   }
138 
139   pending_flow_ids_map_.Erase(track_id);
140 }
141 
InsertFlow(FlowId flow_id,SliceId slice_out_id,SliceId slice_in_id)142 void FlowTracker::InsertFlow(FlowId flow_id,
143                              SliceId slice_out_id,
144                              SliceId slice_in_id) {
145   tables::FlowTable::Row row(slice_out_id, slice_in_id, flow_id,
146                              kInvalidArgSetId);
147   auto id = context_->storage->mutable_flow_table()->Insert(row).id;
148 
149   auto* it = flow_id_to_v1_flow_id_map_.Find(flow_id);
150   if (it) {
151     // TODO(b/168007725): Add any args from v1 flow events and also export them.
152     auto inserter = context_->args_tracker->AddArgsTo(id);
153     inserter.AddArg(name_key_id_, Variadic::String(it->name));
154     inserter.AddArg(cat_key_id_, Variadic::String(it->cat));
155     context_->args_tracker->Flush();
156   }
157 }
158 
InsertFlow(SliceId slice_out_id,SliceId slice_in_id)159 void FlowTracker::InsertFlow(SliceId slice_out_id, SliceId slice_in_id) {
160   tables::FlowTable::Row row(slice_out_id, slice_in_id, std::nullopt,
161                              kInvalidArgSetId);
162   context_->storage->mutable_flow_table()->Insert(row);
163 }
164 
165 }  // namespace trace_processor
166 }  // namespace perfetto
167