net: Measure memory usage of storage thread. (#37053)

Our persistent localstorage data can be meaningfully large after testing
real world sites. This change ensures it shows up in about:memory.

Testing: Opened about:memory after launching the browser with a
persistent config
Fixes: Part of #11559

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2025-05-20 10:50:02 -04:00 committed by GitHub
parent 5b2305784a
commit 603ae44bcd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 46 additions and 5 deletions

View file

@ -97,14 +97,15 @@ pub fn new_resource_threads(
let (public_core, private_core) = new_core_resource_thread(
devtools_sender,
time_profiler_chan,
mem_profiler_chan,
mem_profiler_chan.clone(),
embedder_proxy,
config_dir.clone(),
ca_certificates,
ignore_certificate_errors,
protocols,
);
let storage: IpcSender<StorageThreadMsg> = StorageThreadFactory::new(config_dir);
let storage: IpcSender<StorageThreadMsg> =
StorageThreadFactory::new(config_dir, mem_profiler_chan);
(
ResourceThreads::new(public_core, storage.clone()),
ResourceThreads::new(private_core, storage),

View file

@ -8,7 +8,12 @@ use std::path::PathBuf;
use std::thread;
use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use malloc_size_of::MallocSizeOf;
use net_traits::storage_thread::{StorageThreadMsg, StorageType};
use profile_traits::mem::{
ProcessReports, ProfilerChan as MemProfilerChan, Report, ReportKind, perform_memory_report,
};
use profile_traits::path;
use servo_url::ServoUrl;
use crate::resource_thread;
@ -16,17 +21,26 @@ use crate::resource_thread;
const QUOTA_SIZE_LIMIT: usize = 5 * 1024 * 1024;
pub trait StorageThreadFactory {
fn new(config_dir: Option<PathBuf>) -> Self;
fn new(config_dir: Option<PathBuf>, mem_profiler_chan: MemProfilerChan) -> Self;
}
impl StorageThreadFactory for IpcSender<StorageThreadMsg> {
/// Create a storage thread
fn new(config_dir: Option<PathBuf>) -> IpcSender<StorageThreadMsg> {
fn new(
config_dir: Option<PathBuf>,
mem_profiler_chan: MemProfilerChan,
) -> IpcSender<StorageThreadMsg> {
let (chan, port) = ipc::channel().unwrap();
let chan2 = chan.clone();
thread::Builder::new()
.name("StorageManager".to_owned())
.spawn(move || {
StorageManager::new(port, config_dir).start();
mem_profiler_chan.run_with_memory_reporting(
|| StorageManager::new(port, config_dir).start(),
String::from("storage-reporter"),
chan2,
StorageThreadMsg::CollectMemoryReport,
);
})
.expect("Thread spawning failed");
chan
@ -83,6 +97,10 @@ impl StorageManager {
self.clear(sender, url, storage_type);
self.save_state()
},
StorageThreadMsg::CollectMemoryReport(sender) => {
let reports = self.collect_memory_reports();
sender.send(ProcessReports::new(reports));
},
StorageThreadMsg::Exit(sender) => {
// Nothing to do since we save localstorage set eagerly.
let _ = sender.send(());
@ -92,6 +110,24 @@ impl StorageManager {
}
}
fn collect_memory_reports(&self) -> Vec<Report> {
let mut reports = vec![];
perform_memory_report(|ops| {
reports.push(Report {
path: path!["storage", "local"],
kind: ReportKind::ExplicitJemallocHeapSize,
size: self.local_data.size_of(ops),
});
reports.push(Report {
path: path!["storage", "session"],
kind: ReportKind::ExplicitJemallocHeapSize,
size: self.session_data.size_of(ops),
});
});
reports
}
fn save_state(&self) {
if let Some(ref config_dir) = self.config_dir {
resource_thread::write_json_to_file(&self.local_data, config_dir, "local_data.json");

View file

@ -4,6 +4,7 @@
use ipc_channel::ipc::IpcSender;
use malloc_size_of_derive::MallocSizeOf;
use profile_traits::mem::ReportsChan;
use serde::{Deserialize, Serialize};
use servo_url::ServoUrl;
@ -45,4 +46,7 @@ pub enum StorageThreadMsg {
/// send a reply when done cleaning up thread resources and then shut it down
Exit(IpcSender<()>),
/// Measure memory used by this thread and send the report over the provided channel.
CollectMemoryReport(ReportsChan),
}