I have this as a module now, will upload to CPAN, but there was a bug in the above code ($max - $current ) was wrong.
Here's a rewrite with some other improvements as well:
Code:
my $threshold = { Threshold => 0.9, %$self }->{Threshold};
my $is_ratio = $threshold =~ /^0*\./;
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";
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;
}