Skip to content
Open
Show file tree
Hide file tree
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
25 changes: 25 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub struct Config {
#[doc(hidden)]
pub include_thread_ids: bool,
#[doc(hidden)]
pub collect_thread_names: bool,
#[doc(hidden)]
pub subprocesses: bool,
#[doc(hidden)]
pub gil_only: bool,
Expand Down Expand Up @@ -126,6 +128,7 @@ impl Default for Config {
gil_only: false,
include_idle: false,
include_thread_ids: false,
collect_thread_names: true,
hide_progress: false,
capture_output: true,
dump_json: false,
Expand Down Expand Up @@ -447,6 +450,11 @@ impl Config {

config.subprocesses = matches.get_flag("subprocesses");
config.command = subcommand.to_owned();
config.collect_thread_names = match subcommand {
"record" => config.include_thread_ids || config.format == Some(FileFormat::speedscope),
"top" => false,
_ => true,
};

// options that can be shared between subcommands
config.pid = matches.get_one::<String>("pid").map(|p| {
Expand Down Expand Up @@ -558,11 +566,26 @@ mod tests {
assert_eq!(config.include_idle, false);
assert_eq!(config.gil_only, false);
assert_eq!(config.include_thread_ids, false);
assert_eq!(config.collect_thread_names, false);

let config_flags = get_config("py-spy r -p 1234 -o foo --idle --gil --threads").unwrap();
assert_eq!(config_flags.include_idle, true);
assert_eq!(config_flags.gil_only, true);
assert_eq!(config_flags.include_thread_ids, true);
assert_eq!(config_flags.collect_thread_names, true);

let speedscope = get_config("py-spy record -p 1234 -f speedscope").unwrap();
assert_eq!(speedscope.collect_thread_names, true);

let raw = get_config("py-spy record -p 1234 -f raw").unwrap();
assert_eq!(raw.collect_thread_names, false);

let chrometrace = get_config("py-spy record -p 1234 -f chrometrace").unwrap();
assert_eq!(chrometrace.collect_thread_names, false);

let chrometrace_threads =
get_config("py-spy record -p 1234 -f chrometrace --threads").unwrap();
assert_eq!(chrometrace_threads.collect_thread_names, true);
}

#[test]
Expand All @@ -571,6 +594,7 @@ mod tests {
let config = get_config("py-spy dump --pid 1234").unwrap();
assert_eq!(config.pid, Some(1234));
assert_eq!(config.command, String::from("dump"));
assert_eq!(config.collect_thread_names, true);

// short version
let short_config = get_config("py-spy d -p 1234").unwrap();
Expand All @@ -589,6 +613,7 @@ mod tests {
let config = get_config("py-spy top --pid 1234").unwrap();
assert_eq!(config.pid, Some(1234));
assert_eq!(config.command, String::from("top"));
assert_eq!(config.collect_thread_names, false);

// short version
let short_config = get_config("py-spy t -p 1234").unwrap();
Expand Down
6 changes: 5 additions & 1 deletion src/python_spy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,11 @@ 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 = if self.config.collect_thread_names {
self._get_python_thread_name(python_thread_id)
} else {
None
};
trace.owns_gil = owns_gil;
trace.pid = self.process.pid;

Expand Down
21 changes: 21 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,27 @@ fn test_thread_names() {
}
}

#[test]
fn test_skip_thread_names() {
#[cfg(target_os = "macos")]
{
// We need root permissions here to run this on OSX
if unsafe { libc::geteuid() } != 0 {
return;
}
}
let config = Config {
collect_thread_names: false,
include_idle: true,
..Default::default()
};
let mut runner = TestRunner::new(config, "./tests/scripts/thread_names.py");

let traces = runner.spy.get_stack_traces().unwrap();
assert_eq!(traces.len(), 11);
assert!(traces.iter().all(|trace| trace.thread_name.is_none()));
}

#[test]
fn test_recursive() {
#[cfg(target_os = "macos")]
Expand Down