public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pve-devel] [PATCH pve-common 0/4] improve host read_proc_stat
@ 2022-01-10  4:52 Alexandre Derumier
  2022-01-10  4:52 ` [pve-devel] [PATCH pve-common 1/4] read_proc_stat : initialize newer fields to 0 Alexandre Derumier
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Alexandre Derumier @ 2022-01-10  4:52 UTC (permalink / raw)
  To: pve-devel

This patch series improve current host cpu stats


Alexandre Derumier (4):
  read_proc_stat : initialize newer fields to 0
  read_proc_stat: substract guest && guest_nice from user && nice time
  read_proc_stat: add irq/softirq/steal to total used cpu
  read_proc_stat: use total of fields to compute percentage

 src/PVE/ProcFSTools.pm | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

-- 
2.30.2




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] [PATCH pve-common 1/4] read_proc_stat : initialize newer fields to 0
  2022-01-10  4:52 [pve-devel] [PATCH pve-common 0/4] improve host read_proc_stat Alexandre Derumier
@ 2022-01-10  4:52 ` Alexandre Derumier
  2022-01-10  4:52 ` [pve-devel] [PATCH pve-common 2/4] read_proc_stat: substract guest && guest_nice from user && nice time Alexandre Derumier
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Alexandre Derumier @ 2022-01-10  4:52 UTC (permalink / raw)
  To: pve-devel

new fields has been added recently, but values are not initialized
https://git.proxmox.com/?p=pve-common.git;a=commit;h=5a82eb712e4c879a271686f07c589fadc0b09185

as total of all fields is compute later, this can give undef values
---
 src/PVE/ProcFSTools.pm | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/PVE/ProcFSTools.pm b/src/PVE/ProcFSTools.pm
index a75274a..c816131 100644
--- a/src/PVE/ProcFSTools.pm
+++ b/src/PVE/ProcFSTools.pm
@@ -162,7 +162,7 @@ sub read_pressure {
 my $last_proc_stat;
 
 sub read_proc_stat {
-    my $res = { user => 0, nice => 0, system => 0, idle => 0 , sum => 0};
+    my $res = { user => 0, nice => 0, system => 0, idle => 0 , iowait => 0, irq => 0, softirq => 0, steal => 0, guest => 0, guest_nice => 0, sum => 0};
 
     my $cpucount = 0;
 
-- 
2.30.2




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] [PATCH pve-common 2/4] read_proc_stat: substract guest && guest_nice from user && nice time
  2022-01-10  4:52 [pve-devel] [PATCH pve-common 0/4] improve host read_proc_stat Alexandre Derumier
  2022-01-10  4:52 ` [pve-devel] [PATCH pve-common 1/4] read_proc_stat : initialize newer fields to 0 Alexandre Derumier
@ 2022-01-10  4:52 ` Alexandre Derumier
  2022-01-10  4:52 ` [pve-devel] [PATCH pve-common 3/4] read_proc_stat: add irq/softirq/steal to total used cpu Alexandre Derumier
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Alexandre Derumier @ 2022-01-10  4:52 UTC (permalink / raw)
  To: pve-devel

user && nice time already include guest value
https://github.com/torvalds/linux/blob/4ec9f7a18/kernel/sched/cputime.c#L151-L158

Other monitoring tools are already substracting theses guest values

https://github.com/htop-dev/htop/blob/main/linux/LinuxProcessList.c
https://github.com/influxdata/telegraf/blob/c66ccee46f47717c399ccc0348d17c95d11f477d/plugins/inputs/cpu/cpu.go
---
 src/PVE/ProcFSTools.pm | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/PVE/ProcFSTools.pm b/src/PVE/ProcFSTools.pm
index c816131..79d4a4c 100644
--- a/src/PVE/ProcFSTools.pm
+++ b/src/PVE/ProcFSTools.pm
@@ -169,8 +169,8 @@ sub read_proc_stat {
     if (my $fh = IO::File->new ("/proc/stat", "r")) {
 	while (defined (my $line = <$fh>)) {
 	    if ($line =~ m|^cpu\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)|) {
-		$res->{user} = $1;
-		$res->{nice} = $2;
+		$res->{user} = $1 - $9;
+		$res->{nice} = $2 - $10;
 		$res->{system} = $3;
 		$res->{idle} = $4;
 		$res->{used} = $1+$2+$3;
@@ -191,7 +191,7 @@ sub read_proc_stat {
 
     my $ctime = gettimeofday; # floating point time in seconds
 
-    # the sum of all (non-guest) fields
+    # the sum of all fields
     $res->{total} = $res->{user}
 	+ $res->{nice}
 	+ $res->{system}
@@ -199,7 +199,9 @@ sub read_proc_stat {
 	+ $res->{irq}
 	+ $res->{softirq}
 	+ $res->{steal}
-	+ $res->{idle};
+	+ $res->{idle}
+	+ $res->{guest}
+	+ $res->{guest_nice};
 
     $res->{ctime} = $ctime;
     $res->{cpu} = 0;
-- 
2.30.2




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] [PATCH pve-common 3/4] read_proc_stat: add irq/softirq/steal to total used cpu
  2022-01-10  4:52 [pve-devel] [PATCH pve-common 0/4] improve host read_proc_stat Alexandre Derumier
  2022-01-10  4:52 ` [pve-devel] [PATCH pve-common 1/4] read_proc_stat : initialize newer fields to 0 Alexandre Derumier
  2022-01-10  4:52 ` [pve-devel] [PATCH pve-common 2/4] read_proc_stat: substract guest && guest_nice from user && nice time Alexandre Derumier
@ 2022-01-10  4:52 ` Alexandre Derumier
  2022-01-10  4:53 ` [pve-devel] [PATCH pve-common 4/4] read_proc_stat: use total of fields to compute percentage Alexandre Derumier
  2022-01-13 15:33 ` [pve-devel] applied-series: [PATCH pve-common 0/4] improve host read_proc_stat Thomas Lamprecht
  4 siblings, 0 replies; 6+ messages in thread
From: Alexandre Derumier @ 2022-01-10  4:52 UTC (permalink / raw)
  To: pve-devel

---
 src/PVE/ProcFSTools.pm | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/PVE/ProcFSTools.pm b/src/PVE/ProcFSTools.pm
index 79d4a4c..10c0cbd 100644
--- a/src/PVE/ProcFSTools.pm
+++ b/src/PVE/ProcFSTools.pm
@@ -173,7 +173,7 @@ sub read_proc_stat {
 		$res->{nice} = $2 - $10;
 		$res->{system} = $3;
 		$res->{idle} = $4;
-		$res->{used} = $1+$2+$3;
+		$res->{used} = $1+$2+$3+$6+$7+$8;
 		$res->{iowait} = $5;
 		$res->{irq} = $6;
 		$res->{softirq} = $7;
-- 
2.30.2




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] [PATCH pve-common 4/4] read_proc_stat: use total of fields to compute percentage
  2022-01-10  4:52 [pve-devel] [PATCH pve-common 0/4] improve host read_proc_stat Alexandre Derumier
                   ` (2 preceding siblings ...)
  2022-01-10  4:52 ` [pve-devel] [PATCH pve-common 3/4] read_proc_stat: add irq/softirq/steal to total used cpu Alexandre Derumier
@ 2022-01-10  4:53 ` Alexandre Derumier
  2022-01-13 15:33 ` [pve-devel] applied-series: [PATCH pve-common 0/4] improve host read_proc_stat Thomas Lamprecht
  4 siblings, 0 replies; 6+ messages in thread
From: Alexandre Derumier @ 2022-01-10  4:53 UTC (permalink / raw)
  To: pve-devel

---
 src/PVE/ProcFSTools.pm | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/PVE/ProcFSTools.pm b/src/PVE/ProcFSTools.pm
index 10c0cbd..88df5d2 100644
--- a/src/PVE/ProcFSTools.pm
+++ b/src/PVE/ProcFSTools.pm
@@ -214,11 +214,15 @@ sub read_proc_stat {
     if ($diff > 1000) { # don't update too often
 	my $useddiff =  $res->{used} - $last_proc_stat->{used};
 	$useddiff = $diff if $useddiff > $diff;
-	$res->{cpu} = $useddiff/$diff;
+
+	my $totaldiff = $res->{total} - $last_proc_stat->{total};
+	$totaldiff = $diff if $totaldiff > $diff;
+
+	$res->{cpu} = $useddiff/$totaldiff;
 
 	my $waitdiff =  $res->{iowait} - $last_proc_stat->{iowait};
 	$waitdiff = $diff if $waitdiff > $diff;
-	$res->{wait} = $waitdiff/$diff;
+	$res->{wait} = $waitdiff/$totaldiff;
 
 	$last_proc_stat = $res;
     } else {
-- 
2.30.2




^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] applied-series: [PATCH pve-common 0/4] improve host read_proc_stat
  2022-01-10  4:52 [pve-devel] [PATCH pve-common 0/4] improve host read_proc_stat Alexandre Derumier
                   ` (3 preceding siblings ...)
  2022-01-10  4:53 ` [pve-devel] [PATCH pve-common 4/4] read_proc_stat: use total of fields to compute percentage Alexandre Derumier
@ 2022-01-13 15:33 ` Thomas Lamprecht
  4 siblings, 0 replies; 6+ messages in thread
From: Thomas Lamprecht @ 2022-01-13 15:33 UTC (permalink / raw)
  To: Proxmox VE development discussion, Alexandre Derumier

On 10.01.22 05:52, Alexandre Derumier wrote:
> This patch series improve current host cpu stats
> 
> 
> Alexandre Derumier (4):
>   read_proc_stat : initialize newer fields to 0
>   read_proc_stat: substract guest && guest_nice from user && nice time
>   read_proc_stat: add irq/softirq/steal to total used cpu
>   read_proc_stat: use total of fields to compute percentage
> 
>  src/PVE/ProcFSTools.pm | 22 ++++++++++++++--------
>  1 file changed, 14 insertions(+), 8 deletions(-)
> 

applied series, thanks!




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-01-13 15:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-10  4:52 [pve-devel] [PATCH pve-common 0/4] improve host read_proc_stat Alexandre Derumier
2022-01-10  4:52 ` [pve-devel] [PATCH pve-common 1/4] read_proc_stat : initialize newer fields to 0 Alexandre Derumier
2022-01-10  4:52 ` [pve-devel] [PATCH pve-common 2/4] read_proc_stat: substract guest && guest_nice from user && nice time Alexandre Derumier
2022-01-10  4:52 ` [pve-devel] [PATCH pve-common 3/4] read_proc_stat: add irq/softirq/steal to total used cpu Alexandre Derumier
2022-01-10  4:53 ` [pve-devel] [PATCH pve-common 4/4] read_proc_stat: use total of fields to compute percentage Alexandre Derumier
2022-01-13 15:33 ` [pve-devel] applied-series: [PATCH pve-common 0/4] improve host read_proc_stat Thomas Lamprecht

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