Search code examples
windowscommand-linewindows-services

Limit the number of CPU cores used by an Windows service running from net start command


I am running an benchmark for PostgreSQL 12 on Windows 10. I want to limit the number of CPU cores used by PostgreSQL service to test how does the CPU performance affect TPU.

Now I am starting PostgreSQL service with following command:

 net start postgresql-x64-12

and I know how to limit the number of CPU cores for ordinary Windows application like:

 start /affinity 1 "" "C:\Windows\System32\calc.exe"

How can I limit the number of CPU cores used by an Windows service running from net start command? Is there an /affinity option equivalent in net start command?


Solution

  • I found a solution. First, you cannot set CPU affinity to Windows system processes or services (see https://www.atmarkit.co.jp/ait/articles/0703/16/news151.html (Japanese)).

    In my situation, I can run PostgreSQL process from pg_ctl command from cmd.exe with /affinity option like:

    cmd.exe /c "start /affinity 1F /B c:\path\to\PostgreSQL\12\bin\pg_ctl.exe start -w -s -D C:\path\to\PostgreSQL\12\data"
    

    Note that you cannot use Start-Process cmdlet and ProcessorAffinity property like this:

    $app = Start-Process 'c:\path\to\PostgreSQL\12\bin\pg_ctl.exe' 'start -D C:\path\to\PostgreSQL\12\data' -PassThru -NoNewWindow
    $app.ProcessorAffinity = 0x3
    

    This causes SetValueInvocationException because pg_ctl.exe is immediately exit after it starts PostgreSQL instance.