kodr
Programmer
- Dec 4, 2003
- 368
Below is some code I'm trying to understand. I'm not sure if it's working correctly.
Specifically this line:
The data is:
If I'm reading this correctly, the string cpu is being read into the variable $ab then used in some calculations:
Am I reading this correctly, or am I missing something?
Code:
function loadavg ($bar = false) {
$buf = rfts( '/proc/loadavg' );
if( $buf == "ERROR" ) {
$results['avg'] = array('N.A.', 'N.A.', 'N.A.');
} else {
$results['avg'] = preg_split("/\s/", $buf, 4);
unset($results['avg'][3]); // don't need the extra values, only first three
}
if ($bar) {
$buf = rfts( '/proc/stat', 1 );
if( $buf != "ERROR" ) {
sscanf($buf, "%*s %Ld %Ld %Ld %Ld", $ab, $ac, $ad, $ae);
// Find out the CPU load
// user + sys = load
// total = total
$load = $ab + $ac + $ad; // cpu.user + cpu.sys
$total = $ab + $ac + $ad + $ae; // cpu.total
// we need a second value, wait 1 second befor getting (< 1 second no good value will occour)
sleep(1);
$buf = rfts( '/proc/stat', 1 );
sscanf($buf, "%*s %Ld %Ld %Ld %Ld", $ab, $ac, $ad, $ae);
$load2 = $ab + $ac + $ad;
$total2 = $ab + $ac + $ad + $ae;
$results['cpupercent'] = ($total2 != $total)?((100*($load2 - $load)) / ($total2 - $total)):0;
}
}
return $results;
}
Specifically this line:
Code:
sscanf($buf, "%*s %Ld %Ld %Ld %Ld", $ab, $ac, $ad, $ae);
The data is:
Code:
cpu 222344 54093 124547 559475335 124669 9647 12628 0
If I'm reading this correctly, the string cpu is being read into the variable $ab then used in some calculations:
Code:
$load2 = $ab + $ac + $ad;
$total2 = $ab + $ac + $ad + $ae;
$results['cpupercent'] = ($total2 != $total)?((100*($load2 - $load)) / ($total2 - $total)):0;
Am I reading this correctly, or am I missing something?