[pve-devel] [PATCH_V2 pve-container] fix bug #770: CPU usage stats for containers

Wolfgang Link w.link at proxmox.com
Thu Oct 29 13:11:07 CET 2015


---
 src/PVE/LXC.pm | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 0fd2ec8..e3160e4 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -18,11 +18,14 @@ use PVE::Tools qw($IPV6RE $IPV4RE dir_glob_foreach);
 use PVE::Network;
 use PVE::AccessControl;
 use PVE::ProcFSTools;
+use Time::HiRes qw (gettimeofday);
 
 use Data::Dumper;
 
 my $nodename = PVE::INotify::nodename();
 
+my $cpuinfo= PVE::ProcFSTools::read_cpuinfo();
+
 cfs_register_file('/lxc/', \&parse_pct_config, \&write_pct_config);
 
 my $rootfs_desc = {
@@ -761,6 +764,25 @@ sub get_container_disk_usage {
     return $res;
 }
 
+my $last_proc_vmid_stat;
+
+my $parse_cpuacct_stat = sub {
+    my ($vmid) = @_;
+
+    my $raw = read_cgroup_value('cpuacct', $vmid, 'cpuacct.stat', 1);
+
+    my $stat = {};
+
+    if ($raw =~ m/^user (\d+)\nsystem (\d+)\n/) {
+
+	$stat->{utime} = $1;
+	$stat->{stime} = $2;
+
+    }
+
+    return $stat;
+};
+
 sub vmstatus {
     my ($opt_vmid) = @_;
 
@@ -768,6 +790,12 @@ sub vmstatus {
 
     my $active_hash = list_active_containers();
 
+    my $cpucount = $cpuinfo->{cpus} || 1;
+
+    my $cdtime = gettimeofday;
+
+    my $uptime = (PVE::ProcFSTools::read_proc_uptime(1))[0];
+
     foreach my $vmid (keys %$list) {
 	my $d = $list->{$vmid};
 
@@ -834,6 +862,35 @@ sub vmstatus {
 		$d->{diskwrite} = $2 if $key eq 'Write';
 	    }
 	}
+
+	my $pstat = &$parse_cpuacct_stat($vmid);
+
+	my $used = $pstat->{utime} + $pstat->{stime};
+
+	my $old = $last_proc_vmid_stat->{$vmid};
+	if (!$old) {
+	    $last_proc_vmid_stat->{$vmid} = {
+		time => $cdtime,
+		used => $used,
+		cpu => 0,
+	    };
+	    next;
+	}
+
+	my $dtime = ($cdtime -  $old->{time}) * $cpucount * $cpuinfo->{user_hz};
+
+	if ($dtime > 1000) {
+	    my $dutime = $used -  $old->{used};
+
+	    $d->{cpu} = (($dutime/$dtime)* $cpucount) / $d->{cpus};
+	    $last_proc_vmid_stat->{$vmid} = {
+		time => $cdtime,
+		used => $used,
+		cpu => $d->{cpu},
+	    };
+	} else {
+	    $d->{cpu} = $old->{cpu};
+	}
     }
 
     my $netdev = PVE::ProcFSTools::read_proc_net_dev();
-- 
2.1.4





More information about the pve-devel mailing list