-
Notifications
You must be signed in to change notification settings - Fork 695
tp: integrate ProtoVM #4768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
tp: integrate ProtoVM #4768
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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")), | ||
|
|
@@ -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()) { | ||
| 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()); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) { | ||
|
|
||
| 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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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_ |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.