[pve-devel] [PATCH] .

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


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

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 4b9ace5..4d8446c 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 = PVE::ProcFSTools::read_cpuinfo() || 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,49 @@ 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();
+
+    foreach my $dev (keys %$netdev) {
+	next if $dev !~ m/^veth([1-9]\d*)i/;
+	my $vmid = $1;
+	my $d = $list->{$vmid};
+
+	next if !$d;
+
+	$d->{netout} += $netdev->{$dev}->{receive};
+	$d->{netin} += $netdev->{$dev}->{transmit};
+
     }
 
     return $list;
-- 
2.1.4





More information about the pve-devel mailing list