简体   繁体   中英

How to determine which CPU a thread runs on?

Is there a way to determine on which CPU a given thread runs on? Preferably in C#, but C++ would do.

The .NET Process and ProcessThread classes don't seem to provide this information.

ETA Clarifications:

We are developing a server application that processes http multicast streams and spawns multiple video encoders. This runs on a system with 12 physical cores, resulting in 24 logical CPUs (hyperthreading). Via TaskManager and ProcessExplorer we have verified that our spawned processes spread evenly over the logical CPUs. However, we are seeing a lot of (kernel?) activity on just one CPU that interferes by eating up unusual amounts of CPU time. We are trying to identify which process(es)/thread(s) are running on this particular CPU. Neither TaskManager nor ProcessExplorer seem to provide that information. 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. 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. 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.

This is not possible to do in a continuous reliable way. OS task scheduler optimises threads and splits load between available CPU cores. In general case a thread can be executed on any CPU. Moreover with context switches it can also change it's 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.

Since Vista/Server2003, you can PInvoke the GetCurrentProcessorNumber() Windows API method ( https://docs.microsoft.com/en-gb/windows/desktop/api/processthreadsapi/nf-processthreadsapi-getcurrentprocessornumber ).

Obviously Windows-only, so not ideal if you plan to support other platforms.

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.

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