public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Alexandre Derumier <aderumier@odiso.com>
To: pve-devel@lists.proxmox.com
Cc: t.lamprecht@proxmox.com, Alexandre Derumier <aderumier@odiso.com>
Subject: [pve-devel] [PATCH pve-manager 3/4] pvestatd: qemu/lxc/node : add hostcpu/hostmem average stats
Date: Wed,  1 Jun 2022 10:12:51 +0200	[thread overview]
Message-ID: <20220601081253.2542697-9-aderumier@odiso.com> (raw)
In-Reply-To: <20220601081253.2542697-1-aderumier@odiso.com>

we aggregate each last X second stats to 1min average
we aggragate each last 5 1min average to 5min average.

vm avgstats are resetted when vm is stopped or removed.

Signed-off-by: Alexandre Derumier <aderumier@odiso.com>
---
 PVE/Service/pvestatd.pm | 94 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 93 insertions(+), 1 deletion(-)

diff --git a/PVE/Service/pvestatd.pm b/PVE/Service/pvestatd.pm
index dd578d6b..a33b21cd 100755
--- a/PVE/Service/pvestatd.pm
+++ b/PVE/Service/pvestatd.pm
@@ -81,6 +81,7 @@ my $next_flag_update_time;
 my $failed_flag_update_delay_sec = 120;
 my $balancer_stats = {};
 my $last_balancer_broadcast_time = 0;
+my $avgstats = {};
 
 sub update_supported_cpuflags {
     my $kvm_version = PVE::QemuServer::kvm_user_version();
@@ -177,6 +178,11 @@ sub update_node_status {
     $node_metric->{cpustat}->{cpus} = $maxcpu;
 
     compute_pressure($ctime, $node_metric, 'node', $nodename);
+    my $avg_metrics = {
+	cpu => $stat->{cpu},
+        mem => $meminfo->{memused},
+    };
+    compute_avg_metrics($ctime, $avg_metrics, 'node', $nodename);
 
     my $transactions = PVE::ExtMetric::transactions_start($status_cfg);
     PVE::ExtMetric::update_all($transactions, 'node', $nodename, $node_metric, $ctime);
@@ -233,6 +239,11 @@ sub update_qemu_status {
 		 $d->{netin}, $d->{netout}, $d->{diskread}, $d->{diskwrite}]);
 
 	    compute_pressure($ctime, $d, 'qemu', $vmid);
+	    my $avg_metrics = {
+		cpu => $d->{hostcpu},
+		mem => $d->{hostmem},
+	    };
+	    compute_avg_metrics($ctime, $avg_metrics, 'qemu', $vmid);
 	} else {
 	    $data = $generate_rrd_string->(
 		[0, $d->{name}, $status, $template, $ctime, $d->{cpus}, undef,
@@ -242,7 +253,7 @@ sub update_qemu_status {
 
 	PVE::ExtMetric::update_all($transactions, 'qemu', $vmid, $d, $ctime, $nodename);
     }
-
+    delete_old_qemu_avgstats($vmstatus);
     PVE::ExtMetric::transactions_finish($transactions);
 }
 
@@ -443,6 +454,11 @@ sub update_lxc_status {
 		 $d->{netin}, $d->{netout},
 		 $d->{diskread}, $d->{diskwrite}]);
 	    compute_pressure($ctime, $d, 'lxc', $vmid);
+	    my $avg_metrics = {
+		cpu => $d->{cpu},
+		mem => $d->{mem},
+	    };
+	    compute_avg_metrics($ctime, $avg_metrics, 'lxc', $vmid);
 	} else {
 	    $data = $generate_rrd_string->(
 		[0, $d->{name}, $d->{status}, $template, $ctime, $d->{cpus}, undef,
@@ -452,6 +468,7 @@ sub update_lxc_status {
 
 	PVE::ExtMetric::update_all($transactions, 'lxc', $vmid, $d, $ctime, $nodename);
     }
+    delete_old_lxc_avgstats($vmstatus);
     PVE::ExtMetric::transactions_finish($transactions);
 }
 
@@ -541,6 +558,81 @@ sub compute_pressure {
     delete $d->{pressure};
 }
 
+sub compute_avg_metrics {
+   my ($ctime, $avg_metrics, $objectype, $id) = @_;
+
+    foreach my $metric (keys %$avg_metrics) {
+	my $value = $avg_metrics->{$metric};
+	next if !defined($value);
+
+        my $stats = $avgstats->{$objectype}->{$id}->{$metric} || {};
+	$stats->{series}->{60}->{$ctime} = $value;
+	$stats->{avg60} = 0 if !defined($stats->{avg60});
+	$stats->{avg300} = 0 if !defined($stats->{avg300});
+	$stats->{last_compute_time} = $ctime if !defined($stats->{last_compute_time});
+
+	#compute avg each minute
+
+	if($stats->{last_compute_time} && $ctime >= $stats->{last_compute_time} + 60) {
+	    $stats->{avg60} = compute_avg($stats->{series}->{60}, $ctime, 60);
+	    $stats->{series}->{300}->{$ctime} = $stats->{avg60};
+	    $stats->{avg300} = compute_avg($stats->{series}->{300}, $ctime, 300);
+	    $stats->{last_compute_time} = $ctime;
+	}
+
+	$balancer_stats->{$objectype}->{$id}->{$metric}->{avg60} = $stats->{avg60};
+	$balancer_stats->{$objectype}->{$id}->{$metric}->{avg300} = $stats->{avg300};
+	$avgstats->{$objectype}->{$id}->{$metric} = $stats;
+    }
+}
+
+sub compute_avg {
+   my ($series, $ctime, $delta) = @_;
+
+    my $total = 0;
+    my $count = 0;
+    my $to_delete;
+
+    foreach my $time (keys %$series) {
+	if ($ctime - $delta >= $time) {
+	    $to_delete->{$time} = 1;
+	    next;
+	}
+	$count++;
+	$total += $series->{$time};
+    }
+
+    delete %$series{keys %{$to_delete}};
+
+    my $avg = $total / $count if $count > 0;
+    return $avg;
+}
+
+sub delete_old_qemu_avgstats {
+    my ($vmstatus) = @_;
+
+    my $stats = $avgstats->{'qemu'};
+
+    my $to_delete;
+    #delete stats for removed vm, or stopped vm
+    foreach my $vmid (keys %$stats) {
+	$to_delete->{$vmid} = 1 if !defined($vmstatus->{$vmid}) || !$vmstatus->{$vmid}->{pid};
+    }
+    delete %$stats{keys %{$to_delete}};
+}
+
+sub delete_old_lxc_avgstats {
+    my ($vmstatus) = @_;
+
+    my $stats = $avgstats->{'lxc'};
+
+    my $to_delete;
+    #delete stats for removed ct, or stopped ct
+    foreach my $vmid (keys %$stats) {
+	$to_delete->{$vmid} = 1 if !defined($vmstatus->{$vmid}) || $vmstatus->{$vmid}->{status} ne 'running';
+    }
+    delete %$stats{keys %{$to_delete}};
+}
 my $broadcast_version_info_done = 0;
 my sub broadcast_version_info : prototype() {
     if (!$broadcast_version_info_done) {
-- 
2.30.2




  parent reply	other threads:[~2022-06-01  8:13 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-01  8:12 [pve-devel] [PATCH-SERIES common/qemu-server/lxc/manager] brodcast new metric stats in kvstore Alexandre Derumier
2022-06-01  8:12 ` [pve-devel] [PATCH pve-cluster 1/1] add pve2-metrics rrd (single metrics) Alexandre Derumier
2022-06-01  8:12 ` [pve-devel] [PATCH pve-common 1/2] cgroup: get_pressure_stat: use controllers in get_path Alexandre Derumier
2022-06-01  8:12 ` [pve-devel] [PATCH pve-manager 1/4] pvestatd: add broadcast_balancer_stats Alexandre Derumier
2022-06-01  8:12 ` [pve-devel] [PATCH pve-manager 1/3] pvestatd: qemu/lxc/host : broadcast rrd pressure metrics Alexandre Derumier
2022-06-01  8:12 ` [pve-devel] [PATCH pve-common 2/2] cgroup: get_pressure_stat: add cpu full pressure Alexandre Derumier
2022-06-01  8:12 ` [pve-devel] [PATCH pve-manager 2/4] pvestatd: qemu/lxc/node : add pressure stats Alexandre Derumier
2022-06-01  8:12 ` [pve-devel] [PATCH qemu-server 2/3] vmstatus: add hostmem value Alexandre Derumier
2022-06-01  8:12 ` Alexandre Derumier [this message]
2022-06-01  8:12 ` [pve-devel] [PATCH qemu-server 3/3] vmstatus: add pressure stats Alexandre Derumier
2022-06-01  8:12 ` [pve-devel] [PATCH pve-manager 4/4] pvestatd: node : broadcast ksm value Alexandre Derumier

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=20220601081253.2542697-9-aderumier@odiso.com \
    --to=aderumier@odiso.com \
    --cc=pve-devel@lists.proxmox.com \
    --cc=t.lamprecht@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