From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <p.hufnagl@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id AF91CE3DF
 for <pve-devel@lists.proxmox.com>; Tue, 18 Jul 2023 14:01:07 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 903661A30A
 for <pve-devel@lists.proxmox.com>; Tue, 18 Jul 2023 14:00:37 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Tue, 18 Jul 2023 14:00:37 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id CBF8943078
 for <pve-devel@lists.proxmox.com>; Tue, 18 Jul 2023 14:00:36 +0200 (CEST)
Message-ID: <edc36f4f-f830-fba6-696e-0db56c81d318@proxmox.com>
Date: Tue, 18 Jul 2023 14:00:28 +0200
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
 Thunderbird/102.12.0
To: pve-devel@lists.proxmox.com
References: <20230718115828.170254-1-p.hufnagl@proxmox.com>
Content-Language: en-US
From: Philipp Hufnagl <p.hufnagl@proxmox.com>
In-Reply-To: <20230718115828.170254-1-p.hufnagl@proxmox.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.048 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 NICE_REPLY_A           -0.097 Looks like a legit reply (A)
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 T_SCC_BODY_TEXT_LINE    -0.01 -
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [lxc.pm, pct.pm]
Subject: Re: [pve-devel] [PATCH pve-container] pct: fix cpu load calculation
 on command line
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 18 Jul 2023 12:01:07 -0000

Sorry forgott to tag as v2

On 7/18/23 13:58, Philipp Hufnagl wrote:
>   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
>
> fixes #4765
>
> 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..9fc171f 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} = {