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



##########
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 getCpuSpeedFromCommandLscpu();
+        } 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 getCpuSpeedFromCommandLscpu() {
+        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);
+        LOGGER.info(String.format("Command [%s] resulted in the value [%s] for 
CPU speed.", command, speed));
+        return speed;
+    }
+
+    private static long getCpuSpeedFromFile() throws IOException {
+        Reader reader = new FileReader(cpuInfoMaxFreqFileName);

Review comment:
       add a log info here ?
   ```
           LOGGER.info("Fetching CPU speed from file \"" + 
cpuInfoMaxFreqFileName + "\".");
   ```

##########
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 getCpuSpeedFromCommandLscpu();
+        } 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 getCpuSpeedFromCommandLscpu() {

Review comment:
       suggestion:
   add try...catch in getCpuSpeedFromCommandLscpu and getCpuSpeedFromFile
   if exception is caught, then return the a default value like 0 or -1.




-- 
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