linux - What does pcpu signify and why multiply by 1000? -
i reading calculating cpu usage of process.
seconds = utime / hertz total_time = utime + stime if include_dead_children total_time = total_time + cutime + cstime endif seconds = uptime - starttime / hertz pcpu = (total_time * 1000 / hertz) / seconds print: "%cpu" pcpu / 10 "." pcpu % 10
what don't is, 'seconds' algorithm means time computer spent doing operations other interested process, , before it. since, uptime time our computer spent being operational , starttime means time our [interested] process started.
then why dividing total_time
seconds
[time computer spent doing else] pcpu
? doesn't make sense.
the standard meanings of variables:
# name description 14 utime cpu time spent in user code, measured in jiffies 15 stime cpu time spent in kernel code, measured in jiffies 16 cutime cpu time spent in user code, including time children 17 cstime cpu time spent in kernel code, including time children 22 starttime time when process started, measured in jiffies /proc/uptime :the uptime of system (seconds), , amount of time spent in idle process (seconds). hertz :number of clock ticks per second
now you've provided each of variables represent, here's comments on pseudo-code:
seconds = utime / hertz
the above line pointless, new value of seconds
never used before it's overwritten few lines later.
total_time = utime + stime
total running time (user + system) of process, in jiffies, since both utime
, stime
are.
if include_dead_children total_time = total_time + cutime + cstime endif
this should total_time = cutime + cstime
, since definitions seem indicate that, e.g. cutime
includes utime
, plus time spent children in user mode. so, written, overstates value including contribution process twice. or, definition wrong... regardless, total_time
still in jiffies.
seconds = uptime - starttime / hertz
uptime
in seconds; starttime / hertz
converts starttime
jiffies seconds, seconds
becomes "the time in seconds since process started".
pcpu = (total_time * 1000 / hertz) / seconds
total_time
still in jiffies, total_time / hertz
converts seconds, number of cpu seconds consumed process. divided seconds
give scaled cpu-usage percentage since process start if floating point operation. since isn't, it's scaled 1000 give resolution of 1/10%. scaling forced done use of parentheses, preserve accuracy.
print: "%cpu" pcpu / 10 "." pcpu % 10
and undoes scaling, finding dividend , remainder when dividing pcpu
10, , printing values in format looks floating point value.
Comments
Post a Comment