Skip to content

Commit 78a981f

Browse files
committed
Add cpu speed detection methods
Added additional match for lscpu Added additional file to check
1 parent 019f2c6 commit 78a981f

File tree

1 file changed

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

1 file changed

+24
-13
lines changed

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ public class KVMHostInfo {
5858
private long reservedMemory;
5959
private long overCommitMemory;
6060
private List<String> capabilities = new ArrayList<>();
61-
62-
private static String cpuInfoFreqFileName = "/sys/devices/system/cpu/cpu0/cpufreq/base_frequency";
6361
private static String cpuArchCommand = "/usr/bin/arch";
62+
private static List<String> cpuInfoFreqFileNames = List.of("/sys/devices/system/cpu/cpu0/cpufreq/base_frequency","/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq");
6463

6564
public KVMHostInfo(long reservedMemory, long overCommitMemory, long manualSpeed, int reservedCpus) {
6665
this.cpuSpeed = manualSpeed;
@@ -134,29 +133,41 @@ protected static long getCpuSpeed(final String cpabilities, final NodeInfo nodeI
134133
}
135134

136135
private static long getCpuSpeedFromCommandLscpu() {
136+
long speed = 0L;
137+
LOGGER.info("Fetching CPU speed from command \"lscpu\".");
137138
try {
138-
LOGGER.info("Fetching CPU speed from command \"lscpu\".");
139139
String command = "lscpu | grep -i 'Model name' | head -n 1 | egrep -o '[[:digit:]].[[:digit:]]+GHz' | sed 's/GHz//g'";
140140
String result = Script.runSimpleBashScript(command);
141-
long speed = (long) (Float.parseFloat(result) * 1000);
141+
speed = (long) (Float.parseFloat(result) * 1000);
142142
LOGGER.info(String.format("Command [%s] resulted in the value [%s] for CPU speed.", command, speed));
143143
return speed;
144144
} catch (NullPointerException | NumberFormatException e) {
145145
LOGGER.error(String.format("Unable to retrieve the CPU speed from lscpu."), e);
146-
return 0L;
147146
}
147+
try {
148+
String command = "lscpu | grep -i 'CPU max MHz' | head -n 1 | sed 's/^.*: //' | xargs";
149+
String result = Script.runSimpleBashScript(command);
150+
speed = (long) (Float.parseFloat(result));
151+
LOGGER.info(String.format("Command [%s] resulted in the value [%s] for CPU speed.", command, speed));
152+
return speed;
153+
} catch (NullPointerException | NumberFormatException e) {
154+
LOGGER.error(String.format("Unable to retrieve the CPU speed from lscpu."), e);
155+
}
156+
return speed;
148157
}
149158

150159
private static long getCpuSpeedFromFile() {
151-
LOGGER.info(String.format("Fetching CPU speed from file [%s].", cpuInfoFreqFileName));
152-
try (Reader reader = new FileReader(cpuInfoFreqFileName)) {
153-
Long cpuInfoFreq = Long.parseLong(IOUtils.toString(reader).trim());
154-
LOGGER.info(String.format("Retrieved value [%s] from file [%s]. This corresponds to a CPU speed of [%s] MHz.", cpuInfoFreq, cpuInfoFreqFileName, cpuInfoFreq / 1000));
155-
return cpuInfoFreq / 1000;
156-
} catch (IOException | NumberFormatException e) {
157-
LOGGER.error(String.format("Unable to retrieve the CPU speed from file [%s]", cpuInfoFreqFileName), e);
158-
return 0L;
160+
for (final String cpuInfoFreqFileName: cpuInfoFreqFileNames) {
161+
LOGGER.info(String.format("Fetching CPU speed from file [%s].", cpuInfoFreqFileName));
162+
try (Reader reader = new FileReader(cpuInfoFreqFileName)) {
163+
Long cpuInfoFreq = Long.parseLong(IOUtils.toString(reader).trim());
164+
LOGGER.info(String.format("Retrieved value [%s] from file [%s]. This corresponds to a CPU speed of [%s] MHz.", cpuInfoFreq, cpuInfoFreqFileName, cpuInfoFreq / 1000));
165+
return cpuInfoFreq / 1000;
166+
} catch (IOException | NumberFormatException e) {
167+
LOGGER.error(String.format("Unable to retrieve the CPU speed from file [%s]", cpuInfoFreqFileName), e);
168+
}
159169
}
170+
return 0L;
160171
}
161172

162173
protected static long getCpuSpeedFromHostCapabilities(final String capabilities) {

0 commit comments

Comments
 (0)