[pve-devel] [PATCH container] vmstatus: fix memory usage value including cache sizes

Thomas Lamprecht t.lamprecht at proxmox.com
Tue Oct 18 15:35:14 CEST 2016


The cgroup value memory.usage_in_bytes includes the memory used by
file buffers and other caches, resolve this by getting the cache
value from the memory.stat file and substract it from
memory.usage_in_bytes when calculating the current memory usage of
the CT.
This results in the same value as a `free` call from the container
does (when not including the buffered data), at least with a free
version which uses data from /proc and not the sysinfo() syscall.

Addresses partly the bug #1139

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
CC: Wolfgang Bumiller <w.bumiller at proxmox.com>
---

CCing wolfgang for review

 src/PVE/LXC.pm | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 35ce796..535147f 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -192,8 +192,11 @@ sub vmstatus {
 	my $ctime = (stat("/proc/$pid"))[10]; # 10 = ctime
 	$d->{uptime} = time - $ctime; # the method lxcfs uses
 
-	$d->{mem} = read_cgroup_value('memory', $vmid, 'memory.usage_in_bytes');
-	$d->{swap} = read_cgroup_value('memory', $vmid, 'memory.memsw.usage_in_bytes') - $d->{mem};
+	my $memory_stat = read_cgroup_list('memory', $vmid, 'memory.stat');
+	my $mem_usage_in_bytes = read_cgroup_value('memory', $vmid, 'memory.usage_in_bytes');
+
+	$d->{mem} = $mem_usage_in_bytes - $memory_stat->{cache};
+	$d->{swap} = read_cgroup_value('memory', $vmid, 'memory.memsw.usage_in_bytes') - $mem_usage_in_bytes;
 
 	my $blkio_bytes = read_cgroup_value('blkio', $vmid, 'blkio.throttle.io_service_bytes', 1);
 	my @bytes = split(/\n/, $blkio_bytes);
@@ -251,6 +254,14 @@ sub vmstatus {
     return $list;
 }
 
+sub read_cgroup_list {
+    my ($group, $vmid, $name) = @_;
+
+    my $content = read_cgroup_value($group, $vmid, $name, 1);
+
+    return { split(/\s+/, $content) };
+}
+
 sub read_cgroup_value {
     my ($group, $vmid, $name, $full) = @_;
 
-- 
2.1.4





More information about the pve-devel mailing list