Skip to content

Commit

Permalink
Merge "[chrome] Support chrome trigger hash translation" into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Etienne Pierre-doray authored and Gerrit Code Review committed Nov 22, 2024
2 parents 65818b4 + 9e11af5 commit 4e3c160
Show file tree
Hide file tree
Showing 20 changed files with 236 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -5488,6 +5488,7 @@ genrule {
"protos/perfetto/metrics/chrome/scroll_jank.proto",
"protos/perfetto/metrics/chrome/scroll_jank_v3.proto",
"protos/perfetto/metrics/chrome/slice_names.proto",
"protos/perfetto/metrics/chrome/study_hashes.proto",
"protos/perfetto/metrics/chrome/test_chrome_metric.proto",
"protos/perfetto/metrics/chrome/touch_jank.proto",
"protos/perfetto/metrics/chrome/unsymbolized_args.proto",
Expand Down Expand Up @@ -13240,6 +13241,7 @@ genrule {
"src/trace_processor/metrics/sql/chrome/chrome_scroll_jank_v3.sql",
"src/trace_processor/metrics/sql/chrome/chrome_slice_names.sql",
"src/trace_processor/metrics/sql/chrome/chrome_stack_samples_for_task.sql",
"src/trace_processor/metrics/sql/chrome/chrome_study_hashes.sql",
"src/trace_processor/metrics/sql/chrome/chrome_tasks.sql",
"src/trace_processor/metrics/sql/chrome/chrome_tasks_delaying_input_processing.sql",
"src/trace_processor/metrics/sql/chrome/chrome_tasks_delaying_input_processing_base.sql",
Expand Down
2 changes: 2 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2545,6 +2545,7 @@ perfetto_filegroup(
"src/trace_processor/metrics/sql/chrome/chrome_scroll_jank_v3.sql",
"src/trace_processor/metrics/sql/chrome/chrome_slice_names.sql",
"src/trace_processor/metrics/sql/chrome/chrome_stack_samples_for_task.sql",
"src/trace_processor/metrics/sql/chrome/chrome_study_hashes.sql",
"src/trace_processor/metrics/sql/chrome/chrome_tasks.sql",
"src/trace_processor/metrics/sql/chrome/chrome_tasks_delaying_input_processing.sql",
"src/trace_processor/metrics/sql/chrome/chrome_tasks_delaying_input_processing_base.sql",
Expand Down Expand Up @@ -5230,6 +5231,7 @@ perfetto_proto_library(
"protos/perfetto/metrics/chrome/scroll_jank.proto",
"protos/perfetto/metrics/chrome/scroll_jank_v3.proto",
"protos/perfetto/metrics/chrome/slice_names.proto",
"protos/perfetto/metrics/chrome/study_hashes.proto",
"protos/perfetto/metrics/chrome/test_chrome_metric.proto",
"protos/perfetto/metrics/chrome/touch_jank.proto",
"protos/perfetto/metrics/chrome/unsymbolized_args.proto",
Expand Down
1 change: 1 addition & 0 deletions protos/perfetto/metrics/chrome/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ perfetto_proto_library("@TYPE@") {
"scroll_jank.proto",
"scroll_jank_v3.proto",
"slice_names.proto",
"study_hashes.proto",
"test_chrome_metric.proto",
"touch_jank.proto",
"unsymbolized_args.proto",
Expand Down
2 changes: 2 additions & 0 deletions protos/perfetto/metrics/chrome/all_chrome_metrics.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import "protos/perfetto/metrics/chrome/args_class_names.proto";
import "protos/perfetto/metrics/chrome/dropped_frames.proto";
import "protos/perfetto/metrics/chrome/frame_times.proto";
import "protos/perfetto/metrics/chrome/histogram_hashes.proto";
import "protos/perfetto/metrics/chrome/study_hashes.proto";
import "protos/perfetto/metrics/chrome/histogram_summaries.proto";
import "protos/perfetto/metrics/chrome/long_latency.proto";
import "protos/perfetto/metrics/chrome/media_metric.proto";
Expand Down Expand Up @@ -55,4 +56,5 @@ extend TraceMetrics {
optional ChromeArgsClassNames chrome_args_class_names = 1015;
optional ChromeScrollJankV3 chrome_scroll_jank_v3 = 1017;
optional ChromeHistogramSummaries chrome_histogram_summaries = 1018;
optional ChromeStudyHashes chrome_study_hashes = 1019;
}
26 changes: 26 additions & 0 deletions protos/perfetto/metrics/chrome/study_hashes.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

syntax = "proto2";

package perfetto.protos;

// The list of Chrome Study hashes in trace track events. Use cases include
// translating study hashes to study names by getting this list, and prepending
// a translation table to the trace.
message ChromeStudyHashes {
repeated int64 hash = 1;
}
6 changes: 6 additions & 0 deletions protos/perfetto/trace/perfetto_trace.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15557,6 +15557,7 @@ message TranslationTable {
ChromePerformanceMarkTranslationTable chrome_performance_mark = 3;
SliceNameTranslationTable slice_name = 4;
ProcessTrackNameTranslationTable process_track_name = 5;
ChromeStudyTranslationTable chrome_study = 6;
}
}

Expand Down Expand Up @@ -15586,6 +15587,11 @@ message ProcessTrackNameTranslationTable {
map<string, string> raw_to_deobfuscated_name = 1;
};

// Chrome study hash -> name translation rules.
message ChromeStudyTranslationTable {
map<uint64, string> hash_to_name = 1;
};

// End of protos/perfetto/trace/translation/translation_table.proto

// Begin of protos/perfetto/trace/trigger.proto
Expand Down
6 changes: 6 additions & 0 deletions protos/perfetto/trace/translation/translation_table.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ message TranslationTable {
ChromePerformanceMarkTranslationTable chrome_performance_mark = 3;
SliceNameTranslationTable slice_name = 4;
ProcessTrackNameTranslationTable process_track_name = 5;
ChromeStudyTranslationTable chrome_study = 6;
}
}

Expand Down Expand Up @@ -55,3 +56,8 @@ message SliceNameTranslationTable {
message ProcessTrackNameTranslationTable {
map<string, string> raw_to_deobfuscated_name = 1;
};

// Chrome study hash -> name translation rules.
message ChromeStudyTranslationTable {
map<uint64, string> hash_to_name = 1;
};
27 changes: 27 additions & 0 deletions src/trace_processor/importers/common/args_translation_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ ArgsTranslationTable::ArgsTranslationTable(TraceStorage* storage)
storage->InternString(kChromePerformanceMarkMarkHashKey)),
interned_chrome_performance_mark_mark_key_(
storage->InternString(kChromePerformanceMarkMarkKey)),
interned_chrome_trigger_hash_key_(
storage->InternString(kChromeTriggerHashKey)),
interned_chrome_trigger_name_key_(
storage->InternString(kChromeTriggerNameKey)),
interned_mojo_method_mapping_id_(
storage->InternString(kMojoMethodMappingIdKey)),
interned_mojo_method_rel_pc_(storage->InternString(kMojoMethodRelPcKey)),
Expand Down Expand Up @@ -156,6 +160,17 @@ void ArgsTranslationTable::TranslateArgs(
}
break;
}
case KeyType::kChromeTriggerHash: {
inserter.AddArg(interned_chrome_trigger_hash_key_, arg.value);
const std::optional<base::StringView> translated_value =
TranslateChromeStudyHash(arg.value.uint_value);
if (translated_value) {
inserter.AddArg(
interned_chrome_trigger_name_key_,
Variadic::String(storage_->InternString(*translated_value)));
}
break;
}
case KeyType::kMojoMethodMappingId: {
mapping_id = arg.value.uint_value;
break;
Expand Down Expand Up @@ -192,6 +207,9 @@ ArgsTranslationTable::KeyIdAndTypeToEnum(StringId flat_key_id,
if (key_id == interned_mojo_method_rel_pc_) {
return KeyType::kMojoMethodRelPc;
}
if (key_id == interned_chrome_trigger_hash_key_) {
return KeyType::kChromeTriggerHash;
}
} else if (type == Variadic::Type::kString) {
if (flat_key_id == interned_obfuscated_view_dump_class_name_flat_key_) {
return KeyType::kClassName;
Expand Down Expand Up @@ -238,6 +256,15 @@ ArgsTranslationTable::TranslateChromePerformanceMarkMarkHash(
return base::StringView(*value);
}

std::optional<base::StringView> ArgsTranslationTable::TranslateChromeStudyHash(
uint64_t hash) const {
auto* value = chrome_study_hash_to_name_.Find(hash);
if (!value) {
return std::nullopt;
}
return base::StringView(*value);
}

std::optional<ArgsTranslationTable::SourceLocation>
ArgsTranslationTable::TranslateNativeSymbol(MappingId mapping_id,
uint64_t rel_pc) const {
Expand Down
15 changes: 15 additions & 0 deletions src/trace_processor/importers/common/args_translation_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class ArgsTranslationTable {
base::StringView name) {
chrome_performance_mark_mark_hash_to_name_.Insert(hash, name.ToStdString());
}
void AddChromeStudyTranslationRule(uint64_t hash, base::StringView name) {
chrome_study_hash_to_name_.Insert(hash, name.ToStdString());
}
void AddNativeSymbolTranslationRule(MappingId mapping_id,
uint64_t rel_pc,
const SourceLocation& loc) {
Expand All @@ -102,6 +105,10 @@ class ArgsTranslationTable {
TranslateChromePerformanceMarkMarkHashForTesting(uint64_t hash) const {
return TranslateChromePerformanceMarkMarkHash(hash);
}
std::optional<base::StringView> TranslateChromeStudyHashForTesting(
uint64_t hash) const {
return TranslateChromeStudyHash(hash);
}
std::optional<StringId> TranslateClassNameForTesting(
StringId obfuscated_class_name_id) const {
return TranslateClassName(obfuscated_class_name_id);
Expand All @@ -116,6 +123,7 @@ class ArgsTranslationTable {
kMojoMethodMappingId = 4,
kMojoMethodRelPc = 5,
kClassName = 6,
kChromeTriggerHash = 7,
};

static constexpr char kChromeHistogramHashKey[] =
Expand All @@ -138,6 +146,9 @@ class ArgsTranslationTable {
static constexpr char kChromePerformanceMarkMarkKey[] =
"chrome_hashed_performance_mark.mark";

static constexpr char kChromeTriggerHashKey[] = "chrome_trigger.name_hash";
static constexpr char kChromeTriggerNameKey[] = "chrome_trigger.name";

static constexpr char kMojoMethodMappingIdKey[] =
"chrome_mojo_event_info.mojo_interface_method.native_symbol.mapping_id";
static constexpr char kMojoMethodRelPcKey[] =
Expand All @@ -159,6 +170,8 @@ class ArgsTranslationTable {
StringId interned_chrome_performance_mark_site_key_;
StringId interned_chrome_performance_mark_mark_hash_key_;
StringId interned_chrome_performance_mark_mark_key_;
StringId interned_chrome_trigger_hash_key_;
StringId interned_chrome_trigger_name_key_;

StringId interned_mojo_method_mapping_id_;
StringId interned_mojo_method_rel_pc_;
Expand All @@ -175,6 +188,7 @@ class ArgsTranslationTable {
chrome_performance_mark_site_hash_to_name_;
base::FlatHashMap<uint64_t, std::string>
chrome_performance_mark_mark_hash_to_name_;
base::FlatHashMap<uint64_t, std::string> chrome_study_hash_to_name_;
base::FlatHashMap<NativeSymbolKey, SourceLocation> native_symbol_to_location_;
// A translation mapping for obfuscated Java class names and its members.
DeobfuscationMappingTable deobfuscation_mapping_table_;
Expand All @@ -194,6 +208,7 @@ class ArgsTranslationTable {
uint64_t hash) const;
std::optional<base::StringView> TranslateChromePerformanceMarkMarkHash(
uint64_t hash) const;
std::optional<base::StringView> TranslateChromeStudyHash(uint64_t hash) const;
std::optional<SourceLocation> TranslateNativeSymbol(MappingId mapping_id,
uint64_t rel_pc) const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ TEST(ArgsTranslationTable, TranslatesPerformanceMarkMarkHashes) {
std::nullopt);
}

TEST(ArgsTranslationTable, TranslatesStudyHashes) {
TraceStorage storage;
ArgsTranslationTable table(&storage);
table.AddChromeStudyTranslationRule(1, "hash1");
table.AddChromeStudyTranslationRule(10, "hash2");
EXPECT_EQ(table.TranslateChromeStudyHashForTesting(1),
std::optional<base::StringView>("hash1"));
EXPECT_EQ(table.TranslateChromeStudyHashForTesting(10),
std::optional<base::StringView>("hash2"));
EXPECT_EQ(table.TranslateChromeStudyHashForTesting(2), std::nullopt);
}

TEST(ArgsTranslationTable, TranslateClassName) {
TraceStorage storage;
StringId xyz_id = storage.InternString("xyz");
Expand Down
22 changes: 17 additions & 5 deletions src/trace_processor/importers/proto/metadata_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ MetadataModule::MetadataModule(TraceProcessorContext* context)
: context_(context),
producer_name_key_id_(context_->storage->InternString("producer_name")),
trusted_producer_uid_key_id_(
context_->storage->InternString("trusted_producer_uid")) {
context_->storage->InternString("trusted_producer_uid")),
chrome_trigger_name_id_(
context_->storage->InternString("chrome_trigger.name")),
chrome_trigger_hash_id_(
context_->storage->InternString("chrome_trigger.name_hash")) {
RegisterForField(TracePacket::kUiStateFieldNumber, context);
RegisterForField(TracePacket::kTriggerFieldNumber, context);
RegisterForField(TracePacket::kChromeTriggerFieldNumber, context);
Expand Down Expand Up @@ -133,11 +137,19 @@ void MetadataModule::ParseChromeTrigger(int64_t ts, ConstBytes blob) {
if (trigger.has_trigger_name()) {
name_id = context_->storage->InternString(trigger.trigger_name());
} else {
name_id = context_->storage->InternString(
base::StringView(base::IntToHexString(trigger.trigger_name_hash())));
name_id =
context_->storage->InternString(base::StringView("chrome_trigger"));
}
context_->slice_tracker->Scoped(ts, track_id, cat_id, name_id,
/* duration = */ 0);
context_->slice_tracker->Scoped(
ts, track_id, cat_id, name_id,
/* duration = */ 0, [&](ArgsTracker::BoundInserter* inserter) {
inserter->AddArg(
chrome_trigger_hash_id_,
Variadic::UnsignedInteger(trigger.trigger_name_hash()));
if (trigger.has_trigger_name()) {
inserter->AddArg(chrome_trigger_name_id_, Variadic::String(name_id));
}
});

MetadataTracker* metadata = context_->metadata_tracker.get();
metadata->SetDynamicMetadata(
Expand Down
2 changes: 2 additions & 0 deletions src/trace_processor/importers/proto/metadata_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class MetadataModule : public ProtoImporterModule {

const StringId producer_name_key_id_;
const StringId trusted_producer_uid_key_id_;
const StringId chrome_trigger_name_id_;
const StringId chrome_trigger_hash_id_;
};

} // namespace trace_processor
Expand Down
14 changes: 14 additions & 0 deletions src/trace_processor/importers/proto/translation_table_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ ModuleResult TranslationTableModule::TokenizePacket(
ParseSliceNameRules(translation_table.slice_name());
} else if (translation_table.has_process_track_name()) {
ParseProcessTrackNameRules(translation_table.process_track_name());
} else if (translation_table.has_chrome_study()) {
ParseChromeStudyRules(translation_table.chrome_study());
}
return ModuleResult::Handled();
}
Expand Down Expand Up @@ -128,5 +130,17 @@ void TranslationTableModule::ParseProcessTrackNameRules(
}
}

void TranslationTableModule::ParseChromeStudyRules(
protozero::ConstBytes bytes) {
const auto chrome_study =
protos::pbzero::ChromeStudyTranslationTable::Decoder(bytes);
for (auto it = chrome_study.hash_to_name(); it; ++it) {
protos::pbzero::ChromeStudyTranslationTable::HashToNameEntry::Decoder entry(
*it);
context_->args_translation_table->AddChromeStudyTranslationRule(
entry.key(), entry.value());
}
}

} // namespace trace_processor
} // namespace perfetto
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class TranslationTableModule : public ProtoImporterModule {
void ParseChromePerformanceMarkRules(protozero::ConstBytes bytes);
void ParseSliceNameRules(protozero::ConstBytes bytes);
void ParseProcessTrackNameRules(protozero::ConstBytes bytes);
void ParseChromeStudyRules(protozero::ConstBytes bytes);

TraceProcessorContext* context_;
};
Expand Down
1 change: 1 addition & 0 deletions src/trace_processor/metrics/sql/chrome/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ perfetto_sql_source_set("chrome_sql") {
"chrome_scroll_jank_v3.sql",
"chrome_slice_names.sql",
"chrome_stack_samples_for_task.sql",
"chrome_study_hashes.sql",
"chrome_tasks.sql",
"chrome_tasks_delaying_input_processing.sql",
"chrome_tasks_delaying_input_processing_base.sql",
Expand Down
26 changes: 26 additions & 0 deletions src/trace_processor/metrics/sql/chrome/chrome_study_hashes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--
-- Copyright 2024 The Android Open Source Project
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

DROP VIEW IF EXISTS chrome_study_hashes_output;

CREATE PERFETTO VIEW chrome_study_hashes_output AS
SELECT ChromeStudyHashes(
'hash', (
SELECT RepeatedField(int_value)
FROM args
WHERE key = 'chrome_trigger.name_hash'
ORDER BY int_value
)
);
Loading

0 comments on commit 4e3c160

Please sign in to comment.