Programming using direct I/O

Using direct I/O on Linux

Under Linux, the best way to enable direct I/O is on a per-file basis. This is done by using the O_DIRECT flag to the open() system call. For example, in an application written in C, you might see a line similar to this:

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 512-byte aligned and a multiple of 512 bytes in size. Buffers used to read data to and write data from must be page-size aligned.

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

Page 25
Image 25
HP c-Class Performance Tuning manual Programming using direct I/O, Using direct I/O on Linux, Fd = openfilename, Owronly