Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the problem that /debug/pprof/heap will wait 10s #403

Merged
merged 16 commits into from
Dec 16, 2024
30 changes: 4 additions & 26 deletions proxy_components/proxy_server/src/status_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) => {
Expand All @@ -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" {
JaySon-Huang marked this conversation as resolved.
Show resolved Hide resolved
response.header("Content-Type", mime::IMAGE_SVG.to_string())
} else {
response.header("Content-Type", mime::APPLICATION_OCTET_STREAM.to_string())
Expand Down
16 changes: 16 additions & 0 deletions proxy_components/proxy_server/src/status_server/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>, 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 {
Expand Down
Loading