How to increase WSL/docker container performance on quite common laptop?

6.6k views Asked by At

I have this PC configuration, which should have quite normal performance compared to common laptops:

ASUS TUF GAMING A15  
AMD Ryzen 7 4800H  
16GB RAM on 3200 MHz  
SSD  
64b Windows 10

First, I setup WSL2 and it was rather slow and I realized that the OpenSuse over WSL was probably swapping, because the processor has a lot of cores, but the 16GB was not enough for it.
So basically I open .wslconfig and set processors=2 and swap=0, it greatly improved performance of the WSL, probably stopped swapping.

  1. Was it a good idea?

So I was using the WSL quite happily, but after some time, I needed to install VMware for testing some functionality and it worked until the first reboot of the virtual machine and after that I realized that I can't use both WSL and VMware on 1 system, because the VMware wanted me to disable some option on Windows that was needed by WSL. (I think it was something with virtualization)

But after using the VMware I used the WSL again and realized that it's way slower. I checked the changes from the git, I rebuilt everything, I reverted to versions as before, but it was still way slower than before. I needed to start using docker container at that time (because of different reason), it was running quite fast at first so I did not pay much attention. But then I realized that some operations run very slowly and I did not know, why. Then I realized it must have been that VMware, so I uninstalled it and the WSL got faster, only the WSL, but the slowness in docker container remained.

When I check a Task Manager, CPU and Disk seem totally fine and Memory seems fine as well. So I guess the docker container is probably not using all available resources? The task runs 30 minutes instead of running 10 seconds which runs on similar PC directly, without docker container, it's huge difference...

Before running task in docker container: Performance Processes

While running task in docker container: Performance enter image description here

  1. How to set VSCode to make the docker container use much more resources and run the task almost as fast as without running it in a docker container?
    I realized it's maybe related to Antimalware Service Executable process, which takes around 5% CPU when running the task and uses Microsoft Defender Antivirus Service service. Can it be the problem? If so, how to add exception for the docker container or VSCode which runs it, in the least intrusive way? Disabling Real-time protection only removed its 5% CPU usage, but did not make the container task any faster.

I called docker stats and it's really not using almost any resources at all, see: docker stats

3

There are 3 answers

0
VonC On

Check also if the cores are fully used.
Issues like microsoft/WSL #6923 "WSL2 does not regconize 72c/144t (4 sockets server) only 64c" illustrate the fact that not all cores are always used on WSL2.

See the 2020 issue 5423: "WSL 2 uses half the number of cores on AMD Threadripper 3990X"

It references xieyubo/WSL2, which is about using more than 64 cores.
It is not matching your use-case with your 2020 8-cores AMD Ryzen 7 4800H), but their instruction (about changing the hypervisor scheduler type) could still have a beneficial impact.

You can see all CPUs in WSL2, but actually, you might still can't use all of them. For example, if you run:

make -j`nproc`

from task manager, you might still see only 50% CPUs are working. That because the default HyperV scheduler is Root, all tasks are still not scheduled cross all CPU groups. You can run the following command from Powershell console to check your current HyperV scheduler:

Get-WinEvent -FilterHashTable @{ProviderName="Microsoft-Windows-Hyper-V -Hypervisor"; ID=2} -MaxEvents 1

If it outputs Hypervisor scheduler type is 0x4, it means you are using Root scheduler. You need run the following command to change the scheduler type to Core:

bcdedit /set hypervisorschedulertype Core

Restart machine to take the settings effect. Note, if you enable BitLocker, you might need input the BitLocker key after restarting. So please save your BitLocker key before restarting.

Now, you can use full cores really!

0
ganadara On
  • On a Linux file system: /home/yourdir in WSL2
  • On a Windows file system: /mnt/c/yourdir

Internally converts file formats between Windows (NTFS) and Linux (extf).

Also:

docker on wsl2 very slow

0
Dave Amphlett On

VSCode dev containers is supposed to make this kind of setup repeatable / shareable, but on Windows I've found the performance to be unusable which all seemed to boil down to the speed delay in wsl2 mapping files to the host.

I've settled on running VSCode and docker inside a Linux VM on Windows, and have a 96% time saving in things like running up a server and watching code for changes making this setup my preferred way now.

The standardisation of devcontainer.json and being able reliably share dev setup between developers, and even to use github codespaces if you're away from your normal dev machine make this whole setup a pleasure to use.

see https://stackoverflow.com/a/72787362/183005 for detailed timing comparison and setup details