Search in sources :

Example 1 with NodeInfo

use of org.libvirt.NodeInfo in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method getVmStat.

private VmStatsEntry getVmStat(Connect conn, String vmName) throws LibvirtException {
    Domain dm = null;
    try {
        dm = getDomain(conn, vmName);
        DomainInfo info = dm.getInfo();
        VmStatsEntry stats = new VmStatsEntry();
        stats.setNumCPUs(info.nrVirtCpu);
        stats.setEntityType("vm");
        /* get cpu utilization */
        vmStats oldStats = null;
        Calendar now = Calendar.getInstance();
        oldStats = _vmStats.get(vmName);
        long elapsedTime = 0;
        if (oldStats != null) {
            elapsedTime = now.getTimeInMillis() - oldStats._timestamp.getTimeInMillis();
            double utilization = (info.cpuTime - oldStats._usedTime) / ((double) elapsedTime * 1000000);
            NodeInfo node = conn.nodeInfo();
            utilization = utilization / node.cpus;
            stats.setCPUUtilization(utilization * 100);
        }
        /* get network stats */
        List<InterfaceDef> vifs = getInterfaces(conn, vmName);
        long rx = 0;
        long tx = 0;
        for (InterfaceDef vif : vifs) {
            DomainInterfaceStats ifStats = dm.interfaceStats(vif.getDevName());
            rx += ifStats.rx_bytes;
            tx += ifStats.tx_bytes;
        }
        if (oldStats != null) {
            long deltarx = rx - oldStats._rx;
            if (deltarx > 0)
                stats.setNetworkReadKBs(deltarx / 1000);
            long deltatx = tx - oldStats._tx;
            if (deltatx > 0)
                stats.setNetworkWriteKBs(deltatx / 1000);
        }
        vmStats newStat = new vmStats();
        newStat._usedTime = info.cpuTime;
        newStat._rx = rx;
        newStat._tx = tx;
        newStat._timestamp = now;
        _vmStats.put(vmName, newStat);
        return stats;
    } finally {
        if (dm != null) {
            dm.free();
        }
    }
}
Also used : InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) NodeInfo(org.libvirt.NodeInfo) DomainInterfaceStats(org.libvirt.DomainInterfaceStats) Calendar(java.util.Calendar) DomainInfo(org.libvirt.DomainInfo) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) Domain(org.libvirt.Domain)

Example 2 with NodeInfo

use of org.libvirt.NodeInfo in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method getHostInfo.

protected List<Object> getHostInfo() {
    final ArrayList<Object> info = new ArrayList<Object>();
    long speed = 0;
    long cpus = 0;
    long ram = 0;
    String cap = null;
    try {
        Connect conn = LibvirtConnection.getConnection();
        final NodeInfo hosts = conn.nodeInfo();
        boolean result = false;
        try {
            BufferedReader in = new BufferedReader(new FileReader("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"));
            speed = Long.parseLong(in.readLine()) / 1000;
            result = true;
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } catch (NumberFormatException e) {
        }
        if (!result) {
            speed = hosts.mhz;
        }
        cpus = hosts.cpus;
        ram = hosts.memory * 1024L;
        LibvirtCapXMLParser parser = new LibvirtCapXMLParser();
        parser.parseCapabilitiesXML(conn.getCapabilities());
        ArrayList<String> oss = parser.getGuestOsType();
        for (String s : oss) {
            /*
				 * Even host supports guest os type more than hvm, we only
				 * report hvm to management server
				 */
            if (s.equalsIgnoreCase("hvm")) {
                cap = "hvm";
            }
        }
    } catch (LibvirtException e) {
    }
    if (isSnapshotSupported()) {
        cap = cap + ",snapshot";
    }
    info.add((int) cpus);
    info.add(speed);
    info.add(ram);
    info.add(cap);
    // save a maximum
    long dom0ram = Math.min(ram / 10, 768 * 1024 * 1024L);
    // of 10% of
    // system ram or
    // 768M
    dom0ram = Math.max(dom0ram, _dom0MinMem);
    info.add(dom0ram);
    s_logger.debug("cpus=" + cpus + ", speed=" + speed + ", ram=" + ram + ", dom0ram=" + dom0ram);
    return info;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) NodeInfo(org.libvirt.NodeInfo) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 3 with NodeInfo

use of org.libvirt.NodeInfo 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 4 with NodeInfo

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

the class LibvirtComputingResource method getHostInfo.

protected List<Object> getHostInfo() {
    final ArrayList<Object> info = new ArrayList<Object>();
    long speed = 0;
    long cpus = 0;
    long ram = 0;
    int cpuSockets = 0;
    String cap = null;
    try {
        final Connect conn = LibvirtConnection.getConnection();
        final NodeInfo hosts = conn.nodeInfo();
        speed = getCpuSpeed(hosts);
        /*
            * Some CPUs report a single socket and multiple NUMA cells.
            * We need to multiply them to get the correct socket count.
            */
        cpuSockets = hosts.sockets;
        if (hosts.nodes > 0) {
            cpuSockets = hosts.sockets * hosts.nodes;
        }
        cpus = hosts.cpus;
        ram = hosts.memory * 1024L;
        final LibvirtCapXMLParser parser = new LibvirtCapXMLParser();
        parser.parseCapabilitiesXML(conn.getCapabilities());
        final ArrayList<String> oss = parser.getGuestOsType();
        for (final String s : oss) {
            /*
                 * Even host supports guest os type more than hvm, we only
                 * report hvm to management server
                 */
            if (s.equalsIgnoreCase("hvm")) {
                cap = "hvm";
            }
        }
    } catch (final LibvirtException e) {
        s_logger.trace("Ignoring libvirt error.", e);
    }
    if (isSnapshotSupported()) {
        cap = cap + ",snapshot";
    }
    info.add((int) cpus);
    info.add(speed);
    // Report system's RAM as actual RAM minus host OS reserved RAM
    ram = ram - _dom0MinMem;
    info.add(ram);
    info.add(cap);
    info.add(_dom0MinMem);
    info.add(cpuSockets);
    s_logger.debug("cpus=" + cpus + ", speed=" + speed + ", ram=" + ram + ", _dom0MinMem=" + _dom0MinMem + ", cpu sockets=" + cpuSockets);
    return info;
}
Also used : LibvirtException(org.libvirt.LibvirtException) NodeInfo(org.libvirt.NodeInfo) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList)

Example 5 with NodeInfo

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

the class LibvirtComputingResource method getVmStat.

public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws LibvirtException {
    Domain dm = null;
    try {
        dm = getDomain(conn, vmName);
        if (dm == null) {
            return null;
        }
        DomainInfo info = dm.getInfo();
        final VmStatsEntry stats = new VmStatsEntry();
        stats.setNumCPUs(info.nrVirtCpu);
        stats.setEntityType("vm");
        stats.setMemoryKBs(info.maxMem);
        stats.setTargetMemoryKBs(info.memory);
        stats.setIntFreeMemoryKBs(getMemoryFreeInKBs(dm));
        /* get cpu utilization */
        VmStats oldStats = null;
        final Calendar now = Calendar.getInstance();
        oldStats = _vmStats.get(vmName);
        long elapsedTime = 0;
        if (oldStats != null) {
            elapsedTime = now.getTimeInMillis() - oldStats._timestamp.getTimeInMillis();
            double utilization = (info.cpuTime - oldStats._usedTime) / ((double) elapsedTime * 1000000);
            final NodeInfo node = conn.nodeInfo();
            utilization = utilization / node.cpus;
            if (utilization > 0) {
                stats.setCPUUtilization(utilization * 100);
            }
        }
        /* get network stats */
        final List<InterfaceDef> vifs = getInterfaces(conn, vmName);
        long rx = 0;
        long tx = 0;
        for (final InterfaceDef vif : vifs) {
            final DomainInterfaceStats ifStats = dm.interfaceStats(vif.getDevName());
            rx += ifStats.rx_bytes;
            tx += ifStats.tx_bytes;
        }
        if (oldStats != null) {
            final double deltarx = rx - oldStats._rx;
            if (deltarx > 0) {
                stats.setNetworkReadKBs(deltarx / 1024);
            }
            final double deltatx = tx - oldStats._tx;
            if (deltatx > 0) {
                stats.setNetworkWriteKBs(deltatx / 1024);
            }
        }
        /* get disk stats */
        final List<DiskDef> disks = getDisks(conn, vmName);
        long io_rd = 0;
        long io_wr = 0;
        long bytes_rd = 0;
        long bytes_wr = 0;
        for (final DiskDef disk : disks) {
            final DomainBlockStats blockStats = dm.blockStats(disk.getDiskLabel());
            io_rd += blockStats.rd_req;
            io_wr += blockStats.wr_req;
            bytes_rd += blockStats.rd_bytes;
            bytes_wr += blockStats.wr_bytes;
        }
        if (oldStats != null) {
            final long deltaiord = io_rd - oldStats._ioRead;
            if (deltaiord > 0) {
                stats.setDiskReadIOs(deltaiord);
            }
            final long deltaiowr = io_wr - oldStats._ioWrote;
            if (deltaiowr > 0) {
                stats.setDiskWriteIOs(deltaiowr);
            }
            final double deltabytesrd = bytes_rd - oldStats._bytesRead;
            if (deltabytesrd > 0) {
                stats.setDiskReadKBs(deltabytesrd / 1024);
            }
            final double deltabyteswr = bytes_wr - oldStats._bytesWrote;
            if (deltabyteswr > 0) {
                stats.setDiskWriteKBs(deltabyteswr / 1024);
            }
        }
        /* save to Hashmap */
        final VmStats newStat = new VmStats();
        newStat._usedTime = info.cpuTime;
        newStat._rx = rx;
        newStat._tx = tx;
        newStat._ioRead = io_rd;
        newStat._ioWrote = io_wr;
        newStat._bytesRead = bytes_rd;
        newStat._bytesWrote = bytes_wr;
        newStat._timestamp = now;
        _vmStats.put(vmName, newStat);
        return stats;
    } finally {
        if (dm != null) {
            dm.free();
        }
    }
}
Also used : DomainInterfaceStats(org.libvirt.DomainInterfaceStats) Calendar(java.util.Calendar) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) NodeInfo(org.libvirt.NodeInfo) DomainBlockStats(org.libvirt.DomainBlockStats) DomainInfo(org.libvirt.DomainInfo) Domain(org.libvirt.Domain)

Aggregations

NodeInfo (org.libvirt.NodeInfo)6 VmStatsEntry (com.cloud.agent.api.VmStatsEntry)3 Connect (org.libvirt.Connect)3 Domain (org.libvirt.Domain)3 DomainInfo (org.libvirt.DomainInfo)3 DomainInterfaceStats (org.libvirt.DomainInterfaceStats)3 DiskDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef)2 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)2 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 Test (org.junit.Test)2 DomainBlockStats (org.libvirt.DomainBlockStats)2 LibvirtException (org.libvirt.LibvirtException)2 InterfaceDef (com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef)1 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 MemoryStatistic (org.libvirt.MemoryStatistic)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1