|
18 | 18 |
|
19 | 19 | import com.cloud.hypervisor.kvm.resource.LibvirtCapXMLParser; |
20 | 20 | import com.cloud.hypervisor.kvm.resource.LibvirtConnection; |
| 21 | +import com.cloud.utils.script.Script; |
| 22 | + |
21 | 23 | import org.apache.commons.io.IOUtils; |
22 | 24 | import org.apache.log4j.Logger; |
23 | 25 | import org.libvirt.Connect; |
@@ -80,13 +82,45 @@ public List<String> getCapabilities() { |
80 | 82 | } |
81 | 83 |
|
82 | 84 | 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)); |
83 | 117 | try (Reader reader = new FileReader(cpuInfoMaxFreqFileName)) { |
84 | 118 | Long cpuInfoMaxFreq = Long.parseLong(IOUtils.toString(reader).trim()); |
85 | 119 | LOGGER.info(String.format("Retrieved value [%s] from file [%s]. This corresponds to a CPU speed of [%s] MHz.", cpuInfoMaxFreq, cpuInfoMaxFreqFileName, cpuInfoMaxFreq / 1000)); |
86 | 120 | return cpuInfoMaxFreq / 1000; |
87 | 121 | } 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; |
90 | 124 | } |
91 | 125 | } |
92 | 126 |
|
|
0 commit comments