This is an automated email from the ASF dual-hosted git repository.

pearl11594 pushed a commit to branch 4.20
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/4.20 by this push:
     new ee32f4cfe8c Add cpu speed detection methods (#9762)
ee32f4cfe8c is described below

commit ee32f4cfe8c76e9d71649d87994c9093c9feef84
Author: BartJM <[email protected]>
AuthorDate: Wed Feb 19 14:33:49 2025 +0100

    Add cpu speed detection methods (#9762)
    
    Added additional match for lscpu
    Added additional file to check
---
 .../apache/cloudstack/utils/linux/KVMHostInfo.java | 37 ++++++++++++++--------
 1 file changed, 24 insertions(+), 13 deletions(-)

diff --git 
a/plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/linux/KVMHostInfo.java
 
b/plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/linux/KVMHostInfo.java
index c0b416410cb..cee07421314 100644
--- 
a/plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/linux/KVMHostInfo.java
+++ 
b/plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/linux/KVMHostInfo.java
@@ -58,9 +58,8 @@ public class KVMHostInfo {
     private long reservedMemory;
     private long overCommitMemory;
     private List<String> capabilities = new ArrayList<>();
-
-    private static String cpuInfoFreqFileName = 
"/sys/devices/system/cpu/cpu0/cpufreq/base_frequency";
     private static String cpuArchCommand = "/usr/bin/arch";
+    private static List<String> cpuInfoFreqFileNames = 
List.of("/sys/devices/system/cpu/cpu0/cpufreq/base_frequency","/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq");
 
     public KVMHostInfo(long reservedMemory, long overCommitMemory, long 
manualSpeed, int reservedCpus) {
         this.cpuSpeed = manualSpeed;
@@ -134,29 +133,41 @@ public class KVMHostInfo {
     }
 
     private static long getCpuSpeedFromCommandLscpu() {
+        long speed = 0L;
+        LOGGER.info("Fetching CPU speed from command \"lscpu\".");
         try {
-            LOGGER.info("Fetching CPU speed from command \"lscpu\".");
             String command = "lscpu | grep -i 'Model name' | head -n 1 | egrep 
-o '[[:digit:]].[[:digit:]]+GHz' | sed 's/GHz//g'";
             String result = Script.runSimpleBashScript(command);
-            long speed = (long) (Float.parseFloat(result) * 1000);
+            speed = (long) (Float.parseFloat(result) * 1000);
             LOGGER.info(String.format("Command [%s] resulted in the value [%s] 
for CPU speed.", command, speed));
             return speed;
         } catch (NullPointerException | NumberFormatException e) {
             LOGGER.error(String.format("Unable to retrieve the CPU speed from 
lscpu."), e);
-            return 0L;
         }
+        try {
+            String command = "lscpu | grep -i 'CPU max MHz' | head -n 1 | sed 
's/^.*: //' | xargs";
+            String result = Script.runSimpleBashScript(command);
+            speed = (long) (Float.parseFloat(result));
+            LOGGER.info(String.format("Command [%s] resulted in the value [%s] 
for CPU speed.", command, speed));
+            return speed;
+        } catch (NullPointerException | NumberFormatException e) {
+            LOGGER.error(String.format("Unable to retrieve the CPU speed from 
lscpu."), e);
+        }
+        return speed;
     }
 
     private static long getCpuSpeedFromFile() {
-        LOGGER.info(String.format("Fetching CPU speed from file [%s].", 
cpuInfoFreqFileName));
-        try (Reader reader = new FileReader(cpuInfoFreqFileName)) {
-            Long cpuInfoFreq = Long.parseLong(IOUtils.toString(reader).trim());
-            LOGGER.info(String.format("Retrieved value [%s] from file [%s]. 
This corresponds to a CPU speed of [%s] MHz.", cpuInfoFreq, 
cpuInfoFreqFileName, cpuInfoFreq / 1000));
-            return cpuInfoFreq / 1000;
-        } catch (IOException | NumberFormatException e) {
-            LOGGER.error(String.format("Unable to retrieve the CPU speed from 
file [%s]", cpuInfoFreqFileName), e);
-            return 0L;
+        for (final String cpuInfoFreqFileName:  cpuInfoFreqFileNames) {
+            LOGGER.info(String.format("Fetching CPU speed from file [%s].", 
cpuInfoFreqFileName));
+            try (Reader reader = new FileReader(cpuInfoFreqFileName)) {
+                Long cpuInfoFreq = 
Long.parseLong(IOUtils.toString(reader).trim());
+                LOGGER.info(String.format("Retrieved value [%s] from file 
[%s]. This corresponds to a CPU speed of [%s] MHz.", cpuInfoFreq, 
cpuInfoFreqFileName, cpuInfoFreq / 1000));
+                return cpuInfoFreq / 1000;
+            } catch (IOException | NumberFormatException e) {
+                LOGGER.error(String.format("Unable to retrieve the CPU speed 
from file [%s]", cpuInfoFreqFileName), e);
+            }
         }
+        return 0L;
     }
 
     protected static long getCpuSpeedFromHostCapabilities(final String 
capabilities) {

Reply via email to