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.
You can use ps command to find out which process is currently assigned to which CPU core. Lookout for the PSR field in ps command output. The above command output indicates that the process with PID 24868 (crond) is assigned to CPU core 2.
The answer is: It depends. On a system with multiple processors or CPU cores (as is common with modern processors), multiple processes or threads can be executed in parallel. On a single processor, though, it is not possible to have processes or threads truly executing at the same time.
In short: yes, a thread can run on different cores.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With