C# Core 2.1 使用 2 个 CPU

Cr4*_*ter 0 c# multithreading multiprocessing .net-core

I'm Currently building an application which relies heavily on running multithreaded. I'm Currently spawning as many threads as the system has cores using the environment.ProcessorCount constant, but when I run the application on our server which has 2 separate CPUs (Intel Xeon E5-2620) all threads work on the first CPU while the second CPU idles. Is it possible to utilize the second CPU in one process or do I need to split the process and force the utilisation with Processor affinity?

编辑:

我尝试使用 IdealProcessor 设置亲和力以拆分为 CPU 0 和 1,但没有成功。

Dmy*_*lov 7

看起来您的多处理器架构是NUMA,这意味着 CPU 对不同内存区域的访问时间不同。具有这种架构的系统平衡 CPU 之间的负载,以便仅加载那些“更接近”(具有最低访问时间)正在运行的内存区域的处理器。如果是有序的 .NET 应用程序标准内存布局意味着驻留在相同的内存区域中,这在 NUMA 架构上导致仅使用那些更接近此的 CPU(在您的情况下,它可以是 2 个 NUMA 节点,每个节点有 1 个 CPU,只有 1 个我们使用它是因为它为您的应用程序使用的内存区域提供服务)。

需要从 NUMA 架构中获益的应用程序应该使用特定的 API,这些 API 会在其他调用中公开这些调用,以指示在哪个 NUMA 节点中分配内存(这里是Windows 提供的 API 函数的示例)。从 4.5 版开始的 .NET CLR 能够通过特定的配置设置间接使用此 API。在 CLR 运行时,您需要在应用程序设置中设置以下选项:

<configuration>
   <runtime>
      <gcServer enabled="true"/>
      <Thread_UseAllCpuGroups  enabled="true"/>
   </runtime>
</configuration>
Run Code Online (Sandbox Code Playgroud)

其中gcServermode 控制内存分配的 NUMA 感知,以便运行时可以支持不同 NUMA 节点的多个堆,并Thread_UseAllCpuGroups控制线程池的 NUMA 感知。

但是,对于 .NET Core 运行时,您需要使用运行时选项打开 gcServer 模式,而Thread_UseAllCpuGroups作为ThreadPool 设置的一部分,可以根据前缀COMPlus_(即set COMPlus_Thread_UseAllCpuGroups=1)通过环境变量传递。