How can I see which CPU core a thread is running in?

flypen picture flypen · Nov 7, 2011 · Viewed 92.7k times · Source

In Linux, supposing a thread's pid is [pid], from the directory /proc/[pid] we can get many useful information. For example, these proc files, /proc/[pid]/status,/proc/[pid]/stat and /proc/[pid]/schedstat are all useful. But how can I get the CPU core number that a thread is running in? If a thread is in sleep state, how can I know which core it will run after it is scheduled again?

BTW, is there a way to dump the process(thread) list of running and sleeping tasks for each CPU core?

Answer

maltanar picture maltanar · Jul 12, 2012

The "top" command may help towards this, it does not have CPU-grouped list of threads but rather you can see the list of threads (probably for a single process) and which CPU cores the threads are running on by

top -H -p {PROC_ID}

then pressing f to go into field selection, j to enable the CPU core column, and Enter to display.