如何在Windows 11 WSL2 Ubuntu 环境下安装和配置perf性能分析工具?

在Windows 11 WSL2 Ubuntu 环境下完整安装和配置perf性能分析工具

一、背景

由于WSL2使用的是微软定制的内核,并非标准的Ubuntu内核,因此直接使用apt安装linux-tools包会失败。

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

会遇到如下错误:

E: Unable to locate package linux-tools-5.15.167.4-microsoft-standard-WSL2
E: Couldn't find any package by glob 'linux-tools-5.15.167.4-microsoft-standard-WSL2'

这个时候需要手动编译安装内核代码。本文详细介绍如何在Windows 11的WSL2 (Ubuntu 22.04)环境中成功安装并配置perf性能分析工具,一步步从获取内核源码到最终测试perf的完整过程,并提供更全面的依赖库安装方法以确保perf功能完整。

在这里插入图片描述

二、准备工作

  1. 更新WSL2: 在Windows PowerShell中运行以下命令更新WSL2到最新版本:

    wsl --update
    
  2. 安装必要的编译工具和依赖库: 在Ubuntu终端中执行以下命令,安装编译perf所需的工具链和基础依赖库:

    sudo apt update
    sudo apt install -y bc build-essential flex bison git cmake libelf-dev
    

    这里我们预先安装了 cmake ,在后续编译过程中可能会用到,避免了不必要的安装错误。

三、获取并编译Linux内核源码

  1. 确定内核版本: 在Ubuntu终端中运行以下命令获取当前WSL2内核版本:

    uname -r
    

    输出可能类似于

    5.15.167.4-microsoft-standard-WSL2
    

    我们仅提取主版本号,例如 5.15.167.4。注意:这个版本号可能会有变化, 你的实际操作结果跟这里的版本号可能会不同。

  2. 克隆内核源码: 使用Git克隆微软提供的WSL2内核源码。为了提高效率,我们只克隆必要的代码分支:

    # 设置一个环境变量方便引用
    export KERNEL_VERSION=$(uname -r | cut -d'-' -f1)
    git clone --depth 1 --single-branch --branch=linux-msft-wsl-${KERNEL_VERSION} https://github.com/microsoft/WSL2-Linux-Kernel.git
    

    这将克隆指定版本的内核源码到 WSL2-Linux-Kernel 目录。

  3. 编译内核及perf工具: 进入克隆的目录,并使用 make 命令编译内核。为了加快编译速度,可以使用 -j 参数指定并行编译的进程数,例如使用 -j$(nproc) 使用所有可用CPU核心进行并行编译。请注意,此步骤无需root权限。

    cd WSL2-Linux-Kernel
    make KCONFIG_CONFIG=Microsoft/config-wsl -j$(nproc)
    

    这个过程会让你输入y/n,一直输入y就好。例如:

     *
     * Mitigations for CPU vulnerabilities
     *
     Mitigations for CPU vulnerabilities (CPU_MITIGATIONS) [Y/n/?] (NEW) y
     Remove the kernel mapping in user mode (PAGE_TABLE_ISOLATION) [Y/n/?] y
     Avoid speculative indirect branches in kernel (RETPOLINE) [Y/n/?] y
         Enable return-thunks (RETHUNK) [Y/n/?] y
         Enable UNRET on kernel entry (CPU_UNRET_ENTRY) [Y/n/?] y
     Enable IBPB on kernel entry (CPU_IBPB_ENTRY) [Y/n/?] y
     Enable IBRS on kernel entry (CPU_IBRS_ENTRY) [Y/n/?] y
     Mitigate speculative RAS overflow on AMD (CPU_SRSO) [Y/n/?] y
     Mitigate Straight-Line-Speculation (SLS) [N/y/?] n
     Force GDS Mitigation (GDS_FORCE_MITIGATION) [N/y/?] n
     RFDS Mitigation (MITIGATION_RFDS) [Y/n/?] (NEW) y
     Mitigate Spectre-BHB (Branch History Injection) (MITIGATION_SPECTRE_BHI) [Y/n/?] (NEW) y
     *
     * Core Netfilter Configuration
     *
    
  4. 编译完成后,进入 tools/perf 目录,编译perf工具:

    cd tools/perf
    make
    

四、安装和配置perf

  1. 安装perf: 编译完成后,使用以下命令将 perf 可执行文件复制到系统目录:

    sudo cp perf /usr/bin/
    
  2. 安装全功能perf的依赖库: 为了确保perf拥有全部功能,需要安装额外的依赖库。这步比之前的例子更全面,涵盖更多:

    sudo apt install binutils-dev dwarves 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 python3-dev python3-setuptools systemtap-sdt-dev zlib1g-dev
    
  3. 重新编译perf (可选): 安装完所有依赖库后,建议重新编译perf以确保所有功能都被启用。

    make clean && make
    

    编译完成后,再次执行第三章节中的安装命令。编译过程中会显示哪些功能被支持。

五、测试perf

运行以下命令测试perf是否成功安装:

perf --version

如果成功安装,将显示perf的版本信息。

perf version 5.15.167.4.g6ac7abbd97a0
$ perf

 usage: perf [--version] [--help] [OPTIONS] COMMAND [ARGS]

 The most commonly used perf commands are:
   annotate        Read perf.data (created by perf record) and display annotated code
   archive         Create archive with object files with build-ids found in perf.data file
   bench           General framework for benchmark suites
   buildid-cache   Manage build-id cache.
   buildid-list    List the buildids in a perf.data file
   c2c             Shared Data C2C/HITM Analyzer.
   config          Get and set variables in a configuration file.
   daemon          Run record sessions on background
   data            Data file related processing
   diff            Read perf.data files and display the differential profile
   evlist          List the event names in a perf.data file
   ftrace          simple wrapper for kernel's ftrace functionality
   inject          Filter to augment the events stream with additional information
   iostat          Show I/O performance metrics
   kallsyms        Searches running kernel for symbols
   kmem            Tool to trace/measure kernel memory properties
   kvm             Tool to trace/measure kvm guest os
   list            List all symbolic event types
   lock            Analyze lock events
   mem             Profile memory accesses
   record          Run a command and record its profile into perf.data
   report          Read perf.data (created by perf record) and display the profile
   sched           Tool to trace/measure scheduler properties (latencies)
   script          Read perf.data (created by perf record) and display trace output
   stat            Run a command and gather performance counter statistics
   test            Runs sanity tests.
   timechart       Tool to visualize total system behavior during a workload
   top             System profiling tool.
   version         display the version of perf binary
   probe           Define new dynamic tracepoints
   trace           strace inspired tool

 See 'perf help COMMAND' for more information on a specific command.

六、总结

  • 内核版本号可能因WSL2更新而变化,请根据实际情况修改相应的版本号。
  • 编译内核是一个耗时的过程,请耐心等待。
  • 确保系统具有足够的磁盘空间,内核源码和编译过程会占用大量的磁盘空间。

参考:

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lion 莱恩呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值