(answered) nvs_get_stats/nvs_get_used_entry_count returning incorrect count?

User avatar
mzimmers
Posts: 503
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

(answered) nvs_get_stats/nvs_get_used_entry_count returning incorrect count?

Postby mzimmers » Mon Sep 14, 2020 8:14 pm

Hi all -

I'm seeing some results from this code that I don't understand:

Code: Select all

    const char NVS_PARTITION_SYSLOG[] = "nvs_syslog";
    nvs_stats_t nvs_stats;
    nvs_get_stats(NVS_PARTITION_SYSLOG, &nvs_stats);
    printf("%s syslogTask: %s UsedEntries = (%d), FreeEntries = (%d), AllEntries = (%d) NamespaceCount = (%d).\n",
           TAG, NVS_PARTITION_SYSLOG,
           nvs_stats.used_entries, nvs_stats.free_entries,
           nvs_stats.total_entries, nvs_stats.namespace_count);
    printf("%s syslogTask(): m_nvsHandle is %d.\n", TAG, m_nvsHandle);

    size_t used_entries;
    if (nvs_get_used_entry_count(m_nvsHandle, &used_entries) == ESP_OK)
    {
        printf("%s syslogTask(): used_entries is %d.\n", TAG, used_entries);
    }

    nvs_iterator_t it;
    it = nvs_entry_find(NVS_PARTITION_SYSLOG, nullptr, NVS_TYPE_ANY);
    while (it != NULL)
    {
            nvs_entry_info_t info;
            nvs_entry_info(it, &info);
            it = nvs_entry_next(it);
            printf("%s syslogTask(): namespace '%s' key '%s', type '0x%x' \n",
                   TAG, info.namespace_name, info.key, info.type);
    }
    nvs_release_iterator(it);
produces this output:
Syslog syslogTask: nvs_syslog UsedEntries = (20), FreeEntries = (13588), AllEntries = (13608) NamespaceCount = (1).
Syslog syslogTask(): m_nvsHandle is 8.
Syslog syslogTask(): used_entries is 19.
Syslog syslogTask(): namespace 'CD_SYSLOG' key 'LOG_0', type '0x42'
Syslog syslogTask(): namespace 'CD_SYSLOG' key 'LOG_1', type '0x42'
Syslog syslogTask(): namespace 'CD_SYSLOG' key 'LOG_2', type '0x42'
Syslog syslogTask(): namespace 'CD_SYSLOG' key 'LOG_3', type '0x42'
So, it's telling me there are 19 entries, but it's only reporting 4 to me. (The numbers change as the log grows.) Why don't these numbers agree?

Thanks...
Last edited by mzimmers on Tue Sep 15, 2020 5:18 pm, edited 1 time in total.

ESP_Sprite
Posts: 3937
Joined: Thu Nov 26, 2015 4:08 am

Re: nvs_get_stats/nvs_get_used_entry_count returning incorrect count?

Postby ESP_Sprite » Tue Sep 15, 2020 9:54 am

I would guess that it's reporting all written entries, either current or stale, in the nvs_get_stats function call, while the nvs_get_used_entry_count call returns only the total amount of current entries. (The difference is that if you re-write an entry in NVS, it doesn't physically erase the old entry, as erasing is expensive on flash. Instead, it writes a second, newer entry to NVS memory, making the older entry stale/invalid. It does this until there's no more room on flash, at which point it does a garbage collection run erasing some of the stale entries to make more room.)

User avatar
mzimmers
Posts: 503
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: nvs_get_stats/nvs_get_used_entry_count returning incorrect count?

Postby mzimmers » Tue Sep 15, 2020 5:18 pm

Hi Sprite - thanks for the explanation. As a possible future addition, it would be nice to get a count of all *current* entries. In my case, it would be useful for cleaning up any corruption to my log entries.

Who is online

Users browsing this forum: Bing [Bot], k.ifantidis and 37 guests