device_memory_report: emit instant events for snapshot report - #36
Conversation
Emit events on trace session start and live events. Allows tracking of memory usage over time. BUG=b/559839199
olehkuznetsov
left a comment
There was a problem hiding this comment.
Code Review Summary for PR #36 (vmpxxnwsswos)
Thank you for adding Perfetto snapshot dumping and memory tracking events! Enabling state reconstruction for trace sessions started mid-execution is a valuable improvement.
Key Observations & Findings:
- [P1] Missing
RemoveSessionObserverin Destructor (device_memory_report_perfetto.cpp):
g_session_observermust unregister viaperfetto::TrackEvent::RemoveSessionObserver(this)in its destructor to prevent a dangling pointer in Perfetto's static registry after layer unloading (dlclose), which leads to SIGSEGV on subsequent trace sessions. - [P1] Pre-Existing Tracing Sessions Miss Snapshot (
device_memory_report_perfetto.cpp):
AddSessionObserveronly fires for future session starts. If tracing is already running when the Vulkan instance is created (standard ADB profiling flow), checkingTRACE_EVENT_CATEGORY_ENABLEDupon initialization ensures the snapshot is captured. - [P1] Phantom
BINDInstant Event for Unbound Memory (device_memory_report.cpp):
Emittingoperation: "BIND"withoffset: 0forapplied_unbound_bytesinvents a synthetic binding that collides with real resources at offset 0 and diverges from live tracing (which tracks unbound memory viaTRACE_COUNTER). This block should be removed. - [P1] Dead Write-Only
heap_index& Semantic Conflation (device_memory_report.h/.cpp):
allocation.heap_indexis never read, and assigningmemory_type_indexinto it conflates memory type index with heap index. Also remove the unused local variableheap_idx. - [P1/P2] Unnecessary
std::stringAllocations on Hot Paths (device_memory_report.cpp):
GetCluster()returnsconst char*. Usingconst char*directly eliminates heap allocation churn on resource binding/unbinding paths even when tracing is disabled. - [P1] Vacuous
EXPECT_TRUE(true)in Snapshot Test (test_devicememoryreport.cpp):
Convert the smoke test into a meaningful regression test usingDeviceMemoryReportTestPeerto assert tracked allocation state, suballocations, unbound bytes, and teardown cleanup. - [P2] Naming & Commit Scope:
Expand abbreviated identifiers (is_img,sub_size,src_str,op_str,cb_data) to full descriptive names per project style.
Please see the detailed inline review comments for line-by-line suggestions.
…es in RemoveResourceBinding
olehkuznetsov
left a comment
There was a problem hiding this comment.
Review Summary: Instant Events & Snapshot Dump for VK_LAYER_GOOGLE_device_memory_report
Thanks for adding live and session-start snapshot instant event tracing (VulkanMemoryAllocation) to DeviceMemoryReport!
Overall, the architecture for capturing session-start state via TrackEventSessionObserver is solid. Before merging, there are a few lifecycle, schema-consistency, and commit-hygiene items to address:
- C++ Static Destruction Order (
device_memory_report_perfetto.cpp):g_session_observeris initialized at library load time (.init_array), whereasDeviceMemoryReport::Get()is initialized lazily on first call. By C++ LIFO static destruction rules ([basic.start.term]),~DeviceMemoryReport()destroyscounter_mutex_and internal maps before~DeviceMemoryReportSessionObserver()unregisters from Perfetto. - Synthetic Unbound
BINDEvent in Snapshot (device_memory_report.cpp:423-432): Emitting a synthetic"operation", "BIND"event atoffset = 0forunbound_memorydiverges from live tracing (which tracks unbound headroom viaTRACE_COUNTER), spatially overlaps resources bound at offset0, and is never retired or shrunk when subsequent live resources bind to the slab. - Suballocation & Fallback
DESTROYCompleteness (device_memory_report.cpp):- When a
VkDeviceMemoryslab is freed while resources are still bound to it (which is legal in Vulkan per Spec Section 11.2.13 and occurs inMemoryReportSnapshotDump),RemoveAllocationTrackingclearsresource_to_memory_map_without emitting suballocationDESTROYevents, causing subsequentOnDestroyObjectcalls to silently dropDESTROYevents. - In
OnFreeMemory, callingRemoveAllocationTracking(handle)before readingtotal_sizehardcodes"size", 0and emits phantomDESTROYevents forVK_NULL_HANDLE/ untracked handles. - In
OnMemoryReportEvent, driverDESTROY(FREE_EXT) events always fall back to"unbound_memory"becausevkDestroyImage/vkDestroyBuffercallsOnDestroyObject(erasingresources_[object_handle]) before downstreamfpDestroyImage/fpDestroyBufferfiresFREE_EXT.
- When a
- Commit History Hygiene: Commits 2–8 (
vxwltluqnlvu..omqunmrtmqvt) are incremental fixups repairing defects introduced in Commit 1 (vmpxxnwsswos, e.g., deadheap_indexmember, missing observer teardown,EXPECT_TRUE(true)test). Please squash this stack into 1 atomic commit (or 2 commits: implementation + test) before merging so intermediate broken states are not preserved ingit bisecthistory.
…ballocation DESTROY events
…e in BindResourceMemory
…copies in snapshot dump
…cluster_name in OnMemoryReportEvent
Return early when handling FREE_EXT or UNIMPORT_EXT for allocations that are not tracked in memory_allocations_, preventing phantom DESTROY trace events from being emitted.
…-report-snapshot-events
olehkuznetsov
left a comment
There was a problem hiding this comment.
This is a strong series — the static destruction ordering fix in 62c14e418 ("fix static destruction order of session observer"), dropping the synthetic unbound BIND in 91d221c42 ("remove synthetic unbound BIND event and string copies in snapshot dump"), and the driver-keying alignment in 888957aef ("align key with non-driver check and use cached cluster_name in OnMemoryReportEvent") all hold up under scrutiny. The locked std::optional test peer is the right shape, and I traced the new test's counter arithmetic end to end — it's net-zero, so it's safe under --gtest_shuffle.
One real issue: OnMemoryReportEvent reports a spec-undefined size on DESTROY. You already fixed the identical thing in OnFreeMemory two commits earlier, so I think this is just an oversight. One-line fix.
Emit events on trace session start and live events.
Allows tracking of memory usage over time.
BUG=b/559839199