From 50efe563355911162c7418e9b98064a0ad333ff9 Mon Sep 17 00:00:00 2001 From: Nitin Date: Fri, 9 Sep 2011 14:23:13 +0530 Subject: [PATCH] bug 11051: listHostsCmd - Fixed the capacity and stats response. Also removed the unwarranted code. --- server/src/com/cloud/api/ApiDBUtils.java | 12 +++- .../src/com/cloud/api/ApiResponseHelper.java | 64 ++++++++----------- .../com/cloud/server/ManagementServer.java | 2 +- .../cloud/server/ManagementServerImpl.java | 4 +- 4 files changed, 39 insertions(+), 43 deletions(-) diff --git a/server/src/com/cloud/api/ApiDBUtils.java b/server/src/com/cloud/api/ApiDBUtils.java index 16624cb61b0..193acbbb21f 100755 --- a/server/src/com/cloud/api/ApiDBUtils.java +++ b/server/src/com/cloud/api/ApiDBUtils.java @@ -223,14 +223,22 @@ public class ApiDBUtils { return _ms.findVMInstanceById(vmId); } - public static long getMemoryUsagebyHost(Long hostId) { + public static long getMemoryOrCpuCapacitybyHost(Long hostId, short capacityType) { // TODO: This method is for the API only, but it has configuration values (ramSize for system vms) // so if this Utils class can have some kind of config rather than a static initializer (maybe from // management server instantiation?) then maybe the management server method can be moved entirely // into this utils class. - return _ms.getMemoryUsagebyHost(hostId); + return _ms.getMemoryOrCpuCapacityByHost(hostId,capacityType); } + public static long getStorageCapacitybyPool(Long poolId, short capacityType) { + // TODO: This method is for the API only, but it has configuration values (ramSize for system vms) + // so if this Utils class can have some kind of config rather than a static initializer (maybe from + // management server instantiation?) then maybe the management server method can be moved entirely + // into this utils class. + return _ms.getMemoryOrCpuCapacityByHost(poolId, capacityType); + } + public static Long getPodIdForVlan(long vlanDbId) { return _ms.getPodIdForVlan(vlanDbId); } diff --git a/server/src/com/cloud/api/ApiResponseHelper.java b/server/src/com/cloud/api/ApiResponseHelper.java index 9d17aa85cec..2129f9dbd06 100755 --- a/server/src/com/cloud/api/ApiResponseHelper.java +++ b/server/src/com/cloud/api/ApiResponseHelper.java @@ -549,47 +549,35 @@ public class ApiResponseHelper implements ResponseGenerator { } } - DecimalFormat decimalFormat = new DecimalFormat("#.##"); - - // calculate cpu allocated by vm - if ((host.getCpus() != null) && (host.getSpeed() != null)) { - int cpu = 0; - String cpuAlloc = null; - List instances = ApiDBUtils.listUserVMsByHostId(host.getId()); - for (UserVmVO vm : instances) { - ServiceOffering so = ApiDBUtils.findServiceOfferingById(vm.getServiceOfferingId()); - cpu += so.getCpu() * so.getSpeed(); - } - cpuAlloc = decimalFormat.format(((float) cpu / (float) (host.getCpus() * host.getSpeed())) * 100f) + "%"; - hostResponse.setCpuAllocated(cpuAlloc); - - String cpuWithOverprovisioning = new Float(host.getCpus() * host.getSpeed() * ApiDBUtils.getCpuOverprovisioningFactor()).toString(); - hostResponse.setCpuWithOverprovisioning(cpuWithOverprovisioning); - } - - // calculate cpu utilized - String cpuUsed = null; - HostStats hostStats = ApiDBUtils.getHostStatistics(host.getId()); - if (hostStats != null) { - float cpuUtil = (float) hostStats.getCpuUtilization(); - cpuUsed = decimalFormat.format(cpuUtil) + "%"; - hostResponse.setCpuUsed(cpuUsed); - hostResponse.setNetworkKbsRead((new Double(hostStats.getNetworkReadKBs())).longValue()); - hostResponse.setNetworkKbsWrite((new Double(hostStats.getNetworkWriteKBs())).longValue()); - } - - if (host.getType() == Host.Type.Routing) { + DecimalFormat decimalFormat = new DecimalFormat("#.##"); + if (host.getType() == Host.Type.Routing) { + //set allocated capacities + Long mem = ApiDBUtils.getMemoryOrCpuCapacitybyHost(host.getId(),Capacity.CAPACITY_TYPE_MEMORY); + Long cpu = ApiDBUtils.getMemoryOrCpuCapacitybyHost(host.getId(),Capacity.CAPACITY_TYPE_CPU); + + hostResponse.setMemoryAllocated(mem); hostResponse.setMemoryTotal(host.getTotalMemory()); - - // calculate memory allocated by systemVM and userVm - Long mem = ApiDBUtils.getMemoryUsagebyHost(host.getId()); - hostResponse.setMemoryAllocated(mem); - hostResponse.setMemoryUsed(mem); hostResponse.setHostTags(ApiDBUtils.getHostTags(host.getId())); hostResponse.setHypervisorVersion(host.getHypervisorVersion()); - } else if (host.getType().toString().equals("Storage")) { - hostResponse.setDiskSizeTotal(host.getTotalSize()); - hostResponse.setDiskSizeAllocated(0L); + + String cpuAlloc = decimalFormat.format(((float) cpu / (float) (host.getCpus() * host.getSpeed())) * 100f) + "%"; + hostResponse.setCpuAllocated(cpuAlloc); + String cpuWithOverprovisioning = new Float(host.getCpus() * host.getSpeed() * ApiDBUtils.getCpuOverprovisioningFactor()).toString(); + hostResponse.setCpuWithOverprovisioning(cpuWithOverprovisioning); + + // set CPU/RAM/Network stats + String cpuUsed = null; + HostStats hostStats = ApiDBUtils.getHostStatistics(host.getId()); + if (hostStats != null) { + float cpuUtil = (float) hostStats.getCpuUtilization(); + cpuUsed = decimalFormat.format(cpuUtil) + "%"; + hostResponse.setCpuUsed(cpuUsed); + hostResponse.setMemoryUsed( (new Double(hostStats.getUsedMemory())).longValue()); + hostResponse.setNetworkKbsRead((new Double(hostStats.getNetworkReadKBs())).longValue()); + hostResponse.setNetworkKbsWrite((new Double(hostStats.getNetworkWriteKBs())).longValue()); + + } + } if (host.getClusterId() != null) { diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index b399b7783d9..200ca69374d 100755 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -496,7 +496,7 @@ public interface ManagementServer extends ManagementService { */ List listIsos(Criteria c); - public long getMemoryUsagebyHost(Long hostId); + public long getMemoryOrCpuCapacityByHost(Long hostId, short capacityType); /** * List private templates for which the given account/domain has been granted permission to launch instances diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 0ebae8c614a..d537f84ec52 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -3325,9 +3325,9 @@ public class ManagementServerImpl implements ManagementServer { } @Override - public long getMemoryUsagebyHost(Long hostId) { + public long getMemoryOrCpuCapacityByHost(Long hostId, short capacityType) { - CapacityVO capacity = _capacityDao.findByHostIdType(hostId, CapacityVO.CAPACITY_TYPE_MEMORY); + CapacityVO capacity = _capacityDao.findByHostIdType(hostId, capacityType); return capacity == null ? 0 : capacity.getReservedCapacity() + capacity.getUsedCapacity(); }