bug 11051: listHostsCmd - Fixed the capacity and stats response. Also removed the unwarranted code.

This commit is contained in:
Nitin 2011-09-09 14:23:13 +05:30
parent 4e663bbef9
commit 50efe56335
4 changed files with 39 additions and 43 deletions

View File

@ -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);
}

View File

@ -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<UserVmVO> 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) {

View File

@ -496,7 +496,7 @@ public interface ManagementServer extends ManagementService {
*/
List<VMTemplateVO> 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

View File

@ -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();
}