Arm Mbed and Pelion device management support forum

Bad performance / big number of reads when using LittleFS

Hello,

I am using LittleFS as the the file system for my device.
The purpose of this device is to record and log data to a SPI flash memory (S25FL128L).

There are two files in my memory, a configuration file and a measurement file. A measurement point will be written to the measurement file every second.
Both files are open at the same time so that I can quickly change configuration parameters without having to close or open the other file.

Now that I have set up my code and the logging part is working I noticed that the lfs_file_write / lfs_file_sync performance decreases every time the function is being called. I have set up counters which increment when the read, prog or erase functions are being called by lfs.

Here you can see how the numbers of reads and writes increases over time.
Also, there are some huge spikes visible where the number of reads goes > 1000.
For every write there is a block erase being triggered. Sometimes two.

You can also download the raw data file from https://dataspace.presens.de/#/public/shares-downloads/TpDcXEAYSlA9i9MSamyLVBbLJ7SFiCoz

This is my LittleFS configuration:

#define READ_SIZE 64
#define WRITE_SIZE 64
#define BLOCK_SIZE 4096
#define BLOCK_COUNT 4096
#define CACHE_SIZE 64
#define LOOKAHEAD_SIZE 64
#define BLOCK_CYCLES 500

Here is the code I am using to write a measurement point:

    read_counter = 0;
    write_counter = 0;
    erase_counter = 0;

    // ... write to file
    int32_t err = lfs_file_write(&lfs, &active_measurement_file->file, measurement_row, sizeof(m_lfs_storage_measurement_row_t));
    if (err < 0)
    {
        // return error
    }

    err = lfs_file_sync(&lfs, &active_measurement_file->file);
    if (err < 0)
    {
        // return error
    }

    LOG_INFO("File written %d r, %d w, %d e", read_counter, write_counter, erase_counter);
    //return no error;

Can you tell me what is causing these massive spikes and the performance degradation over time?
Also, why does LittleFS need to delete and rewrite a whole block every time I append a new measurement point to the end of my file?

Hope you can help me.

Regards
Michael