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

后端 未结 1 1301
忘了有多久
忘了有多久 2021-02-14 23:10

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

相关标签:
1条回答
  • 2021-02-14 23:29

    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.

    0 讨论(0)
提交回复
热议问题