directio
Hi All,
Iam working on SunOS 5.8 Generic_117350-11 sun4u sparc SUNW,Sun-Fire-880 system.
I have read in manpages of directio() system call that, using this we can improve performance(improve speed) in reading and writing big(GB) files.
Below is the sample program. For testing i tried both with and without using directio() call and checked time taken by the application using "time" command.
But iam not seeing the performance improvement (speed).
Can you please let me know the procedure i followed test directio() is correct or not.
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main()
{
FILE *file, *file1;
char line[2000]={0};
int fd,fd1;
fd = -1;
/*file = fopen("sample1","r");
file1 = fopen("outfile","w");*/
fd = open("sample1",O_RDONLY);
if(fd == -1)
perror("");
fd1 = open("outfile",O_WRONLY);
/*
if(file && file1)
{
fd=fileno(file);
fd1=fileno(file1);
}
*/
directio(fd,DIRECTIO_ON);
directio(fd1,DIRECTIO_ON);
//while(fgets(line,sizeof(line),file))
while(read(fd,line,sizeof(line)) > 0)
{
//strcat(line,"\n");
//fputs(line,file1);
write (fd1,line,sizeof(line));
}
close(fd);
close(fd1);
//fclose(file);
//fclose(file1);
}
[1480 byte] By [
peppya] at [2007-11-27 10:55:20]

# 1
directio performance is a difficult thing to measure since its effects are mostly indirect.
For example it avoids having useful data pushed out of cache by your big IO.
So one way to test it might be something like.
time cat bigfile > /dev/null # cache bigfile
time cat bigfile >/dev/null # should be faster second time..
program with or without directio
time cat bigfile >/dev/null
When using directio, bigfile should still be cached. So the final cat should still be fast.
# 2
BTW, your program has a minor bug.
read returns the number of of bytes read. But you always write out 2000 bytes regardless. So on the last read/write your probably writing out a bogus chunk at the end of the buffer.
Oh and I've just noticed a more major problem. Directio only works on buffers that are a multiple of the sector size in length and are "aligned" on sector boundaries in the file.
And the memory buffer also have to be on a 2 byte boundary in memory. Although thats probably true here anyway, if it is its by luck not design.
Since your reading writing in chunks of 2000 bytes. You won't in general be on sector boundaries.
But I believe a sector is 512 bytes. So the minimal change is to make your buffer size 2048 bytes.
But for efficiency reasons, and that is why your doing this after all, you should make your buffers nice and big. So I recommend you make your buffer a malloc'ed block of at least a meg in size.
And to avoid drifting off alignment in case of a short read. You should keep reading in a loop until your buffer is full before writing it out...