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
This commit is contained in:
Dennis Konrad 2019-12-17 10:37:05 +01:00 committed by Gabriel Beims Bräscher
parent da62cecb68
commit 2e8c069dd2
10 changed files with 50 additions and 8 deletions

View File

@ -35,6 +35,6 @@ public interface HostStats {
public HostStats getHostStats(); public HostStats getHostStats();
// public double getAverageLoad(); public double getLoadAverage();
// public double getXapiMemoryUsageKBs(); // public double getXapiMemoryUsageKBs();
} }

View File

@ -71,6 +71,7 @@ public class ApiConstants {
public static final String COMPONENT = "component"; public static final String COMPONENT = "component";
public static final String CPU_NUMBER = "cpunumber"; public static final String CPU_NUMBER = "cpunumber";
public static final String CPU_SPEED = "cpuspeed"; public static final String CPU_SPEED = "cpuspeed";
public static final String CPU_LOAD_AVERAGE = "cpuloadaverage";
public static final String CREATED = "created"; public static final String CREATED = "created";
public static final String CTX_ACCOUNT_ID = "ctxaccountid"; public static final String CTX_ACCOUNT_ID = "ctxaccountid";
public static final String CTX_DETAILS = "ctxDetails"; public static final String CTX_DETAILS = "ctxDetails";

View File

@ -114,9 +114,9 @@ public class HostResponse extends BaseResponse {
@Param(description = "the amount of the host's CPU after applying the cpu.overprovisioning.factor ") @Param(description = "the amount of the host's CPU after applying the cpu.overprovisioning.factor ")
private String cpuWithOverprovisioning; private String cpuWithOverprovisioning;
@SerializedName("averageload") @SerializedName(ApiConstants.CPU_LOAD_AVERAGE)
@Param(description = "the cpu average load on the host") @Param(description = "the cpu average load on the host")
private Long averageLoad; private Double cpuloadaverage;
@SerializedName("networkkbsread") @SerializedName("networkkbsread")
@Param(description = "the incoming network traffic on the host") @Param(description = "the incoming network traffic on the host")
@ -337,8 +337,8 @@ public class HostResponse extends BaseResponse {
this.cpuUsed = cpuUsed; this.cpuUsed = cpuUsed;
} }
public void setAverageLoad(Long averageLoad) { public void setCpuAverageLoad(Double averageLoad) {
this.averageLoad = averageLoad; this.cpuloadaverage = averageLoad;
} }
public void setNetworkKbsRead(Long networkKbsRead) { public void setNetworkKbsRead(Long networkKbsRead) {
@ -570,8 +570,8 @@ public class HostResponse extends BaseResponse {
return cpuUsed; return cpuUsed;
} }
public Long getAverageLoad() { public Double getAverageLoad() {
return averageLoad; return cpuloadaverage;
} }
public Long getNetworkKbsRead() { public Long getNetworkKbsRead() {

View File

@ -69,6 +69,11 @@ public class GetHostStatsAnswer extends Answer implements HostStats {
return hostStats.getCpuUtilization(); return hostStats.getCpuUtilization();
} }
@Override
public double getLoadAverage() {
return hostStats.getLoadAverage();
}
@Override @Override
public double getNetworkReadKBs() { public double getNetworkReadKBs() {
return hostStats.getNetworkReadKBs(); return hostStats.getNetworkReadKBs();

View File

@ -28,6 +28,7 @@ public class HostStatsEntry implements HostStats {
HostVO hostVo; HostVO hostVo;
String entityType; String entityType;
double cpuUtilization; double cpuUtilization;
double averageLoad;
double networkReadKBs; double networkReadKBs;
double networkWriteKBs; double networkWriteKBs;
double totalMemoryKBs; double totalMemoryKBs;
@ -45,6 +46,7 @@ public class HostStatsEntry implements HostStats {
this.networkWriteKBs = networkWriteKBs; this.networkWriteKBs = networkWriteKBs;
this.totalMemoryKBs = totalMemoryKBs; this.totalMemoryKBs = totalMemoryKBs;
this.freeMemoryKBs = freeMemoryKBs; this.freeMemoryKBs = freeMemoryKBs;
this.averageLoad = averageLoad;
} }
@Override @Override
@ -101,6 +103,15 @@ public class HostStatsEntry implements HostStats {
this.cpuUtilization = cpuUtilization; this.cpuUtilization = cpuUtilization;
} }
@Override
public double getLoadAverage() {
return this.averageLoad;
}
public void setAverageLoad(double cpuAvgLoad) {
this.averageLoad = cpuAvgLoad;
}
@Override @Override
public double getUsedMemory() { public double getUsedMemory() {
return (totalMemoryKBs - freeMemoryKBs) * 1024; return (totalMemoryKBs - freeMemoryKBs) * 1024;

View File

@ -42,10 +42,11 @@ public final class LibvirtGetHostStatsCommandWrapper extends CommandWrapper<GetH
MemStat memStat = libvirtComputingResource.getMemStat(); MemStat memStat = libvirtComputingResource.getMemStat();
final double cpuUtil = cpuStat.getCpuUsedPercent(); final double cpuUtil = cpuStat.getCpuUsedPercent();
final double loadAvg = cpuStat.getCpuLoadAverage();
final Pair<Double, Double> nicStats = libvirtComputingResource.getNicStats(libvirtComputingResource.getPublicBridgeName()); final Pair<Double, Double> nicStats = libvirtComputingResource.getNicStats(libvirtComputingResource.getPublicBridgeName());
final HostStatsEntry hostStats = new HostStatsEntry(command.getHostId(), cpuUtil, nicStats.first() / 1024, nicStats.second() / 1024, "host", memStat.getTotal() / 1024, memStat.getAvailable() / 1024, 0, 0); final HostStatsEntry hostStats = new HostStatsEntry(command.getHostId(), cpuUtil, nicStats.first() / 1024, nicStats.second() / 1024, "host", memStat.getTotal() / 1024, memStat.getAvailable() / 1024, 0, loadAvg);
return new GetHostStatsAnswer(command, hostStats); return new GetHostStatsAnswer(command, hostStats);
} }
} }

View File

@ -30,6 +30,7 @@ public class CPUStat {
private UptimeStats _lastStats; private UptimeStats _lastStats;
private final String _sysfsCpuDir = "/sys/devices/system/cpu"; private final String _sysfsCpuDir = "/sys/devices/system/cpu";
private final String _uptimeFile = "/proc/uptime"; private final String _uptimeFile = "/proc/uptime";
private final String _loadavgFile = "/proc/loadavg";
class UptimeStats { class UptimeStats {
public Double upTime = 0d; public Double upTime = 0d;
@ -80,6 +81,17 @@ public class CPUStat {
return _cores; return _cores;
} }
public Double getCpuLoadAverage() {
File f = new File(_loadavgFile);
String[] load = {"0.0"};
try (Scanner scanner = new Scanner(f,"UTF-8");) {
load = scanner.useDelimiter("\\Z").next().split("\\s+");
} catch (FileNotFoundException ex) {
s_logger.warn("File " + _uptimeFile + " not found:" + ex.toString());
}
return Double.parseDouble(load[0]);
}
public Double getCpuUsedPercent() { public Double getCpuUsedPercent() {
Double cpuUsed = 0d; Double cpuUsed = 0d;
if (_cores == null || _cores == 0) { if (_cores == null || _cores == 0) {

View File

@ -279,6 +279,7 @@ public class MetricsServiceImpl extends ComponentLifecycleBase implements Metric
metricsResponse.setCpuTotal(hostResponse.getCpuNumber(), hostResponse.getCpuSpeed(), cpuOvercommitRatio); metricsResponse.setCpuTotal(hostResponse.getCpuNumber(), hostResponse.getCpuSpeed(), cpuOvercommitRatio);
metricsResponse.setCpuUsed(hostResponse.getCpuUsed(), hostResponse.getCpuNumber(), hostResponse.getCpuSpeed()); metricsResponse.setCpuUsed(hostResponse.getCpuUsed(), hostResponse.getCpuNumber(), hostResponse.getCpuSpeed());
metricsResponse.setCpuAllocated(hostResponse.getCpuAllocated(), hostResponse.getCpuNumber(), hostResponse.getCpuSpeed()); metricsResponse.setCpuAllocated(hostResponse.getCpuAllocated(), hostResponse.getCpuNumber(), hostResponse.getCpuSpeed());
metricsResponse.setLoadAverage(hostResponse.getAverageLoad());
metricsResponse.setMemTotal(hostResponse.getMemoryTotal(), memoryOvercommitRatio); metricsResponse.setMemTotal(hostResponse.getMemoryTotal(), memoryOvercommitRatio);
metricsResponse.setMemAllocated(hostResponse.getMemoryAllocated()); metricsResponse.setMemAllocated(hostResponse.getMemoryAllocated());
metricsResponse.setMemUsed(hostResponse.getMemoryUsed()); metricsResponse.setMemUsed(hostResponse.getMemoryUsed());

View File

@ -43,6 +43,10 @@ public class HostMetricsResponse extends HostResponse {
@Param(description = "the total cpu allocated in Ghz") @Param(description = "the total cpu allocated in Ghz")
private String cpuAllocated; private String cpuAllocated;
@SerializedName("cpuloadaverage")
@Param(description = "the average cpu load the last minute")
private Double loadAverage;
@SerializedName("memorytotalgb") @SerializedName("memorytotalgb")
@Param(description = "the total cpu capacity in GiB") @Param(description = "the total cpu capacity in GiB")
private String memTotal; private String memTotal;
@ -117,6 +121,12 @@ public class HostMetricsResponse extends HostResponse {
} }
} }
public void setLoadAverage(final Double loadAverage) {
if (loadAverage != null) {
this.loadAverage = loadAverage;
}
}
public void setCpuAllocated(final String cpuAllocated, final Integer cpuNumber, final Long cpuSpeed) { public void setCpuAllocated(final String cpuAllocated, final Integer cpuNumber, final Long cpuSpeed) {
if (cpuAllocated != null && cpuNumber != null && cpuSpeed != null) { if (cpuAllocated != null && cpuNumber != null && cpuSpeed != null) {
this.cpuAllocated = String.format("%.2f Ghz", Double.valueOf(cpuAllocated.replace("%", "")) * cpuNumber * cpuSpeed / (100.0 * 1000.0)); this.cpuAllocated = String.format("%.2f Ghz", Double.valueOf(cpuAllocated.replace("%", "")) * cpuNumber * cpuSpeed / (100.0 * 1000.0));

View File

@ -194,6 +194,7 @@ public class HostJoinDaoImpl extends GenericDaoBase<HostJoinVO, Long> implements
float cpuUtil = (float)hostStats.getCpuUtilization(); float cpuUtil = (float)hostStats.getCpuUtilization();
cpuUsed = decimalFormat.format(cpuUtil) + "%"; cpuUsed = decimalFormat.format(cpuUtil) + "%";
hostResponse.setCpuUsed(cpuUsed); hostResponse.setCpuUsed(cpuUsed);
hostResponse.setCpuAverageLoad(hostStats.getLoadAverage());
hostResponse.setMemoryUsed((new Double(hostStats.getUsedMemory())).longValue()); hostResponse.setMemoryUsed((new Double(hostStats.getUsedMemory())).longValue());
hostResponse.setNetworkKbsRead((new Double(hostStats.getNetworkReadKBs())).longValue()); hostResponse.setNetworkKbsRead((new Double(hostStats.getNetworkReadKBs())).longValue());
hostResponse.setNetworkKbsWrite((new Double(hostStats.getNetworkWriteKBs())).longValue()); hostResponse.setNetworkKbsWrite((new Double(hostStats.getNetworkWriteKBs())).longValue());