jfroebe has asked for the wisdom of the Perl Monks concerning the following question:

I've written a C program that creates a 20GB file using O_DIRECT and O_ASYNC correctly. The trick to write to a file using O_DIRECT is that I have to align the buffer with respect to the memory block size.

When I write a similar program using Perl, I receive the expected "System write error: Invalid argument" as the buffer is not aligned.

So, my question is: Is it possible, in Perl, to align the buffer?

#!/usr/bin/perl use strict; use warnings; $|++; use Fcntl qw(:DEFAULT O_ASYNC O_DIRECT); my $FH; sysopen($FH, "./test.dat", O_WRONLY | O_TRUNC | O_CREAT | O_ASYNC | O_ +DIRECT, 0666); my $BUFFER = "0"x1048576; my $BUFSIZE = 1048576; for (my $i = 0; $i < 20480; $i++) { my $written = syswrite($FH, $BUFFER, $BUFSIZE); die "System write error: $!\n" unless defined $written; }

Jason L. Froebe

Don't forget Mother's Day! It's this Sunday so get her some sock yarn.

Blog, Tech Blog

Replies are listed 'Best First'.
Re: O_DIRECT & O_ASYNC, Linux & Perl
by sgifford (Prior) on May 11, 2007 at 03:04 UTC
    Sys::Mmap will give you a page-aligned chunk of memory:
    #!/usr/bin/perl use strict; use warnings; use Fcntl qw(:DEFAULT O_ASYNC O_DIRECT); use Sys::Mmap; sysopen(my $FH,"./test.dat", O_WRONLY | O_TRUNC | O_CREAT | O_ASYNC | O_DIRECT, 0666) or die "Couldn't open\n"; my $BUFSIZE = 1048576; my $BUFFER; mmap($BUFFER,$BUFSIZE,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON, STDOUT) or die "Couldn't mmap\n"; for (my $i = 0; $i < 20480; $i++) { my $written = syswrite($FH,$BUFFER,$BUFSIZE); die "System write error: $!\n" unless defined $written; }

    Update: use sysopen with the proper flags. Thanks for the correction, jfroebe! Looks like I screwed up the code cleaning it up for posting.

      Thanks! That works perfectly (code below with the O_DIRECT & O_ASYNC flags)

      #!/usr/bin/perl use strict; use warnings; $|++; use Fcntl qw(:DEFAULT O_ASYNC O_DIRECT); use Sys::Mmap; my $FH; sysopen($FH, "./test.dat", O_WRONLY | O_TRUNC | O_CREAT | O_ASYNC | O_ +DIRECT, 0666); my $BUFSIZE = 1048576; my $BUFFER; mmap($BUFFER, $BUFSIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, ST +DOUT) or die "Couldn't mmap\n"; substr($BUFFER, 0, $BUFSIZE) = "0"x$BUFSIZE; for (my $i = 0; $i < 20480; $i++) { my $written = syswrite($FH, $BUFFER, $BUFSIZE); die "System write error: $!\n" unless defined $written; }

      Jason L. Froebe

      Help find a cure for breast cancer! Net proceeds benefit the Susan G. Komen Breast Cancer Foundation and the National Philanthropic Trust. Help by donating - I'm walking 60 miles in 3 days in August 2007. (The day I return from TechWave is the first day of the Walk).

      Blog, Tech Blog

Re: O_DIRECT & O_ASYNC, Linux & Perl
by tirwhan (Abbot) on May 11, 2007 at 03:44 UTC

      LOL ;-) Yup. Saw that. He also said that raw partitions were a bad thing. Unfortunately/Fortunately, he doesn't know everything. Neither raw partitions or directio are going anywhere :)

      Perhaps I should explain myself: The problem is that while Linus is extremely good at what he does, he isn't always able to see the needs of the users. In this case, raw partitions and O_DIRECT are used heavily in the DBMS community because they are the best tools that we have. The methods suggested by Linus in his post months ago, aren't able to ensure reliability or performance needed by the DBMS systems. These DBMS systems are used in mission critical environments where an outage of a few minutes can be tens of thousands of dollars. Preventing corruption in the database (either physical or logical) is critical.

      I highly doubt that raw partitions or O_DIRECT will be going anywhere anytime soon. What I do expect is a rewrite of the O_DIRECT mechanism to appease Linus and others. :)

      Jason L. Froebe

      Help find a cure for breast cancer! Net proceeds benefit the Susan G. Komen Breast Cancer Foundation and the National Philanthropic Trust. Help by donating - I'm walking 60 miles in 3 days in August 2007. (The day I return from TechWave is the first day of the Walk).

      Blog, Tech Blog