Skip to content

Commit a80333e

Browse files
committed
[nrf fromlist] settings: zms: add option to disable updating linked list
When deleting a settings entry the linked list is updated by removing the deleted node. This operation is time consuming and add some delays. For applications that use almost the same set of Setting entries, add an option to make this operation faster at a cost of having some empty nodes in the linked list. These empty nodes can be used later when the deleted entry is written again. Each empty node occupies 16B of storage. Upstream PR #: 87792 Signed-off-by: Riadh Ghaddab <[email protected]> (cherry picked from commit f98c708040fd640f2522ab58de513030d5fba98a)
1 parent afd93bb commit a80333e

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

subsys/settings/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,17 @@ config SETTINGS_ZMS_MAX_COLLISIONS_BITS
190190
The maximum number of hash collisions needs to be well sized depending
191191
on the data that is going to be stored in ZMS and its hash values
192192

193+
config SETTINGS_ZMS_NO_LL_DELETE
194+
bool "Disable deletion of Linked list hashes"
195+
help
196+
For some applications, the Settings delete operation is too long for
197+
ZMS because of the linked list update.
198+
As a tradeoff for performance the linked list is not updated. As a
199+
result, some nodes will be unused and will occupy some space in the
200+
storage.
201+
These nodes will be used again when the same Settings element that has
202+
been deleted is created again.
203+
193204
config SETTINGS_SHELL
194205
bool "Settings shell"
195206
depends on SHELL

subsys/settings/src/settings_zms.c

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ static int settings_zms_dst(struct settings_zms *cf)
6161
return 0;
6262
}
6363

64+
#ifndef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
6465
static int settings_zms_unlink_ll_node(struct settings_zms *cf, uint32_t name_hash)
6566
{
6667
int rc = 0;
@@ -119,6 +120,7 @@ static int settings_zms_unlink_ll_node(struct settings_zms *cf, uint32_t name_ha
119120

120121
return rc;
121122
}
123+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
122124

123125
#if CONFIG_SETTINGS_ZMS_NAME_CACHE
124126
static void settings_zms_cache_add(struct settings_zms *cf, uint32_t name_hash, uint8_t flags)
@@ -169,6 +171,7 @@ static int settings_zms_delete(struct settings_zms *cf, uint32_t name_hash)
169171
if (rc < 0) {
170172
return rc;
171173
}
174+
#ifndef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
172175
rc = settings_zms_unlink_ll_node(cf, name_hash);
173176
if (rc < 0) {
174177
return rc;
@@ -179,7 +182,7 @@ static int settings_zms_delete(struct settings_zms *cf, uint32_t name_hash)
179182
if (rc < 0) {
180183
return rc;
181184
}
182-
185+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
183186
#ifdef CONFIG_SETTINGS_ZMS_NAME_CACHE
184187
/* Update the flag of the Settings entry in cache. */
185188
uint8_t cache_flags = 0;
@@ -225,15 +228,27 @@ static int settings_zms_load(struct settings_store *cs, const struct settings_lo
225228
ZMS_DATA_ID_OFFSET);
226229

227230
if ((rc1 <= 0) || (rc2 <= 0)) {
228-
/* Settings item is not stored correctly in the ZMS.
229-
* ZMS entry for its name or value is either missing
230-
* or deleted. Clean dirty entries to make space for
231-
* future settings item.
231+
/* In case we are not updating the linked list, this is an empty mode
232+
* Just continue
233+
*/
234+
#ifndef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
235+
/* Otherwise, Settings item is not stored correctly in the ZMS.
236+
* ZMS entry for its name or value is either missing or deleted.
237+
* Clean dirty entries to make space for future settings item.
232238
*/
233239
ret = settings_zms_delete(cf, ZMS_NAME_ID_FROM_LL_NODE(ll_hash_id));
234240
if (ret < 0) {
235241
return ret;
236242
}
243+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
244+
245+
ret = zms_read(&cf->cf_zms, ll_hash_id, &settings_element,
246+
sizeof(struct settings_hash_linked_list));
247+
if (ret < 0) {
248+
return ret;
249+
}
250+
/* update next ll_hash_id */
251+
ll_hash_id = settings_element.next_hash;
237252
continue;
238253
}
239254

@@ -282,6 +297,9 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
282297
bool write_name;
283298
int rc = 0;
284299
int first_available_hash_index = -1;
300+
#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
301+
bool ll_node_exist = false;
302+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
285303

286304
if (!name) {
287305
return -EINVAL;
@@ -304,11 +322,18 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
304322
} else {
305323
write_name = false;
306324
}
325+
#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
326+
/* In this case the settings entry is deleted, which means that
327+
* its linked list node still exist in this case and do not need
328+
* to be updated.
329+
*/
330+
ll_node_exist = true;
331+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
307332
goto no_hash_collision;
308333
}
309334
#endif
310335

311-
/* Let's find out if there is no hash collisions in the storage */
336+
/* Let's find out if there are hash collisions in the storage */
312337
write_name = true;
313338

314339
for (int i = 0; i <= cf->hash_collision_num; i++) {
@@ -382,6 +407,20 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
382407
if (rc < 0) {
383408
return rc;
384409
}
410+
#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
411+
if (ll_node_exist) {
412+
goto no_ll_update;
413+
}
414+
/* verify that the ll_node doesn't exist otherwise do not update it */
415+
rc = zms_read(&cf->cf_zms, name_hash | 1, &settings_element,
416+
sizeof(struct settings_hash_linked_list));
417+
if (rc >= 0) {
418+
goto no_ll_update;
419+
} else if (rc != -ENOENT) {
420+
return rc;
421+
}
422+
/* else the LL node doesn't exist let's update it */
423+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
385424
/* write linked list structure element */
386425
settings_element.next_hash = 0;
387426
/* Verify first that the linked list last element is not broken.
@@ -411,6 +450,9 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
411450
cf->second_to_last_hash_id = cf->last_hash_id;
412451
cf->last_hash_id = name_hash | 1;
413452
}
453+
#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
454+
no_ll_update:
455+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
414456
#ifdef CONFIG_SETTINGS_ZMS_NAME_CACHE
415457
/* Add the flags of the written settings entry in cache */
416458
cache_flags = 0;

0 commit comments

Comments
 (0)