AIFeed

“安全税”:在 WSL 2 中夺回 30% 的性能

AI summary

我今年三月写过的一个重大 WSL2 性能问题,现在有了更清晰的根因分析和复现步骤。设置 kernelCommandLine=spec_rstack_overflow=off 可以让构建性能提升 50% 以上,另见 https://github.com/microsoft/WSL/issues/41827 的评论。

Translation(model translation, not the source's own text)

文章标题:The "Security Tax": Reclaiming 30% Performance in WSL 2

对于在 WSL 2 上运行精简环境的工程师来说,每一兆字节的 RAM 和每一个 CPU 周期都至关重要。许多开发者并未意识到,针对 Spectre 和 Meltdown 等漏洞的内核级缓解措施带来了显著的“性能税”。

在最近针对一个基于 Go 的后端项目进行的一组基准测试中,禁用这些缓解措施使实际编译时间减少了 31.7%。以下是你如何审计自己的系统,并判断这种取舍是否适合你的工作流程。

基准测试:Go 编译

编译是一项“系统调用密集”的任务。每当编译器读取头文件、打开文件或链接库时,它都要与内核通信。在启用缓解措施的情况下,这数千次转换中的每一次都要求 CPU “净化”其状态,从而拖慢整个过程。

结果:编译时间减少 32%!

指标缓解措施开启(默认)缓解措施关闭差异
用户时间81.87s82.15s可忽略
系统时间24.89s13.51s-45.7%
总(实际)时间26.98s18.31s-31.7%
CPU 效率395%522%+127%

最能说明问题的是系统时间。通过关闭缓解措施,在内核中花费的时间减少了近一半,让 CPU 有更多时间真正用于编译代码(体现在 CPU 效率的提升上)。

向下滚动查看配置与基准测试详情。

审计你的系统

你可以通过检查 CPU 漏洞目录,查看你的内核当前是否正在被“征税”。

在你的 WSL 终端中运行以下命令:


        grep . /sys/devices/system/cpu/vulnerabilities/*
      
  • 如果你看到“Mitigation: …”:你的内核受到保护,但你的性能受到限制。
  • 如果你看到“Vulnerable”:你的缓解措施已禁用,你正以最大硬件速度运行。

修复性能税:.wslconfig

要切换这些设置,你必须编辑位于 %UserProfile%\.wslconfig 的 Windows 侧配置文件。

示例:“超精简”开发者配置

此配置禁用了安全税,并设置了严格的内存上限,以打造更敏捷、更高效的环境。


        [wsl2]
# The "Magic" performance toggle
kernelCommandLine=mitigations=off quiet loglevel=0 audit=0
      

注意:保存此文件后,你必须在 PowerShell 中运行 wsl --shutdown,更改才会生效。

1. 基础 Linux perf 基准测试

这衡量两个进程之间通信和上下文切换的速度。


        # apt install linux-perf OR linux-tools-common
perf bench sched pipe -l 100000
      

关注 usecs/op。越低越好。

结果:基础基准测试提升 19%

指标缓解措施关闭缓解措施开启(默认)性能差异
执行时间3.751s4.445s慢 18.5%
延迟(usecs/op)37.51 μs44.45 μs多 6.94 μs 开销
吞吐量(ops/sec)“26,659”“22,493”损失 15.6%

2. 编译测试


        # Clean cache to ensure a fresh run
go clean -cache
# Time the build without verbose output
time go build ./...
# 82.15s user 13.51s system 522% cpu 18.308 total
      

结果:编译时间提升 32%!

结论

对于用于编译 Go、C++ 或 Rust 的本地开发机器来说,30% 的速度提升通常值得承担这一经过计算的风险。一行内核配置带来的性能提升,相当于价值 $1000+ 的硬件升级。

另见

  • AI-Powered Infrastructure Hardening: Using Gemini-CLI for GCP Security Auditing
  • Getting to Yes -- As Quickly as Possible
  • Using AWS IOT To Arm Blink Cameras
  • Free SSL Certificates using ACM (AWS Certificate Manager)
  • Securing Your Network Using Auto-Updating Security Groups

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by Lobsters
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗