Skip to content

Commit fcd8edb

Browse files
committed
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. Signed-off-by: Riadh Ghaddab <[email protected]>
1 parent 2ce391b commit fcd8edb

File tree

2 files changed

+58
-32
lines changed

2 files changed

+58
-32
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: 47 additions & 32 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,32 +120,7 @@ static int settings_zms_unlink_ll_node(struct settings_zms *cf, uint32_t name_ha
119120

120121
return rc;
121122
}
122-
123-
static int settings_zms_delete(struct settings_zms *cf, uint32_t name_hash)
124-
{
125-
int rc = 0;
126-
127-
rc = zms_delete(&cf->cf_zms, name_hash);
128-
if (rc >= 0) {
129-
rc = zms_delete(&cf->cf_zms, name_hash + ZMS_DATA_ID_OFFSET);
130-
}
131-
if (rc < 0) {
132-
return rc;
133-
}
134-
135-
rc = settings_zms_unlink_ll_node(cf, name_hash);
136-
if (rc < 0) {
137-
return rc;
138-
}
139-
140-
/* Now delete the current linked list element */
141-
rc = zms_delete(&cf->cf_zms, name_hash | 1);
142-
if (rc < 0) {
143-
return rc;
144-
}
145-
146-
return rc;
147-
}
123+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
148124

149125
#if CONFIG_SETTINGS_ZMS_NAME_CACHE
150126
static void settings_zms_cache_add(struct settings_zms *cf, uint32_t name_hash, uint8_t flags)
@@ -194,6 +170,7 @@ static int settings_zms_delete(struct settings_zms *cf, uint32_t name_hash)
194170
if (rc < 0) {
195171
return rc;
196172
}
173+
#ifndef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
197174
rc = settings_zms_unlink_ll_node(cf, name_hash);
198175
if (rc < 0) {
199176
return rc;
@@ -204,7 +181,7 @@ static int settings_zms_delete(struct settings_zms *cf, uint32_t name_hash)
204181
if (rc < 0) {
205182
return rc;
206183
}
207-
184+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
208185
#ifdef CONFIG_SETTINGS_ZMS_NAME_CACHE
209186
/* Update the flag of the Settings entry in cache. */
210187
uint8_t cache_flags = 0;
@@ -250,15 +227,26 @@ static int settings_zms_load(struct settings_store *cs, const struct settings_lo
250227
ZMS_DATA_ID_OFFSET);
251228

252229
if ((rc1 <= 0) || (rc2 <= 0)) {
253-
/* Settings item is not stored correctly in the ZMS.
254-
* ZMS entry for its name or value is either missing
255-
* or deleted. Clean dirty entries to make space for
256-
* 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.
257237
*/
258238
ret = settings_zms_delete(cf, ZMS_NAME_ID_FROM_LL_NODE(ll_hash_id));
259239
if (ret < 0) {
260240
return ret;
261241
}
242+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
243+
/* update next ll_hash_id */
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+
ll_hash_id = settings_element.next_hash;
262250
continue;
263251
}
264252

@@ -307,6 +295,9 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
307295
bool write_name;
308296
int rc = 0;
309297
int first_available_hash_index = -1;
298+
#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
299+
bool ll_node_exist = false;
300+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
310301

311302
if (!name) {
312303
return -EINVAL;
@@ -329,11 +320,18 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
329320
} else {
330321
write_name = false;
331322
}
323+
#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
324+
/* In this case the settings entry is deleted, which means that
325+
* its linked list node still exist in this case and do not need
326+
* to be updated.
327+
*/
328+
ll_node_exist = true;
329+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
332330
goto no_hash_collision;
333331
}
334332
#endif
335333

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

339337
for (int i = 0; i <= cf->hash_collision_num; i++) {
@@ -407,6 +405,20 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
407405
if (rc < 0) {
408406
return rc;
409407
}
408+
#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
409+
if (ll_node_exist) {
410+
goto no_ll_update;
411+
}
412+
/* verify that the ll_node doesn't exist otherwise do not update it */
413+
rc = zms_read(&cf->cf_zms, name_hash | 1, &settings_element,
414+
sizeof(struct settings_hash_linked_list));
415+
if (rc >= 0) {
416+
goto no_ll_update;
417+
} else if (rc != -ENOENT) {
418+
return rc;
419+
}
420+
/* else the LL node doesn't exist let's update it */
421+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
410422
/* write linked list structure element */
411423
settings_element.next_hash = 0;
412424
/* Verify first that the linked list last element is not broken.
@@ -436,6 +448,9 @@ static int settings_zms_save(struct settings_store *cs, const char *name, const
436448
cf->second_to_last_hash_id = cf->last_hash_id;
437449
cf->last_hash_id = name_hash | 1;
438450
}
451+
#ifdef CONFIG_SETTINGS_ZMS_NO_LL_DELETE
452+
no_ll_update:
453+
#endif /* CONFIG_SETTINGS_ZMS_NO_LL_DELETE */
439454
#ifdef CONFIG_SETTINGS_ZMS_NAME_CACHE
440455
/* Add the flags of the written settings entry in cache */
441456
cache_flags = 0;

0 commit comments

Comments
 (0)