1#![cfg_attr(
5 all(feature = "docs", unix),
6 doc = "\n\nSee the [changelog][changelog] for a full release history."
7)]
8#![cfg_attr(feature = "docs", doc = "## Feature flags")]
9#![cfg_attr(feature = "docs", doc = document_features::document_features!())]
10#![cfg_attr(all(coverage_nightly, test), feature(coverage_attribute))]
43#![cfg_attr(docsrs, feature(doc_auto_cfg))]
44#![cfg(unix)]
45#![deny(missing_docs)]
46#![deny(unsafe_code)]
47#![deny(unreachable_pub)]
48
49mod cpu;
50
51pub use cpu::Cpu;
52
53#[derive(Debug, thiserror::Error)]
55pub enum PprofError {
56 #[error(transparent)]
58 Io(#[from] std::io::Error),
59 #[error(transparent)]
61 Pprof(#[from] pprof::Error),
62}
63
64#[cfg(feature = "docs")]
66#[scuffle_changelog::changelog]
67pub mod changelog {}
68
69#[cfg(test)]
70#[cfg_attr(all(coverage_nightly, test), coverage(off))]
71mod tests {
72 use std::io::Read;
73 use std::time::SystemTime;
74
75 use flate2::read::GzDecoder;
76 use pprof::protos::Message;
77
78 use crate::Cpu;
79
80 #[test]
81 fn empty_profile() {
82 let before_nanos = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_nanos() as i64;
83
84 let cpu = Cpu::new::<String>(1000, &[]);
85 let profile = cpu.capture(std::time::Duration::from_secs(1)).unwrap();
86
87 let mut reader = GzDecoder::new(profile.as_slice());
89 let mut buf = Vec::new();
90 reader.read_to_end(&mut buf).unwrap();
91 let profile = pprof::protos::Profile::decode(buf.as_slice()).unwrap();
92
93 assert!(profile.duration_nanos > 1_000_000_000);
94 assert!(profile.time_nanos > before_nanos);
95 let now_nanos = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_nanos() as i64;
96 assert!(profile.time_nanos < now_nanos);
97
98 assert_eq!(profile.string_table[profile.drop_frames as usize], "");
99 assert_eq!(profile.string_table[profile.keep_frames as usize], "");
100
101 let Some(period_type) = profile.period_type else {
102 panic!("missing period type");
103 };
104 assert_eq!(profile.string_table[period_type.ty as usize], "cpu");
105 assert_eq!(profile.string_table[period_type.unit as usize], "nanoseconds");
106
107 assert_eq!(profile.period, 1_000_000);
108 }
109}