diff ovz_statshow.pl @ 2:4080b53fd4a2

ovz_statshow: Works but vestat seems buggy
author Peter Gervai <grin@grin.hu>
date Mon, 23 Mar 2009 17:00:16 +0100
parents 068d0e1deb08
children 4cd907a693d1
line wrap: on
line diff
--- a/ovz_statshow.pl	Mon Mar 23 14:08:23 2009 +0100
+++ b/ovz_statshow.pl	Mon Mar 23 17:00:16 2009 +0100
@@ -1,7 +1,9 @@
 #!/usr/bin/perl
 #$Id$
 #(C)Peter Gervai, 2009
+# email: grin.grin.hu
 #
+# Released under GPL v2 or later and CC-BY-SA-3.0
 
 use strict;
 use warnings;
@@ -16,44 +18,64 @@
 
 
 my @fields = (1,2,3,4,5,7,8);
+my @fieldname = qw( user nice sys up idle up_c used_c );
 my (%ve, %ve1);
 
 my $cpu_mhz = qx( egrep '^cpu MHz' /proc/cpuinfo | head -n1 | awk '{print \$4}' );
 chomp $cpu_mhz;
+$cpu_mhz = $cpu_mhz * 1_048_576;
 print "Running on $cpu_mhz MHz CPU(s)\n";
 
 
 while(1) {
     my $f = new IO::File "< $f_stat" or die $!;
-    my $l = <$f>;
-    if( $l !~ /Version: $stat_ver/ ) {
-        die "Unknown file version $l, required $stat_ver";
-    }
-    $l = <$f>; # headers
+    
+    my $fdata;
+    # have to read one chunk at once :-/ length is arbitrary
+    sysread $f, $fdata, 65535;
+    $f->close;
+    
+    my @vestat = split /\n/, $fdata;
     
-    while( <$f> ) {
-        chomp;
-        my @d = split;
-        # 0veid, 1user, 2nice, 3systm, 4uptime, 5idle, (6strv)
-        # 7uptime, 8used, (9maxlat, 10totlat, 11numched)
+    if( $vestat[0] !~ /Version: $stat_ver/ ) {
+        die "Unknown file version $vestat[0], required $stat_ver";
+    }
+    my $debug_proc = "";
+    for my $i ( 2 .. $#vestat ) {
+        my $l = $vestat[$i];
+        #chomp $l;
+        $l =~ s/^\s*(.*?)\s*$/$1/;
+        #print "##$l##\n";
+        $debug_proc .= "$l\n";
+        my @d = split /\s+/, $l;
+        # 0veid, 1user, 2nice, 3systm, 4uptime,                                 <- jiffy
+        # 5idle, (6strv), 7uptime, 8used, (9maxlat, 10totlat, 11numched)        <- cycles
         $ve{$d[0]} = [ @d[0..11] ];
     }
     
     for my $veid ( sort {$a<=>$b} keys %ve ) {
         if( defined( $ve1{$veid} ) ) {
-            print "$veid: ";
-            for my $field ( @fields ) {
+            printf "%03d:", $veid;
+            for my $i ( 0..$#fields ) {
+                my $field = $fields[$i];
                 #print "[$field,", $ve{$veid}, ",", $ve1{$veid} ,",", $ve{$veid}->[$field],",", $ve1{$veid}->[$field], "] ";
                 my $val = $ve{$veid}->[$field] - $ve1{$veid}->[$field];
-                print "field=";
-                if( $field <6 ) {
-                    print &jiffie2ms( $val );
+                printf "%6s(%3d)=", $fieldname[$i], $field;
+                
+                if( $val < 0 ) {
+                    # debug
+                    print "\n#", $ve{$veid}->[$field], ' ', $ve1{$veid}->[$field], "#$_#\n";
+                    print "$debug_proc\n";
+                }
+                
+                if( $field < 5 ) {
+                    printf "%4d", &jiffie2ms( $val );
                 } else {
-                    print &clock2ms( $val );
+                    printf "%4d", &cycle2ms( $val );
                 }
                 print "; ";
                 if( $field == 7 ) {
-                    print "<", &clock2ms( $val - ($ve{$veid}->[5] - $ve1{$veid}->[5]) ), "> ";
+                    printf "<%4d> ", &cycle2ms( $val - ($ve{$veid}->[5] - $ve1{$veid}->[5]) );
                 }
             }
             print "\n";
@@ -65,8 +87,8 @@
 }
 
 
-sub clock2ms ($) {
-    return int( (shift) / ( $cpu_mhz * $jiffies * $jiffies ) * 1000 );
+sub cycle2ms ($) {
+    return int( (shift) / $cpu_mhz * 1000 );
 }