library: properly handle memory used by tmpfs

tmpfs has become much more widely used since distributions use it for
/tmp (Fedora 18+). In /proc/meminfo, memory used by tmpfs is accounted
into "Cached" (aka "NR_FILE_PAGES",
 http://lxr.free-electrons.com/source/mm/shmem.c#L301 ).

The tools just pass it on, so what top, free and vmstat report as
"cached" is the sum of page cache and tmpfs.

free has the extremely useful "-/+ buffers/cache" output. However, now
that tmpfs is accounted into "cached", those numbers are way off once
you have big files in /tmp.

Fortunately, kernel 2.6.32 introduces "Shmem", which makes tmpfs memory
usage accessible from userspace (
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=4b02108ac1b3354a22b0d83c684797692efdc395 ).

This patch substracts Shmem from Cached to get the actual page cache
memory. This makes both issues mentioned above disappear. For older
kernels, Shmem is not available (hence zero) and this patch is no-op.

Additionally:
* Update the man pages of free and vmstat to explain what is happening
* Finally drop "MemShared" from the /proc/meminfo parser, it has been
  dead for 10+ years and is only causing confusion ( removed in kernel
  2.5.54, see
  https://git.kernel.org/cgit/linux/kernel/git/tglx/history.git/commit/?id=fe04e9451e5a159247cf9f03c615a4273ac0c571 )
This commit is contained in:
Jakob Unterwurzacher
2014-02-18 22:12:21 +01:00
committed by Jaromir Capik
parent 24f1fbd9d0
commit 3569c0351f
4 changed files with 31 additions and 12 deletions

29
free.1
View File

@@ -11,11 +11,30 @@ free \- Display amount of free and used memory in the system
.SH DESCRIPTION
.B free
displays the total amount of free and used physical and swap memory in the
system, as well as the buffers used by the kernel.
The shared memory column represents either the MemShared value (2.4 series
kernels) or the Shmem value (2.6 series kernels and later) taken from the
/proc/meminfo file. The value is zero if none of the entries is exported
by the kernel.
system, as well as the buffers and caches used by the kernel. The
information is gathered by parsing /proc/meminfo. The displayed
columns are:
.TP
\fBtotal\fR
Total installed memory (MemTotal and SwapTotal in /proc/meminfo)
.TP
\fBused\fR
Used memory (calculated as total - free)
.TP
\fBfree\fR
Unused memory (MemFree and SwapFree in /proc/meminfo)
.TP
\fBshared\fR
Memory used (mostly) by tmpfs (Shmem in /proc/meminfo, available on
kernels 2.6.32, displayed as zero if not available)
.TP
\fBbuffers\fR
Memory used by kernel buffers (Buffers in /proc/meminfo)
.TP
\fBcached\fR
Memory used by the page cache (calculated as Cached - Shmem in
/proc/meminfo - the Cached value is actually the sum of page cache and
tmpfs memory)
.SH OPTIONS
.TP
\fB\-b\fR, \fB\-\-bytes\fR