Sample Perl I/O limiting code
If there's interest, I'll CPAN a module containing it; also please let me know if you see any issues or improvements.
Thanks,
Barrie
while (1) {
open IO, "< /proc/io_status" or die $!;
my $l = <io>;
close IO;
my ( $current, $refill, $max ) =
$l =~ / io_tokens=(-?\d+).* token_refill=(-?\d+).* token_max=(-?\d+)/
or die "Couldn't parse \"$l\"\n";
last if $current >= $max*0.90;
sleep 1 + ( $refill ? ( $max - $current ) / $refill : 10 );
}</io>
1 Reply
Here's a rewrite with some other improvements as well:
my $threshold = { Threshold => 0.9, %$self }->{Threshold};
my $is_ratio = $threshold =~ /^0*\./;
while (1) {
open IO, ";
close IO;
my ( $current, $refill, $max ) =
$l =~ / io_tokens=(-?\d+).* token_refill=(-?\d+).* token_max=(-?\d+)/
or die "Couldn't parse \"$l\"\n";
my $limit = $is_ratio ? $max * $threshold : $threshold;
last if $current >= $limit;
my $t = 1 + ( $refill ? ( $limit - $current ) / $refill : 10 );
warn "Sleeping for $t seconds waiting for ", $limit - $current, " tokens\n"
if $self->{Verbose};
sleep $t;
}