当前位置: 首页 > news >正文

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

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

  • 一、背景
  • 二、准备工作
  • 三、获取并编译Linux内核源码
  • 四、安装和配置perf
  • 五、测试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) yRemove the kernel mapping in user mode (PAGE_TABLE_ISOLATION) [Y/n/?] yAvoid speculative indirect branches in kernel (RETPOLINE) [Y/n/?] yEnable return-thunks (RETHUNK) [Y/n/?] yEnable UNRET on kernel entry (CPU_UNRET_ENTRY) [Y/n/?] yEnable IBPB on kernel entry (CPU_IBPB_ENTRY) [Y/n/?] yEnable IBRS on kernel entry (CPU_IBRS_ENTRY) [Y/n/?] yMitigate speculative RAS overflow on AMD (CPU_SRSO) [Y/n/?] yMitigate Straight-Line-Speculation (SLS) [N/y/?] nForce GDS Mitigation (GDS_FORCE_MITIGATION) [N/y/?] nRFDS Mitigation (MITIGATION_RFDS) [Y/n/?] (NEW) yMitigate 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
$ perfusage: perf [--version] [--help] [OPTIONS] COMMAND [ARGS]The most commonly used perf commands are:annotate        Read perf.data (created by perf record) and display annotated codearchive         Create archive with object files with build-ids found in perf.data filebench           General framework for benchmark suitesbuildid-cache   Manage build-id cache.buildid-list    List the buildids in a perf.data filec2c             Shared Data C2C/HITM Analyzer.config          Get and set variables in a configuration file.daemon          Run record sessions on backgrounddata            Data file related processingdiff            Read perf.data files and display the differential profileevlist          List the event names in a perf.data fileftrace          simple wrapper for kernel's ftrace functionalityinject          Filter to augment the events stream with additional informationiostat          Show I/O performance metricskallsyms        Searches running kernel for symbolskmem            Tool to trace/measure kernel memory propertieskvm             Tool to trace/measure kvm guest oslist            List all symbolic event typeslock            Analyze lock eventsmem             Profile memory accessesrecord          Run a command and record its profile into perf.datareport          Read perf.data (created by perf record) and display the profilesched           Tool to trace/measure scheduler properties (latencies)script          Read perf.data (created by perf record) and display trace outputstat            Run a command and gather performance counter statisticstest            Runs sanity tests.timechart       Tool to visualize total system behavior during a workloadtop             System profiling tool.version         display the version of perf binaryprobe           Define new dynamic tracepointstrace           strace inspired toolSee 'perf help COMMAND' for more information on a specific command.

六、总结

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

参考:

  • https://github.com/microsoft/WSL2-Linux-Kernel
  • abel0b/install-linux-perf-on-wsl2.sh, https://gist.github.com/abel0b/b1881e41b9e1c4b16d84e5e083c38a13
  • https://medium.com/@manas.marwah/building-perf-tool-fc838f084f71
    在这里插入图片描述
http://www.lryc.cn/news/518081.html

相关文章:

  • Docker运维高级容器技术知识点总结
  • react-quill 富文本组件编写和应用
  • LabVIEW轴承性能测试系统
  • 【《游戏编程模式》实战04】状态模式实现敌人AI
  • 借助免费GIS工具箱轻松实现las点云格式到3dtiles格式的转换
  • 科研绘图系列:R语言科研绘图之标记热图(heatmap)
  • 【轻松学C:编程小白的大冒险】--- C语言简介 02
  • 《HeadFirst设计模式》笔记(上)
  • 数据结构:ArrayList与顺序表
  • SpringBoot之核心配置
  • EasyExcel上传校验文件错误信息放到文件里以Base64 返回给前端
  • 单片机软件定时器V4.0
  • 超完整Docker学习记录,Docker常用命令详解
  • C++ 入门第26天:文件与流操作基础
  • 使用python将多个Excel表合并成一个表
  • halcon三维点云数据处理(七)find_shape_model_3d_recompute_score
  • vue js实现时钟以及刻度效果
  • unity学习15:预制体prefab
  • 基于Thinkphp6+uniapp的陪玩陪聊软件开发方案分析
  • MySQL - 子查询和相关子查询详解
  • Android 系统签名 keytool-importkeypair
  • 安卓漏洞学习(十八):Android加固基本原理
  • Docker 使用Dockerfile创建镜像
  • 【Python运维】利用Python实现高效的持续集成与部署(CI/CD)流程
  • 成功!QT 5.15.2编译mysql驱动
  • 安卓NDK视觉开发——手机拍照文档边缘检测实现方法与库封装
  • 第二届 Sui 游戏峰会将于 3 月 18 日在旧金山举行
  • 自动驾驶相关知识学习笔记
  • uniapp - 基于uniapp+vue3实现自定义增强版table表格组件体验「兼容H5+小程序+App端」
  • 新时期下k8s 网络插件calico 安装