If you have a component that manages memory allocations, you should be registering and tracking those allocations with Chrome's memory-infra system. This lets you:
Some existing components which make use of memory tracing infra:
In order to hook into Chrome's memory infra system, your component needs to do two things:
In order to tie into the memory infra system, you must write a base::trace_event::MemoryDumpProvider for your component. You can implement this as a stand-alone class, or as an additional interface on an existing class. For example, this interface is frequently implemented on classes which manage a pool of allocations (see cc::ResourcePool for an example).
A MemoryDumpProvider has one basic job, to implement MemoryDumpProvider::OnMemoryDump. This function is responsible for iterating over the resources allocated/tracked by your component, and creating a base::trace_event::MemoryAllocatorDump for each using ProcessMemoryDump::CreateAllocatorDump. Here is a simple example:
For many components, this may be all that is needed. See Handling Shared Memory Allocations and Sub Allocations for information on more complex use cases.
Once you have created a base::trace_event::MemoryDumpProvider, you need to register it with the base::trace_event::MemoryDumpManager before the system can start polling it for memory information. Registration is generally straightforward, and involves calling MemoryDumpManager::RegisterDumpProvider:
In the above code, my_memory_dump_provider_ is the base::trace_event::MemoryDumpProvider outlined in the previous section. my_single_thread_task_runner_ is more complex and may be a number of things:
Unregistering a base::trace_event::MemoryDumpProvider is very similar to registering one:
The main complexity here is that unregistration must happen on the thread belonging to the base::SingleThreadTaskRunner provided at registration time. Unregistering on another thread can lead to race conditions if tracing is active when the provider is unregistered.
When an allocation is shared between two components, it may be useful to dump the allocation in both components, but you also want to avoid double-counting the allocation. This can be achieved using memory infra's concept of "ownership edges". An ownership edge represents that the "source" memory allocator dump owns a "target" memory allocator dump. If multiple "source" dumps own a single "target", then the cost of that "target" allocation will be split between the "source"s. Additionally, importance can be added to a specific ownership edge, allowing the highest importance "source" of that edge to claim the entire cost of the "target".
In the typical case, you will use base::trace_event::ProcessMemoryDump to create a shared global allocator dump. This dump will act as the target of all component-specific dumps of a specific resource:
If an allocation is being shared across process boundaries, it may be useful to generate a GUID which incorporates the ID of the local process, preventing two processes from generating colliding GUIDs. As it is not recommended to pass a Process ID between processes for security reasons, a function MemoryDumpManager::GetTracingProcessId is provided which generates a unique ID per process that can be passed with the resource without security concerns. Frequently this ID is used to generate a GUID that is based on the allocated resource's ID combined with the allocating process' tracing ID.
Another advanced use case involves tracking sub-allocations of a larger allocation. For instance, this is used in gpu::gles2::TextureManager to dump both the sub-allocations which make up a texture. Creating a sub allocation is easy - instead of calling ProcessMemoryDump::CreateAllocatorDump to create a base::trace_event::MemoryAllocatorDump, you simply call ProcessMemoryDump::AddSubAllocation, providing the GUID of the parent allocation as the first parameter.
For Developers > How-Tos > The Trace Event Profiling Tool (about:tracing) > Memory profiling in chrome://tracing >