]> git.proxmox.com Git - pve-container.git/commitdiff
vmstatus: fix memory usage value including cache sizes
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 18 Oct 2016 13:35:14 +0000 (15:35 +0200)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Fri, 21 Oct 2016 13:28:20 +0000 (15:28 +0200)
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@proxmox.com>
CC: Wolfgang Bumiller <w.bumiller@proxmox.com>
src/PVE/LXC.pm

index 35ce7962759810f5b976ba93d60ec89665c67cda..535147fa97a2c45c7d53546ecbe7e195f3753d96 100644 (file)
@@ -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) = @_;