Identify processor (core) is used by specific thread
Asked Answered
O

2

6

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.

Ocarina answered 29/7, 2010 at 21:3 Comment(0)
S
7

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).

Sandpiper answered 29/7, 2010 at 21:23 Comment(4)
Thank you for your response, Patrick! So another question is appeared - is it possible to get to know how much time a specific thread runs on specific core? (for example some thread runs 20% of its time slice on core 1 and 80% of its time slice runs on core 2)Ocarina
Not that I'm aware of Denis. What you can do is limit a thread to specific cores (this is called thread-affinity). Maybe you can split up your process in multiple threads where each thread is assigned to one core. Can you explain what the original problem is? (leading to your original question) Maybe your problem can be solved in another way.Sandpiper
I've been tasked to create two functions: 1. The first function returns total multicore processor usage by specific thread id. I've implemented the function using Performance Counter Functions. 2. The second function returns specific core usage by specific thread id. I have no idea how to create the function, it seems it is impossible.Ocarina
I also think this is impossible.Sandpiper
C
3

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.

Cirilla answered 19/12, 2013 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.