comparison 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
comparison
equal deleted inserted replaced
1:068d0e1deb08 2:4080b53fd4a2
1 #!/usr/bin/perl 1 #!/usr/bin/perl
2 #$Id$ 2 #$Id$
3 #(C)Peter Gervai, 2009 3 #(C)Peter Gervai, 2009
4 # email: grin.grin.hu
4 # 5 #
6 # Released under GPL v2 or later and CC-BY-SA-3.0
5 7
6 use strict; 8 use strict;
7 use warnings; 9 use warnings;
8 10
9 use IO::File; 11 use IO::File;
14 # jiffie per second ( zgrep ^CONFIG_HZ /proc/config.gz ) 16 # jiffie per second ( zgrep ^CONFIG_HZ /proc/config.gz )
15 my $jiffies = 100; 17 my $jiffies = 100;
16 18
17 19
18 my @fields = (1,2,3,4,5,7,8); 20 my @fields = (1,2,3,4,5,7,8);
21 my @fieldname = qw( user nice sys up idle up_c used_c );
19 my (%ve, %ve1); 22 my (%ve, %ve1);
20 23
21 my $cpu_mhz = qx( egrep '^cpu MHz' /proc/cpuinfo | head -n1 | awk '{print \$4}' ); 24 my $cpu_mhz = qx( egrep '^cpu MHz' /proc/cpuinfo | head -n1 | awk '{print \$4}' );
22 chomp $cpu_mhz; 25 chomp $cpu_mhz;
26 $cpu_mhz = $cpu_mhz * 1_048_576;
23 print "Running on $cpu_mhz MHz CPU(s)\n"; 27 print "Running on $cpu_mhz MHz CPU(s)\n";
24 28
25 29
26 while(1) { 30 while(1) {
27 my $f = new IO::File "< $f_stat" or die $!; 31 my $f = new IO::File "< $f_stat" or die $!;
28 my $l = <$f>; 32
29 if( $l !~ /Version: $stat_ver/ ) { 33 my $fdata;
30 die "Unknown file version $l, required $stat_ver"; 34 # have to read one chunk at once :-/ length is arbitrary
35 sysread $f, $fdata, 65535;
36 $f->close;
37
38 my @vestat = split /\n/, $fdata;
39
40 if( $vestat[0] !~ /Version: $stat_ver/ ) {
41 die "Unknown file version $vestat[0], required $stat_ver";
31 } 42 }
32 $l = <$f>; # headers 43 my $debug_proc = "";
33 44 for my $i ( 2 .. $#vestat ) {
34 while( <$f> ) { 45 my $l = $vestat[$i];
35 chomp; 46 #chomp $l;
36 my @d = split; 47 $l =~ s/^\s*(.*?)\s*$/$1/;
37 # 0veid, 1user, 2nice, 3systm, 4uptime, 5idle, (6strv) 48 #print "##$l##\n";
38 # 7uptime, 8used, (9maxlat, 10totlat, 11numched) 49 $debug_proc .= "$l\n";
50 my @d = split /\s+/, $l;
51 # 0veid, 1user, 2nice, 3systm, 4uptime, <- jiffy
52 # 5idle, (6strv), 7uptime, 8used, (9maxlat, 10totlat, 11numched) <- cycles
39 $ve{$d[0]} = [ @d[0..11] ]; 53 $ve{$d[0]} = [ @d[0..11] ];
40 } 54 }
41 55
42 for my $veid ( sort {$a<=>$b} keys %ve ) { 56 for my $veid ( sort {$a<=>$b} keys %ve ) {
43 if( defined( $ve1{$veid} ) ) { 57 if( defined( $ve1{$veid} ) ) {
44 print "$veid: "; 58 printf "%03d:", $veid;
45 for my $field ( @fields ) { 59 for my $i ( 0..$#fields ) {
60 my $field = $fields[$i];
46 #print "[$field,", $ve{$veid}, ",", $ve1{$veid} ,",", $ve{$veid}->[$field],",", $ve1{$veid}->[$field], "] "; 61 #print "[$field,", $ve{$veid}, ",", $ve1{$veid} ,",", $ve{$veid}->[$field],",", $ve1{$veid}->[$field], "] ";
47 my $val = $ve{$veid}->[$field] - $ve1{$veid}->[$field]; 62 my $val = $ve{$veid}->[$field] - $ve1{$veid}->[$field];
48 print "field="; 63 printf "%6s(%3d)=", $fieldname[$i], $field;
49 if( $field <6 ) { 64
50 print &jiffie2ms( $val ); 65 if( $val < 0 ) {
66 # debug
67 print "\n#", $ve{$veid}->[$field], ' ', $ve1{$veid}->[$field], "#$_#\n";
68 print "$debug_proc\n";
69 }
70
71 if( $field < 5 ) {
72 printf "%4d", &jiffie2ms( $val );
51 } else { 73 } else {
52 print &clock2ms( $val ); 74 printf "%4d", &cycle2ms( $val );
53 } 75 }
54 print "; "; 76 print "; ";
55 if( $field == 7 ) { 77 if( $field == 7 ) {
56 print "<", &clock2ms( $val - ($ve{$veid}->[5] - $ve1{$veid}->[5]) ), "> "; 78 printf "<%4d> ", &cycle2ms( $val - ($ve{$veid}->[5] - $ve1{$veid}->[5]) );
57 } 79 }
58 } 80 }
59 print "\n"; 81 print "\n";
60 } 82 }
61 $ve1{$veid} = $ve{$veid}; 83 $ve1{$veid} = $ve{$veid};
63 print "\n"; 85 print "\n";
64 sleep(1); 86 sleep(1);
65 } 87 }
66 88
67 89
68 sub clock2ms ($) { 90 sub cycle2ms ($) {
69 return int( (shift) / ( $cpu_mhz * $jiffies * $jiffies ) * 1000 ); 91 return int( (shift) / $cpu_mhz * 1000 );
70 } 92 }
71 93
72 94
73 sub jiffie2ms ($) { 95 sub jiffie2ms ($) {
74 return int( (shift) / $jiffies * 1000 ); 96 return int( (shift) / $jiffies * 1000 );