1 /* 2 * Copyright (C) 2021 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_INTERNED_MESSAGE_VIEW_H_ 18 #define SRC_TRACE_PROCESSOR_UTIL_INTERNED_MESSAGE_VIEW_H_ 19 20 #include "perfetto/ext/base/flat_hash_map.h" 21 #include "perfetto/trace_processor/trace_blob_view.h" 22 23 namespace perfetto { 24 namespace trace_processor { 25 26 #if PERFETTO_DCHECK_IS_ON() 27 // When called from GetOrCreateDecoder(), should include the stringified name of 28 // the MessageType. 29 #define PERFETTO_TYPE_IDENTIFIER PERFETTO_DEBUG_FUNCTION_IDENTIFIER() 30 #else // PERFETTO_DCHECK_IS_ON() 31 #define PERFETTO_TYPE_IDENTIFIER nullptr 32 #endif // PERFETTO_DCHECK_IS_ON() 33 34 // Entry in an interning index, refers to the interned message. 35 class InternedMessageView { 36 public: InternedMessageView(TraceBlobView msg)37 explicit InternedMessageView(TraceBlobView msg) : message_(std::move(msg)) {} 38 39 InternedMessageView(InternedMessageView&&) = default; 40 InternedMessageView& operator=(InternedMessageView&&) = default; 41 42 // Allow copy by cloning the TraceBlobView. This is required for 43 // UpdateTracePacketDefaults(). InternedMessageView(const InternedMessageView & view)44 InternedMessageView(const InternedMessageView& view) 45 : message_(view.message_.copy()) {} 46 47 InternedMessageView& operator=(const InternedMessageView& view) { 48 this->message_ = view.message_.copy(); 49 this->decoder_ = nullptr; 50 this->decoder_type_ = nullptr; 51 this->submessages_.Clear(); 52 return *this; 53 } 54 55 // Lazily initializes and returns the decoder object for the message. The 56 // decoder is stored in the InternedMessageView to avoid having to parse the 57 // message multiple times. 58 template <typename MessageType> GetOrCreateDecoder()59 typename MessageType::Decoder* GetOrCreateDecoder() { 60 if (!decoder_) { 61 // Lazy init the decoder and save it away, so that we don't have to 62 // reparse the message every time we access the interning entry. 63 decoder_ = std::unique_ptr<void, std::function<void(void*)>>( 64 new typename MessageType::Decoder(message_.data(), message_.length()), 65 [](void* obj) { 66 delete reinterpret_cast<typename MessageType::Decoder*>(obj); 67 }); 68 decoder_type_ = PERFETTO_TYPE_IDENTIFIER; 69 } 70 // Verify that the type of the decoder didn't change. 71 if (PERFETTO_TYPE_IDENTIFIER && 72 strcmp(decoder_type_, 73 // GCC complains if this arg can be null. 74 PERFETTO_TYPE_IDENTIFIER ? PERFETTO_TYPE_IDENTIFIER : "") != 0) { 75 PERFETTO_FATAL( 76 "Interning entry accessed under different types! previous type: " 77 "%s. new type: %s.", 78 decoder_type_, PERFETTO_DEBUG_FUNCTION_IDENTIFIER()); 79 } 80 return reinterpret_cast<typename MessageType::Decoder*>(decoder_.get()); 81 } 82 83 // Lookup a submessage of the interned message, which is then itself stored 84 // as InternedMessageView, so that we only need to parse it once. Returns 85 // nullptr if the field isn't set. 86 // TODO(eseckler): Support repeated fields. 87 template <typename MessageType, uint32_t FieldId> GetOrCreateSubmessageView()88 InternedMessageView* GetOrCreateSubmessageView() { 89 auto it_and_ins = submessages_.Insert(FieldId, nullptr); 90 if (!it_and_ins.second) 91 return it_and_ins.first->get(); 92 auto* decoder = GetOrCreateDecoder<MessageType>(); 93 // Calls the at() template method on the decoder. 94 auto field = decoder->template at<FieldId>().as_bytes(); 95 if (!field.data) 96 return nullptr; 97 TraceBlobView submessage = message_.slice(field.data, field.size); 98 InternedMessageView* submessage_view = 99 new InternedMessageView(std::move(submessage)); 100 it_and_ins.first->reset(submessage_view); 101 return submessage_view; 102 } 103 message()104 const TraceBlobView& message() { return message_; } 105 106 private: 107 using SubMessageViewMap = 108 base::FlatHashMap<uint32_t /*field_id*/, 109 std::unique_ptr<InternedMessageView>>; 110 111 TraceBlobView message_; 112 113 // Stores the decoder for the message_, so that the message does not have to 114 // be re-decoded every time the interned message is looked up. Lazily 115 // initialized in GetOrCreateDecoder(). Since we don't know the type of the 116 // decoder until GetOrCreateDecoder() is called, we store the decoder as a 117 // void* unique_pointer with a destructor function that's supplied in 118 // GetOrCreateDecoder() when the decoder is created. 119 std::unique_ptr<void, std::function<void(void*)>> decoder_; 120 121 // Type identifier for the decoder. Only valid in debug builds and on 122 // supported platforms. Used to verify that GetOrCreateDecoder() is always 123 // called with the same template argument. 124 const char* decoder_type_ = nullptr; 125 126 // Views of submessages of the interned message. Submessages are lazily 127 // added by GetOrCreateSubmessageView(). By storing submessages and their 128 // decoders, we avoid having to decode submessages multiple times if they 129 // looked up often. 130 SubMessageViewMap submessages_; 131 }; 132 133 } // namespace trace_processor 134 } // namespace perfetto 135 136 #endif // SRC_TRACE_PROCESSOR_UTIL_INTERNED_MESSAGE_VIEW_H_ 137