简体   繁体   English

如何确定线程运行的CPU?

[英]How to determine which CPU a thread runs on?

Is there a way to determine on which CPU a given thread runs on? 有没有办法确定给定线程在哪个CPU上运行? Preferably in C#, but C++ would do. 最好是在C#中,但C ++会这样做。

The .NET Process and ProcessThread classes don't seem to provide this information. .NET Process和ProcessThread类似乎不提供此信息。

ETA Clarifications: ETA澄清:

We are developing a server application that processes http multicast streams and spawns multiple video encoders. 我们正在开发一个服务器应用程序,用于处理http多播流并生成多个视频编码器。 This runs on a system with 12 physical cores, resulting in 24 logical CPUs (hyperthreading). 这在具有12个物理内核的系统上运行,产生24个逻辑CPU(超线程)。 Via TaskManager and ProcessExplorer we have verified that our spawned processes spread evenly over the logical CPUs. 通过TaskManager和ProcessExplorer,我们验证了我们生成的进程在逻辑CPU上均匀分布。 However, we are seeing a lot of (kernel?) activity on just one CPU that interferes by eating up unusual amounts of CPU time. 但是,我们在一个CPU上看到了很多(内核?)活动,这些活动因占用不寻常的CPU时间而产生干扰。 We are trying to identify which process(es)/thread(s) are running on this particular CPU. 我们正在尝试确定在此特定CPU上运行的进程/线程。 Neither TaskManager nor ProcessExplorer seem to provide that information. TaskManager和ProcessExplorer似乎都没有提供这些信息。 If they do, please explain how such information can be obtained. 如果他们这样做,请解释如何获得这些信息。

Otherwise, we are contemplating writing our own tool to get this information. 否则,我们正在考虑编写自己的工具来获取此信息。 And that is what we need help with. 这就是我们需要帮助的地方。

We know how to change a threads affinity (and we know that there is no guarantee that a thread will remain associated with any CPU, although in this particular case the thread(s) eating up CPU remain associated with only one CPU), but in order to do so, we need to first determine WHICH process/thread needs to be relocated. 我们知道如何更改线程亲缘关系(我们知道无法保证线程将与任何CPU保持关联,尽管在这种特殊情况下,占用CPU的线程仍然只与一个CPU相关联),但是为了做到这一点,我们需要首先确定需要重新定位WHICH进程/线程。 That is the sole objective of this question. 这是这个问题的唯一目标。

I hope this helps clarifying the issue. 我希望这有助于澄清问题。

From MSDN , using the ProcessThread.ProcessorAffinity property you can set the thread affinity, but you cannot get it. MSDN中 ,使用ProcessThread.ProcessorAffinity属性可以设置线程关联,但是无法获取它。 By default threads have no affinity (can operate on any processor). 默认情况下,线程没有亲和力(可以在任何处理器上运行)。

using System;
using System.Diagnostics;

namespace ProcessThreadIdealProcessor
{
    class Program
    {
        static void Main(string[] args)
        {
            // Make sure there is an instance of notepad running.
            Process[] notepads = Process.GetProcessesByName("notepad");
            if (notepads.Length == 0)
                Process.Start("notepad");
            ProcessThreadCollection threads;
            //Process[] notepads;
            // Retrieve the Notepad processes.
            notepads = Process.GetProcessesByName("Notepad");
            // Get the ProcessThread collection for the first instance
            threads = notepads[0].Threads;
            // Set the properties on the first ProcessThread in the collection
            threads[0].IdealProcessor = 0;
            threads[0].ProcessorAffinity = (IntPtr)1;
        }
    }
}

Similarly Thread.SetProcessorAffinity does the same thing. 类似地, Thread.SetProcessorAffinity做同样的事情。

This is not possible to do in a continuous reliable way. 这是不可能以连续可靠的方式进行的。 OS task scheduler optimises threads and splits load between available CPU cores. OS任务调度程序优化线程并在可用CPU核心之间拆分负载。 In general case a thread can be executed on any CPU. 通常情况下,可以在任何CPU上执行线程。 Moreover with context switches it can also change it's CPU. 此外,通过上下文切换,它还可以改变它的CPU。

If you need to pin-point specific a thread or a process you can only assign its affinity, so you can have reasonable hope that the process/thread will be executed on a particular logical CPU. 如果您需要精确定位一个线程或一个进程,您只能分配它的亲和性,那么您可以合理地希望进程/线程将在特定的逻辑CPU上执行。

Since Vista/Server2003, you can PInvoke the GetCurrentProcessorNumber() Windows API method ( https://docs.microsoft.com/en-gb/windows/desktop/api/processthreadsapi/nf-processthreadsapi-getcurrentprocessornumber ). 从Vista / Server2003开始,您可以PInvoke GetCurrentProcessorNumber() Windows API方法( https://docs.microsoft.com/en-gb/windows/desktop/api/processthreadsapi/nf-processthreadsapigetcurrentprocessornumber )。

Obviously Windows-only, so not ideal if you plan to support other platforms. 显然只有Windows,如果你计划支持其他平台,那就不理想了。

I'm not sure the OP's use case was ideal, but where it might be sensible/useful is when trying to create a timestamp that is guaranteed unique within a system, even if all processors are computing one at exactly the same time. 我不确定OP的用例是否理想,但在尝试创建一个在系统中保证唯一的时间戳时,它可能是合理的/有用的,即使所有处理器都在同一时间计算一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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