processor affinity group C#

↘锁芯ラ 提交于 2021-02-09 05:30:32

问题


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"/> 

回答1:


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
    }
}


来源:https://stackoverflow.com/questions/44854648/processor-affinity-group-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!