From 6cfe4f375ddcba62bfe40bfcec93ae307b92639c Mon Sep 17 00:00:00 2001 From: Vladimir Vukicevic Date: Sun, 21 Apr 2024 13:34:51 -0700 Subject: [PATCH] Support marker file paths with thread ID (#143) --- samply/src/mac/task_profiler.rs | 16 +++++++++++++--- samply/src/mac/thread_profiler.rs | 4 ++-- samply/src/shared/marker_file.rs | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/samply/src/mac/task_profiler.rs b/samply/src/mac/task_profiler.rs index 472f5a80..69529fbd 100644 --- a/samply/src/mac/task_profiler.rs +++ b/samply/src/mac/task_profiler.rs @@ -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}; @@ -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)); } } } diff --git a/samply/src/mac/thread_profiler.rs b/samply/src/mac/thread_profiler.rs index 82e94a8e..22ff3447 100644 --- a/samply/src/mac/thread_profiler.rs +++ b/samply/src/mac/thread_profiler.rs @@ -25,8 +25,8 @@ use super::thread_info::{ pub struct ThreadProfiler { thread_act: thread_act_t, name: Option, - 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, diff --git a/samply/src/shared/marker_file.rs b/samply/src/shared/marker_file.rs index cf7097fa..8cd53ea6 100644 --- a/samply/src/shared/marker_file.rs +++ b/samply/src/shared/marker_file.rs @@ -57,6 +57,24 @@ impl Iterator for MarkerFile { } } +pub struct MarkerFileInfo { + pub prefix: String, + pub pid: u32, + pub tid: Option, +} + +#[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>,