Skip to content

Commit 40c9851

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 c6366b462e9d63789c691cb9de98936b86349537)
1 parent beee654 commit 40c9851

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)
@@ -168,6 +170,7 @@ static int settings_zms_delete(struct settings_zms *cf, uint32_t name_hash)
168170
if (rc < 0) {
169171
return rc;
170172
}
173+
#ifndef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
171174
rc = settings_zms_unlink_ll_node(cf, name_hash);
172175
if (rc < 0) {
173176
return rc;
@@ -178,7 +181,7 @@ static int settings_zms_delete(struct settings_zms *cf, uint32_t name_hash)
178181
if (rc < 0) {
179182
return rc;
180183
}
181-
184+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
182185
#ifdef CONFIG_SETTINGS_ZMS_NAME_CACHE
183186
/* Update the flag of the Settings entry in cache. */
184187
uint8_t cache_flags = 0;
@@ -224,15 +227,27 @@ static int settings_zms_load(struct settings_store *cs, const struct settings_lo
224227
ZMS_DATA_ID_OFFSET);
225228

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

@@ -281,6 +296,9 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
281296
bool write_name;
282297
int rc = 0;
283298
int first_available_hash_index = -1;
299+
#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
300+
bool ll_node_exist = false;
301+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
284302

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

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

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

0 commit comments

Comments
 (0)