How to See Which CPU Core a Thread Is Running In

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


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.

31466 (bc) S 31348 31466 31348 34819 31466 4202496 2557 0 0 0 5006 16 0 0 20 0 1 0 10196934 121827328 1091 18446744073709551615 4194304 4271839 140737264235072 140737264232056 217976807456 0 0 0 137912326 18446744071581662243 0 0 17 3 0 0 0 0 0

Not currently running. Last ran on core 3.

31466 (bc) R 31348 31466 31348 34819 31466 4202496 2557 0 0 0 3818 12 0 0 20 0 1 0 10196934 121827328 1091 18446744073709551615 4194304 4271839 140737264235072 140737264231824 4235516 0 0 0 2 0 0 0 17 2 0 0 0 0 0

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_stat function in fs/proc/array.c or Documentation/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 open call on the file in proc and when that call returned.

Determine CPU on which the calling thread is running?

Your computer has 2 cores and 4 "logical processors" (CPUs). Because you have 4 CPUs the ID numbers are from 0 to 3.

The main problem here is terminology - how "CPU" (and "thread") is defined. You're assuming "CPU" is defined as "core", but most people define it as "logical processor" or "(hardware) thread"; and with hyper-threading you have 2 logical processors per core so you end up with 4 CPUs (from 2 cores).

Check which thread is running on which CPU in linux

Funny that people downvotes but don't tell why. I searched for a while and found that following command gives which thread running on which CPU:

ps -p <PID> -L -o pid,tid,psr

Where < PID > is the pid of the process we are interested in.

How to find on which CPU&Core my thread is running

You might want to check the GetCurrentProcessorNumber API (or GetCurrentProcessorNumberEx if you have more than 64 logical processors).

How can I find which processor each thread is running on?

Use GetCurrentProcessorNumber() ( http://msdn.microsoft.com/en-us/library/windows/desktop/ms683181(v=vs.85).aspx )

Android: Find out which core the thread is running on

The good news is, the necessary library and system call are defined on Android (sched_getcpu() and __getcpu()). The bad news is, they aren't part of the NDK.

You can roll your own syscall wrapper and library call, using the method shown in this answer.

Another approach is to read /proc/self/stat and parse out the processor entry. The proc(5) man page describes the format:

  /proc/[pid]/stat
Status information about the process. This is used by ps(1).
It is defined in /usr/src/linux/fs/proc/array.c.
...
processor %d (since Linux 2.2.8)
CPU number last executed on.

This is much slower, and the "file" format may change if the kernel is updated, so it's not the recommended approach.

Is it possible to check on which processor a thread is running in JAVA?


Is it possible to check on which processor a thread was running?

Not sure about VisualVM, but you can get OS thread IDs for java threads with jstack utility.

Then you can find out on which processors the threads are running via any method provided by your OS.



Related Topics



Leave a reply



Submit