Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify processor (core) is used by specific thread

I would like to know if it is possible to identify physical processor (core) used by thread with specific thread-id?

For example, I have a multithreaded application that has two (2) threads (thread-id = 10 and thread-id = 20, for instance). I run the application on a system that has a dual core processor (core 1 and core 2). So, how do I to get core number used by thread with thread-id = 20?

P.S. Windows platforms.

Thank you,

Denis.

like image 513
DenisL Avatar asked Jul 29 '10 21:07

DenisL


People also ask

How do I know which core my thread is running on?

To get the information you want, look in /proc/<pid>/task/<tid>/status. The third field will be an 'R' if the thread is running. The sixth from the last field will be the core the thread is currently running on, or the core it last ran on (or was migrated to) if it's not currently running.

How do I check CPU cores and threads?

Press Ctrl + Shift + Esc to open Task Manager. Select the Performance tab to see how many cores and logical processors your PC has.

What is a processor core thread?

Core is a physical hardware component whereas thread is the virtual component that manages the tasks of the core. Cores enable completion of more work at a time, while threads enhance computational speed and throughput. Cores use content switching but threads use multiple processors for executing different processes.

What is the use of core and thread in processor?

Cores increase the amount of work accomplished at a time, whereas threads improve throughput, computational speed-up. Cores is an actual hardware component whereas thread is a virtual component that manages the tasks. Cores use content switching while threads use multiple CPUs for operating numerous processes.


2 Answers

Unless you use thread-affinity, threads are not assigned to specific cores. With every time slice, the thread can be executed on different cores. This means that if there would be a function to get the core of a thread, by the time you get the return value, there's a big chance that the thread is already executing on another core.

If you are using thread-affinity, you could take a look at the Windows thread-affinity functions (http://msdn.microsoft.com/en-us/library/ms684847%28v=VS.85%29.aspx).

like image 169
Patrick Avatar answered Nov 01 '22 16:11

Patrick


There are functions called GetCurrentProcessorNumber (available since Server 2003 and Vista) and GetCurrentProcessorNumberEx (available since Server 2008 R2 and Windows 7).

See also this question's answers for more related options and considerations (including Windows XP - primarily this answer describing the use of cpuid instruction).

Of course the core number can be changed any time by the scheduler so if You need to be sure then perhaps it helps for a reasonable amount if You check the core number both before and after something You measured or executed for a short amount of time, and if the core number is still same then You know on which core most likely the intermediate code also executed.

like image 27
Roland Pihlakas Avatar answered Nov 01 '22 18:11

Roland Pihlakas