Skip to content

Instantly share code, notes, and snippets.

@benjaminRomano
Created February 9, 2023 21:37
Show Gist options
  • Save benjaminRomano/d92c7d2d85848872e1df2dd27e542fbd to your computer and use it in GitHub Desktop.
Save benjaminRomano/d92c7d2d85848872e1df2dd27e542fbd to your computer and use it in GitHub Desktop.
Support custom tracks by using colon-based categorization. Replace `<package name>` with relevant package name
diff --git a/src/trace_processor/importers/common/process_tracker.cc b/src/trace_processor/importers/common/process_tracker.cc
index df14d2cdf..767d225df 100644
--- a/src/trace_processor/importers/common/process_tracker.cc
+++ b/src/trace_processor/importers/common/process_tracker.cc
@@ -348,6 +348,11 @@ void ProcessTracker::SetProcessNameIfUnset(UniquePid upid,
process_table->mutable_name()->Set(upid, process_name_id);
}
+StringId ProcessTracker::GetProcessName(UniquePid upid) {
+ auto* process_table = context_->storage->mutable_process_table();
+ return process_table->name()[upid];
+}
+
void ProcessTracker::SetStartTsIfUnset(UniquePid upid,
int64_t start_ts_nanoseconds) {
auto* process_table = context_->storage->mutable_process_table();
diff --git a/src/trace_processor/importers/common/process_tracker.h b/src/trace_processor/importers/common/process_tracker.h
index 37298a6ab..d7491860a 100644
--- a/src/trace_processor/importers/common/process_tracker.h
+++ b/src/trace_processor/importers/common/process_tracker.h
@@ -133,6 +133,9 @@ class ProcessTracker {
// have a name yet.
virtual void SetProcessNameIfUnset(UniquePid upid, StringId process_name_id);
+ // Returns the process name for a given upid
+ StringId GetProcessName(UniquePid upid);
+
// Sets the start timestamp to the process identified by |upid| if it doesn't
// have a timestamp yet.
void SetStartTsIfUnset(UniquePid upid, int64_t start_ts_nanoseconds);
diff --git a/src/trace_processor/importers/systrace/systrace_parser.cc b/src/trace_processor/importers/systrace/systrace_parser.cc
index 538278d95..6e08fe619 100644
--- a/src/trace_processor/importers/systrace/systrace_parser.cc
+++ b/src/trace_processor/importers/systrace/systrace_parser.cc
@@ -170,13 +170,46 @@ void SystraceParser::ParseSystracePoint(
case 'S':
case 'F': {
StringId name_id = context_->storage->InternString(point.name);
+
int64_t cookie = point.int_value;
UniquePid upid =
context_->process_tracker->GetOrCreateProcess(point.tgid);
- auto track_set_id =
- context_->async_track_set_tracker
- ->InternAndroidLegacyUnnestableTrackSet(upid, name_id);
+ AsyncTrackSetTracker::TrackSetId track_set_id;
+ StringId cat_id;
+
+ // The process name determines whether categorization should be applied
+ StringId process_name_id = context_->process_tracker->GetProcessName(upid);
+ std::string process_name_string = context_->storage->string_pool().Get(process_name_id).c_str();
+
+ // NOTE: Replace this with your package name. This ensure you don't modify behavior of other apps.
+ std::string::size_type snap_process_pos = process_name_string.find_last_of("<package name>");
+ if (snap_process_pos != std::string::npos) {
+
+ // This is snap process, apply categorization of tracks
+ std::string name_string = context_->storage->string_pool().Get(name_id).c_str();
+
+ // Category string is calculated with span name, from start till latest ':`
+ std::string cat_string;
+ std::string::size_type pos = name_string.find_last_of(":");
+ if (pos != std::string::npos) {
+ cat_string = name_string.substr(0, pos);
+ } else {
+ cat_string = name_string;
+ }
+
+ base::StringView cat_string_view(cat_string.c_str());
+ cat_id = context_->storage->InternString(cat_string_view);
+
+ } else {
+
+ // This is not snap process, do not apply categorization of tracks
+ cat_id = name_id;
+ }
+
+ // Track set id determines if this span should be in a group or not
+ track_set_id = context_->async_track_set_tracker
+ ->InternAndroidLegacyUnnestableTrackSet(upid, cat_id);
if (point.phase == 'S') {
// Historically, async slices on Android did not support nesting async
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment