Is there way to determine using standard Linux/Unix tools how much a process has spent in User mode and how much waiting for kernel?
In other words, exactly the "user" and "sys" values that you get when using time
, but during process runtime?
NOTE: one way is to use WBEM and enumerate Linux_UnixProcess, but I need to use other tool than this; my aim is to verify what WBEM provider tells me, i.e. I need to use different tool.
Best Answer
On Linux, the info is available in fields 14 to 17 of
/proc/$pid/stat
(see proc(5) for details):Fields are:
(all the threads of a given process have the same values there)
They are not directly reported by
ps
.ps
reports 14 + 15 withps -o time
and14 + 15 + 16 + 17
withps --cumulative -o bsdtime
.Beware that the second field in
/proc/$pid/stat
may contain spaces or newline characters, so you can't parse it with awk's$1
,$2
...You can use
perl
like:A process can retrieve its own times with
getrusage(RUSAGE_SELF)
andgetrusage(RUSAGE_CHILDREN)
.There's also a
times
system call that retrieves the same information. POSIX shells have atimes
builtin for that. Some shells also provide with that information withtime
(without arguments).