Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help understanding a bit of code. 1

Status
Not open for further replies.

kodr

Programmer
Dec 4, 2003
368
Below is some code I'm trying to understand. I'm not sure if it's working correctly.

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?
 
sscanf is quite a cool function. it performs no calculations though.

Code:
sscanf($buf, "%*s %Ld %Ld %Ld %Ld", $ab, $ac, $ad, $ae);
what this line of code is saying is as follows
1. take the string $buf and
2. match it against the format in the second argument. this is, in the current example, SUPPOSED TO BE a string followed by a space followed by a set of numbers, followed by a spaece etc.
3. assign each matched grouping to the arguments specified afterwards : i.e. the string goes to $ab, the first number to $ac and they have discarded the fourth grouping, strangely.


but the code looks wrong as the format specifiers don't look right. i would say it should be as follows:

Code:
sscanf($buf, "%s %d %d %d %d", $ab, $ac, $ad, $ae, $af);
equally you could do this
Code:
$array = sscanf($buf, "%s %d %d %d %d");
and the found groupings would be nicely inside the array $array.


 
So, then the sscanf is taking the value of $buf (cpu 222344 54093 124547 559475335 124669 9647 12628 0) and putting the first four space seperated values into the variables $ab thru $ae?

With the value cpu ending up in $ab, correct?

Then the second code piece, takes those variables and tries to perform some calculations?

Code:
 $load2 = $ab + $ac + $ad;
 $total2 = $ab + $ac + $ad + $ae;
 $results['cpupercent'] = ($total2 != $total)?((100*($load2 - $load)) / ($total2 - $total)):0;

I think what is happening is that $ab is actually containing a string and not a numerical value, and thus giving me erroneous results.
 
correct. and equally correct that perhaps $ab should have had a $aa in front of it!
 
Thanks. That was my thinking, but I didn't quite trust myself.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top