Miniprofiler (#42385)
Release Notes: - Added hang detection and a built in performance profiler
This commit is contained in:
@@ -37,7 +37,8 @@ use std::{
|
||||
io::{self, IsTerminal},
|
||||
path::{Path, PathBuf},
|
||||
process,
|
||||
sync::Arc,
|
||||
sync::{Arc, OnceLock},
|
||||
time::Instant,
|
||||
};
|
||||
use theme::{ActiveTheme, GlobalTheme, ThemeRegistry};
|
||||
use util::{ResultExt, TryFutureExt, maybe};
|
||||
@@ -162,7 +163,11 @@ fn fail_to_open_window(e: anyhow::Error, _cx: &mut App) {
|
||||
}
|
||||
}
|
||||
|
||||
pub static STARTUP_TIME: OnceLock<Instant> = OnceLock::new();
|
||||
|
||||
pub fn main() {
|
||||
STARTUP_TIME.get_or_init(|| Instant::now());
|
||||
|
||||
#[cfg(unix)]
|
||||
util::prevent_root_execution();
|
||||
|
||||
@@ -637,6 +642,7 @@ pub fn main() {
|
||||
zeta::init(cx);
|
||||
inspector_ui::init(app_state.clone(), cx);
|
||||
json_schema_store::init(cx);
|
||||
miniprofiler_ui::init(*STARTUP_TIME.get().unwrap(), cx);
|
||||
|
||||
cx.observe_global::<SettingsStore>({
|
||||
let http = app_state.client.http_client();
|
||||
@@ -1226,6 +1232,7 @@ fn init_paths() -> HashMap<io::ErrorKind, Vec<&'static Path>> {
|
||||
paths::database_dir(),
|
||||
paths::logs_dir(),
|
||||
paths::temp_dir(),
|
||||
paths::hang_traces_dir(),
|
||||
]
|
||||
.into_iter()
|
||||
.fold(HashMap::default(), |mut errors, path| {
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
use anyhow::{Context as _, Result};
|
||||
use client::{TelemetrySettings, telemetry::MINIDUMP_ENDPOINT};
|
||||
use futures::AsyncReadExt;
|
||||
use gpui::{App, AppContext as _};
|
||||
use gpui::{App, AppContext as _, SerializedThreadTaskTimings};
|
||||
use http_client::{self, HttpClient, HttpClientWithUrl};
|
||||
use log::info;
|
||||
use project::Project;
|
||||
use proto::{CrashReport, GetCrashFilesResponse};
|
||||
use reqwest::multipart::{Form, Part};
|
||||
use settings::Settings;
|
||||
use smol::stream::StreamExt;
|
||||
use std::{ffi::OsStr, fs, sync::Arc};
|
||||
use std::{ffi::OsStr, fs, sync::Arc, thread::ThreadId, time::Duration};
|
||||
use util::ResultExt;
|
||||
|
||||
use crate::STARTUP_TIME;
|
||||
|
||||
pub fn init(http_client: Arc<HttpClientWithUrl>, installation_id: Option<String>, cx: &mut App) {
|
||||
monitor_hangs(cx);
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
monitor_main_thread_hangs(http_client.clone(), installation_id.clone(), cx);
|
||||
|
||||
@@ -272,6 +277,94 @@ pub fn monitor_main_thread_hangs(
|
||||
.detach()
|
||||
}
|
||||
|
||||
fn monitor_hangs(cx: &App) {
|
||||
let main_thread_id = std::thread::current().id();
|
||||
|
||||
let foreground_executor = cx.foreground_executor();
|
||||
let background_executor = cx.background_executor();
|
||||
|
||||
// 3 seconds hang
|
||||
let (mut tx, mut rx) = futures::channel::mpsc::channel(3);
|
||||
foreground_executor
|
||||
.spawn(async move { while (rx.next().await).is_some() {} })
|
||||
.detach();
|
||||
|
||||
background_executor
|
||||
.spawn({
|
||||
let background_executor = background_executor.clone();
|
||||
async move {
|
||||
let mut hang_time = None;
|
||||
|
||||
let mut hanging = false;
|
||||
loop {
|
||||
background_executor.timer(Duration::from_secs(1)).await;
|
||||
match tx.try_send(()) {
|
||||
Ok(_) => {
|
||||
hang_time = None;
|
||||
hanging = false;
|
||||
continue;
|
||||
}
|
||||
Err(e) => {
|
||||
let is_full = e.into_send_error().is_full();
|
||||
if is_full && !hanging {
|
||||
hanging = true;
|
||||
hang_time = Some(chrono::Local::now());
|
||||
}
|
||||
|
||||
if is_full {
|
||||
save_hang_trace(
|
||||
main_thread_id,
|
||||
&background_executor,
|
||||
hang_time.unwrap(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
fn save_hang_trace(
|
||||
main_thread_id: ThreadId,
|
||||
background_executor: &gpui::BackgroundExecutor,
|
||||
hang_time: chrono::DateTime<chrono::Local>,
|
||||
) {
|
||||
let thread_timings = background_executor.dispatcher.get_all_timings();
|
||||
let thread_timings = thread_timings
|
||||
.into_iter()
|
||||
.map(|mut timings| {
|
||||
if timings.thread_id == main_thread_id {
|
||||
timings.thread_name = Some("main".to_string());
|
||||
}
|
||||
|
||||
SerializedThreadTaskTimings::convert(*STARTUP_TIME.get().unwrap(), timings)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let trace_path = paths::hang_traces_dir().join(&format!(
|
||||
"hang-{}.miniprof",
|
||||
hang_time.format("%Y-%m-%d_%H-%M-%S")
|
||||
));
|
||||
|
||||
let Some(timings) = serde_json::to_string(&thread_timings)
|
||||
.context("hang timings serialization")
|
||||
.log_err()
|
||||
else {
|
||||
return;
|
||||
};
|
||||
|
||||
std::fs::write(&trace_path, timings)
|
||||
.context("hang trace file writing")
|
||||
.log_err();
|
||||
|
||||
info!(
|
||||
"hang detected, trace file saved at: {}",
|
||||
trace_path.display()
|
||||
);
|
||||
}
|
||||
|
||||
pub async fn upload_previous_minidumps(
|
||||
http: Arc<HttpClientWithUrl>,
|
||||
installation_id: Option<String>,
|
||||
|
||||
Reference in New Issue
Block a user