Before I start, I understand the logic behind buffer cache and what not with writes and reads. Whats I was doing was benchmarking the writing and reading of a file with x process doing some x read operatioins and looking at performance hits. The machine Im working with has 512 MB memory so I use fairly large files to assure none of it stays in the buffer (2-3 times the memory size).
What I'm find is no matter what the load on the system the write factor seems to stay constant + or - 3-4 seconds. The read factor has a linear like quality until a certain load then an expodential increase as I suspected would happen.
The issue Im trying to solve is why my writes are not producing the results that I expect. I have played with sync, fsync on the file, ect.. but still no change. Any ideas would be awesome.
Code:
#include <stdio.h>
#include <errno.h>
#include <sys/time.h>
#define BLOCK_SIZE_WRITE 4096
#define FILE_NAME "/tmp/WRITE_TEST_OUT"
double currentTime(void)
{
struct timeval tval;
gettimeofday(&tval, NULL);
return (tval.tv_sec + tval.tv_usec/1000000.0);
}
main(int argc, char **argv){
FILE *fd_w, *fd_r;
unsigned long num_writes = 0;
char buffer[BLOCK_SIZE_WRITE];
char file_name[255];
int returnVal = 0;
unsigned long i,j;
double start_time, end_time, write_time, read_time;
unsigned long FILE_SIZE;
if(argc < 2){
fprintf(stderr, "BAD NUM ARGS: %s [file size] \n", argv[0]);
exit(1);
}
FILE_SIZE = atol(argv[1]);
fprintf(stderr, "FILE SIZE IS %u\n", FILE_SIZE);
fprintf(stderr, "STARTING WRITE TEST\n");
start_time = currentTime();
for(i=0; i<1; i++){
fd_w=fopen(FILE_NAME, "w+b");
if(!fd_w){
perror("ERROR CREATING FILE");
exit(1);
}
num_writes = FILE_SIZE/(unsigned long)BLOCK_SIZE_WRITE;
for(j=0; j<num_writes; j++){
returnVal = fwrite(buffer, BLOCK_SIZE_WRITE, 1, fd_w);
if(returnVal != 1){
perror("ERROR WRITING TO FILE");
break;
}
}
sync();
close(fd_w);
}
end_time = currentTime();
write_time = end_time-start_time;
fprintf(stderr, "ENDING WRITE TEST\n");
fprintf(stderr, "STARTING READ TEST\n");
start_time = currentTime();
for(i=0; i<1; i++){
fd_r=fopen(FILE_NAME, "r");
if(!fd_r){
perror("ERROR OPENING FILE\n");
exit(1);
}
num_writes = FILE_SIZE/BLOCK_SIZE_WRITE;
for(j=0; j<num_writes-1; j++){
returnVal = fread(buffer, BLOCK_SIZE_WRITE, 1, fd_r);
if(returnVal != 1){
printf("J IS %lu\n", j*BLOCK_SIZE_WRITE);
perror("ERROR READING FROM FILE");
break;
}
}
close(fd_r);
}
end_time = currentTime();
read_time = end_time-start_time;
fprintf(stderr, "EDNING READ TEST\n");
printf("%g %g\n", write_time, read_time);
remove(FILE_NAME);
}