2

I'm writing some bytes to the filesystem. Afterward I'm calling sync to ensure the buffered data is flushed to the SD card. This command takes several seconds!

I'm working with raspbian. My SD Card is a 16GB Kingston Class 10 (industrial). Logging the disk io with sudo iotop -o -d 1 -a say, that only some 100 KBytes are written to disk.

To find out the source of the problem I'm doing following:

  • Watching the output of sudo iotop -o -d 1 -a in one Terminal
  • Run some application that writes about every minute some 100Kbytes
  • Calling time sync in an other terminal after the write is done. This one takes sometimes several seconds (up to 5sec). (if nothing is written to disk it takes only some milliseconds)

Whats wrong that sync takes as long?

5
  • How are you calling sync? What command line options?
    – Craig
    Commented Jun 20, 2018 at 14:52
  • How often are you calling sync?
    – Craig
    Commented Jun 20, 2018 at 15:38
  • 1
    sync has no args. Calling sync after writing some kb to the filesystem.
    – powerpete
    Commented Jun 21, 2018 at 6:39
  • What is the frequency of the calls? How many times per second/minute/hour?
    – Craig
    Commented Jun 21, 2018 at 14:06
  • Added some additional info
    – powerpete
    Commented Jun 22, 2018 at 7:03

2 Answers 2

1

First, sync synchronizes all mounted file systems, not just the one you are writing to. Using Linux-specific syncfs call instead of sync could help here: if syncfs is always fast, the problem is likely with a different filesystem not the SD card.

The second possible offender could be the journal. Disabling the journal on your SD card will likely make sync much more deterministic.

In any case, check for background tasks which could be modifying files, like unattended upgrades. You cannot synchronize just the 100KB your program has written, you always synchronize the entire filesystem. You should have seen this in iotop though.

-1

Until sync or flush is called, the writes to the filesystem might be cached in RAM. Or perhaps metadata to the filesystem isn't written yet.

Once sync is called, all this data must be really written to the filesystem.

This is why it's a bad idea to turn any linux system off until you've shutdown, which does the equivalent of a sync. Some cached data might not have been written yet, or you could be turning it off in the middle of a write.

1
  • 6
    nice explanation what sync is doing. The Question why it takes so long remains open.
    – powerpete
    Commented Jun 25, 2018 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.