SD card - random write delays
Posted: Wed Oct 04, 2023 2:43 pm
Hello, I've noticed random/unpredictable longer write time onto SD card and would like to know why it happens.
Normally, to write 4096 byte block usually takes 4-5 ms, so the speed is 0.70 MB/s in average.
However, sometimes it takes up to 50 ms.
At first I thought it is because FAT library is allocating new sector for file, but it occurs for raw sdmmc_write_sectors() too.
Anyone know what could cause these long delays?
Could it be not-well optimized SDSPI driver, or more like SD card controller performing some erase cycles?
Is there any way to prevent it? Like slow format SD card or something?
Write test results:
512 byte blocks:
2048 byte blocks:
4096 byte blocks:
8192 byte blocks:
SDMMC sector write (bypass FatFS):
Btw, on PC the write speed to the same SD card is about 10 MB/s, so there is a room for improvement on ESP32
But, yeah, thats probably because PC uses 4-bit mode or something...
Normally, to write 4096 byte block usually takes 4-5 ms, so the speed is 0.70 MB/s in average.
However, sometimes it takes up to 50 ms.
At first I thought it is because FAT library is allocating new sector for file, but it occurs for raw sdmmc_write_sectors() too.
Anyone know what could cause these long delays?
Could it be not-well optimized SDSPI driver, or more like SD card controller performing some erase cycles?
Is there any way to prevent it? Like slow format SD card or something?
Write test results:
512 byte blocks:
Code: Select all
Test write (2MB | 512 bytes chunks) | min: 1.475 ms | max: 52.959 ms | total: 9025 ms (0.23 MB/s)
Test write (2MB | 512 bytes chunks) | min: 1.479 ms | max: 53.115 ms | total: 8249 ms (0.25 MB/s)
Test write (2MB | 512 bytes chunks) | min: 1.499 ms | max: 53.282 ms | total: 8861 ms (0.24 MB/s)
Test write (2MB | 512 bytes chunks) | min: 1.503 ms | max: 52.962 ms | total: 8861 ms (0.24 MB/s)
Code: Select all
Test write (2MB | 2048 bytes chunks) | min: 2.684 ms | max: 29.506 ms | total: 3447 ms (0.61 MB/s)
Test write (2MB | 2048 bytes chunks) | min: 2.663 ms | max: 33.304 ms | total: 3718 ms (0.56 MB/s)
Test write (2MB | 2048 bytes chunks) | min: 2.687 ms | max: 21.590 ms | total: 3676 ms (0.57 MB/s)
Test write (2MB | 2048 bytes chunks) | min: 2.670 ms | max: 20.062 ms | total: 3496 ms (0.60 MB/s)
Code: Select all
Test write (2MB | 4096 bytes chunks) | min: 4.219 ms | max: 12.968 ms | total: 2703 ms (0.78 MB/s)
Test write (2MB | 4096 bytes chunks) | min: 4.233 ms | max: 21.813 ms | total: 2827 ms (0.74 MB/s)
Test write (2MB | 4096 bytes chunks) | min: 4.236 ms | max: 29.091 ms | total: 2722 ms (0.77 MB/s)
Test write (2MB | 4096 bytes chunks) | min: 4.234 ms | max: 11.089 ms | total: 2671 ms (0.79 MB/s)
Code: Select all
Test write (2MB | 8192 bytes chunks) | min: 7.381 ms | max: 34.616 ms | total: 2473 ms (0.85 MB/s)
Test write (2MB | 8192 bytes chunks) | min: 7.353 ms | max: 34.327 ms | total: 2375 ms (0.88 MB/s)
Test write (2MB | 8192 bytes chunks) | min: 7.360 ms | max: 17.250 ms | total: 2330 ms (0.90 MB/s)
Test write (2MB | 8192 bytes chunks) | min: 7.358 ms | max: 27.664 ms | total: 2331 ms (0.90 MB/s)
Code: Select all
Test write (2MB | 4096 bytes chunks) | min: 4.089 ms | max: 20.760 ms | total: 2363 ms (0.89 MB/s)
Test write (2MB | 4096 bytes chunks) | min: 4.075 ms | max: 8.001 ms | total: 2325 ms (0.90 MB/s)
Test write (2MB | 4096 bytes chunks) | min: 4.075 ms | max: 20.277 ms | total: 2341 ms (0.90 MB/s)
Test write (2MB | 4096 bytes chunks) | min: 4.082 ms | max: 11.014 ms | total: 2342 ms (0.90 MB/s)
But, yeah, thats probably because PC uses 4-bit mode or something...