Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce Visual Studio build process priority to prevent unresponsive system?

We are compiling big C++ projects using Visual Studio (2013) on a regular basis. Building the source can take up to 45 mins on a developer machine. During this time, the machine often becomes unresponsive due to 100% CPU load. Is there any way to tell Visual Studio to run the compile/build tools with a lower process priority so that the Windows UI isn't slowed down? I'd happily have the machine spend a bit more time overall on the build process if i could do other things meanwhile.

The problem has even become more annoying since installing an SSD! Although the overall build time is significantly improved, now that there is no more disk bottleneck, the build process runs into CPU max load even more often, with an unresponsive system as a consequence.

How to solve this issue?

like image 523
Scrontch Avatar asked Nov 27 '18 16:11

Scrontch


2 Answers

Update

I've just found the following thread, where someone by the name of Mikhail Virovets provided an automatic solution: https://developercommunity.visualstudio.com/idea/436208/limit-cpu-usage-of-visual-studio.html

In the registry under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\cl.exe\PerfOptions, create a DWORD value called CpuPriorityClass and set it to 5. This will set the base priority of all newly created processes with that name to below normal. You should probably also create a similar entry for the linker.

Of course this is a little bit more invasive as it will affect all processes with those names, regardless of where, when and how they are created. But I think it will probably work fine.

Just don't forget you have added those values in case it causes some unwanted slowdowns in the future, otherwise you may be looking for the problem for a long time.

You can use the following .reg file to set the CPU and IO priority of the compiler and linker to "below normal"/"low":

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\cl.exe]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\cl.exe\PerfOptions]
"CpuPriorityClass"=dword:00000005
"IoPriority"=dword:00000001

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\link.exe]
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\link.exe\PerfOptions]
"CpuPriorityClass"=dword:00000005
"IoPriority"=dword:00000001

Original Answer

I was facing the same problem and it really annoyed me. As a stop gap solution, I have created a small batch file that simply lowers the process priority of the most important build tools:

wmic process where name="msbuild.exe" CALL setpriority "idle"
wmic process where name="cl.exe" CALL setpriority "idle"
wmic process where name="link.exe" CALL setpriority "idle"

(This is for C++ builds, if you build something else you may want to change the .exe names to whatever your main build tools are.)

Of course this only affects processes that are currently running, so you have to run it every time you start a build. The priorities of newly created sub-processes (compiler, linker, ...) are being inherited from the main MSBuild process though, so this will not only affect the currently running compiler/linker instances but the whole build.

I've added an "external tool" entry with Command: cmd.exe and Arguments: /c C:\Path\To\My\LowerBuildPriority.bat so I can start the batch file from within Visual Studio.

A more convenient solution would probably be to add a custom task to the build that alters the process priority. Since AFAIK tasks are executed from within the main MSBuild process, this should work. But I haven't gotten around to trying it yet.

like image 190
Paul Groke Avatar answered Nov 15 '22 03:11

Paul Groke


You can now set check box in Visual Studio 2022 under Tools -> Options -> Projects and Solutions -> Build And Run -> “Run build at low process priority”. This will run all compile and link processes at "Below normal" priority

Update on Paul Groke's answer and thread linked in his answer https://developercommunity.visualstudio.com/idea/436208/limit-cpu-usage-of-visual-studio.html, because I cannot comment yet

like image 2
Roman Danielis Avatar answered Nov 15 '22 03:11

Roman Danielis