xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/common/metadata_tracker.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_METADATA_TRACKER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_METADATA_TRACKER_H_
19 
20 #include <array>
21 #include <cstddef>
22 #include <cstdint>
23 #include <optional>
24 
25 #include "perfetto/trace_processor/basic_types.h"
26 #include "src/trace_processor/storage/metadata.h"
27 #include "src/trace_processor/storage/trace_storage.h"
28 
29 namespace perfetto::trace_processor {
30 
31 // Tracks information in the metadata table.
32 class MetadataTracker {
33  public:
34   explicit MetadataTracker(TraceStorage* storage);
35 
36   // Example usage:
37   // SetMetadata(metadata::benchmark_name,
38   //             Variadic::String(storage->InternString("foo"));
39   // Returns the id of the new entry.
40   MetadataId SetMetadata(metadata::KeyId key, Variadic value);
41 
42   // Example usage:
43   // AppendMetadata(metadata::benchmark_story_tags,
44   //                Variadic::String(storage->InternString("bar"));
45   // Returns the id of the new entry.
46   MetadataId AppendMetadata(metadata::KeyId key, Variadic value);
47 
48   // Sets a metadata entry using any interned string as key.
49   // Returns the id of the new entry.
50   MetadataId SetDynamicMetadata(StringId key, Variadic value);
51 
52   // Reads back a set metadata value.
53   // Only kSingle types are supported right now.
54   std::optional<SqlValue> GetMetadata(metadata::KeyId key);
55 
56   // Tracks how many ChromeMetadata bundles have been parsed.
IncrementChromeMetadataBundleCount()57   uint32_t IncrementChromeMetadataBundleCount() {
58     return ++chrome_metadata_bundle_count_;
59   }
60 
61  private:
62   static constexpr size_t kNumKeys =
63       static_cast<size_t>(metadata::KeyId::kNumKeys);
64   static constexpr size_t kNumKeyTypes =
65       static_cast<size_t>(metadata::KeyType::kNumKeyTypes);
66 
67   void WriteValue(uint32_t row, Variadic value);
68 
69   std::array<StringId, kNumKeys> key_ids_;
70   std::array<StringId, kNumKeyTypes> key_type_ids_;
71   uint32_t chrome_metadata_bundle_count_ = 0;
72 
73   TraceStorage* storage_;
74 };
75 
76 }  // namespace perfetto::trace_processor
77 
78 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_METADATA_TRACKER_H_
79