Add cpu core count to /nodes/{id}/status API result

This is useful for when collecting data for Guest OS licensing (Microsoft)
where physical core count matters, not socket or thread count.

The "cpus" value counts threads when Hyperthreading is enabled.

Signed-off-by: Mike Beattie <mike@ethernal.org>
This commit is contained in:
Mike Beattie 2020-05-29 22:58:52 +12:00 committed by Thomas Lamprecht
parent aa91ae3d1b
commit ddcdc5eed7

View File

@ -5,6 +5,7 @@ use warnings;
use POSIX; use POSIX;
use Time::HiRes qw (gettimeofday); use Time::HiRes qw (gettimeofday);
use IO::File; use IO::File;
use List::Util qw(sum);
use PVE::Tools; use PVE::Tools;
use Cwd qw(); use Cwd qw();
@ -35,6 +36,7 @@ sub read_cpuinfo {
my $fh = IO::File->new ($fn, "r"); my $fh = IO::File->new ($fn, "r");
return $res if !$fh; return $res if !$fh;
my $cpuid = 0;
my $idhash = {}; my $idhash = {};
my $count = 0; my $count = 0;
while (defined(my $line = <$fh>)) { while (defined(my $line = <$fh>)) {
@ -47,7 +49,10 @@ sub read_cpuinfo {
} elsif ($line =~ m/^flags\s*:\s*(.*)$/) { } elsif ($line =~ m/^flags\s*:\s*(.*)$/) {
$res->{flags} = $1 if !length $res->{flags}; $res->{flags} = $1 if !length $res->{flags};
} elsif ($line =~ m/^physical id\s*:\s*(\d+)\s*$/i) { } elsif ($line =~ m/^physical id\s*:\s*(\d+)\s*$/i) {
$idhash->{$1} = 1; $cpuid = $1;
$idhash->{$1} = 1 if not defined($idhash->{$1});
} elsif ($line =~ m/^cpu cores\s*:\s*(\d+)\s*$/i) {
$idhash->{$cpuid} = $1 if defined($idhash->{$cpuid});
} }
} }
@ -56,6 +61,8 @@ sub read_cpuinfo {
$res->{sockets} = scalar(keys %$idhash) || 1; $res->{sockets} = scalar(keys %$idhash) || 1;
$res->{cores} = sum(values %$idhash) || 1;
$res->{cpus} = $count; $res->{cpus} = $count;
$fh->close; $fh->close;