Skip to content

Commit 09972a1

Browse files
authored
Merge pull request #913 from littlefs-project/gc-compactions
Extend lfs_fs_gc to compact metadata, compact_thresh
2 parents ed7bd05 + b5cd957 commit 09972a1

File tree

7 files changed

+139
-48
lines changed

7 files changed

+139
-48
lines changed

lfs.c

Lines changed: 83 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -593,19 +593,6 @@ static int lfs_rawunmount(lfs_t *lfs);
593593

594594

595595
/// Block allocator ///
596-
#ifndef LFS_READONLY
597-
static int lfs_alloc_lookahead(void *p, lfs_block_t block) {
598-
lfs_t *lfs = (lfs_t*)p;
599-
lfs_block_t off = ((block - lfs->lookahead.start)
600-
+ lfs->block_count) % lfs->block_count;
601-
602-
if (off < lfs->lookahead.size) {
603-
lfs->lookahead.buffer[off / 8] |= 1U << (off % 8);
604-
}
605-
606-
return 0;
607-
}
608-
#endif
609596

610597
// allocations should call this when all allocated blocks are committed to
611598
// the filesystem
@@ -624,7 +611,21 @@ static void lfs_alloc_drop(lfs_t *lfs) {
624611
}
625612

626613
#ifndef LFS_READONLY
627-
static int lfs_fs_rawgc(lfs_t *lfs) {
614+
static int lfs_alloc_lookahead(void *p, lfs_block_t block) {
615+
lfs_t *lfs = (lfs_t*)p;
616+
lfs_block_t off = ((block - lfs->lookahead.start)
617+
+ lfs->block_count) % lfs->block_count;
618+
619+
if (off < lfs->lookahead.size) {
620+
lfs->lookahead.buffer[off / 8] |= 1U << (off % 8);
621+
}
622+
623+
return 0;
624+
}
625+
#endif
626+
627+
#ifndef LFS_READONLY
628+
static int lfs_alloc_scan(lfs_t *lfs) {
628629
// move lookahead buffer to the first unused block
629630
//
630631
// note we limit the lookahead buffer to at most the amount of blocks
@@ -693,7 +694,7 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
693694

694695
// No blocks in our lookahead buffer, we need to scan the filesystem for
695696
// unused blocks in the next lookahead window.
696-
int err = lfs_fs_rawgc(lfs);
697+
int err = lfs_alloc_scan(lfs);
697698
if(err) {
698699
return err;
699700
}
@@ -4189,6 +4190,14 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
41894190
// wear-leveling.
41904191
LFS_ASSERT(lfs->cfg->block_cycles != 0);
41914192

4193+
// check that compact_thresh makes sense
4194+
//
4195+
// metadata can't be compacted below block_size/2, and metadata can't
4196+
// exceed a block_size
4197+
LFS_ASSERT(lfs->cfg->compact_thresh == 0
4198+
|| lfs->cfg->compact_thresh >= lfs->cfg->block_size/2);
4199+
LFS_ASSERT(lfs->cfg->compact_thresh == (lfs_size_t)-1
4200+
|| lfs->cfg->compact_thresh <= lfs->cfg->block_size);
41924201

41934202
// setup read cache
41944203
if (lfs->cfg->read_buffer) {
@@ -5080,6 +5089,57 @@ static lfs_ssize_t lfs_fs_rawsize(lfs_t *lfs) {
50805089
return size;
50815090
}
50825091

5092+
// explicit garbage collection
5093+
#ifndef LFS_READONLY
5094+
static int lfs_fs_rawgc(lfs_t *lfs) {
5095+
// force consistency, even if we're not necessarily going to write,
5096+
// because this function is supposed to take care of janitorial work
5097+
// isn't it?
5098+
int err = lfs_fs_forceconsistency(lfs);
5099+
if (err) {
5100+
return err;
5101+
}
5102+
5103+
// try to compact metadata pairs, note we can't really accomplish
5104+
// anything if compact_thresh doesn't at least leave a prog_size
5105+
// available
5106+
if (lfs->cfg->compact_thresh
5107+
< lfs->cfg->block_size - lfs->cfg->prog_size) {
5108+
// iterate over all mdirs
5109+
lfs_mdir_t mdir = {.tail = {0, 1}};
5110+
while (!lfs_pair_isnull(mdir.tail)) {
5111+
err = lfs_dir_fetch(lfs, &mdir, mdir.tail);
5112+
if (err) {
5113+
return err;
5114+
}
5115+
5116+
// not erased? exceeds our compaction threshold?
5117+
if (!mdir.erased || ((lfs->cfg->compact_thresh == 0)
5118+
? mdir.off > lfs->cfg->block_size - lfs->cfg->block_size/8
5119+
: mdir.off > lfs->cfg->compact_thresh)) {
5120+
// the easiest way to trigger a compaction is to mark
5121+
// the mdir as unerased and add an empty commit
5122+
mdir.erased = false;
5123+
err = lfs_dir_commit(lfs, &mdir, NULL, 0);
5124+
if (err) {
5125+
return err;
5126+
}
5127+
}
5128+
}
5129+
}
5130+
5131+
// try to populate the lookahead buffer, unless it's already full
5132+
if (lfs->lookahead.size < 8*lfs->cfg->lookahead_size) {
5133+
err = lfs_alloc_scan(lfs);
5134+
if (err) {
5135+
return err;
5136+
}
5137+
}
5138+
5139+
return 0;
5140+
}
5141+
#endif
5142+
50835143
#ifndef LFS_READONLY
50845144
static int lfs_fs_rawgrow(lfs_t *lfs, lfs_size_t block_count) {
50855145
// shrinking is not supported
@@ -6286,32 +6346,32 @@ int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void *, lfs_block_t), void *data) {
62866346
}
62876347

62886348
#ifndef LFS_READONLY
6289-
int lfs_fs_gc(lfs_t *lfs) {
6349+
int lfs_fs_mkconsistent(lfs_t *lfs) {
62906350
int err = LFS_LOCK(lfs->cfg);
62916351
if (err) {
62926352
return err;
62936353
}
6294-
LFS_TRACE("lfs_fs_gc(%p)", (void*)lfs);
6354+
LFS_TRACE("lfs_fs_mkconsistent(%p)", (void*)lfs);
62956355

6296-
err = lfs_fs_rawgc(lfs);
6356+
err = lfs_fs_rawmkconsistent(lfs);
62976357

6298-
LFS_TRACE("lfs_fs_gc -> %d", err);
6358+
LFS_TRACE("lfs_fs_mkconsistent -> %d", err);
62996359
LFS_UNLOCK(lfs->cfg);
63006360
return err;
63016361
}
63026362
#endif
63036363

63046364
#ifndef LFS_READONLY
6305-
int lfs_fs_mkconsistent(lfs_t *lfs) {
6365+
int lfs_fs_gc(lfs_t *lfs) {
63066366
int err = LFS_LOCK(lfs->cfg);
63076367
if (err) {
63086368
return err;
63096369
}
6310-
LFS_TRACE("lfs_fs_mkconsistent(%p)", (void*)lfs);
6370+
LFS_TRACE("lfs_fs_gc(%p)", (void*)lfs);
63116371

6312-
err = lfs_fs_rawmkconsistent(lfs);
6372+
err = lfs_fs_rawgc(lfs);
63136373

6314-
LFS_TRACE("lfs_fs_mkconsistent -> %d", err);
6374+
LFS_TRACE("lfs_fs_gc -> %d", err);
63156375
LFS_UNLOCK(lfs->cfg);
63166376
return err;
63176377
}

lfs.h

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,17 @@ struct lfs_config {
227227
// can track 8 blocks.
228228
lfs_size_t lookahead_size;
229229

230+
// Threshold for metadata compaction during lfs_fs_gc in bytes. Metadata
231+
// pairs that exceed this threshold will be compacted during lfs_fs_gc.
232+
// Defaults to ~88% block_size when zero, though the default may change
233+
// in the future.
234+
//
235+
// Note this only affects lfs_fs_gc. Normal compactions still only occur
236+
// when full.
237+
//
238+
// Set to -1 to disable metadata compaction during lfs_fs_gc.
239+
lfs_size_t compact_thresh;
240+
230241
// Optional statically allocated read buffer. Must be cache_size.
231242
// By default lfs_malloc is used to allocate this buffer.
232243
void *read_buffer;
@@ -709,18 +720,6 @@ lfs_ssize_t lfs_fs_size(lfs_t *lfs);
709720
// Returns a negative error code on failure.
710721
int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
711722

712-
// Attempt to proactively find free blocks
713-
//
714-
// Calling this function is not required, but may allowing the offloading of
715-
// the expensive block allocation scan to a less time-critical code path.
716-
//
717-
// Note: littlefs currently does not persist any found free blocks to disk.
718-
// This may change in the future.
719-
//
720-
// Returns a negative error code on failure. Finding no free blocks is
721-
// not an error.
722-
int lfs_fs_gc(lfs_t *lfs);
723-
724723
#ifndef LFS_READONLY
725724
// Attempt to make the filesystem consistent and ready for writing
726725
//
@@ -733,6 +732,24 @@ int lfs_fs_gc(lfs_t *lfs);
733732
int lfs_fs_mkconsistent(lfs_t *lfs);
734733
#endif
735734

735+
#ifndef LFS_READONLY
736+
// Attempt any janitorial work
737+
//
738+
// This currently:
739+
// 1. Calls mkconsistent if not already consistent
740+
// 2. Compacts metadata > compact_thresh
741+
// 3. Populates the block allocator
742+
//
743+
// Though additional janitorial work may be added in the future.
744+
//
745+
// Calling this function is not required, but may allow the offloading of
746+
// expensive janitorial work to a less time-critical code path.
747+
//
748+
// Returns a negative error code on failure. Accomplishing nothing is not
749+
// an error.
750+
int lfs_fs_gc(lfs_t *lfs);
751+
#endif
752+
736753
#ifndef LFS_READONLY
737754
// Grows the filesystem to a new size, updating the superblock with the new
738755
// block count.

runners/bench_runner.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,7 @@ void perm_run(
13211321
.block_cycles = BLOCK_CYCLES,
13221322
.cache_size = CACHE_SIZE,
13231323
.lookahead_size = LOOKAHEAD_SIZE,
1324+
.compact_thresh = COMPACT_THRESH,
13241325
};
13251326

13261327
struct lfs_emubd_config bdcfg = {

runners/bench_runner.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,12 @@ intmax_t bench_define(size_t define);
9595
#define BLOCK_COUNT_i 5
9696
#define CACHE_SIZE_i 6
9797
#define LOOKAHEAD_SIZE_i 7
98-
#define BLOCK_CYCLES_i 8
99-
#define ERASE_VALUE_i 9
100-
#define ERASE_CYCLES_i 10
101-
#define BADBLOCK_BEHAVIOR_i 11
102-
#define POWERLOSS_BEHAVIOR_i 12
98+
#define COMPACT_THRESH_i 8
99+
#define BLOCK_CYCLES_i 9
100+
#define ERASE_VALUE_i 10
101+
#define ERASE_CYCLES_i 11
102+
#define BADBLOCK_BEHAVIOR_i 12
103+
#define POWERLOSS_BEHAVIOR_i 13
103104

104105
#define READ_SIZE bench_define(READ_SIZE_i)
105106
#define PROG_SIZE bench_define(PROG_SIZE_i)
@@ -109,6 +110,7 @@ intmax_t bench_define(size_t define);
109110
#define BLOCK_COUNT bench_define(BLOCK_COUNT_i)
110111
#define CACHE_SIZE bench_define(CACHE_SIZE_i)
111112
#define LOOKAHEAD_SIZE bench_define(LOOKAHEAD_SIZE_i)
113+
#define COMPACT_THRESH bench_define(COMPACT_THRESH_i)
112114
#define BLOCK_CYCLES bench_define(BLOCK_CYCLES_i)
113115
#define ERASE_VALUE bench_define(ERASE_VALUE_i)
114116
#define ERASE_CYCLES bench_define(ERASE_CYCLES_i)
@@ -124,14 +126,15 @@ intmax_t bench_define(size_t define);
124126
BENCH_DEF(BLOCK_COUNT, ERASE_COUNT/lfs_max(BLOCK_SIZE/ERASE_SIZE,1))\
125127
BENCH_DEF(CACHE_SIZE, lfs_max(64,lfs_max(READ_SIZE,PROG_SIZE))) \
126128
BENCH_DEF(LOOKAHEAD_SIZE, 16) \
129+
BENCH_DEF(COMPACT_THRESH, 0) \
127130
BENCH_DEF(BLOCK_CYCLES, -1) \
128131
BENCH_DEF(ERASE_VALUE, 0xff) \
129132
BENCH_DEF(ERASE_CYCLES, 0) \
130133
BENCH_DEF(BADBLOCK_BEHAVIOR, LFS_EMUBD_BADBLOCK_PROGERROR) \
131134
BENCH_DEF(POWERLOSS_BEHAVIOR, LFS_EMUBD_POWERLOSS_NOOP)
132135

133136
#define BENCH_GEOMETRY_DEFINE_COUNT 4
134-
#define BENCH_IMPLICIT_DEFINE_COUNT 13
137+
#define BENCH_IMPLICIT_DEFINE_COUNT 14
135138

136139

137140
#endif

runners/test_runner.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,7 @@ static void run_powerloss_none(
13461346
.block_cycles = BLOCK_CYCLES,
13471347
.cache_size = CACHE_SIZE,
13481348
.lookahead_size = LOOKAHEAD_SIZE,
1349+
.compact_thresh = COMPACT_THRESH,
13491350
#ifdef LFS_MULTIVERSION
13501351
.disk_version = DISK_VERSION,
13511352
#endif
@@ -1422,6 +1423,7 @@ static void run_powerloss_linear(
14221423
.block_cycles = BLOCK_CYCLES,
14231424
.cache_size = CACHE_SIZE,
14241425
.lookahead_size = LOOKAHEAD_SIZE,
1426+
.compact_thresh = COMPACT_THRESH,
14251427
#ifdef LFS_MULTIVERSION
14261428
.disk_version = DISK_VERSION,
14271429
#endif
@@ -1515,6 +1517,7 @@ static void run_powerloss_log(
15151517
.block_cycles = BLOCK_CYCLES,
15161518
.cache_size = CACHE_SIZE,
15171519
.lookahead_size = LOOKAHEAD_SIZE,
1520+
.compact_thresh = COMPACT_THRESH,
15181521
#ifdef LFS_MULTIVERSION
15191522
.disk_version = DISK_VERSION,
15201523
#endif
@@ -1606,6 +1609,7 @@ static void run_powerloss_cycles(
16061609
.block_cycles = BLOCK_CYCLES,
16071610
.cache_size = CACHE_SIZE,
16081611
.lookahead_size = LOOKAHEAD_SIZE,
1612+
.compact_thresh = COMPACT_THRESH,
16091613
#ifdef LFS_MULTIVERSION
16101614
.disk_version = DISK_VERSION,
16111615
#endif
@@ -1795,6 +1799,7 @@ static void run_powerloss_exhaustive(
17951799
.block_cycles = BLOCK_CYCLES,
17961800
.cache_size = CACHE_SIZE,
17971801
.lookahead_size = LOOKAHEAD_SIZE,
1802+
.compact_thresh = COMPACT_THRESH,
17981803
#ifdef LFS_MULTIVERSION
17991804
.disk_version = DISK_VERSION,
18001805
#endif

runners/test_runner.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,13 @@ intmax_t test_define(size_t define);
8888
#define BLOCK_COUNT_i 5
8989
#define CACHE_SIZE_i 6
9090
#define LOOKAHEAD_SIZE_i 7
91-
#define BLOCK_CYCLES_i 8
92-
#define ERASE_VALUE_i 9
93-
#define ERASE_CYCLES_i 10
94-
#define BADBLOCK_BEHAVIOR_i 11
95-
#define POWERLOSS_BEHAVIOR_i 12
96-
#define DISK_VERSION_i 13
91+
#define COMPACT_THRESH_i 8
92+
#define BLOCK_CYCLES_i 9
93+
#define ERASE_VALUE_i 10
94+
#define ERASE_CYCLES_i 11
95+
#define BADBLOCK_BEHAVIOR_i 12
96+
#define POWERLOSS_BEHAVIOR_i 13
97+
#define DISK_VERSION_i 14
9798

9899
#define READ_SIZE TEST_DEFINE(READ_SIZE_i)
99100
#define PROG_SIZE TEST_DEFINE(PROG_SIZE_i)
@@ -103,6 +104,7 @@ intmax_t test_define(size_t define);
103104
#define BLOCK_COUNT TEST_DEFINE(BLOCK_COUNT_i)
104105
#define CACHE_SIZE TEST_DEFINE(CACHE_SIZE_i)
105106
#define LOOKAHEAD_SIZE TEST_DEFINE(LOOKAHEAD_SIZE_i)
107+
#define COMPACT_THRESH TEST_DEFINE(COMPACT_THRESH_i)
106108
#define BLOCK_CYCLES TEST_DEFINE(BLOCK_CYCLES_i)
107109
#define ERASE_VALUE TEST_DEFINE(ERASE_VALUE_i)
108110
#define ERASE_CYCLES TEST_DEFINE(ERASE_CYCLES_i)
@@ -119,6 +121,7 @@ intmax_t test_define(size_t define);
119121
TEST_DEF(BLOCK_COUNT, ERASE_COUNT/lfs_max(BLOCK_SIZE/ERASE_SIZE,1)) \
120122
TEST_DEF(CACHE_SIZE, lfs_max(64,lfs_max(READ_SIZE,PROG_SIZE))) \
121123
TEST_DEF(LOOKAHEAD_SIZE, 16) \
124+
TEST_DEF(COMPACT_THRESH, 0) \
122125
TEST_DEF(BLOCK_CYCLES, -1) \
123126
TEST_DEF(ERASE_VALUE, 0xff) \
124127
TEST_DEF(ERASE_CYCLES, 0) \
@@ -127,7 +130,7 @@ intmax_t test_define(size_t define);
127130
TEST_DEF(DISK_VERSION, 0)
128131

129132
#define TEST_GEOMETRY_DEFINE_COUNT 4
130-
#define TEST_IMPLICIT_DEFINE_COUNT 14
133+
#define TEST_IMPLICIT_DEFINE_COUNT 15
131134

132135

133136
#endif

tests/test_alloc.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ if = 'BLOCK_CYCLES == -1'
77
defines.FILES = 3
88
defines.SIZE = '(((BLOCK_SIZE-8)*(BLOCK_COUNT-6)) / FILES)'
99
defines.GC = [false, true]
10+
defines.COMPACT_THRESH = ['-1', '0', 'BLOCK_SIZE/2']
1011
code = '''
1112
const char *names[] = {"bacon", "eggs", "pancakes"};
1213
lfs_file_t files[FILES];
@@ -60,6 +61,7 @@ code = '''
6061
defines.FILES = 3
6162
defines.SIZE = '(((BLOCK_SIZE-8)*(BLOCK_COUNT-6)) / FILES)'
6263
defines.GC = [false, true]
64+
defines.COMPACT_THRESH = ['-1', '0', 'BLOCK_SIZE/2']
6365
code = '''
6466
const char *names[] = {"bacon", "eggs", "pancakes"};
6567

0 commit comments

Comments
 (0)