Skip to content

[Backport v4.0.99-ncs1-branch] Settings ZMS backend optimizations #2758

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

Merged
merged 27 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
49f2e7b
Revert "[nrf noup] settings: zms: use dedicated lookup cache hash fun…
rghaddab Feb 19, 2025
41e223c
Revert "[nrf fromlist] settings: ZMS: add a backend for ZMS (Zephyr M…
rghaddab Feb 19, 2025
4dbae5e
[nrf fromtree] settings: ZMS: add a backend for ZMS (Zephyr Memory St…
rghaddab Sep 17, 2024
590b94e
[nrf fromtree] tests: settings: Performance test for settings
omkar3141 Oct 23, 2024
8838b6c
[nrf fromtree] fs: zms: multiple style fixes from previous PR review
rghaddab Oct 30, 2024
b187367
[nrf fromtree] samples/subsys/fs/zms: run on native_sim instead of na…
aescolar Feb 25, 2025
6a1435b
[nrf fromtree] samples/subsys/fs/zms: Allow it to run and add check
aescolar Feb 25, 2025
bed9eaf
[nrf fromtree] sample: zms: add ZMS sample Kconfig
butok Feb 25, 2025
604a4ef
[nrf fromtree] samples: zms: fix the loop_cnt iteration 0 check
butok Mar 3, 2025
2d9d047
[nrf fromtree] settings: zms: fix some bugs related to the name's ID
rghaddab Feb 22, 2025
a586d96
[nrf fromtree] doc: settings: add reference to ZMS backend
rghaddab Feb 22, 2025
752bf34
[nrf fromtree] tests: zms: allow to build & run on real platforms
butok Feb 26, 2025
45ede2d
[nrf fromtree] samples: zms: fix 9999 messages dropped
butok Mar 4, 2025
0cce904
[nrf fromlist] settings: zms: add option to disable updating linked list
rghaddab Feb 27, 2025
c5e4818
[nrf fromlist] zms: remove non needed lookup cache before writing
rghaddab Mar 7, 2025
cd88d10
[nrf fromlist] zms: optimize cache for settings subsystem
rghaddab Mar 7, 2025
6c34114
[nrf fromlist] settings: zms: load only subtree in the argument
rghaddab Mar 11, 2025
c24329a
[nrf fromlist] settings: zms: add cache for linked list hash
rghaddab Mar 13, 2025
343aafe
[nrf fromlist] settings: add an API function to load only one setting…
rghaddab Mar 18, 2025
7a17f53
[nrf fromlist] tests: settings: add a functional test for settings_lo…
rghaddab Mar 24, 2025
276e036
[nrf fromlist] tests: settings: add functional test for ZMS
rghaddab Mar 24, 2025
2169782
[nrf fromlist] settings: zms: add more robustness to the save function
rghaddab Mar 18, 2025
0f11556
[nrf fromlist] settings: zms: recover linked list if broken
rghaddab Mar 18, 2025
ae53347
[nrf fromlist] settings: add new API function settings_get_val_len()
rghaddab Mar 25, 2025
562d797
[nrf fromlist] settings: zms: use the safe function strnlen instead o…
rghaddab Mar 20, 2025
1b69855
[nrf fromlist] doc: settings: new API functions
rghaddab Mar 27, 2025
ef64623
[nrf fromlist] settings: zms: code style clean up
rghaddab Apr 4, 2025
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
59 changes: 44 additions & 15 deletions doc/services/settings/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Settings

The settings subsystem gives modules a way to store persistent per-device
configuration and runtime state. A variety of storage implementations are
provided behind a common API using FCB, NVS, or a file system. These different
implementations give the application developer flexibility to select an
appropriate storage medium, and even change it later as needs change. This
provided behind a common API using FCB, NVS, ZMS or a file system. These
different implementations give the application developer flexibility to select
an appropriate storage medium, and even change it later as needs change. This
subsystem is used by various Zephyr components and can be used simultaneously by
user applications.

Expand All @@ -23,8 +23,8 @@ For an example of the settings subsystem refer to :zephyr:code-sample:`settings`

.. note::

As of Zephyr release 2.1 the recommended backend for non-filesystem
storage is :ref:`NVS <nvs_api>`.
As of Zephyr release 4.1 the recommended backends for non-filesystem
storage are :ref:`NVS <nvs_api>` and :ref:`ZMS <zms_api>`.

Handlers
********
Expand All @@ -39,7 +39,7 @@ for static handlers.
:c:func:`settings_runtime_get()` from the runtime backend.

**h_set**
This gets called when the value is loaded from persisted storage with
This gets called when the value is loaded from persistent storage with
:c:func:`settings_load()`, or when using :c:func:`settings_runtime_set()` from the
runtime backend.

Expand Down Expand Up @@ -78,6 +78,14 @@ backend.
This gets called when loading values from persistent storage using
:c:func:`settings_load()`.

**csi_load_one**
This gets called when loading only one item from persistent storage using
:c:func:`settings_load_one()`.

**csi_get_val_len**
This gets called when getting a value's length from persistent storage using
:c:func:`settings_get_val_len()`.

**csi_save**
This gets called when saving a single setting to persistent storage using
:c:func:`settings_save_one()`.
Expand All @@ -93,10 +101,12 @@ backend.
Zephyr Storage Backends
***********************

Zephyr has three storage backends: a Flash Circular Buffer
(:kconfig:option:`CONFIG_SETTINGS_FCB`), a file in the filesystem
(:kconfig:option:`CONFIG_SETTINGS_FILE`), or non-volatile storage
(:kconfig:option:`CONFIG_SETTINGS_NVS`).
Zephyr offers the following storage backends:

* Flash Circular Buffer (:kconfig:option:`CONFIG_SETTINGS_FCB`).
* A file in the filesystem (:kconfig:option:`CONFIG_SETTINGS_FILE`).
* Non-Volatile Storage (:kconfig:option:`CONFIG_SETTINGS_NVS`).
* Zephyr Memory Storage (:kconfig:option:`CONFIG_SETTINGS_ZMS`).

You can declare multiple sources for settings; settings from
all of these are restored when :c:func:`settings_load()` is called.
Expand All @@ -109,30 +119,49 @@ using :c:func:`settings_fcb_dst()`. As a side-effect, :c:func:`settings_fcb_src
initializes the FCB area, so it must be called before calling
:c:func:`settings_fcb_dst()`. File read target is registered using
:c:func:`settings_file_src()`, and write target by using :c:func:`settings_file_dst()`.

Non-volatile storage read target is registered using
:c:func:`settings_nvs_src()`, and write target by using
:c:func:`settings_nvs_dst()`.

Zephyr Memory Storage (ZMS) read target is registered using :c:func:`settings_zms_src()`,
and write target is registered using :c:func:`settings_zms_dst()`.

ZMS backend has the particularity of using hash functions to hash the settings
key before storing it to the persistent storage. This implementation implies
that some collisions between key's hashes could occur if a big number of
different keys are stored. This number depends on the selected hash function.

ZMS backend can handle :math:`2^n` maximum collisions where n is defined by
(:kconfig:option:`SETTINGS_ZMS_MAX_COLLISIONS_BITS`).


Storage Location
****************

The FCB and non-volatile storage (NVS) backends both look for a fixed
The FCB, non-volatile storage (NVS) and ZMS backends look for a fixed
partition with label "storage" by default. A different partition can be
selected by setting the ``zephyr,settings-partition`` property of the
chosen node in the devicetree.

The file path used by the file backend to store settings is selected via the
option :kconfig:option:`CONFIG_SETTINGS_FILE_PATH`.

Loading data from persisted storage
***********************************
Loading data from persistent storage
************************************

A call to :c:func:`settings_load()` uses an ``h_set`` implementation
to load settings data from storage to volatile memory.
After all data is loaded, the ``h_commit`` handler is issued,
signalling the application that the settings were successfully
retrieved.

Alternatively, a call to :c:func:`settings_load_one()` will load only one
Settings entry and store it in the provided buffer.

To get the value's length associated with the Settings entry, a call to
:c:func:`settings_get_val_len()` should be performed

Technically FCB and file backends may store some history of the entities.
This means that the newest data entity is stored after any
older existing data entities.
Expand All @@ -146,7 +175,7 @@ A call to :c:func:`settings_save_one()` uses a backend implementation to store
settings data to the storage medium. A call to :c:func:`settings_save()` uses an
``h_export`` implementation to store different data in one operation using
:c:func:`settings_save_one()`.
A key need to be covered by a ``h_export`` only if it is supposed to be stored
A key needs to be covered by a ``h_export`` only if it is supposed to be stored
by :c:func:`settings_save()` call.

For both FCB and file back-end only storage requests with data which
Expand Down Expand Up @@ -227,7 +256,7 @@ Example: Persist Runtime State

This is a simple example showing how to persist runtime state. In this example,
only ``h_set`` is defined, which is used when restoring value from
persisted storage.
persistent storage.

In this example, the ``main`` function increments ``foo_val``, and then
persists the latest number. When the system restarts, the application calls
Expand Down
Loading