I'm not sure to express this question correctly and I've try to read the man proc
, but I can't manage to find a proper answer and I couldn't find a simple way to verify it.
I try to get CPU/RAM consumption for one PID but I don't know how many child processes will be cast by the program and I want to get the total amount of CPU and RAM consumption not just the main process.
I know for fact and experience that /proc/[PID]/io
is indeed cumulative through all child processes, but I would like to know and if possible with proof if the same applies to /proc/[PID]/stat
.
Does /proc/[PID]/stat display cumulative CPU stats about child processes
proc
Best Answer
Conlusion:
If you don't want to read the whole explanation just read this:
Yes the value contained in /proc/[PID]/stat allows to determine the amount of CPU time used by a process and its children.
However, you can't use it for real time monitoring because value for children CPU time is updated only when child process die.
Explanation:
According to the
man time
time returns the following stats :If one reads
man times
one could learn that the structure is define as :Which means that this command returns the cumulated user and system CPU time from the process and all it's children.
Now we need to know what we can extract from
/proc
. In theman proc
in section/proc/[PID]/stat
you can extract the following informations:So basically this
/proc/[PID]/stat
file contains the value use by time to determine CPU time in secondsStrong of that knowledge I tried to run my script like this
time load.sh
and I add the end of the scriptcat /proc/$$/stat
Here's the results:output of the
time
command:According to
man proc
we need to look at the columns 14,15,16 and 17:192 520 3964 1165
so if we sum up time spent in user/system cpu by process and its children.Et voilà, the CPU time is not exactly cumulative but you can calculate pretty accuratly (centisecond) the CPU time use by your program and it's children using
/proc/[PID]/stat
.EDIT:
After futher testing and talk wqith people, I finally get an answer, I've run a script that simply contains:
And using watch to monitor the metric in
/proc/$$/stat
at the same time. As long as the child process is not finished the counter are not updated. Whenstress
ends then the value displayed in/proc/$$/stat
are updated and ends with similar result betweentime
command and the column 14 to 17 of/proc
.Old edit
I though it was over but after doing some more research I tried the same with the commandstress
During the execution I watch 2 times/second the result of the command:
Whileps faux | grep stress
would give me this particular PID as father of the fourstress
thread.