Sergey L.
Sergey L.

Reputation: 22552

get thread last scheduled CPU/core

I have a number of living threads in my application for which I have pthread_t IDs and can get other IDs if necessary. Those are stored in a separate array.

How can I determine the ID/number of the CPU that a specific thread is currently running (or one it was recently run on) calling from another thread.

sched_getcpu(2) only works for the calling thread and pthread_getaffinity_np only gives me the allowed cpu mask. /proc is being equally as useless only giving me a CPU ID for the whole process.

I need this for debugging/tuning NUMA aware code.

Upvotes: 1

Views: 739

Answers (1)

alk
alk

Reputation: 70981

At least on Lnux you could get the tid for every thread by calling gettid().

Then look up the CPU id by reading the 39th element from /proc/<pid>/task/<tid>/stat.

(where pid is read via getpid())


See also:

Upvotes: 2

Related Questions