littlefs-project / littlefs

A little fail-safe filesystem designed for microcontrollers
BSD 3-Clause "New" or "Revised" License
4.92k stars 774 forks source link

How much memory is my lfs using #869

Open Dan2605 opened 10 months ago

Dan2605 commented 10 months ago

Hi @geky, I want to calculate the current memory usage of the file system configuration I have set up. Is there a way to do this? I know of lfs_fs_size, but does this take into account of other "hidden" memory usage, such as maybe from the superblocks, unused blocks etc?

geky commented 10 months ago

Hi @Dan2605, lfs_fs_size does include all blocks, including superblocks and future auxiliary data structures, so I believe it's what you're looking for.

Though note sometimes littlefs needs to allocate blocks to get work done, so it's always a good idea to reserve some extra storage in that case. This is a common problem for CoW/logging filesystems.

spresg commented 10 months ago

Hi @geky How to find out the actual memory used? Can I use lfs_stat()?

geky commented 10 months ago

This should get you the usage in bytes, assuming littlefs is mounted and assuming you know the block_size of your device:

lfs_ssize_t blocks = lfs_fs_size(&lfs);
if (blocks < 0) {
    return blocks;
}

printf("usage: %d B\n", blocks * BLOCK_SIZE);

I considered adding this information to lfs_fs_stat, but lfs_fs_size has the potential to take a long time on larger filesystems, so I am hesitant. We can always add the field to lfs_fs_stat if it's useful in the future.