Skip to content

Commit

Permalink
tp: migrate thread tracks to new system
Browse files Browse the repository at this point in the history
Change-Id: I2766669e7216f868e7ba8a9bd71844c98217d4ee
  • Loading branch information
LalitMaganti committed Dec 16, 2024
1 parent 43595ee commit e31f77f
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 186 deletions.
5 changes: 2 additions & 3 deletions src/trace_processor/db/query_executor_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ namespace perfetto::trace_processor {
namespace {

using SliceTable = tables::SliceTable;
using ThreadTrackTable = tables::ThreadTrackTable;
using ExpectedFrameTimelineSliceTable = tables::ExpectedFrameTimelineSliceTable;
using RawTable = tables::RawTable;
using FtraceEventTable = tables::FtraceEventTable;
Expand Down Expand Up @@ -123,7 +122,7 @@ SliceTable::Row GetSliceTableRow(const std::string& string_row,
PERFETTO_CHECK(row_vec.size() >= 14);
row.ts = *base::StringToInt64(row_vec[2]);
row.dur = *base::StringToInt64(row_vec[3]);
row.track_id = ThreadTrackTable::Id(*base::StringToUInt32(row_vec[4]));
row.track_id = tables::TrackTable::Id(*base::StringToUInt32(row_vec[4]));
row.category = StripAndIntern(pool, row_vec[5]);
row.name = StripAndIntern(pool, row_vec[6]);
row.depth = *base::StringToUInt32(row_vec[7]);
Expand Down Expand Up @@ -174,7 +173,7 @@ struct ExpectedFrameTimelineTableForBenchmark {
ExpectedFrameTimelineSliceTable::Row row;
row.ts = *base::StringToInt64(row_vec[2]);
row.dur = *base::StringToInt64(row_vec[3]);
row.track_id = ThreadTrackTable::Id(*base::StringToUInt32(row_vec[4]));
row.track_id = tables::TrackTable::Id(*base::StringToUInt32(row_vec[4]));
row.depth = *base::StringToUInt32(row_vec[7]);
row.stack_id = *base::StringToInt32(row_vec[8]);
row.parent_stack_id = *base::StringToInt32(row_vec[9]);
Expand Down
57 changes: 24 additions & 33 deletions src/trace_processor/export_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <optional>
#include <sstream>
#include <string>
#include <string_view>
#include <tuple>
#include <unordered_map>
#include <utility>
Expand All @@ -38,10 +39,12 @@
#include "perfetto/base/status.h"
#include "perfetto/ext/base/string_splitter.h"
#include "perfetto/ext/base/string_utils.h"
#include "perfetto/ext/base/string_view.h"
#include "perfetto/public/compiler.h"
#include "perfetto/trace_processor/basic_types.h"
#include "src/trace_processor/containers/null_term_string_view.h"
#include "src/trace_processor/export_json.h"
#include "src/trace_processor/importers/common/tracks_common.h"
#include "src/trace_processor/storage/metadata.h"
#include "src/trace_processor/storage/stats.h"
#include "src/trace_processor/storage/trace_storage.h"
Expand Down Expand Up @@ -815,8 +818,6 @@ class JsonExporter {
!(*track_args)["is_root_in_scope"].asBool();
}

const auto& thread_track = storage_->thread_track_table();
const auto& process_track = storage_->process_track_table();
const auto& virtual_track_slices = storage_->virtual_track_slices();

int64_t duration_ns = it.dur();
Expand Down Expand Up @@ -848,11 +849,9 @@ class JsonExporter {
}
}

auto tt_rr = thread_track.FindById(track_id);
if (tt_rr && !is_child_track) {
if (track_row_ref.utid() && !is_child_track) {
// Synchronous (thread) slice or instant event.
UniqueTid utid = tt_rr->utid();
auto pid_and_tid = UtidToPidAndTid(utid);
auto pid_and_tid = UtidToPidAndTid(*track_row_ref.utid());
event["pid"] = Json::Int(pid_and_tid.first);
event["tid"] = Json::Int(pid_and_tid.second);

Expand Down Expand Up @@ -897,13 +896,10 @@ class JsonExporter {
} else if (is_child_track ||
(legacy_chrome_track && track_args->isMember("trace_id"))) {
// Async event slice.
auto pt_rr = process_track.FindById(track_id);
if (legacy_chrome_track) {
// Legacy async tracks are always process-associated and have args.
PERFETTO_DCHECK(pt_rr);
PERFETTO_DCHECK(track_args);
UniquePid upid = pt_rr->upid();
uint32_t exported_pid = UpidToPid(upid);
uint32_t exported_pid = UpidToPid(*track_row_ref.upid());
event["pid"] = Json::Int(exported_pid);
event["tid"] =
Json::Int(legacy_utid ? UtidToPidAndTid(*legacy_utid).second
Expand All @@ -914,7 +910,7 @@ class JsonExporter {
PERFETTO_DCHECK(track_args->isMember("trace_id"));
PERFETTO_DCHECK(track_args->isMember("trace_id_is_process_scoped"));
PERFETTO_DCHECK(track_args->isMember("source_scope"));
uint64_t trace_id =
auto trace_id =
static_cast<uint64_t>((*track_args)["trace_id"].asInt64());
std::string source_scope = (*track_args)["source_scope"].asString();
if (!source_scope.empty())
Expand All @@ -931,15 +927,13 @@ class JsonExporter {
event["id"] = base::Uint64ToHexString(trace_id);
}
} else {
if (tt_rr) {
UniqueTid utid = tt_rr->utid();
auto pid_and_tid = UtidToPidAndTid(utid);
if (track_row_ref.utid()) {
auto pid_and_tid = UtidToPidAndTid(*track_row_ref.utid());
event["pid"] = Json::Int(pid_and_tid.first);
event["tid"] = Json::Int(pid_and_tid.second);
event["id2"]["local"] = base::Uint64ToHexString(track_id.value);
} else if (pt_rr) {
uint32_t upid = pt_rr->upid();
uint32_t exported_pid = UpidToPid(upid);
} else if (track_row_ref.upid()) {
uint32_t exported_pid = UpidToPid(*track_row_ref.upid());
event["pid"] = Json::Int(exported_pid);
event["tid"] =
Json::Int(legacy_utid ? UtidToPidAndTid(*legacy_utid).second
Expand Down Expand Up @@ -1017,10 +1011,8 @@ class JsonExporter {
event["ph"] = legacy_phase;
}

auto pt_rr = process_track.FindById(track_id);
if (pt_rr.has_value()) {
UniquePid upid = pt_rr->upid();
uint32_t exported_pid = UpidToPid(upid);
if (track_row_ref.upid()) {
uint32_t exported_pid = UpidToPid(*track_row_ref.upid());
event["pid"] = Json::Int(exported_pid);
event["tid"] =
Json::Int(legacy_utid ? UtidToPidAndTid(*legacy_utid).second
Expand All @@ -1043,20 +1035,22 @@ class JsonExporter {
Json::Value args,
bool flow_begin) {
const auto& slices = storage_->slice_table();
const auto& thread_tracks = storage_->thread_track_table();

auto opt_slice_rr = slices.FindById(slice_id);
if (!opt_slice_rr)
return std::nullopt;
auto slice_rr = opt_slice_rr.value();

TrackId track_id = slice_rr.track_id();
auto opt_ttrr = thread_tracks.FindById(track_id);
auto rr = storage_->track_table().FindById(track_id);

// catapult only supports flow events attached to thread-track slices
if (!opt_ttrr)
if (!rr || !rr->utid()) {
return std::nullopt;
}

auto pid_and_tid = UtidToPidAndTid(opt_ttrr->utid());
UniqueTid utid = *rr->utid();
auto pid_and_tid = UtidToPidAndTid(utid);
Json::Value event;
event["id"] = flow_id;
event["pid"] = Json::Int(pid_and_tid.first);
Expand Down Expand Up @@ -1539,8 +1533,10 @@ class JsonExporter {
std::optional<StringId> peak_resident_set_id =
storage_->string_pool().GetId("chrome.peak_resident_set_kb");

std::optional<StringId> process_stats =
storage_->string_pool().GetId("chrome_process_stats");
std::string_view chrome_process_stats =
tracks::kChromeProcessStatsBlueprint.classification;
std::optional<StringId> process_stats = storage_->string_pool().GetId(
{chrome_process_stats.data(), chrome_process_stats.size()});

for (auto sit = memory_snapshots.IterateRows(); sit; ++sit) {
Json::Value event_base;
Expand All @@ -1565,15 +1561,10 @@ class JsonExporter {
Json::Value& totals = event["args"]["dumps"]["process_totals"];

for (auto it = track_table.IterateRows(); it; ++it) {
auto arg_set_id = it.dimension_arg_set_id();
if (!arg_set_id) {
continue;
}
if (it.classification() != process_stats) {
continue;
}
uint64_t upid = args_builder_.GetArgs(*arg_set_id)["upid"].asUInt64();
if (upid != pit.id().value) {
if (it.upid() != pit.id().value) {
continue;
}
TrackId track_id = it.id();
Expand Down
45 changes: 21 additions & 24 deletions src/trace_processor/importers/common/track_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ TrackId TrackTracker::CreateTrack(tracks::TrackClassification classification,
row.dimension_arg_set_id = dimensions->arg_set_id;
}
row.machine_id = context_->machine_id();
row.event_type = context_->storage->InternString("slice");

return context_->storage->mutable_track_table()->Insert(row).id;
}
Expand All @@ -92,25 +93,27 @@ TrackId TrackTracker::CreateProcessTrack(
row.classification =
context_->storage->InternString(tracks::ToString(classification));
row.machine_id = context_->machine_id();
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,
const TrackName& name) {
UniqueTid utid) {
Dimensions dims_id = SingleDimension(utid_id_, Variadic::Integer(utid));

tables::ThreadTrackTable::Row row(
StringIdFromTrackName(classification, name));
row.utid = 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_thread_track_table()->Insert(row).id;
return context_->storage->mutable_track_table()->Insert(row).id;
}

TrackId TrackTracker::InternProcessTrack(
Expand All @@ -129,17 +132,6 @@ TrackId TrackTracker::InternProcessTrack(
return track_id;
}

TrackId TrackTracker::InternThreadTrack(UniqueTid utid, const TrackName& name) {
Dimensions dims = SingleDimension(utid_id_, Variadic::Integer(utid));

auto* it = tracks_.Find({tracks::thread, dims});
if (it)
return *it;
TrackId track_id = CreateThreadTrack(tracks::thread, utid, name);
tracks_[{tracks::thread, dims}] = track_id;
return track_id;
}

TrackId TrackTracker::LegacyInternLegacyChromeAsyncTrack(
StringId raw_name,
uint32_t upid,
Expand Down Expand Up @@ -182,6 +174,7 @@ TrackId TrackTracker::LegacyInternLegacyChromeAsyncTrack(
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;
Expand Down Expand Up @@ -217,15 +210,23 @@ TrackId TrackTracker::AddTrack(const tracks::BlueprintBase& blueprint,
GlobalArgsTracker::CompactArg* d_args,
uint32_t d_size,
const SetArgsCallback& args) {
tables::TrackTable::Row row(name);
const auto* dims = blueprint.dimension_blueprints.data();
for (uint32_t i = 0; i < d_size; ++i) {
StringId key = context_->storage->InternString(
base::StringView(dims[i].name.data(), dims[i].name.size()));
base::StringView str(dims[i].name.data(), dims[i].name.size());
if (str == "cpu" && d_args[i].value.type == Variadic::kInt) {
context_->cpu_tracker->MarkCpuValid(
static_cast<uint32_t>(d_args[i].value.int_value));
} else if (str == "utid" && d_args[i].value.type == Variadic::kInt) {
row.utid = static_cast<uint32_t>(d_args[i].value.int_value);
} else if (str == "upid" && d_args[i].value.type == Variadic::kInt) {
row.upid = static_cast<uint32_t>(d_args[i].value.int_value);
}
StringId key = context_->storage->InternString(str);
d_args[i].key = key;
d_args[i].flat_key = key;
}

tables::TrackTable::Row row(name);
row.machine_id = context_->machine_id();
row.classification = context_->storage->InternString(base::StringView(
blueprint.classification.data(), blueprint.classification.size()));
Expand All @@ -244,8 +245,4 @@ TrackId TrackTracker::AddTrack(const tracks::BlueprintBase& blueprint,
return id;
}

void TrackTracker::MarkCpuValid(uint32_t cpu) {
context_->cpu_tracker->MarkCpuValid(cpu);
}

} // namespace perfetto::trace_processor
29 changes: 18 additions & 11 deletions src/trace_processor/importers/common/track_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "src/trace_processor/importers/common/args_tracker.h"
#include "src/trace_processor/importers/common/global_args_tracker.h"
#include "src/trace_processor/importers/common/tracks.h"
#include "src/trace_processor/importers/common/tracks_common.h"
#include "src/trace_processor/importers/common/tracks_internal.h"
#include "src/trace_processor/storage/trace_storage.h"
#include "src/trace_processor/tables/track_tables_py.h"
Expand Down Expand Up @@ -143,6 +144,20 @@ class TrackTracker {
return *it;
}

// Wrapper function for `InternTrack` in cases where you want the "main"
// slice track for the thread.
//
// This function should be used in situations where the thread cannot be
// executing anything else while the slice is active. It should *not* be used
// in cases where the function could overlap; use InternTrack directly with a
// custom blueprint.
TrackId InternThreadTrack(UniqueTid utid) {
static constexpr auto kBlueprint = tracks::SliceBlueprint(
"thread_execution",
tracks::DimensionBlueprints(tracks::kThreadDimensionBlueprint));
return InternTrack(kBlueprint, tracks::Dimensions(utid));
}

// ********WARNING************
// EVERYTHING BELOW THIS POINT IS LEGACY AND SHOULD BE REMOVED WITH TIME.
// ********WARNING************
Expand Down Expand Up @@ -221,9 +236,6 @@ class TrackTracker {
return DimensionsBuilder(this);
}

// Interns a thread track into the storage.
TrackId InternThreadTrack(UniqueTid, const TrackName& = AutoName());

// Interns a process track into the storage.
TrackId InternProcessTrack(tracks::TrackClassification,
UniquePid,
Expand Down Expand Up @@ -265,9 +277,7 @@ class TrackTracker {
std::optional<Dimensions>,
const TrackName&);

TrackId CreateThreadTrack(tracks::TrackClassification,
UniqueTid,
const TrackName&);
TrackId CreateThreadTrack(tracks::TrackClassification, UniqueTid);

TrackId CreateProcessTrack(tracks::TrackClassification,
UniquePid,
Expand All @@ -292,9 +302,6 @@ class TrackTracker {
if constexpr (i < kTupleSize) {
using elem_t = std::tuple_element_t<i, TupleDimensions>;
if constexpr (std::is_same_v<elem_t, uint32_t>) {
if (dimensions_schema[i].is_cpu) {
MarkCpuValid(std::get<i>(dimensions));
}
a[i].value = Variadic::Integer(std::get<i>(dimensions));
} else if constexpr (std::is_integral_v<elem_t>) {
a[i].value = Variadic::Integer(std::get<i>(dimensions));
Expand All @@ -306,10 +313,10 @@ class TrackTracker {
}
DimensionsToArgs<i + 1>(dimensions, dimensions_schema, a);
}
// Required for GCC to not complain.
base::ignore_result(dimensions_schema);
}

void MarkCpuValid(uint32_t cpu);

Dimensions SingleDimension(StringId key, const Variadic& val) {
std::array args{GlobalArgsTracker::CompactArg{key, key, val}};
return Dimensions{
Expand Down
6 changes: 3 additions & 3 deletions src/trace_processor/importers/common/tracks.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ constexpr auto DimensionBlueprints(DimensionBlueprint... dimensions) {

// Adds a unit32_t dimension with the given name.
constexpr auto UintDimensionBlueprint(const char name[]) {
return DimensionBlueprintT<uint32_t>{{name, std::string_view(name) == "cpu"}};
return DimensionBlueprintT<uint32_t>{{name}};
}

// Adds a string dimension with the given name.
constexpr auto StringDimensionBlueprint(const char name[]) {
return DimensionBlueprintT<base::StringView>{{name, false}};
return DimensionBlueprintT<base::StringView>{{name}};
}

// Adds a int64_t dimension with the given name.
constexpr auto LongDimensionBlueprint(const char name[]) {
return DimensionBlueprintT<int64_t>{{name, false}};
return DimensionBlueprintT<int64_t>{{name}};
}

// Indicates the name should be automatically determined by trace processor.
Expand Down
1 change: 0 additions & 1 deletion src/trace_processor/importers/common/tracks_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ using DimensionsT = std::tuple<T...>;

struct DimensionBlueprintBase {
std::string_view name;
bool is_cpu;
};

template <typename T>
Expand Down
Loading

0 comments on commit e31f77f

Please sign in to comment.