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