简体   繁体   中英

processor affinity group C#

I'm using Windows Server 2016 with 72 cores. I see that there are 2 groups of processors. my .net app will use one or the other groups. I need to be able to force my app to use the Group of my choice. I see a code example below but I am unable to make it work. I might be passing the wrong variables. I want the app to pick group 1 and all the processors then group 2 and all the processors.

my question is how do i force my .net app to use group 1 or group 2? i am not sure if the link below will work.

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

I did try adding this to my config but the app only uses group 0 but i do show all the cores with this code. I know an option is to go to the bios and choose flatten but i'm not sure that is the right way to do things.

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

The posted examply only sets current thread to a CPU processor group. But you want to set it for all threads of a process. You need to call SetProcessAffinityMask for your process.

There is no need to PInvoke to SetProcessAffinityMask because the Process class already has a property ProcessorAffinity which lets you 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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM