]> git.proxmox.com Git - pve-common.git/commitdiff
use unified parser for pressure stats
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 8 Feb 2021 15:03:35 +0000 (16:03 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 8 Feb 2021 15:03:35 +0000 (16:03 +0100)
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
src/PVE/CGroup.pm
src/PVE/ProcFSTools.pm

index cbd77cbc9b733dec1048b519bb086c3db19ebac1..bf6c66d7736863451105e7b1a4444a7e6d600b5d 100644 (file)
@@ -387,18 +387,9 @@ sub get_pressure_stat {
        # container or VM most likely isn't running
        return undef;
     } elsif ($ver == 2) {
-
-       foreach my $type (qw(cpu memory io)) {
-           if (my $fh = IO::File->new ("$path/$type.pressure", "r")) {
-               while (defined (my $line = <$fh>)) {
-                   if ($line =~ /^(some|full)\s+avg10\=(\d+\.\d+)\s+avg60\=(\d+\.\d+)\s+avg300\=(\d+\.\d+)\s+total\=(\d+)/) {
-                       $res->{$type}->{$1}->{avg10} = $2;
-                       $res->{$type}->{$1}->{avg60} = $3;
-                       $res->{$type}->{$1}->{avg300} = $4;
-                   }
-               }
-               $fh->close;
-           }
+       for my $type (qw(cpu memory io)) {
+           my $stats = PVE::ProcFSTools::parse_pressure("$path/$type.pressure");
+           $res->{$type} = $stats if $stats;
        }
     } else {
        die "bad cgroup version: $ver\n";
index 7687c13c4e30b9d483ebb624c8d522ca8a74ca3d..cbd576813f7d6eb20d40c03c3beef19aa0b36a91 100644 (file)
@@ -132,21 +132,30 @@ sub read_loadavg {
     return wantarray ? (0, 0, 0) : 0;
 }
 
-sub read_pressure {
+sub parse_pressure {
+    my ($path) = @_;
 
     my $res = {};
-    foreach my $type (qw(cpu memory io)) {
-       if (my $fh = IO::File->new ("/proc/pressure/$type", "r")) {
-           while (defined (my $line = <$fh>)) {
-               if ($line =~ /^(some|full)\s+avg10\=(\d+\.\d+)\s+avg60\=(\d+\.\d+)\s+avg300\=(\d+\.\d+)\s+total\=(\d+)/) {
-                   $res->{$type}->{$1}->{avg10} = $2;
-                   $res->{$type}->{$1}->{avg60} = $3;
-                   $res->{$type}->{$1}->{avg300} = $4;
-               }
-           }
-           $fh->close;
+    my $v = qr/\d+\.\d+/;
+    my $fh = IO::File->new($path, "r") or return undef;
+    while (defined (my $line = <$fh>)) {
+       if ($line =~ /^(some|full)\s+avg10\=($v)\s+avg60\=($v)\s+avg300\=($v)\s+total\=(\d+)/) {
+           $res->{$1}->{avg10} = $2;
+           $res->{$1}->{avg60} = $3;
+           $res->{$1}->{avg300} = $4;
+           $res->{$1}->{total} = $4;
        }
     }
+    $fh->close;
+    return $res;
+}
+
+sub read_pressure {
+    my $res = {};
+    foreach my $type (qw(cpu memory io)) {
+       my $stats = parse_pressure("/proc/pressure/$type");
+       $res->{$type} = $stats if $stats;
+    }
     return $res;
 }