diff options
author | Rik van Riel <riel@redhat.com> | 2014-01-21 18:49:05 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-01-21 19:19:43 -0500 |
commit | 34e431b0ae398fc54ea69ff85ec700722c9da773 (patch) | |
tree | a2a0de67b4cc754b5aa7627df3b0d1778d4cf10f /fs/proc | |
parent | 5eaf1a9e233d61438377f57facb167f8208ba9fd (diff) |
/proc/meminfo: provide estimated available memory
Many load balancing and workload placing programs check /proc/meminfo to
estimate how much free memory is available. They generally do this by
adding up "free" and "cached", which was fine ten years ago, but is
pretty much guaranteed to be wrong today.
It is wrong because Cached includes memory that is not freeable as page
cache, for example shared memory segments, tmpfs, and ramfs, and it does
not include reclaimable slab memory, which can take up a large fraction
of system memory on mostly idle systems with lots of files.
Currently, the amount of memory that is available for a new workload,
without pushing the system into swap, can be estimated from MemFree,
Active(file), Inactive(file), and SReclaimable, as well as the "low"
watermarks from /proc/zoneinfo.
However, this may change in the future, and user space really should not
be expected to know kernel internals to come up with an estimate for the
amount of free memory.
It is more convenient to provide such an estimate in /proc/meminfo. If
things change in the future, we only have to change it in one place.
Signed-off-by: Rik van Riel <riel@redhat.com>
Reported-by: Erik Mouw <erik.mouw_2@nxp.com>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'fs/proc')
-rw-r--r-- | fs/proc/meminfo.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/fs/proc/meminfo.c b/fs/proc/meminfo.c index a77d2b299199..24270eceddbf 100644 --- a/fs/proc/meminfo.c +++ b/fs/proc/meminfo.c | |||
@@ -26,7 +26,11 @@ static int meminfo_proc_show(struct seq_file *m, void *v) | |||
26 | unsigned long committed; | 26 | unsigned long committed; |
27 | struct vmalloc_info vmi; | 27 | struct vmalloc_info vmi; |
28 | long cached; | 28 | long cached; |
29 | long available; | ||
30 | unsigned long pagecache; | ||
31 | unsigned long wmark_low = 0; | ||
29 | unsigned long pages[NR_LRU_LISTS]; | 32 | unsigned long pages[NR_LRU_LISTS]; |
33 | struct zone *zone; | ||
30 | int lru; | 34 | int lru; |
31 | 35 | ||
32 | /* | 36 | /* |
@@ -47,12 +51,44 @@ static int meminfo_proc_show(struct seq_file *m, void *v) | |||
47 | for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++) | 51 | for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++) |
48 | pages[lru] = global_page_state(NR_LRU_BASE + lru); | 52 | pages[lru] = global_page_state(NR_LRU_BASE + lru); |
49 | 53 | ||
54 | for_each_zone(zone) | ||
55 | wmark_low += zone->watermark[WMARK_LOW]; | ||
56 | |||
57 | /* | ||
58 | * Estimate the amount of memory available for userspace allocations, | ||
59 | * without causing swapping. | ||
60 | * | ||
61 | * Free memory cannot be taken below the low watermark, before the | ||
62 | * system starts swapping. | ||
63 | */ | ||
64 | available = i.freeram - wmark_low; | ||
65 | |||
66 | /* | ||
67 | * Not all the page cache can be freed, otherwise the system will | ||
68 | * start swapping. Assume at least half of the page cache, or the | ||
69 | * low watermark worth of cache, needs to stay. | ||
70 | */ | ||
71 | pagecache = pages[LRU_ACTIVE_FILE] + pages[LRU_INACTIVE_FILE]; | ||
72 | pagecache -= min(pagecache / 2, wmark_low); | ||
73 | available += pagecache; | ||
74 | |||
75 | /* | ||
76 | * Part of the reclaimable swap consists of items that are in use, | ||
77 | * and cannot be freed. Cap this estimate at the low watermark. | ||
78 | */ | ||
79 | available += global_page_state(NR_SLAB_RECLAIMABLE) - | ||
80 | min(global_page_state(NR_SLAB_RECLAIMABLE) / 2, wmark_low); | ||
81 | |||
82 | if (available < 0) | ||
83 | available = 0; | ||
84 | |||
50 | /* | 85 | /* |
51 | * Tagged format, for easy grepping and expansion. | 86 | * Tagged format, for easy grepping and expansion. |
52 | */ | 87 | */ |
53 | seq_printf(m, | 88 | seq_printf(m, |
54 | "MemTotal: %8lu kB\n" | 89 | "MemTotal: %8lu kB\n" |
55 | "MemFree: %8lu kB\n" | 90 | "MemFree: %8lu kB\n" |
91 | "MemAvailable: %8lu kB\n" | ||
56 | "Buffers: %8lu kB\n" | 92 | "Buffers: %8lu kB\n" |
57 | "Cached: %8lu kB\n" | 93 | "Cached: %8lu kB\n" |
58 | "SwapCached: %8lu kB\n" | 94 | "SwapCached: %8lu kB\n" |
@@ -105,6 +141,7 @@ static int meminfo_proc_show(struct seq_file *m, void *v) | |||
105 | , | 141 | , |
106 | K(i.totalram), | 142 | K(i.totalram), |
107 | K(i.freeram), | 143 | K(i.freeram), |
144 | K(available), | ||
108 | K(i.bufferram), | 145 | K(i.bufferram), |
109 | K(cached), | 146 | K(cached), |
110 | K(total_swapcache_pages()), | 147 | K(total_swapcache_pages()), |