Skip to content

Commit

Permalink
Support marker file paths with thread ID (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
vvuk authored Apr 21, 2024
1 parent 89e7b72 commit 6cfe4f3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
16 changes: 13 additions & 3 deletions samply/src/mac/task_profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::shared::jitdump_manager::JitDumpManager;
use crate::shared::lib_mappings::{
LibMappingAdd, LibMappingInfo, LibMappingOp, LibMappingOpQueue, LibMappingRemove,
};
use crate::shared::marker_file;
use crate::shared::marker_file::get_markers;
use crate::shared::perf_map::try_load_perf_map;
use crate::shared::process_sample_data::{MarkerSpanOnThread, ProcessSampleData};
Expand Down Expand Up @@ -533,10 +534,19 @@ impl TaskProfiler {
);
}
JitdumpOrMarkerPath::MarkerFilePath(marker_file_path) => {
// TODO: Detect which thread the marker file is opened on, and use that thread's
// thread handle so that the markers are put on that thread in the profile.
// count the number of - characters in marker_file_path
let marker_info = marker_file::parse_marker_file_path(&marker_file_path);
let thread_handle = if marker_info.tid.is_some() {
self.live_threads
.iter()
.find(|(_, thread)| thread.tid == marker_info.tid.unwrap())
.map(|(_, thread)| thread.profile_thread)
.unwrap_or(self.main_thread_handle)
} else {
self.main_thread_handle
};
self.marker_file_paths
.push((self.main_thread_handle, marker_file_path));
.push((thread_handle, marker_file_path));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions samply/src/mac/thread_profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use super::thread_info::{
pub struct ThreadProfiler {
thread_act: thread_act_t,
name: Option<String>,
tid: u32,
profile_thread: ThreadHandle,
pub(crate) tid: u32,
pub(crate) profile_thread: ThreadHandle,
tick_count: usize,
stack_memory: ForeignMemory,
previous_sample_cpu_time_us: u64,
Expand Down
18 changes: 18 additions & 0 deletions samply/src/shared/marker_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ impl Iterator for MarkerFile {
}
}

pub struct MarkerFileInfo {
pub prefix: String,
pub pid: u32,
pub tid: Option<u32>,
}

#[allow(unused)]
pub fn parse_marker_file_path(path: &Path) -> MarkerFileInfo {
let filename = path.file_name().unwrap().to_str().unwrap();
// strip .txt extension
let filename = &filename[..filename.len() - 4];
let mut parts = filename.splitn(3, '-');
let prefix = parts.next().unwrap().to_owned();
let pid = parts.next().unwrap().parse().unwrap();
let tid = parts.next().map(|tid| tid.parse().unwrap());
MarkerFileInfo { prefix, pid, tid }
}

pub fn get_markers(
marker_file: &Path,
extra_dir: Option<&Path>,
Expand Down

0 comments on commit 6cfe4f3

Please sign in to comment.