Clion 基于 WSL 2 使用 Profiler 及 WSL 2 的安装
官方文档
根据官方文档阐述:
OS: macOS / Linux / WSL
Required tools: Perf on Linux and WSL / DTrace on macOS
Project format: CMake / Gradle
Toolchain: Local / Remote (Linux only) / WSL
- For the case of WSL, the required Perf backend can only be installed on WSL 2.
在 Windows 环境下如果需要使用 Profiler 则必须安装 WSL2 。
WSL 的全称为 Windows Subsystem for Linux ,官方对其的描述如下:
适用于 Linux 的 Windows 子系统可让开发人员直接在 Windows 上按原样运行 GNU/Linux 环境(包括大多数命令行工具、实用工具和应用程序),且不会产生传统虚拟机或双启动设置开销。
安装 WSL2
官方文档
https://docs.microsoft.com/zh-cn/windows/wsl/install
不过本人并非使用官方文档中的安装方式,参考了网友的安装步骤以及 Clion 官方文档对 WSL 环境的配置教程。具体安装步骤如下:
安装 Windows Terminal
为了便于安装我先安装了 Windows Terminal ,个人觉得 Windows Terminal 的界面看起来更加舒适。
Windows Terminal 可以直接在 Microsoft Store 中安装。
开启WSL相关功能
以管理员的身份开启 Windows Terminall 选择 PowerShell 输入如下命令
1 | dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart |
下载 WSL
从 Microsoft Store 下载并安装 WSL Ubuntu 20.04 LTS
启动 WSL 并切换至 WSL 2
如果第一次启动遇到如下情况,则是因为未安装 Windows 子系统支持。
输入如下命令可解决此问题,注意:此处需要重启电脑!
1 | Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux |
重启后再次打开 WSL 则会要求新建 Ubuntu 用户名及密码。
此时 WSL 已安装完成,由于我们需要的是 WSL2 ,所以需要再将 WSL 版本号切换到 WSL2 。
1 | # 查看 WSL 版本号 |
若此处提示 WSL 2 需要更新其内核组件,则根据提示前往 WSL 2 内核下载界面下载其内核,其内核安装方式是一键式操作,在此就不再赘述。
自此,WSL 2 就安装完成了。
配置 WSL 2
更换下载源
为了后续的方便,先将 Ubuntu 的下载源更换到了阿里云。
以防万一先备份原下载源
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
在 Windows 系统下访问 WSL2 Linux 子系统文件目录路径为\\wsl$\Ubuntu-20.04\
,于是我们在资源管理器直接访问\\wsl$\Ubuntu-20.04\etc\apt\sources.list
,由于我安装的是 Ubuntu 20.04 ,所以将其内容修改为如下阿里云镜像站地址,若系统版本不同则参考阿里云镜像站官方文档。
1 | deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse |
更换完成后刷新软件源即完成下载源替换。
1 | sudo apt update && sudo apt full-upgrade |
安装 cmake 等 C++ 必备包
1 | sudo apt-get install cmake gcc clang gdb build-essential |
为了便于后续操作,我在此处在 Windows Terminal 中将用户身份切换到了 root 用户
1 | ubuntu2004 config --default-user root |
修改 Clion 配置
配置 Perf
此时若根据Clion文档的要求在 Settings / Preferences | Build, Execution, Deployment | Dynamic Analysis Tools | Profiler
中将 Perf executable
的路径设置为\\wsl$\Ubuntu-20.04\usr\bin\perf
后点击 Profile 按钮后却提示报错:
但当回到 WSL 中尝试使用命令 sudo apt-get install linux-tools-5.10.16.3-microsoft-standard-WSL2
安装所需要的 Packages 却提示:
1 | E: Unable to locate package 5.10.16.3-microsoft-standard-WSL2 |
无论如何都装不上所缺的 Packages ,最终在 stackoverflow 上的一篇名为 Is there any method to run perf under wsl 的问题中找到了解决办法:
WARNING: perf not found for kernel 4.19.84-microsoft
Because WSL2 uses custom Linux kernel. Its source code can be found here microsoft/WSL2-Linux-Kernel. We have to compile perf tools from it.
Procedure
- Install required build packages. If you are using Ubuntu in WSL2 this is the required command:
1 sudo apt install build-essential flex bison libssl-dev libelf-dev
- Clone the WSL2 Linux kernel repository:
1 git clone --depth=1 https://github.com/microsoft/WSL2-Linux-Kernel.git
- Go to perf folder and compile it:
1
2 cd WSL2-Linux-Kernel/tools/perf
make
perf
executable file will be in that folder.
由于 WSL2 使用的是自定义 Linux 内核,于是我们可以从其源码中编译 perf 工具
首先安装其所需的 Packages
1 | sudo apt install build-essential flex bison libssl-dev libelf-dev |
将 WSL2 Linux 核心仓库 clone 下来
1 | git clone --depth=1 https://github.com/microsoft/WSL2-Linux-Kernel.git |
进入到 perf 目录中将其编译
1 | cd WSL2-Linux-Kernel/tools/perf |
最后在 Settings / Preferences | Build, Execution, Deployment | Dynamic Analysis Tools | Profiler
中将 Perf executable
的路径设置为 \\wsl$\Ubuntu-20.04\root\WSL2-Linux-Kernel\tools\perf\perf
再点击 Profile 按钮后发现该功能已可正常运行。
问题
点击 Profile 按钮后控制台出现了疑似报错的内容,暂不知如何解决。