Skip to content

Commit

Permalink
tp: migrate process tracks to new track interning system
Browse files Browse the repository at this point in the history
Change-Id: If22933c150bfea44056194a76617da895821900a
  • Loading branch information
LalitMaganti committed Dec 16, 2024
1 parent e31f77f commit 3384192
Show file tree
Hide file tree
Showing 20 changed files with 189 additions and 178 deletions.
19 changes: 15 additions & 4 deletions src/trace_processor/export_json_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,11 @@ TEST_F(ExportJsonTest, StorageWithThreadName) {
}

TEST_F(ExportJsonTest, SystemEventsIgnored) {
static constexpr auto kBlueprint = tracks::SliceBlueprint(
"unknown",
tracks::DimensionBlueprints(tracks::kProcessDimensionBlueprint));
TrackId track =
context_.track_tracker->InternProcessTrack(tracks::unknown, 0);
context_.track_tracker->InternTrack(kBlueprint, tracks::Dimensions(0));
context_.args_tracker->Flush(); // Flush track args.

// System events have no category.
Expand Down Expand Up @@ -1814,9 +1817,13 @@ TEST_F(ExportJsonTest, MemorySnapshotOsDumpEvent) {
const char* kModuleDebugid = "debugid";
const char* kModuleDebugPath = "debugpath";

static constexpr auto kBlueprint = tracks::SliceBlueprint(
"track_event",
tracks::DimensionBlueprints(tracks::kProcessDimensionBlueprint));

UniquePid upid = context_.process_tracker->GetOrCreateProcess(kProcessID);
TrackId track =
context_.track_tracker->InternProcessTrack(tracks::track_event, upid);
context_.track_tracker->InternTrack(kBlueprint, tracks::Dimensions(upid));
StringId level_of_detail_id =
context_.storage->InternString(base::StringView(kLevelOfDetail));
auto snapshot_id = context_.storage->mutable_memory_snapshot_table()
Expand Down Expand Up @@ -1931,10 +1938,14 @@ TEST_F(ExportJsonTest, MemorySnapshotChromeDumpEvent) {
const std::string kScalarAttrName = "scalar_name";
const std::string kStringAttrName = "string_name";

static constexpr auto kBlueprint = tracks::SliceBlueprint(
"track_event",
tracks::DimensionBlueprints(tracks::kProcessDimensionBlueprint));

UniquePid os_upid =
context_.process_tracker->GetOrCreateProcess(kOsProcessID);
TrackId track =
context_.track_tracker->InternProcessTrack(tracks::track_event, os_upid);
TrackId track = context_.track_tracker->InternTrack(
kBlueprint, tracks::Dimensions(os_upid));
StringId level_of_detail_id =
context_.storage->InternString(base::StringView(kLevelOfDetail));
auto snapshot_id = context_.storage->mutable_memory_snapshot_table()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ TEST_F(AsyncTrackSetTrackerUnittest, Smoke) {

ASSERT_EQ(begin, end);

const auto& process = storage_->process_track_table();
const auto& process = storage_->track_table();
auto rr = *process.FindById(begin);
ASSERT_EQ(rr.upid(), 1u);
ASSERT_EQ(rr.name(), storage_->string_pool().GetId("test"));
Expand All @@ -78,7 +78,7 @@ TEST_F(AsyncTrackSetTrackerUnittest, Smoke) {
TEST_F(AsyncTrackSetTrackerUnittest, EndFirst) {
auto end = tracker_->End(nestable_id_, 1);

const auto& process = storage_->process_track_table();
const auto& process = storage_->track_table();
auto rr = *process.FindById(end);
ASSERT_EQ(rr.upid(), 1u);
ASSERT_EQ(rr.name(), storage_->string_pool().GetId("test"));
Expand Down
41 changes: 3 additions & 38 deletions src/trace_processor/importers/common/track_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ TrackId TrackTracker::CreateProcessTrack(
Dimensions dims_id =
dims ? *dims : SingleDimension(upid_id_, Variadic::Integer(upid));

tables::ProcessTrackTable::Row row(
StringIdFromTrackName(classification, name));
tables::TrackTable::Row row(StringIdFromTrackName(classification, name));
row.upid = upid;
row.dimension_arg_set_id = dims_id.arg_set_id;
row.classification =
Expand All @@ -96,42 +95,9 @@ TrackId TrackTracker::CreateProcessTrack(
row.event_type = context_->storage->InternString("slice");
row.upid = upid;

return context_->storage->mutable_process_track_table()->Insert(row).id;
}

TrackId TrackTracker::CreateThreadTrack(
tracks::TrackClassification classification,
UniqueTid utid) {
Dimensions dims_id = SingleDimension(utid_id_, Variadic::Integer(utid));

tables::TrackTable::Row row(
StringIdFromTrackName(classification, AutoName()));
row.classification =
context_->storage->InternString(tracks::ToString(classification));
row.dimension_arg_set_id = dims_id.arg_set_id;
row.machine_id = context_->machine_id();
row.event_type = context_->storage->InternString("slice");
row.utid = utid;

return context_->storage->mutable_track_table()->Insert(row).id;
}

TrackId TrackTracker::InternProcessTrack(
tracks::TrackClassification classification,
UniquePid upid,
const TrackName& name) {
Dimensions dims_id = SingleDimension(upid_id_, Variadic::Integer(upid));

auto* it = tracks_.Find({classification, dims_id});
if (it)
return *it;

TrackId track_id =
CreateProcessTrack(classification, upid, std::nullopt, name);
tracks_[{classification, dims_id}] = track_id;
return track_id;
}

TrackId TrackTracker::LegacyInternLegacyChromeAsyncTrack(
StringId raw_name,
uint32_t upid,
Expand Down Expand Up @@ -168,16 +134,15 @@ TrackId TrackTracker::LegacyInternLegacyChromeAsyncTrack(

// Legacy async tracks are always drawn in the context of a process, even if
// the ID's scope is global.
tables::ProcessTrackTable::Row track(name);
tables::TrackTable::Row track(name);
track.upid = upid;
track.classification =
context_->storage->InternString(tracks::ToString(tracks::unknown));
track.dimension_arg_set_id = key.dimensions->arg_set_id;
track.machine_id = context_->machine_id();
track.event_type = context_->storage->InternString("slice");

TrackId id =
context_->storage->mutable_process_track_table()->Insert(track).id;
TrackId id = context_->storage->mutable_track_table()->Insert(track).id;
tracks_[key] = id;

args_tracker_.AddArgsTo(id)
Expand Down
7 changes: 0 additions & 7 deletions src/trace_processor/importers/common/track_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,6 @@ class TrackTracker {
return DimensionsBuilder(this);
}

// Interns a process track into the storage.
TrackId InternProcessTrack(tracks::TrackClassification,
UniquePid,
const TrackName& = AutoName());

// Everything below this point are legacy functions and should no longer be
// used.

Expand Down Expand Up @@ -277,8 +272,6 @@ class TrackTracker {
std::optional<Dimensions>,
const TrackName&);

TrackId CreateThreadTrack(tracks::TrackClassification, UniqueTid);

TrackId CreateProcessTrack(tracks::TrackClassification,
UniquePid,
std::optional<Dimensions>,
Expand Down
1 change: 0 additions & 1 deletion src/trace_processor/importers/common/tracks.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <array>
#include <cstddef>
#include <cstdint>
#include <string_view>
#include <tuple>

#include "perfetto/ext/base/hash.h"
Expand Down
9 changes: 9 additions & 0 deletions src/trace_processor/importers/common/tracks_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ static constexpr auto kAndroidDeviceStateBlueprint =
tracks::DimensionBlueprints(),
tracks::StaticNameBlueprint("DeviceStateChanged"));

static constexpr auto kAndroidLmkBlueprint = tracks::SliceBlueprint(
"android_lmk",
tracks::DimensionBlueprints(tracks::kProcessDimensionBlueprint),
tracks::StaticNameBlueprint("LMK"));

static constexpr auto kChromeProcessInstantBlueprint = tracks::SliceBlueprint(
"chrome_process_instant",
tracks::Dimensions(tracks::kProcessDimensionBlueprint));

// End slice blueprints.

// Begin counter blueprints.
Expand Down
13 changes: 8 additions & 5 deletions src/trace_processor/importers/json/json_trace_parser_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,14 @@ void JsonTraceParserImpl::ParseJsonPacket(int64_t timestamp,
break;
}
UniquePid upid = context_->process_tracker->GetOrCreateProcess(pid);
track_id = context_->track_tracker->InternProcessTrack(
tracks::chrome_process_instant, upid);
context_->args_tracker->AddArgsTo(track_id).AddArg(
context_->storage->InternString("source"),
Variadic::String(context_->storage->InternString("chrome")));
track_id = context_->track_tracker->InternTrack(
tracks::kChromeProcessInstantBlueprint, tracks::Dimensions(upid),
tracks::BlueprintName(),
[this](ArgsTracker::BoundInserter& inserter) {
inserter.AddArg(
context_->storage->InternString("source"),
Variadic::String(context_->storage->InternString("chrome")));
});
} else if (scope == "t" || scope.data() == nullptr) {
if (!opt_tid) {
context_->storage->IncrementStats(stats::json_parser_failure);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1448,10 +1448,9 @@ TEST_F(ProtoTraceParserTest, TrackEventAsyncEvents) {
EXPECT_EQ(storage_->track_table()[4].name(), ev_2);
EXPECT_EQ(storage_->track_table()[5].name(), ev_2);

EXPECT_EQ(storage_->process_track_table().row_count(), 3u);
EXPECT_EQ(storage_->process_track_table()[0].upid(), 1u);
EXPECT_EQ(storage_->process_track_table()[1].upid(), 1u);
EXPECT_EQ(storage_->process_track_table()[2].upid(), 1u);
EXPECT_EQ(storage_->track_table()[1].upid(), 1u);
EXPECT_EQ(storage_->track_table()[4].upid(), 1u);
EXPECT_EQ(storage_->track_table()[5].upid(), 1u);

EXPECT_EQ(storage_->virtual_track_slices().slice_count(), 1u);
EXPECT_EQ(storage_->virtual_track_slices().slice_ids()[0], SliceId(0u));
Expand Down
59 changes: 29 additions & 30 deletions src/trace_processor/importers/proto/track_event_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -390,37 +390,32 @@ class TrackEventParser::EventImporter {
}
track_id_ = *opt_track_id;

auto rr = storage_->track_table().FindById(track_id_);
auto rr = storage_->mutable_track_table()->FindById(track_id_);
if (rr && rr->utid()) {
utid_ = rr->utid();
upid_ = storage_->thread_table()[*utid_].upid();
} else {
auto pt_rr = storage_->process_track_table().FindById(track_id_);
if (pt_rr) {
upid_ = pt_rr->upid();
if (sequence_state_->pid_and_tid_valid()) {
auto pid = static_cast<uint32_t>(sequence_state_->pid());
auto tid = static_cast<uint32_t>(sequence_state_->tid());
UniqueTid utid_candidate = procs->UpdateThread(tid, pid);
if (storage_->thread_table()[utid_candidate].upid() == upid_) {
legacy_passthrough_utid_ = utid_candidate;
}
}
} else {
auto* tracks = context_->storage->mutable_track_table();
auto t_rr = tracks->FindById(track_id_);
if (t_rr) {
StringPool::Id id = t_rr->name();
if (id.is_null()) {
t_rr->set_name(name_id_);
}
} else if (rr && rr->upid()) {
upid_ = rr->upid();
if (sequence_state_->pid_and_tid_valid()) {
auto pid = static_cast<uint32_t>(sequence_state_->pid());
auto tid = static_cast<uint32_t>(sequence_state_->tid());
UniqueTid utid_candidate = procs->UpdateThread(tid, pid);
if (storage_->thread_table()[utid_candidate].upid() == upid_) {
legacy_passthrough_utid_ = utid_candidate;
}
if (sequence_state_->pid_and_tid_valid()) {
auto pid = static_cast<uint32_t>(sequence_state_->pid());
auto tid = static_cast<uint32_t>(sequence_state_->tid());
legacy_passthrough_utid_ = procs->UpdateThread(tid, pid);
}
} else {
if (rr) {
StringPool::Id id = rr->name();
if (id.is_null()) {
rr->set_name(name_id_);
}
}
if (sequence_state_->pid_and_tid_valid()) {
auto pid = static_cast<uint32_t>(sequence_state_->pid());
auto tid = static_cast<uint32_t>(sequence_state_->tid());
legacy_passthrough_utid_ = procs->UpdateThread(tid, pid);
}
}
} else {
bool pid_tid_state_valid = sequence_state_->pid_and_tid_valid();
Expand Down Expand Up @@ -543,11 +538,15 @@ class TrackEventParser::EventImporter {
"Process-scoped instant event without process association");
}

track_id_ = context_->track_tracker->InternProcessTrack(
tracks::chrome_process_instant, *upid_);
context_->args_tracker->AddArgsTo(track_id_).AddArg(
context_->storage->InternString("source"),
Variadic::String(context_->storage->InternString("chrome")));
track_id_ = context_->track_tracker->InternTrack(
tracks::kChromeProcessInstantBlueprint,
tracks::Dimensions(*upid_), tracks::BlueprintName(),
[this](ArgsTracker::BoundInserter& inserter) {
inserter.AddArg(
context_->storage->InternString("source"),
Variadic::String(
context_->storage->InternString("chrome")));
});
legacy_passthrough_utid_ = utid_;
utid_ = std::nullopt;
break;
Expand Down
50 changes: 38 additions & 12 deletions src/trace_processor/importers/proto/track_event_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ constexpr auto kGlobalCounterTrackBlueprint = tracks::CounterBlueprint(
tracks::DimensionBlueprints(tracks::LongDimensionBlueprint("track_uuid")),
tracks::DynamicNameBlueprint());

constexpr auto kThreadTrackBlueprint = tracks::SliceBlueprint(
"thread_track_event",
tracks::DimensionBlueprints(tracks::kThreadDimensionBlueprint,
tracks::LongDimensionBlueprint("track_uuid")),
tracks::DynamicNameBlueprint());

constexpr auto kProcessTrackBlueprint = tracks::SliceBlueprint(
"process_track_event",
tracks::DimensionBlueprints(tracks::kProcessDimensionBlueprint,
tracks::LongDimensionBlueprint("track_uuid")),
tracks::DynamicNameBlueprint());

constexpr auto kGlobalTrackBlueprint = tracks::SliceBlueprint(
"global_track_event",
tracks::DimensionBlueprints(tracks::LongDimensionBlueprint("track_uuid")),
tracks::DynamicNameBlueprint());

} // namespace

TrackEventTracker::TrackEventTracker(TraceProcessorContext* context)
Expand Down Expand Up @@ -195,16 +212,21 @@ TrackId TrackEventTracker::CreateTrackFromResolved(
if (it != thread_tracks_.end()) {
return it->second;
}
TrackId id = context_->track_tracker->CreateThreadTrack(
tracks::track_event, track.utid());
TrackId id = context_->track_tracker->InternTrack(
kThreadTrackBlueprint,
tracks::Dimensions(track.utid(), static_cast<int64_t>(uuid)),
tracks::DynamicName(kNullStringId));
thread_tracks_[track.utid()] = id;
return id;
}
return context_->track_tracker->InternThreadTrack(track.utid());
}
case ResolvedDescriptorTrack::Scope::kProcess:
return context_->track_tracker->InternProcessTrack(tracks::track_event,
track.upid());
case ResolvedDescriptorTrack::Scope::kProcess: {
return context_->track_tracker->InternTrack(
kProcessTrackBlueprint,
tracks::Dimensions(track.upid(), static_cast<int64_t>(uuid)),
tracks::DynamicName(kNullStringId));
}
case ResolvedDescriptorTrack::Scope::kGlobal:
// Will be handled below.
break;
Expand Down Expand Up @@ -248,17 +270,21 @@ TrackId TrackEventTracker::CreateTrackFromResolved(

switch (track.scope()) {
case ResolvedDescriptorTrack::Scope::kThread: {
return context_->track_tracker->CreateThreadTrack(tracks::track_event,
track.utid());
return context_->track_tracker->InternTrack(
kThreadTrackBlueprint,
tracks::Dimensions(track.utid(), static_cast<int64_t>(uuid)),
tracks::DynamicName(kNullStringId));
}
case ResolvedDescriptorTrack::Scope::kProcess: {
return context_->track_tracker->CreateProcessTrack(
tracks::track_event, track.upid(), std::nullopt,
TrackTracker::AutoName());
return context_->track_tracker->InternTrack(
kProcessTrackBlueprint,
tracks::Dimensions(track.upid(), static_cast<int64_t>(uuid)),
tracks::DynamicName(kNullStringId));
}
case ResolvedDescriptorTrack::Scope::kGlobal: {
return context_->track_tracker->CreateTrack(
tracks::track_event, std::nullopt, TrackTracker::AutoName());
return context_->track_tracker->InternTrack(
kGlobalTrackBlueprint, tracks::Dimensions(static_cast<int64_t>(uuid)),
tracks::DynamicName(kNullStringId));
}
}
PERFETTO_FATAL("For GCC");
Expand Down
8 changes: 4 additions & 4 deletions src/trace_processor/importers/systrace/systrace_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ void SystraceParser::ParseSystracePoint(
if (killed_pid != 0) {
UniquePid killed_upid =
context_->process_tracker->GetOrCreateProcess(killed_pid);
TrackId track = context_->track_tracker->InternProcessTrack(
tracks::android_lmk, killed_upid);
TrackId track = context_->track_tracker->InternTrack(
tracks::kAndroidLmkBlueprint, tracks::Dimensions(killed_upid));
context_->slice_tracker->Scoped(ts, track, kNullStringId, lmk_id_, 0);
}
// TODO(lalitm): we should not add LMK events to the counters table
Expand Down Expand Up @@ -367,8 +367,8 @@ void SystraceParser::PostProcessSpecialSliceBegin(int64_t ts,
context_->event_tracker->PushCounter(ts, *oom_score_adj, counter_track);

// Add mem.lmk instant event for consistency with other methods.
TrackId track = context_->track_tracker->InternProcessTrack(
tracks::android_lmk, killed_upid);
TrackId track = context_->track_tracker->InternTrack(
tracks::kAndroidLmkBlueprint, tracks::Dimensions(killed_upid));
context_->slice_tracker->Scoped(ts, track, kNullStringId, lmk_id_, 0);
}
}
Expand Down
Loading

0 comments on commit 3384192

Please sign in to comment.