Search in sources :

Example 1 with MemoryStatistic

use of org.libvirt.MemoryStatistic in project cloudstack by apache.

the class LibvirtComputingResourceTest method testGetVmStat.

@Test
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;
    nodeInfo.memory = 8 * 1024 * 1024;
    nodeInfo.sockets = 2;
    nodeInfo.threads = 2;
    nodeInfo.model = "Foo processor";
    Mockito.when(connect.nodeInfo()).thenReturn(nodeInfo);
    // this is testing the interface stats, returns an increasing number of sent and received bytes
    Mockito.when(domain.interfaceStats(Matchers.anyString())).thenAnswer(new org.mockito.stubbing.Answer<DomainInterfaceStats>() {

        // increment with less than a KB, so this should be less than 1 KB
        static final int increment = 1000;

        int rxBytes = 1000;

        int txBytes = 1000;

        @Override
        public DomainInterfaceStats answer(final InvocationOnMock invocation) throws Throwable {
            final DomainInterfaceStats domainInterfaceStats = new DomainInterfaceStats();
            domainInterfaceStats.rx_bytes = rxBytes += increment;
            domainInterfaceStats.tx_bytes = txBytes += increment;
            return domainInterfaceStats;
        }
    });
    Mockito.when(domain.blockStats(Matchers.anyString())).thenAnswer(new org.mockito.stubbing.Answer<DomainBlockStats>() {

        // a little less than a KB
        static final int increment = 1000;

        int rdBytes = 0;

        int wrBytes = 1024;

        @Override
        public DomainBlockStats answer(final InvocationOnMock invocation) throws Throwable {
            final DomainBlockStats domainBlockStats = new DomainBlockStats();
            domainBlockStats.rd_bytes = rdBytes += increment;
            domainBlockStats.wr_bytes = wrBytes += increment;
            return domainBlockStats;
        }
    });
    final LibvirtComputingResource libvirtComputingResource = new LibvirtComputingResource() {

        @Override
        public List<InterfaceDef> getInterfaces(final Connect conn, final String vmName) {
            final InterfaceDef interfaceDef = new InterfaceDef();
            return Arrays.asList(interfaceDef);
        }

        @Override
        public List<DiskDef> getDisks(final Connect conn, final String vmName) {
            final DiskDef diskDef = new DiskDef();
            return Arrays.asList(diskDef);
        }
    };
    libvirtComputingResource.getVmStat(connect, VMNAME);
    final VmStatsEntry vmStat = libvirtComputingResource.getVmStat(connect, VMNAME);
    // network traffic as generated by the logic above, must be greater than zero
    Assert.assertTrue(vmStat.getNetworkReadKBs() > 0);
    Assert.assertTrue(vmStat.getNetworkWriteKBs() > 0);
    // 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());
}
Also used : DomainInterfaceStats(org.libvirt.DomainInterfaceStats) Connect(org.libvirt.Connect) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) MemoryStatistic(org.libvirt.MemoryStatistic) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) NodeInfo(org.libvirt.NodeInfo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DomainBlockStats(org.libvirt.DomainBlockStats) DomainInfo(org.libvirt.DomainInfo) Domain(org.libvirt.Domain) Test(org.junit.Test)

Example 2 with MemoryStatistic

use of org.libvirt.MemoryStatistic in project cloudstack by apache.

the class LibvirtComputingResourceTest method testMemoryFreeInKBsDomainReturningOfSomeMemoryStatistics.

@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);
}
Also used : Domain(org.libvirt.Domain) MemoryStatistic(org.libvirt.MemoryStatistic) Test(org.junit.Test)

Example 3 with MemoryStatistic

use of org.libvirt.MemoryStatistic in project cloudstack by apache.

the class LibvirtComputingResourceTest method createMemoryStatisticFreeMemory100.

private MemoryStatistic[] createMemoryStatisticFreeMemory100() {
    virDomainMemoryStats stat = new virDomainMemoryStats();
    stat.val = 100;
    MemoryStatistic[] mem = new MemoryStatistic[2];
    mem[0] = new MemoryStatistic(stat);
    return mem;
}
Also used : org.libvirt.jna.virDomainMemoryStats(org.libvirt.jna.virDomainMemoryStats) MemoryStatistic(org.libvirt.MemoryStatistic)

Aggregations

MemoryStatistic (org.libvirt.MemoryStatistic)3 Test (org.junit.Test)2 Domain (org.libvirt.Domain)2 VmStatsEntry (com.cloud.agent.api.VmStatsEntry)1 DiskDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef)1 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)1 Connect (org.libvirt.Connect)1 DomainBlockStats (org.libvirt.DomainBlockStats)1 DomainInfo (org.libvirt.DomainInfo)1 DomainInterfaceStats (org.libvirt.DomainInterfaceStats)1 NodeInfo (org.libvirt.NodeInfo)1 org.libvirt.jna.virDomainMemoryStats (org.libvirt.jna.virDomainMemoryStats)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1