Go Down

Topic: writing 8 bytes of data to SD card in each loop, should i flush? (Read 347 times) previous topic - next topic

steveh2112

i want to log some debug data to sdcard from my main loop running at about 10Hz, its only 8 bytes so not a huge data rate

the issue is i don't want the sd library buffering up a ton of data then slowing down one of the loops, i'd rather take the performance hit on each loop equally. so i was planning to do a flush on each 8 byte write block. i also want to make so i have all the latest data if i lose power.

but we all know the toilet rules, 'if it's yellow, let it mellow, if it's brown, flush it down' so should i treat my 8 byte block like a no 1 or a no 2?

thanks

Lucario448

but we all know the toilet rules, 'if it's yellow, let it mellow, if it's brown, flush it down'
I knew how to save water in the toilet, but they never taught it to me that way. Oh man you made my day, that was a pretty good one indeed :D
All right, back to your question.


Indeed, the same rule applies for (disk) caches; mostly for the sake of performance (analogous to the water savings in a toilet) and a little for wear issues.

Assuming a precisely timed event (i.e. triggered by a timer interrupt) of 10 Hz that writes 8 bytes to an already opened file in a SD card, means that you have at least 98 ms to spare between events; which is plenty for a cache flush (that takes at most 5 ms).
Regardless of how much data you write without flushing, this process will take the same amount of time; and as I said before, it's not enough to cause a sporadic jitter in your sampling unless the event isn't precisely timed.



Summarizing the answer of your question: "should I flush every time or should I let it happen automatically?", I will say... do whatever you feel is more convenient.
Nevertheless, keep in mind that by flushing every time, you'll be somewhat accelerating the SD card's wear; not by a panicking magnitude, but considerable enough in the long run.

steveh2112

thanks, so if the data size isn't particularly important maybe i'll wait for about 10 cycles (1 sec) before flushing. that means i'll loose the last second of data when i lose power but i think no big deal

Lucario448

Again, if you want or feel more convenient.

Also remember that the card's wear won't be catastrophically fast, it's not like you'll break it in an hour of continuous writting; specially when most (if not all) SD cards have a built-in wear-leveling system.
Yet I won't guarantee it will last for more than a month by flushing non-stop at a pace of 10 Hz.


that means i'll loose the last second of data when i lose power but i think no big deal
That's a trade-off you always have to do with re-writable solid-state non-volatile memories: reliability vs longevity.

Go Up