0

I am calling the function below continuously after a 500ms interval.

Basically, this function will copy bytes from the source file to the destination file when extra bytes are added to the source file.

I want to make it much faster for my embedded platform.

int CopyResume(CHAR8 *src_file, CHAR8 *dst_file)
{
   int    status               = SUCCESS;
   FILE                    *fp_read             = NULL;
   FILE                    *fp_write            = NULL;
   long                    size_src             = 0L;
   long                    size_dst             = 0L;
   long                    size                 = 0L;
   char                    *buffer              = NULL;

   do
   {
      fp_read = fopen(src_file,"r");
      if(NULL == fp_read)
      {
         status = FAIL;
         break;
      }
      fp_write = fopen(dst_file,"a+");
      if(NULL == fp_write)
      {
         status = FAIL;
         break;
      }

      fseek(fp_write, 0L , SEEK_END);
      size_dst = ftell(fp_write);
      fseek(fp_read, 0L, SEEK_END);
      size_src = ftell(fp_read);
      fseek(fp_read, size_dst, SEEK_SET);

      size = (size_src - size_dst);
      if(size <= 0)
      {
         break;
      }

      buffer = (char*) malloc (sizeof(char) * size + 1);
      if (buffer == NULL)
      {
         status = FAIL;
         break;
      }

      if(fread (buffer,1,size, fp_read) != size)
      {
         status = FAIL;
         break;
      }
      // Write Buffer
      if(fwrite(buffer, 1, size, fp_write) != size)
      {
         status = FAIL;
         break;
      }
   }while(0);

   if(NULL != fp_write)
   {
      fflush(fp_write);
      fsync(fileno(fp_write));
      usleep(1000);
      fclose(fp_write);
      fp_write = NULL;
   }
   if(NULL != fp_read)
   {
      fflush(fp_read);
      fsync(fileno(fp_read));
      usleep(1000);
      fclose(fp_read);
      fp_read = NULL;
   }
   if(NULL != buffer)
   {
      free (buffer);
   }
   return status;
}

My function takes ~100 ms to copy 115kb of data from SRC to DST.

How can I optimize this function to make it faster?

hat
  • 781
  • 2
  • 14
  • 25
Harshil Makwana
  • 155
  • 2
  • 13
  • pre allocating a buffer instead of creating one each loop could possibly speed it up – Neijwiert Jun 11 '18 at 11:30
  • 2
    Why do you call `usleep`? Any reason to call `fsync` and `fflush` before `fclose`? On `flclose` `any unwritten buffered data are flushed to the OS. Any unread buffered data are discarded.` – KamilCuk Jun 11 '18 at 11:34
  • Why flush the read file ? – Ôrel Jun 11 '18 at 11:52
  • use [splice](http://man7.org/linux/man-pages/man2/splice.2.html) its a linux system call that sends data from one file desciptor to another without passing through userspace – Tyker Jun 11 '18 at 11:58
  • @KamiCuk Ok will remove flush and fsync before fclose. Any other suggestions ? – Harshil Makwana Jun 11 '18 at 12:15
  • @Tyker Can you send me example code or any link ? – Harshil Makwana Jun 11 '18 at 12:17
  • @HarshilMakwana - click on the blue `splice` in 'tyker's` comment, It is a link. – ryyker Jun 11 '18 at 12:19
  • Would it be more accurate to say _I am calling below function at 500ms intervals._ ??? (_continuously_ and _every 500 ms_ are at odds.) Or are you really calling it continuously after 500ms? – ryyker Jun 11 '18 at 12:26
  • There is an interesting conversation _[HERE](https://stackoverflow.com/questions/3002122/fastest-file-reading-in-c)_ about fast file reading/writing in C. I like what one of the posters summarized as a good approach to solving such a problem: _Don't prematurely optimize. Make it run, make it right, make it fast,_ _[in]_ _that order._. (emphasis mine) – ryyker Jun 11 '18 at 12:37
  • Yes, Calling it continuously after 500ms – Harshil Makwana Jun 11 '18 at 12:37
  • Thanks @ryyker Let me check it. – Harshil Makwana Jun 11 '18 at 12:47
  • 1
    You have 3 expensive operations inside the loop: open, close, malloc. move them outside the loop. Initially, position both pointer at the same position: the end the the write file. Then loop reading on read file and copy on write one until eof on read. And you should better explain the reason for `usleep` in your code. – Serge Ballesta Jun 11 '18 at 12:48
  • Yeah it is, @SergeBallesta Please tell me solution or other way. – Harshil Makwana Jun 11 '18 at 12:58
  • Removing the sleeps is a good start. – Retired Ninja Jun 11 '18 at 13:42
  • move the whole function to a `thread`, so it does not delay what ever else you application is trying to do. – user3629249 Jun 11 '18 at 14:27
  • @ryyker Please send me code sample , which uses MMAP to copy MP4 files. – Harshil Makwana Jun 12 '18 at 15:06
  • Google query: "mmap example in C to copy file" yielded this post: _[mmap, memcpy to copy file from A to B](https://stackoverflow.com/questions/26582920/mmap-memcpy-to-copy-file-from-a-to-b)_. I do not see why, with some minor adaptation, it should not work for you. – ryyker Jun 12 '18 at 19:25
  • Google query: "mmap example in C to copy file" yielded this post: _[mmap, memcpy to copy file from A to B](https://stackoverflow.com/questions/26582920/mmap-memcpy-to-copy-file-from-a-to-b)_. I do not see why, with some minor adaptation, it should not work for you. But because you are doing this continuously, there are reasons why this may not be the best option for you. Read and understand the first link I gave you in an earlier comment. – ryyker Jun 12 '18 at 19:28

0 Answers0