Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --unlink-aux-files option #142

Merged
merged 2 commits into from
Apr 21, 2024
Merged
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
4 changes: 1 addition & 3 deletions samply/src/import/perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ where
let interpretation = EventInterpretation::divine_from_attrs(attributes);

let mut converter = Converter::<U>::new(
&profile_creation_props.profile_name,
&profile_creation_props,
Some(Box::new(move |name| {
format!("{name} on {host} (perf version {perf_version})")
})),
Expand All @@ -115,8 +115,6 @@ where
cache,
extra_dir,
interpretation.clone(),
profile_creation_props.reuse_threads,
profile_creation_props.fold_recursive_prefix,
);

let mut last_timestamp = 0;
Expand Down
4 changes: 1 addition & 3 deletions samply/src/linux/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ fn make_converter(
};

Converter::<framehop::UnwinderNative<MmapRangeOrVec, framehop::MayAllocateDuringUnwind>>::new(
&profile_creation_props.profile_name,
&profile_creation_props,
None,
HashMap::new(),
machine_info.as_ref().map(|info| info.release.as_str()),
Expand All @@ -365,8 +365,6 @@ fn make_converter(
framehop::CacheNative::new(),
None,
interpretation,
profile_creation_props.reuse_threads,
profile_creation_props.fold_recursive_prefix,
)
}

Expand Down
14 changes: 8 additions & 6 deletions samply/src/linux_shared/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use super::vdso::VdsoObject;

use crate::shared::jit_category_manager::JitCategoryManager;
use crate::shared::process_sample_data::RssStatMember;
use crate::shared::recording_props::ProfileCreationProps;
use crate::shared::timestamp_converter::TimestampConverter;
use crate::shared::types::{StackFrame, StackMode};
use crate::shared::unresolved_samples::{
Expand Down Expand Up @@ -83,7 +84,7 @@ where
{
#[allow(clippy::too_many_arguments)]
pub fn new(
product: &str,
profile_creation_props: &ProfileCreationProps,
delayed_product_name_generator: Option<BoxedProductNameGenerator>,
build_ids: HashMap<DsoKey, DsoInfo>,
linux_version: Option<&str>,
Expand All @@ -92,15 +93,13 @@ where
cache: U::Cache,
extra_binary_artifact_dir: Option<&Path>,
interpretation: EventInterpretation,
reuse_threads: bool,
fold_recursive_prefix: bool,
) -> Self {
let interval = match interpretation.sampling_is_time_based {
Some(nanos) => SamplingInterval::from_nanos(nanos),
None => SamplingInterval::from_millis(1),
};
let profile = Profile::new(
product,
&profile_creation_props.profile_name,
ReferenceTimestamp::from_system_time(SystemTime::now()),
interval,
);
Expand All @@ -124,7 +123,10 @@ where
Self {
profile,
cache,
processes: Processes::new(reuse_threads),
processes: Processes::new(
profile_creation_props.reuse_threads,
profile_creation_props.unlink_aux_files,
),
timestamp_converter,
current_sample_time: first_sample_time,
build_ids,
Expand All @@ -140,7 +142,7 @@ where
kernel_symbols,
pe_mappings: PeMappings::new(),
jit_category_manager: JitCategoryManager::new(),
fold_recursive_prefix,
fold_recursive_prefix: profile_creation_props.fold_recursive_prefix,
}
}

Expand Down
6 changes: 4 additions & 2 deletions samply/src/linux_shared/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ where
name: Option<String>,
thread_recycler: Option<ThreadRecycler>,
jit_function_recycler: Option<JitFunctionRecycler>,
unlink_aux_files: bool,
) -> Self {
Self {
profile_process: process_handle,
unwinder: U::default(),
jitdump_manager: JitDumpManager::new(),
jitdump_manager: JitDumpManager::new(unlink_aux_files),
lib_mapping_ops: Default::default(),
name,
pid,
Expand Down Expand Up @@ -181,7 +182,8 @@ where
None
};

let jitdump_manager = std::mem::replace(&mut self.jitdump_manager, JitDumpManager::new());
let jitdump_manager =
std::mem::replace(&mut self.jitdump_manager, JitDumpManager::new(false));
let jitdump_ops = jitdump_manager.finish(
jit_category_manager,
profile,
Expand Down
9 changes: 8 additions & 1 deletion samply/src/linux_shared/processes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ where

/// The sample data for all removed processes.
process_sample_datas: Vec<ProcessSampleData>,

/// Whether aux files (like jitdump) should be unlinked on open
unlink_aux_data: bool,
}

impl<U> Processes<U>
where
U: Unwinder + Default,
{
pub fn new(allow_reuse: bool) -> Self {
pub fn new(allow_reuse: bool, unlink_aux_data: bool) -> Self {
let process_recycler = if allow_reuse {
Some(ProcessRecycler::new())
} else {
Expand All @@ -41,6 +44,7 @@ where
processes_by_pid: HashMap::new(),
process_recycler,
process_sample_datas: Vec::new(),
unlink_aux_data,
}
}

Expand Down Expand Up @@ -70,6 +74,7 @@ where
name,
Some(thread_recycler),
Some(jit_function_recycler),
self.unlink_aux_data,
);
return entry.insert(process);
}
Expand Down Expand Up @@ -101,6 +106,7 @@ where
name,
thread_recycler,
jit_function_recycler,
self.unlink_aux_data,
);
entry.insert(process)
}
Expand Down Expand Up @@ -130,6 +136,7 @@ where
None, // no name
thread_recycler,
jit_function_recycler,
self.unlink_aux_data,
)
})
}
Expand Down
5 changes: 4 additions & 1 deletion samply/src/mac/task_profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ impl TaskProfiler {
ignored_errors: Vec::new(),
unwinder: UnwinderNative::new(),
path_receiver,
jitdump_manager: JitDumpManager::new(),
jitdump_manager: JitDumpManager::new(profile_creation_props.unlink_aux_files),
marker_file_paths: Vec::new(),
lib_mapping_ops: Default::default(),
unresolved_samples: Default::default(),
Expand Down Expand Up @@ -602,6 +602,9 @@ impl TaskProfiler {
name: span.name,
}
}));
if self.profile_creation_props.unlink_aux_files {
std::fs::remove_file(marker_file_path).ok();
}
}
}
let process_sample_data = ProcessSampleData::new(
Expand Down
9 changes: 9 additions & 0 deletions samply/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ pub struct ProfileCreationArgs {
/// Fold repeated frames at the base of the stack.
#[arg(long)]
fold_recursive_prefix: bool,

/// If a process produces jitdump or marker files, unlink them after
/// opening. This ensures that the files will not be left in /tmp,
/// but it will also be impossible to look at JIT disassembly, and line
/// numbers will be missing for JIT frames.
#[arg(long)]
unlink_aux_files: bool,
}

fn main() {
Expand Down Expand Up @@ -293,6 +300,7 @@ impl ImportArgs {
profile_name,
reuse_threads: self.profile_creation_args.reuse_threads,
fold_recursive_prefix: self.profile_creation_args.fold_recursive_prefix,
unlink_aux_files: self.profile_creation_args.unlink_aux_files,
}
}
}
Expand Down Expand Up @@ -371,6 +379,7 @@ impl RecordArgs {
profile_name,
reuse_threads: self.profile_creation_args.reuse_threads,
fold_recursive_prefix: self.profile_creation_args.fold_recursive_prefix,
unlink_aux_files: self.profile_creation_args.unlink_aux_files,
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions samply/src/shared/jitdump_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ use super::utils::open_file_with_fallback;
pub struct JitDumpManager {
pending_jitdump_paths: Vec<(ThreadHandle, PathBuf, Option<PathBuf>)>,
processors: Vec<SingleJitDumpProcessor>,
unlink_after_open: bool,
}

impl JitDumpManager {
pub fn new() -> Self {
pub fn new(unlink_after_open: bool) -> Self {
JitDumpManager {
pending_jitdump_paths: Vec::new(),
processors: Vec::new(),
unlink_after_open,
}
}

Expand All @@ -51,13 +53,17 @@ impl JitDumpManager {
fn jitdump_reader_for_path(
path: &Path,
fallback_dir: Option<&Path>,
unlink_after_open: bool,
) -> Option<(JitDumpReader<std::fs::File>, PathBuf)> {
let (file, path) = open_file_with_fallback(path, fallback_dir).ok()?;
let reader = JitDumpReader::new(file).ok()?;
if unlink_after_open {
std::fs::remove_file(&path).ok()?;
}
Some((reader, path))
}
let Some((reader, actual_path)) =
jitdump_reader_for_path(path, fallback_dir.as_deref())
jitdump_reader_for_path(path, fallback_dir.as_deref(), self.unlink_after_open)
else {
return true;
};
Expand Down
2 changes: 2 additions & 0 deletions samply/src/shared/recording_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub struct ProfileCreationProps {
pub reuse_threads: bool,
/// Fold repeated frames at the base of the stack.
pub fold_recursive_prefix: bool,
/// Unlink jitdump/marker files
pub unlink_aux_files: bool,
}

/// Properties which are meaningful for launching and recording a fresh process.
Expand Down
Loading