Skip to content

Commit 26e4e18

Browse files
davidjumaniGutoVeronezisureshanaparti
authored
kvm: Use lscpu to get cpu max speed (#5506)
* kvm: Use lscpu to get cpu max speed * Fix str conversion * Reorder * Refactor * Apply suggestions from code review Co-authored-by: Daniel Augusto Veronezi Salvador <38945620+GutoVeronezi@users.noreply.github.com> * Updated the calling method name getCpuSpeedFromCommandLscpu * Make it more readable Co-authored-by: Daniel Augusto Veronezi Salvador <38945620+GutoVeronezi@users.noreply.github.com> Co-authored-by: sureshanaparti <12028987+sureshanaparti@users.noreply.github.com>
1 parent f458964 commit 26e4e18

File tree

1 file changed

+36
-2
lines changed
  • plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/linux

1 file changed

+36
-2
lines changed

plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/linux/KVMHostInfo.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import com.cloud.hypervisor.kvm.resource.LibvirtCapXMLParser;
2020
import com.cloud.hypervisor.kvm.resource.LibvirtConnection;
21+
import com.cloud.utils.script.Script;
22+
2123
import org.apache.commons.io.IOUtils;
2224
import org.apache.log4j.Logger;
2325
import org.libvirt.Connect;
@@ -80,13 +82,45 @@ public List<String> getCapabilities() {
8082
}
8183

8284
protected static long getCpuSpeed(final NodeInfo nodeInfo) {
85+
long speed = 0L;
86+
speed = getCpuSpeedFromCommandLscpu();
87+
if(speed > 0L) {
88+
return speed;
89+
}
90+
91+
speed = getCpuSpeedFromFile();
92+
if(speed > 0L) {
93+
return speed;
94+
}
95+
96+
LOGGER.info(String.format("Using the value [%s] provided by Libvirt.", nodeInfo.mhz));
97+
speed = nodeInfo.mhz;
98+
return speed;
99+
}
100+
101+
private static long getCpuSpeedFromCommandLscpu() {
102+
try {
103+
LOGGER.info("Fetching CPU speed from command \"lscpu\".");
104+
String command = "lscpu | grep -i 'Model name' | head -n 1 | egrep -o '[[:digit:]].[[:digit:]]+GHz' | sed 's/GHz//g'";
105+
String result = Script.runSimpleBashScript(command);
106+
long speed = (long) (Float.parseFloat(result) * 1000);
107+
LOGGER.info(String.format("Command [%s] resulted in the value [%s] for CPU speed.", command, speed));
108+
return speed;
109+
} catch (NullPointerException | NumberFormatException e) {
110+
LOGGER.error(String.format("Unable to retrieve the CPU speed from lscpu."), e);
111+
return 0L;
112+
}
113+
}
114+
115+
private static long getCpuSpeedFromFile() {
116+
LOGGER.info(String.format("Fetching CPU speed from file [%s].", cpuInfoMaxFreqFileName));
83117
try (Reader reader = new FileReader(cpuInfoMaxFreqFileName)) {
84118
Long cpuInfoMaxFreq = Long.parseLong(IOUtils.toString(reader).trim());
85119
LOGGER.info(String.format("Retrieved value [%s] from file [%s]. This corresponds to a CPU speed of [%s] MHz.", cpuInfoMaxFreq, cpuInfoMaxFreqFileName, cpuInfoMaxFreq / 1000));
86120
return cpuInfoMaxFreq / 1000;
87121
} catch (IOException | NumberFormatException e) {
88-
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);
89-
return nodeInfo.mhz;
122+
LOGGER.error(String.format("Unable to retrieve the CPU speed from file [%s]", cpuInfoMaxFreqFileName), e);
123+
return 0L;
90124
}
91125
}
92126

0 commit comments

Comments
 (0)