Programming And Stuff, You Know The Thing…

Perl Direct I/O

Posted at — Apr 13, 2021


Direct IO requires buffer alignment. There is a trick to do this in perl. The trick is to use unpack/pack:

use Fcntl qw(:DEFAULT O_DIRECT);
use Digest::SHA qw(sha1);

# computing the aligned offset does the trick
my $bufsize = 1024*1024;
my $align= 512;
my $buf= 'x' x ($align+$bufsize);
my $off= unpack( "J", pack "p", $buf ) % $align;
$off= $align - $off
    if  $off;

sysopen(my $fh, $file, O_RDONLY | O_DIRECT) or die $!;
while (($count = sysread($fh,$buf,$bufsize,$off)) > 0) {
  # this doesn't cost much cpu time...
  my $a = substr($buf,$off);
  # ... but checksumming does:
close($fh) or die $!;



Do it in C/C++ - unless you really have no relevant processing overhead.

Further Reading