'Is there any method to run perf under WSL?

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


Solution 1:[1]

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.

Solution 2:[2]

You can install linux-tools-generic.

apt install linux-tools-generic

Then run perf using the install path /usr/lib/linux-tools/<linux-version>-generic/perf.

Some tools, like flamegraph, will use environment variable PERF as the perf path.

PERF=/usr/lib/linux-tools/<linux-version>-generic/perf flamegraph -- my_program

Solution 3:[3]

The accepted answer works. However, some features are missing.

In order to get useful and demangled information, I had to install the following libs and then run make again.

  • libbabeltrace-dev
  • libunwind-dev
  • libdw-dev
  • binutils-dev
  • libiberty-dev

I'm not sure if all of them are necessary. However, those are adequate for cargo-flamegraph (my usecase) to work.

Solution 4:[4]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 JamesThomasMoon
Solution 3 cwhakes
Solution 4 Jakob Ledermann