WSL2文件系统处理速度较慢

测试一下文件写入速度

WSL1:

jiang@DESKTOP-QK7OLED:~$ dd if=/dev/zero of=/mnt/e/testfile bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 3.17351 s, 330 MB/s

 

WSL2:

测试前删除缓存

lzjiang@DESKTOP-QK7OLED:/mnt/c/Users/jleads$ sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'

测试速度

lzjiang@DESKTOP-QK7OLED:/mnt/c/Users/jleads$ dd if=/dev/zero of=/mnt/e/testfile bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 11.7056 s, 89.6 MB/s

 WSL1(330 MB/s)  比  WSL2(89.6 MB/s) 快了三倍多。

 

测试一下读取速度:

WSL1:

#先删除缓存#这句话在WSL1 1804上执行失败,不知原因
sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'

测试速度

jiang@DESKTOP-QK7OLED:~$ dd if=/mnt/e/testfile of=/dev/null bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 0.183714 s, 5.7 GB/s

WSL2:

先删除缓存

lzjiang@DESKTOP-QK7OLED:/mnt/c/Users/jleads$ sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'
测试速度

lzjiang@DESKTOP-QK7OLED:/mnt/c/Users/jleads$ dd if=/mnt/e/testfile of=/dev/null bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 3.74485 s, 280 MB/s

WSL1(5.7 GB/s)是 WSL2(280 MB/s)的20倍!

主要原因是WSL2采用是9P协议访问Windows的驱动器,这个协议目前比较慢:

WSL2 uses the 9P protocol to access Windows drives, and it is currently (See Footnote) known to be very slow when compared to:

  • Native NTFS (obviously)
  • The ext4 filesystem on the virtual disk used by WSL2
  • And even the performance of WSL1 with Windows drives

---------------https://stackoverflow.com/a/68974497/6386067

WSL开发人员的有较为详细的解释https://github.com/microsoft/WSL/issues/4197#issuecomment-604592340 。

个人理解,WSL2的操作系统运行在Linux的原生文件系统(ext)上,WSL2文件系统本身相当于一个封闭的箱子,在操作其他文件系统的文件(比如母系统Windows系统的文件)时,WSL2要先"走出这个箱子"——进行文件系统格式的转换,所以比较慢。

WSL1的操作系统是运行在Windows的文件系统(NTFS)上,WSL1的文件系统的和母系统Windows的文件系统格式相同,在操作“挂载在Windows系统上的磁盘”的文件时,不涉及IO转换,所以会快一些。但是WSL1的操作系统本身是“翻译成Windows”的Linux。

 

解决WSL2访问Windows系统文件慢的一个办法是,改用NFS协议链接(参见https://stackoverflow.com/a/76314967/6386067)。(经测试,效果存疑)

大致办法如下

1. 在Windows母系统设置NFS目录共享。

此步骤实现方案有很多,比如借助第三方软件 haneWIN NFS Server for Windows(https://hanewin.net/shop/en.htm),在Windows上进行设置。

比如参考https://www.cnblogs.com/fuzidage/p/17490699.html相应步骤设置

2. 在WSL2子系统安装nfs-common工具,挂载这个共享目录

测试一下此办法挂载目录的效果

WSL2+NFS

写入速度

删除缓存

lzjiang@DESKTOP-QK7OLED:/mnt/c/Users/jleads$ sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'
测试速度

lzjiang@DESKTOP-QK7OLED:/mnt/c/Users/jleads$ dd if=/dev/zero of=/mnt/nfs/testfile bs=1M count=1000 conv=fsync
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 33.5345 s, 31.3 MB/s

 

读取速度

删除缓存

lzjiang@DESKTOP-QK7OLED:/mnt/c/Users/jleads$ sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'

运行测试

lzjiang@DESKTOP-QK7OLED:/mnt/c/Users/jleads$ dd if=/mnt/nfs/testfile of=/dev/null bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 9.29422 s, 113 MB/s 

似乎还没有不加NFS快,不知是何原因!

 

再运行一组测试,运行相同程序

WSL1

jiang@DESKTOP-QK7OLED:/mnt/e/Doksuri/scripts$ time ncl draw_maximum_rain_position_terrain_time_average_divergence.ncl
real 0m1.423s

user 0m0.656s

sys 0m0.766s

 

WSL2

(ncl_stable) lzjiang@DESKTOP-QK7OLED:/mnt/e/Doksuri/scripts$ time ncl draw_maximum_rain_position_terrain_time_average_divergence.ncl
real 0m4.792s

user 0m1.247s
sys 0m0.178s

 

 WSL2(NFS)挂载

(ncl_stable) lzjiang@DESKTOP-QK7OLED:/mnt/nfs/scripts$ time ncl draw_maximum_rain_position_terrain_time_average_divergence.ncl

real 0m16.880s

user 0m1.260s
sys 0m0.800s

 

实践再次证明,NFS使用挂载后,程序运行没有更快,反而更慢!

只能说,可能是因为nfs没有配置好,数据经过了网关路由器,所以慢了!

 

参照了https://stackoverflow.com/a/76314967/6386067的说法,使用WSL1IP的地址作为Windows母系统的IP地址,WSL2+NFS的速度有提升,但是仍然没有WSL2快,更不用说和WSL1比了。所以,暂时认为,NFS挂载效果不大!

 

 

 

还有一种理论的办法,不经过Windows系统挂载,直接从WSL2子系统挂载磁盘。https://learn.microsoft.com/en-us/windows/wsl/wsl2-mount-disk

测试一下,有一个格式为exFAT的移动硬盘,从WSL2直接挂载试试,不知道是否可以呢,速度是否有提升呢?

经过测试,WSL2目前还不支持直接挂载exFAT格式的磁盘。其他格式可能支持,但没有尝试过。

尝试记录

#Powershell admin命令行
#查询挂载的驱动器ID
PS C:\Windows\system32> GET-CimInstance -query "SELECT * from Win32_DiskDrive" DeviceID Caption Partitions Size Model -------- ------- ---------- ---- ----- \\.\PHYSICALDRIVE0 SSDPEMKF512G8 NVMe INTEL 512GB 3 512105932800 SSDPEMKF512G8 NVMe INTEL 512GB \\.\PHYSICALDRIVE1 WD My Passport 2682 USB Device 1 5000945564160 WD My Passport 2682 USB Device \\.\PHYSICALDRIVE2 WD Elements SE SSD SCSI Disk Device 1 2000396321280 WD Elements SE SSD SCSI Disk Device

#使用--mount选项直接挂载磁盘。失败,WSL2不支持格式。
PS C:\Windows\system32> wsl --mount \\.\PHYSICALDRIVE2
The disk was attached but failed to mount: Invalid argument.
For more details, run 'dmesg' inside WSL2.
To detach the disk, run 'wsl.exe --unmount \\.\PHYSICALDRIVE2'.

#卸载磁盘。
PS C:\Windows\system32> wsl --unmount \\.\PHYSICALDRIVE2
The operation completed successfully.

#尝试使用--bare指令,分两步挂载
#第一步,在PowerShell中挂载
PS C:\Windows\system32> wsl --mount \\.\PHYSICALDRIVE2 --bare
The operation completed successfully.
#第二步,在bash中调用mount -t选项挂载。提示错误,无法挂载
root@DESKTOP-QK7OLED:~# sudo mount -t exfat /dev/sdd1 /mnt/e mount: /mnt/e: unknown filesystem type 'exfat'. dmesg(1) may have more information after failed mount system call.

 

posted @ 2024-07-31 04:58  chinagod  阅读(104)  评论(0编辑  收藏  举报