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