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