Skip to repository content68 lines · 2.0 KB · rust
tenant.openagents/omega
No repository description is available.
OpenAgents Git authority 2026-07-28T06:52:18.778Z Public web read
NIP-34 coordinate
30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omegaMaintainersHidden in public view
References2 branches · 1 tag
Read-only clone
git clone https://openagents.com/git/tenant.openagents/omega.gitBrowse files
task_traces.rs
1use std::path::PathBuf;
2use std::thread::ThreadId;
3
4use anyhow::Context;
5use gpui::{SerializedThreadTaskTimings, TasksIncluded, profiler};
6use util::ResultExt;
7
8use crate::STARTUP_TIME;
9
10pub fn save_any(main_thread_id: ThreadId) -> Option<PathBuf> {
11 cleanup_old_hang_traces();
12 let thread_timings = gpui::profiler::get_all_timings(TasksIncluded::CompletedAndRunning);
13
14 let thread_timings = thread_timings
15 .into_iter()
16 .map(|mut timings| {
17 if timings.thread_id == main_thread_id {
18 timings.thread_name = Some("main".to_string());
19 }
20
21 SerializedThreadTaskTimings::convert(*STARTUP_TIME.get().unwrap(), timings)
22 })
23 .collect::<Vec<_>>();
24
25 let Some(timings) = serde_json::to_string(&thread_timings)
26 .context("hang timings serialization")
27 .log_err()
28 else {
29 return None;
30 };
31
32 if profiler::trace_enabled() {
33 None
34 } else {
35 cleanup_old_hang_traces();
36 let trace_path = paths::hang_traces_dir().join(&format!(
37 "hang-{}.miniprof.json",
38 chrono::Local::now().format("%Y-%m-%d_%H-%M-%S")
39 ));
40 std::fs::write(&trace_path, timings)
41 .context("hang trace file writing")
42 .log_err();
43 Some(trace_path)
44 }
45}
46
47pub fn cleanup_old_hang_traces() {
48 if let Ok(entries) = std::fs::read_dir(paths::hang_traces_dir()) {
49 let mut files: Vec<_> = entries
50 .filter_map(|entry| entry.ok())
51 .filter(|entry| {
52 entry
53 .path()
54 .extension()
55 .is_some_and(|ext| ext == "json" || ext == "miniprof")
56 })
57 .collect();
58
59 const MAX_HANG_TRACES: usize = 3;
60 if files.len() > MAX_HANG_TRACES {
61 files.sort_by_key(|entry| entry.file_name());
62 for entry in files.iter().take(files.len() - MAX_HANG_TRACES) {
63 std::fs::remove_file(entry.path()).log_err();
64 }
65 }
66 }
67}
68