From 2d3a7c87c2887d851eddc36ce760771ae3e861df Mon Sep 17 00:00:00 2001 From: Narfinger Date: Wed, 28 May 2025 02:58:05 +0200 Subject: [PATCH] Refactor the memory profiler code to return the struct. (#37155) Refactor the memory profiler code to return the struct and handle the serializing in servointernal page. This allows other users of the memory profiler to see the whole report without parsing json. Testing: I do not know if the memory page is covered by tests. --------- Signed-off-by: Narfinger --- components/profile/mem.rs | 20 +++++--------------- components/script/dom/servointernals.rs | 4 +++- components/shared/profile/mem.rs | 15 +++++++++++++-- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/components/profile/mem.rs b/components/profile/mem.rs index 258099b9685..b9920f252b4 100644 --- a/components/profile/mem.rs +++ b/components/profile/mem.rs @@ -12,9 +12,9 @@ use ipc_channel::ipc::{self, IpcReceiver}; use ipc_channel::router::ROUTER; use log::debug; use profile_traits::mem::{ - MemoryReportResult, ProfilerChan, ProfilerMsg, Report, Reporter, ReporterRequest, ReportsChan, + MemoryReport, MemoryReportResult, ProfilerChan, ProfilerMsg, Report, Reporter, ReporterRequest, + ReportsChan, }; -use serde::Serialize; use crate::system_reporter; @@ -100,28 +100,18 @@ impl Profiler { ProfilerMsg::Report(sender) => { let main_pid = std::process::id(); - #[derive(Serialize)] - struct JsonReport { - pid: u32, - #[serde(rename = "isMainProcess")] - is_main_process: bool, - reports: Vec, - } - let reports = self.collect_reports(); // Turn the pid -> reports map into a vector and add the // hint to find the main process. - let json_reports: Vec = reports + let results: Vec = reports .into_iter() - .map(|(pid, reports)| JsonReport { + .map(|(pid, reports)| MemoryReport { pid, reports, is_main_process: pid == main_pid, }) .collect(); - let content = serde_json::to_string(&json_reports) - .unwrap_or_else(|_| "{ error: \"failed to create memory report\"}".to_owned()); - let _ = sender.send(MemoryReportResult { content }); + let _ = sender.send(MemoryReportResult { results }); true }, diff --git a/components/script/dom/servointernals.rs b/components/script/dom/servointernals.rs index eb7260b61be..b104238a8af 100644 --- a/components/script/dom/servointernals.rs +++ b/components/script/dom/servointernals.rs @@ -59,7 +59,9 @@ impl ServoInternalsMethods for ServoInternals { impl RoutedPromiseListener for ServoInternals { fn handle_response(&self, response: MemoryReportResult, promise: &Rc, can_gc: CanGc) { - promise.resolve_native(&response.content, can_gc); + let stringified = serde_json::to_string(&response.results) + .unwrap_or_else(|_| "{ error: \"failed to create memory report\"}".to_owned()); + promise.resolve_native(&stringified, can_gc); } } diff --git a/components/shared/profile/mem.rs b/components/shared/profile/mem.rs index b626facd042..cd378bf32eb 100644 --- a/components/shared/profile/mem.rs +++ b/components/shared/profile/mem.rs @@ -247,8 +247,19 @@ macro_rules! path { /// The results produced by the memory reporter. #[derive(Debug, Deserialize, Serialize)] pub struct MemoryReportResult { - /// The stringified output. - pub content: String, + /// All the results from the MemoryReports + pub results: Vec, +} + +#[derive(Debug, Deserialize, Serialize)] +/// A simple memory report +pub struct MemoryReport { + /// The pid of the report + pub pid: u32, + /// Is this the main process + pub is_main_process: bool, + /// All the reports for this pid + pub reports: Vec, } /// Messages that can be sent to the memory profiler thread.