GutoVeronezi commented on a change in pull request #5506:
URL: https://github.com/apache/cloudstack/pull/5506#discussion_r796829292



##########
File path: 
plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/linux/KVMHostInfo.java
##########
@@ -80,16 +82,39 @@ public long getOverCommitMemory() {
     }
 
     protected static long getCpuSpeed(final NodeInfo nodeInfo) {
-        try (Reader reader = new FileReader(cpuInfoMaxFreqFileName)) {
-            Long cpuInfoMaxFreq = 
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.", cpuInfoMaxFreq, 
cpuInfoMaxFreqFileName, cpuInfoMaxFreq / 1000));
-            return cpuInfoMaxFreq / 1000;
-        } catch (IOException | NumberFormatException e) {
-            LOGGER.error(String.format("Unable to retrieve the CPU speed from 
file [%s]. Using the value [%s] provided by the Libvirt.", 
cpuInfoMaxFreqFileName, nodeInfo.mhz), e);
+        try {
+            return getCpuSpeedInternal();
+        } catch (IOException | NumberFormatException ex) {
+            LOGGER.error(String.format("Unable to retrieve the CPU speed from 
file [%s] and lscpu. Using the value [%s] provided by the Libvirt.", 
cpuInfoMaxFreqFileName, nodeInfo.mhz), ex);
             return nodeInfo.mhz;
         }
     }
 
+    private static long getCpuSpeedInternal() throws IOException {
+        try {
+            return getCpuSpeedByLSCPU();
+        } catch (NullPointerException | NumberFormatException e) {
+            LOGGER.error(String.format("Unable to retrieve the CPU speed from 
lscpu. Using the value in [%s].", cpuInfoMaxFreqFileName), e);
+            return getCpuSpeedFromFile();
+        }
+    }
+
+    private static long getCpuSpeedByLSCPU() {

Review comment:
       ```suggestion
       private static long getCpuSpeedFromCommandLscpu() {
   ```

##########
File path: 
plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/linux/KVMHostInfo.java
##########
@@ -80,16 +82,39 @@ public long getOverCommitMemory() {
     }
 
     protected static long getCpuSpeed(final NodeInfo nodeInfo) {
-        try (Reader reader = new FileReader(cpuInfoMaxFreqFileName)) {
-            Long cpuInfoMaxFreq = 
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.", cpuInfoMaxFreq, 
cpuInfoMaxFreqFileName, cpuInfoMaxFreq / 1000));
-            return cpuInfoMaxFreq / 1000;
-        } catch (IOException | NumberFormatException e) {
-            LOGGER.error(String.format("Unable to retrieve the CPU speed from 
file [%s]. Using the value [%s] provided by the Libvirt.", 
cpuInfoMaxFreqFileName, nodeInfo.mhz), e);
+        try {
+            return getCpuSpeedInternal();
+        } catch (IOException | NumberFormatException ex) {
+            LOGGER.error(String.format("Unable to retrieve the CPU speed from 
file [%s] and lscpu. Using the value [%s] provided by the Libvirt.", 
cpuInfoMaxFreqFileName, nodeInfo.mhz), ex);
             return nodeInfo.mhz;
         }
     }
 
+    private static long getCpuSpeedInternal() throws IOException {
+        try {
+            return getCpuSpeedByLSCPU();
+        } catch (NullPointerException | NumberFormatException e) {
+            LOGGER.error(String.format("Unable to retrieve the CPU speed from 
lscpu. Using the value in [%s].", cpuInfoMaxFreqFileName), e);
+            return getCpuSpeedFromFile();
+        }
+    }
+
+    private static long getCpuSpeedByLSCPU() {
+        LOGGER.info("Fetching cpu speed from it from lscpu");

Review comment:
       ```suggestion
           LOGGER.info("Fetching CPU speed from command \"lscpu\".");
   ```

##########
File path: 
plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/linux/KVMHostInfo.java
##########
@@ -80,16 +82,39 @@ public long getOverCommitMemory() {
     }
 
     protected static long getCpuSpeed(final NodeInfo nodeInfo) {
-        try (Reader reader = new FileReader(cpuInfoMaxFreqFileName)) {
-            Long cpuInfoMaxFreq = 
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.", cpuInfoMaxFreq, 
cpuInfoMaxFreqFileName, cpuInfoMaxFreq / 1000));
-            return cpuInfoMaxFreq / 1000;
-        } catch (IOException | NumberFormatException e) {
-            LOGGER.error(String.format("Unable to retrieve the CPU speed from 
file [%s]. Using the value [%s] provided by the Libvirt.", 
cpuInfoMaxFreqFileName, nodeInfo.mhz), e);
+        try {
+            return getCpuSpeedInternal();
+        } catch (IOException | NumberFormatException ex) {
+            LOGGER.error(String.format("Unable to retrieve the CPU speed from 
file [%s] and lscpu. Using the value [%s] provided by the Libvirt.", 
cpuInfoMaxFreqFileName, nodeInfo.mhz), ex);
             return nodeInfo.mhz;
         }
     }
 
+    private static long getCpuSpeedInternal() throws IOException {
+        try {
+            return getCpuSpeedByLSCPU();
+        } catch (NullPointerException | NumberFormatException e) {
+            LOGGER.error(String.format("Unable to retrieve the CPU speed from 
lscpu. Using the value in [%s].", cpuInfoMaxFreqFileName), e);
+            return getCpuSpeedFromFile();
+        }
+    }
+
+    private static long getCpuSpeedByLSCPU() {
+        LOGGER.info("Fetching cpu speed from it from 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);
+        LOGGER.info(String.format("Got [%d] speed from lscpu", speed));

Review comment:
       ```suggestion
           LOGGER.info(String.format("Command [%s] resulted in the value [%s] 
for CPU speed.", command, speed));
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to