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 number in the output of vmstat overflows its column width, it pushes everything else to the right, leaving the columns misaligned 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, to be able to vary the field size, but makes no functional change to vmstat.
  • the second spots when output overflows its column and aligns the following columns as soon as it can by omitting unnecessary leading spaces from columns that do not use their whole column width.

Example before, 81 columns:

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, 80 columns:

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

In the example, "free" overflows, and "buff" and "cache" are full, so a leading space is removed from "si" to realign it and the other columns.

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 fail anyway when the first figure overflows, so there is no extra harm!
Edited by Martin Guy

Merge request reports