LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > General
User Name
Password
General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!

Notices


Reply
  Search this Thread
Old 08-28-2004, 11:01 PM   #1
jwstric2
Member
 
Registered: Jan 2004
Posts: 105

Rep: Reputation: 15
shouldn't disk writes be slower than reads


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);
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
freeBSD 4.9 SLOW writes to disk dkc_ace *BSD 1 08-04-2005 04:16 PM
CD R/RW: reads or writes but not both hatha Linux - General 6 04-17-2005 07:12 PM
Floppy Disk Writes hangs the kernel marcosm Linux - Hardware 3 04-13-2005 08:14 PM
editor that reads & writes msword doc files leobing Linux - General 3 04-08-2004 11:35 AM
cdburner reads not writes NGraphiX Linux - Hardware 1 02-28-2003 04:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > General

All times are GMT -5. The time now is 12:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration