diff --git a/mediapipe/framework/packet.h b/mediapipe/framework/packet.h index 1024cbc15..39c6321c8 100644 --- a/mediapipe/framework/packet.h +++ b/mediapipe/framework/packet.h @@ -18,6 +18,7 @@ #define MEDIAPIPE_FRAMEWORK_PACKET_H_ #include +#include #include #include #include @@ -368,11 +369,14 @@ class HolderBase { } // Returns a printable string identifying the type stored in the holder. virtual const std::string DebugTypeName() const = 0; + // Returns debug data id. + virtual int64_t DebugDataId() const = 0; // Returns the registered type name if it's available, otherwise the // empty string. virtual const std::string RegisteredTypeName() const = 0; // Get the type id of the underlying data type. virtual TypeId GetTypeId() const = 0; + // Downcasts this to Holder. Returns nullptr if deserialization // failed or if the requested type is not what is stored. template @@ -534,6 +538,7 @@ class Holder : public HolderBase { const std::string DebugTypeName() const final { return MediaPipeTypeStringOrDemangled(); } + int64_t DebugDataId() const final { return reinterpret_cast(ptr_); } const std::string RegisteredTypeName() const final { const std::string* type_string = MediaPipeTypeString(); if (type_string) { diff --git a/mediapipe/framework/profiler/graph_tracer_test.cc b/mediapipe/framework/profiler/graph_tracer_test.cc index c1cc819c1..07518aa6c 100644 --- a/mediapipe/framework/profiler/graph_tracer_test.cc +++ b/mediapipe/framework/profiler/graph_tracer_test.cc @@ -1423,5 +1423,13 @@ TEST_F(GraphTracerE2ETest, DestructGraph) { } } +TEST(TraceBuilderTest, EventDataIsExtracted) { + int value = 10; + Packet p = PointToForeign(&value); + TraceEvent event; + event.set_packet_data_id(&p); + EXPECT_EQ(event.event_data, reinterpret_cast(&value)); +} + } // namespace } // namespace mediapipe diff --git a/mediapipe/framework/profiler/trace_buffer.h b/mediapipe/framework/profiler/trace_buffer.h index b5e2d9994..8dc09aef7 100644 --- a/mediapipe/framework/profiler/trace_buffer.h +++ b/mediapipe/framework/profiler/trace_buffer.h @@ -15,6 +15,9 @@ #ifndef MEDIAPIPE_FRAMEWORK_PROFILER_TRACE_BUFFER_H_ #define MEDIAPIPE_FRAMEWORK_PROFILER_TRACE_BUFFER_H_ +#include +#include + #include "absl/time/time.h" #include "mediapipe/framework/calculator_profile.pb.h" #include "mediapipe/framework/packet.h" @@ -23,17 +26,6 @@ namespace mediapipe { -namespace packet_internal { -// Returns a hash of the packet data address from a packet data holder. -inline const int64 GetPacketDataId(const HolderBase* holder) { - if (holder == nullptr) { - return 0; - } - const void* address = &(static_cast*>(holder)->data()); - return reinterpret_cast(address); -} -} // namespace packet_internal - // Packet trace log event. struct TraceEvent { using EventType = GraphTrace::EventType; @@ -75,8 +67,12 @@ struct TraceEvent { return *this; } inline TraceEvent& set_packet_data_id(const Packet* packet) { - this->event_data = - packet_internal::GetPacketDataId(packet_internal::GetHolder(*packet)); + const auto* holder = packet_internal::GetHolder(*packet); + int64_t data_id = 0; + if (holder != nullptr) { + data_id = holder->DebugDataId(); + } + this->event_data = data_id; return *this; } inline TraceEvent& set_thread_id(int thread_id) {