xref: /aosp_15_r20/external/perfetto/src/trace_processor/util/proto_profiler.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_UTIL_PROTO_PROFILER_H_
18 #define SRC_TRACE_PROCESSOR_UTIL_PROTO_PROFILER_H_
19 
20 #include <algorithm>
21 #include <vector>
22 
23 #include "perfetto/ext/base/hash.h"
24 #include "perfetto/protozero/field.h"
25 #include "src/trace_processor/util/descriptors.h"
26 
27 namespace perfetto {
28 namespace trace_processor {
29 namespace util {
30 
31 class SizeProfileComputer {
32  public:
33   struct Field {
34     Field(uint32_t field_idx_in,
35           const FieldDescriptor* field_descriptor_in,
36           uint32_t type_in,
37           const ProtoDescriptor* proto_descriptor_in);
38 
has_field_nameField39     bool has_field_name() const {
40       return field_descriptor || field_idx == static_cast<uint32_t>(-1);
41     }
42 
43     std::string field_name() const;
44     std::string type_name() const;
45 
46     bool operator==(const Field& other) const {
47       return field_idx == other.field_idx && type == other.type;
48     }
49 
50     uint32_t field_idx;
51     uint32_t type;
52     const FieldDescriptor* field_descriptor;
53     const ProtoDescriptor* proto_descriptor;
54   };
55 
56   using FieldPath = std::vector<Field>;
57   struct FieldPathHasher {
58     using argument_type = FieldPath;
59     using result_type = size_t;
60 
operatorFieldPathHasher61     result_type operator()(const argument_type& p) const {
62       size_t h = 0u;
63       for (auto v : p) {
64         h += (std::hash<uint32_t>{}(v.field_idx) +
65               std::hash<uint32_t>{}(v.type));
66         h = (h << 5) - h;
67       }
68       return h;
69     }
70   };
71 
72   explicit SizeProfileComputer(DescriptorPool* pool,
73                                const std::string& message_type);
74 
75   // Re-initializes the computer to iterate over samples (i.e. all encountered
76   // field sizes) for each field path in trace proto contained in the given
77   // range.
78   // TODO(kraskevich): consider switching to internal DescriptorPool.
79   void Reset(const uint8_t* ptr, size_t size);
80 
81   // Returns the next sample size, or std::nullopt if data is exhausted. The
82   // associated path can be queried with GetPath().
83   std::optional<size_t> GetNext();
84 
85   // Returns the field path associated with the last sample returned by
86   // GetNext().
GetPath()87   const FieldPath& GetPath() const { return field_path_; }
88 
89   operator bool() const;
90 
91  private:
92   size_t GetFieldSize(const protozero::Field& f);
93 
94   DescriptorPool* pool_;
95   uint32_t root_message_idx_;
96   // The current 'stack' we're considering as we parse the protobuf.
97   // For example if we're currently looking at the varint field baz which is
98   // nested inside message Bar which is in turn a field named bar on the message
99   // Foo. Then the stack would be: Foo, #bar, Bar, #baz, int
100   // We keep track of both the field names (#bar, #baz) and the field types
101   // (Foo, Bar, int) as sometimes we are intrested in which fields are big
102   // and sometimes which types are big.
103   FieldPath field_path_;
104 
105   // Internal state used to iterate over field path.
106   struct State {
107     const ProtoDescriptor* descriptor;
108     protozero::ProtoDecoder decoder;
109     size_t overhead;
110     size_t unknown;
111   };
112   std::vector<State> state_stack_;
113 };
114 
115 }  // namespace util
116 }  // namespace trace_processor
117 }  // namespace perfetto
118 
119 #endif  // SRC_TRACE_PROCESSOR_UTIL_PROTO_PROFILER_H_
120