Perf for WSL2

WSL2 has better support for native Linux development, and Vscode can work with it well. But it doesn’t provide the `perf’ command, which is used intensively. The post in stackoverflow provides several solutions. One solution is to build `perf’ from the source code. And it is the most reliable way to keep the `perf’ command matching the Linux kernel.

The steps are listed as follows,

  1. Clone the kernel source code for WSL2 Linux

    $ git clone https://github.com/microsoft/WSL2-Linux-Kernel
    
  2. Kernel version must match `uname -a`, find the correct tag.

    https://github.com/microsoft/WSL2-Linux-Kernel/tags

  3. Get latest tags and check it out

    https://devconnected.com/how-to-checkout-git-tags/

    $ git fetch --all --tags
    $ git checkout tags/<tag> -b <branch>
    
  4. Build (make sure to install the missing packages)

    $ cd WSL2-Linux-Kernel/tools/perf
    $ make
    <snipped>
    Makefile.config:600: slang not found, disables TUI support. Please install slang-devel, libslang-dev or libslang2-dev
    Makefile.config:614: GTK2 not found, disables GTK2 support. Please install gtk2-devel or libgtk2.0-dev
    <snipped>
    

    Hold breath. Pay attention to the error messages and install the missing packages. For example, If TUI is not enabled, “perf top -g” will not have the expandable “+” sign. Without this functionality, perf (top|report) -g’s output is ugly and not readable. If there are any warnings saying the include files’ mismatch, make sure to sync the kernel include files with the tools include files. Copying files from $topdir/include/xxx to tools/include/xxx may not work, see https://github.com/bombela/backward-cpp/pull/165/files for a concrete example on errors in srcline.c.

If everything is OK, you can run the perf command under tools/ directory or copy it to /usr/bin/perf.

[ WSL , perf ]
Written on April 30, 2021