source: subversion/sites/other/tilesAtHome/lib/cpu.inc @ 3655

Last change on this file since 3655 was 3058, checked in by ojw, 13 years ago

+ Load average

File size: 1.2 KB
Line 
1<?php
2
3function TestLoad(){
4  header("Content-type:text/plain");
5  print "Testing GetLoad()\n";
6  foreach(array("user","idle","iowait","system") as $Item){
7      printf("%s = %1.2f\n", $Item, GetLoad($Item));
8  }
9}
10
11function GetLoadAvg()
12{
13  if(!($fp = fopen("/proc/loadavg", "r"))){
14    return(-1);
15  }
16  $Info = fgets($fp, 4096);
17  fclose($fp);
18 
19  # Split into fields
20  $Parts  = explode(" ", $Info);
21
22  return($Parts[0]);
23}
24
25function GetLoad($Type = "idle"){
26  # Get first line from /proc/stat, of the form:
27  # "cpu  41644332 829140 7041132 54137011 110995115 807596 568065 0"
28  if(!($fp = fopen("/proc/stat", "r"))){
29    return(-1);
30  }
31  $Info = fgets($fp, 4096);
32  fclose($fp);
33 
34  # Split into fields
35  $t = array();
36  list(
37    $label,
38    $t["user"],
39    $t["nice"],
40    $t["system"],
41    $t["idle"],
42    $t["iowait"],
43    $t["irq"],
44    $t["softirq"])  = preg_split("/\s+/", $Info);
45 
46  # Calculate total
47  $Total = 0;
48  foreach($t as $name => $val){
49    $Total += $val;
50  }
51 
52  # Calcuate requested field as percentage
53  $Percent = 100 * $t[$Type] / $Total;
54 
55  if(0){
56    printf("%d of %d = %1.2f in %s",
57      $t["idle"],
58      $Total,
59      $Percent,
60      $label);
61    }
62   
63  return($Percent);
64}
65
66
67?>
Note: See TracBrowser for help on using the repository browser.