Windows Server 2022 - Setting affinity in .NET 6 application errors

Marcus Smallman 1 Reputation point
2022-07-19T12:53:17.127+00:00

Hi,

I have a Windows Server 2022 machine with 40 cores and 80 logical processors. Whenever I try and run my application on this machine and attempt to set it's affinity in code or via Task Manager, I get errors.

The error I get in code is:

222324-image.png

Win32Exception (87): The parameter is incorrect   

When I try and set it via Task Manager (as an admin with UAC disabled) I get:

222361-image.png

Unable to access or set process affinity  

Also, when I try and get the affinity mask for the process in code, I get 0 back.

After reading the docs:

https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getprocessaffinitymask
https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setprocessaffinitymask
https://learn.microsoft.com/en-us/windows/win32/procthread/processor-groups

It is clear that on Windows 11 and Server 2022 that threads are not bound to a single processor group any more if there are more than 64 processors and can run across multiple processor groups.

My theory is that I am unable to set the affinity for my application because it is performing actions on threads across multiple processor groups.

I have tried setting the affinity mask at startup in my application using:

Process proc = Process.GetCurrentProcess();  
long affinityMask = (long)proc.ProcessorAffinity;  
affinityMask &= 15; // First 4 processors  
proc.ProcessorAffinity = (IntPtr)affinityMask;  

But I just get the Win32Exception (87): The parameter is incorrect

And when I try to get the affinity mask at start up it is set to 0 meaning it was unable to get it because there are threads in multiple groups.

So for me, is it possible to set a .NET 6 application to ONLY run in a single processor group on Windows Server 2022 before the application runs, so I can then set its affinity mask?

Windows for business | Windows Server | User experience | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 39,921 Reputation points
    2022-07-20T09:53:22.247+00:00

    Hi there,

    The affinity type determines how the job scheduler interacts with your applications to assign processor cores to tasks in a job. If the job scheduler sets affinity for a task, it overrides the affinity that is set by the application.

    Try this, Open Task Manager - Click “More details”-Right-click the app you want to modify, and click “Go to details”-Right-click the app in the “Details” window and choose “Set affinity”-Select the cores/logical cores you’d like to assign to the program.

    To set the processor affinity for a process using PowerShell, run the command:

    $process = Get-Process pwsh
    $process.ProcessorAffinity = *

    ------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.