1use std::os::raw::c_int;
17
18use anyhow::bail;
19use pprof::ProfilerGuard;
20use pprof_util::{StackProfile, WeightedStack};
21use tokio::time::{self, Duration};
22
23pub async unsafe fn prof_time(
28 total_time: Duration,
29 sample_freq: u32,
30 merge_threads: bool,
31) -> anyhow::Result<StackProfile> {
32 if sample_freq == 0 {
33 bail!("Sampling frequency must be greater than zero.");
34 }
35 if sample_freq > 1_000_000 {
36 bail!("Sub-microsecond intervals are not supported.");
37 }
38 let sample_freq = c_int::try_from(sample_freq)?;
39 let pg = ProfilerGuard::new(sample_freq)?;
40 time::sleep(total_time).await;
41 let builder = pg.report();
42 let report = builder.build_unresolved()?;
43 let mut profile = StackProfile::default();
44 for (f, weight) in report.data {
45 let thread_name;
46 #[allow(clippy::as_conversions)]
48 let mut addrs: Vec<_> = f.frames.iter().map(|f| f.ip() as usize).collect();
49 addrs.reverse();
50 #[allow(clippy::as_conversions)]
52 let weight = weight as f64;
53 let anno = if merge_threads {
54 None
55 } else {
56 thread_name = String::from_utf8_lossy(&f.thread_name[0..f.thread_name_length]);
57 Some(thread_name.as_ref())
58 };
59 let stack = WeightedStack { addrs, weight };
60 profile.push_stack(stack, anno);
61 }
62
63 Ok(profile)
64}