WSL2开发使用小贴士

WSL 1 和 WSL 2 有哪些关键不同?以及使用 Linux 工具链时,如何获取最大的文件性能?


WSL概述

适用于 Linux 的 Windows 子系统(Windows Subsystem for Linux,WSL)是 Windows 的一项功能,用于在 Windows 计算机上运行 Linux 环境,而无需完整的虚拟机。

目前 WSL 有 WSL 1 和 WSL 2 两个版本,虽然都是为了在 Windows 上运行 Linux 环境,但它们的实现方式完全不同。

功能 / 特点 WSL 1 WSL 2
架构 系统调用的翻译层 Hyper-V 轻量级虚拟机
文件系统 Windows NT 层模拟 Linux 文件系统 ext4 格式的 VHDX 文件系统
快速启动
完整 Linux 内核
完整的系统调用兼容性
读写 Linux 上文件的性能
读写 Windows 上文件的性能
systemd 支持

比较实现方式

WSL 1 是一个翻译层,在 Windows NT 内核上模拟 Linux 系统调用层,不是虚拟机也没有 Linux 内核

WSL1 的机制是:

  1. Windows 截获 Linux 程序发出的系统调用;
  2. 转换成 Windows 的系统调用;
  3. 在 Windows 内核中执行。

比较文件系统

WSL 1 的文件操作本质上都是 Windows 的 NTFS 文件系统在操作:

  • 访问 Windows 文件:Windows 磁盘看似“挂载”在 /mnt 下,实际上翻译层会直接将文件路径 /mnt/c/xxx 转换为 Windows 路径 C:\xxx,相当于 NTFS 直接访问原始文件,速度快
  • Linux 根文件系统:在 NTFS 上仿真实现 Linux 文件系统的行为,增加额外的仿真开销,速度稍慢

对文件读写性能进行排序:WSL 2 读写 Linux 文件 > WSL 1 读写 Windows 文件 > WSL 1 读写 Linux 文件 > WSL 2 读写 Windows 文件。

WSL2读写性能测试

若想获得最快的性能速度,请将文件存储在 WSL 文件系统中,使用 Linux 工具在 Linux 命令行中处理这些文件。跨操作系统访问文件可能会显著降低性能。

WSL 文档 - 最佳设置实践

WSL 2 操作 Linux 文件和 Windows 文件的性能差距有多大呢?写个脚本简单测试下:

>folded wsl_fs_benchmark.sh
#!/bin/bash

echo "=== WSL2 Disk I/O Benchmark ==="
echo

TEST_SIZE_MB=512
SMALL_FILES=2000

# Locations
EXT4_DIR="$HOME/wsl_test_ext4"
WIN_DIR="/mnt/d/wsl_test_win"

mkdir -p "$EXT4_DIR" "$WIN_DIR"

benchmark_seq_write() {
DIR=$1
echo "[SEQ WRITE] Testing $DIR ..."
dd if=/dev/zero of=$DIR/testfile bs=1M count=$TEST_SIZE_MB conv=fdatasync 2>&1 | grep -E --color=never 'copied'
}

benchmark_seq_read() {
DIR=$1
echo "[SEQ READ] Testing $DIR ..."
dd if=$DIR/testfile of=/dev/null bs=1M 2>&1 | grep -E --color=never 'copied'
}

benchmark_small_files() {
DIR=$1
echo "[SMALL FILES] Creating $SMALL_FILES files in $DIR ..."
START=$(date +%s.%N)
for i in $(seq 1 $SMALL_FILES); do
echo "hello" > "$DIR/small_$i.txt"
done
END=$(date +%s.%N)
TIME=$(echo "$END - $START" | bc)
echo "Time: $TIME seconds"
}

echo "------------------------------------------"
echo "Testing ext4 (/home)"
echo "------------------------------------------"
benchmark_seq_write "$EXT4_DIR"
benchmark_seq_read "$EXT4_DIR"
benchmark_small_files "$EXT4_DIR"

echo
echo "------------------------------------------"
echo "Testing Windows (/mnt/d)"
echo "------------------------------------------"
benchmark_seq_write "$WIN_DIR"
benchmark_seq_read "$WIN_DIR"
benchmark_small_files "$WIN_DIR"

echo
echo "Benchmark complete!"

在 i5-12500H(4E8P)和 NVMe SSD 的环境下,测试结果如下:

>folded 测试结果
$ ./wsl_fs_benchmark.sh
=== WSL2 Disk I/O Benchmark ===

------------------------------------------
Testing ext4 (/home)
------------------------------------------
[SEQ WRITE] Testing /home/thinklong/wsl_test_ext4 ...
536870912 bytes (537 MB, 512 MiB) copied, 0.592031 s, 907 MB/s
[SEQ READ] Testing /home/thinklong/wsl_test_ext4 ...
536870912 bytes (537 MB, 512 MiB) copied, 0.102132 s, 5.3 GB/s
[SMALL FILES] Creating 2000 files in /home/thinklong/wsl_test_ext4 ...
Time: .079443942 seconds

------------------------------------------
Testing Windows (/mnt/d)
------------------------------------------
[SEQ WRITE] Testing /mnt/d/wsl_test_win ...
536870912 bytes (537 MB, 512 MiB) copied, 4.79956 s, 112 MB/s
[SEQ READ] Testing /mnt/d/wsl_test_win ...
536870912 bytes (537 MB, 512 MiB) copied, 3.87322 s, 139 MB/s
[SMALL FILES] Creating 2000 files in /mnt/d/wsl_test_win ...
Time: 10.715896161 seconds

Benchmark complete!
---
config:
  theme: 'base'
  xyChart:
    width: 600
    height: 150
    plotReservedSpacePercent: 50
    showDataLabel: false
  themeVariables:
    xyChart:
      darkMode: true
      fontSize: 16px
      backgroundColor: '#000'
      titleColor: '#fcee09'
      labelFontSize: 16
      xAxisLabelColor: '#fff'
      xAxisTitleColor: '#fff'
      xAxisTickColor: '#fff'
      xAxisLineColor: '#fff'
      yAxisLabelColor: '#fff'
      yAxisTitleColor: '#fff'
      yAxisTickColor: '#fff'
      yAxisLineColor: '#fff'
      plotColorPalette: '#fcee09, #02d7f2'
---
xychart horizontal
  title "WSL2操作不同文件系统的性能"
  x-axis ["顺序读", "顺序写", "创建小文件"]
  y-axis "速度(GB/s)" 0 --> 5.5
  bar [5.3,0.89,5.36]
  bar [0.14,0.11,0.04]
测试项目 Linux Windows
顺序读(GB/s) 5.3 0.14
顺序写(GB/s) 0.89 0.11
创建大量小文件用时(秒) 0.08 10.72

可以看到在 WSL 2 中,Linux 工具处理 Linux 文件的性能远远大于挂载的 Windows 文件。因此,官方文档中建议将文件存储在 WSL 文件系统中。


参考资料

WSL 文档 - 比较 WSL 版本
WSL 文档 - 跨文件系统的文件存储和性能

WSL2开发使用小贴士

https://thinklong.me/develop-on-wsl2/

作者

ThinkLong

发布于

2025-09-10

更新于

2025-12-11

许可协议

评论

+