weizhouapache commented on a change in pull request #5506:
URL: https://github.com/apache/cloudstack/pull/5506#discussion_r798289437
##########
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.
then the getSpeed method will be simple
```
long speed = getCpuSpeedFromCommandLscpu();
if (speed = 0L) {
speed = getCpuSpeedFromFile();
}
if (speed = 0L) {
speed = nodeInfo.mhz;
}
return 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]