Skip to content

Commit 1e9df6d

Browse files
committed
Review
1 parent fe7a0ef commit 1e9df6d

File tree

5 files changed

+60
-7
lines changed

5 files changed

+60
-7
lines changed

crates/api/src/runtime.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,14 @@ pub enum OptLevel {
379379
SpeedAndSize,
380380
}
381381

382-
/// Select which profiling technique to use
382+
/// Select which profiling technique to support.
383383
#[derive(Debug, Clone, Copy)]
384384
pub enum ProfilingStrategy {
385-
/// No profiler support
385+
/// No profiler support.
386386
None,
387387

388-
/// Collect profile for jitdump file format, used with `perf` on Linux
388+
/// Collect profiling info for "jitdump" file format, used with `perf` on
389+
/// Linux.
389390
JitDump,
390391
}
391392

crates/profiling/src/jitdump_linux.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,11 @@ impl ProfilingAgent for JitDumpAgent {
221221

222222
impl State {
223223
/// Returns timestamp from a single source
224-
#[allow(unused_variables)]
225224
fn get_time_stamp(&self) -> u64 {
225+
// We need to use `CLOCK_MONOTONIC` on Linux which is what `Instant`
226+
// conveniently also uses, but `Instant` doesn't allow us to get access
227+
// to nanoseconds as an internal detail, so we calculate the nanoseconds
228+
// ourselves here.
226229
unsafe {
227230
let mut ts = mem::MaybeUninit::zeroed();
228231
assert_eq!(
@@ -231,12 +234,11 @@ impl State {
231234
);
232235
let ts = ts.assume_init();
233236
// TODO: What does it mean for either sec or nsec to be negative?
234-
(ts.tv_sec * 1000000000 + ts.tv_nsec) as u64
237+
(ts.tv_sec * 1_000_000_000 + ts.tv_nsec) as u64
235238
}
236239
}
237240

238241
/// Returns the ELF machine architecture.
239-
#[allow(dead_code)]
240242
fn get_e_machine(&self) -> u32 {
241243
match target_lexicon::HOST.architecture {
242244
Architecture::X86_64 => elf::header::EM_X86_64 as u32,
@@ -247,7 +249,6 @@ impl State {
247249
}
248250
}
249251

250-
#[allow(dead_code)]
251252
fn write_file_header(&mut self) -> Result<()> {
252253
let header = FileHeader {
253254
timestamp: self.get_time_stamp(),

docs/assets/perf-annotate-fib.png

58.3 KB
Loading

docs/assets/perf-report-fib.png

27 KB
Loading

docs/examples-profiling.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,55 @@ You shouldn't need to do anything else to get this information into `perf`. The
9898
perf collection data should automatically pick up all this dwarf debug
9999
information.
100100

101+
### `perf` example
102+
103+
Let's run through a quick example with `perf` to get the feel for things. First
104+
let's take a look at some wasm:
105+
106+
```rust
107+
fn main() {
108+
let n = 42;
109+
println!("fib({}) = {}", n, fib(n));
110+
}
111+
112+
fn fib(n: u32) -> u32 {
113+
if n <= 2 {
114+
1
115+
} else {
116+
fib(n - 1) + fib(n - 2)
117+
}
118+
}
119+
```
120+
121+
To collect perf information for this wasm module we'll execute:
122+
123+
```sh
124+
$ rustc --target wasm32-wasi fib.rs -O
125+
$ perf record -k mono wasmtime --jitdump fib.wasm
126+
fib(42) = 267914296
127+
[ perf record: Woken up 1 times to write data ]
128+
[ perf record: Captured and wrote 0.147 MB perf.data (3435 samples) ]
129+
$ perf inject --jit --input perf.data --output perf.jit.data
130+
```
131+
132+
And we should have all out information now! We can execute `perf report` for
133+
example to see that 99% of our runtime (as expected) is spent in our `fib`
134+
function. Note that the symbol has been demangled to `fib::fib` which is what
135+
the Rust symbol is:
136+
137+
```
138+
$ perf report --input perf.jit-data
139+
```
140+
141+
![perf report output](assets/perf-report-fib.png)
142+
143+
Alternatively we could also use `perf annotate` to take a look at the
144+
disassembly of the `fib` function, seeing what the JIT generated:
145+
146+
```
147+
$ perf annotate --input perf.jit-data
148+
```
149+
150+
![perf annotate output](assets/perf-annotate-fib.png)
151+
101152
[`Config::debug_info`]: https://bytecodealliance.github.io/wasmtime/api/wasmtime/struct.Config.html#method.debug_info

0 commit comments

Comments
 (0)