diff --git a/proxy_components/proxy_server/src/status_server/mod.rs b/proxy_components/proxy_server/src/status_server/mod.rs index 036960127e9..f38f951f862 100644 --- a/proxy_components/proxy_server/src/status_server/mod.rs +++ b/proxy_components/proxy_server/src/status_server/mod.rs @@ -42,8 +42,8 @@ use openssl::{ }; use pin_project::pin_project; use profile::{ - activate_heap_profile, deactivate_heap_profile, jeprof_heap_profile, list_heap_profiles, - read_file, start_one_cpu_profile, start_one_heap_profile, + activate_heap_profile, deactivate_heap_profile, dump_one_heap_profile, list_heap_profiles, + start_one_cpu_profile, }; use raftstore::store::{transport::CasualRouter, CasualMessage}; use regex::Regex; @@ -270,29 +270,7 @@ where } .to_string(); - let result = if let Some(name) = query_pairs.get("name") { - if use_jeprof { - jeprof_heap_profile(name, output_format) - } else { - read_file(name) - } - } else { - let mut seconds = 10; - if let Some(s) = query_pairs.get("seconds") { - match s.parse() { - Ok(val) => seconds = val, - Err(_) => { - let errmsg = "request should have seconds argument".to_owned(); - return Ok(make_response(StatusCode::BAD_REQUEST, errmsg)); - } - } - } - let timer = GLOBAL_TIMER_HANDLE.delay(Instant::now() + Duration::from_secs(seconds)); - let end = Compat01As03::new(timer) - .map_err(|_| TIMER_CANCELED.to_owned()) - .into_future(); - start_one_heap_profile(end, use_jeprof, output_format).await - }; + let result = dump_one_heap_profile(use_jeprof, output_format.clone()); match result { Ok(body) => { @@ -301,7 +279,7 @@ where .header("X-Content-Type-Options", "nosniff") .header("Content-Disposition", "attachment; filename=\"profile\"") .header("Content-Length", body.len()); - response = if use_jeprof { + response = if use_jeprof && output_format == "--svg" { response.header("Content-Type", mime::IMAGE_SVG.to_string()) } else { response.header("Content-Type", mime::APPLICATION_OCTET_STREAM.to_string()) diff --git a/proxy_components/proxy_server/src/status_server/profile.rs b/proxy_components/proxy_server/src/status_server/profile.rs index 2459fa499c4..96abdcc2410 100644 --- a/proxy_components/proxy_server/src/status_server/profile.rs +++ b/proxy_components/proxy_server/src/status_server/profile.rs @@ -132,6 +132,22 @@ where ProfileGuard::new(on_start, on_end, end.boxed())?.await } +/// Trigger a heap profie and return the content. +#[allow(dead_code)] +pub fn dump_one_heap_profile(use_jeprof: bool, output_format: String) -> Result, String> { + info!("dump_one_heap_profile"; "use_jeprof" => use_jeprof, "output_format" => &output_format); + let f = NamedTempFile::new().map_err(|e| format!("create tmp file fail: {}", e))?; + let path = f.path().to_str().unwrap(); + dump_prof(path).map_err(|e| format!("dump_prof: {}", e))?; + if use_jeprof { + // Use jeprof to transform heap file into svg/raw/collapsed... + jeprof_heap_profile(path, output_format) + } else { + // Juse return the heap file. + read_file(path) + } +} + pub fn set_prof_active(val: bool) -> Result<(), String> { let activate = has_activate_prof(); if activate == val {