Implement use of better API for getting node CPU stats
authorMichal Novotny <minovotn@redhat.com>
Tue, 7 Aug 2012 17:14:45 +0000 (19:14 +0200)
committerMichal Novotny <minovotn@redhat.com>
Tue, 7 Aug 2012 17:14:45 +0000 (19:14 +0200)
Signed-off-by: Michal Novotny <minovotn@redhat.com>

classes/libvirt.php
pages/info.php

index a3039e6..d5cbfb7 100644 (file)
                        return ($tmp) ? $tmp : $this->_set_last_error();
                }
 
+               function node_get_cpu_stats_each_cpu($sec = 0) {
+                       $tmp = libvirt_node_get_cpu_stats_for_each_cpu($this->conn, $sec);
+
+                       if ($sec <= 1)
+                               return ($tmp) ? $tmp : $this->_set_last_error();
+
+                       if (!$tmp) return $this->_set_last_error();
+
+                       $numcpus = sizeof($tmp[0]) - 1;
+                       $numvalues = sizeof($tmp) - 1;
+                       $out = array();
+                       for ($i = 0; $i < $numcpus; $i++) {
+                               $out[$i]['kernel'] = (($tmp[ sizeof($tmp) - 2 ][$i]['kernel'] - $tmp[0][$i]['kernel']) / $numvalues);
+                               $out[$i]['iowait'] = (($tmp[ sizeof($tmp) - 2 ][$i]['iowait'] - $tmp[0][$i]['iowait']) / $numvalues);
+                               $out[$i]['idle'] = (($tmp[ sizeof($tmp) - 2 ][$i]['idle'] - $tmp[0][$i]['idle']) / $numvalues);
+                               $out[$i]['user'] = (($tmp[ sizeof($tmp) - 2 ][$i]['user'] - $tmp[0][$i]['user']) / $numvalues);
+                       }
+
+                       return $out;
+               }
+
                function node_get_cpu_stats_raw($cpu = VIR_NODE_CPU_STATS_ALL_CPUS) {
                        $tmp = libvirt_node_get_cpu_stats($this->conn, $cpu);
                        return ($tmp) ? $tmp : $this->_set_last_error();
index 108ce66..69fbeb2 100644 (file)
@@ -136,21 +136,17 @@ if (is_array($tmp)) foreach ($tmp as $name => $value) {
 <br />
 <div class="section"><?php echo $lang->get('cpu_stats_per_each_cpu'); ?></div>
 <?php
-for ($i = 0; $i < $numCpus; $i++) {
-    $tmp = $lv->node_get_cpu_stats_raw($i);
-    $newvalues = array();
-    foreach ($tmp[0] as $key => $elem) {
-        $newvalues[$key] = $tmp[1][$key] - $elem;
+    $tmp = $lv->node_get_cpu_stats_each_cpu(2);
+    foreach ($tmp as $k => $value) {
+        echo '<div class="label">CPU #'.$k.'</div>';
+        echo '<div class="value">';
+        foreach ($value as $key => $val) {
+            if ($key != 'time')
+                echo '<div class="label">'.$key.'</div><div class="value" style="text-align: right">'.$val.'</div>';
+        }
+
+        echo '</div><br />';
     }
-    echo '<div class="label">CPU #'.$i.'</div>';
-    echo '<div class="value">';
-    foreach ($tmp[0] as $key => $val) {
-        $v = $tmp[1][$key] - $val;
-        if ($key != 'time')
-            echo '<div class="label">'.$key.'</div><div class="value" style="text-align: right">'.$v.'</div>';
-    }
-    echo '</div><br />';
-}
 ?>
 <div style="clear:both"></div>
 <div class="section"><?php echo $lang->get('mem_stats'); ?></div>
@@ -160,7 +156,7 @@ if (is_array($tmp)) foreach ($tmp as $name => $value) {
     echo '<div class="label">'.$name.'</div>';
 
     if ($name != 'time')
-        echo '<div class="value">'.$value.'</div><div class="nl">';
+        echo '<div class="value">'.round($value / 1024).' MiB</div><div class="nl">';
     else
         echo '<div class="value">'.@Date($lang->get('date-format'), $value).'</div><div class="nl">';
 }