Programming using direct I/O
Using direct I/O on Linux
Under Linux, the best way to enable direct I/O is on a
fd = open(filename, O_WRONLY);
To make this file accessible through unbuffered or direct I/O, change the line to the following:
fd = open(filename, O_WRONLY O_DIRECT );
The IO Accelerator requires that all I/O performed on a device using O_DIRECT must be
open(), getpagesize(), and posix_memalign().
The following is a simple application that writes a pattern to the entire IO Accelerator in 1 MiB chunks, using O_DIRECT: Generally, performance can be slightly enhanced by using pwrite instead of write.
#define _XOPEN_SOURCE 600 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #define __USE_GNU #include <fcntl.h> #include <string.h> #define FILENAME "/dev/fiob"
int main( int argc, char **argv)
{
void *buf; int ret = 0;
int ps = getpagesize();
unsigned long long int bytes_written = 0; int fd;
if( (ret = posix_memalign(&buf, ps, ps*256)) ) { perror("Memalign failed");
exit(ret);
}
Programming using direct I/O 25