2010-01-15 03:18:26     Some times write data to sdhc sdcard need much time in bf518 board

Document created by Aaronwu Employee on Aug 20, 2013
Version 1Show Document
  • View in full screen mode

2010-01-15 03:18:26     Some times write data to sdhc sdcard need much time in bf518 board

Bill Xie (CHINA)

Message: 84635   

 

Hello,

 

 

 

      I do a simple test write data packet to sd card in bf518 board+ uClinux -dist-2009R1-RC6 with 8G SanDisk  micro sd card.The normal time 320 bytes write sd card cost about 200us.but some time need much long time by digital oscilloscope and print cyces  to write a data packet (320 bytes) .The image choose write back way and the sd card file system is FAT32.

 

In my project  every 20ms  have data to write to sd card,so writing data to sd card must use less time.

 

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <sys/ioctl.h>

#include <unistd.h>

#include <stdio.h>

#include "Audio_codec.h"

 

 

unsigned long cycles() {

  unsigned long ret;

 

   __asm__ __volatile__

   (

   "%0 = CYCLES;\n\t"

   : "=&d" (ret)

   :

   : "R1"

   );

 

   return ret;

}  

 

void write_file(int h_device)

{

 

    FILE*        fw = NULL;

    short databuf[160]={0x55};

 

    mkdir("/mnt/line-rec", 0);                                      //sd card mount  in /mnt dir

    unsigned long before, after, user;

    int ret;

    long num =0;

 

 

    fw = fopen("/mnt/line-rec/writSd.dat","wb");

     if(!fw)

    {

      printf("openf file failed\n");

      return ;

    }

    while(1)

    {

       ioctl(h_device,IOCTL_GPIO_H,NULL,0);     //set GPIO output high ,just  digital oscilloscope  measure time

       before = cycles();

 

        ret = fwrite(&databuf[0],sizeof(short),160,fw);

        if(ret != 160)

          printf("write len=%d\n",ret);

    

        after = cycles();

        user = after-before;

         ioctl(h_device,IOCTL_GPIO_L,NULL,0);    //set GPIO output low ,just  digital oscilloscope  measure time

         num++;                                                            //test num

         if(num >=4294967294)

           {

           printf("user num:%lu \n",num);

           num = 0;

           }

       if(user>800000)       //  >= 2ms

          printf("user cycle:%lu  %lu\n",user,num);

     

 

       usleep(15000); 

    }

 

    fclose(fw);

 

}

int main()

{

    int fd;

 

    fd = open("/dev/Le_gpio", O_RDWR);   //change gpio output level  for  digital oscilloscope  measure time 

    if(fd<0)

    {

        printf("open Le_gpio faild\n");

        return -1;

    }

    write_file(fd);

 

    close(fd);

  

    return 0;

}

 

 

 

user cycle:867420    2919                                   >2ms

user cycle:822165    6196                                  >2ms

user cycle:831920   9473                                  >2ms

user cycle:823850  12749                                 >2ms

user cycle:229805975  16026             about   574ms

user cycle:816175  25857

user cycle:810910  29133

user cycle:84977285  30772

 

-Bill

QuoteReplyEditDelete

 

 

2010-01-15 03:50:46     Re: Some times write data to sdhc sdcard need much time in bf518 board

Mike Frysinger (UNITED STATES)

Message: 84639   

 

you're basically dealing with a flash device with SD/MMC cards.  that means sometimes pages/sectors will need to be erased and cause a larger delay.

 

reading the signals probably doesnt get you the whole info.  you should look at the actual commands being sent and the status returned from the card to see what is going on when the delays occur.

QuoteReplyEditDelete

 

 

2010-01-15 04:09:41     Re: Some times write data to sdhc sdcard need much time in bf518 board

Wolfgang Muees (GERMANY)

Message: 84643   

 

Bill,

 

my advice is that you should use a fsync() once in a second ( == every 50th write), so that the amount of data that the file system is writing to SD card will not become too large.

 

Remember that you are not writing to SD card, but to a buffer in memory, and the file system is flushing the buffer to SD card only from time to time.

 

regards

 

Wolfgang

TranslateQuoteReplyEditDelete

 

 

2010-01-15 04:12:01     Re: Some times write data to sdhc sdcard need much time in bf518 board

Bill Xie (CHINA)

Message: 84644   

 

Mike,

 

     Thank you.Does chanage  mmc/sd card drivers  can reduce erased delay?

 

 

 

- Bill

QuoteReplyEditDelete

 

 

2010-01-15 04:21:54     Re: Some times write data to sdhc sdcard need much time in bf518 board

Mike Frysinger (UNITED STATES)

Message: 84645   

 

every manufacturer will probably have different timings, and even different revisions of cards

 

however, you'll probably want to try Wolfgang's suggestion first as it might mitigate the effects enough for your needs.

QuoteReplyEditDelete

 

 

2010-01-15 04:22:35     Re: Some times write data to sdhc sdcard need much time in bf518 board

Mike Frysinger (UNITED STATES)

Message: 84646   

 

fdatasync() might be better ... or it might not really matter between the two

QuoteReplyEditDelete

 

 

2010-01-15 04:49:41     Re: Some times write data to sdhc sdcard need much time in bf518 board

Bill Xie (CHINA)

Message: 84648   

 

Mike and Wolfgang,

 

     Thank you,I will try this test.Format sd card to ext2 file system feel effect better than before.

 

 

 

-Bill

QuoteReplyEditDelete

 

 

2010-01-15 05:32:23     Re: Some times write data to sdhc sdcard need much time in bf518 board

Bill Xie (CHINA)

Message: 84650   

 

Using  fdatasync() and fsync() to write file is inefficient and cann't meet my needs.

QuoteReplyEditDelete

 

 

2010-01-15 06:30:31     Re: Some times write data to sdhc sdcard need much time in bf518 board

Robin Getz (UNITED STATES)

Message: 84651   

 

Bill:

 

writing/erasing flash takes time. There isn't anything you can do about it except use a different (Faster) card.

 

when your application write data to the card - it does not go to the card - it goes to a buffer in memory.When the kernel decides it needs to reclaim that memory for something else - it flushes/writes it to the card. Using fsync - puts you in control.

 

To see how fast/slow your card is - try bonnie:

 

https://docs.blackfin.uclinux.org/doku.php?id=linux-kernel:drivers:bfin_sdh#bf51x_sd_mmc_performance

 

or check it with dd - like we did on USB file systems: (with dd)

 

https://docs.blackfin.uclinux.org/doku.php?id=linux-kernel:drivers:musb#bf54x_usb_host_performance_test

 

-Robin

QuoteReplyEditDelete

 

 

2010-01-15 10:55:15     Re: Some times write data to sdhc sdcard need much time in bf518 board

Wolfgang Muees (GERMANY)

Message: 84655   

 

Bill,

 

if you have a data adquisition process which receives 320 bytes of data every 20ms, there is no chance that you can write these data to a SD card in realtime.

 

So you have to do some buffering in the data aquisition thread, and start a second thread which will write a bigger block of data into the filesystem. Use fsync() after fwrite(). The write thread will block until data is really written to SD card, but there is no problem if your (ring-)buffer for data between the 2 threads is big enough.

 

regards

 

Wolfgang

TranslateQuoteReplyEditDelete

 

 

2010-01-17 20:58:07     Re: Some times write data to sdhc sdcard need much time in bf518 board

Bill Xie (CHINA)

Message: 84696   

 

Wolfgang,

 

            Thank you for reply.In my program have 8 channels to write file to sd card,not only one channel.As your suggestion,1 second data(320*50=16000 bytes) need 54.18ms,the rate is about 295k Bytes/s far below sdhc performance.If new write

 

thread to write file,1 second data need 8*55=440ms.During this period,my program have many work to do.I hope to impove rate of write data to sd card.

 

-Bill

QuoteReplyEditDelete

 

 

2010-01-18 01:30:08     Re: Some times write data to sdhc sdcard need much time in bf518 board

Bill Xie (CHINA)

Message: 84697   

 

   I donn't know which process need more delay time when from cache write back to sd card.please to help me to solve

 

this problem.

QuoteReplyEditDelete

 

 

2010-01-18 01:42:46     Re: Some times write data to sdhc sdcard need much time in bf518 board

Bill Xie (CHINA)

Message: 84698   

 

Mike,

 

      I donn't understand the top of  word.The Sd card is not full,why pages/secotors need to be  erased  in sometimes?

 

If sd/mmc card as general disk,Can avoid this problem ?

 

 

 

-Bill

QuoteReplyEditDelete

 

 

2010-01-18 01:49:51     Re: Some times write data to sdhc sdcard need much time in bf518 board

Mike Frysinger (UNITED STATES)

Message: 84699   

 

dont abuse the color options on the forum

 

the SD card has firmware in the SD card probably attempts to do transparent wear leveling.  you really dont have much control over this.  Woflgang's suggestion is probably your best bet.

QuoteReplyEditDelete

 

 

2010-01-18 02:53:55     Re: Some times write data to sdhc sdcard need much time in bf518 board

Bill Xie (CHINA)

Message: 84700   

 

Mike,

 

   Thank you.If I according to Woflgang's suggestion,Directly  write data to sd card will hold about 44% time at the periods that may be lead to no enough time to deal with other work.If my program have 16 channel to wrtie data to sd card,most of the time spent to write files.So i want to find out the best way to write data to sd card. But i donn't know how to solve this problem.

 

-Bill

 

 

QuoteReplyEditDelete

 

 

2010-01-18 04:58:51     Re: Some times write data to sdhc sdcard need much time in bf518 board

Bill Xie (CHINA)

Message: 84710   

 

    Using fwrite() and fflush() can improve write data performance.Average writing rate is about 1.6M bytes/s.

QuoteReplyEditDelete

 

 

2010-01-18 14:43:53     Re: Some times write data to sdhc sdcard need much time in bf518 board

Mike Frysinger (UNITED STATES)

Message: 84724   

 

fwrite is basically a userspace buffer whereas write always goes to the kernel

QuoteReplyEditDelete

 

 

2010-01-19 03:50:26     Re: Some times write data to sdhc sdcard need much time in bf518 board

Bill Xie (CHINA)

Message: 84753   

 

    Sorry,this problem cann't be solved really.I just test in one channel,but  it also have long time in sometimes in mutli-channel

 

wirting data to sd card.

QuoteReplyEditDelete

 

 

2010-01-20 09:53:24     Re: Some times write data to sdhc sdcard need much time in bf518 board

Wolfgang Muees (GERMANY)

Message: 84854   

 

Bill,

 

you wrote:

Directly  write data to sd card will hold about 44% time at the periods that may be lead to no enough time to deal with other work.If my program have 16 channel to wrtie data to sd card,most of the time spent to write files.

 

There _IS_ enough time to do the work. But you have to put more work into the design of your program, going to a multi-tasking application.

 

It's a matter of fact that the writing thread HAS to wait until the SD card is ready to receive new data. So you have to design a writing thread which has no realtime requirements.

 

If you have 8 threads which are doing data aquisition, add a 9th thread for writing the data to SD card, and make a suitable buffering between the data aquision threads and the writing.

 

Remenber that writing to SD card does not require CPU cycles, so the data aquisition may take place without waiting.

 

regards

 

Wolfgang

TranslateQuoteReplyEditDelete

 

 

2010-01-21 21:28:59     Re: Some times write data to sdhc sdcard need much time in bf518 board

Bill Xie (CHINA)

Message: 84948   

 

Wolfgang,

 

       Thank you very much.

 

      you wrote:

 

    < Remenber that writing to SD card does not require CPU cycles, so the data aquisition may take place without waiting.>

 

I don't understand this means.blackfin 518 is only cpu. In multi-threads program, every thread need cpu cycels to finish work.

 

 

 

I don't understand why by cache way spend more than 100 times and 1,000 times the time writing file to sd card  in sometimes.Do we have no way to fix SD card driver?

 

Best regards

 

-Bill

QuoteReplyEditDelete

 

 

2010-01-22 05:36:24     Re: Some times write data to sdhc sdcard need much time in bf518 board

Wolfgang Muees (GERMANY)

Message: 84989   

 

Bill,

 

what I mean with "uses no CPU cycles" is:

 

- the writing to SD card is done by the SDIO controller of the Blackfin CPU. The CPU can do something else and is woken by an interrupt if the SD card transfer is done.

 

So this is the perfect setup for a multitasking application. Data aquisition and writing to mass storage device can be done in parallel threads.

 

You do not understand why the SD card is sometimes using so much time for writing. Let's explain:

 

- the controller of the SD card is connected to the flash chips. And there is an internal, limited buffer in the controller.

 

- If a write command is issued to the controller, the data is placed in the buffer and written from the buffer to the flash.

 

- As long as the buffer is not full, the controller does not send a "busy" signal to the host.

 

- If a certain amount of data is to be written to the SD card, without sufficient free time for the card controller to do some housekeeping (wear leveling, partial block recombination) the card controller is sending a "busy" to the host. This period can last up to 1000 ms (real measured value). The SD card "standard" allows only 250ms here.

 

- I have tested a takeMS SDHC card here which is sending "busy" every 10s for 900ms (if the card is written to with maximum speed).

 

So I would advice you to use a multithreading application, and lay out the buffers between data aquisition and writing for a maximum capacity of 2000ms.

 

If you are using a SD card formated with FAT, and do continious writing, be aware of the accelerated wear-out because of the modifications of the file allocation table. Do not use the "sync" option for mounting, instead use "flush" or "noatime". Do some lifetime tests under heavy load, and use another file system type if the lifetime for FAT is not sufficient - or use the FAT file system in such a way that the file allocation table is not modified by writing.

 

regards

 

Wolfgang

TranslateQuoteReplyEditDelete

 

 

2010-01-23 08:18:20     Re: Some times write data to sdhc sdcard need much time in bf518 board

Bill Xie (CHINA)

Message: 85042   

 

Wolfgang,

 

       Thank you for your reply.

 

Best regards

 

-Bill

Attachments

    Outcomes