Sample Perl I/O limiting code

Here's a loop I tossed together to allow several disk-intensive background processes to not run my io_tokens in to negativeland.

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

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:

    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;
}

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct