赞
踩
本文介绍如何在Windows11 WSL2中安装perf
工具。这要求编译安装相应的内核代码.
在WSL2中安装perf
时,需要如下命令:
sudo apt install linux-tools-common linux-tools-generic linux-tools-$(uname -r)
此时会遇到如下错误:
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'
这是因为WSL2使用的Linux内核是定制化的, 并非Ubuntu
母公司Canonical
发布的标准内核.
这个时候需要我们手动编译安装内核代码.
打开Windows PowerShell, 更新WSL2:
PS C:\Users\xuron> wsl --update
正在检查更新。
已安装最新版本的适用于 Linux 的 Windows 子系统。
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
这部分步骤需要小心操作, 毕竟是内核代码.
需要注意的是, 编译内核代码的时候不要使用root权限. 只需要在安装perf
的时候使用root
权限.
首先我们需要查看当前Ubuntu
的内核版本:
aronic@arong:~$ uname -r
5.15.146.1-microsoft-standard-WSL2
这个版本号将决定我们下载的内核代码版本, 即5.15.146.1
版本.
请注意这个版本号可能会有变化, 你的实际操作结果跟这里的版本号可能会有所不同.
设置一个环境变量KERNEL_VERSION
, 方便后续引用:
aronic@arong:~$ export KERNEL_VERSION=$(uname -r | cut -d'-' -f1)
这里提供两种可选方式:
从Github Release下载, 找到对应版本的内核代码.
从Github上clone代码:
aronic@arong:~$ git clone \
--depth 1 \
--single-branch --branch=linux-msft-wsl-${KERNEL_VERSION} \
https://github.com/microsoft/WSL2-Linux-Kernel.git
使用make
命令编译内核代码:
aronic@arong:~$ cd WSL2-Linux-Kernel
aronic@arong:~/WSL2-Linux-Kernel$ make KCONFIG_CONFIG=Microsoft/config-wsl
为了加快编译速度, 可以使用-j
参数:
aronic@arong:~/WSL2-Linux-Kernel$ make -j $(nproc) KCONFIG_CONFIG=Microsoft/config-wsl
编译perf
工具:
aronic@arong:~/WSL2-Linux-Kernel$ cd tools/perf
aronic@arong:~/WSL2-Linux-Kernel/tools/perf$ make
编译完成后安装到系统目录:
aronic@arong:~/WSL2-Linux-Kernel/tools/perf$ sudo cp perf /usr/bin/
perf
perf
编译的时候会根据你本地安装的库的版本来决定是否支持某些功能. 如果你需要全功能的perf
则需要进一步安装依赖库.
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
perf
aronic@arong:~/WSL2-Linux-Kernel/tools/perf$ make clean && make
编译过程中会显示哪些功能被支持.
... 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 ] ...
perf
aronic@arong:~/WSL2-Linux-Kernel/tools/perf$ sudo cp perf /usr/bin/
perf
aronic@arong:~$ perf --version
perf version 5.15.146.1.gee5b8e3dcbc6
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。