Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -15721,6 +15721,7 @@ filegroup {
"src/trace_processor/importers/proto/proto_trace_parser_impl.cc",
"src/trace_processor/importers/proto/proto_trace_reader.cc",
"src/trace_processor/importers/proto/proto_trace_tokenizer.cc",
"src/trace_processor/importers/proto/protovm_incremental_tracing.cc",
"src/trace_processor/importers/proto/stack_profile_sequence_state.cc",
"src/trace_processor/importers/proto/track_event_module.cc",
"src/trace_processor/importers/proto/track_event_parser.cc",
Expand Down Expand Up @@ -16949,6 +16950,7 @@ cc_library_static {
":perfetto_src_base_version",
":perfetto_src_kernel_utils_kernel_wakelock_errors",
":perfetto_src_kernel_utils_syscall_table",
":perfetto_src_protovm_protovm",
":perfetto_src_protozero_proto_ring_buffer",
":perfetto_src_protozero_protozero",
":perfetto_src_protozero_text_to_proto_text_to_proto",
Expand Down Expand Up @@ -20298,6 +20300,7 @@ cc_library_static {
":perfetto_src_base_clock_snapshots",
":perfetto_src_kernel_utils_kernel_wakelock_errors",
":perfetto_src_kernel_utils_syscall_table",
":perfetto_src_protovm_protovm",
":perfetto_src_protozero_protozero",
":perfetto_src_protozero_text_to_proto_text_to_proto",
":perfetto_src_trace_processor_containers_containers",
Expand Down Expand Up @@ -20829,6 +20832,7 @@ cc_binary_host {
":perfetto_src_base_version",
":perfetto_src_kernel_utils_kernel_wakelock_errors",
":perfetto_src_kernel_utils_syscall_table",
":perfetto_src_protovm_protovm",
":perfetto_src_protozero_proto_ring_buffer",
":perfetto_src_protozero_protozero",
":perfetto_src_protozero_text_to_proto_text_to_proto",
Expand Down
6 changes: 6 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ perfetto_cc_library(
srcs = [
":src_kernel_utils_kernel_wakelock_errors",
":src_kernel_utils_syscall_table",
":src_protovm_protovm",
":src_protozero_proto_ring_buffer",
":src_protozero_text_to_proto_text_to_proto",
":src_trace_processor_core_common_common",
Expand Down Expand Up @@ -564,6 +565,7 @@ perfetto_cc_library(
srcs = [
":src_kernel_utils_kernel_wakelock_errors",
":src_kernel_utils_syscall_table",
":src_protovm_protovm",
":src_protozero_proto_ring_buffer",
":src_protozero_text_to_proto_text_to_proto",
":src_trace_processor_core_common_common",
Expand Down Expand Up @@ -2837,6 +2839,8 @@ perfetto_filegroup(
"src/trace_processor/importers/proto/proto_trace_reader.h",
"src/trace_processor/importers/proto/proto_trace_tokenizer.cc",
"src/trace_processor/importers/proto/proto_trace_tokenizer.h",
"src/trace_processor/importers/proto/protovm_incremental_tracing.cc",
"src/trace_processor/importers/proto/protovm_incremental_tracing.h",
"src/trace_processor/importers/proto/stack_profile_sequence_state.cc",
"src/trace_processor/importers/proto/stack_profile_sequence_state.h",
"src/trace_processor/importers/proto/track_event_event_importer.h",
Expand Down Expand Up @@ -8459,6 +8463,7 @@ perfetto_cc_library(
srcs = [
":src_kernel_utils_kernel_wakelock_errors",
":src_kernel_utils_syscall_table",
":src_protovm_protovm",
":src_protozero_text_to_proto_text_to_proto",
":src_trace_processor_core_common_common",
":src_trace_processor_core_dataframe_dataframe",
Expand Down Expand Up @@ -8700,6 +8705,7 @@ perfetto_cc_binary(
":include_perfetto_trace_processor_util",
":src_kernel_utils_kernel_wakelock_errors",
":src_kernel_utils_syscall_table",
":src_protovm_protovm",
":src_protozero_proto_ring_buffer",
":src_protozero_text_to_proto_text_to_proto",
":src_trace_processor_core_common_common",
Expand Down
3 changes: 3 additions & 0 deletions src/trace_processor/importers/proto/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ source_set("minimal") {
"proto_trace_reader.h",
"proto_trace_tokenizer.cc",
"proto_trace_tokenizer.h",
"protovm_incremental_tracing.cc",
"protovm_incremental_tracing.h",
"stack_profile_sequence_state.cc",
"stack_profile_sequence_state.h",
"track_event_event_importer.h",
Expand Down Expand Up @@ -94,6 +96,7 @@ source_set("minimal") {
"../../../../protos/perfetto/trace/translation:zero",
"../../../../protos/third_party/chromium:zero",
"../../../base",
"../../../protovm",
"../../../protozero",
"../../containers",
"../../sorter",
Expand Down
13 changes: 13 additions & 0 deletions src/trace_processor/importers/proto/proto_trace_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class InlineSchedWakingSink
ProtoTraceReader::ProtoTraceReader(TraceProcessorContext* ctx)
: context_(ctx),
parser_(std::make_unique<ProtoTraceParserImpl>(ctx, &module_context_)),
protovm_(ctx),
skipped_packet_key_id_(ctx->storage->InternString("skipped_packet")),
invalid_incremental_state_key_id_(
ctx->storage->InternString("invalid_incremental_state")),
Expand Down Expand Up @@ -236,6 +237,18 @@ base::Status ProtoTraceReader::ParsePacket(TraceBlobView packet) {
PERFETTO_DCHECK(decoder.has_machine_id() ==
(context_->machine_id() != MachineId(kDefaultMachineId)));

if (decoder.has_trace_provenance()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to all happen even before machine id right? In theoery this should always be handled on the root level eben before the above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uhm I am a bit lost on how multiple-machine traces are produced. If with multiple machines we have only one TraceProvenance packet and one ProtoVms packet (because there is only one traced instance that emits them), then yes, I should move the protovm stuff before machine id.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it's possible for there to be multiple traced instances which are being cominbed into one fianl trace file. This is how VM tracing works. It really depends on whether you can affect machine_id via a VM trace. You also seem to have a dependency on trusted_packet_sequence_id not being modifuable.

I suggest defining in the protovm documentation which fields are eligible to be targetted via protovm and which are not. Will make resolving things like this a lot easier.

protovm_.ProcessTraceProvenancePacket(decoder.trace_provenance());
} else if (decoder.has_protovms()) {
protovm_.ProcessProtoVmsPacket(decoder.protovms());
} else if (auto new_packet = protovm_.TryProcessPatch(packet); new_packet) {
packet = TraceBlobView(std::move(*new_packet));
// TODO(keanmariotti): fix this hack
decoder.protos::pbzero::TracePacket::Decoder::~Decoder();
new (&decoder)
protos::pbzero::TracePacket::Decoder(packet.data(), packet.length());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LalitMaganti what about extending decoder's interface to fix this hack? Either an assignment operator or a Reset() member function

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add the move assignment operator.

}

uint32_t seq_id = decoder.trusted_packet_sequence_id();
auto [scoped_state, inserted] = sequence_state_.Insert(seq_id, {});
if (decoder.has_trusted_packet_sequence_id()) {
Expand Down
2 changes: 2 additions & 0 deletions src/trace_processor/importers/proto/proto_trace_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "src/trace_processor/importers/proto/packet_sequence_state_builder.h"
#include "src/trace_processor/importers/proto/proto_importer_module.h"
#include "src/trace_processor/importers/proto/proto_trace_tokenizer.h"
#include "src/trace_processor/importers/proto/protovm_incremental_tracing.h"
#include "src/trace_processor/storage/trace_storage.h"
#include "src/trace_processor/types/trace_processor_context.h"

Expand Down Expand Up @@ -125,6 +126,7 @@ class ProtoTraceReader : public ChunkedTraceReader {
std::unique_ptr<ProtoTraceParserImpl> parser_;
base::FlatHashMap<uint32_t, std::unique_ptr<ProtoTraceReader>>
machine_to_proto_readers_;
ProtoVmIncrementalTracing protovm_;

// Temporary. Currently trace packets do not have a timestamp, so the
// timestamp given is latest_timestamp_.
Expand Down
109 changes: 109 additions & 0 deletions src/trace_processor/importers/proto/protovm_incremental_tracing.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (C) 2026 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.
*/

#include "src/trace_processor/importers/proto/protovm_incremental_tracing.h"

#include "perfetto/protozero/field.h"
#include "perfetto/protozero/scattered_heap_buffer.h"
#include "protos/perfetto/trace/perfetto/trace_provenance.pbzero.h"
#include "src/protovm/vm.h"
#include "src/trace_processor/storage/stats.h"
#include "src/trace_processor/storage/trace_storage.h"
#include "src/trace_processor/types/trace_processor_context.h"

namespace perfetto::trace_processor {

ProtoVmIncrementalTracing::ProtoVmIncrementalTracing(
TraceProcessorContext* context)
: context_(context) {}

ProtoVmIncrementalTracing::~ProtoVmIncrementalTracing() = default;

void ProtoVmIncrementalTracing::ProcessTraceProvenancePacket(
protozero::ConstBytes blob) {
protos::pbzero::TraceProvenance::Decoder trace_provenance(blob);
for (auto it_buf = trace_provenance.buffers(); it_buf; ++it_buf) {
protos::pbzero::TraceProvenance::Buffer::Decoder buffer(*it_buf);
for (auto it_seq = buffer.sequences(); it_seq; ++it_seq) {
protos::pbzero::TraceProvenance::Sequence::Decoder sequence(*it_seq);
producer_id_to_sequence_ids_[sequence.producer_id()].push_back(
sequence.id());
}
}
}

void ProtoVmIncrementalTracing::ProcessProtoVmsPacket(
protozero::ConstBytes blob) {
protos::pbzero::TracePacket::ProtoVms::Decoder decoder(blob);
for (auto it = decoder.instance(); it; ++it) {
protos::pbzero::TracePacket::ProtoVms::Instance::Decoder instance(*it);
protozero::ConstBytes state = instance.has_state()
? instance.state()
: protozero::ConstBytes{nullptr, 0};
vms_.push_back(std::make_unique<protovm::Vm>(
instance.program(), 1024 * instance.memory_limit_kb(), state));
protovm::Vm* vm = vms_.back().get();
for (auto producer_id = instance.producer_id(); producer_id;
++producer_id) {
auto* sequence_ids = producer_id_to_sequence_ids_.Find(*producer_id);
if (!sequence_ids) {
context_->storage->IncrementStats(stats::protovm_registration_error);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use import logging.

continue;
}
for (auto sequence_id : *sequence_ids) {
sequence_id_to_vms_[sequence_id].push_back(vm);
}
}
}
}

std::optional<TraceBlob> ProtoVmIncrementalTracing::TryProcessPatch(
const TraceBlobView& blob) {
protos::pbzero::TracePacket::Decoder patch(blob.data(), blob.size());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we creating another decoder here? Why not just pass it in?

if (PERFETTO_UNLIKELY(!patch.has_trusted_packet_sequence_id())) {
return std::nullopt;
}
std::vector<protovm::Vm*>* vms =
sequence_id_to_vms_.Find(patch.trusted_packet_sequence_id());
if (!vms) {
return std::nullopt;
}
for (auto* vm : *vms) {
auto status =
vm->ApplyPatch(protozero::ConstBytes{blob.data(), blob.size()});
if (status.IsOk()) {
return SerializeIncrementalState(*vm, patch);
}
if (status.IsAbort()) {
context_->storage->IncrementStats(stats::protovm_abort);
}
}
return std::nullopt;
}

TraceBlob ProtoVmIncrementalTracing::SerializeIncrementalState(
const protovm::Vm& vm,
const protos::pbzero::TracePacket::Decoder& patch) const {
protozero::HeapBuffered<protos::pbzero::TracePacket> proto;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again please use the BlobPacketWriter to avoid copies.

vm.SerializeIncrementalState(proto.get());
proto->set_trusted_uid(patch.trusted_uid());
proto->set_trusted_pid(patch.trusted_pid());
proto->set_trusted_packet_sequence_id(patch.trusted_packet_sequence_id());
auto [data, size] = proto.SerializeAsUniquePtr();
return TraceBlob::TakeOwnership(std::move(data), size);
}

} // namespace perfetto::trace_processor
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2026 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.
*/

#ifndef SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTOVM_INCREMENTAL_TRACING_H_
#define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTOVM_INCREMENTAL_TRACING_H_

#include <memory>
#include <optional>
#include <vector>

#include "perfetto/ext/base/flat_hash_map.h"
#include "perfetto/trace_processor/trace_blob.h"
#include "perfetto/trace_processor/trace_blob_view.h"
#include "protos/perfetto/trace/trace_packet.pbzero.h"

namespace perfetto {

namespace protovm {
class Vm;
}

namespace trace_processor {

class TraceProcessorContext;

class ProtoVmIncrementalTracing {
public:
explicit ProtoVmIncrementalTracing(TraceProcessorContext*);
~ProtoVmIncrementalTracing();
void ProcessTraceProvenancePacket(protozero::ConstBytes blob);
void ProcessProtoVmsPacket(protozero::ConstBytes blob);
std::optional<TraceBlob> TryProcessPatch(const TraceBlobView& packet);

private:
TraceBlob SerializeIncrementalState(
const protovm::Vm& vm,
const protos::pbzero::TracePacket::Decoder& patch) const;

TraceProcessorContext* context_;
base::FlatHashMap<int32_t, std::vector<uint32_t>>
producer_id_to_sequence_ids_;
base::FlatHashMap<uint32_t, std::vector<protovm::Vm*>> sequence_id_to_vms_;
std::vector<std::unique_ptr<protovm::Vm>> vms_;
};

} // namespace trace_processor
} // namespace perfetto

#endif // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_PROTOVM_INCREMENTAL_TRACING_H_
10 changes: 9 additions & 1 deletion src/trace_processor/storage/stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,15 @@ namespace perfetto::trace_processor::stats {
F(primes_missing_parent_id, kSingle, kInfo, kAnalysis, \
"The parent_id field was missing from an edge that requires it."), \
F(primes_malformed_timestamp, kSingle, kDataLoss, kAnalysis, \
"The timestamp for an edge or trace was not able to be parsed") // clang-format on
"The timestamp for an edge or trace was not able to be parsed"), \
F(protovm_abort, kSingle, kError, kAnalysis, \
"A ProtoVM instance aborted the execution while applying the patch. " \
"This might be due to inconsistencies between VM program logic and " \
"actual patch format."), \
F(protovm_registration_error, kSingle, kError, kAnalysis, \
"Failed to find the sequence IDs corresponding to a ProtoVM's producer " \
"ID. Such mapping should be provided by the TraceProvenance packet.")
// clang-format on

enum Type {
kSingle, // Single-value property, one value per key.
Expand Down
2 changes: 2 additions & 0 deletions test/trace_processor/diff_tests/include_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
from diff_tests.parser.android.tests_inputmethod_manager_service import InputMethodManagerService
from diff_tests.parser.android.tests_inputmethod_service import InputMethodService
from diff_tests.parser.android.tests_protolog import ProtoLog
from diff_tests.parser.android.tests_protovm_incremental_tracing import ProtoVmIncrementalTracing
from diff_tests.parser.android.tests_shell_transitions import ShellTransitions
from diff_tests.parser.android.tests_surfaceflinger_layers import SurfaceFlingerLayers
from diff_tests.parser.android.tests_surfaceflinger_transactions import SurfaceFlingerTransactions
Expand Down Expand Up @@ -248,6 +249,7 @@ def fetch_all_diff_tests(
SurfaceFlingerTransactions,
ShellTransitions,
ProtoLog,
ProtoVmIncrementalTracing,
ViewCapture,
WindowManager,
TrackEvent,
Expand Down
Loading
Loading