12

When I wanted to run perf under WSL, I met the follow question:

WARNING: perf not found for kernel 4.4.0-18362

You may need to install the following packages for this specific kernel:

linux-tools-4.4.0-18362-Microsoft

linux-cloud-tools-4.4.0-18362-Microsoft

You may also want to install one of the following packages to keep up to date:

linux-tools-Microsoft

linux-cloud-tools-Microsoft

But I can't find packages called linux-tools-4.4.0-18362-Microsoft or linux-cloud-tools-4.4.0-18362-Microsoft. I guess the package names are generated automatically.

I also tried to use perf in docker container. However, docker container use the same kernel as the hosts.

Is there any method to run perf under WSL?


I heard that perf can be used in WSL2. But after I upgraded to WSL2, it shows the similar error message:

WARNING: perf not found for kernel 4.19.84-microsoft

You may need to install the following packages for this specific kernel:

linux-tools-4.19.84-microsoft-standard

linux-cloud-tools-4.19.84-microsoft-standard

You may also want to install one of the following packages to keep up to date:

linux-tools-standard

linux-cloud-tools-standard
yodahaji
  • 397
  • 2
  • 10
  • 2
    In WSL2, perf can be used – Biswapriyo Feb 16 '20 at 20:53
  • 1
    @Biswapriyo Hi, I upgraded wsl1 to wsl2. However, it shows the semilar error message in wsl1: – yodahaji Feb 18 '20 at 07:59
  • Did you install any `linux*-tools*` package(s) that you can see in `aptitute` or whatever package-manager you use? Your 2nd error message even lists some package names to install. – Peter Cordes Feb 18 '20 at 08:32
  • @PeterCordes Hi, I searched for these packages but found nothing. I guess these names are generated automatically. I found the correct answer below. – yodahaji Feb 18 '20 at 09:13
  • It didn't say that in your question so it seemed like there'd be some hope that the distro would package `linux-tools` since the kernel supports `perf`. But apparently not. Silly Microsoft :/ – Peter Cordes Feb 18 '20 at 09:32

3 Answers3

30

WARNING: perf not found for kernel 4.19.84-microsoft

Because WSL2 uses custom Linux kernel. Its source code can be found here microsoft/WSL2-Linux-Kernel. We have to compile perf tools from it.

Procedure

  • Install required build packages. If you are using Ubuntu in WSL2 this is the required command:
sudo apt install build-essential flex bison libssl-dev libelf-dev
  • Clone the WSL2 Linux kernel repository:
git clone --depth=1 https://github.com/microsoft/WSL2-Linux-Kernel.git
  • Go to perf folder and compile it:
cd WSL2-Linux-Kernel/tools/perf
make

perf executable file will be in that folder.

Community
  • 1
  • 1
Biswapriyo
  • 1,941
  • 2
  • 12
  • 33
  • 2
    Perf tool sources have standalone version at https://mirrors.edge.kernel.org/pub/linux/kernel/tools/perf/ which is easier to use than doing full linux kernel git checkout or download. This site have perf for several recent versions of upstream linux kernel; they should work for any kernel. Or you can try to generate same targz file from WSL2 git with `make perf-targz-src-pkg`. Also: did you check the generated perf executable - what works and what does not; what is in `perf list`, what says perf stat for simple "echo 1", are there hardware events? – osgx Feb 22 '20 at 08:04
  • @osgx I didn't check every options but `./perf stat` works as usual. – Biswapriyo Feb 22 '20 at 08:52
  • Make sure you have WSL 2 installed other wise you will get `perf_event_open(..., 0) failed unexpectedly with error 22 (Invalid argument) ` as described here: https://github.com/microsoft/WSL/issues/329#issuecomment-564454956 – joseph Mar 03 '21 at 00:43
  • I'm getting this:- Error: Unable to find debugfs/tracefs Hint: Was your kernel compiled with debugfs/tracefs support? Hint: Is the debugfs/tracefs filesystem mounted? Hint: Try 'sudo mount -t debugfs nodev /sys/kernel/debug' rebroad@J3PMPJP:~/bin$ sudo mount -t debugfs nodev /sys/kernel/debug mount: /sys/kernel/debug: unknown filesystem type 'debugfs'. – Rebroad May 03 '21 at 14:31
7

You can install linux-tools-generic, and run perf directly through /usr/lib/linux-tools/<linux-version>-generic/perf

Gloit
  • 71
  • 1
  • 2
0

If you follow the accepted answer, make sure you read the complains the make command prints at the start, as it might be missing some headers and disables functionality.

For me it disabled tui, gtk and demangling to name a few features.

  • 3
    So which specific packages did you have to install to satisfy those optional dependencies and enable those features? That would make this answer more useful to future readers. – Peter Cordes Oct 17 '20 at 13:07