1 /* 2 * Copyright (C) 2022 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_ARGS_TRANSLATION_TABLE_H_ 18 #define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_ARGS_TRANSLATION_TABLE_H_ 19 20 #include <cstdint> 21 22 #include "perfetto/ext/base/flat_hash_map.h" 23 #include "perfetto/ext/base/optional.h" 24 #include "perfetto/ext/base/string_view.h" 25 #include "src/trace_processor/importers/common/args_tracker.h" 26 #include "src/trace_processor/storage/trace_storage.h" 27 #include "src/trace_processor/util/proto_to_args_parser.h" 28 29 namespace perfetto { 30 namespace trace_processor { 31 32 // Tracks and stores args translation rules. It allows Trace Processor 33 // to map for example hashes to their names. 34 class ArgsTranslationTable { 35 public: 36 using Key = util::ProtoToArgsParser::Key; 37 38 ArgsTranslationTable(TraceStorage* storage); 39 40 // Returns true if the translation table fully handles the arg, in which case 41 // the original arg doesn't need to be processed. This function has not added 42 // anything if returning false. 43 bool TranslateUnsignedIntegerArg(const Key& key, 44 uint64_t value, 45 ArgsTracker::BoundInserter& inserter); 46 AddChromeHistogramTranslationRule(uint64_t hash,base::StringView name)47 void AddChromeHistogramTranslationRule(uint64_t hash, base::StringView name) { 48 chrome_histogram_hash_to_name_.Insert(hash, name.ToStdString()); 49 } AddChromeUserEventTranslationRule(uint64_t hash,base::StringView action)50 void AddChromeUserEventTranslationRule(uint64_t hash, 51 base::StringView action) { 52 chrome_user_event_hash_to_action_.Insert(hash, action.ToStdString()); 53 } AddChromePerformanceMarkSiteTranslationRule(uint64_t hash,base::StringView name)54 void AddChromePerformanceMarkSiteTranslationRule(uint64_t hash, 55 base::StringView name) { 56 chrome_performance_mark_site_hash_to_name_.Insert(hash, name.ToStdString()); 57 } AddChromePerformanceMarkMarkTranslationRule(uint64_t hash,base::StringView name)58 void AddChromePerformanceMarkMarkTranslationRule(uint64_t hash, 59 base::StringView name) { 60 chrome_performance_mark_mark_hash_to_name_.Insert(hash, name.ToStdString()); 61 } 62 TranslateChromeHistogramHashForTesting(uint64_t hash)63 base::Optional<base::StringView> TranslateChromeHistogramHashForTesting( 64 uint64_t hash) const { 65 return TranslateChromeHistogramHash(hash); 66 } TranslateChromeUserEventHashForTesting(uint64_t hash)67 base::Optional<base::StringView> TranslateChromeUserEventHashForTesting( 68 uint64_t hash) const { 69 return TranslateChromeUserEventHash(hash); 70 } 71 base::Optional<base::StringView> TranslateChromePerformanceMarkSiteHashForTesting(uint64_t hash)72 TranslateChromePerformanceMarkSiteHashForTesting(uint64_t hash) const { 73 return TranslateChromePerformanceMarkSiteHash(hash); 74 } 75 base::Optional<base::StringView> TranslateChromePerformanceMarkMarkHashForTesting(uint64_t hash)76 TranslateChromePerformanceMarkMarkHashForTesting(uint64_t hash) const { 77 return TranslateChromePerformanceMarkMarkHash(hash); 78 } 79 80 private: 81 static constexpr char kChromeHistogramHashKey[] = 82 "chrome_histogram_sample.name_hash"; 83 static constexpr char kChromeHistogramNameKey[] = 84 "chrome_histogram_sample.name"; 85 86 static constexpr char kChromeUserEventHashKey[] = 87 "chrome_user_event.action_hash"; 88 static constexpr char kChromeUserEventActionKey[] = 89 "chrome_user_event.action"; 90 91 static constexpr char kChromePerformanceMarkSiteHashKey[] = 92 "chrome_hashed_performance_mark.site_hash"; 93 static constexpr char kChromePerformanceMarkSiteKey[] = 94 "chrome_hashed_performance_mark.site"; 95 96 static constexpr char kChromePerformanceMarkMarkHashKey[] = 97 "chrome_hashed_performance_mark.mark_hash"; 98 static constexpr char kChromePerformanceMarkMarkKey[] = 99 "chrome_hashed_performance_mark.mark"; 100 101 TraceStorage* storage_; 102 StringId interned_chrome_histogram_hash_key_; 103 StringId interned_chrome_histogram_name_key_; 104 StringId interned_chrome_user_event_hash_key_; 105 StringId interned_chrome_user_event_action_key_; 106 StringId interned_chrome_performance_mark_site_hash_key_; 107 StringId interned_chrome_performance_mark_site_key_; 108 StringId interned_chrome_performance_mark_mark_hash_key_; 109 StringId interned_chrome_performance_mark_mark_key_; 110 base::FlatHashMap<uint64_t, std::string> chrome_histogram_hash_to_name_; 111 base::FlatHashMap<uint64_t, std::string> chrome_user_event_hash_to_action_; 112 base::FlatHashMap<uint64_t, std::string> 113 chrome_performance_mark_site_hash_to_name_; 114 base::FlatHashMap<uint64_t, std::string> 115 chrome_performance_mark_mark_hash_to_name_; 116 117 base::Optional<base::StringView> TranslateChromeHistogramHash( 118 uint64_t hash) const; 119 base::Optional<base::StringView> TranslateChromeUserEventHash( 120 uint64_t hash) const; 121 base::Optional<base::StringView> TranslateChromePerformanceMarkSiteHash( 122 uint64_t hash) const; 123 base::Optional<base::StringView> TranslateChromePerformanceMarkMarkHash( 124 uint64_t hash) const; 125 }; 126 127 } // namespace trace_processor 128 } // namespace perfetto 129 130 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_ARGS_TRANSLATION_TABLE_H_ 131