Skip to content

Commit 2e8c069

Browse files
DennisKonradGabrielBrascher
authored andcommitted
Load Average for KVM (#3738)
* Avgload (#2) * Adding avgload for kvm * Fix coding style issue * Add getter/setter * Fix several small errors * Add override * Uncomment getAverageLoad * Override getAverageLoad() * Checkstyle bug? * Delete trailing spaces * Renaming function * Change interface to match * Rename method in GetHostStatsAnswer * Change method call name * Convert double to long * Remove trailing whitespace * Change names around * Make load visible to return it * Parse string to double * Change Long to Double * Fix getter * Unify naming to cpuloadaverage * Change cpuloadaverage String to Double in listHostsMetrics Remove some unnecessary whitespaces * Add CPU_LOAD_AVERAGE to ApiConstants
1 parent da62cec commit 2e8c069

File tree

10 files changed

+50
-8
lines changed

10 files changed

+50
-8
lines changed

api/src/main/java/com/cloud/host/HostStats.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public interface HostStats {
3535

3636
public HostStats getHostStats();
3737

38-
// public double getAverageLoad();
38+
public double getLoadAverage();
3939
// public double getXapiMemoryUsageKBs();
4040
}

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public class ApiConstants {
7171
public static final String COMPONENT = "component";
7272
public static final String CPU_NUMBER = "cpunumber";
7373
public static final String CPU_SPEED = "cpuspeed";
74+
public static final String CPU_LOAD_AVERAGE = "cpuloadaverage";
7475
public static final String CREATED = "created";
7576
public static final String CTX_ACCOUNT_ID = "ctxaccountid";
7677
public static final String CTX_DETAILS = "ctxDetails";

api/src/main/java/org/apache/cloudstack/api/response/HostResponse.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ public class HostResponse extends BaseResponse {
114114
@Param(description = "the amount of the host's CPU after applying the cpu.overprovisioning.factor ")
115115
private String cpuWithOverprovisioning;
116116

117-
@SerializedName("averageload")
117+
@SerializedName(ApiConstants.CPU_LOAD_AVERAGE)
118118
@Param(description = "the cpu average load on the host")
119-
private Long averageLoad;
119+
private Double cpuloadaverage;
120120

121121
@SerializedName("networkkbsread")
122122
@Param(description = "the incoming network traffic on the host")
@@ -337,8 +337,8 @@ public void setCpuUsed(String cpuUsed) {
337337
this.cpuUsed = cpuUsed;
338338
}
339339

340-
public void setAverageLoad(Long averageLoad) {
341-
this.averageLoad = averageLoad;
340+
public void setCpuAverageLoad(Double averageLoad) {
341+
this.cpuloadaverage = averageLoad;
342342
}
343343

344344
public void setNetworkKbsRead(Long networkKbsRead) {
@@ -570,8 +570,8 @@ public String getCpuUsed() {
570570
return cpuUsed;
571571
}
572572

573-
public Long getAverageLoad() {
574-
return averageLoad;
573+
public Double getAverageLoad() {
574+
return cpuloadaverage;
575575
}
576576

577577
public Long getNetworkKbsRead() {

core/src/main/java/com/cloud/agent/api/GetHostStatsAnswer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public double getCpuUtilization() {
6969
return hostStats.getCpuUtilization();
7070
}
7171

72+
@Override
73+
public double getLoadAverage() {
74+
return hostStats.getLoadAverage();
75+
}
76+
7277
@Override
7378
public double getNetworkReadKBs() {
7479
return hostStats.getNetworkReadKBs();

core/src/main/java/com/cloud/agent/api/HostStatsEntry.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class HostStatsEntry implements HostStats {
2828
HostVO hostVo;
2929
String entityType;
3030
double cpuUtilization;
31+
double averageLoad;
3132
double networkReadKBs;
3233
double networkWriteKBs;
3334
double totalMemoryKBs;
@@ -45,6 +46,7 @@ public HostStatsEntry(long hostId, double cpuUtilization, double networkReadKBs,
4546
this.networkWriteKBs = networkWriteKBs;
4647
this.totalMemoryKBs = totalMemoryKBs;
4748
this.freeMemoryKBs = freeMemoryKBs;
49+
this.averageLoad = averageLoad;
4850
}
4951

5052
@Override
@@ -101,6 +103,15 @@ public void setCpuUtilization(double cpuUtilization) {
101103
this.cpuUtilization = cpuUtilization;
102104
}
103105

106+
@Override
107+
public double getLoadAverage() {
108+
return this.averageLoad;
109+
}
110+
111+
public void setAverageLoad(double cpuAvgLoad) {
112+
this.averageLoad = cpuAvgLoad;
113+
}
114+
104115
@Override
105116
public double getUsedMemory() {
106117
return (totalMemoryKBs - freeMemoryKBs) * 1024;

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetHostStatsCommandWrapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ public Answer execute(final GetHostStatsCommand command, final LibvirtComputingR
4242
MemStat memStat = libvirtComputingResource.getMemStat();
4343

4444
final double cpuUtil = cpuStat.getCpuUsedPercent();
45+
final double loadAvg = cpuStat.getCpuLoadAverage();
4546

4647
final Pair<Double, Double> nicStats = libvirtComputingResource.getNicStats(libvirtComputingResource.getPublicBridgeName());
4748

48-
final HostStatsEntry hostStats = new HostStatsEntry(command.getHostId(), cpuUtil, nicStats.first() / 1024, nicStats.second() / 1024, "host", memStat.getTotal() / 1024, memStat.getAvailable() / 1024, 0, 0);
49+
final HostStatsEntry hostStats = new HostStatsEntry(command.getHostId(), cpuUtil, nicStats.first() / 1024, nicStats.second() / 1024, "host", memStat.getTotal() / 1024, memStat.getAvailable() / 1024, 0, loadAvg);
4950
return new GetHostStatsAnswer(command, hostStats);
5051
}
5152
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class CPUStat {
3030
private UptimeStats _lastStats;
3131
private final String _sysfsCpuDir = "/sys/devices/system/cpu";
3232
private final String _uptimeFile = "/proc/uptime";
33+
private final String _loadavgFile = "/proc/loadavg";
3334

3435
class UptimeStats {
3536
public Double upTime = 0d;
@@ -80,6 +81,17 @@ public Integer getCores() {
8081
return _cores;
8182
}
8283

84+
public Double getCpuLoadAverage() {
85+
File f = new File(_loadavgFile);
86+
String[] load = {"0.0"};
87+
try (Scanner scanner = new Scanner(f,"UTF-8");) {
88+
load = scanner.useDelimiter("\\Z").next().split("\\s+");
89+
} catch (FileNotFoundException ex) {
90+
s_logger.warn("File " + _uptimeFile + " not found:" + ex.toString());
91+
}
92+
return Double.parseDouble(load[0]);
93+
}
94+
8395
public Double getCpuUsedPercent() {
8496
Double cpuUsed = 0d;
8597
if (_cores == null || _cores == 0) {

plugins/metrics/src/main/java/org/apache/cloudstack/metrics/MetricsServiceImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ public List<HostMetricsResponse> listHostMetrics(List<HostResponse> hostResponse
279279
metricsResponse.setCpuTotal(hostResponse.getCpuNumber(), hostResponse.getCpuSpeed(), cpuOvercommitRatio);
280280
metricsResponse.setCpuUsed(hostResponse.getCpuUsed(), hostResponse.getCpuNumber(), hostResponse.getCpuSpeed());
281281
metricsResponse.setCpuAllocated(hostResponse.getCpuAllocated(), hostResponse.getCpuNumber(), hostResponse.getCpuSpeed());
282+
metricsResponse.setLoadAverage(hostResponse.getAverageLoad());
282283
metricsResponse.setMemTotal(hostResponse.getMemoryTotal(), memoryOvercommitRatio);
283284
metricsResponse.setMemAllocated(hostResponse.getMemoryAllocated());
284285
metricsResponse.setMemUsed(hostResponse.getMemoryUsed());

plugins/metrics/src/main/java/org/apache/cloudstack/response/HostMetricsResponse.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public class HostMetricsResponse extends HostResponse {
4343
@Param(description = "the total cpu allocated in Ghz")
4444
private String cpuAllocated;
4545

46+
@SerializedName("cpuloadaverage")
47+
@Param(description = "the average cpu load the last minute")
48+
private Double loadAverage;
49+
4650
@SerializedName("memorytotalgb")
4751
@Param(description = "the total cpu capacity in GiB")
4852
private String memTotal;
@@ -117,6 +121,12 @@ public void setCpuUsed(final String cpuUsed, final Integer cpuNumber, final Long
117121
}
118122
}
119123

124+
public void setLoadAverage(final Double loadAverage) {
125+
if (loadAverage != null) {
126+
this.loadAverage = loadAverage;
127+
}
128+
}
129+
120130
public void setCpuAllocated(final String cpuAllocated, final Integer cpuNumber, final Long cpuSpeed) {
121131
if (cpuAllocated != null && cpuNumber != null && cpuSpeed != null) {
122132
this.cpuAllocated = String.format("%.2f Ghz", Double.valueOf(cpuAllocated.replace("%", "")) * cpuNumber * cpuSpeed / (100.0 * 1000.0));

server/src/main/java/com/cloud/api/query/dao/HostJoinDaoImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ public HostResponse newHostResponse(HostJoinVO host, EnumSet<HostDetails> detail
194194
float cpuUtil = (float)hostStats.getCpuUtilization();
195195
cpuUsed = decimalFormat.format(cpuUtil) + "%";
196196
hostResponse.setCpuUsed(cpuUsed);
197+
hostResponse.setCpuAverageLoad(hostStats.getLoadAverage());
197198
hostResponse.setMemoryUsed((new Double(hostStats.getUsedMemory())).longValue());
198199
hostResponse.setNetworkKbsRead((new Double(hostStats.getNetworkReadKBs())).longValue());
199200
hostResponse.setNetworkKbsWrite((new Double(hostStats.getNetworkWriteKBs())).longValue());

0 commit comments

Comments
 (0)