xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/i2c/i2c_tracker.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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/i2c/i2c_tracker.h"
18 #include "perfetto/ext/base/string_utils.h"
19 
20 namespace perfetto {
21 namespace trace_processor {
22 
I2cTracker(TraceProcessorContext * context)23 I2cTracker::I2cTracker(TraceProcessorContext* context) : context_(context) {
24   for (size_t i = 0; i < kMaxI2cAdapters; i++) {
25     StringId id = kNullStringId;
26     base::StackString<255> adapter_name("i2c-%zu", i);
27     id = context_->storage->InternString(adapter_name.string_view());
28     i2c_adapter_to_string_id_[i] = id;
29   }
30 }
31 
32 I2cTracker::~I2cTracker() = default;
33 
Enter(int64_t ts,UniqueTid utid,uint32_t adapter_nr,uint32_t msg_nr)34 void I2cTracker::Enter(int64_t ts,
35                        UniqueTid utid,
36                        uint32_t adapter_nr,
37                        uint32_t msg_nr) {
38   StringId name = i2c_adapter_to_string_id_[adapter_nr];
39   if (name.is_null())
40     return;
41   TrackId track_id = context_->track_tracker->InternThreadTrack(utid);
42   std::vector<I2cAdapterMessageCount>& ops = inflight_i2c_ops_[utid];
43   if (ops.empty()) {
44     context_->slice_tracker->Begin(ts, track_id, kNullStringId, name);
45     I2cAdapterMessageCount req_count;
46     req_count.adapter_nr = adapter_nr;
47     req_count.nr_msgs = msg_nr + 1;
48     ops.push_back(req_count);
49   } else {
50     ops.back().nr_msgs = std::max(msg_nr + 1, ops.back().nr_msgs);
51   }
52 }
53 
Exit(int64_t ts,UniqueTid utid,uint32_t adapter_nr,uint32_t nr_msgs)54 void I2cTracker::Exit(int64_t ts,
55                       UniqueTid utid,
56                       uint32_t adapter_nr,
57                       uint32_t nr_msgs) {
58   StringId name = i2c_adapter_to_string_id_[adapter_nr];
59   if (name.is_null())
60     return;
61   std::vector<I2cAdapterMessageCount>& ops = inflight_i2c_ops_[utid];
62   if (ops.empty())
63     return;
64   if (ops.back().adapter_nr != adapter_nr || ops.back().nr_msgs != nr_msgs)
65     return;
66   TrackId track_id = context_->track_tracker->InternThreadTrack(utid);
67   ops.pop_back();
68   context_->slice_tracker->End(ts, track_id, kNullStringId, name);
69 }
70 
71 }  // namespace trace_processor
72 }  // namespace perfetto
73