Skip to content

Commit

Permalink
Don't merge the ETL files.
Browse files Browse the repository at this point in the history
This saves some time during `xperf -stop` because we now run it without
the `-d` argument.
  • Loading branch information
mstange committed Jul 15, 2024
1 parent af11492 commit dbb6a4b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 35 deletions.
21 changes: 16 additions & 5 deletions samply/src/windows/elevated_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ impl ElevatedRecordingProps {
#[allow(clippy::enum_variant_names)]
enum ElevatedHelperReplyMsg {
AckStartXperf,
AckStopXperf(PathBuf),
AckStopXperf {
kernel_output_file: PathBuf,
user_output_file: Option<PathBuf>,
},
AckGetKernelModules,
}

Expand Down Expand Up @@ -103,12 +106,17 @@ impl ElevatedHelperSession {
}
}

pub fn stop_xperf(&mut self) -> Result<PathBuf, Box<dyn Error + Send + Sync>> {
pub fn stop_xperf(
&mut self,
) -> Result<(PathBuf, Option<PathBuf>), Box<dyn Error + Send + Sync>> {
let reply = self
.elevated_session
.send_msg_and_wait_for_response(ElevatedHelperRequestMsg::StopXperf)?;
match reply {
ElevatedHelperReplyMsg::AckStopXperf(path) => Ok(path),
ElevatedHelperReplyMsg::AckStopXperf {
kernel_output_file,
user_output_file,
} => Ok((kernel_output_file, user_output_file)),
other_msg => Err(format!("Unexpected reply to StartXperf msg: {other_msg:?}").into()),
}
}
Expand Down Expand Up @@ -195,8 +203,11 @@ impl UtilityProcessChild<ElevatedHelperRequestMsg, ElevatedHelperReplyMsg> for E
Ok(ElevatedHelperReplyMsg::AckStartXperf)
}
ElevatedHelperRequestMsg::StopXperf => {
let output_file = self.xperf.stop_xperf()?;
Ok(ElevatedHelperReplyMsg::AckStopXperf(output_file))
let (kernel_output_file, user_output_file) = self.xperf.stop_xperf()?;
Ok(ElevatedHelperReplyMsg::AckStopXperf {
kernel_output_file,
user_output_file,
})
}
ElevatedHelperRequestMsg::GetKernelModules => Err("todo".into()),
}
Expand Down
25 changes: 20 additions & 5 deletions samply/src/windows/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub fn start_recording(

eprintln!("Stopping xperf...");

let merged_etl = elevated_helper
let (kernel_output_file, user_output_file) = elevated_helper
.stop_xperf()
.expect("Should have produced a merged ETL file");

Expand All @@ -144,7 +144,11 @@ pub fn start_recording(
let unstable_presymbolicate = profile_creation_props.unstable_presymbolicate;
let mut context =
ProfileContext::new(profile, &arch, included_processes, profile_creation_props);
etw_gecko::process_etl_files(&mut context, &merged_etl, &[]);
let extra_etls = match &user_output_file {
Some(user_etl) => vec![user_etl.clone()],
None => Vec::new(),
};
etw_gecko::process_etl_files(&mut context, &kernel_output_file, &extra_etls);

if let Some(win_version) = winver::WindowsVersion::detect() {
context.set_os_name(&format!("Windows {win_version}"))
Expand All @@ -153,14 +157,25 @@ pub fn start_recording(
let profile = context.finish();

if !recording_props.keep_etl {
std::fs::remove_file(&merged_etl).unwrap_or_else(|_| {
std::fs::remove_file(&kernel_output_file).unwrap_or_else(|_| {
panic!(
"Failed to delete ETL file {:?}",
merged_etl.to_str().unwrap()
kernel_output_file.to_str().unwrap()
)
});
if let Some(user_output_file) = &user_output_file {
std::fs::remove_file(user_output_file).unwrap_or_else(|_| {
panic!(
"Failed to delete ETL file {:?}",
user_output_file.to_str().unwrap()
)
});
}
} else {
eprintln!("ETL path: {:?}", merged_etl);
eprintln!("ETL path: {}", kernel_output_file.to_str().unwrap());
if let Some(user_output_file) = &user_output_file {
eprintln!("User ETL path: {}", user_output_file.to_str().unwrap());
}
}

save_profile_to_file(&profile, &output_file).expect("Couldn't write JSON");
Expand Down
35 changes: 10 additions & 25 deletions samply/src/windows/xperf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Xperf {
if kernel_etl_file.extension() == Some(OsStr::new("gz")) {
kernel_etl_file.set_extension("");
}
kernel_etl_file.set_extension("unmerged-etl");
kernel_etl_file.set_extension("kernel.etl");

const MIN_INTERVAL_NANOS: u64 = 122100; // 8192 kHz
let interval_nanos = props.interval_nanos.clamp(MIN_INTERVAL_NANOS, u64::MAX);
Expand Down Expand Up @@ -100,8 +100,11 @@ impl Xperf {
xperf.arg(&kernel_etl_file);

let user_etl_file = if !user_providers.is_empty() {
let mut user_etl_file = kernel_etl_file.clone();
user_etl_file.set_extension("user-unmerged-etl");
let mut user_etl_file = output_path.to_owned();
if user_etl_file.extension() == Some(OsStr::new("gz")) {
user_etl_file.set_extension("");
}
user_etl_file.set_extension("user.etl");

xperf.arg("-start");
xperf.arg("SamplySession");
Expand Down Expand Up @@ -131,14 +134,15 @@ impl Xperf {
Ok(())
}

pub fn stop_xperf(&mut self) -> Result<PathBuf, Box<dyn Error + Send + Sync>> {
pub fn stop_xperf(
&mut self,
) -> Result<(PathBuf, Option<PathBuf>), Box<dyn Error + Send + Sync>> {
let prev_state = std::mem::replace(&mut self.state, XperfState::Stopped);
let (kernel_etl, user_etl) = match prev_state {
XperfState::Stopped => return Err("xperf wasn't running, can't stop it".into()),
XperfState::RecordingKernelToFile(kpath) => (kpath, None),
XperfState::RecordingKernelAndUserToFile(kpath, upath) => (kpath, Some(upath)),
};
let merged_etl = kernel_etl.with_extension("etl");

let xperf_path = self.get_xperf_path()?;
let mut xperf = std::process::Command::new(xperf_path);
Expand All @@ -149,32 +153,13 @@ impl Xperf {
xperf.arg("SamplySession");
}

xperf.arg("-d");
xperf.arg(&merged_etl);

let _ = xperf
.status()
.expect("Failed to execute xperf -stop! xperf may still be recording.");

eprintln!("xperf session stopped.");

std::fs::remove_file(&kernel_etl).map_err(|_| {
format!(
"Failed to delete unmerged ETL file {:?}",
kernel_etl.to_str().unwrap()
)
})?;

if let Some(user_etl) = &user_etl {
std::fs::remove_file(user_etl).map_err(|_| {
format!(
"Failed to delete unmerged ETL file {:?}",
user_etl.to_str().unwrap()
)
})?;
}

Ok(merged_etl)
Ok((kernel_etl, user_etl))
}
}

Expand Down

0 comments on commit dbb6a4b

Please sign in to comment.