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 static_cast<bool>(PERFETTO_TYPE_IDENTIFIER) 75 ? PERFETTO_TYPE_IDENTIFIER 76 : "") != 0) { 77 PERFETTO_FATAL( 78 "Interning entry accessed under different types! previous type: " 79 "%s. new type: %s.", 80 decoder_type_, PERFETTO_DEBUG_FUNCTION_IDENTIFIER()); 81 } 82 return reinterpret_cast<typename MessageType::Decoder*>(decoder_.get()); 83 } 84 85 // Lookup a submessage of the interned message, which is then itself stored 86 // as InternedMessageView, so that we only need to parse it once. Returns 87 // nullptr if the field isn't set. 88 // TODO(eseckler): Support repeated fields. 89 template <typename MessageType, uint32_t FieldId> GetOrCreateSubmessageView()90 InternedMessageView* GetOrCreateSubmessageView() { 91 auto it_and_ins = submessages_.Insert(FieldId, nullptr); 92 if (!it_and_ins.second) 93 return it_and_ins.first->get(); 94 auto* decoder = GetOrCreateDecoder<MessageType>(); 95 // Calls the at() template method on the decoder. 96 auto field = decoder->template at<FieldId>().as_bytes(); 97 if (!field.data) 98 return nullptr; 99 TraceBlobView submessage = message_.slice(field.data, field.size); 100 InternedMessageView* submessage_view = 101 new InternedMessageView(std::move(submessage)); 102 it_and_ins.first->reset(submessage_view); 103 return submessage_view; 104 } 105 message()106 const TraceBlobView& message() const { return message_; } 107 108 private: 109 using SubMessageViewMap = 110 base::FlatHashMap<uint32_t /*field_id*/, 111 std::unique_ptr<InternedMessageView>>; 112 113 TraceBlobView message_; 114 115 // Stores the decoder for the message_, so that the message does not have to 116 // be re-decoded every time the interned message is looked up. Lazily 117 // initialized in GetOrCreateDecoder(). Since we don't know the type of the 118 // decoder until GetOrCreateDecoder() is called, we store the decoder as a 119 // void* unique_pointer with a destructor function that's supplied in 120 // GetOrCreateDecoder() when the decoder is created. 121 std::unique_ptr<void, std::function<void(void*)>> decoder_; 122 123 // Type identifier for the decoder. Only valid in debug builds and on 124 // supported platforms. Used to verify that GetOrCreateDecoder() is always 125 // called with the same template argument. 126 const char* decoder_type_ = nullptr; 127 128 // Views of submessages of the interned message. Submessages are lazily 129 // added by GetOrCreateSubmessageView(). By storing submessages and their 130 // decoders, we avoid having to decode submessages multiple times if they 131 // looked up often. 132 SubMessageViewMap submessages_; 133 }; 134 135 } // namespace trace_processor 136 } // namespace perfetto 137 138 #endif // SRC_TRACE_PROCESSOR_UTIL_INTERNED_MESSAGE_VIEW_H_ 139