Question

What library function can I call to get mapping of processes to cores or given a process id tell me what core it's running on, it ran last time, or scheduled to run. So something like this:

core 1:  14232,42323
core 2:  42213,63434,434
core 3:  34232,34314
core 4:  42325,6353,1434,4342
core 5:  43432,64535,14345,34233
core 6:  23242,53422,4231,34242
core 7:  78789
core 8:  23423,23124,5663

I sched_getcpu returns the core number of calling process. If there was a function that given a process id, would return the core number that would be good too but I have not found one. sched_getaffinity is not useful either; It just tells you given a process what cores it can run on which is not what I'm interested in.

Was it helpful?

Solution 3

Yes, the virtual file /proc/[pid]/stat seems to have this info: man 5 proc:

/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.
      (...fields description...)

      processor %d (since Linux 2.2.8)
                      CPU number last executed on.

on my dual core:

cat /proc/*/stat | awk '{printf "%-32s %d\n", $2 ":", $(NF-5)}'
(su):                            0
(bash):                          0
(tail):                          1
(hd-audio0):                     1
(chromium-browse):               0
(bash):                          1
(upstart-socket-):               1
(rpcbind):                       1

..though I can't say if it's pertinent and/or accurate..

OTHER TIPS

I don't know that you can get information about what CPU any particular process is running on, but if you look in /proc, you'll find one entry for each running process. Under that, in /proc/<pid>/cpuset you'll find information about the set of CPUs that can be used to run that process.

Your question does not have any precise answer. The scheduler can migrate a process from one processor core to another at any time (and it is actually doing that). So by the time you got the answer it may be already wrong. And a process is usually not tied to any particular core (unless its CPU affinity has been set e.g. with sched_setaffinity(2), which is unusual; see also cpuset(7) for more).

Why are you asking? Why does that matter?

You probably want to dig inside /proc, see proc(5) man page.

In other words, if the kernel does give that information, it is thru /proc/ but I guess that information is not available because it does not make any sense.

NB. The kernel will schedule processes on the various processor cores much better than you can do, so even with a warehouse, you should not care about the core running some pid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top