Skip to content

If a vmstat column overflows its width, realign following columns by dropping redundant leading spaces

Martin Guy requested to merge (removed):master into master

When a fiure of vmstat output overflows its column width it pushes everything else to the right, leaving the columns unmisaligned and exceeding 80 characters' output width.

This change solves the problem in two commits:

  • the first replaces the giant printf() lines with a table of column headers and widths, but makes no functional chane to vmstat.
  • the second spots when output overflows its column and aligns the following columns as soon as it can by omitting extra leading spaces from those that do not use their whole column width.

Example before: procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 0 1814524 142572 788672 0 0 7 7 318 262 2 1 96 0 0 0 0 0 1814812 142588 788672 0 0 0 14 687 1526 0 0 99 0 0 0 0 0 1814564 142588 788672 0 0 0 3 960 2123 1 1 99 0 0

Example after: procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 0 0 0 1809572 142604 760956 0 0 7 7 318 263 2 1 96 0 0 0 0 0 1809700 142612 760956 0 0 0 2 804 1758 1 1 99 0 0 0 0 0 1809328 142620 760956 0 0 0 5 1069 2350 1 1 99 0 0

"free" overflows, "buff" and "cache" are full, so a leading space is removed from "si" to align it and the other columns and fit 80 characters.

Programs that interpret the output of vmstat should not break:

  • if they're matching space-separated fields, that will still work because at lesat one space is still guaranteed between fields
  • if they're matching on column numbers (like "cut -c"), that would have failed anyway when the first figure overflowed, so there is no extra harm!

Merge request reports