Skip to content

Commit

Permalink
Merge changes Iedcf5f5f,I8de3fd23 into main
Browse files Browse the repository at this point in the history
* changes:
  [Reland] tp: add support for importing legacy v8 samples from JSON
  tp: simplify approach for including JSON parser
  • Loading branch information
Treehugger Robot authored and Gerrit Code Review committed Oct 15, 2024
2 parents 8ed3570 + 6699e0a commit 4d0d3ab
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 102 deletions.
13 changes: 0 additions & 13 deletions Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -2491,7 +2491,6 @@ cc_test {
":perfetto_src_trace_processor_importers_i2c_full",
":perfetto_src_trace_processor_importers_instruments_instruments",
":perfetto_src_trace_processor_importers_instruments_row",
":perfetto_src_trace_processor_importers_json_full",
":perfetto_src_trace_processor_importers_json_minimal",
":perfetto_src_trace_processor_importers_memory_tracker_graph_processor",
":perfetto_src_trace_processor_importers_ninja_ninja",
Expand Down Expand Up @@ -12619,15 +12618,6 @@ filegroup {
name: "perfetto_src_trace_processor_importers_instruments_row",
}

// GN: //src/trace_processor/importers/json:full
filegroup {
name: "perfetto_src_trace_processor_importers_json_full",
srcs: [
"src/trace_processor/importers/json/json_trace_parser_impl.cc",
"src/trace_processor/importers/json/json_trace_tokenizer.cc",
],
}

// GN: //src/trace_processor/importers/json:minimal
filegroup {
name: "perfetto_src_trace_processor_importers_json_minimal",
Expand Down Expand Up @@ -15642,7 +15632,6 @@ cc_test {
":perfetto_src_trace_processor_importers_i2c_full",
":perfetto_src_trace_processor_importers_instruments_instruments",
":perfetto_src_trace_processor_importers_instruments_row",
":perfetto_src_trace_processor_importers_json_full",
":perfetto_src_trace_processor_importers_json_minimal",
":perfetto_src_trace_processor_importers_memory_tracker_graph_processor",
":perfetto_src_trace_processor_importers_memory_tracker_unittests",
Expand Down Expand Up @@ -16715,7 +16704,6 @@ cc_binary {
":perfetto_src_trace_processor_importers_i2c_full",
":perfetto_src_trace_processor_importers_instruments_instruments",
":perfetto_src_trace_processor_importers_instruments_row",
":perfetto_src_trace_processor_importers_json_full",
":perfetto_src_trace_processor_importers_json_minimal",
":perfetto_src_trace_processor_importers_memory_tracker_graph_processor",
":perfetto_src_trace_processor_importers_ninja_ninja",
Expand Down Expand Up @@ -17142,7 +17130,6 @@ cc_binary_host {
":perfetto_src_trace_processor_importers_i2c_full",
":perfetto_src_trace_processor_importers_instruments_instruments",
":perfetto_src_trace_processor_importers_instruments_row",
":perfetto_src_trace_processor_importers_json_full",
":perfetto_src_trace_processor_importers_json_minimal",
":perfetto_src_trace_processor_importers_memory_tracker_graph_processor",
":perfetto_src_trace_processor_importers_ninja_ninja",
Expand Down
12 changes: 6 additions & 6 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ perfetto_cc_library(
":src_trace_processor_importers_i2c_full",
":src_trace_processor_importers_instruments_instruments",
":src_trace_processor_importers_instruments_row",
":src_trace_processor_importers_json_full",
":src_trace_processor_importers_json_json",
":src_trace_processor_importers_json_minimal",
":src_trace_processor_importers_memory_tracker_graph_processor",
":src_trace_processor_importers_ninja_ninja",
Expand Down Expand Up @@ -1810,9 +1810,9 @@ perfetto_filegroup(
],
)

# GN target: //src/trace_processor/importers/json:full
# GN target: //src/trace_processor/importers/json:json
perfetto_filegroup(
name = "src_trace_processor_importers_json_full",
name = "src_trace_processor_importers_json_json",
srcs = [
"src/trace_processor/importers/json/json_trace_parser_impl.cc",
"src/trace_processor/importers/json/json_trace_parser_impl.h",
Expand Down Expand Up @@ -6438,7 +6438,7 @@ perfetto_cc_library(
":src_trace_processor_importers_i2c_full",
":src_trace_processor_importers_instruments_instruments",
":src_trace_processor_importers_instruments_row",
":src_trace_processor_importers_json_full",
":src_trace_processor_importers_json_json",
":src_trace_processor_importers_json_minimal",
":src_trace_processor_importers_memory_tracker_graph_processor",
":src_trace_processor_importers_ninja_ninja",
Expand Down Expand Up @@ -6642,7 +6642,7 @@ perfetto_cc_binary(
":src_trace_processor_importers_i2c_full",
":src_trace_processor_importers_instruments_instruments",
":src_trace_processor_importers_instruments_row",
":src_trace_processor_importers_json_full",
":src_trace_processor_importers_json_json",
":src_trace_processor_importers_json_minimal",
":src_trace_processor_importers_memory_tracker_graph_processor",
":src_trace_processor_importers_ninja_ninja",
Expand Down Expand Up @@ -6903,7 +6903,7 @@ perfetto_cc_binary(
":src_trace_processor_importers_i2c_full",
":src_trace_processor_importers_instruments_instruments",
":src_trace_processor_importers_instruments_row",
":src_trace_processor_importers_json_full",
":src_trace_processor_importers_json_json",
":src_trace_processor_importers_json_minimal",
":src_trace_processor_importers_memory_tracker_graph_processor",
":src_trace_processor_importers_ninja_ninja",
Expand Down
6 changes: 4 additions & 2 deletions src/trace_processor/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ if (enable_perfetto_trace_processor_sqlite) {
"importers/etw:full",
"importers/ftrace:full",
"importers/fuchsia:full",
"importers/json:full",
"importers/json:minimal",
"importers/ninja",
"importers/perf",
Expand Down Expand Up @@ -209,7 +208,10 @@ if (enable_perfetto_trace_processor_sqlite) {
deps += [ "importers/instruments" ]
}
if (enable_perfetto_trace_processor_json) {
deps += [ "importers/gecko" ]
deps += [
"importers/gecko",
"importers/json",
]
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/trace_processor/importers/common/trace_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ class ProtoTraceParser {
virtual void ParseFtraceEvent(uint32_t, int64_t, TracePacketData) = 0;
virtual void ParseInlineSchedSwitch(uint32_t, int64_t, InlineSchedSwitch) = 0;
virtual void ParseInlineSchedWaking(uint32_t, int64_t, InlineSchedWaking) = 0;
virtual void ParseLegacyV8ProfileEvent(int64_t, LegacyV8CpuProfileEvent) = 0;
};

class JsonTraceParser {
public:
virtual ~JsonTraceParser();
virtual void ParseJsonPacket(int64_t, std::string) = 0;
virtual void ParseSystraceLine(int64_t, SystraceLine) = 0;
virtual void ParseLegacyV8ProfileEvent(int64_t, LegacyV8CpuProfileEvent) = 0;
};

class FuchsiaRecordParser {
Expand Down
47 changes: 24 additions & 23 deletions src/trace_processor/importers/json/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,39 @@ source_set("minimal") {
}
}

source_set("full") {
sources = [
"json_trace_parser_impl.cc",
"json_trace_parser_impl.h",
"json_trace_tokenizer.cc",
"json_trace_tokenizer.h",
]
deps = [
":minimal",
"../../../../gn:default_deps",
"../../sorter",
"../../storage",
"../../tables",
"../../types",
"../common",
"../systrace:full",
"../systrace:systrace_line",
]
if (enable_perfetto_trace_processor_json) {
public_deps = [ "../../../../gn:jsoncpp" ]
if (enable_perfetto_trace_processor_json) {
source_set("json") {
sources = [
"json_trace_parser_impl.cc",
"json_trace_parser_impl.h",
"json_trace_tokenizer.cc",
"json_trace_tokenizer.h",
]
deps = [
":minimal",
"../../../../gn:default_deps",
"../../sorter",
"../../storage",
"../../tables",
"../../types",
"../common",
"../common:parser_types",
"../systrace:full",
"../systrace:systrace_line",
]
if (enable_perfetto_trace_processor_json) {
public_deps = [ "../../../../gn:jsoncpp" ]
}
}
}

if (enable_perfetto_trace_processor_json) {
perfetto_unittest_source_set("unittests") {
testonly = true
sources = [
"json_trace_tokenizer_unittest.cc",
"json_utils_unittest.cc",
]
deps = [
":full",
":json",
":minimal",
"../../../../gn:default_deps",
"../../../../gn:gtest_and_gmock",
Expand Down
35 changes: 17 additions & 18 deletions src/trace_processor/importers/json/json_trace_parser_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@

#include "perfetto/base/build_config.h"
#include "perfetto/base/logging.h"
#include "perfetto/base/status.h"
#include "perfetto/ext/base/hash.h"
#include "perfetto/ext/base/string_utils.h"
#include "perfetto/ext/base/string_view.h"
#include "src/trace_processor/importers/common/event_tracker.h"
#include "src/trace_processor/importers/common/flow_tracker.h"
#include "src/trace_processor/importers/common/legacy_v8_cpu_profile_tracker.h"
#include "src/trace_processor/importers/common/parser_types.h"
#include "src/trace_processor/importers/common/process_tracker.h"
#include "src/trace_processor/importers/common/slice_tracker.h"
#include "src/trace_processor/importers/common/track_tracker.h"
Expand All @@ -39,10 +42,8 @@
#include "src/trace_processor/tables/slice_tables_py.h"
#include "src/trace_processor/types/trace_processor_context.h"

namespace perfetto {
namespace trace_processor {
namespace perfetto::trace_processor {

#if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON)
namespace {

std::optional<uint64_t> MaybeExtractFlowIdentifier(const Json::Value& value,
Expand All @@ -60,7 +61,6 @@ std::optional<uint64_t> MaybeExtractFlowIdentifier(const Json::Value& value,
}

} // namespace
#endif // PERFETTO_BUILDFLAG(PERFETTO_TP_JSON)

JsonTraceParserImpl::JsonTraceParserImpl(TraceProcessorContext* context)
: context_(context), systrace_line_parser_(context) {}
Expand All @@ -75,7 +75,6 @@ void JsonTraceParserImpl::ParseJsonPacket(int64_t timestamp,
std::string string_value) {
PERFETTO_DCHECK(json::IsJsonSupported());

#if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON)
auto opt_value = json::ParseJsonString(base::StringView(string_value));
if (!opt_value) {
context_->storage->IncrementStats(stats::json_parser_failure);
Expand Down Expand Up @@ -381,18 +380,11 @@ void JsonTraceParserImpl::ParseJsonPacket(int64_t timestamp,
}
}
}
#else
perfetto::base::ignore_result(timestamp);
perfetto::base::ignore_result(context_);
perfetto::base::ignore_result(string_value);
PERFETTO_ELOG("Cannot parse JSON trace due to missing JSON support");
#endif // PERFETTO_BUILDFLAG(PERFETTO_TP_JSON)
}

void JsonTraceParserImpl::MaybeAddFlow(TrackId track_id,
const Json::Value& event) {
PERFETTO_DCHECK(json::IsJsonSupported());
#if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON)
auto opt_bind_id = MaybeExtractFlowIdentifier(event, /* version2 = */ true);
if (opt_bind_id) {
FlowTracker* flow_tracker = context_->flow_tracker.get();
Expand All @@ -410,11 +402,18 @@ void JsonTraceParserImpl::MaybeAddFlow(TrackId track_id,
context_->storage->IncrementStats(stats::flow_without_direction);
}
}
#else
perfetto::base::ignore_result(track_id);
perfetto::base::ignore_result(event);
#endif // PERFETTO_BUILDFLAG(PERFETTO_TP_JSON)
}

} // namespace trace_processor
} // namespace perfetto
void JsonTraceParserImpl::ParseLegacyV8ProfileEvent(
int64_t ts,
LegacyV8CpuProfileEvent event) {
base::Status status = context_->legacy_v8_cpu_profile_tracker->AddSample(
ts, event.session_id, event.pid, event.tid, event.callsite_id);
if (!status.ok()) {
context_->storage->IncrementStats(
stats::legacy_v8_cpu_profile_invalid_sample);
}
context_->args_tracker->Flush();
}

} // namespace perfetto::trace_processor
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class JsonTraceParserImpl : public JsonTraceParser {
// TraceParser implementation.
void ParseJsonPacket(int64_t, std::string) override;
void ParseSystraceLine(int64_t, SystraceLine) override;
void ParseLegacyV8ProfileEvent(int64_t, LegacyV8CpuProfileEvent) override;

private:
TraceProcessorContext* const context_;
Expand Down
67 changes: 62 additions & 5 deletions src/trace_processor/importers/json/json_trace_tokenizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
#include <memory>
#include <optional>
#include <string>
#include <utility>

#include "perfetto/base/logging.h"
#include "perfetto/base/status.h"
#include "perfetto/ext/base/string_utils.h"
#include "perfetto/ext/base/string_view.h"
#include "perfetto/public/compiler.h"
#include "perfetto/trace_processor/trace_blob_view.h"
#include "src/trace_processor/importers/common/legacy_v8_cpu_profile_tracker.h"
#include "src/trace_processor/importers/json/json_utils.h"
#include "src/trace_processor/importers/systrace/systrace_line.h"
#include "src/trace_processor/sorter/trace_sorter.h" // IWYU pragma: keep
#include "src/trace_processor/storage/stats.h"
#include "src/trace_processor/util/status_macros.h"
Expand Down Expand Up @@ -551,6 +551,14 @@ base::Status JsonTraceTokenizer::HandleTraceEvent(const char* start,
break;
}

// Metadata events may omit ts. In all other cases error:
std::optional<std::string> opt_raw_ph;
RETURN_IF_ERROR(ExtractValueForJsonKey(unparsed, "ph", &opt_raw_ph));
if (PERFETTO_UNLIKELY(opt_raw_ph == "P")) {
RETURN_IF_ERROR(ParseV8SampleEvent(unparsed));
continue;
}

std::optional<std::string> opt_raw_ts;
RETURN_IF_ERROR(ExtractValueForJsonKey(unparsed, "ts", &opt_raw_ts));
std::optional<int64_t> opt_ts =
Expand All @@ -563,9 +571,6 @@ base::Status JsonTraceTokenizer::HandleTraceEvent(const char* start,
if (opt_ts.has_value()) {
ts = opt_ts.value();
} else {
// Metadata events may omit ts. In all other cases error:
std::optional<std::string> opt_raw_ph;
RETURN_IF_ERROR(ExtractValueForJsonKey(unparsed, "ph", &opt_raw_ph));
if (!opt_raw_ph || *opt_raw_ph != "M") {
context_->storage->IncrementStats(stats::json_tokenizer_failure);
continue;
Expand All @@ -576,6 +581,58 @@ base::Status JsonTraceTokenizer::HandleTraceEvent(const char* start,
return SetOutAndReturn(next, out);
}

base::Status JsonTraceTokenizer::ParseV8SampleEvent(base::StringView unparsed) {
auto opt_evt = json::ParseJsonString(unparsed);
if (!opt_evt) {
return base::OkStatus();
}
const auto& evt = *opt_evt;
std::optional<uint32_t> id = base::StringToUInt32(evt["id"].asString(), 16);
if (!id) {
return base::OkStatus();
}
uint32_t pid = evt["pid"].asUInt();
uint32_t tid = evt["tid"].asUInt();
const auto& val = evt["args"]["data"];
if (val.isMember("startTime")) {
context_->legacy_v8_cpu_profile_tracker->SetStartTsForSessionAndPid(
*id, pid, val["startTime"].asInt64() * 1000);
return base::OkStatus();
}
const auto& profile = val["cpuProfile"];
for (const auto& n : profile["nodes"]) {
uint32_t node_id = n["id"].asUInt();
std::optional<uint32_t> parent_node_id =
n.isMember("parent") ? std::make_optional(n["parent"].asUInt())
: std::nullopt;
const auto& frame = n["callFrame"];
base::StringView url =
frame.isMember("url") ? frame["url"].asCString() : base::StringView();
base::StringView function_name = frame["functionName"].asCString();
base::Status status = context_->legacy_v8_cpu_profile_tracker->AddCallsite(
*id, pid, node_id, parent_node_id, url, function_name);
if (!status.ok()) {
context_->storage->IncrementStats(
stats::legacy_v8_cpu_profile_invalid_callsite);
continue;
}
}
const auto& samples = profile["samples"];
const auto& deltas = val["timeDeltas"];
if (samples.size() != deltas.size()) {
return base::ErrStatus(
"v8 legacy profile: samples and timestamps do not have same size");
}
for (uint32_t i = 0; i < samples.size(); ++i) {
ASSIGN_OR_RETURN(int64_t ts,
context_->legacy_v8_cpu_profile_tracker->AddDeltaAndGetTs(
*id, pid, deltas[i].asInt64() * 1000));
context_->sorter->PushLegacyV8CpuProfileEvent(ts, *id, pid, tid,
samples[i].asUInt());
}
return base::OkStatus();
}

base::Status JsonTraceTokenizer::HandleDictionaryKey(const char* start,
const char* end,
const char** out) {
Expand Down
Loading

0 comments on commit 4d0d3ab

Please sign in to comment.