1 /*
2 * Copyright (C) 2019 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/metadata_tracker.h"
18
19 #include "src/trace_processor/importers/common/process_tracker.h"
20 #include "src/trace_processor/types/trace_processor_context.h"
21
22 namespace perfetto {
23 namespace trace_processor {
24
MetadataTracker(TraceProcessorContext * context)25 MetadataTracker::MetadataTracker(TraceProcessorContext* context)
26 : context_(context) {
27 for (uint32_t i = 0; i < kNumKeys; ++i) {
28 key_ids_[i] = context->storage->InternString(metadata::kNames[i]);
29 }
30 for (uint32_t i = 0; i < kNumKeyTypes; ++i) {
31 key_type_ids_[i] =
32 context->storage->InternString(metadata::kKeyTypeNames[i]);
33 }
34 }
35
SetMetadata(metadata::KeyIDs key,Variadic value)36 MetadataId MetadataTracker::SetMetadata(metadata::KeyIDs key, Variadic value) {
37 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::KeyType::kSingle);
38 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
39
40 auto* metadata_table = context_->storage->mutable_metadata_table();
41 uint32_t key_idx = static_cast<uint32_t>(key);
42 base::Optional<uint32_t> opt_row =
43 metadata_table->name().IndexOf(metadata::kNames[key_idx]);
44 if (opt_row) {
45 WriteValue(*opt_row, value);
46 return metadata_table->id()[*opt_row];
47 }
48
49 tables::MetadataTable::Row row;
50 row.name = key_ids_[key_idx];
51 row.key_type = key_type_ids_[static_cast<size_t>(metadata::KeyType::kSingle)];
52
53 auto id_and_row = metadata_table->Insert(row);
54 WriteValue(id_and_row.row, value);
55 return id_and_row.id;
56 }
57
AppendMetadata(metadata::KeyIDs key,Variadic value)58 MetadataId MetadataTracker::AppendMetadata(metadata::KeyIDs key,
59 Variadic value) {
60 PERFETTO_DCHECK(key < metadata::kNumKeys);
61 PERFETTO_DCHECK(metadata::kKeyTypes[key] == metadata::KeyType::kMulti);
62 PERFETTO_DCHECK(value.type == metadata::kValueTypes[key]);
63
64 uint32_t key_idx = static_cast<uint32_t>(key);
65 tables::MetadataTable::Row row;
66 row.name = key_ids_[key_idx];
67 row.key_type = key_type_ids_[static_cast<size_t>(metadata::KeyType::kMulti)];
68
69 auto* metadata_table = context_->storage->mutable_metadata_table();
70 auto id_and_row = metadata_table->Insert(row);
71 WriteValue(id_and_row.row, value);
72 return id_and_row.id;
73 }
74
WriteValue(uint32_t row,Variadic value)75 void MetadataTracker::WriteValue(uint32_t row, Variadic value) {
76 auto* metadata_table = context_->storage->mutable_metadata_table();
77 switch (value.type) {
78 case Variadic::Type::kInt:
79 metadata_table->mutable_int_value()->Set(row, value.int_value);
80 break;
81 case Variadic::Type::kString:
82 metadata_table->mutable_str_value()->Set(row, value.string_value);
83 break;
84 case Variadic::Type::kJson:
85 case Variadic::Type::kBool:
86 case Variadic::Type::kPointer:
87 case Variadic::Type::kUint:
88 case Variadic::Type::kReal:
89 PERFETTO_FATAL("Unsupported value type");
90 }
91 }
92
93 } // namespace trace_processor
94 } // namespace perfetto
95