diff --git a/CHANGELOG.md b/CHANGELOG.md index b3519d0c..ad2e07e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Remove `backtrace-rs` feature, as the default choice when not specified (#130) +### Added +- Add `sample_timestamp` to Frames and UnresolvedFrames in order to have more fine-grained info on when the samples are collected (#133) +- ### Fixed - Export `UnresolvedReport` type to allow developers to get the unresolved report (#132) diff --git a/src/frames.rs b/src/frames.rs index 83ed61a9..94887585 100644 --- a/src/frames.rs +++ b/src/frames.rs @@ -5,6 +5,7 @@ use std::fmt::{self, Debug, Display, Formatter}; use std::hash::{Hash, Hasher}; use std::os::raw::c_void; use std::path::PathBuf; +use std::time::SystemTime; use smallvec::SmallVec; use symbolic_demangle::demangle; @@ -18,6 +19,7 @@ pub struct UnresolvedFrames { pub thread_name: [u8; MAX_THREAD_NAME], pub thread_name_length: usize, pub thread_id: u64, + pub sample_timestamp: SystemTime, } impl Default for UnresolvedFrames { @@ -28,6 +30,7 @@ impl Default for UnresolvedFrames { thread_name: [0; MAX_THREAD_NAME], thread_name_length: 0, thread_id: 0, + sample_timestamp: SystemTime::now(), } } } @@ -43,6 +46,7 @@ impl UnresolvedFrames { frames: SmallVec<[::Frame; MAX_DEPTH]>, tn: &[u8], thread_id: u64, + sample_timestamp: SystemTime, ) -> Self { let thread_name_length = tn.len(); let mut thread_name = [0; MAX_THREAD_NAME]; @@ -53,6 +57,7 @@ impl UnresolvedFrames { thread_name, thread_name_length, thread_id, + sample_timestamp, } } } @@ -164,6 +169,7 @@ pub struct Frames { pub frames: Vec>, pub thread_name: String, pub thread_id: u64, + pub sample_timestamp: SystemTime, } impl Frames { @@ -210,6 +216,7 @@ impl From for Frames { thread_name: String::from_utf8_lossy(&frames.thread_name[0..frames.thread_name_length]) .into_owned(), thread_id: frames.thread_id, + sample_timestamp: frames.sample_timestamp, } } } diff --git a/src/profiler.rs b/src/profiler.rs index 2e1f03e8..25c7b123 100644 --- a/src/profiler.rs +++ b/src/profiler.rs @@ -2,6 +2,7 @@ use std::convert::TryInto; use std::os::raw::c_int; +use std::time::SystemTime; use nix::sys::signal; use once_cell::sync::Lazy; @@ -284,6 +285,8 @@ extern "C" fn perf_signal_handler( let mut bt: SmallVec<[::Frame; MAX_DEPTH]> = SmallVec::with_capacity(MAX_DEPTH); let mut index = 0; + + let sample_timestamp: SystemTime = SystemTime::now(); TraceImpl::trace(ucontext, |frame| { let ip = Frame::ip(frame); if profiler.is_blocklisted(ip) { @@ -306,7 +309,7 @@ extern "C" fn perf_signal_handler( write_thread_name(current_thread, &mut name); let name = unsafe { std::ffi::CStr::from_ptr(name_ptr) }; - profiler.sample(bt, name.to_bytes(), current_thread as u64); + profiler.sample(bt, name.to_bytes(), current_thread as u64, sample_timestamp); } } } @@ -392,8 +395,9 @@ impl Profiler { backtrace: SmallVec<[::Frame; MAX_DEPTH]>, thread_name: &[u8], thread_id: u64, + sample_timestamp: SystemTime, ) { - let frames = UnresolvedFrames::new(backtrace, thread_name, thread_id); + let frames = UnresolvedFrames::new(backtrace, thread_name, thread_id, sample_timestamp); self.sample_counter += 1; if let Ok(()) = self.data.add(frames, 1) {}