Refactor common infrastructure for creating memory reports. (#36579)

This removes a bunch of duplicated code needed to support
ConditionalMallocSizeOf correctly, and fixes multiple places where that
code was subtly wrong (the seen pointers hashset was never cleared).

Testing: Measuring https://www.nist.gov/image-gallery lots of times.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2025-04-17 22:14:49 -04:00 committed by GitHub
parent 2a81987590
commit 5e2d42e944
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 68 additions and 57 deletions

View file

@ -8,6 +8,7 @@ use base::id::PipelineId;
use compositing_traits::CrossProcessCompositorApi;
use ipc_channel::ipc::IpcSender;
use log::debug;
use malloc_size_of::MallocSizeOfOps;
use malloc_size_of_derive::MallocSizeOf;
use pixels::{Image, ImageMetadata};
use profile_traits::mem::Report;
@ -116,7 +117,7 @@ pub trait ImageCache: Sync + Send {
where
Self: Sized;
fn memory_report(&self, prefix: &str) -> Report;
fn memory_report(&self, prefix: &str, ops: &mut MallocSizeOfOps) -> Report;
/// Definitively check whether there is a cached, fully loaded image available.
fn get_image(

View file

@ -22,6 +22,7 @@ log = { workspace = true }
malloc_size_of = { workspace = true }
malloc_size_of_derive = { workspace = true }
serde = { workspace = true }
servo_allocator = { path = "../../allocator" }
servo_config = { path = "../../config" }
signpost = { git = "https://github.com/pcwalton/signpost.git" }
strum_macros = { workspace = true }

View file

@ -6,12 +6,16 @@
#![deny(missing_docs)]
use std::cell::{LazyCell, RefCell};
use std::collections::HashSet;
use std::ffi::c_void;
use std::marker::Send;
use crossbeam_channel::Sender;
use ipc_channel::ipc::{self, IpcSender};
use ipc_channel::router::ROUTER;
use log::warn;
use malloc_size_of::MallocSizeOfOps;
use serde::{Deserialize, Serialize};
/// A trait to abstract away the various kinds of message senders we use.
@ -266,3 +270,21 @@ pub enum ProfilerMsg {
/// Triggers sending back the memory profiling metrics,
Report(IpcSender<MemoryReportResult>),
}
thread_local!(static SEEN_POINTERS: LazyCell<RefCell<HashSet<*const c_void>>> = const {
LazyCell::new(Default::default)
});
/// Invoke the provided function after initializing the memory profile tools.
/// The function is expected to call all the desired [MallocSizeOf::size_of]
/// for allocations reachable from the current thread.
pub fn perform_memory_report<F: FnOnce(&mut MallocSizeOfOps)>(f: F) {
SEEN_POINTERS.with(|pointers| pointers.borrow_mut().clear());
let seen_pointer = move |ptr| SEEN_POINTERS.with(|pointers| !pointers.borrow_mut().insert(ptr));
let mut ops = MallocSizeOfOps::new(
servo_allocator::usable_size,
None,
Some(Box::new(seen_pointer)),
);
f(&mut ops);
}

View file

@ -27,6 +27,7 @@ use fonts::{FontContext, SystemFontServiceProxy};
use fxhash::FxHashMap;
use ipc_channel::ipc::IpcSender;
use libc::c_void;
use malloc_size_of::MallocSizeOfOps;
use malloc_size_of_derive::MallocSizeOf;
use net_traits::image_cache::{ImageCache, PendingImageId};
use pixels::Image;
@ -217,7 +218,7 @@ pub trait Layout {
/// Requests that layout measure its memory usage. The resulting reports are sent back
/// via the supplied channel.
fn collect_reports(&self, reports: &mut Vec<Report>);
fn collect_reports(&self, reports: &mut Vec<Report>, ops: &mut MallocSizeOfOps);
/// Sets quirks mode for the document, causing the quirks mode stylesheet to be used.
fn set_quirks_mode(&mut self, quirks_mode: QuirksMode);