top: fix and merge code to parse /proc/meminfo
authorTimo Teräs <timo.teras@iki.fi>
Mon, 21 Jul 2014 11:14:24 +0000 (14:14 +0300)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 27 Jul 2014 19:49:27 +0000 (21:49 +0200)
commit7df1f1dda1f997c44800d16a9a12cf6cae2ed7e7
tree6e5f6e27360e50a574c4e03c624ab0b07576f57c
parent2a563ea49a16589f47ed6afe7b22eebef4e3a6d1
top: fix and merge code to parse /proc/meminfo

display_header() code to parse meminfo as is was buggy:
- uninitialized variables were used if meminfo was not as expected
- meminfo parsing failed on new kernels (3.14+) as new field 'MemAvailable'
  was introduced between MemFree and Buffers
- shared memory was handled only for ancient kernels (2.4.x and earlier)

as result Buffers and shared memory fields were shown with bogus values
on current kernels.

The new code does not try to parse the old style summary header, as the
separated fields are always present (it saves code size). Additionally,
both Shmem (2.6+) and MemShared (2.4 and earlier) fields are now parsed
and summed for shared memory usage; as only one of them exists depending
on kernel version.

display_topmem_header() parses also meminfo so this makes it use the
same code for code shrink.

function                                             old     new   delta
display_header                                         -     681    +681
display_topmem_process_list                          465     684    +219
parse_meminfo                                          -     189    +189
static.fields                                          -     106    +106
static.match                                         132       -    -132
.rodata                                           120254  120117    -137
display_topmem_header                                513       -    -513
display_process_list                                1705     667   -1038
------------------------------------------------------------------------------
(add/remove: 3/2 grow/shrink: 1/2 up/down: 1195/-1820)       Total: -625 bytes

Signed-off-by: Timo Teräs <timo.teras@iki.fi>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
procps/top.c