当前位置:   article > 正文

Windows11 WSL2 Ubuntu编译安装perf工具_wsl perf

wsl perf

简介

本文介绍如何在Windows11 WSL2中安装perf工具。这要求编译安装相应的内核代码.

操作环境

  • Windows11
  • WSL2
  • Ubuntu 22.04

问题来源

在WSL2中安装perf时,需要如下命令:

sudo apt install linux-tools-common linux-tools-generic linux-tools-$(uname -r)
  • 1

此时会遇到如下错误:

E: Unable to locate package linux-tools-5.15.146.1-microsoft-standard-WSL2
E: Couldn't find any package by glob 'linux-tools-5.15.146.1-microsoft-standard-WSL2'
  • 1
  • 2

这是因为WSL2使用的Linux内核是定制化的, 并非Ubuntu母公司Canonical发布的标准内核.
这个时候需要我们手动编译安装内核代码.

准备阶段

1. 更新WSL2

打开Windows PowerShell, 更新WSL2:

PS C:\Users\xuron> wsl --update
正在检查更新。
已安装最新版本的适用于 Linux 的 Windows 子系统。
  • 1
  • 2
  • 3

2. Ubuntu 22.04中安装编译工具

从开始菜单搜索并打开Ubuntu, 也可以在Windows Terminal中选择Ubuntu 22.04.

打开命令行界面后, 输入下面命令安装编译工具以及依赖库:

sudo apt update && \
sudo apt install -y bc build-essential flex bison dwarves libssl-dev libelf-dev
  • 1
  • 2

编译内核代码

这部分步骤需要小心操作, 毕竟是内核代码.

需要注意的是, 编译内核代码的时候不要使用root权限. 只需要在安装perf的时候使用root权限.

1. 检查内核版本

首先我们需要查看当前Ubuntu的内核版本:

aronic@arong:~$ uname -r
5.15.146.1-microsoft-standard-WSL2
  • 1
  • 2

这个版本号将决定我们下载的内核代码版本, 即5.15.146.1版本.
请注意这个版本号可能会有变化, 你的实际操作结果跟这里的版本号可能会有所不同.

设置一个环境变量KERNEL_VERSION, 方便后续引用:

aronic@arong:~$ export KERNEL_VERSION=$(uname -r | cut -d'-' -f1)
  • 1

2. 下载内核代码

这里提供两种可选方式:

  1. Github Release下载, 找到对应版本的内核代码.

  2. 从Github上clone代码:

aronic@arong:~$ git clone \
--depth 1 \
--single-branch --branch=linux-msft-wsl-${KERNEL_VERSION} \
https://github.com/microsoft/WSL2-Linux-Kernel.git
  • 1
  • 2
  • 3
  • 4

3. 编译并安装

使用make命令编译内核代码:

aronic@arong:~$ cd WSL2-Linux-Kernel
aronic@arong:~/WSL2-Linux-Kernel$ make KCONFIG_CONFIG=Microsoft/config-wsl
  • 1
  • 2

为了加快编译速度, 可以使用-j参数:

aronic@arong:~/WSL2-Linux-Kernel$ make -j $(nproc) KCONFIG_CONFIG=Microsoft/config-wsl
  • 1

编译perf工具:

aronic@arong:~/WSL2-Linux-Kernel$ cd tools/perf
aronic@arong:~/WSL2-Linux-Kernel/tools/perf$ make
  • 1
  • 2

编译完成后安装到系统目录:

aronic@arong:~/WSL2-Linux-Kernel/tools/perf$ sudo cp perf /usr/bin/
  • 1

全功能的perf

perf编译的时候会根据你本地安装的库的版本来决定是否支持某些功能. 如果你需要全功能的perf则需要进一步安装依赖库.

1. 安装依赖库

aronic@arong:~/WSL2-Linux-Kernel/tools/perf$ sudo apt install binutils-dev debuginfod default-jdk default-jre libaio-dev libbabeltrace-dev libcap-dev libdw-dev libdwarf-dev libelf-dev libiberty-dev liblzma-dev libnuma-dev libperl-dev libpfm4-dev libslang2-dev libssl-dev libtraceevent-dev libunwind-dev libzstd-dev libzstd1 python-setuptools python3 python3-dev systemtap-sdt-dev zlib1g-dev
  • 1

2. 重新编译perf

aronic@arong:~/WSL2-Linux-Kernel/tools/perf$ make clean && make
  • 1

编译过程中会显示哪些功能被支持.

...
Auto-detecting system features:
...                         dwarf: [ on  ]
...            dwarf_getlocations: [ on  ]
...                         glibc: [ on  ]
...                        libbfd: [ on  ]
...                libbfd-buildid: [ on  ]
...                        libcap: [ on  ]
...                        libelf: [ on  ]
...                       libnuma: [ on  ]
...        numa_num_possible_cpus: [ on  ]
...                       libperl: [ on  ]
...                     libpython: [ on  ]
...                     libcrypto: [ on  ]
...                     libunwind: [ on  ]
...            libdw-dwarf-unwind: [ on  ]
...                          zlib: [ on  ]
...                          lzma: [ on  ]
...                     get_cpuid: [ on  ]
...                           bpf: [ on  ]
...                        libaio: [ on  ]
...                       libzstd: [ on  ]
...        disassembler-four-args: [ on  ]

...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

3. 安装perf

aronic@arong:~/WSL2-Linux-Kernel/tools/perf$ sudo cp perf /usr/bin/
  • 1

测试perf

aronic@arong:~$ perf --version
perf version 5.15.146.1.gee5b8e3dcbc6
  • 1
  • 2

perf ready

参考

  1. Microsoft/WSL2-Linux-Kernel, Github
  2. abel0b/install-linux-perf-on-wsl2.sh, Github
  3. Building Linux Perf tool, Medium
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/931277
推荐阅读
相关标签
  

闽ICP备14008679号