[pve-devel] [PATCH pve-common 1/1] read_cpuinfo: add msr support

Alexandre Derumier aderumier at odiso.com
Mon May 22 12:25:22 CEST 2023


Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 src/PVE/ProcFSTools.pm | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/src/PVE/ProcFSTools.pm b/src/PVE/ProcFSTools.pm
index 3826fcc..f491452 100644
--- a/src/PVE/ProcFSTools.pm
+++ b/src/PVE/ProcFSTools.pm
@@ -21,12 +21,15 @@ my $clock_ticks = POSIX::sysconf(&POSIX::_SC_CLK_TCK);
 my $cpuinfo;
 
 sub read_cpuinfo {
+    my ($msr) = @_;
+
     my $fn = '/proc/cpuinfo';
 
     return $cpuinfo if $cpuinfo;
 
     my $res = {
 	user_hz => $clock_ticks,
+	vendor => 'unknown',
 	model => 'unknown',
 	mhz => 0,
 	cpus => 1,
@@ -45,6 +48,8 @@ sub read_cpuinfo {
 	    $count++;
 	} elsif ($line =~ m/^model\s+name\s*:\s*(.*)\s*$/i) {
 	    $res->{model} = $1 if $res->{model} eq 'unknown';
+	} elsif ($line =~ m/^vendor_id\s*:\s*(.*)\s*$/i) {
+	    $res->{vendor} = $1 if $res->{vendor} eq 'unknown';
 	} elsif ($line =~ m/^cpu\s+MHz\s*:\s*(\d+\.\d+)\s*$/i) {
 	    $res->{mhz} = $1 if !$res->{mhz};
 	} elsif ($line =~ m/^flags\s*:\s*(.*)$/) {
@@ -70,9 +75,45 @@ sub read_cpuinfo {
 
     $cpuinfo = $res;
 
+    return $res if !$msr;
+
+    if ($cpuinfo->{vendor} eq 'GenuineIntel') {
+	$res->{msr} = read_cpu_intel_msr();
+    }
+
     return $res;
 }
 
+sub read_msr {
+    my ($fh, $offset) = @_;
+
+    my $value = undef;
+    seek $fh, $offset,0;
+    read $fh, $value, 8;
+    return unpack("Q", $value) if $value;
+}
+
+sub read_cpu_intel_msr {
+    my $msr = {};
+    my $spectr_msr = undef;
+    my $capabilities_msr = undef;
+    my $fn = '/dev/cpu/0/msr';
+
+    if (!-d $fn) {
+	eval {
+	    system("/sbin/modprobe msr >/dev/null 2>/dev/null");
+	};
+    }
+    return if !$fn;
+
+    my $fh = IO::File->new ($fn, "r");
+    return if !$fh;
+    $msr->{'spec-ctrl'} = read_msr($fh, 0x48);
+    $msr->{'arch-capabilities'} = read_msr($fh, 0x10a);
+    close $fh;
+    return $msr;
+}
+
 sub read_proc_uptime {
     my $ticks = shift;
 
-- 
2.30.2





More information about the pve-devel mailing list