Skip to content

Commit ac3c61d

Browse files
BartJMdhslove
authored andcommitted
Add cpu speed detection methods (apache#9762)
Added additional match for lscpu Added additional file to check
1 parent 4046e46 commit ac3c61d

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;
@@ -138,32 +137,44 @@ protected static long getCpuSpeed(final String cpabilities, final NodeInfo nodeI
138137
}
139138

140139
private static long getCpuSpeedFromCommandLscpu() {
140+
long speed = 0L;
141+
LOGGER.info("Fetching CPU speed from command \"lscpu\".");
141142
try {
142-
LOGGER.info("Fetching CPU speed from command \"lscpu\".");
143143
String command = "lscpu | grep -i 'Model name' | head -n 1 | egrep -o '[[:digit:]].[[:digit:]]+GHz' | sed 's/GHz//g'";
144144
if(isHostS390x()) {
145145
command = "lscpu | grep 'CPU dynamic MHz' | cut -d ':' -f 2 | tr -d ' ' | awk '{printf \"%.1f\\n\", $1 / 1000}'";
146146
}
147147
String result = Script.runSimpleBashScript(command);
148-
long speed = (long) (Float.parseFloat(result) * 1000);
148+
speed = (long) (Float.parseFloat(result) * 1000);
149149
LOGGER.info(String.format("Command [%s] resulted in the value [%s] for CPU speed.", command, speed));
150150
return speed;
151151
} catch (NullPointerException | NumberFormatException e) {
152152
LOGGER.error(String.format("Unable to retrieve the CPU speed from lscpu."), e);
153-
return 0L;
154153
}
154+
try {
155+
String command = "lscpu | grep -i 'CPU max MHz' | head -n 1 | sed 's/^.*: //' | xargs";
156+
String result = Script.runSimpleBashScript(command);
157+
speed = (long) (Float.parseFloat(result));
158+
LOGGER.info(String.format("Command [%s] resulted in the value [%s] for CPU speed.", command, speed));
159+
return speed;
160+
} catch (NullPointerException | NumberFormatException e) {
161+
LOGGER.error(String.format("Unable to retrieve the CPU speed from lscpu."), e);
162+
}
163+
return speed;
155164
}
156165

157166
private static long getCpuSpeedFromFile() {
158-
LOGGER.info(String.format("Fetching CPU speed from file [%s].", cpuInfoFreqFileName));
159-
try (Reader reader = new FileReader(cpuInfoFreqFileName)) {
160-
Long cpuInfoFreq = Long.parseLong(IOUtils.toString(reader).trim());
161-
LOGGER.info(String.format("Retrieved value [%s] from file [%s]. This corresponds to a CPU speed of [%s] MHz.", cpuInfoFreq, cpuInfoFreqFileName, cpuInfoFreq / 1000));
162-
return cpuInfoFreq / 1000;
163-
} catch (IOException | NumberFormatException e) {
164-
LOGGER.error(String.format("Unable to retrieve the CPU speed from file [%s]", cpuInfoFreqFileName), e);
165-
return 0L;
167+
for (final String cpuInfoFreqFileName: cpuInfoFreqFileNames) {
168+
LOGGER.info(String.format("Fetching CPU speed from file [%s].", cpuInfoFreqFileName));
169+
try (Reader reader = new FileReader(cpuInfoFreqFileName)) {
170+
Long cpuInfoFreq = Long.parseLong(IOUtils.toString(reader).trim());
171+
LOGGER.info(String.format("Retrieved value [%s] from file [%s]. This corresponds to a CPU speed of [%s] MHz.", cpuInfoFreq, cpuInfoFreqFileName, cpuInfoFreq / 1000));
172+
return cpuInfoFreq / 1000;
173+
} catch (IOException | NumberFormatException e) {
174+
LOGGER.error(String.format("Unable to retrieve the CPU speed from file [%s]", cpuInfoFreqFileName), e);
175+
}
166176
}
177+
return 0L;
167178
}
168179

169180
protected static long getCpuSpeedFromHostCapabilities(final String capabilities) {

0 commit comments

Comments
 (0)