1 /*
2  * Copyright (C) 2020 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 #include "src/trace_processor/importers/proto/packet_sequence_state.h"
18 
19 namespace perfetto {
20 namespace trace_processor {
21 
InternMessage(uint32_t field_id,TraceBlobView message)22 void PacketSequenceStateGeneration::InternMessage(uint32_t field_id,
23                                                   TraceBlobView message) {
24   constexpr auto kIidFieldNumber = 1;
25 
26   uint64_t iid = 0;
27   auto message_start = message.data();
28   auto message_size = message.length();
29   protozero::ProtoDecoder decoder(message_start, message_size);
30 
31   auto field = decoder.FindField(kIidFieldNumber);
32   if (PERFETTO_UNLIKELY(!field)) {
33     PERFETTO_DLOG("Interned message without interning_id");
34     state_->context()->storage->IncrementStats(
35         stats::interned_data_tokenizer_errors);
36     return;
37   }
38   iid = field.as_uint64();
39 
40   auto res = interned_data_[field_id].emplace(
41       iid, InternedMessageView(std::move(message)));
42 
43   // If a message with this ID is already interned in the same generation,
44   // its data should not have changed (this is forbidden by the InternedData
45   // proto).
46   // TODO(eseckler): This DCHECK assumes that the message is encoded the
47   // same way if it is re-emitted.
48   PERFETTO_DCHECK(res.second ||
49                   (res.first->second.message().length() == message_size &&
50                    memcmp(res.first->second.message().data(), message_start,
51                           message_size) == 0));
52 }
53 
54 }  // namespace trace_processor
55 }  // namespace perfetto
56