This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/5298] New: ftell() after a write() causes glibc to flush the buffer


Calling ftell() after a write() call causes glibc to flush the buffer. This
results in performance degradations for certain of our user applications where
we depend on knowing where the file-pointer while doing small record writes.
Below is  a sample code which demonstrates the issues that we are seeing

testwrite.c

#include <stdlib.h>

#define MEGABYTE 1048576

int main(int argc, char *argv[]) {
  int ret;
  char filename[512];
  char *data;
  int dsize, size, chunksize, count;
  FILE *myfile;

  int ftell_on = 0;
  chunksize = 208;

  //===============================================================
  // Process arguments
  //   <output_file>
  //   <write_size>
  //   <0/1>
  //===============================================================
  char *usage_str = "Usage arguments : <output_file> <file_size> <0/1> \n";
  if (argc < 3) {
    printf ("Invalid arguments : \n");
    printf ("\t %s \n", usage_str);
    return 1;
  }

  sscanf(argv[1],"%s",filename);
  sscanf(argv[2],"%d",&dsize);
  sscanf(argv[3],"%d",&ftell_on);

  dsize *= MEGABYTE;

  data=malloc(sizeof(char) * dsize);

  if(!data) {
    perror("malloc() failed");
    abort;
  }

  myfile = fopen(filename,"w");

  if(myfile == NULL) {
    perror("fopen failed()");
    abort;
  }

  for(count=0; count<dsize; count+=chunksize) {
    if(count+chunksize <= dsize) {
      size = chunksize;
    }
    else {
      size = dsize-count;
    }

    ret = fwrite(data,size,1,myfile);

    if(ret != 1) {
      perror("Write failed\n");
      abort();
    }

   if (ftell_on == 1) {
     int pos;
     pos = ftell(myfile);
     if (pos < 0) {
        perror("ftell() failed");
        abort();

     }
   }

  }
  ret = fclose(myfile);

}

gcc -o testwrite.exe testwrite.c

1. Run code with ftell on

strace ./testwrite.exe file 1 1

write(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 208) = 208
write(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 208) = 208


2. Run code with ftell off 

strace ./testwrite.exe file 1 0


write(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 32768) = 32768
write(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 32768) = 32768

-- 
           Summary: ftell() after a write() causes glibc to flush the buffer
           Product: glibc
           Version: unspecified
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: libc
        AssignedTo: drepper at redhat dot com
        ReportedBy: abdul dot sindhi at pw dot utc dot com
                CC: glibc-bugs at sources dot redhat dot com


http://sourceware.org/bugzilla/show_bug.cgi?id=5298

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]