From 7ad3c5e83442fefa5c027d173ac08c16f52382c6 Mon Sep 17 00:00:00 2001 From: "Maneesha.P" Date: Fri, 9 Oct 2015 15:46:56 +0530 Subject: [PATCH 1/3] CLOUDSTACK-8800 : Improved the listVirtualMachines API call to include memory utilization information for a VM for xenserver,kvm and for vmware. --- api/src/com/cloud/vm/VmStats.java | 6 ++++ .../api/response/UserVmResponse.java | 36 +++++++++++++++++++ .../src/com/cloud/agent/api/VmStatsEntry.java | 35 +++++++++++++++++- .../resource/LibvirtComputingResource.java | 13 +++++++ .../LibvirtComputingResourceTest.java | 11 +++++- .../agent/manager/MockVmManagerImpl.java | 2 +- .../vmware/resource/VmwareResource.java | 24 ++++++++++--- .../resource/CitrixResourceBase.java | 12 +++++-- .../api/query/dao/UserVmJoinDaoImpl.java | 15 +++++--- .../src/com/cloud/server/StatsCollector.java | 7 ++++ 10 files changed, 148 insertions(+), 13 deletions(-) mode change 100755 => 100644 plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java diff --git a/api/src/com/cloud/vm/VmStats.java b/api/src/com/cloud/vm/VmStats.java index 2b7943883e19..1c23c01ed8be 100644 --- a/api/src/com/cloud/vm/VmStats.java +++ b/api/src/com/cloud/vm/VmStats.java @@ -32,4 +32,10 @@ public interface VmStats { public double getDiskWriteKBs(); + public double getMemoryKBs(); + + public double getIntFreeMemoryKBs(); + + public double getTargetMemoryKBs(); + } diff --git a/api/src/org/apache/cloudstack/api/response/UserVmResponse.java b/api/src/org/apache/cloudstack/api/response/UserVmResponse.java index d4e612b001ba..b681d4f70df9 100644 --- a/api/src/org/apache/cloudstack/api/response/UserVmResponse.java +++ b/api/src/org/apache/cloudstack/api/response/UserVmResponse.java @@ -196,6 +196,18 @@ public class UserVmResponse extends BaseResponseWithTagInformation implements Co @Param(description = "the write (bytes) of disk on the vm") private Long diskKbsWrite; + @SerializedName("memorykbs") + @Param(description = "the memory used by the vm") + private Long memoryKBs; + + @SerializedName("memoryintfreekbs") + @Param(description = "the internal memory thats free in vm") + private Long memoryIntFreeKBs; + + @SerializedName("memorytargetkbs") + @Param(description = "the target memory in vm") + private Long memoryTargetKBs; + @SerializedName("diskioread") @Param(description = "the read (io) of disk on the vm") private Long diskIORead; @@ -462,6 +474,18 @@ public Long getDiskKbsWrite() { return diskKbsWrite; } + public Long getMemoryKBs() { + return memoryKBs; + } + + public Long getMemoryIntFreeKBs() { + return memoryIntFreeKBs; + } + + public Long getMemoryTargetKBs() { + return memoryTargetKBs; + } + public Long getDiskIORead() { return diskIORead; } @@ -637,6 +661,18 @@ public void setDiskIORead(Long diskIORead) { this.diskIORead = diskIORead; } + public void setMemoryKBs(Long memoryKBs) { + this.memoryKBs = memoryKBs; + } + + public void setMemoryIntFreeKBs(Long memoryIntFreeKBs) { + this.memoryIntFreeKBs = memoryIntFreeKBs; + } + + public void setMemoryTargetKBs(Long memoryTargetKBs) { + this.memoryTargetKBs = memoryTargetKBs; + } + public void setDiskIOWrite(Long diskIOWrite) { this.diskIOWrite = diskIOWrite; } diff --git a/core/src/com/cloud/agent/api/VmStatsEntry.java b/core/src/com/cloud/agent/api/VmStatsEntry.java index 0f30b6af5302..e6063b9ab5b0 100644 --- a/core/src/com/cloud/agent/api/VmStatsEntry.java +++ b/core/src/com/cloud/agent/api/VmStatsEntry.java @@ -30,13 +30,19 @@ public class VmStatsEntry implements VmStats { double diskWriteIOs; double diskReadKBs; double diskWriteKBs; + double memoryKBs; + double intfreememoryKBs; + double targetmemoryKBs; int numCPUs; String entityType; public VmStatsEntry() { } - public VmStatsEntry(double cpuUtilization, double networkReadKBs, double networkWriteKBs, int numCPUs, String entityType) { + public VmStatsEntry(double memoryKBs,double intfreememoryKBs,double targetmemoryKBs, double cpuUtilization, double networkReadKBs, double networkWriteKBs, int numCPUs, String entityType) { + this.memoryKBs = memoryKBs; + this.intfreememoryKBs = intfreememoryKBs; + this.targetmemoryKBs = targetmemoryKBs; this.cpuUtilization = cpuUtilization; this.networkReadKBs = networkReadKBs; this.networkWriteKBs = networkWriteKBs; @@ -117,6 +123,33 @@ public void setDiskWriteKBs(double diskWriteKBs) { this.diskWriteKBs = diskWriteKBs; } + @Override + public double getMemoryKBs() { + return memoryKBs; + } + + public void setMemoryKBs(double memoryKBs) { + this.memoryKBs = memoryKBs; + } + + @Override + public double getIntFreeMemoryKBs() { + return intfreememoryKBs; + } + + public void setIntFreeMemoryKBs(double intfreememoryKBs) { + this.intfreememoryKBs = intfreememoryKBs; + } + + @Override + public double getTargetMemoryKBs() { + return targetmemoryKBs; + } + + public void setTargetMemoryKBs(double targetmemoryKBs) { + this.targetmemoryKBs = targetmemoryKBs; + } + public int getNumCPUs() { return numCPUs; } diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java old mode 100755 new mode 100644 index 883c8c99429d..02cf872d9271 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java @@ -57,6 +57,7 @@ import org.libvirt.Domain; import org.libvirt.DomainBlockStats; import org.libvirt.DomainInfo; +import org.libvirt.MemoryStatistic; import org.libvirt.DomainInfo.DomainState; import org.libvirt.DomainInterfaceStats; import org.libvirt.LibvirtException; @@ -190,6 +191,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv private long _hvVersion; private long _kernelVersion; private int _timeout; + private static final int NUMMEMSTATS =2; private KVMHAMonitor _monitor; public static final String SSHKEYSPATH = "/root/.ssh"; @@ -3018,6 +3020,9 @@ private class VmStats { long _ioWrote; long _bytesRead; long _bytesWrote; + long _intmemfree; + long _memory; + long _maxmemory; Calendar _timestamp; } @@ -3026,11 +3031,16 @@ public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws Li try { dm = getDomain(conn, vmName); final DomainInfo info = dm.getInfo(); + final MemoryStatistic[] mems = dm.memoryStats(NUMMEMSTATS); //number of memory statistics required. final VmStatsEntry stats = new VmStatsEntry(); + stats.setNumCPUs(info.nrVirtCpu); stats.setEntityType("vm"); + stats.setMemoryKBs(info.maxMem); + stats.setTargetMemoryKBs(info.memory); + stats.setIntFreeMemoryKBs((double) mems[0].getValue()); /* get cpu utilization */ VmStats oldStats = null; @@ -3115,6 +3125,9 @@ public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws Li newStat._bytesRead = bytes_rd; newStat._bytesWrote = bytes_wr; newStat._timestamp = now; + newStat._intmemfree = mems[0].getValue(); + newStat._memory = info.memory; + newStat._maxmemory = info.maxMem; _vmStats.put(vmName, newStat); return stats; } finally { diff --git a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java index 9803785a3478..391313e191ff 100644 --- a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java +++ b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java @@ -67,6 +67,8 @@ import org.libvirt.LibvirtException; import org.libvirt.NodeInfo; import org.libvirt.StorageVol; +import org.libvirt.MemoryStatistic; + import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.Mockito; @@ -418,7 +420,10 @@ public void testGetVmStat() throws LibvirtException { final Connect connect = Mockito.mock(Connect.class); final Domain domain = Mockito.mock(Domain.class); final DomainInfo domainInfo = new DomainInfo(); + final MemoryStatistic[] domainMem = new MemoryStatistic[2]; + domainMem[0] = Mockito.mock(MemoryStatistic.class); Mockito.when(domain.getInfo()).thenReturn(domainInfo); + Mockito.when(domain.memoryStats(2)).thenReturn(domainMem); Mockito.when(connect.domainLookupByName(VMNAME)).thenReturn(domain); final NodeInfo nodeInfo = new NodeInfo(); nodeInfo.cpus = 8; @@ -485,6 +490,10 @@ public List getDisks(final Connect conn, final String vmName) { // IO traffic as generated by the logic above, must be greater than zero Assert.assertTrue(vmStat.getDiskReadKBs() > 0); Assert.assertTrue(vmStat.getDiskWriteKBs() > 0); + // Memory limit of VM must be greater than zero + Assert.assertTrue(vmStat.getIntFreeMemoryKBs() >= 0); + Assert.assertTrue(vmStat.getMemoryKBs() >= 0); + Assert.assertTrue(vmStat.getTargetMemoryKBs() >= vmStat.getMemoryKBs()); } @Test @@ -5031,4 +5040,4 @@ public void testIsInterface () { } } } -} \ No newline at end of file +} diff --git a/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockVmManagerImpl.java b/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockVmManagerImpl.java index 5efcd3b57e68..6eaf09cad856 100644 --- a/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockVmManagerImpl.java +++ b/plugins/hypervisors/simulator/src/com/cloud/agent/manager/MockVmManagerImpl.java @@ -319,7 +319,7 @@ public Answer getVmStats(final GetVmStatsCommand cmd) { final HashMap vmStatsNameMap = new HashMap(); final List vmNames = cmd.getVmNames(); for (final String vmName : vmNames) { - final VmStatsEntry entry = new VmStatsEntry(0, 0, 0, 0, "vm"); + final VmStatsEntry entry = new VmStatsEntry(0, 0, 0, 0, 0, 0, 0, "vm"); entry.setNetworkReadKBs(32768); // default values 256 KBps entry.setNetworkWriteKBs(16384); entry.setCPUUtilization(10); diff --git a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java index 184159a94a32..4d04abca4fa7 100644 --- a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java +++ b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java @@ -100,6 +100,7 @@ import org.apache.cloudstack.storage.command.StorageSubSystemCommand; import org.apache.cloudstack.storage.to.TemplateObjectTO; import org.apache.cloudstack.storage.to.VolumeObjectTO; +import org.apache.commons.lang.math.NumberUtils; import com.cloud.agent.IAgentControl; import com.cloud.agent.api.Answer; @@ -4944,8 +4945,14 @@ private HashMap getVmStats(List vmNames) throws Ex } String instanceNameCustomField = "value[" + key + "]"; + final String numCpuStr = "summary.config.numCpu"; + final String cpuUseStr = "summary.quickStats.overallCpuUsage"; + final String guestMemUseStr = "summary.quickStats.guestMemoryUsage"; + final String memLimitStr = "resourceConfig.memoryAllocation.limit"; + final String memMbStr = "config.hardware.memoryMB"; + ObjectContent[] ocs = - hyperHost.getVmPropertiesOnHyperHost(new String[] {"name", "summary.config.numCpu", "summary.quickStats.overallCpuUsage", instanceNameCustomField}); + hyperHost.getVmPropertiesOnHyperHost(new String[] {"name", numCpuStr, cpuUseStr ,guestMemUseStr ,memLimitStr ,memMbStr, instanceNameCustomField}); if (ocs != null && ocs.length > 0) { for (ObjectContent oc : ocs) { List objProps = oc.getPropSet(); @@ -4953,6 +4960,9 @@ private HashMap getVmStats(List vmNames) throws Ex String name = null; String numberCPUs = null; String maxCpuUsage = null; + String memlimit = null; + String memkb = null; + String guestMemusage = null; String vmNameOnVcenter = null; String vmInternalCSName = null; for (DynamicProperty objProp : objProps) { @@ -4961,10 +4971,16 @@ private HashMap getVmStats(List vmNames) throws Ex } else if (objProp.getName().contains(instanceNameCustomField)) { if (objProp.getVal() != null) vmInternalCSName = ((CustomFieldStringValue)objProp.getVal()).getValue(); - } else if (objProp.getName().equals("summary.config.numCpu")) { + }else if(objProp.getName().equals(guestMemusage)){ + guestMemusage = objProp.getVal().toString(); + }else if (objProp.getName().equals(numCpuStr)) { numberCPUs = objProp.getVal().toString(); - } else if (objProp.getName().equals("summary.quickStats.overallCpuUsage")) { + } else if (objProp.getName().equals(cpuUseStr)) { maxCpuUsage = objProp.getVal().toString(); + } else if (objProp.getName().equals(memLimitStr)) { + memlimit = objProp.getVal().toString(); + } else if (objProp.getName().equals(memMbStr)) { + memkb = objProp.getVal().toString(); } } new VirtualMachineMO(hyperHost.getContext(), oc.getObj()); @@ -5033,7 +5049,7 @@ private HashMap getVmStats(List vmNames) throws Ex } } } - vmResponseMap.put(name, new VmStatsEntry(Integer.parseInt(maxCpuUsage), networkReadKBs, networkWriteKBs, Integer.parseInt(numberCPUs), "vm")); + vmResponseMap.put(name, new VmStatsEntry( NumberUtils.toDouble(memkb)*1024,NumberUtils.toDouble(guestMemusage)*1024,NumberUtils.toDouble(memlimit)*1024, NumberUtils.toDouble(maxCpuUsage), networkReadKBs, networkWriteKBs, NumberUtils.toInt(numberCPUs), "vm")); } } } diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java index 155c84bbadf7..6da2c7b02a3b 100644 --- a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java +++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java @@ -3276,7 +3276,7 @@ public HashMap getVmStats(final Connection conn, final Get final HashMap vmResponseMap = new HashMap(); for (final String vmUUID : vmUUIDs) { - vmResponseMap.put(vmUUID, new VmStatsEntry(0, 0, 0, 0, "vm")); + vmResponseMap.put(vmUUID, new VmStatsEntry(0,0,0,0, 0, 0, 0, "vm")); } final Object[] rrdData = getRRDData(conn, 2); // call rrddata with 2 for @@ -3330,7 +3330,14 @@ public HashMap getVmStats(final Connection conn, final Get vmStatsAnswer.setDiskReadKBs(vmStatsAnswer.getDiskReadKBs() + getDataAverage(dataNode, col, numRows) / 1000); } else if (param.matches("vbd_.*_write")) { vmStatsAnswer.setDiskWriteKBs(vmStatsAnswer.getDiskWriteKBs() + getDataAverage(dataNode, col, numRows) / 1000); + } else if (param.contains("memory_internal_free")) { + vmStatsAnswer.setIntFreeMemoryKBs(vmStatsAnswer.getIntFreeMemoryKBs() + getDataAverage(dataNode, col, numRows) / 1024); + } else if (param.contains("memory_target")) { + vmStatsAnswer.setTargetMemoryKBs(vmStatsAnswer.getTargetMemoryKBs() + getDataAverage(dataNode, col, numRows) / 1024); + } else if (param.contains("memory")) { + vmStatsAnswer.setMemoryKBs(vmStatsAnswer.getMemoryKBs() + getDataAverage(dataNode, col, numRows) / 1024); } + } } @@ -3346,6 +3353,7 @@ public HashMap getVmStats(final Connection conn, final Get s_logger.debug("Vm cpu utilization " + vmStatsAnswer.getCPUUtilization()); } } + return vmResponseMap; } @@ -5392,4 +5400,4 @@ public boolean attachConfigDriveToMigratedVm(Connection conn, String vmName, Str } -} \ No newline at end of file +} diff --git a/server/src/com/cloud/api/query/dao/UserVmJoinDaoImpl.java b/server/src/com/cloud/api/query/dao/UserVmJoinDaoImpl.java index 079da57047d3..552200bc297e 100644 --- a/server/src/com/cloud/api/query/dao/UserVmJoinDaoImpl.java +++ b/server/src/com/cloud/api/query/dao/UserVmJoinDaoImpl.java @@ -205,14 +205,21 @@ public UserVmResponse newUserVmResponse(ResponseView view, String objectName, Us userVmResponse.setNetworkKbsWrite((long)vmStats.getNetworkWriteKBs()); - if ((userVm.getHypervisorType() != null) && (userVm.getHypervisorType().equals(HypervisorType.KVM) || userVm.getHypervisorType().equals(HypervisorType.XenServer))) { // support KVM and XenServer only util 2013.06.25 + if ((userVm.getHypervisorType() != null) && (userVm.getHypervisorType().equals(HypervisorType.KVM) || userVm.getHypervisorType().equals(HypervisorType.XenServer) || userVm.getHypervisorType().equals(HypervisorType.VMware))) { // support KVM and XenServer only util 2013.06.25 . supporting Vmware from 2015.09.02 userVmResponse.setDiskKbsRead((long)vmStats.getDiskReadKBs()); - userVmResponse.setDiskKbsWrite((long)vmStats.getDiskWriteKBs()); + userVmResponse.setDiskKbsWrite((long) vmStats.getDiskWriteKBs()); - userVmResponse.setDiskIORead((long)vmStats.getDiskReadIOs()); + userVmResponse.setDiskIORead((long) vmStats.getDiskReadIOs()); + + userVmResponse.setDiskIOWrite((long) vmStats.getDiskWriteIOs()); + + userVmResponse.setMemoryKBs((long) vmStats.getMemoryKBs()); + + userVmResponse.setMemoryIntFreeKBs((long) vmStats.getIntFreeMemoryKBs()); + + userVmResponse.setMemoryTargetKBs((long) vmStats.getTargetMemoryKBs()); - userVmResponse.setDiskIOWrite((long)vmStats.getDiskWriteIOs()); } } } diff --git a/server/src/com/cloud/server/StatsCollector.java b/server/src/com/cloud/server/StatsCollector.java index 0702335308da..387639f4b2e3 100644 --- a/server/src/com/cloud/server/StatsCollector.java +++ b/server/src/com/cloud/server/StatsCollector.java @@ -464,6 +464,9 @@ protected void runInContext() { statsInMemory.setDiskReadIOs(statsInMemory.getDiskReadIOs() + statsForCurrentIteration.getDiskReadIOs()); statsInMemory.setDiskWriteIOs(statsInMemory.getDiskWriteIOs() + statsForCurrentIteration.getDiskWriteIOs()); statsInMemory.setDiskReadKBs(statsInMemory.getDiskReadKBs() + statsForCurrentIteration.getDiskReadKBs()); + statsInMemory.setMemoryKBs(statsForCurrentIteration.getMemoryKBs()); + statsInMemory.setIntFreeMemoryKBs(statsForCurrentIteration.getIntFreeMemoryKBs()); + statsInMemory.setTargetMemoryKBs(statsForCurrentIteration.getTargetMemoryKBs()); _VmStats.put(vmId, statsInMemory); } @@ -484,6 +487,10 @@ protected void runInContext() { metrics.put(externalStatsPrefix + "cloudstack.stats.instances." + vmName + ".disk.read_kbs", statsForCurrentIteration.getDiskReadKBs()); metrics.put(externalStatsPrefix + "cloudstack.stats.instances." + vmName + ".disk.write_iops", statsForCurrentIteration.getDiskWriteIOs()); metrics.put(externalStatsPrefix + "cloudstack.stats.instances." + vmName + ".disk.read_iops", statsForCurrentIteration.getDiskReadIOs()); + metrics.put(externalStatsPrefix + "cloudstack.stats.instances." + vmName + ".memory.total_kbs", statsForCurrentIteration.getMemoryKBs()); + metrics.put(externalStatsPrefix + "cloudstack.stats.instances." + vmName + ".memory.internalfree_kbs", statsForCurrentIteration.getIntFreeMemoryKBs()); + metrics.put(externalStatsPrefix + "cloudstack.stats.instances." + vmName + ".memory.target_kbs", statsForCurrentIteration.getTargetMemoryKBs()); + } } From 866ac7b6e1808e768c29b2491c8b3bfa174f2f78 Mon Sep 17 00:00:00 2001 From: weingartner Date: Fri, 29 Apr 2016 14:25:29 -0300 Subject: [PATCH 2/3] Fixed issues from CLOUDSTACK-8800 that were introduced in PR 780 It was worked around some possible runtime exceptions introduced by the changes that were added by the PR 780. Basically, the points in which a null pointer exception could happen, we added safety checks to avoid them. It was create a specific method do that, all together test cases were created for this newly method that was added. --- .../resource/LibvirtComputingResource.java | 43 +++++++----- .../LibvirtComputingResourceTest.java | 69 ++++++++++++++----- .../resource/CitrixResourceBase.java | 16 +++-- 3 files changed, 84 insertions(+), 44 deletions(-) diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java index 02cf872d9271..0f8e9edb9f2c 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java @@ -52,15 +52,17 @@ import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.math.NumberUtils; import org.apache.log4j.Logger; import org.libvirt.Connect; import org.libvirt.Domain; import org.libvirt.DomainBlockStats; import org.libvirt.DomainInfo; -import org.libvirt.MemoryStatistic; import org.libvirt.DomainInfo.DomainState; import org.libvirt.DomainInterfaceStats; import org.libvirt.LibvirtException; +import org.libvirt.MemoryStatistic; import org.libvirt.NodeInfo; import com.cloud.agent.api.Answer; @@ -189,7 +191,6 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv private String _clusterId; private long _hvVersion; - private long _kernelVersion; private int _timeout; private static final int NUMMEMSTATS =2; @@ -958,13 +959,6 @@ public boolean configure(final String name, final Map params) th storageProcessor.configure(name, params); storageHandler = new StorageSubsystemCommandHandlerBase(storageProcessor); - final String unameKernelVersion = Script.runSimpleBashScript("uname -r"); - final String[] kernelVersions = unameKernelVersion.split("[\\.\\-]"); - _kernelVersion = Integer.parseInt(kernelVersions[0]) * 1000 * 1000 + (long)Integer.parseInt(kernelVersions[1]) * 1000 + Integer.parseInt(kernelVersions[2]); - - /* Disable this, the code using this is pretty bad and non portable - * getOsVersion(); - */ return true; } @@ -3020,9 +3014,6 @@ private class VmStats { long _ioWrote; long _bytesRead; long _bytesWrote; - long _intmemfree; - long _memory; - long _maxmemory; Calendar _timestamp; } @@ -3030,9 +3021,10 @@ public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws Li Domain dm = null; try { dm = getDomain(conn, vmName); - final DomainInfo info = dm.getInfo(); - final MemoryStatistic[] mems = dm.memoryStats(NUMMEMSTATS); //number of memory statistics required. - + if (dm == null) { + return null; + } + DomainInfo info = dm.getInfo(); final VmStatsEntry stats = new VmStatsEntry(); stats.setNumCPUs(info.nrVirtCpu); @@ -3040,7 +3032,8 @@ public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws Li stats.setMemoryKBs(info.maxMem); stats.setTargetMemoryKBs(info.memory); - stats.setIntFreeMemoryKBs((double) mems[0].getValue()); + stats.setIntFreeMemoryKBs(getMemoryFreeInKBs(dm)); + /* get cpu utilization */ VmStats oldStats = null; @@ -3125,9 +3118,6 @@ public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws Li newStat._bytesRead = bytes_rd; newStat._bytesWrote = bytes_wr; newStat._timestamp = now; - newStat._intmemfree = mems[0].getValue(); - newStat._memory = info.memory; - newStat._maxmemory = info.maxMem; _vmStats.put(vmName, newStat); return stats; } finally { @@ -3137,6 +3127,21 @@ public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws Li } } + /** + * This method retrieves the memory statistics from the domain given as parameters. + * If no memory statistic is found, it will return {@link NumberUtils#LONG_ZERO} as the value of free memory in the domain. + * If it can retrieve the domain memory statistics, it will return the free memory statistic; that means, it returns the value at the first position of the array returned by {@link Domain#memoryStats(int)}. + * + * @return the amount of free memory in KBs + */ + protected long getMemoryFreeInKBs(Domain dm) throws LibvirtException { + MemoryStatistic[] mems = dm.memoryStats(NUMMEMSTATS); + if (ArrayUtils.isEmpty(mems)) { + return NumberUtils.LONG_ZERO; + } + return mems[0].getValue(); + } + private boolean canBridgeFirewall(final String prvNic) { final Script cmd = new Script(_securityGroupPath, _timeout, s_logger); cmd.add("can_bridge_firewall"); diff --git a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java index 391313e191ff..488325ea8113 100644 --- a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java +++ b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java @@ -65,10 +65,10 @@ import org.libvirt.DomainInfo.DomainState; import org.libvirt.DomainInterfaceStats; import org.libvirt.LibvirtException; +import org.libvirt.MemoryStatistic; import org.libvirt.NodeInfo; import org.libvirt.StorageVol; -import org.libvirt.MemoryStatistic; - +import org.libvirt.jna.virDomainMemoryStats; import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.Mockito; @@ -182,8 +182,8 @@ public class LibvirtComputingResourceTest { @Mock private LibvirtComputingResource libvirtComputingResource; - String _hyperVisorType = "kvm"; - Random _random = new Random(); + String hyperVisorType = "kvm"; + Random random = new Random(); /** This test tests if the Agent can handle a vmSpec coming @@ -194,10 +194,10 @@ public class LibvirtComputingResourceTest { */ @Test public void testCreateVMFromSpecLegacy() { - final int id = _random.nextInt(65534); + final int id = random.nextInt(65534); final String name = "test-instance-1"; - final int cpus = _random.nextInt(2) + 1; + final int cpus = random.nextInt(2) + 1; final int speed = 1024; final int minRam = 256 * 1024; final int maxRam = 512 * 1024; @@ -213,7 +213,7 @@ public void testCreateVMFromSpecLegacy() { to.setUuid("b0f0a72d-7efb-3cad-a8ff-70ebf30b3af9"); final LibvirtVMDef vm = lcr.createVMFromSpec(to); - vm.setHvsType(_hyperVisorType); + vm.setHvsType(hyperVisorType); verifyVm(to, vm); } @@ -223,7 +223,7 @@ public void testCreateVMFromSpecLegacy() { */ @Test public void testCreateVMFromSpecWithTopology6() { - final int id = _random.nextInt(65534); + final int id = random.nextInt(65534); final String name = "test-instance-1"; final int cpus = 12; @@ -243,7 +243,7 @@ public void testCreateVMFromSpecWithTopology6() { to.setUuid("b0f0a72d-7efb-3cad-a8ff-70ebf30b3af9"); final LibvirtVMDef vm = lcr.createVMFromSpec(to); - vm.setHvsType(_hyperVisorType); + vm.setHvsType(hyperVisorType); verifyVm(to, vm); } @@ -253,7 +253,7 @@ public void testCreateVMFromSpecWithTopology6() { */ @Test public void testCreateVMFromSpecWithTopology4() { - final int id = _random.nextInt(65534); + final int id = random.nextInt(65534); final String name = "test-instance-1"; final int cpus = 8; @@ -273,7 +273,7 @@ public void testCreateVMFromSpecWithTopology4() { to.setUuid("b0f0a72d-7efb-3cad-a8ff-70ebf30b3af9"); final LibvirtVMDef vm = lcr.createVMFromSpec(to); - vm.setHvsType(_hyperVisorType); + vm.setHvsType(hyperVisorType); verifyVm(to, vm); } @@ -287,10 +287,10 @@ public void testCreateVMFromSpecWithTopology4() { */ @Test public void testCreateVMFromSpec() { - final int id = _random.nextInt(65534); + final int id = random.nextInt(65534); final String name = "test-instance-1"; - final int cpus = _random.nextInt(2) + 1; + final int cpus = random.nextInt(2) + 1; final int minSpeed = 1024; final int maxSpeed = 2048; final int minRam = 256 * 1024; @@ -308,7 +308,7 @@ public void testCreateVMFromSpec() { to.setUuid("b0f0a72d-7efb-3cad-a8ff-70ebf30b3af9"); final LibvirtVMDef vm = lcr.createVMFromSpec(to); - vm.setHvsType(_hyperVisorType); + vm.setHvsType(hyperVisorType); verifyVm(to, vm); } @@ -718,10 +718,9 @@ public void testGetVmDiskStatsCommand() { } } - @SuppressWarnings("unchecked") @Test + @SuppressWarnings("unchecked") public void testGetVmDiskStatsCommandException() { - Mockito.mock(Connect.class); final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); final String vmName = "Test"; @@ -941,7 +940,6 @@ public void testRebootRouterCommandConnect() { public void testGetHostStatsCommand() { // A bit difficult to test due to the logger being passed and the parser itself relying on the connection. // Have to spend some more time afterwards in order to refactor the wrapper itself. - Mockito.mock(LibvirtUtilitiesHelper.class); final CPUStat cpuStat = Mockito.mock(CPUStat.class); final MemStat memStat = Mockito.mock(MemStat.class); @@ -3531,7 +3529,6 @@ public void testNetworkUsageCommandVpcNoOption() { verify(libvirtComputingResource, times(1)).configureVPCNetworkUsage(command.getPrivateIP(), command.getGatewayIP(), command.getOption(), command.getVpcCIDR()); } - @SuppressWarnings("unchecked") @Test public void testCreatePrivateTemplateFromVolumeCommand() { //Simple test used to make sure the flow (LibvirtComputingResource => Request => CommandWrapper) is working. @@ -5040,4 +5037,40 @@ public void testIsInterface () { } } } + + @Test + public void testMemoryFreeInKBsDomainReturningOfSomeMemoryStatistics() throws LibvirtException { + LibvirtComputingResource libvirtComputingResource = new LibvirtComputingResource(); + + MemoryStatistic[] mem = createMemoryStatisticFreeMemory100(); + Domain domainMock = getDomainConfiguredToReturnMemoryStatistic(mem); + long memoryFreeInKBs = libvirtComputingResource.getMemoryFreeInKBs(domainMock); + + Assert.assertEquals(100, memoryFreeInKBs); + } + + @Test + public void testMemoryFreeInKBsDomainReturningNoMemoryStatistics() throws LibvirtException { + LibvirtComputingResource libvirtComputingResource = new LibvirtComputingResource(); + + Domain domainMock = getDomainConfiguredToReturnMemoryStatistic(null); + long memoryFreeInKBs = libvirtComputingResource.getMemoryFreeInKBs(domainMock); + + Assert.assertEquals(0, memoryFreeInKBs); + } + + private MemoryStatistic[] createMemoryStatisticFreeMemory100() { + virDomainMemoryStats stat = new virDomainMemoryStats(); + stat.val = 100; + + MemoryStatistic[] mem = new MemoryStatistic[2]; + mem[0] = new MemoryStatistic(stat); + return mem; + } + + private Domain getDomainConfiguredToReturnMemoryStatistic(MemoryStatistic[] mem) throws LibvirtException { + Domain domainMock = Mockito.mock(Domain.class); + when(domainMock.memoryStats(2)).thenReturn(mem); + return domainMock; + } } diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java index 6da2c7b02a3b..efc3f81ed193 100644 --- a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java +++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java @@ -184,6 +184,8 @@ public String toString() { } } + private final static int BASE_TO_CONVERT_BYTES_INTO_KILOBYTES = 1024; + protected static final XenServerConnectionPool ConnPool = XenServerConnectionPool.getInstance(); // static min values for guests on xenserver private static final long mem_128m = 134217728L; @@ -3323,19 +3325,19 @@ public HashMap getVmStats(final Connection conn, final Get vmStatsAnswer.setNumCPUs(vmStatsAnswer.getNumCPUs() + 1); vmStatsAnswer.setCPUUtilization(vmStatsAnswer.getCPUUtilization() + getDataAverage(dataNode, col, numRows)); } else if (param.matches("vif_\\d*_rx")) { - vmStatsAnswer.setNetworkReadKBs(vmStatsAnswer.getNetworkReadKBs() + getDataAverage(dataNode, col, numRows) / 1000); + vmStatsAnswer.setNetworkReadKBs(vmStatsAnswer.getNetworkReadKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES); } else if (param.matches("vif_\\d*_tx")) { - vmStatsAnswer.setNetworkWriteKBs(vmStatsAnswer.getNetworkWriteKBs() + getDataAverage(dataNode, col, numRows) / 1000); + vmStatsAnswer.setNetworkWriteKBs(vmStatsAnswer.getNetworkWriteKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES); } else if (param.matches("vbd_.*_read")) { - vmStatsAnswer.setDiskReadKBs(vmStatsAnswer.getDiskReadKBs() + getDataAverage(dataNode, col, numRows) / 1000); + vmStatsAnswer.setDiskReadKBs(vmStatsAnswer.getDiskReadKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES); } else if (param.matches("vbd_.*_write")) { - vmStatsAnswer.setDiskWriteKBs(vmStatsAnswer.getDiskWriteKBs() + getDataAverage(dataNode, col, numRows) / 1000); + vmStatsAnswer.setDiskWriteKBs(vmStatsAnswer.getDiskWriteKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES); } else if (param.contains("memory_internal_free")) { - vmStatsAnswer.setIntFreeMemoryKBs(vmStatsAnswer.getIntFreeMemoryKBs() + getDataAverage(dataNode, col, numRows) / 1024); + vmStatsAnswer.setIntFreeMemoryKBs(vmStatsAnswer.getIntFreeMemoryKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES); } else if (param.contains("memory_target")) { - vmStatsAnswer.setTargetMemoryKBs(vmStatsAnswer.getTargetMemoryKBs() + getDataAverage(dataNode, col, numRows) / 1024); + vmStatsAnswer.setTargetMemoryKBs(vmStatsAnswer.getTargetMemoryKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES); } else if (param.contains("memory")) { - vmStatsAnswer.setMemoryKBs(vmStatsAnswer.getMemoryKBs() + getDataAverage(dataNode, col, numRows) / 1024); + vmStatsAnswer.setMemoryKBs(vmStatsAnswer.getMemoryKBs() + getDataAverage(dataNode, col, numRows) / BASE_TO_CONVERT_BYTES_INTO_KILOBYTES); } } From 417d9a5d800facc48b6df46ada2e73223a16b5dd Mon Sep 17 00:00:00 2001 From: weingartner Date: Wed, 4 May 2016 15:27:17 -0300 Subject: [PATCH 3/3] =?UTF-8?q?Removed=20unnecessary=20check=20when=20crea?= =?UTF-8?q?ting=20the=20=E2=80=9CuserVmResponse=E2=80=9D=20object.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/query/dao/UserVmJoinDaoImpl.java | 31 ++++++------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/server/src/com/cloud/api/query/dao/UserVmJoinDaoImpl.java b/server/src/com/cloud/api/query/dao/UserVmJoinDaoImpl.java index 552200bc297e..106cd25a5b0b 100644 --- a/server/src/com/cloud/api/query/dao/UserVmJoinDaoImpl.java +++ b/server/src/com/cloud/api/query/dao/UserVmJoinDaoImpl.java @@ -27,9 +27,6 @@ import javax.inject.Inject; -import org.apache.log4j.Logger; -import org.springframework.stereotype.Component; - import org.apache.cloudstack.affinity.AffinityGroupResponse; import org.apache.cloudstack.api.ApiConstants.VMDetails; import org.apache.cloudstack.api.ResponseObject.ResponseView; @@ -38,11 +35,12 @@ import org.apache.cloudstack.api.response.SecurityGroupResponse; import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.framework.config.dao.ConfigurationDao; +import org.apache.log4j.Logger; +import org.springframework.stereotype.Component; import com.cloud.api.ApiDBUtils; import com.cloud.api.query.vo.UserVmJoinVO; import com.cloud.gpu.GPU; -import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.service.ServiceOfferingDetailsVO; import com.cloud.user.Account; import com.cloud.user.AccountManager; @@ -200,27 +198,16 @@ public UserVmResponse newUserVmResponse(ResponseView view, String objectName, Us VmStats vmStats = ApiDBUtils.getVmStatistics(userVm.getId()); if (vmStats != null) { userVmResponse.setCpuUsed(new DecimalFormat("#.##").format(vmStats.getCPUUtilization()) + "%"); - userVmResponse.setNetworkKbsRead((long)vmStats.getNetworkReadKBs()); - userVmResponse.setNetworkKbsWrite((long)vmStats.getNetworkWriteKBs()); + userVmResponse.setDiskKbsRead((long)vmStats.getDiskReadKBs()); + userVmResponse.setDiskKbsWrite((long)vmStats.getDiskWriteKBs()); + userVmResponse.setDiskIORead((long)vmStats.getDiskReadIOs()); + userVmResponse.setDiskIOWrite((long)vmStats.getDiskWriteIOs()); + userVmResponse.setMemoryKBs((long)vmStats.getMemoryKBs()); + userVmResponse.setMemoryIntFreeKBs((long)vmStats.getIntFreeMemoryKBs()); + userVmResponse.setMemoryTargetKBs((long)vmStats.getTargetMemoryKBs()); - if ((userVm.getHypervisorType() != null) && (userVm.getHypervisorType().equals(HypervisorType.KVM) || userVm.getHypervisorType().equals(HypervisorType.XenServer) || userVm.getHypervisorType().equals(HypervisorType.VMware))) { // support KVM and XenServer only util 2013.06.25 . supporting Vmware from 2015.09.02 - userVmResponse.setDiskKbsRead((long)vmStats.getDiskReadKBs()); - - userVmResponse.setDiskKbsWrite((long) vmStats.getDiskWriteKBs()); - - userVmResponse.setDiskIORead((long) vmStats.getDiskReadIOs()); - - userVmResponse.setDiskIOWrite((long) vmStats.getDiskWriteIOs()); - - userVmResponse.setMemoryKBs((long) vmStats.getMemoryKBs()); - - userVmResponse.setMemoryIntFreeKBs((long) vmStats.getIntFreeMemoryKBs()); - - userVmResponse.setMemoryTargetKBs((long) vmStats.getTargetMemoryKBs()); - - } } }