xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/i2c/i2c_tracker.h (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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_I2C_I2C_TRACKER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_I2C_I2C_TRACKER_H_
19 
20 #include <limits>
21 #include <tuple>
22 
23 #include "perfetto/ext/base/flat_hash_map.h"
24 #include "perfetto/ext/base/string_view.h"
25 #include "src/trace_processor/importers/common/slice_tracker.h"
26 #include "src/trace_processor/importers/common/track_tracker.h"
27 #include "src/trace_processor/storage/trace_storage.h"
28 #include "src/trace_processor/types/destructible.h"
29 #include "src/trace_processor/types/trace_processor_context.h"
30 
31 namespace perfetto {
32 namespace trace_processor {
33 
34 static constexpr size_t kMaxI2cAdapters = 256;
35 
36 using I2cAdapterId = uint32_t;
37 
38 class I2cTracker : public Destructible {
39  public:
40   I2cTracker(const I2cTracker&) = delete;
41   I2cTracker& operator=(const I2cTracker&) = delete;
42   ~I2cTracker() override;
GetOrCreate(TraceProcessorContext * context)43   static I2cTracker* GetOrCreate(TraceProcessorContext* context) {
44     if (!context->i2c_tracker) {
45       context->i2c_tracker.reset(new I2cTracker(context));
46     }
47     return static_cast<I2cTracker*>(context->i2c_tracker.get());
48   }
49 
50   struct I2cAdapterMessageCount {
51     I2cAdapterId adapter_nr{};
52     uint32_t nr_msgs{};
53   };
54 
55   // Enter processes the start of an I2C transaction, which consists of a
56   // series i2c_write and i2c_read's. Multiple messages may be included in a
57   // transaction. The position of a write or read message in the transaction
58   // is indicated by the msg_nr field.
59   void Enter(int64_t ts, UniqueTid utid, uint32_t adapter_nr, uint32_t msg_nr);
60 
61   // Exit processes the end of an I2C transaction, which is indicated by an
62   // i2c_result. The nr_msgs field indicates how many write or read requests
63   // are considered to be matching the current i2c_result.
64   void Exit(int64_t ts, UniqueTid utid, uint32_t adapter_nr, uint32_t nr_msgs);
65 
66  private:
67   explicit I2cTracker(TraceProcessorContext*);
68   TraceProcessorContext* const context_;
69   std::array<StringId, kMaxI2cAdapters> i2c_adapter_to_string_id_{};
70 
71   // In-flight I2C operation counts per I2C adapter per Unique TID. This is
72   // used to match an i2c_result message against the i2c_read and i2c_write
73   // messages that precede it in the transaction.
74   base::FlatHashMap<UniqueTid, std::vector<I2cAdapterMessageCount>>
75       inflight_i2c_ops_;
76 };
77 
78 }  // namespace trace_processor
79 }  // namespace perfetto
80 
81 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_I2C_I2C_TRACKER_H_
82