Commit 75a026ca authored by Olav Kvittem's avatar Olav Kvittem
Browse files

sleep when partial or blocked

parent 182e5ea7
......@@ -8,15 +8,16 @@ use threads;
use FileHandle;
use File::Basename;
use Getopt::Long;
use Time::HiRes qw(usleep);
my $pipe_size=2**19; # 0.5MB
my (@opt_cmd, $opt_v, $opt_h);
my (@opt_cmd, $opt_v, $opt_h, $opt_debug);
my $usage="Usage\n$0 --cmd <command> --cmd ...
-pipe_size - buffering output to commands ($pipe_size)
-v - print summary stats at end
$0 index output-directory [data-file]..\n
";
GetOptions('cmd=s'=> \@opt_cmd, 'pipe_size=s'=>\$pipe_size, 'v'=>\$opt_v, 'h'=>\$opt_h) or die "$usage : $!" ;
GetOptions('cmd=s'=> \@opt_cmd, 'pipe_size=s'=>\$pipe_size, 'debug' => \$opt_debug, 'v'=>\$opt_v, 'h'=>\$opt_h) or die "$usage : $!" ;
die $usage if $opt_h;
......@@ -53,6 +54,9 @@ $SIG{TERM} = 'summary';
my @queues=();
my @fh=();
my @blocked=();
my $n_slept=();
my $t_slept=();
foreach $i ( 0.. $#cmds ){
......@@ -67,17 +71,27 @@ while(<>){
foreach $i ( 0.. $#cmds ){
push ( @{$queues[$i]}, $_);
while ( $#{$queues[$i]} >= 0 ){
if ( $blocked[$i] > 0 ){
printf "blocked index %d for %d\n", $i, $blocked[$i] if $opt_debug && $blocked[$i] > 1;
usleep(1000 * $blocked[$i]); # additive increase in sleep_time
$n_slept[$i] ++;
$t_slept[$i] += $blocked[$i];
}
my $line = shift(@{$queues[$i]});
my $rv = syswrite( $fh[$i], $line, length $line);
if (!defined($rv) && $!{EAGAIN}) { # would block
unshift @{$queues[$i]}, $line;
$would_block++;
$blocked[$i]++;
break;
} elsif ($rv != length $buffer) { # incomplete write
unshift @{$queues[$i]}, substr( $line, $rv );
$partial_write++;
$blocked[$i]++;
} else { # successfully wrote
$normal_write++;
$blocked[$i]=0;
}
}
}
......@@ -92,6 +106,9 @@ sub summary{
printf "summary: would_block %d partial_write %d normal_write %d \n",
$would_block, $partial_write, $normal_write ;
foreach $i(0..$#cmds){
printf "%8d %8dms %s\n", $n_slept[$i], $t_slept[$id], $cmds[$i];
}
}
sub set_pipe_size{
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment