RFC: flag/bitfield pretty printers #15945
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation and Context
As a good C program, OpenZFS uses bitfields everywhere. I am extremely bad at decoding these in my head, so I frequently find myself writing weird custom formatters. Today I finally got sick of that, so I had a go at making some formatting facilities for bitfields.
I'm looking for code review, suggestions for improvements and any other thoughts you might have. It might be that a generic facility isn't worth it, but it'd be nice to have something available we can reach for for the common cases, and as you see, there's opportunity to improve user tools as well.
Description
In the commits you will find
zfs_pretty
, which is a bunch of tables that describe display styles for a handful of bitfield types, and functions to take a value and format it. There's three display styles available for each value: a single character, a character pair, and a full name.There's then three example applications, which I will show here.
When we've got plenty of space to work with and/or being explicit is important, there's a full expansion mode available (
zfs_pretty_<type>_str()
). An example is when showing ZIO contents inzio_events
after an error:When a much tighter format is wanted, there's a one-char-per-bit mode available (
zfs_pretty_<type>_bits()
). This is likely to be more useful for types with relatively few defined bits, as there's a limited number of unambiguous characters available. However, since unset bits are shown as blank space, this mode may still be useful in some cases as a visual representation of shape or sequence. This example shows something I do pretty often, which is to dump bits of ZIOs or ABDs in flight:Finally, there's a compact "pairs" mode (
zfs_pretty_<type>_pairs()
), which has two chars per bit, with a visual separator. This can work well when there's too many bits defined for the "bits" mode to be useful, but there's still a limited amount of space available. Here's an example showing ARC header flags in the dbuf stats:I don't claim that these are good uses (well, I like the
zpool events
one); they were just convenient ways to show what's going on.(I will spare you further experiments involving colour and emojis ... for now!)
How Has This Been Tested?
Draft, so minimal; just hand testing for now.
Types of changes
Checklist:
Signed-off-by
.