Skip to content

Commit 10cdee3

Browse files
authored
Merge pull request #1522 from nnethercote/add-samply
Add support for profiling the compiler with Samply.
2 parents 57d72e1 + 59a4092 commit 10cdee3

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

ci/check-profiling.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ cargo build -p collector --bin rustc-fake
3939
# oprofile: untested... it's not used much, and might have the same problems
4040
# that `perf` has due to virtualized hardware.
4141

42+
# samply: untested because it has the same problems that `perf` has due to
43+
# virtualized hardware.
44+
4245
# Cachegrind.
4346
RUST_BACKTRACE=1 RUST_LOG=raw_cargo_messages=trace,collector=debug,rust_sysroot=debug \
4447
cargo run -p collector --bin collector -- \

collector/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@ The mandatory `<PROFILER>` argument must be one of the following.
289289
- **Notes**. OProfile fails moderately often with this message: "operf-record
290290
process killed by signal 13". The failures seem to be random; re-running
291291
often results in success.
292+
- `samply`: Profile with [Samply](https://github.com/mstange/samply/), a
293+
sampling profiler.
294+
- **Purpose**. Samply is a general-purpose profiler, good for seeing
295+
where execution time is spent and finding hot functions.
296+
- **Slowdown**. Negligible.
297+
- **Output**. Binary output is written to a file with a `samply` prefix.
298+
That file can be loaded with `samply load`.
292299
- `cachegrind`: Profile with
293300
[Cachegrind](http://valgrind.org/docs/manual/cg-manual.html), a tracing
294301
profiler. Requires Valgrind 3.15 or later.

collector/src/bin/rustc-fake.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,19 @@ fn main() {
230230
run_with_determinism_env(cmd);
231231
}
232232

233+
"Samply" => {
234+
let mut cmd = Command::new("samply");
235+
let has_samply = cmd.output().is_ok();
236+
assert!(has_samply);
237+
cmd.arg("record")
238+
.arg("--no-open")
239+
.arg("--save-only")
240+
.arg(&tool)
241+
.args(&args);
242+
243+
run_with_determinism_env(cmd);
244+
}
245+
233246
"Cachegrind" => {
234247
let mut cmd = Command::new("valgrind");
235248
let has_valgrind = cmd.output().is_ok();

collector/src/execute/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ impl PerfTool {
5151
| ProfileTool(SelfProfile)
5252
| ProfileTool(PerfRecord)
5353
| ProfileTool(Oprofile)
54+
| ProfileTool(Samply)
5455
| ProfileTool(Cachegrind)
5556
| ProfileTool(Callgrind)
5657
| ProfileTool(Dhat)
@@ -86,6 +87,7 @@ impl PerfTool {
8687
| ProfileTool(SelfProfile)
8788
| ProfileTool(PerfRecord)
8889
| ProfileTool(Oprofile)
90+
| ProfileTool(Samply)
8991
| ProfileTool(Cachegrind)
9092
| ProfileTool(Callgrind)
9193
| ProfileTool(Dhat)

collector/src/execute/profiler.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub enum Profiler {
1414
SelfProfile,
1515
PerfRecord,
1616
Oprofile,
17+
Samply,
1718
Cachegrind,
1819
Callgrind,
1920
Dhat,
@@ -206,6 +207,16 @@ impl<'a> Processor for ProfileProcessor<'a> {
206207
fs::write(opann_file, &op_annotate_cmd.output()?.stdout)?;
207208
}
208209

210+
// Samply produces (via rustc-fake) a data file called
211+
// `profile.json`. We copy it from the temp dir to the output dir,
212+
// giving it a new name in the process.
213+
Profiler::Samply => {
214+
let tmp_samply_file = filepath(data.cwd.as_ref(), "profile.json");
215+
let samply_file = filepath(self.output_dir, &out_file("samply"));
216+
217+
fs::copy(&tmp_samply_file, &samply_file)?;
218+
}
219+
209220
// Cachegrind produces (via rustc-fake) a data file called `cgout`.
210221
// We copy it from the temp dir to the output dir, giving it a new
211222
// name in the process, and then post-process it to produce another

0 commit comments

Comments
 (0)