Commit Graph

795 Commits

Author SHA1 Message Date
Christopher Haster 745b89d02b Fixed issue where looking up tag 0 fails after a delete id0
Well not really fixed, more just added an assert to make sure
lfsr_rbyd_lookup is not called with tag 0. Because our alt tags only
encode less-than-or-equal and greater-than, which can be flipped
trivially, it's not possible to encode removal of tag 0 during deletes.

Fortunately, this tag should already not exist for other pragmatic
reasons, it was just used as the initial value for traversals, where it
could cause this bug.
2023-02-12 17:14:57 -06:00
Christopher Haster 11e91e6612 Cleaned up dbgrbyd.py, implemented tree rendering for the new 3-leb encoding 2023-02-12 17:14:57 -06:00
Christopher Haster bab79bb1e7 Added fuzzing rbyd tests, mostly to find remove/delete balancing issues
Note that despite being prng based, these are still strictly
reproducible. Running the tests multiple times will always give the same
result.
2023-02-12 17:14:57 -06:00
Christopher Haster 7af2e722a8 Some minor rbyd cleanup 2023-02-12 17:14:57 -06:00
Christopher Haster 2a4b6fcad9 Rbyd tests are now passing again, however range removal needs a review 2023-02-12 17:14:57 -06:00
Christopher Haster 56fbf4155b Reenabling more tests, tracking down another difficult bug 2023-02-12 17:14:57 -06:00
Christopher Haster 2f3c0129d6 An ugly lfsr_rbyd_append implementation, but a working one 2023-02-12 17:14:57 -06:00
Christopher Haster 1c64ccbde7 Steady state before attempting a different rewrite of rbyd pruning 2023-02-12 17:14:57 -06:00
Christopher Haster 588a103db7 Working through 3-leb range deletes, proving to be problematic
The seperate interactions between ids and keys is new and confusing.
This was something that the previous combined weights hid.
2023-02-12 17:14:57 -06:00
Christopher Haster 5e0418029d Added mixed attr+id testing, tweaks to 3-leb tag utility functions 2023-02-12 17:14:57 -06:00
Christopher Haster 08f5d9ddf4 Middle of a rewrite for 3-leb encoding, but rbyd appends and creates both work
If we combine rbyd ids and B-tree weights, we need 32-bit ids since this
will eventually need to cover the full range of a file. This simply
doesn't fit into a single word anymore, unless littlefs uses 64-bit tags.
Generally not a great idea for a filesystem targeting even 8-bit
microcontrollers.

So here is a tag encoding that uses 3 leb128 words. This will likely
have more code cost and slightly more disk usage (we can no longer fit
tags into 2 bytes), though with most tags being alt pointers (O(m log m)
vs O(m)), this may not be that significant.

Note that we try to keep tags limited to 14-bits to avoid an extra leb128 byte,
which would likely affect all alt pointers. To pull this off we do away
with the subtype/suptype distinction, limiting in-tree tag types to
10-bits encoded on a per-suptype basis:

  in-tree tags:
                       ttttttt ttt00rv
                                 ^--^^- 10-bit type
                                    '|- removed bit
                                     '- valid bit
  iiii iiiiiii iiiiiii iiiiiii iiiiiii
                                     ^- n-bit id
       lllllll lllllll lllllll lllllll
                                     ^- m-bit length

  out-of-tree tags:
                       ttttttt ttt010v
                                 ^---^- 10-bit type
                                     '- valid bit
                               0000000
       lllllll lllllll lllllll lllllll
                                     ^- m-bit length

  alt tags:
                       kkkkkkk kkk1dcv
                                 ^-^^^- 10-bit key
                                   '||- direction bit
                                    '|- color bit
                                     '- valid bit
  wwww wwwwwww wwwwwww wwwwwww wwwwwww
                                     ^- n-bit weight
       jjjjjjj jjjjjjj jjjjjjj jjjjjjj
                                     ^- m-bit jump

The real pain is that with separate integers for id and tag, it no
longer makes sense to combine these into one big weight field. This
requires a significant rewrite.
2023-02-12 17:14:44 -06:00
Christopher Haster cdc3a486d6 Initial exploration of B-trees, but ran into issues composing with rbyds
The original idea was weighted B-trees composed out of weighted rbyds,
with the two weight systems being independent. Descent down the B-tree
uses the same technique in the current metadata data-structure of
searching for which branch to take during fetch, basically getting the
search for free (well, on top of the already required O(m) fetch
operation).

But this is fundamentally flawed. While file names provide an absolute
reference for finding matches, weights are relative references. So we
don't have enough information to do weight-based lookup during fetch.

This smells just like the relative-vs-absolute key issues that led to
rbyd vs rbd trees in the first place...

One option is to do rbyd traversals at each B-tree node to build the
necessary information to figure out the weights. But with rbyd
traversals taking O(m log m), this makes B-tree lookups O(log n * m log m),
and B-tree traversals a messy O(n log n * m log m), which is acceptable, but
disapointing for what will likely be the most common operation in the
filesystem.

But the rbyd trees _are_ already weighted. A better solution might be to
go back and rethink the seperation of B-tree weights and rbyd ids.
Unfortunately, with only 16-bits available for rbyd ids, this would
likely require a rewrite of how rbyd tags are encoded...
2023-02-12 17:14:42 -06:00
Christopher Haster 22f00649a6 Reduced caching during rbyd lookup based on preliminary benchmarking
This deserves more scrutiny in the future, but early benchmarking shows
caching here to add significant cost, since we often load branches we
don't take.

There may be smarter things we can do, but ideas quickly get into
branch-prediction territory, which is very, very out of scope.
2023-02-12 17:14:42 -06:00
Christopher Haster 0eccd6515f In plot.py/plotmpl.py, allowed escaped commas in certain comma-separated fields 2023-02-12 17:14:42 -06:00
Christopher Haster 9a8e1d93c6 Added some rbyd benchmarks, fixed/tweaked some related scripts
- Added both uattr (limited to 256) and id (limited to 65535) benchmarks
  covering the main rbyd operations

- Fixed issue where --defines gets passed to the test/bench runners when
  querying id-specific information. After changing the test/bench
  runners to prioritize explicit defines, this causes problems for
  recorded benchmark results and debug related things.

- In plot.py/plotmpl.py, made --by/-x/-y in subplots behave somewhat
  reasonably, contributing to a global dataset and the figure's legend,
  colors, etc, but only shown in the specified subplot. This is useful
  mainly for showing different -y values on different subplots.

- In plot.py/plotmpl.py, added --labels to allow explicit configuration
  of legend labels, much like --colors/--formats/--chars/etc. This
  removes one of the main annoying needs for modifying benchmark results.
2023-02-12 17:14:42 -06:00
Christopher Haster 27e4fbd3ad Re-upped on-disk leb128 limit for tags to 5-bytes
This gives us the full 16-bit range of ids (65536) instead of the much
smaller 12-bit range (4096) when limited to truncated 4-byte leb128s.

The real motivation for the truncated 4-byte leb128s is to keep the
wasted space in crc padding down, which this doesn't matter for.

In the future this may be configurable. Or maybe not. Only if truncated
leb128 tags prove to have value, at the moment it looks more like a
premature optimization if anything...

Either way we do need to test for overflowing this.
2023-02-12 17:14:42 -06:00
Christopher Haster d08497c299 Rearranged type encoding for crcs so they mostly fit in a single byte
I'm still not sure this is the best decision, since it may add some
complexity to tag parsing, but making most crcs one byte may be valuable
since these exist in every single commit.

This gives tags three high-level encodings:

  in-tree tags:
  iiiiiii iiiiitt ttTTTTT TTT00rv
              ^----^--------^--^^- 16-bit id
                   '--------|--||- 4-bit suptype
                            '--||- 8-bit subtype
                               '|- removed bit
                                '- valid bit
  lllllll lllllll lllllll lllllll
                                ^- n-bit length

  out-of-tree tags:
  ------- -----TT TTTTTTt ttt01pv
                       ^----^--^^- 8-bit subtype
                            '--||- 4-bit suptype
                               '|- perturb bit
                                '- valid bit
  lllllll lllllll lllllll lllllll
                                ^- n-bit length

  alt tags:
  wwwwwww wwwwwww wwwwwww www1dcv
                            ^-^^^- 28-bit weight
                              '||- direction bit
                               '|- color bit
                                '- valid bit
  jjjjjjj jjjjjjj jjjjjjj jjjjjjj
                                ^- n-bit jump

Having the location of the subtype flipped for crc tags vs tree tags is
unintuitive, but it makes more crc tags fit in a single byte, while
preserving expected tag ordering for tree tags.

The only case where crc tags don't fit in a single byte if is non-crc
checksums (sha256?) are added, at which point I expect the subtype to
indicate which checksum algorithm is in use.
2023-02-12 17:14:14 -06:00
Christopher Haster 55b072e761 Opened up rbyd testing for all geometries, and fixed related bugs
- Caching is still presenting issues with the new requirements for
  rbyd trees, in this case the default bd, with 64 byte progs, revealed
  and issue where rcache could become outdated when reading from disk
  while ignoring what's in the pcache.

  It assumes the pcache will always override the rcache, but this is not
  true after pcache is flushed.

  This didn't happen before as the rcache and pcache don't
  interact while writing in the previous implementation. Because of
  these new requirements the caching system probably deserves a
  rework...

- The quick tests for sublinear space utilization don't work when
  prog_size is > a byte, fortunately we should always have NOR-like
  geometry under test, so we can limit these asserts to NOR-like
  geometry.

- Lots of problems fitting these tests into 512-byte block_size
  geometries, which is a bit concerning. This may be a larger change
  from the previous implementation than expected. This may deserve more
  scrutiny at small block sizes to see how things fit, since the
  sublinear space utilization doesn't really kick in at this scale...

  On the other hand it may just be that these tests are too aggressive
  for 512-byte block sizes, since they don't yet do compaction, which
  should help with padding/crc overhead...
2023-02-12 17:14:12 -06:00
Christopher Haster 01dfd1feef Added tree rendering to dbgrbyd.py
$ ./scripts/dbgrbyd.py disk 4096 0 -t
  mdir 0x0, rev 1, size 121
  off                tag                     data (truncated)
  0000005e: +-+-+--> uattr 0x01 4            aa aa aa aa              ....
  0000000f: | | '--> uattr 0x02 4            aa aa aa aa              ....
  0000001d: | '----> uattr 0x03 4            aa aa aa aa              ....
  0000002d: | .----> uattr 0x04 4            aa aa aa aa              ....
  0000003d: | | .--> uattr 0x05 4            aa aa aa aa              ....
  0000004f: '-+-+-+> uattr 0x06 4            aa aa aa aa              ....
  00000004:       '> uattr 0x07 4            aa aa aa aa              ....

Unfortunately this tree can end up a bit confusing when alt pointers
live in unrelated search paths...
2023-02-12 17:14:12 -06:00
Christopher Haster 8581eec433 Added lfs_rbyd_rangesize (untested), some cleanup
Toying around with the idea that since rbyd trees have strict height
gaurantees after compaction (2*log2(n)+1), we can proactively calculate
the maximum on-disk space required for a worst case tree+leb128
encoding.

This would _greatly_ simplify things such as metadata compaction and
splitting, and allow unstorable file metadata (too many custom
attributes) to error early.

One issue is that this calculated worst case will likely be ~4-5x worst
than the actual encoding due to leb128 compression. Though this may be an
acceptable tradeoff for the simplification and more reliable behavior.
2023-02-12 17:14:12 -06:00
Christopher Haster 4aabb8f631 Reworked tag representation so that sup/sub types have expected order
Previously the subtype was encoded above the suptype. This was an issue
if you wanted to, say, traverse all tags in a given suptype.

I'm not sure yet if this sort of functionality is needed, it may be
useful for cleaning up/replacing classes of tags, such as file struct
tags, but not sure yet. At the very least is avoids unintuitive tag
ordering in the tree, which could potential cause problems for
create/deletes.

New encoding:

  tags:
  iiiiiii iiiiitt ttTTTTT TTT0trv
              ^----^--------^-^^^- 16-bit id
                   '--------|-'||- 5-bit suptype (split)
                            '--||- 8-bit subtype
                               '|- perturb/remove bit
                                '- valid bit
  lllllll lllllll lllllll lllllll
                                ^- n-bit length

  alts:
  wwwwwww wwwwwww wwwwwww www1dcv
                            ^^^-^- 28-bit weight
                             '|-|- color bit
                              '-|- direction bit
                                '- valid bit
  jjjjjjj jjjjjjj jjjjjjj jjjjjjj
                                ^- n-bit jump

Also a large amount of name changes and other cleanup.
2023-02-12 17:13:57 -06:00
Christopher Haster 1d037f4ec9 Symbol cleanup as weight representation becomes more concrete 2023-02-12 15:24:04 -06:00
Christopher Haster 21012dbb1b Some minor append cleanup, mostly around follow conditions 2023-02-12 15:23:48 -06:00
Christopher Haster 6a5aae98fc Cleaned up the leaf splitting condition
Turns out if do the less-than condition first, we can simplify things. I
think this is due to how we bias the tree weights.
2023-02-12 15:22:21 -06:00
Christopher Haster 5c42e52324 Revert "A different thought on tracking found tags during lfs_rbyd_append" 2023-02-12 15:21:42 -06:00
Christopher Haster 209e70897c A different thought on tracking found tags during lfs_rbyd_append
The idea here is to just use a separate variable for found tags, instead
of stealing a bit for tracking which tags were found.

I was hoping this would simplify more operations on the tags to avoid
some masking operations, but it doesn't seem like it.
2023-02-12 15:19:03 -06:00
Christopher Haster 3d008b793d This method of leaf splitting works, but I want to explore another option
Committing current progress.
2023-02-12 15:18:15 -06:00
Christopher Haster d8540974d4 Significant cleanup of lfs_rbyd_append, simplified pruning rules 2023-02-12 15:17:54 -06:00
Christopher Haster 17a043f4b8 Merged lfs_rbyd_append and lfs_rbyd_delete
Now we have one massive chimera of a function that does everything. This
is good software design, trust me.
2023-02-12 15:14:23 -06:00
Christopher Haster 179a1df3d3 Changed rbyd removes to remove from the tree, similar to deletes
Tag removal is basically a range-delete of one that doesn't change
the tree weights. This deduplicates the two methods of deleting tags and
completely gets rid of tombstoning.

Note we still need a "removed tag" encoding so that we can invalidate
tags that may be found during fetch operations. Fortunately this
encoding is basically free due to overlap with alt encoding.
2023-02-12 15:14:06 -06:00
Christopher Haster 92ce5df949 Added tests for theoretical bound on tree height and recovery from delete-all
- Unless there is a bug, rbyd trees should be strictly <= (2*log2(n)+1)
  in height. The extra +1 from traditional red-black trees is due to the
  introduced to-be-pruned alt, but since we clean those up as soon as we
  can, only one will ever exist in any search path.

  This also holds true with range deletion, however the definition of n
  changes to the number of tree operations. This is the same for tombstoning.

- Delete-all recovery is a bit tricky because we have no tree at that
  point, which is weird for an append-only data-structure.
2023-02-12 15:14:01 -06:00
Christopher Haster 9a1ce1be47 Removed erroneous new red edges on deletion 2023-02-12 15:14:01 -06:00
Christopher Haster 1a5efcfa89 Removed unnecessary use of graft in early continues 2023-02-12 15:14:01 -06:00
Christopher Haster 788e98a989 Made delete remove the path from the tree completely, fixed failing tests 2023-02-12 15:13:52 -06:00
Christopher Haster 045a1098fd Added similar delete permutation tests, note that some are failing 2023-02-12 15:02:45 -06:00
Christopher Haster b14851b657 Added more tests, mostly around remove permutations 2023-02-12 15:02:27 -06:00
Christopher Haster dc5e74ac41 Added range deletion testing 2023-02-12 15:02:11 -06:00
Christopher Haster fbb6bd0919 A working delete implementation?
Though note this still uses tombstoning.
2023-02-12 15:01:05 -06:00
Christopher Haster b621774759 Adopted upper/lower bounds in lfs_rbyd_append
There are two ways to represent the bounds in the search down the rbyd
tree:

1. Using lower/upper bounds and the id we are searching for:

     lower bound    id    upper bound
     |              |     |
     v              v     v
   <-a--b--c--d--e--f--g--h->

2. Using the lower/upper weights, which implicitly encodes the id,
   saving a word:

       lower weight     upper weight
       |                |
     .-'-----------. .--'-.
   <-a--b--c--d--e--f--g--h->

Now that I am diving deep into the rbyd algorithm again, the lower/upper
weight based approach just isn't worth the extra mental steps required
to understand what the algorithm is doing. Besides, we likely pay for
the implicit id anyways since we need enough state to remove the
ambiguity of sparse tags.
2023-02-12 14:52:46 -06:00
Christopher Haster d78c4412b6 Half-adopted upper/lower bounds in lookup and append 2023-02-12 14:51:34 -06:00
Christopher Haster 55e9df571b Minor test_rbyd improvements
Attempted to implement TEST_PERMUTATIONS but that went nowhere useful.
Unfortunately test defines don't have a way to represent arrays, and
adding such a representation would be unreasonably complicated.

For practical purposes it's better to just do the permutation generation
inside the tests. Maybe at some point it would be nice to integrate
permutation testing so the state gets hashed somehow as a part of the
test failure, but now is not the time.
2023-02-12 14:49:04 -06:00
Christopher Haster ef7ee6eb7d Added delete permutation testing (failing as expected) and some minor tweaks 2023-02-12 14:48:33 -06:00
Christopher Haster 22879c1cbb Avoided issue with tail deletions by tossing out-of-bound lookups early 2023-02-12 14:47:38 -06:00
Christopher Haster 5d9e7c8e86 Moved lifetimes in dbgrbyd.py so lifetimes and jumps can both be rendered
$ ./scripts/dbgrbyd.py disk 4096 0 -g -j
  mdir 0x0, rev 1, size 59
  off             tag                     data (truncated)
  00000004: .     createreg id1 4         aa aa aa aa              ....  <--.
  0000000c: |     altblt x80d0 x4                                        -' |
  00000010: | .   createreg id2 4         cc cc cc cc              ....  <. |
  00000018: | |   altrlt x80d0 x4                                        -|-'
  0000001c: | |   altbgt x8000 x10                                       -'
  00000020: | .\  createreg id2 4         bb bb bb bb              ....
  00000028: | | | fcrc 5                  51 53 7d 52 01           QS}R.
  0000002f: | | | crc0 7                  5f db 22 8a 1b 1b 1b     _."....
2023-02-12 14:44:28 -06:00
Christopher Haster 1b6e1fdd33 Fixed delete issue with cut starting in a red edge
Though this is a bit of an ugly fix...

Red edges mess everything up during deletion since we always need to
go down red edges in case we need to do a red flip. This puts our
algorithm into an awkward state.

It would be easier to do away with colors completely when we start
deleting (after all this branch of the tree _will_ be shrinking), but
this conflicts with the ultimate goal of deduplicate rbyd append.
2023-02-12 14:41:47 -06:00
Christopher Haster ca710b5a29 Initial, very, very rough implementation of rbyd range deletion
Tree deletion is such a pain. It always seems like an easy addition to
the core algorithm but always comes with problems.

The initial plan for deletes was to iterate through all tags, tombstone,
and then adjust weights as needed. This accomplishes deletes with little
change to the rbyd algorithm, but adds a complex traversal inside the
commit logic. Doable in one commit, but complex. It also risks weird
unintuitive corner cases since the cost of deletion grows with the number
of tags being deleted (O(m log n)).

But this rbyd data structure is a tree, so in theory it's possible to
delete a whole range of tags in a single O(log n) operation.

---

This is a proof-of-concept range deletion algorithm for rbyd trees.

Note, this does not preserve rbyd's balancing properties! But it is no
worse than tombstoning. This is acceptable for littlefs as any
unbalanced trees will be rebalanced during compaction.

The idea is to follow the same underlying dhara algorithm, where we
follow a search path and save any alt pointers not taken, but we follow
both search paths that form the outside of the range, and only keep
outside edges.

For example, a tree:

        .-------o-------.
        |               |
    .---o---.       .---o---.
    |       |       |       |
  .-o-.   .-o-.   .-o-.   .-o-.
  |   |   |   |   |   |   |   |
  a   b   c   d   e   f   g   h

To delete the range d-e, we would search for d, and search for e:

        ********o********
        *               *
    .---*****       *****---.
    |       *       *       |
  .-o-.   .-***   ***-.   .-o-.
  |   |   |   *   *   |   |   |
  a   b   c   d   e   f   g   h

And keep the outside edges:

    .---                 ---.
    |                       |
  .-o-.   .-         -.   .-o-.
  |   |   |           |   |   |
  a   b   c           f   g   h

But how do we combine the outside edges? The simpler option is to do
both searches seperately, one after the other. This would end up with a
tree like this:

    .---------o
    |         |
  .-o-.   .---o
  |   |   |   |
  a   b   c   o---------.
              |         |
              o---.   .-o-.
              |   |   |   |
              _   f   g   h

But this horribly throws off the balance of our tree! It's worse than
tombstoning, and gets worse with more tags.

An alternative strategy, which is used here, is to alternate edges as we
descend down the tree. This unfortunately is more complex, and requires
~2x the RAM, but better preserves the balance of our tree. It isn't
perfect, because we lose color information, but we can leave that up to
compaction:

  .---------o
  |         |
.-o-.       o---------.
|   |       |         |
a   b   .---o       .-o-.
        |   |       |   |
        c   o---.   g   h
            |   |
            _   f

I also hope this can be merged into lfs_rbyd_append, deduplicating the
entire core rbyd append algorithm.
2023-02-12 13:29:06 -06:00
Christopher Haster cde3ba4cd8 Different rbyd struct usage in commit, moved append into its own function
Under any optimization level the compiler should inline lfs_rbyd_append
into lfs_rbyd_commit, but this code organization was desperately needed.
2023-02-12 13:25:51 -06:00
Christopher Haster 12edc5aee3 Added some ascii art to dbgrbyd.py to help debug how ids change over time
An example:

  $ ./scripts/dbgrbyd.py disk 4096 0 -i
  mdir 0x0, rev 1, size 59
  off       tag                     data (truncated)
  00000004: create x01 id1 4        aa aa aa aa              ....      .
  0000000c: altblt x80d0 x4                                            |
  00000010: create x01 id2 4        cc cc cc cc              ....      | .
  00000018: altrlt x80d0 x4                                            | |
  0000001c: altbgt x8000 x10                                           | |
  00000020: create x01 id2 4        bb bb bb bb              ....      | .\
  00000028: fcrc 5                  51 53 7d 52 01           QS}R.     | | |
  0000002f: crc0 7                  5f db 22 8a 1b 1b 1b     _."....   | | |
2023-02-12 13:23:56 -06:00
Christopher Haster 2979e6273a Added weight-adjusting insert operations, though it is currently very hacky 2023-02-12 13:22:45 -06:00
Christopher Haster 6c66e20349 Added lfs_rbyd_get, bumped hint back up to full during lookup
The idea behind the full hint is that we may stay on the current search
path, benefiting from a loaded cache. Though this should probably be
benchmarked eventually.
2023-02-12 13:21:41 -06:00