Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...