littlefs simulator
This page contains a fully functional simulation of the
little filesystem.
littlefs is a small filesystem designed for resource-constrained
microcontrollers, which are usually paired with small flash chips with
limited erase-cycles. This simulation renders the wear on a simulated
flash chip to demonstrate the dynamic wear-leveling algorithm used in
littlefs. As flash blocks wear out, the littlefs is able to move files
onto unused blocks to extend the lifetime of the storage.
This simulation is built using
emscripten,
a backend for the LLVM that can compile C to javscript. The result is a
fully functional javascript API for littlefs. The charts are provided by
Google Charts
and editor provided by the
Ace code editor.
The full implementation can be found
here,
on GitHub where it is hosted.
Operations on initialization:
// format and mount
var err = lfs.format()
if (err) {
return err
}
var err = lfs.mount()
if (err) {
return err
}
// create static files
for (var i = 0; i < STATIC_FILE_COUNT; i++) {
var f = lfs.open('static' + i, ['wronly', 'creat'])
if (f < 0) {
return err
}
for (var j = 0; j < STATIC_FILE_SIZE; j += 16) {
var err = f.write('0123456789abcdef')
if (err < 0) {
return err
}
}
var err = f.close()
if (err) {
return err
}
}
// unmount
var err = lfs.unmount()
if (err) {
return err
}
return 0
Operations on boot:
// mount
var err = lfs.mount()
if (err) {
return err
}
// rebuild dynamic files
for (var i = 0; i < DYNAMIC_FILE_COUNT; i++) {
var f = lfs.open('dynamic' + i, ['wronly', 'creat'])
if (f < 0) {
return err;
}
for (var j = 0; j < DYNAMIC_FILE_SIZE; j += 16) {
var err = f.write('0123456789abcdef')
if (err < 0) {
return err
}
}
var err = f.close()
if (err < 0) {
return err
}
}
// unmount
var err = lfs.unmount()
if (err) {
return err
}
return 0