Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/python_spy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct PythonSpy {
pub native: Option<NativeStack>,
pub short_filenames: HashMap<String, Option<String>>,
pub python_thread_ids: HashMap<u64, Tid>,
pub python_thread_names: HashMap<u64, String>,
pub python_thread_names: HashMap<u64, Option<String>>,
#[cfg(target_os = "linux")]
pub dockerized: bool,
}
Expand Down Expand Up @@ -232,6 +232,8 @@ impl PythonSpy {
let gil_thread_id = get_gil_threadid::<I, Process>(self.threadstate_address, &self.process)
.context("failed to get gil_thread_id")?;

let mut did_thread_name_lookup = false;

let mut traces = Vec::new();
let mut threads = interp.head();
while !threads.is_null() {
Expand Down Expand Up @@ -278,7 +280,8 @@ impl PythonSpy {
trace.os_thread_id = os_thread_id.map(|id| id as u64);
}

trace.thread_name = self._get_python_thread_name(python_thread_id);
trace.thread_name =
self._get_python_thread_name(&mut did_thread_name_lookup, python_thread_id);
trace.owns_gil = owns_gil;
trace.pid = self.process.pid;

Expand Down Expand Up @@ -527,12 +530,29 @@ impl PythonSpy {
Ok(None)
}

fn _get_python_thread_name(&mut self, python_thread_id: u64) -> Option<String> {
fn _get_python_thread_name(
&mut self,
did_thread_name_lookup: &mut bool,
python_thread_id: u64,
) -> Option<String> {
match self.python_thread_names.get(&python_thread_id) {
Some(thread_name) => Some(thread_name.clone()),
Some(thread_name) => thread_name.clone(),
None => {
self.python_thread_names = thread_name_lookup(self).unwrap_or_default();
self.python_thread_names.get(&python_thread_id).cloned()
// avoid triggering a lookup if we already did one during this sample
if !*did_thread_name_lookup {
info!("looking up thread names");
self.python_thread_names = thread_name_lookup(self)
.unwrap_or_default()
.into_iter()
.map(|(k, v)| (k, Some(v)))
.collect();
*did_thread_name_lookup = true;
}
// avoid triggering a lookup next time if the thread has no name
self.python_thread_names
.entry(python_thread_id)
.or_insert(None)
.clone()
}
}
}
Expand Down
Loading