From 2db6ab2d024396b29420ebdb04bf0d8856013701 Mon Sep 17 00:00:00 2001 From: Matthieu Castet Date: Tue, 30 Apr 2024 13:18:50 +0200 Subject: [PATCH] lfs_dir_getslice: reverse caching cache trace show possible caching in lfs_dir_getslice cache recache for block 83 number 23 line=790 cache recache for block 881 number 23 line=790 cache recache for block 929 number 23 line=790 cache recache for block 977 number 23 line=790 cache recache for block 128 number 24 line=790 For the same test than previous patch before patch top read operation (number of flash read size, number of flash op) 15044, 41 15044, 41 15044, 41 15044, 41 16068, 42 total read/write total read=18809040 B write=4287328 B total num read=49477 op write=7732 op after patch top read operation (number of flash read size, number of flash op) 15044, 41 15044, 41 15044, 41 15044, 41 16068, 42 total read/write total read=18852728 B write=4287328 B total num read=45678 op write=7732 op cache recache for block 882 number 2 line=998 cache recache for block 911 number 2 line=998 cache recache for block 930 number 2 line=998 cache recache for block 959 number 2 line=998 cache recache for block 978 number 2 line=998 --- lfs.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lfs.c b/lfs.c index e51a7606..7a01266a 100644 --- a/lfs.c +++ b/lfs.c @@ -99,11 +99,16 @@ static inline void lfs_cache_zero(lfs_t *lfs, lfs_cache_t *pcache) { #define lfs_bd_read(...) lfs_bd_read_raw(__VA_ARGS__, __LINE__) static int lfs_bd_read_raw(lfs_t *lfs, - const lfs_cache_t *pcache, lfs_cache_t *rcache, lfs_size_t hint, + const lfs_cache_t *pcache, lfs_cache_t *rcache, lfs_ssize_t shint, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size, int line) { uint8_t *data = buffer; (void)line; + lfs_size_t hint = shint; + int reverse = shint < 0; + if (reverse) { + hint = -hint; + } if (off+size > lfs->cfg->block_size || (lfs->block_count && block >= lfs->block_count)) { return LFS_ERR_CORRUPT; @@ -168,10 +173,20 @@ static int lfs_bd_read_raw(lfs_t *lfs, // load to cache, first condition can no longer fail LFS_ASSERT(!lfs->block_count || block < lfs->block_count); rcache->block = block; - rcache->off = lfs_aligndown(off, lfs->cfg->read_size); + lfs_off_t moff = off; + if (reverse) { + lfs_size_t msize; + moff = off + size; + msize = lfs_min(hint, lfs->cfg->cache_size); + if (moff > msize) + moff = lfs_alignup(moff - msize, lfs->cfg->read_size); + else + moff = 0; + } + rcache->off = lfs_aligndown(moff, lfs->cfg->read_size); rcache->size = lfs_min( lfs_min( - lfs_alignup(off+hint, lfs->cfg->read_size), + lfs_alignup(moff+hint, lfs->cfg->read_size), lfs->cfg->block_size) - rcache->off, lfs->cfg->cache_size); @@ -787,7 +802,7 @@ static lfs_stag_t lfs_dir_getslice(lfs_t *lfs, const lfs_mdir_t *dir, off -= lfs_tag_dsize(ntag); lfs_tag_t tag = ntag; int err = lfs_bd_read(lfs, - NULL, &lfs->rcache, sizeof(ntag), + NULL, &lfs->rcache, -lfs->cfg->block_size, dir->pair[0], off, &ntag, sizeof(ntag)); if (err) { return err;