2

I'd like to monitor the CPU usage for all processes in a particular PID namespace. I'd like to do it programmatically (C, if possible). I have an idea of how to do it, but I'd like to know if Linux provides a simpler/better way.

For instance, could I just use the cutime and cstime values of the namespace's init process to get total CPU usage? Also, if I mount the namespace's /proc filesystem (rather than the root namespace's), will /proc/stat only show CPU utilization of that particular namespace?

Possible solution:

  • Start a new process inside the PID namespace
  • Mount the namespace's /proc filesystem somewhere safe
  • Every few milliseconds, read the /proc directory and compute CPU usage for every process listed inside
  • Use IPC to communicate the aggregate value to the parent process
1

For instance, could I just use the cutime and cstime values of the namespace's init process to get total CPU usage?

That will only include the time of the waited-for (and thus terminated) children and descendants (or ripped-processes in the case of init). So you'll only get the time for the dead orphans.

If you compute the sum of the cutime and cstime of all processes in the namespace (beware of race conditions), you should get the cumulative time of all current and past processes in that namespace except those that joined the namespace (like with nsenter -p) whose time will be accounted to their parent in the original namespace (I find those processes have a PPID of 0 in the entered namespace).

Also, if I mount the namespace's /proc filesystem (rather than the root namespace's), will /proc/stat only show CPU utilization of that particular namespace?

That's easily checked:

$ grep cpu /proc/stat
cpu  69003764 88576 8499514 244070762 5120799 0 407453 0 0 0
cpu0 34269946 49008 4228860 122466614 2365498 0 55409 0 0 0
cpu1 34733818 39567 4270653 121604147 2755300 0 352044 0 0 0
$ sudo unshare --mount-proc -mpf grep cpu /proc/stat
cpu  69005266 88576 8499588 244072663 5120837 0 407462 0 0 0
cpu0 34270449 49008 4228896 122467803 2365522 0 55410 0 0 0
cpu1 34734816 39567 4270692 121604860 2755315 0 352051 0 0 0

So it would seem not.

AFAICT, you should be able to compute the stats from the root namespace.

For instance, for the namespace of pid $pid, with zsh and perl:

perl -l -0777 -ne '
   if (/\(.*\)(?: .*?){12} (.*?) (.*?) (.*?) (.*?)/) {$s+=$1+$2+$3+$4}
   END{print $s}' /proc/*/ns/pid(e:'[[ $REPLY -ef /proc/$pid/ns/pid ]]'::h:s/ns/stat)

That is, add up fields 14 to 17 of the /proc/pid/stat of all the processes that have the same pid namespace as $pid.

As said above, that doesn't include the time of processes that have nsentered the name space later and died (nor the children they waited for (the other ones seem to be ripped by the init of the namespace)).

  • I was just reading the times documentation and it says its cutime/cstime values don't get updated until the child exits. Does /proc/[pid]/stat behave the same way? – Azmisov Feb 3 '16 at 19:34
  • @Azmisov, the cutime/cstime fields there do, but the utime/stime get you real time cpu usage. So if you add up the cutime+cstime+utime+stime of every process, as I suggest, you get the cumulative time of every past and current processes. – Stéphane Chazelas Feb 3 '16 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.