Skip to content

Commit

Permalink
lfs_dir_getslice: reverse caching
Browse files Browse the repository at this point in the history
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
  • Loading branch information
matthieu-c-tagheuer committed May 2, 2024
1 parent f96dd7e commit 2db6ab2
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 2db6ab2

Please sign in to comment.