From 09de5e0b51d1cd0e1d84eef120ae48358300fdcd Mon Sep 17 00:00:00 2001 From: Bharat Kumar Date: Fri, 20 Nov 2015 14:00:38 +0530 Subject: [PATCH] CLOUDSTACK-9665 List hosts api dose not report correct cpu and memory usage. --- .../response/HostForMigrationResponse.java | 29 ++++++++++++------- .../cloudstack/api/response/HostResponse.java | 20 +++++++++---- server/src/com/cloud/api/ApiDBUtils.java | 13 +++++---- .../cloud/api/query/dao/HostJoinDaoImpl.java | 22 ++++++++------ 4 files changed, 54 insertions(+), 30 deletions(-) diff --git a/api/src/org/apache/cloudstack/api/response/HostForMigrationResponse.java b/api/src/org/apache/cloudstack/api/response/HostForMigrationResponse.java index 2f3c86b0a0c..8cc31097205 100644 --- a/api/src/org/apache/cloudstack/api/response/HostForMigrationResponse.java +++ b/api/src/org/apache/cloudstack/api/response/HostForMigrationResponse.java @@ -107,6 +107,15 @@ public class HostForMigrationResponse extends BaseResponse { @Param(description = "the amount of the host's CPU after applying the cpu.overprovisioning.factor ") private String cpuWithOverprovisioning; + @Deprecated + @SerializedName("memorytotal") + @Param(description = "the memory total of the host, this parameter is deprecated use memorywithoverprovisioning") + private Long memoryTotal; + + @SerializedName("memorywithoverprovisioning") + @Param(description = "the amount of the host's memory after applying the mem.overprovisioning.factor ") + private String memWithOverprovisioning; + @SerializedName("averageload") @Param(description = "the cpu average load on the host") private Long averageLoad; @@ -119,13 +128,9 @@ public class HostForMigrationResponse extends BaseResponse { @Param(description = "the outgoing network traffic on the host") private Long networkKbsWrite; - @SerializedName("memorytotal") - @Param(description = "the memory total of the host") - private Long memoryTotal; - @SerializedName("memoryallocated") @Param(description = "the amount of the host's memory currently allocated") - private Long memoryAllocated; + private String memoryAllocated; @SerializedName("memoryused") @Param(description = "the amount of the host's memory currently used") @@ -305,11 +310,7 @@ public class HostForMigrationResponse extends BaseResponse { this.networkKbsWrite = networkKbsWrite; } - public void setMemoryTotal(Long memoryTotal) { - this.memoryTotal = memoryTotal; - } - - public void setMemoryAllocated(Long memoryAllocated) { + public void setMemoryAllocated(String memoryAllocated) { this.memoryAllocated = memoryAllocated; } @@ -401,6 +402,10 @@ public class HostForMigrationResponse extends BaseResponse { this.cpuWithOverprovisioning = cpuWithOverprovisioning; } + public void setMemWithOverprovisioning(String memWithOverprovisioning){ + this.memWithOverprovisioning=memWithOverprovisioning; + } + public void setHypervisorVersion(String hypervisorVersion) { this.hypervisorVersion = hypervisorVersion; } @@ -408,4 +413,8 @@ public class HostForMigrationResponse extends BaseResponse { public void setHaHost(Boolean haHost) { this.haHost = haHost; } + + public void setMemoryTotal(Long memoryTotal) { + this.memoryTotal = memoryTotal; + } } diff --git a/api/src/org/apache/cloudstack/api/response/HostResponse.java b/api/src/org/apache/cloudstack/api/response/HostResponse.java index 90fe800a8bc..1a3a80ce0bf 100644 --- a/api/src/org/apache/cloudstack/api/response/HostResponse.java +++ b/api/src/org/apache/cloudstack/api/response/HostResponse.java @@ -125,13 +125,18 @@ public class HostResponse extends BaseResponse { @Param(description = "the outgoing network traffic on the host") private Long networkKbsWrite; + @Deprecated @SerializedName("memorytotal") - @Param(description = "the memory total of the host") + @Param(description = "the memory total of the host, this parameter is deprecated use memorywithoverprovisioning") private Long memoryTotal; + @SerializedName("memorywithoverprovisioning") + @Param(description = "the amount of the host's memory after applying the mem.overprovisioning.factor") + private String memWithOverprovisioning; + @SerializedName("memoryallocated") @Param(description = "the amount of the host's memory currently allocated") - private Long memoryAllocated; + private long memoryAllocated; @SerializedName("memoryused") @Param(description = "the amount of the host's memory currently used") @@ -328,11 +333,11 @@ public class HostResponse extends BaseResponse { this.networkKbsWrite = networkKbsWrite; } - public void setMemoryTotal(Long memoryTotal) { - this.memoryTotal = memoryTotal; + public void setMemWithOverprovisioning(String memWithOverprovisioning){ + this.memWithOverprovisioning=memWithOverprovisioning; } - public void setMemoryAllocated(Long memoryAllocated) { + public void setMemoryAllocated(long memoryAllocated) { this.memoryAllocated = memoryAllocated; } @@ -458,6 +463,9 @@ public class HostResponse extends BaseResponse { } + public void setMemoryTotal(Long memoryTotal) { + this.memoryTotal = memoryTotal; + } public String getName() { return name; } @@ -542,7 +550,7 @@ public class HostResponse extends BaseResponse { return memoryTotal; } - public Long getMemoryAllocated() { + public long getMemoryAllocated() { return memoryAllocated; } diff --git a/server/src/com/cloud/api/ApiDBUtils.java b/server/src/com/cloud/api/ApiDBUtils.java index 6a2b28750f6..5c5abc99bfe 100644 --- a/server/src/com/cloud/api/ApiDBUtils.java +++ b/server/src/com/cloud/api/ApiDBUtils.java @@ -289,7 +289,6 @@ import com.cloud.user.dao.UserDao; import com.cloud.user.dao.UserStatisticsDao; import com.cloud.uservm.UserVm; import com.cloud.utils.EnumUtils; -import com.cloud.utils.NumbersUtil; import com.cloud.utils.Pair; import com.cloud.vm.ConsoleProxyVO; import com.cloud.vm.DomainRouterVO; @@ -1275,10 +1274,14 @@ public class ApiDBUtils { return s_networkModel.getDedicatedNetworkDomain(networkId); } - public static float getCpuOverprovisioningFactor() { - String opFactor = s_configDao.getValue(CapacityManager.CpuOverprovisioningFactorCK); - float cpuOverprovisioningFactor = NumbersUtil.parseFloat(opFactor, 1); - return cpuOverprovisioningFactor; + public static float getCpuOverprovisioningFactor(long clusterId) { + float opFactor = CapacityManager.CpuOverprovisioningFactor.valueIn(clusterId); + return opFactor; + } + + public static float getMemOverprovisioningFactor(long clusterId) { + float opFactor = CapacityManager.MemOverprovisioningFactor.valueIn(clusterId); + return opFactor; } public static boolean isExtractionDisabled() { diff --git a/server/src/com/cloud/api/query/dao/HostJoinDaoImpl.java b/server/src/com/cloud/api/query/dao/HostJoinDaoImpl.java index 58a03664c13..3d8cc948944 100644 --- a/server/src/com/cloud/api/query/dao/HostJoinDaoImpl.java +++ b/server/src/com/cloud/api/query/dao/HostJoinDaoImpl.java @@ -154,8 +154,10 @@ public class HostJoinDaoImpl extends GenericDaoBase implements Long mem = host.getMemReservedCapacity() + host.getMemUsedCapacity(); Long cpu = host.getCpuReservedCapacity() + host.getCpuUsedCapacity(); + hostResponse.setMemoryTotal(mem); + Float totalMemorywithOverprovisioning=new Float((host.getTotalMemory()*ApiDBUtils.getMemOverprovisioningFactor(host.getClusterId()))); + hostResponse.setMemWithOverprovisioning(totalMemorywithOverprovisioning.toString()); hostResponse.setMemoryAllocated(mem); - hostResponse.setMemoryTotal(host.getTotalMemory()); String hostTags = host.getTag(); hostResponse.setHostTags(host.getTag()); @@ -173,10 +175,10 @@ public class HostJoinDaoImpl extends GenericDaoBase implements hostResponse.setHypervisorVersion(host.getHypervisorVersion()); - String cpuAlloc = decimalFormat.format(((float)cpu / (float)(host.getCpus() * host.getSpeed())) * 100f) + "%"; + Float cpuWithOverprovisioning = new Float(host.getCpus() * host.getSpeed() * ApiDBUtils.getCpuOverprovisioningFactor(host.getClusterId())); + String cpuAlloc = decimalFormat.format(((float)cpu / cpuWithOverprovisioning * 100f)) + "%"; hostResponse.setCpuAllocated(cpuAlloc); - String cpuWithOverprovisioning = new Float(host.getCpus() * host.getSpeed() * ApiDBUtils.getCpuOverprovisioningFactor()).toString(); - hostResponse.setCpuWithOverprovisioning(cpuWithOverprovisioning); + hostResponse.setCpuWithOverprovisioning(cpuWithOverprovisioning.toString()); } if (details.contains(HostDetails.all) || details.contains(HostDetails.stats)) { @@ -297,8 +299,10 @@ public class HostJoinDaoImpl extends GenericDaoBase implements Long mem = host.getMemReservedCapacity() + host.getMemUsedCapacity(); Long cpu = host.getCpuReservedCapacity() + host.getCpuReservedCapacity(); - hostResponse.setMemoryAllocated(mem); - hostResponse.setMemoryTotal(host.getTotalMemory()); + hostResponse.setMemoryTotal(mem); + Float memWithOverprovisioning =new Float((host.getTotalMemory()*ApiDBUtils.getMemOverprovisioningFactor(host.getClusterId()))); + hostResponse.setMemWithOverprovisioning(memWithOverprovisioning.toString()); + hostResponse.setMemoryAllocated(decimalFormat.format((float)mem/ memWithOverprovisioning*100f).toString()+"%"); String hostTags = host.getTag(); hostResponse.setHostTags(host.getTag()); @@ -316,10 +320,10 @@ public class HostJoinDaoImpl extends GenericDaoBase implements hostResponse.setHypervisorVersion(host.getHypervisorVersion()); - String cpuAlloc = decimalFormat.format(((float)cpu / (float)(host.getCpus() * host.getSpeed())) * 100f) + "%"; + Float cpuWithOverprovisioning = new Float(host.getCpus() * host.getSpeed() * ApiDBUtils.getCpuOverprovisioningFactor(host.getClusterId())); + String cpuAlloc = decimalFormat.format(((float)cpu / cpuWithOverprovisioning * 100f)).toString() + "%"; hostResponse.setCpuAllocated(cpuAlloc); - String cpuWithOverprovisioning = new Float(host.getCpus() * host.getSpeed() * ApiDBUtils.getCpuOverprovisioningFactor()).toString(); - hostResponse.setCpuWithOverprovisioning(cpuWithOverprovisioning); + hostResponse.setCpuWithOverprovisioning(cpuWithOverprovisioning.toString()); } if (details.contains(HostDetails.all) || details.contains(HostDetails.stats)) {