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