public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Philipp Hufnagl <p.hufnagl@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH pve-container] ptc: fix cpu load calculation on command
Date: Tue, 18 Jul 2023 13:07:07 +0200	[thread overview]
Message-ID: <20230718110707.142459-1-p.hufnagl@proxmox.com> (raw)

 When called from the command line, it was not possible to calculate
 cpu load because there was no 2nd data point available for the
 calculation. Now (when called) from the command line, cpu stats will
 be fetched twice with a minimum delta of 20ms. That way the load can
 be calculated

Signed-off-by: Philipp Hufnagl <p.hufnagl@proxmox.com>
---
 src/PVE/CLI/pct.pm |  4 ++--
 src/PVE/LXC.pm     | 32 +++++++++++++++++++++++++++++---
 2 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/src/PVE/CLI/pct.pm b/src/PVE/CLI/pct.pm
index ff75d33..e531b27 100755
--- a/src/PVE/CLI/pct.pm
+++ b/src/PVE/CLI/pct.pm
@@ -60,8 +60,8 @@ __PACKAGE__->register_method ({
 
 	# test if CT exists
 	my $conf = PVE::LXC::Config->load_config ($param->{vmid});
-
-	my $vmstatus = PVE::LXC::vmstatus($param->{vmid});
+	# workaround to get cpu usage is to fetch cpu stats twice with a delay
+	my $vmstatus = PVE::LXC::vmstatus($param->{vmid}, 20);
 	my $stat = $vmstatus->{$param->{vmid}};
 	if ($param->{verbose}) {
 	    foreach my $k (sort (keys %$stat)) {
diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index a531ea5..cd8a219 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -12,7 +12,7 @@ use IO::Poll qw(POLLIN POLLHUP);
 use IO::Socket::UNIX;
 use POSIX qw(EINTR);
 use Socket;
-use Time::HiRes qw (gettimeofday);
+use Time::HiRes qw (gettimeofday usleep);
 
 use PVE::AccessControl;
 use PVE::CGroup;
@@ -171,11 +171,37 @@ our $vmstatus_return_properties = {
     }
 };
 
+sub get_first_cpu {
+    my ($list, $measure_timespan_ms) = @_;
+    my $cdtime = gettimeofday;
+
+    foreach my $vmid (keys %$list) {
+	my $cgroups = PVE::LXC::CGroup->new($vmid);
+	if (defined(my $cpu = $cgroups->get_cpu_stat())) {
+	    # Total time (in milliseconds) used up by the cpu.
+	    my $used_ms = $cpu->{utime} + $cpu->{stime};
+	    $last_proc_vmid_stat->{$vmid} = {
+		    time => $cdtime,
+		    used => $used_ms,
+		    cpu => 0,
+	    };
+	}
+    }
+    usleep($measure_timespan_ms * 1000);
+}
+
 sub vmstatus {
-    my ($opt_vmid) = @_;
+    my ($opt_vmid, $measure_timespan_ms) = @_;
 
     my $list = $opt_vmid ? { $opt_vmid => { type => 'lxc', vmid => int($opt_vmid) }} : config_list();
 
+	if (defined($measure_timespan_ms))
+	{
+	    get_first_cpu($list, $measure_timespan_ms);
+	}
+
+	$measure_timespan_ms //= 1000;
+
     my $active_hash = list_active_containers();
 
     my $cpucount = $cpuinfo->{cpus} || 1;
@@ -285,7 +311,7 @@ sub vmstatus {
 	    }
 
 	    my $delta_ms = ($cdtime - $old->{time}) * $cpucount * 1000.0;
-	    if ($delta_ms > 1000.0) {
+	    if ($delta_ms > $measure_timespan_ms) {
 		my $delta_used_ms = $used_ms - $old->{used};
 		$d->{cpu} = (($delta_used_ms / $delta_ms) * $cpucount) / $d->{cpus};
 		$last_proc_vmid_stat->{$vmid} = {
-- 
2.39.2





                 reply	other threads:[~2023-07-18 11:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230718110707.142459-1-p.hufnagl@proxmox.com \
    --to=p.hufnagl@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal