flamegraph-rs / flamegraph

Easy flamegraphs for Rust projects and everything else, without Perl or pipes <3
Apache License 2.0
4.63k stars 141 forks source link

Running on WSL fails with invalid argument #82

Open Kixiron opened 4 years ago

Kixiron commented 4 years ago

Attempting to run cargo flamegraph yields the following error (Note: #81 also manifests, possibly due to something related to workspacing)

# cargo flamegraph --bin crunch-parser
    Finished release [optimized + debuginfo] target(s) in 0.15s

WARNING: building without debuginfo. Enable symbol information by adding the following lines to Cargo.toml:

[profile.release]
debug = true

perf_event_open(..., 0) failed unexpectedly with error 22 (Invalid argument)
Error:
The sys_perf_event_open() syscall returned with 22 (Invalid argument) for event (cycles).
/bin/dmesg | grep -i perf may provide additional information.

failed to sample program

Rust Version:

# rustc -Vv
rustc 1.42.0 (b8cedc004 2020-03-09)
binary: rustc
commit-hash: b8cedc00407a4c56a3bda1ed605c6fc166655447
commit-date: 2020-03-09
host: x86_64-unknown-linux-gnu
release: 1.42.0
LLVM version: 9.0

# cargo -Vv
cargo 1.42.0 (86334295e 2020-01-31)
release: 1.42.0
commit-hash: 86334295e82ef241fe5ec618d9fb612585bd7c1f
commit-date: 2020-01-31

System info:

# uname -a
Linux Skrivbord 4.4.0-18362-Microsoft #476-Microsoft Fri Nov 01 16:53:00 PST 2019 x86_64 x86_64 x86_64 GNU/Linux

# perf -v
perf version 4.19.84.gdb99
bjorn3 commented 4 years ago

perf doesn't work with WSL1. It should work with WSL2 though.

Kixiron commented 4 years ago

Alright, would you like me to keep the issue open until WSL2 releases in the next few months?

bjorn3 commented 4 years ago

I am not a maintainer of flamegraph.

spitfire05 commented 4 years ago

WSL2 still has problems running:

 ~/source/repos/flamegraph   master  target/release/flamegraph ls
Cargo.lock  LICENSE-APACHE  README.md  example.svg          flamegraph.svg  perf.data.old  target
Cargo.toml  LICENSE-MIT     bors.toml  example_cropped.png  perf.data       src
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.008 MB perf.data ]
writing flamegraph to "flamegraph.svg"
thread 'main' panicked at 'unable to generate a flamegraph from the collapsed stack data: Io(Custom { kind: InvalidData, error: "No stack counts found" })', /home/mborejszo/source/repos/flamegraph/src/lib.rs:265:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

This is more of a WSL issue though, as perf seems to be lacking some important info from hypervisor (not 100% sure atm if this is due to what Hyper-V presents to guest, or to my specific hardware setup):

List of pre-defined events (to be used in -e):

  alignment-faults                                   [Software event]
  bpf-output                                         [Software event]
  context-switches OR cs                             [Software event]
  cpu-clock                                          [Software event]
  cpu-migrations OR migrations                       [Software event]
  dummy                                              [Software event]
  emulation-faults                                   [Software event]
  major-faults                                       [Software event]
  minor-faults                                       [Software event]
  page-faults OR faults                              [Software event]
  task-clock                                         [Software event]

  msr/tsc/                                           [Kernel PMU event]

  rNNN                                               [Raw hardware event descriptor]
  cpu/t1=v1[,t2=v2,t3 ...]/modifier                  [Raw hardware event descriptor]
   (see 'man perf-list' on how to encode it)

  mem:<addr>[/len][:access]                          [Hardware breakpoint]
 ~/source/repos/flamegraph   master ●  perf stat ls
Cargo.lock  LICENSE-APACHE  README.md  example.svg          flamegraph.svg  perf.data.old  target
Cargo.toml  LICENSE-MIT     bors.toml  example_cropped.png  perf.data       src

 Performance counter stats for 'ls':

              0.41 msec task-clock:u              #    0.782 CPUs utilized
                 0      context-switches:u        #    0.000 K/sec
                 0      cpu-migrations:u          #    0.000 K/sec
               110      page-faults:u             #    0.000 K/sec
   <not supported>      cycles:u
   <not supported>      instructions:u
   <not supported>      branches:u
   <not supported>      branch-misses:u

       0.000519900 seconds time elapsed

       0.000560000 seconds user
       0.000000000 seconds sys

NOTE: to be able to use perf at all in WSL2, you first need to compile it - see https://stackoverflow.com/a/60276918

the-emerald commented 3 years ago

This used to work on my old machine with WSL2, but on my new computer it's been broken. Hmm.

LuisOsta commented 1 year ago

I had/am having somewhat similar issues trying to use flamegraph in WSL 2. I was able to install perf just fine and it does seem to work generally but it does have some missing functionality as mentioned here - https://github.com/microsoft/WSL/issues/8480 (cycles, instructions, etc. is "not supported").

I've tried running both:

cargo flamegraph -c "record -e instructions -c 100 --call-graph lbr -g" -- --pattern Napoleon --file-path war_and_peace.txt --context 0

and

cargo flamegraph -- --pattern Napoleon --file-path war_and_peace.txt --context 0

The key to making it work was running:

echo -1 | sudo tee /proc/sys/kernel/perf_event_paranoid

Before trying to run flamegraph

I am still working on getting it to work with my Criterion benchmarks, getting "no bench target named" errors but the above was setup (perf install and configuration)

Pesky01 commented 1 year ago

Not sure if this was an update or its working for some reason randomly, but my perf is showing the extra stats that it didn't before I updated WSL and in general my profiling tools are working/better. So probably update WSL if it isn't already.

Performance counter stats for 'ls':

              0.91 msec task-clock:u              #    0.414 CPUs utilized
                 0      context-switches:u        #    0.000 /sec
                 0      cpu-migrations:u          #    0.000 /sec
               108      page-faults:u             #  118.877 K/sec
            799088      cycles:u                  #    0.880 GHz
             67771      stalled-cycles-frontend:u #    8.48% frontend cycles idle
            190109      stalled-cycles-backend:u  #   23.79% backend cycles idle
            584112      instructions:u            #    0.73  insn per cycle
                                                  #    0.33  stalled cycles per insn
            122569      branches:u                #  134.914 M/sec
              8289      branch-misses:u           #    6.76% of all branches

       0.002195808 seconds time elapsed

       0.002259000 seconds user
       0.000000000 seconds sys