C # processor affinity group

I am using Windows Server 2016 with 72 cores. I see that there are 2 groups of cpus. my .net application will use one or the other groups. I need to force my application to use a group of its choice. Below is some sample code, but I can't get it to work. I might be passing in the wrong variables. I want the application to select group 1 and all processors select group 2 and all processors.

My question is, how do I force a .net application to use group 1 or group 2? I'm not sure if the link below will work.

https://gist.github.com/alexandrnikitin/babfa4781c68f1664d4a81339fe3a0aa

I tried adding this to my config, but the app only uses group 0, but I am showing all cores with this code. I know the option is to go to bios and choose flatten, but I'm not sure if this is the correct way to do things.

   <Thread_UseAllCpuGroups enabled="true"/>  
      <GCCpuGroup enabled="true"/>  
      <gcServer enabled="true"/> 

      

+3


source to share


1 answer


The above example only sets the current thread to the processor group of the processor. But you want to set it for all threads in the process. You need to call SetProcessAffinityMask for your process.

There is no need for PInvoke for SetProcessAffinityMask, because the Process class already has a ProcessorAffinity property that allows you to set it directly.



class Program
{
    static void SetProcessAffinity(ulong groupMask)
    {
        Process.GetCurrentProcess().ProcessorAffinity = new IntPtr((long)groupMask);
    }
    static void Main(string[] args)
    {
        SetProcessAffinity(1);    // group 0
        // binary literals are a C# 7 feature for which you need VS 2017 or later.
        SetProcessAffinity(0b11); // Both groups 0 and 1 
        SetProcessAffinity(0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111); // for all cpu groups all 64 bits enabled
    }
}

      

+2


source







All Articles