| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
=pod |
| 4 |
|
| 5 |
=head1 NAME |
| 6 |
|
| 7 |
thetop - A 'top' utility for the schwartz |
| 8 |
|
| 9 |
=head1 SYNOPSIS |
| 10 |
|
| 11 |
thetop [--func FORMAT] [--arg FORMAT] [--sort ARGS] [--delay SECS] [--score-dir DIR] |
| 12 |
|
| 13 |
=head1 DESCRIPTION |
| 14 |
|
| 15 |
|
| 16 |
=cut |
| 17 |
|
| 18 |
#--------------------------------------# |
| 19 |
# Dependencies |
| 20 |
|
| 21 |
use strict; |
| 22 |
|
| 23 |
use Getopt::Long; |
| 24 |
use Term::Cap; |
| 25 |
use POSIX; |
| 26 |
|
| 27 |
#--------------------------------------# |
| 28 |
# Global Variables |
| 29 |
|
| 30 |
use vars qw( $OSPEED ); |
| 31 |
|
| 32 |
BEGIN { |
| 33 |
my $termios = POSIX::Termios->new; |
| 34 |
$termios->getattr; |
| 35 |
$OSPEED = $termios->getospeed || 9600; |
| 36 |
} |
| 37 |
|
| 38 |
our $TERM = Term::Cap->Tgetent( { OSPEED => $OSPEED } ); |
| 39 |
|
| 40 |
#--------------------------------------# |
| 41 |
# Main Program |
| 42 |
|
| 43 |
my ( $score_dir, $delay, $func_col, @arg_col, $sort ); |
| 44 |
|
| 45 |
GetOptions( |
| 46 |
'score-dir=s' => \$score_dir, |
| 47 |
'delay|d=s' => \$delay, |
| 48 |
'func=s' => \$func_col, |
| 49 |
'arg=s' => \@arg_col, |
| 50 |
'sort|s=s' => \$sort, |
| 51 |
); |
| 52 |
|
| 53 |
# Make sure we know where to find the scoreboard files |
| 54 |
unless ($score_dir) { |
| 55 |
foreach my $d (qw(/var/run /dev/shm /tmp)) { |
| 56 |
if ( -e "$d/theschwartz" ) { |
| 57 |
$score_dir = "$d/theschwartz"; |
| 58 |
last; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
die "Can't find scoreboard directory. Use '--score-dir'\n" |
| 63 |
unless $score_dir; |
| 64 |
} |
| 65 |
|
| 66 |
# If we got some formatting instructions for the arg column, parse it out |
| 67 |
my %arg_col_by_func; |
| 68 |
if (@arg_col) { |
| 69 |
foreach my $a (@arg_col) { |
| 70 |
if ( $a =~ /=/ ) { |
| 71 |
my ( $func, $fmt ) = split( '=', $a ); |
| 72 |
$arg_col_by_func{$func} = $fmt; |
| 73 |
} |
| 74 |
else { |
| 75 |
$arg_col_by_func{'__ALL__'} = $a; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
# Make sure to give a reasonable default for delay |
| 81 |
$delay ||= 3; |
| 82 |
|
| 83 |
# Start reporting |
| 84 |
clr_screen(); |
| 85 |
while (1) { |
| 86 |
report( $score_dir, $func_col, \%arg_col_by_func, $sort ); |
| 87 |
sleep($delay); |
| 88 |
clr_screen(); |
| 89 |
} |
| 90 |
|
| 91 |
################################################################################ |
| 92 |
|
| 93 |
sub report { |
| 94 |
my ( $dir, $func_col, $arg_col_by_func, $sort ) = @_; |
| 95 |
|
| 96 |
# Find the files available |
| 97 |
opendir( SD, $dir ) or die "Can't read directory '$dir': $!\n"; |
| 98 |
my @files = map { $dir . "/$_" } readdir(SD); |
| 99 |
closedir(SD); |
| 100 |
|
| 101 |
# Grab the data out of them |
| 102 |
my @data; |
| 103 |
foreach my $f (@files) { |
| 104 |
next unless $f =~ /scoreboard\.[0-9]+$/; |
| 105 |
open( SF, '<', $f ) or die "Can't open score file '$f': $!\n"; |
| 106 |
my %dat = map { chomp; split('=') } <SF>; |
| 107 |
close(SF); |
| 108 |
|
| 109 |
$dat{arg_array} = [ split( ',', $dat{arg} || '' ) ]; |
| 110 |
push @data, \%dat; |
| 111 |
} |
| 112 |
|
| 113 |
my $num = scalar(@data); |
| 114 |
my $width = 80 - 17 - $num; |
| 115 |
printf( "Workers: %d total %${width}s\n\n", $num, scalar localtime ); |
| 116 |
printf( "% 5s % 20s % 2s % 7s % 41s\n", |
| 117 |
'PID', 'FUNC', 'S', 'TIME', 'ARGS' ); |
| 118 |
foreach my $d ( sort { order_by( $sort, $a, $b ) } @data ) { |
| 119 |
my $func_str = fmt_func( $d, $func_col ); |
| 120 |
|
| 121 |
printf( |
| 122 |
"% 5s % 20s % 2s % 7s % 41s\n", |
| 123 |
$d->{pid}, $func_str, ( $d->{done} ? 'S' : 'R' ), |
| 124 |
fmt_time($d), fmt_arg( $d, $arg_col_by_func, $func_str ), |
| 125 |
); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
sub order_by { |
| 130 |
my ( $sort, $a, $b ) = @_; |
| 131 |
|
| 132 |
if ($sort) { |
| 133 |
|
| 134 |
} |
| 135 |
else { |
| 136 |
|
| 137 |
# Default to push running tasks to the top |
| 138 |
return ( $a->{done} || 0 ) <=> ( $b->{done} || 0 ) |
| 139 |
|| ( $a->{started} || 0 ) <=> ( $b->{started} || 0 ); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
sub fmt_func { |
| 144 |
my ( $d, $fmt ) = @_; |
| 145 |
my $val = $d->{funcname}; |
| 146 |
|
| 147 |
if ($fmt) { |
| 148 |
if ( $fmt eq 'trim' ) { |
| 149 |
$val =~ s/^.+:://g; |
| 150 |
} |
| 151 |
else { |
| 152 |
$val =~ /($fmt)/; |
| 153 |
$val = $1; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return substr( $val, 0, 20 ),; |
| 158 |
} |
| 159 |
|
| 160 |
sub fmt_time { |
| 161 |
my ($d) = @_; |
| 162 |
my $secs = ( $d->{done} || time ) - $d->{started}; |
| 163 |
|
| 164 |
if ( $secs < 60 ) { |
| 165 |
return sprintf( "%02d:%02d", 0, $secs ); |
| 166 |
} |
| 167 |
elsif ( $secs < 3600 ) { |
| 168 |
my $min = int( $secs / 60 ); |
| 169 |
$secs = $secs % 60; |
| 170 |
return sprintf( "%02d:%02d", $min, $secs ); |
| 171 |
} |
| 172 |
else { |
| 173 |
my $hr = int( $secs / 60 / 60 ); |
| 174 |
my $min = int( $secs / 60 % 60 ); |
| 175 |
$secs = $secs % 60; |
| 176 |
return sprintf( "%d:%02d:%02d", $hr, $min, $secs ); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
## Format the arguments by interpreting the args as either a hash or an array |
| 181 |
## and printing out the appropriate element. |
| 182 |
|
| 183 |
sub fmt_arg { |
| 184 |
my ( $d, $arg_col_by_func, $func_str ) = @_; |
| 185 |
my $val = $d->{arg}; |
| 186 |
my $func_orig = $d->{funcname}; |
| 187 |
|
| 188 |
if ($arg_col_by_func) { |
| 189 |
my $fmt |
| 190 |
= ( $arg_col_by_func{$func_str} |
| 191 |
|| $arg_col_by_func{$func_orig} |
| 192 |
|| $arg_col_by_func{'__ALL__'} ); |
| 193 |
if ($fmt) { |
| 194 |
my $arg_array = $d->{arg_array}; |
| 195 |
|
| 196 |
# If its a number treat the args as an array |
| 197 |
if ( $fmt =~ /^[0-9]+$/ ) { |
| 198 |
$val = $arg_array->[$fmt]; |
| 199 |
} |
| 200 |
|
| 201 |
# otherwise, treat the args as a hash |
| 202 |
else { |
| 203 |
|
| 204 |
# Compensate for odd numbers of args |
| 205 |
push @$arg_array, undef if scalar(@$arg_array) % 2; |
| 206 |
|
| 207 |
my %h = @$arg_array; |
| 208 |
$val = $h{$fmt}; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
return substr( $val || '', 0, 41 ),; |
| 214 |
} |
| 215 |
|
| 216 |
sub clr_screen { |
| 217 |
$TERM->Tputs( 'cl', 1, \*STDOUT ); |
| 218 |
} |