Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...