in reply to sysread/syswrite and O_DIRECT alignment problem

Using mmap() just to get an aligned buffer seems like a bit of a sledgehammer and might be causing problems (though I don't have swapped in the implications of that particular use of mmap). You could try a different approach:

my $bufsize= 4096; my $align= 512; my $buf= 'x' x ($align+$bufsize); my $off= unpack( "J", pack "p", $buf ) % $align; $off= $align - $off if $off; sysread( $FH, $buf, $bufsize, $off ) or ...; my $data= substr( $buf, $off, $bufsize );

- tye        

Replies are listed 'Best First'.
Re^2: sysread/syswrite and O_DIRECT alignment problem (align)
by fishnuts (Acolyte) on Nov 27, 2007 at 11:04 UTC
    Tye, your solution worked perfectly, both for sysread and syswrite. How did you learn of this, and what have you used this for previously?
      How did you learn of this, and what have you used this for previously?

      I read the standard docs on pack and unpack and did experiments to learn the many aspects that aren't clearly explained. Acme::ESP uses parts of this. I've also used it several times to construct arguments to be passed through Win32::API. I also read the standard sysread docs and have used the 4th argument to efficiently append more to a buffer holding a scrolling window of data from a file.

      - tye        

        Awesome, thanks for the help!
Re^2: sysread/syswrite and O_DIRECT alignment problem (align)
by dk (Chaplain) on Nov 27, 2007 at 14:55 UTC
    That doesn't work on my perl, where sizeof(UV) is 8 and sizeof(char*) is 4 :)

      Yeah, I almost mentioned that "J" might not be the right choice. Previously I'd used "L", but I think there are also situations where that doesn't work. I almost switched to writing code to detect the system's endianness and just pull out the least-significant byte, but the possibility of a system with mixed endianness deterred me.

      If I were to put this code someplace like a module I'd probably compare things like length pack "p", "foo" and length pack "J", 0 to pick which format letter to use with unpack.

      - tye