runners: emubd/kiwibd: Adopted lower-level bus+buffer bd sim

After letting it sit for a bit, the previous byte+op sim comes across as
overly clever in a way that is counter-productive. This is highlighted
by erase-timing scaling in a confusing way when per-op.

Fortunately, with a bit of tweaking, we can instead model the bd sim as
separate bus+buffer timings. This seems more intuitive and is closer to
how the actual hardware works.

---

In the bus+buffer model, bd operations are simulated using two sets of
timing estimates:

  buffer timings (nor)          bus timings (nor)
  read_timing (0)               readed_timing (40 ns/B)
  prog_timing (1563 ns/B)       progged_timing (19 ns/B)
  erase_timing (10986 ns/B)     erased_timing (0)

Bus timings are a simple multiplier of the bytes read/progged/erased,
while buffer timings are rounded up + aligned to the nearest "width":

  bd geometry (nor)             bd buffers (nor)
  read_size (1 B)               read_width (1 B)
  prog_size (1 B)               prog_width (256 B)
  erase_size (4096 B)           erase_width (4096 B)

For most purposes, the width should just be the device's read/prog/erase
buffer, but I went with the name width to try to keep it generic and
avoid confusion with "buffer" elsewhere in the codebase.

Some notes:

- Like the byte+op sim, the bus+buffer sim allows penalizing small
  operations without artificially limiting what operations are possible.

- Because buffer timings depend on read/prog/erase alignment, there's no
  simple equation from ops+bytes to bus+buffer. But as a tradeoff, this
  new sim more accurately penalizes unaligned operations.

- All timings are still kept as per-byte instead of per-width. This has
  proven to be more flexible when benchmarking, as you usually what
  timings to scale with the relevant operation.

- Currently this implemented by changing reads/progs/erases to track the
  number of "widths" read/progged/erased after alignment. Which makes
  the simtime formula roughly:

    simtime = reads*read_width*read_timing + readed*readed_timing
              (per-butter)                   (per-bus)

  I considered keeping separate counters for calls (read_calls/
  prog_calls/erase_calls?), but not sure there's a good reason to. The
  theory behind these widths is there no functional difference between
  one big call vs multiple width sized calls, though maybe they would be
  useful for debugging?

  We can always add these later if they turn out to be useful.

- When widths are disable (0), reads/progs/erases reverts to the number
  of read/prog/erase calls.

  This is the behavior when BENCH_SIMPLE is defined at compile-time.
This commit is contained in:
Christopher Haster
2026-02-03 12:19:10 -06:00
parent 7bc23c89b7
commit 3db2bb980b
5 changed files with 148 additions and 88 deletions
+15 -9
View File
@@ -437,7 +437,9 @@ int lfs3_emubd_read(const struct lfs3_cfg *cfg, lfs3_block_t block,
}
// track reads
bd->reads += 1;
bd->reads += (lfs3_alignup(off + size, lfs3_max(bd->cfg->read_width, 1))
- lfs3_aligndown(off, lfs3_max(bd->cfg->read_width, 1)))
/ lfs3_max(bd->cfg->read_width, 1);
bd->readed += size;
if (bd->cfg->read_sleep) {
int err = nanosleep(&(struct timespec){
@@ -749,7 +751,9 @@ progged:;
}
// track progs
bd->progs += 1;
bd->progs += (lfs3_alignup(off + size, lfs3_max(bd->cfg->prog_width, 1))
- lfs3_aligndown(off, lfs3_max(bd->cfg->prog_width, 1)))
/ lfs3_max(bd->cfg->prog_width, 1);
bd->progged += size;
if (bd->cfg->prog_sleep) {
int err = nanosleep(&(struct timespec){
@@ -1045,7 +1049,9 @@ int lfs3_emubd_erase(const struct lfs3_cfg *cfg, lfs3_block_t block) {
erased:;
// track erases
bd->erases += 1;
bd->erases += lfs3_alignup(cfg->block_size,
lfs3_max(bd->cfg->erase_width, 1))
/ lfs3_max(bd->cfg->erase_width, 1);
bd->erased += cfg->block_size;
if (bd->cfg->erase_sleep) {
int err = nanosleep(&(struct timespec){
@@ -1087,9 +1093,9 @@ lfs3_emubd_sns_t lfs3_emubd_simtime(const struct lfs3_cfg *cfg) {
lfs3_emubd_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
if (bd->cfg->read_timing == 0
&& bd->cfg->prog_timing == 0
&& bd->cfg->erase_timing == 0
&& bd->cfg->readed_timing == 0
&& bd->cfg->progged_timing == 0
&& bd->cfg->erased_timing == 0) {
@@ -1098,9 +1104,9 @@ lfs3_emubd_sns_t lfs3_emubd_simtime(const struct lfs3_cfg *cfg) {
}
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->read_timing * bd->reads*bd->cfg->read_width)
+ (bd->cfg->prog_timing * bd->progs*bd->cfg->prog_width)
+ (bd->cfg->erase_timing * bd->erases*bd->cfg->erase_width)
+ (bd->cfg->readed_timing * bd->readed)
+ (bd->cfg->progged_timing * bd->progged)
+ (bd->cfg->erased_timing * bd->erased);