Skip to content

Commit

Permalink
Add --profile-name to record command
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai committed Aug 30, 2023
1 parent fc5b14c commit 422fad0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
7 changes: 4 additions & 3 deletions samply/src/linux/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub type ConvertRegsNative = crate::linux_shared::ConvertRegsAarch64;
#[allow(clippy::too_many_arguments)]
pub fn start_recording(
output_file: &Path,
profile_name: Option<String>,
command_name: OsString,
command_args: &[OsString],
time_limit: Option<Duration>,
Expand Down Expand Up @@ -66,10 +67,9 @@ pub fn start_recording(

// Launch the observer thread. This thread will manage the perf events.
let output_file_copy = output_file.to_owned();
let command_name_copy = command_name.to_string_lossy().to_string();
let product = profile_name.unwrap_or_else(|| command_name.to_string_lossy().to_string());
let conversion_args = conversion_args.clone();
let observer_thread = thread::spawn(move || {
let product = command_name_copy;
let mut converter = make_converter(interval, &product, &conversion_args);

// Wait for the initial pid to profile.
Expand Down Expand Up @@ -191,6 +191,7 @@ pub fn start_recording(

pub fn start_profiling_pid(
output_file: &Path,
profile_name: Option<String>,
pid: u32,
time_limit: Option<Duration>,
interval: Duration,
Expand All @@ -215,7 +216,7 @@ pub fn start_profiling_pid(
crossbeam_channel::bounded(2);

let output_file_copy = output_file.to_owned();
let product = format!("PID {pid}");
let product = profile_name.unwrap_or_else(|| format!("PID {pid}"));
let conversion_args = conversion_args.clone();
let observer_thread = thread::spawn({
let stop = stop.clone();
Expand Down
4 changes: 4 additions & 0 deletions samply/src/mac/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::ConversionArgs;

pub fn start_profiling_pid(
_output_file: &Path,
_profile_name: Option<String>,
_pid: u32,
_time_limit: Option<Duration>,
_interval: Duration,
Expand All @@ -36,6 +37,7 @@ pub fn start_profiling_pid(
#[allow(clippy::too_many_arguments)]
pub fn start_recording(
output_file: &Path,
profile_name: Option<String>,
command_name: OsString,
command_args: &[OsString],
time_limit: Option<Duration>,
Expand All @@ -47,9 +49,11 @@ pub fn start_recording(
let (task_sender, task_receiver) = unbounded();
let command_name_copy = command_name.to_string_lossy().to_string();
let conversion_args = conversion_args.clone();
let product_name = profile_name.unwrap_or_else(|| command_name_copy.clone());
let sampler_thread = thread::spawn(move || {
let sampler = Sampler::new(
command_name_copy,
product_name,
task_receiver,
interval,
time_limit,
Expand Down
5 changes: 4 additions & 1 deletion samply/src/mac/sampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct TaskInit {
}

pub struct Sampler {
product_name: String,
command_name: String,
task_receiver: Receiver<TaskInit>,
interval: Duration,
Expand All @@ -37,6 +38,7 @@ pub struct Sampler {
impl Sampler {
pub fn new(
command: String,
product_name: String,
task_receiver: Receiver<TaskInit>,
interval: Duration,
time_limit: Option<Duration>,
Expand All @@ -57,6 +59,7 @@ impl Sampler {

Sampler {
command_name,
product_name,
task_receiver,
interval,
time_limit,
Expand All @@ -75,7 +78,7 @@ impl Sampler {
};

let mut profile = Profile::new(
&self.command_name,
&self.product_name,
ReferenceTimestamp::from_system_time(reference_system_time),
self.interval.into(),
);
Expand Down
7 changes: 7 additions & 0 deletions samply/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ struct RecordArgs {
#[arg(short, long, default_value = "profile.json")]
output: PathBuf,

/// Overwrite the name shown at the top of the profiler results
/// By default it is either the command that was run or the process pid.
#[arg(long)]
profile_name: Option<String>,

/// How many times to run the profiled command.
#[arg(long, default_value = "1")]
iteration_count: u32,
Expand Down Expand Up @@ -191,6 +196,7 @@ fn main() {
if let Some(pid) = record_args.pid {
profiler::start_profiling_pid(
&record_args.output,
record_args.profile_name,
pid,
time_limit,
interval,
Expand All @@ -200,6 +206,7 @@ fn main() {
} else {
let exit_status = match profiler::start_recording(
&record_args.output,
record_args.profile_name,
record_args.command[0].clone(),
&record_args.command[1..],
time_limit,
Expand Down

0 comments on commit 422fad0

Please sign in to comment.