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?
The answer below is no longer accurate as of 2014
Tasks don’t sleep in any particular core. And the scheduler won’t know ahead of time which core it will run a thread on because that will depend on future usage of those cores.
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.
Not currently running. Last ran on core 3.
Currently running on core 2.
To see what the rest of the fields mean, have a look at the Linux kernel source — specifically the
do_task_statfunction infs/proc/array.corDocumentation/filesystems/stat.txt.Note that all of this information may be obsolete by the time you get it. It was true at some point between when you made the
opencall on the file in proc and when that call returned.