Skip to content

Commit

Permalink
Adds timing info to each sample (#133)
Browse files Browse the repository at this point in the history
* Adds timing info to each sample

Signed-off-by: Francesco Vigliaturo <[email protected]>

* Updates the CHANGELOG.md

Signed-off-by: Francesco Vigliaturo <[email protected]>

* Add PR info to updated CHANGELOG.md

Signed-off-by: Francesco Vigliaturo <[email protected]>

Co-authored-by: YangKeao <[email protected]>
  • Loading branch information
viglia and YangKeao authored Jun 20, 2022
1 parent 108d0d6 commit ee4022d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions src/frames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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(),
}
}
}
Expand All @@ -43,6 +46,7 @@ impl UnresolvedFrames {
frames: SmallVec<[<TraceImpl as Trace>::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];
Expand All @@ -53,6 +57,7 @@ impl UnresolvedFrames {
thread_name,
thread_name_length,
thread_id,
sample_timestamp,
}
}
}
Expand Down Expand Up @@ -164,6 +169,7 @@ pub struct Frames {
pub frames: Vec<Vec<Symbol>>,
pub thread_name: String,
pub thread_id: u64,
pub sample_timestamp: SystemTime,
}

impl Frames {
Expand Down Expand Up @@ -210,6 +216,7 @@ impl From<UnresolvedFrames> 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,
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -284,6 +285,8 @@ extern "C" fn perf_signal_handler(
let mut bt: SmallVec<[<TraceImpl as Trace>::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) {
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -392,8 +395,9 @@ impl Profiler {
backtrace: SmallVec<[<TraceImpl as Trace>::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) {}
Expand Down

0 comments on commit ee4022d

Please sign in to comment.