mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Add cpu speed detection methods (#9762)
Added additional match for lscpu Added additional file to check
This commit is contained in:
parent
c0e05c4a6d
commit
ee32f4cfe8
@ -58,9 +58,8 @@ public class KVMHostInfo {
|
|||||||
private long reservedMemory;
|
private long reservedMemory;
|
||||||
private long overCommitMemory;
|
private long overCommitMemory;
|
||||||
private List<String> capabilities = new ArrayList<>();
|
private List<String> capabilities = new ArrayList<>();
|
||||||
|
|
||||||
private static String cpuInfoFreqFileName = "/sys/devices/system/cpu/cpu0/cpufreq/base_frequency";
|
|
||||||
private static String cpuArchCommand = "/usr/bin/arch";
|
private static String cpuArchCommand = "/usr/bin/arch";
|
||||||
|
private static List<String> cpuInfoFreqFileNames = List.of("/sys/devices/system/cpu/cpu0/cpufreq/base_frequency","/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq");
|
||||||
|
|
||||||
public KVMHostInfo(long reservedMemory, long overCommitMemory, long manualSpeed, int reservedCpus) {
|
public KVMHostInfo(long reservedMemory, long overCommitMemory, long manualSpeed, int reservedCpus) {
|
||||||
this.cpuSpeed = manualSpeed;
|
this.cpuSpeed = manualSpeed;
|
||||||
@ -134,29 +133,41 @@ public class KVMHostInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static long getCpuSpeedFromCommandLscpu() {
|
private static long getCpuSpeedFromCommandLscpu() {
|
||||||
|
long speed = 0L;
|
||||||
|
LOGGER.info("Fetching CPU speed from command \"lscpu\".");
|
||||||
try {
|
try {
|
||||||
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 command = "lscpu | grep -i 'Model name' | head -n 1 | egrep -o '[[:digit:]].[[:digit:]]+GHz' | sed 's/GHz//g'";
|
||||||
String result = Script.runSimpleBashScript(command);
|
String result = Script.runSimpleBashScript(command);
|
||||||
long speed = (long) (Float.parseFloat(result) * 1000);
|
speed = (long) (Float.parseFloat(result) * 1000);
|
||||||
LOGGER.info(String.format("Command [%s] resulted in the value [%s] for CPU speed.", command, speed));
|
LOGGER.info(String.format("Command [%s] resulted in the value [%s] for CPU speed.", command, speed));
|
||||||
return speed;
|
return speed;
|
||||||
} catch (NullPointerException | NumberFormatException e) {
|
} catch (NullPointerException | NumberFormatException e) {
|
||||||
LOGGER.error(String.format("Unable to retrieve the CPU speed from lscpu."), e);
|
LOGGER.error(String.format("Unable to retrieve the CPU speed from lscpu."), e);
|
||||||
return 0L;
|
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
String command = "lscpu | grep -i 'CPU max MHz' | head -n 1 | sed 's/^.*: //' | xargs";
|
||||||
|
String result = Script.runSimpleBashScript(command);
|
||||||
|
speed = (long) (Float.parseFloat(result));
|
||||||
|
LOGGER.info(String.format("Command [%s] resulted in the value [%s] for CPU speed.", command, speed));
|
||||||
|
return speed;
|
||||||
|
} catch (NullPointerException | NumberFormatException e) {
|
||||||
|
LOGGER.error(String.format("Unable to retrieve the CPU speed from lscpu."), e);
|
||||||
|
}
|
||||||
|
return speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static long getCpuSpeedFromFile() {
|
private static long getCpuSpeedFromFile() {
|
||||||
LOGGER.info(String.format("Fetching CPU speed from file [%s].", cpuInfoFreqFileName));
|
for (final String cpuInfoFreqFileName: cpuInfoFreqFileNames) {
|
||||||
try (Reader reader = new FileReader(cpuInfoFreqFileName)) {
|
LOGGER.info(String.format("Fetching CPU speed from file [%s].", cpuInfoFreqFileName));
|
||||||
Long cpuInfoFreq = Long.parseLong(IOUtils.toString(reader).trim());
|
try (Reader reader = new FileReader(cpuInfoFreqFileName)) {
|
||||||
LOGGER.info(String.format("Retrieved value [%s] from file [%s]. This corresponds to a CPU speed of [%s] MHz.", cpuInfoFreq, cpuInfoFreqFileName, cpuInfoFreq / 1000));
|
Long cpuInfoFreq = Long.parseLong(IOUtils.toString(reader).trim());
|
||||||
return cpuInfoFreq / 1000;
|
LOGGER.info(String.format("Retrieved value [%s] from file [%s]. This corresponds to a CPU speed of [%s] MHz.", cpuInfoFreq, cpuInfoFreqFileName, cpuInfoFreq / 1000));
|
||||||
} catch (IOException | NumberFormatException e) {
|
return cpuInfoFreq / 1000;
|
||||||
LOGGER.error(String.format("Unable to retrieve the CPU speed from file [%s]", cpuInfoFreqFileName), e);
|
} catch (IOException | NumberFormatException e) {
|
||||||
return 0L;
|
LOGGER.error(String.format("Unable to retrieve the CPU speed from file [%s]", cpuInfoFreqFileName), e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static long getCpuSpeedFromHostCapabilities(final String capabilities) {
|
protected static long getCpuSpeedFromHostCapabilities(final String capabilities) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user