处理器关联组 C# [英] processor affinity group C#

查看:63
本文介绍了处理器关联组 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 72 核的 Windows Server 2016.我看到有两组处理器.我的 .net 应用程序将使用一个或其他组.我需要能够强制我的应用程序使用我选择的组.我在下面看到一个代码示例,但我无法使其工作.我可能传递了错误的变量.我希望应用选择第 1 组和所有处理器,然后选择第 2 组和所有处理器.

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.

我的问题是如何强制我的 .net 应用程序使用组 1 或组 2?我不确定下面的链接是否有效.

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

我确实尝试将其添加到我的配置中,但该应用程序仅使用组 0,但我确实使用此代码显示了所有核心.我知道一个选项是进入 bios 并选择 flatten,但我不确定这是做事的正确方法.

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

推荐答案

发布的示例仅将当前线程设置为 CPU 处理器组.但是您想为进程的所有线程设置它.您需要为您的流程调用 SetProcessAffinityMask.

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.

不需要对 SetProcessAffinityMask 进行 PInvoke,因为 Process 类已经有一个属性 ProcessorAffinity 可以让您直接设置它.

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

这篇关于处理器关联组 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆