runners: emubd/kiwibd: Adopted emulated simtime API

This is based on some work in external benchmarks. What's worked well
there is emulating a global simtime based on per-byte estimates.

This moves the emulated simtime into emubd/kiwibd, and extends the idea
with both per-byte and per-op timing estimates for hopefully more
realistic results.

---

The problem is how NAND flash reads work.

Per-byte timing estimates are surprisingly accurate for NOR flash. There
is some overhead for sending the address, but it's mostly dominated by
bus cost (~20ns/B [1]).

NAND flash, on the otherhand, technically does support byte-level reads,
but first needs to read into 2KiB buffer. Surprisingly, these are pretty
close in cost (~19ns/B bus [2] vs ~12ns/B buffer [2]).

This close-ness makes modeling NAND flash difficult. If we set
read_size=1, we risk hiding the cost of small reads, which littlefs3 is
full of (rbyd lookups). If we set read_size=2048, we unfairly penalize
littlefs3 for the same reason.

---

The solution here is to expose both per-byte and per-op timing
estimates. This lets you model NAND reads using two data points:

  ^
  |                                realtime --> ...............o
  |                                             :    .....'''' :
  |                              ...............:''''  ^       :
  |                              :....'''''            |       :
  |               ..........::::::                  simtime    :
  |          .....:''''                                        :
  |o....:::::.....:                                            :
  |:                                                           :
  |:                                                           :
  +:-----------------------------------------------------------:>
   min read                                              max read

Where:

  bus_timing = 19ns
  buffer_timing = 25us
  buffer_size = 2KiB
  erase_size = 128KiB

  min_read = buffer_timing
  max_read = (erase_size/buffer_size)*buffer_timing - buffer_timing
  read_timing = min_read
  readed_timing = ((max_read - min_read)/erase_size) + bus_timing

  simtime = reads*read_timing + readed*readed_timing
            (per-op)            (per-byte)

This should correctly penalize small reads without complicating
emubd/kiwibd too much.

That's the idea anyways! It will take some use to understand if this is
a reasonable approach.

As a plus, this is a superset of the per-byte model, so both can be used
for realistic vs idealistic simulations (and to test the bus+buffer
model itself).

1: https://www.winbond.com/resource-files/W25Q256JV%20SPI%20RevQ%2002072025%20Plus.pdf
2: https://www.winbond.com/resource-files/W25N01GV%20Rev%20R%20070323.pdf
This commit is contained in:
Christopher Haster
2026-01-18 23:57:23 -06:00
parent 1b70c1f199
commit d3dd927de3
10 changed files with 548 additions and 278 deletions
+100 -56
View File
@@ -217,8 +217,11 @@ int lfs3_emubd_createcfg(const struct lfs3_cfg *cfg, const char *path,
// setup testing things
bd->blocks = NULL;
bd->reads = 0;
bd->progs = 0;
bd->erases = 0;
bd->readed = 0;
bd->proged = 0;
bd->progged = 0;
bd->erased = 0;
bd->prng = bd->cfg->seed;
bd->power_cycles = bd->cfg->power_cycles;
@@ -434,6 +437,7 @@ int lfs3_emubd_read(const struct lfs3_cfg *cfg, lfs3_block_t block,
}
// track reads
bd->reads += 1;
bd->readed += size;
if (bd->cfg->read_sleep) {
int err = nanosleep(&(struct timespec){
@@ -745,7 +749,8 @@ progged:;
}
// track progs
bd->proged += size;
bd->progs += 1;
bd->progged += size;
if (bd->cfg->prog_sleep) {
int err = nanosleep(&(struct timespec){
.tv_sec=bd->cfg->prog_sleep/1000000000,
@@ -1040,6 +1045,7 @@ int lfs3_emubd_erase(const struct lfs3_cfg *cfg, lfs3_block_t block) {
erased:;
// track erases
bd->erases += 1;
bd->erased += cfg->block_size;
if (bd->cfg->erase_sleep) {
int err = nanosleep(&(struct timespec){
@@ -1076,24 +1082,65 @@ int lfs3_emubd_sync(const struct lfs3_cfg *cfg) {
/// Additional emubd features for testing ///
void lfs3_emubd_seed(const struct lfs3_cfg *cfg, uint32_t seed) {
LFS3_EMUBD_TRACE("lfs3_emubd_seed(%p, 0x%08"PRIx32")",
(void*)cfg, seed);
lfs3_emubd_sns_t lfs3_emubd_simtime(const struct lfs3_cfg *cfg) {
LFS3_EMUBD_TRACE("lfs3_emubd_simtime(%p)", (void*)cfg);
lfs3_emubd_t *bd = cfg->context;
bd->prng = seed;
// error if all possible timings are zero
if (bd->cfg->reads_timing == 0
&& bd->cfg->progs_timing == 0
&& bd->cfg->erases_timing == 0
&& bd->cfg->readed_timing == 0
&& bd->cfg->progged_timing == 0
&& bd->cfg->erased_timing == 0) {
LFS3_EMUBD_TRACE("lfs3_emubd_simtime -> %d", LFS3_ERR_NOTSUP);
return LFS3_ERR_NOTSUP;
}
LFS3_EMUBD_TRACE("lfs3_emubd_seed -> _");
lfs3_emubd_ns_t ns
= (bd->cfg->reads_timing * bd->reads)
+ (bd->cfg->progs_timing * bd->progs)
+ (bd->cfg->erases_timing * bd->erases)
+ (bd->cfg->readed_timing * bd->readed)
+ (bd->cfg->progged_timing * bd->progged)
+ (bd->cfg->erased_timing * bd->erased);
LFS3_EMUBD_TRACE("lfs3_emubd_simtime -> %"PRIu64, ns);
return ns;
}
uint32_t lfs3_emubd_prng(const struct lfs3_cfg *cfg) {
LFS3_EMUBD_TRACE("lfs3_emubd_prng(%p)", (void*)cfg);
int lfs3_emubd_simreset(const struct lfs3_cfg *cfg) {
LFS3_EMUBD_TRACE("lfs3_emubd_simreset(%p)", (void*)cfg);
lfs3_emubd_t *bd = cfg->context;
bd->reads = 0;
bd->progs = 0;
bd->erases = 0;
bd->readed = 0;
bd->progged = 0;
bd->erased = 0;
LFS3_EMUBD_TRACE("lfs3_emubd_simreset -> %d", 0);
return 0;
}
uint32_t x = lfs3_emubd_prng_(&bd->prng);
lfs3_emubd_sio_t lfs3_emubd_reads(const struct lfs3_cfg *cfg) {
LFS3_EMUBD_TRACE("lfs3_emubd_reads(%p)", (void*)cfg);
lfs3_emubd_t *bd = cfg->context;
LFS3_EMUBD_TRACE("lfs3_emubd_reads -> %"PRIu64, bd->reads);
return bd->reads;
}
LFS3_EMUBD_TRACE("lfs3_emubd_prng -> 0x%08"PRIx32, x);
return x;
lfs3_emubd_sio_t lfs3_emubd_progs(const struct lfs3_cfg *cfg) {
LFS3_EMUBD_TRACE("lfs3_emubd_progs(%p)", (void*)cfg);
lfs3_emubd_t *bd = cfg->context;
LFS3_EMUBD_TRACE("lfs3_emubd_progs -> %"PRIu64, bd->progs);
return bd->progs;
}
lfs3_emubd_sio_t lfs3_emubd_erases(const struct lfs3_cfg *cfg) {
LFS3_EMUBD_TRACE("lfs3_emubd_erases(%p)", (void*)cfg);
lfs3_emubd_t *bd = cfg->context;
LFS3_EMUBD_TRACE("lfs3_emubd_erases -> %"PRIu64, bd->erases);
return bd->erases;
}
lfs3_emubd_sio_t lfs3_emubd_readed(const struct lfs3_cfg *cfg) {
@@ -1103,11 +1150,11 @@ lfs3_emubd_sio_t lfs3_emubd_readed(const struct lfs3_cfg *cfg) {
return bd->readed;
}
lfs3_emubd_sio_t lfs3_emubd_proged(const struct lfs3_cfg *cfg) {
LFS3_EMUBD_TRACE("lfs3_emubd_proged(%p)", (void*)cfg);
lfs3_emubd_sio_t lfs3_emubd_progged(const struct lfs3_cfg *cfg) {
LFS3_EMUBD_TRACE("lfs3_emubd_progged(%p)", (void*)cfg);
lfs3_emubd_t *bd = cfg->context;
LFS3_EMUBD_TRACE("lfs3_emubd_proged -> %"PRIu64, bd->proged);
return bd->proged;
LFS3_EMUBD_TRACE("lfs3_emubd_progged -> %"PRIu64, bd->progged);
return bd->progged;
}
lfs3_emubd_sio_t lfs3_emubd_erased(const struct lfs3_cfg *cfg) {
@@ -1117,33 +1164,6 @@ lfs3_emubd_sio_t lfs3_emubd_erased(const struct lfs3_cfg *cfg) {
return bd->erased;
}
int lfs3_emubd_setreaded(const struct lfs3_cfg *cfg,
lfs3_emubd_io_t readed) {
LFS3_EMUBD_TRACE("lfs3_emubd_setreaded(%p, %"PRIu64")", (void*)cfg, readed);
lfs3_emubd_t *bd = cfg->context;
bd->readed = readed;
LFS3_EMUBD_TRACE("lfs3_emubd_setreaded -> %d", 0);
return 0;
}
int lfs3_emubd_setproged(const struct lfs3_cfg *cfg,
lfs3_emubd_io_t proged) {
LFS3_EMUBD_TRACE("lfs3_emubd_setproged(%p, %"PRIu64")", (void*)cfg, proged);
lfs3_emubd_t *bd = cfg->context;
bd->proged = proged;
LFS3_EMUBD_TRACE("lfs3_emubd_setproged -> %d", 0);
return 0;
}
int lfs3_emubd_seterased(const struct lfs3_cfg *cfg,
lfs3_emubd_io_t erased) {
LFS3_EMUBD_TRACE("lfs3_emubd_seterased(%p, %"PRIu64")", (void*)cfg, erased);
lfs3_emubd_t *bd = cfg->context;
bd->erased = erased;
LFS3_EMUBD_TRACE("lfs3_emubd_seterased -> %d", 0);
return 0;
}
lfs3_emubd_swear_t lfs3_emubd_wear(const struct lfs3_cfg *cfg,
lfs3_block_t block) {
LFS3_EMUBD_TRACE("lfs3_emubd_wear(%p, %"PRIu32")", (void*)cfg, block);
@@ -1189,9 +1209,9 @@ int lfs3_emubd_setwear(const struct lfs3_cfg *cfg,
return 0;
}
int lfs3_emubd_markbad(const struct lfs3_cfg *cfg,
int lfs3_emubd_mkbad(const struct lfs3_cfg *cfg,
lfs3_block_t block) {
LFS3_EMUBD_TRACE("lfs3_emubd_markbad(%p, %"PRIu32")",
LFS3_EMUBD_TRACE("lfs3_emubd_mkbad(%p, %"PRIu32")",
(void*)cfg, block);
lfs3_emubd_t *bd = cfg->context;
@@ -1201,7 +1221,7 @@ int lfs3_emubd_markbad(const struct lfs3_cfg *cfg,
// mutate the block
lfs3_emubd_block_t *b = lfs3_emubd_mutblock(cfg, bd->blocks[block]);
if (!b) {
LFS3_EMUBD_TRACE("lfs3_emubd_markbad -> %d", LFS3_ERR_NOMEM);
LFS3_EMUBD_TRACE("lfs3_emubd_mkbad -> %d", LFS3_ERR_NOMEM);
return LFS3_ERR_NOMEM;
}
bd->blocks[block] = b;
@@ -1215,13 +1235,13 @@ int lfs3_emubd_markbad(const struct lfs3_cfg *cfg,
% (cfg->block_size*8);
}
LFS3_EMUBD_TRACE("lfs3_emubd_markbad -> %d", 0);
LFS3_EMUBD_TRACE("lfs3_emubd_mkbad -> %d", 0);
return 0;
}
int lfs3_emubd_markgood(const struct lfs3_cfg *cfg,
int lfs3_emubd_mkgood(const struct lfs3_cfg *cfg,
lfs3_block_t block) {
LFS3_EMUBD_TRACE("lfs3_emubd_markgood(%p, %"PRIu32")",
LFS3_EMUBD_TRACE("lfs3_emubd_mkgood(%p, %"PRIu32")",
(void*)cfg, block);
lfs3_emubd_t *bd = cfg->context;
@@ -1231,7 +1251,7 @@ int lfs3_emubd_markgood(const struct lfs3_cfg *cfg,
// mutate the block
lfs3_emubd_block_t *b = lfs3_emubd_mutblock(cfg, bd->blocks[block]);
if (!b) {
LFS3_EMUBD_TRACE("lfs3_emubd_markgood -> %d", LFS3_ERR_NOMEM);
LFS3_EMUBD_TRACE("lfs3_emubd_mkgood -> %d", LFS3_ERR_NOMEM);
return LFS3_ERR_NOMEM;
}
bd->blocks[block] = b;
@@ -1239,7 +1259,7 @@ int lfs3_emubd_markgood(const struct lfs3_cfg *cfg,
// set the wear
b->wear = 0;
LFS3_EMUBD_TRACE("lfs3_emubd_markgood -> %d", 0);
LFS3_EMUBD_TRACE("lfs3_emubd_mkgood -> %d", 0);
return 0;
}
@@ -1312,9 +1332,9 @@ int lfs3_emubd_randomizebadbit(const struct lfs3_cfg *cfg,
return 0;
}
int lfs3_emubd_markbadbit(const struct lfs3_cfg *cfg,
int lfs3_emubd_mkbadbit(const struct lfs3_cfg *cfg,
lfs3_block_t block, lfs3_size_t bit) {
LFS3_EMUBD_TRACE("lfs3_emubd_markbadbit(%p, %"PRIu32", %"PRIu32")",
LFS3_EMUBD_TRACE("lfs3_emubd_mkbadbit(%p, %"PRIu32", %"PRIu32")",
(void*)cfg, block, bit);
lfs3_emubd_t *bd = cfg->context;
@@ -1324,7 +1344,7 @@ int lfs3_emubd_markbadbit(const struct lfs3_cfg *cfg,
// mutate the block
lfs3_emubd_block_t *b = lfs3_emubd_mutblock(cfg, bd->blocks[block]);
if (!b) {
LFS3_EMUBD_TRACE("lfs3_emubd_markbadbit -> %d", LFS3_ERR_NOMEM);
LFS3_EMUBD_TRACE("lfs3_emubd_mkbadbit -> %d", LFS3_ERR_NOMEM);
return LFS3_ERR_NOMEM;
}
bd->blocks[block] = b;
@@ -1334,7 +1354,7 @@ int lfs3_emubd_markbadbit(const struct lfs3_cfg *cfg,
// set the bad bit and mark as fixed
b->bad_bit = 0x80000000 | bit;
LFS3_EMUBD_TRACE("lfs3_emubd_markbadbit -> %d", 0);
LFS3_EMUBD_TRACE("lfs3_emubd_mkbadbit -> %d", 0);
return 0;
}
@@ -1434,6 +1454,27 @@ int lfs3_emubd_setpowercycles(const struct lfs3_cfg *cfg,
return 0;
}
void lfs3_emubd_seed(const struct lfs3_cfg *cfg, uint32_t seed) {
LFS3_EMUBD_TRACE("lfs3_emubd_seed(%p, 0x%08"PRIx32")",
(void*)cfg, seed);
lfs3_emubd_t *bd = cfg->context;
bd->prng = seed;
LFS3_EMUBD_TRACE("lfs3_emubd_seed -> _");
}
uint32_t lfs3_emubd_prng(const struct lfs3_cfg *cfg) {
LFS3_EMUBD_TRACE("lfs3_emubd_prng(%p)", (void*)cfg);
lfs3_emubd_t *bd = cfg->context;
uint32_t x = lfs3_emubd_prng_(&bd->prng);
LFS3_EMUBD_TRACE("lfs3_emubd_prng -> 0x%08"PRIx32, x);
return x;
}
int lfs3_emubd_cpy(const struct lfs3_cfg *cfg, lfs3_emubd_t *copy) {
LFS3_EMUBD_TRACE("lfs3_emubd_cpy(%p, %p)", (void*)cfg, (void*)copy);
lfs3_emubd_t *bd = cfg->context;
@@ -1472,8 +1513,11 @@ int lfs3_emubd_cpy(const struct lfs3_cfg *cfg, lfs3_emubd_t *copy) {
}
// other state
copy->reads = bd->reads;
copy->progs = bd->progs;
copy->erases = bd->erases;
copy->readed = bd->readed;
copy->proged = bd->proged;
copy->progged = bd->progged;
copy->erased = bd->erased;
copy->prng = bd->prng;
copy->power_cycles = bd->power_cycles;
+67 -35
View File
@@ -63,8 +63,8 @@ typedef uint32_t lfs3_emubd_powercycles_t;
typedef int32_t lfs3_emubd_spowercycles_t;
// Type for delays in nanoseconds
typedef uint64_t lfs3_emubd_sleep_t;
typedef int64_t lfs3_emubd_ssleep_t;
typedef uint64_t lfs3_emubd_ns_t;
typedef int64_t lfs3_emubd_sns_t;
// emubd config, this is required for testing
struct lfs3_emubd_cfg {
@@ -74,6 +74,42 @@ struct lfs3_emubd_cfg {
// does _not_ rely on this!).
int32_t erase_value;
// Simulated read transaction timing in nanoseconds, this is added
// to simtime each read call, ignoring the requested size
lfs3_emubd_ns_t reads_timing;
// Simulated prog transaction timing in nanoseconds, this is added
// to simtime each prog call, ignoring the requested size
lfs3_emubd_ns_t progs_timing;
// Simulated erase transaction timing in nanoseconds, this is added
// to simtime each erase call, ignoring the requested size
lfs3_emubd_ns_t erases_timing;
// Simulated read byte timing in nanoseconds, this is scaled by the
// requested size and added to simtime each read call.
lfs3_emubd_ns_t readed_timing;
// Simulated prog byte timing in nanoseconds, this is scaled by the
// requested size and added to simtime each prog call.
lfs3_emubd_ns_t progged_timing;
// Simulated erase byte timing in nanoseconds, this is scaled by the
// requested size and added to simtime each erase call.
lfs3_emubd_ns_t erased_timing;
// Artificial read transaction delay in nanoseconds, there is no
// purpose for this other than slowing down the simulation.
lfs3_emubd_ns_t read_sleep;
// Artificial prog transaction delay in nanoseconds, there is no
// purpose for this other than slowing down the simulation.
lfs3_emubd_ns_t prog_sleep;
// Artificial erase transaction delay in nanoseconds, there is no
// purpose for this other than slowing down the simulation.
lfs3_emubd_ns_t erase_sleep;
// Number of erase cycles before a block becomes "bad". The exact behavior
// of bad blocks is controlled by badblock_behavior.
uint32_t erase_cycles;
@@ -99,18 +135,6 @@ struct lfs3_emubd_cfg {
// Seed for prng, which may be used for emulating failed progs. This does
// not affect normal operation.
uint32_t seed;
// Artificial delay in nanoseconds, there is no purpose for this other
// than slowing down the simulation.
lfs3_emubd_sleep_t read_sleep;
// Artificial delay in nanoseconds, there is no purpose for this other
// than slowing down the simulation.
lfs3_emubd_sleep_t prog_sleep;
// Artificial delay in nanoseconds, there is no purpose for this other
// than slowing down the simulation.
lfs3_emubd_sleep_t erase_sleep;
};
// A reference counted block
@@ -138,8 +162,11 @@ typedef struct lfs3_emubd {
lfs3_emubd_block_t **blocks;
// some other test state
lfs3_emubd_io_t reads;
lfs3_emubd_io_t progs;
lfs3_emubd_io_t erases;
lfs3_emubd_io_t readed;
lfs3_emubd_io_t proged;
lfs3_emubd_io_t progged;
lfs3_emubd_io_t erased;
uint32_t prng;
lfs3_emubd_powercycles_t power_cycles;
@@ -188,33 +215,32 @@ int lfs3_emubd_sync(const struct lfs3_cfg *cfg);
/// Additional emubd features for testing ///
// Set the current prng state
void lfs3_emubd_seed(const struct lfs3_cfg *cfg, uint32_t seed);
// Get total simulated runtime
lfs3_emubd_sns_t lfs3_emubd_simtime(const struct lfs3_cfg *cfg);
// Get a pseudo-random number from emubd's internal prng
uint32_t lfs3_emubd_prng(const struct lfs3_cfg *cfg);
// Reset simulation counters
//
// You probably shouldn't call this, instead diff before/after simtimes
int lfs3_emubd_simreset(const struct lfs3_cfg *cfg);
// Get total number of read transactions
lfs3_emubd_sio_t lfs3_emubd_reads(const struct lfs3_cfg *cfg);
// Get total number of prog transactions
lfs3_emubd_sio_t lfs3_emubd_progs(const struct lfs3_cfg *cfg);
// Get total number of erase transactions
lfs3_emubd_sio_t lfs3_emubd_erases(const struct lfs3_cfg *cfg);
// Get total amount of bytes read
lfs3_emubd_sio_t lfs3_emubd_readed(const struct lfs3_cfg *cfg);
// Get total amount of bytes programmed
lfs3_emubd_sio_t lfs3_emubd_proged(const struct lfs3_cfg *cfg);
lfs3_emubd_sio_t lfs3_emubd_progged(const struct lfs3_cfg *cfg);
// Get total amount of bytes erased
lfs3_emubd_sio_t lfs3_emubd_erased(const struct lfs3_cfg *cfg);
// Manually set amount of bytes read
int lfs3_emubd_setreaded(const struct lfs3_cfg *cfg,
lfs3_emubd_io_t readed);
// Manually set amount of bytes programmed
int lfs3_emubd_setproged(const struct lfs3_cfg *cfg,
lfs3_emubd_io_t proged);
// Manually set amount of bytes erased
int lfs3_emubd_seterased(const struct lfs3_cfg *cfg,
lfs3_emubd_io_t erased);
// Get simulated wear on a given block
lfs3_emubd_swear_t lfs3_emubd_wear(const struct lfs3_cfg *cfg,
lfs3_block_t block);
@@ -224,10 +250,10 @@ int lfs3_emubd_setwear(const struct lfs3_cfg *cfg,
lfs3_block_t block, lfs3_emubd_wear_t wear);
// Mark a block as bad, this is equivalent to setting wear to maximum
int lfs3_emubd_markbad(const struct lfs3_cfg *cfg, lfs3_block_t block);
int lfs3_emubd_mkbad(const struct lfs3_cfg *cfg, lfs3_block_t block);
// Clear any simulated wear on a given block
int lfs3_emubd_markgood(const struct lfs3_cfg *cfg, lfs3_block_t block);
int lfs3_emubd_mkgood(const struct lfs3_cfg *cfg, lfs3_block_t block);
// Get which bit failed, this changes on erase/power-loss unless manually set
lfs3_ssize_t lfs3_emubd_badbit(const struct lfs3_cfg *cfg,
@@ -242,7 +268,7 @@ int lfs3_emubd_randomizebadbit(const struct lfs3_cfg *cfg,
lfs3_block_t block);
// Mark a block as bad and which bit should fail
int lfs3_emubd_markbadbit(const struct lfs3_cfg *cfg,
int lfs3_emubd_mkbadbit(const struct lfs3_cfg *cfg,
lfs3_block_t block, lfs3_size_t bit);
// Flip a bit in a given block, intended for emulating bit errors
@@ -260,6 +286,12 @@ lfs3_emubd_spowercycles_t lfs3_emubd_powercycles(
int lfs3_emubd_setpowercycles(const struct lfs3_cfg *cfg,
lfs3_emubd_powercycles_t power_cycles);
// Get a pseudo-random number from emubd's internal prng
uint32_t lfs3_emubd_prng(const struct lfs3_cfg *cfg);
// Set the current prng state
void lfs3_emubd_seed(const struct lfs3_cfg *cfg, uint32_t seed);
// Create a copy-on-write copy of the state of this block device
int lfs3_emubd_cpy(const struct lfs3_cfg *cfg, lfs3_emubd_t *copy);
+73 -36
View File
@@ -117,8 +117,11 @@ int lfs3_kiwibd_createcfg(const struct lfs3_cfg *cfg, const char *path,
bd->cfg = bdcfg;
// setup some initial state
bd->reads = 0;
bd->progs = 0;
bd->erases = 0;
bd->readed = 0;
bd->proged = 0;
bd->progged = 0;
bd->erased = 0;
bd->fd = -1;
if (path) {
@@ -276,6 +279,7 @@ int lfs3_kiwibd_read(const struct lfs3_cfg *cfg, lfs3_block_t block,
}
// track reads
bd->reads += 1;
bd->readed += size;
if (bd->cfg->read_sleep) {
int err = nanosleep(&(struct timespec){
@@ -404,7 +408,8 @@ int lfs3_kiwibd_prog(const struct lfs3_cfg *cfg, lfs3_block_t block,
}
// track progs
bd->proged += size;
bd->progs += 1;
bd->progged += size;
if (bd->cfg->prog_sleep) {
int err = nanosleep(&(struct timespec){
.tv_sec=bd->cfg->prog_sleep/1000000000,
@@ -465,6 +470,7 @@ int lfs3_kiwibd_erase(const struct lfs3_cfg *cfg, lfs3_block_t block) {
erased:;
// track erases
bd->erases += 1;
bd->erased += cfg->block_size;
if (bd->cfg->erase_sleep) {
int err = nanosleep(&(struct timespec){
@@ -501,6 +507,67 @@ int lfs3_kiwibd_sync(const struct lfs3_cfg *cfg) {
/// Additional kiwibd features ///
lfs3_kiwibd_sns_t lfs3_kiwibd_simtime(const struct lfs3_cfg *cfg) {
LFS3_KIWIBD_TRACE("lfs3_kiwibd_simtime(%p)", (void*)cfg);
lfs3_kiwibd_t *bd = cfg->context;
// error if all possible timings are zero
if (bd->cfg->reads_timing == 0
&& bd->cfg->progs_timing == 0
&& bd->cfg->erases_timing == 0
&& bd->cfg->readed_timing == 0
&& bd->cfg->progged_timing == 0
&& bd->cfg->erased_timing == 0) {
LFS3_KIWIBD_TRACE("lfs3_kiwibd_simtime -> %d", LFS3_ERR_NOTSUP);
return LFS3_ERR_NOTSUP;
}
lfs3_kiwibd_ns_t ns
= (bd->cfg->reads_timing * bd->reads)
+ (bd->cfg->progs_timing * bd->progs)
+ (bd->cfg->erases_timing * bd->erases)
+ (bd->cfg->readed_timing * bd->readed)
+ (bd->cfg->progged_timing * bd->progged)
+ (bd->cfg->erased_timing * bd->erased);
LFS3_KIWIBD_TRACE("lfs3_kiwibd_simtime -> %"PRIu64, ns);
return ns;
}
int lfs3_kiwibd_simreset(const struct lfs3_cfg *cfg) {
LFS3_KIWIBD_TRACE("lfs3_kiwibd_simreset(%p)", (void*)cfg);
lfs3_kiwibd_t *bd = cfg->context;
bd->reads = 0;
bd->progs = 0;
bd->erases = 0;
bd->readed = 0;
bd->progged = 0;
bd->erased = 0;
LFS3_KIWIBD_TRACE("lfs3_kiwibd_simreset -> %d", 0);
return 0;
}
lfs3_kiwibd_sio_t lfs3_kiwibd_reads(const struct lfs3_cfg *cfg) {
LFS3_KIWIBD_TRACE("lfs3_kiwibd_reads(%p)", (void*)cfg);
lfs3_kiwibd_t *bd = cfg->context;
LFS3_KIWIBD_TRACE("lfs3_kiwibd_reads -> %"PRIu64, bd->reads);
return bd->reads;
}
lfs3_kiwibd_sio_t lfs3_kiwibd_progs(const struct lfs3_cfg *cfg) {
LFS3_KIWIBD_TRACE("lfs3_kiwibd_progs(%p)", (void*)cfg);
lfs3_kiwibd_t *bd = cfg->context;
LFS3_KIWIBD_TRACE("lfs3_kiwibd_progs -> %"PRIu64, bd->progs);
return bd->progs;
}
lfs3_kiwibd_sio_t lfs3_kiwibd_erases(const struct lfs3_cfg *cfg) {
LFS3_KIWIBD_TRACE("lfs3_kiwibd_erases(%p)", (void*)cfg);
lfs3_kiwibd_t *bd = cfg->context;
LFS3_KIWIBD_TRACE("lfs3_kiwibd_erases -> %"PRIu64, bd->erases);
return bd->erases;
}
lfs3_kiwibd_sio_t lfs3_kiwibd_readed(const struct lfs3_cfg *cfg) {
LFS3_KIWIBD_TRACE("lfs3_kiwibd_readed(%p)", (void*)cfg);
lfs3_kiwibd_t *bd = cfg->context;
@@ -508,11 +575,11 @@ lfs3_kiwibd_sio_t lfs3_kiwibd_readed(const struct lfs3_cfg *cfg) {
return bd->readed;
}
lfs3_kiwibd_sio_t lfs3_kiwibd_proged(const struct lfs3_cfg *cfg) {
LFS3_KIWIBD_TRACE("lfs3_kiwibd_proged(%p)", (void*)cfg);
lfs3_kiwibd_sio_t lfs3_kiwibd_progged(const struct lfs3_cfg *cfg) {
LFS3_KIWIBD_TRACE("lfs3_kiwibd_progged(%p)", (void*)cfg);
lfs3_kiwibd_t *bd = cfg->context;
LFS3_KIWIBD_TRACE("lfs3_kiwibd_proged -> %"PRIu64, bd->proged);
return bd->proged;
LFS3_KIWIBD_TRACE("lfs3_kiwibd_progged -> %"PRIu64, bd->progged);
return bd->progged;
}
lfs3_kiwibd_sio_t lfs3_kiwibd_erased(const struct lfs3_cfg *cfg) {
@@ -522,33 +589,3 @@ lfs3_kiwibd_sio_t lfs3_kiwibd_erased(const struct lfs3_cfg *cfg) {
return bd->erased;
}
int lfs3_kiwibd_setreaded(const struct lfs3_cfg *cfg,
lfs3_kiwibd_io_t readed) {
LFS3_KIWIBD_TRACE("lfs3_kiwibd_setreaded(%p, %"PRIu64")",
(void*)cfg, readed);
lfs3_kiwibd_t *bd = cfg->context;
bd->readed = readed;
LFS3_KIWIBD_TRACE("lfs3_kiwibd_setreaded -> %d", 0);
return 0;
}
int lfs3_kiwibd_setproged(const struct lfs3_cfg *cfg,
lfs3_kiwibd_io_t proged) {
LFS3_KIWIBD_TRACE("lfs3_kiwibd_setproged(%p, %"PRIu64")",
(void*)cfg, proged);
lfs3_kiwibd_t *bd = cfg->context;
bd->proged = proged;
LFS3_KIWIBD_TRACE("lfs3_kiwibd_setproged -> %d", 0);
return 0;
}
int lfs3_kiwibd_seterased(const struct lfs3_cfg *cfg,
lfs3_kiwibd_io_t erased) {
LFS3_KIWIBD_TRACE("lfs3_kiwibd_seterased(%p, %"PRIu64")",
(void*)cfg, erased);
lfs3_kiwibd_t *bd = cfg->context;
bd->erased = erased;
LFS3_KIWIBD_TRACE("lfs3_kiwibd_seterased -> %d", 0);
return 0;
}
+60 -28
View File
@@ -28,32 +28,56 @@ typedef uint64_t lfs3_kiwibd_io_t;
typedef int64_t lfs3_kiwibd_sio_t;
// Type for delays in nanoseconds
typedef uint64_t lfs3_kiwibd_sleep_t;
typedef int64_t lfs3_kiwibd_ssleep_t;
typedef uint64_t lfs3_kiwibd_ns_t;
typedef int64_t lfs3_kiwibd_sns_t;
// kiwibd config, this is required for testing
struct lfs3_kiwibd_cfg {
// Optional statically allocated buffer for the block device. Ignored
// if disk_path is provided.
void *buffer;
// 8-bit erase value to use for simulating erases. -1 simulates a noop
// erase, which is faster than simulating a fixed erase value. -2 emulates
// nor-masking, which is useful for testing other filesystems (littlefs
// does _not_ rely on this!).
int32_t erase_value;
// Optional statically allocated buffer for the block device. Ignored
// if disk_path is provided.
void *buffer;
// Simulated read transaction timing in nanoseconds, this is added
// to simtime each read call, ignoring the requested size
lfs3_kiwibd_ns_t reads_timing;
// Artificial delay in nanoseconds, there is no purpose for this other
// than slowing down the simulation.
lfs3_kiwibd_sleep_t read_sleep;
// Simulated prog transaction timing in nanoseconds, this is added
// to simtime each prog call, ignoring the requested size
lfs3_kiwibd_ns_t progs_timing;
// Artificial delay in nanoseconds, there is no purpose for this other
// than slowing down the simulation.
lfs3_kiwibd_sleep_t prog_sleep;
// Simulated erase transaction timing in nanoseconds, this is added
// to simtime each erase call, ignoring the requested size
lfs3_kiwibd_ns_t erases_timing;
// Artificial delay in nanoseconds, there is no purpose for this other
// than slowing down the simulation.
lfs3_kiwibd_sleep_t erase_sleep;
// Simulated read byte timing in nanoseconds, this is scaled by the
// requested size and added to simtime each read call.
lfs3_kiwibd_ns_t readed_timing;
// Simulated prog byte timing in nanoseconds, this is scaled by the
// requested size and added to simtime each prog call.
lfs3_kiwibd_ns_t progged_timing;
// Simulated erase byte timing in nanoseconds, this is scaled by the
// requested size and added to simtime each erase call.
lfs3_kiwibd_ns_t erased_timing;
// Artificial read transaction delay in nanoseconds, there is no
// purpose for this other than slowing down the simulation.
lfs3_kiwibd_ns_t read_sleep;
// Artificial prog transaction delay in nanoseconds, there is no
// purpose for this other than slowing down the simulation.
lfs3_kiwibd_ns_t prog_sleep;
// Artificial erase transaction delay in nanoseconds, there is no
// purpose for this other than slowing down the simulation.
lfs3_kiwibd_ns_t erase_sleep;
};
// kiwibd state
@@ -66,8 +90,11 @@ typedef struct lfs3_kiwibd {
} u;
// amount read/progged/erased
lfs3_kiwibd_io_t reads;
lfs3_kiwibd_io_t progs;
lfs3_kiwibd_io_t erases;
lfs3_kiwibd_io_t readed;
lfs3_kiwibd_io_t proged;
lfs3_kiwibd_io_t progged;
lfs3_kiwibd_io_t erased;
const struct lfs3_kiwibd_cfg *cfg;
@@ -110,27 +137,32 @@ int lfs3_kiwibd_sync(const struct lfs3_cfg *cfg);
/// Additional kiwibd features ///
// Get total simulated runtime
lfs3_kiwibd_sns_t lfs3_kiwibd_simtime(const struct lfs3_cfg *cfg);
// Reset simulation counters
//
// You probably shouldn't call this, instead diff before/after simtimes
int lfs3_kiwibd_simreset(const struct lfs3_cfg *cfg);
// Get total number of read transactions
lfs3_kiwibd_sio_t lfs3_kiwibd_reads(const struct lfs3_cfg *cfg);
// Get total number of prog transactions
lfs3_kiwibd_sio_t lfs3_kiwibd_progs(const struct lfs3_cfg *cfg);
// Get total number of erase transactions
lfs3_kiwibd_sio_t lfs3_kiwibd_erases(const struct lfs3_cfg *cfg);
// Get total amount of bytes read
lfs3_kiwibd_sio_t lfs3_kiwibd_readed(const struct lfs3_cfg *cfg);
// Get total amount of bytes programmed
lfs3_kiwibd_sio_t lfs3_kiwibd_proged(const struct lfs3_cfg *cfg);
lfs3_kiwibd_sio_t lfs3_kiwibd_progged(const struct lfs3_cfg *cfg);
// Get total amount of bytes erased
lfs3_kiwibd_sio_t lfs3_kiwibd_erased(const struct lfs3_cfg *cfg);
// Manually set amount of bytes read
int lfs3_kiwibd_setreaded(const struct lfs3_cfg *cfg,
lfs3_kiwibd_io_t readed);
// Manually set amount of bytes programmed
int lfs3_kiwibd_setproged(const struct lfs3_cfg *cfg,
lfs3_kiwibd_io_t proged);
// Manually set amount of bytes erased
int lfs3_kiwibd_seterased(const struct lfs3_cfg *cfg,
lfs3_kiwibd_io_t erased);
#endif