Alexander Timin | 97d8785 | 2021-05-17 18:01:33 +0000 | [diff] [blame] | 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 | |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 20 | #include "perfetto/trace_processor/trace_blob_view.h" |
Alexander Timin | 97d8785 | 2021-05-17 18:01:33 +0000 | [diff] [blame] | 21 | |
| 22 | #include <unordered_map> |
| 23 | |
| 24 | namespace perfetto { |
| 25 | namespace trace_processor { |
| 26 | |
| 27 | #if PERFETTO_DCHECK_IS_ON() |
| 28 | // When called from GetOrCreateDecoder(), should include the stringified name of |
| 29 | // the MessageType. |
| 30 | #define PERFETTO_TYPE_IDENTIFIER PERFETTO_DEBUG_FUNCTION_IDENTIFIER() |
| 31 | #else // PERFETTO_DCHECK_IS_ON() |
| 32 | #define PERFETTO_TYPE_IDENTIFIER nullptr |
| 33 | #endif // PERFETTO_DCHECK_IS_ON() |
| 34 | |
| 35 | // Entry in an interning index, refers to the interned message. |
| 36 | class InternedMessageView { |
| 37 | public: |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 38 | explicit InternedMessageView(TraceBlobView msg) : message_(std::move(msg)) {} |
Alexander Timin | 97d8785 | 2021-05-17 18:01:33 +0000 | [diff] [blame] | 39 | |
| 40 | InternedMessageView(InternedMessageView&&) = default; |
| 41 | InternedMessageView& operator=(InternedMessageView&&) = default; |
| 42 | |
| 43 | // Allow copy by cloning the TraceBlobView. This is required for |
| 44 | // UpdateTracePacketDefaults(). |
| 45 | InternedMessageView(const InternedMessageView& view) |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 46 | : message_(view.message_.copy()) {} |
| 47 | |
Alexander Timin | 97d8785 | 2021-05-17 18:01:33 +0000 | [diff] [blame] | 48 | InternedMessageView& operator=(const InternedMessageView& view) { |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 49 | this->message_ = view.message_.copy(); |
Alexander Timin | 97d8785 | 2021-05-17 18:01:33 +0000 | [diff] [blame] | 50 | this->decoder_ = nullptr; |
| 51 | this->decoder_type_ = nullptr; |
| 52 | this->submessages_.clear(); |
| 53 | return *this; |
| 54 | } |
| 55 | |
| 56 | // Lazily initializes and returns the decoder object for the message. The |
| 57 | // decoder is stored in the InternedMessageView to avoid having to parse the |
| 58 | // message multiple times. |
| 59 | template <typename MessageType> |
| 60 | typename MessageType::Decoder* GetOrCreateDecoder() { |
| 61 | if (!decoder_) { |
| 62 | // Lazy init the decoder and save it away, so that we don't have to |
| 63 | // reparse the message every time we access the interning entry. |
| 64 | decoder_ = std::unique_ptr<void, std::function<void(void*)>>( |
| 65 | new typename MessageType::Decoder(message_.data(), message_.length()), |
| 66 | [](void* obj) { |
| 67 | delete reinterpret_cast<typename MessageType::Decoder*>(obj); |
| 68 | }); |
| 69 | decoder_type_ = PERFETTO_TYPE_IDENTIFIER; |
| 70 | } |
| 71 | // Verify that the type of the decoder didn't change. |
| 72 | if (PERFETTO_TYPE_IDENTIFIER && |
| 73 | strcmp(decoder_type_, |
| 74 | // GCC complains if this arg can be null. |
| 75 | PERFETTO_TYPE_IDENTIFIER ? PERFETTO_TYPE_IDENTIFIER : "") != 0) { |
| 76 | PERFETTO_FATAL( |
| 77 | "Interning entry accessed under different types! previous type: " |
| 78 | "%s. new type: %s.", |
| 79 | decoder_type_, PERFETTO_DEBUG_FUNCTION_IDENTIFIER()); |
| 80 | } |
| 81 | return reinterpret_cast<typename MessageType::Decoder*>(decoder_.get()); |
| 82 | } |
| 83 | |
| 84 | // Lookup a submessage of the interned message, which is then itself stored |
| 85 | // as InternedMessageView, so that we only need to parse it once. Returns |
| 86 | // nullptr if the field isn't set. |
| 87 | // TODO(eseckler): Support repeated fields. |
| 88 | template <typename MessageType, uint32_t FieldId> |
| 89 | InternedMessageView* GetOrCreateSubmessageView() { |
| 90 | auto it = submessages_.find(FieldId); |
| 91 | if (it != submessages_.end()) |
| 92 | return it->second.get(); |
| 93 | auto* decoder = GetOrCreateDecoder<MessageType>(); |
| 94 | // Calls the at() template method on the decoder. |
| 95 | auto field = decoder->template at<FieldId>().as_bytes(); |
| 96 | if (!field.data) |
| 97 | return nullptr; |
Primiano Tucci | 3264b59 | 2021-11-08 18:20:51 +0000 | [diff] [blame^] | 98 | TraceBlobView submessage = message_.slice(field.data, field.size); |
Alexander Timin | 97d8785 | 2021-05-17 18:01:33 +0000 | [diff] [blame] | 99 | InternedMessageView* submessage_view = |
| 100 | new InternedMessageView(std::move(submessage)); |
| 101 | submessages_.emplace_hint( |
| 102 | it, FieldId, std::unique_ptr<InternedMessageView>(submessage_view)); |
| 103 | return submessage_view; |
| 104 | } |
| 105 | |
| 106 | const TraceBlobView& message() { return message_; } |
| 107 | |
| 108 | private: |
| 109 | using SubMessageViewMap = |
| 110 | std::unordered_map<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_ |