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 2b7943883e1..1c23c01ed8b 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 d4e612b001b..b681d4f70df 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 class UserVmResponse extends BaseResponseWithTagInformation implements Co 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 class UserVmResponse extends BaseResponseWithTagInformation implements Co 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 0f30b6af530..e6063b9ab5b 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 class VmStatsEntry implements VmStats { 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 883c8c99429..02cf872d927 --- 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.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; @@ -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 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv long _ioWrote; long _bytesRead; long _bytesWrote; + long _intmemfree; + long _memory; + long _maxmemory; Calendar _timestamp; } @@ -3026,11 +3031,16 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv 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 class LibvirtComputingResource extends ServerResourceBase implements Serv 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 9803785a347..391313e191f 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.DomainInterfaceStats; 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 class LibvirtComputingResourceTest { 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 class LibvirtComputingResourceTest { // 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 class LibvirtComputingResourceTest { } } } -} \ 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 5efcd3b57e6..6eaf09cad85 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 class MockVmManagerImpl extends ManagerBase implements MockVmManager { 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 184159a94a3..4d04abca4fa 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 com.vmware.vim25.VmwareDistributedVirtualSwitchVlanIdSpec; 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 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } 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 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa 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 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } 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 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } } } - 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 155c84bbadf..6da2c7b02a3 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 abstract class CitrixResourceBase implements ServerResource, HypervisorRe 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 abstract class CitrixResourceBase implements ServerResource, HypervisorRe 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 abstract class CitrixResourceBase implements ServerResource, HypervisorRe s_logger.debug("Vm cpu utilization " + vmStatsAnswer.getCPUUtilization()); } } + return vmResponseMap; } @@ -5392,4 +5400,4 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } -} \ 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 079da57047d..552200bc297 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 class UserVmJoinDaoImpl extends GenericDaoBaseWithTagInformation 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 02cf872d927..0f8e9edb9f2 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.linux.MemStat; 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 class LibvirtComputingResource extends ServerResourceBase implements Serv 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 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv long _ioWrote; long _bytesRead; long _bytesWrote; - long _intmemfree; - long _memory; - long _maxmemory; Calendar _timestamp; } @@ -3030,9 +3021,10 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv 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 class LibvirtComputingResource extends ServerResourceBase implements Serv 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 class LibvirtComputingResource extends ServerResourceBase implements Serv 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 class LibvirtComputingResource extends ServerResourceBase implements Serv } } + /** + * 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 391313e191f..488325ea811 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; 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 class LibvirtComputingResourceTest { 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 class LibvirtComputingResourceTest { */ @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 class LibvirtComputingResourceTest { 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 class LibvirtComputingResourceTest { */ @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 class LibvirtComputingResourceTest { 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 class LibvirtComputingResourceTest { */ @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 class LibvirtComputingResourceTest { 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 class LibvirtComputingResourceTest { } } - @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 class LibvirtComputingResourceTest { 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 class LibvirtComputingResourceTest { 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 class LibvirtComputingResourceTest { } } } + + @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 6da2c7b02a3..efc3f81ed19 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 abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } + 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 abstract class CitrixResourceBase implements ServerResource, HypervisorRe 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 552200bc297..106cd25a5b0 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 java.util.Set; 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.NicSecondaryIpResponse; 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 class UserVmJoinDaoImpl extends GenericDaoBaseWithTagInformation