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:
Win32Exception (87): The parameter is incorrect
When I try and set it via Task Manager (as an admin with UAC disabled) I get:
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?