Skip to content

Commit

Permalink
Adds timing info to each sample
Browse files Browse the repository at this point in the history
  • Loading branch information
viglia committed Jun 6, 2022
1 parent 3fed55a commit 0d0a125
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/frames.rs
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
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 0d0a125

Please sign in to comment.