Skip to content

Add support for profiling the compiler with Samply. #1522

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

Merged
merged 1 commit into from
Feb 6, 2023
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
3 changes: 3 additions & 0 deletions ci/check-profiling.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ cargo build -p collector --bin rustc-fake
# oprofile: untested... it's not used much, and might have the same problems
# that `perf` has due to virtualized hardware.

# samply: untested because it has the same problems that `perf` has due to
# virtualized hardware.

# Cachegrind.
RUST_BACKTRACE=1 RUST_LOG=raw_cargo_messages=trace,collector=debug,rust_sysroot=debug \
cargo run -p collector --bin collector -- \
Expand Down
7 changes: 7 additions & 0 deletions collector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@ The mandatory `<PROFILER>` argument must be one of the following.
- **Notes**. OProfile fails moderately often with this message: "operf-record
process killed by signal 13". The failures seem to be random; re-running
often results in success.
- `samply`: Profile with [Samply](https://github.com/mstange/samply/), a
sampling profiler.
- **Purpose**. Samply is a general-purpose profiler, good for seeing
where execution time is spent and finding hot functions.
- **Slowdown**. Negligible.
- **Output**. Binary output is written to a file with a `samply` prefix.
That file can be loaded with `samply load`.
- `cachegrind`: Profile with
[Cachegrind](http://valgrind.org/docs/manual/cg-manual.html), a tracing
profiler. Requires Valgrind 3.15 or later.
Expand Down
13 changes: 13 additions & 0 deletions collector/src/bin/rustc-fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ fn main() {
run_with_determinism_env(cmd);
}

"Samply" => {
let mut cmd = Command::new("samply");
let has_samply = cmd.output().is_ok();
assert!(has_samply);
cmd.arg("record")
.arg("--no-open")
.arg("--save-only")
.arg(&tool)
.args(&args);

run_with_determinism_env(cmd);
}

"Cachegrind" => {
let mut cmd = Command::new("valgrind");
let has_valgrind = cmd.output().is_ok();
Expand Down
2 changes: 2 additions & 0 deletions collector/src/execute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl PerfTool {
| ProfileTool(SelfProfile)
| ProfileTool(PerfRecord)
| ProfileTool(Oprofile)
| ProfileTool(Samply)
| ProfileTool(Cachegrind)
| ProfileTool(Callgrind)
| ProfileTool(Dhat)
Expand Down Expand Up @@ -86,6 +87,7 @@ impl PerfTool {
| ProfileTool(SelfProfile)
| ProfileTool(PerfRecord)
| ProfileTool(Oprofile)
| ProfileTool(Samply)
| ProfileTool(Cachegrind)
| ProfileTool(Callgrind)
| ProfileTool(Dhat)
Expand Down
11 changes: 11 additions & 0 deletions collector/src/execute/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub enum Profiler {
SelfProfile,
PerfRecord,
Oprofile,
Samply,
Cachegrind,
Callgrind,
Dhat,
Expand Down Expand Up @@ -206,6 +207,16 @@ impl<'a> Processor for ProfileProcessor<'a> {
fs::write(opann_file, &op_annotate_cmd.output()?.stdout)?;
}

// Samply produces (via rustc-fake) a data file called
// `profile.json`. We copy it from the temp dir to the output dir,
// giving it a new name in the process.
Profiler::Samply => {
let tmp_samply_file = filepath(data.cwd.as_ref(), "profile.json");
let samply_file = filepath(self.output_dir, &out_file("samply"));

fs::copy(&tmp_samply_file, &samply_file)?;
}

// Cachegrind produces (via rustc-fake) a data file called `cgout`.
// We copy it from the temp dir to the output dir, giving it a new
// name in the process, and then post-process it to produce another
Expand Down