Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

settings: nvs: Fix first write issue with cache #69103

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions subsys/settings/include/settings/settings_nvs.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ struct settings_nvs {
} cache[CONFIG_SETTINGS_NVS_NAME_CACHE_SIZE];

uint16_t cache_next;
uint16_t cache_total;
bool loaded;
#endif
};

Expand Down
23 changes: 23 additions & 0 deletions subsys/settings/src/settings_nvs.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ int settings_nvs_dst(struct settings_nvs *cf)
}

#if CONFIG_SETTINGS_NVS_NAME_CACHE
#define SETTINGS_NVS_CACHE_OVFL(cf) ((cf)->cache_total > ARRAY_SIZE((cf)->cache))

static void settings_nvs_cache_add(struct settings_nvs *cf, const char *name,
uint16_t name_id)
{
Expand Down Expand Up @@ -129,12 +131,22 @@ static int settings_nvs_load(struct settings_store *cs,
ssize_t rc1, rc2;
uint16_t name_id = NVS_NAMECNT_ID;

#if CONFIG_SETTINGS_NVS_NAME_CACHE
uint16_t cached = 0;

cf->loaded = false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will disable the optimization if settings_load_subtree_direct with a handler that breaks the traversal before reaching NVS_NAMECNT_ID is used. It's still better than the existing behavior, so it's not a blocking comment, but just letting you know.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, true. And also if an app loads a subtree (directly or not) several times, the cache will contain duplicated entries. The only way to make cache work correctly is to only use settings_load() unfortunately.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add info on that behavior to documentation?
Should we log an issue for that to be solved at some point?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding an issue I'm not certain. The cache kind of does what it is asked to do. An application should just be aware of this. I think documenting this behavior should be enough. I'll need to allocate a time to do this though.

#endif

name_id = cf->last_name_id + 1;

while (1) {

name_id--;
if (name_id == NVS_NAMECNT_ID) {
#if CONFIG_SETTINGS_NVS_NAME_CACHE
cf->loaded = true;
cf->cache_total = cached;
#endif
break;
}

Expand Down Expand Up @@ -186,6 +198,7 @@ static int settings_nvs_load(struct settings_store *cs,

#if CONFIG_SETTINGS_NVS_NAME_CACHE
settings_nvs_cache_add(cf, name, name_id);
cached++;
#endif

ret = settings_call_set_handler(
Expand Down Expand Up @@ -231,6 +244,13 @@ static int settings_nvs_save(struct settings_store *cs, const char *name,
write_name_id = cf->last_name_id + 1;
write_name = true;

#if CONFIG_SETTINGS_NVS_NAME_CACHE
/* We can skip reading NVS if we know that the cache wasn't overflowed. */
if (cf->loaded && !SETTINGS_NVS_CACHE_OVFL(cf)) {
goto found;
}
#endif

while (1) {
name_id--;
if (name_id == NVS_NAMECNT_ID) {
Expand Down Expand Up @@ -325,6 +345,9 @@ static int settings_nvs_save(struct settings_store *cs, const char *name,
#if CONFIG_SETTINGS_NVS_NAME_CACHE
if (!name_in_cache) {
settings_nvs_cache_add(cf, name, write_name_id);
if (cf->loaded && !SETTINGS_NVS_CACHE_OVFL(cf)) {
cf->cache_total++;
}
}
#endif

Expand Down
Loading