0

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.

theRealWorld
  • 1,188
  • 2
  • 8
  • 23
  • 1
    At a given instant a CPU core runs at most one process... – Basile Starynkevitch Apr 13 '13 at 07:04
  • 2
    Why do ask? Want would you do with that useless information? – Basile Starynkevitch Apr 13 '13 at 07:13
  • I edited it. I have a warehouse and limited number of processes. It is really important for me to know what processes run on what cores. Also, don't forget that you can hard set the process on a core so it will only run on that core. – theRealWorld Apr 13 '13 at 07:29
  • 2
    I don't understand why is it important. The scheduler will do its job, better than you can do it. The main case where cpu affinity could matter is for heterogeneous multi-core... – Basile Starynkevitch Apr 13 '13 at 13:33
  • 1
    I think you're solving the wrong problem here. My advice would be to take a step back and unwind the problem. – Flexo Apr 15 '13 at 07:08
  • Scheduler does not move around the processes from cores too often for caching purposes. My task is related to profiling, and it is important for me to know where each process is running on. Don't want to be rude, but you don't know the problem I'm solving. – theRealWorld Apr 15 '13 at 07:08

3 Answers3

3

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
2

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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I have a warehouse and limited number of processes. It is really important for me to know what processes run on what cores. Also, don't forget that you can hard set the process on a core so it will only run on that core. – theRealWorld Apr 13 '13 at 07:30
  • Scheduler does not move around the processes from cores too often for caching purposes. My task is related to profiling, and it is important for me to know where each process is running on. Many people often think the processors do everything the best. Often, little tweaks can save millions in warehouses. – theRealWorld Apr 15 '13 at 18:59
1

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..

xtof pernod
  • 862
  • 5
  • 7