--- trunk/dwm-status.pl 2007/05/26 23:47:35 31 +++ trunk/dwm-status.pl 2007/05/27 09:14:14 35 @@ -13,6 +13,8 @@ my $dt = 3; my $acpi_every = 10; +my $disk_blk_size = 512; + my $debug = shift @ARGV; $|=1; @@ -36,7 +38,7 @@ warn "unit( $v )\n" if ($debug); - my @units = qw/b k m g/; + my @units = qw/b k M G/; my $o = 0; while ( ( $v / 1024 ) >= 1 ) { @@ -45,13 +47,16 @@ } if ( $v >= 1 ) { - return sprintf("%d%s/s", $v, $units[$o]); + return sprintf("%d%s", $v, $units[$o]); + } elsif ( $v == 0 ) { + return ''; } else { - return sprintf("%.1f%s/s", $v, $units[$o]); + return sprintf("%.1f%s", $v, $units[$o]); } } my ( $lrx, $ltx ) = ( 0, 0 ); +my ( $ld_r, $ld_w ) = ( 0, 0 ); my $bat; my $i = 0; @@ -61,23 +66,32 @@ if ( $i % $acpi_every == 0 ) { - $bat->{state} = proc2hash( '/proc/acpi/battery/BAT0/state' ); - $bat->{info} = proc2hash( '/proc/acpi/battery/BAT0/info' ); + my $state = proc2hash( '/proc/acpi/battery/BAT0/state' ); + + if ( $state->{'present rate'} != 0 ) { + my $info = proc2hash( '/proc/acpi/battery/BAT0/info' ); - $bat->{pcnt} = $bat->{state}->{'remaining capacity'} / $bat->{info}->{'design capacity'}; + my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; - my $time = ( $bat->{info}->{'design capacity'} - $bat->{state}->{'remaining capacity'} ) / $bat->{state}->{'present rate'}; + my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); - warn "time = $time\n" if ($debug); + warn "time = $time\n" if ($debug); - $bat->{hh} = int( $time ); - $bat->{mm} = int( ( $time - $bat->{hh} ) * 60 ); - $bat->{ss} = ( $time * 3600 ) % 60; + my $hh = int( $time ); + my $mm = int( ( $time - $hh ) * 60 ); + my $ss = ( $time * 3600 ) % 60; - $bat->{new} = '!'; + $bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ", + substr($state->{'charging state'},0,1), + $pcnt * 100, $hh, $mm, $ss, + $state->{'present rate'} / 1000 + ); + } else { + $bat = ''; + } } else { - $bat->{new} = ' '; + $bat =~ s/!(\|\s)$/ $1/; } $i++; @@ -103,17 +117,35 @@ } warn "rx: $rx tx: $tx\n" if ($debug); - my $r = ( $rx - $lrx ) / $dt; - my $t = ( $tx - $ltx ) / $dt; + my $net_rx = ( $rx - $lrx ) / $dt; + my $net_tx = ( $tx - $ltx ) / $dt; ( $lrx, $ltx ) = ( $rx, $tx ); - printf "%s | %s |%6s >> %-6s| %s %2d%% %02d:%02d:%02d %3.1fW%s| %s\n", + my $disk = read_file('/proc/diskstats'); + my ( $d_r, $d_w ) = ( 0,0 ); + + foreach ( split(/\n/, $disk) ) { + s/^\s+//; + my @d = split(/\s+/, $_, 17); + next unless ( $d[2] =~ m/^[sh]d\w$/ ); + + warn dump( @d ) if ($debug); + $d_r += $d[5] * $disk_blk_size; + $d_w += $d[7] * $disk_blk_size; + } + warn "d_r: $d_r d_w: $d_w\n" if ($debug); + + my $d_read = ( $d_r - $ld_r ) / $dt; + my $d_write = ( $d_w - $ld_w ) / $dt; + ( $ld_r, $ld_w ) = ( $d_r, $d_w ); + + printf("%s | %s |%5s D %-5s|%5s > %-5s| %s%s\n", $s, $load, - unit( $r ), unit( $t ), - substr($bat->{state}->{'charging state'},0,1), $bat->{pcnt} * 100, $bat->{hh}, $bat->{mm}, $bat->{ss}, - $bat->{state}->{'present rate'} / 1000, $bat->{new}, - $temp; + unit( $d_read ), unit( $d_write ), + unit( $net_rx ), unit( $net_tx ), + $bat, $temp, + ); sleep $dt; }