Skip to content

Commit 6644c38

Browse files
committed
zms: optimize write function by skipping unnecessary reads
when performing a write ZMS checks if the data exists in the storage to avoid double writing the same data and save some memory cycle life time. However this downgrades the write performance. Enable this feature only when CONFIG_ZMS_NO_DOUBLE_WRITE is enabled. Signed-off-by: Riadh Ghaddab <[email protected]> (cherry picked from commit e0f0256)
1 parent 22c029d commit 6644c38

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

subsys/fs/zms/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ config ZMS_CUSTOM_BLOCK_SIZE
5050
help
5151
Changes the internal buffer size of ZMS
5252

53+
config ZMS_NO_DOUBLE_WRITE
54+
bool "Avoid writing the same data again in the storage"
55+
help
56+
For some memory technologies, write cycles for memory cells are limited and any
57+
unncessary writes should be avoided.
58+
Enable this config to avoid rewriting data in the storage if it already exists.
59+
This option will reduce write performance as it will need to do a research of the
60+
data in the whole storage before any write.
61+
5362
module = ZMS
5463
module-str = zms
5564
source "subsys/logging/Kconfig.template.log_config"

subsys/fs/zms/zms.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,12 +1392,10 @@ ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len)
13921392
{
13931393
int rc;
13941394
size_t data_size;
1395-
struct zms_ate wlk_ate;
13961395
uint64_t wlk_addr;
13971396
uint64_t rd_addr;
13981397
uint32_t gc_count;
13991398
uint32_t required_space = 0U; /* no space, appropriate for delete ate */
1400-
int prev_found = 0;
14011399

14021400
if (!fs->ready) {
14031401
LOG_ERR("zms not initialized");
@@ -1428,15 +1426,14 @@ ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len)
14281426
#endif
14291427
rd_addr = wlk_addr;
14301428

1429+
#ifdef CONFIG_ZMS_NO_DOUBLE_WRITE
14311430
/* Search for a previous valid ATE with the same ID */
1432-
prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, &rd_addr);
1431+
struct zms_ate wlk_ate;
1432+
int prev_found = zms_find_ate_with_id(fs, id, wlk_addr, fs->ate_wra, &wlk_ate, &rd_addr);
14331433
if (prev_found < 0) {
14341434
return prev_found;
14351435
}
14361436

1437-
#ifdef CONFIG_ZMS_LOOKUP_CACHE
1438-
no_cached_entry:
1439-
#endif
14401437
if (prev_found) {
14411438
/* previous entry found */
14421439
if (len > ZMS_DATA_IN_ATE_SIZE) {
@@ -1473,7 +1470,11 @@ ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len)
14731470
return 0;
14741471
}
14751472
}
1473+
#endif
14761474

1475+
#ifdef CONFIG_ZMS_LOOKUP_CACHE
1476+
no_cached_entry:
1477+
#endif
14771478
/* calculate required space if the entry contains data */
14781479
if (data_size) {
14791480
/* Leave space for delete ate */

tests/subsys/fs/zms/src/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ ZTEST_F(zms, test_delete)
545545
ate_wra = fixture->fs.ate_wra;
546546
data_wra = fixture->fs.data_wra;
547547

548+
#ifdef CONFIG_ZMS_NO_DOUBLE_WRITE
548549
/* delete already deleted entry */
549550
err = zms_delete(&fixture->fs, 1);
550551
zassert_true(err == 0, "zms_delete call failure: %d", err);
@@ -558,6 +559,7 @@ ZTEST_F(zms, test_delete)
558559
zassert_true(ate_wra == fixture->fs.ate_wra && data_wra == fixture->fs.data_wra,
559560
"delete nonexistent entry should not make"
560561
" any footprint in the storage");
562+
#endif
561563
}
562564

563565
/*

0 commit comments

Comments
 (0)