Skip to content

Commit 2db6ab2

Browse files
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
1 parent f96dd7e commit 2db6ab2

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

lfs.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,16 @@ static inline void lfs_cache_zero(lfs_t *lfs, lfs_cache_t *pcache) {
9999

100100
#define lfs_bd_read(...) lfs_bd_read_raw(__VA_ARGS__, __LINE__)
101101
static int lfs_bd_read_raw(lfs_t *lfs,
102-
const lfs_cache_t *pcache, lfs_cache_t *rcache, lfs_size_t hint,
102+
const lfs_cache_t *pcache, lfs_cache_t *rcache, lfs_ssize_t shint,
103103
lfs_block_t block, lfs_off_t off,
104104
void *buffer, lfs_size_t size, int line) {
105105
uint8_t *data = buffer;
106106
(void)line;
107+
lfs_size_t hint = shint;
108+
int reverse = shint < 0;
109+
if (reverse) {
110+
hint = -hint;
111+
}
107112
if (off+size > lfs->cfg->block_size
108113
|| (lfs->block_count && block >= lfs->block_count)) {
109114
return LFS_ERR_CORRUPT;
@@ -168,10 +173,20 @@ static int lfs_bd_read_raw(lfs_t *lfs,
168173
// load to cache, first condition can no longer fail
169174
LFS_ASSERT(!lfs->block_count || block < lfs->block_count);
170175
rcache->block = block;
171-
rcache->off = lfs_aligndown(off, lfs->cfg->read_size);
176+
lfs_off_t moff = off;
177+
if (reverse) {
178+
lfs_size_t msize;
179+
moff = off + size;
180+
msize = lfs_min(hint, lfs->cfg->cache_size);
181+
if (moff > msize)
182+
moff = lfs_alignup(moff - msize, lfs->cfg->read_size);
183+
else
184+
moff = 0;
185+
}
186+
rcache->off = lfs_aligndown(moff, lfs->cfg->read_size);
172187
rcache->size = lfs_min(
173188
lfs_min(
174-
lfs_alignup(off+hint, lfs->cfg->read_size),
189+
lfs_alignup(moff+hint, lfs->cfg->read_size),
175190
lfs->cfg->block_size)
176191
- rcache->off,
177192
lfs->cfg->cache_size);
@@ -787,7 +802,7 @@ static lfs_stag_t lfs_dir_getslice(lfs_t *lfs, const lfs_mdir_t *dir,
787802
off -= lfs_tag_dsize(ntag);
788803
lfs_tag_t tag = ntag;
789804
int err = lfs_bd_read(lfs,
790-
NULL, &lfs->rcache, sizeof(ntag),
805+
NULL, &lfs->rcache, -lfs->cfg->block_size,
791806
dir->pair[0], off, &ntag, sizeof(ntag));
792807
if (err) {
793808
return err;

0 commit comments

Comments
 (0)