From 0d172ab55c6eb9a235870d8f98d619078bb91078 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Tue, 16 Nov 2021 08:43:18 +0100 Subject: Add CPU topology functionality for Libvirt domain XML configs --- .../java/org/openslx/libvirt/domain/Domain.java | 84 +++++++++++ .../org/openslx/libvirt/domain/DomainTest.java | 68 +++++++++ ...mu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml | 166 +++++++++++++++++++++ 3 files changed, 318 insertions(+) create mode 100644 src/test/resources/libvirt/xml/qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml diff --git a/src/main/java/org/openslx/libvirt/domain/Domain.java b/src/main/java/org/openslx/libvirt/domain/Domain.java index e6049fa..ca10261 100644 --- a/src/main/java/org/openslx/libvirt/domain/Domain.java +++ b/src/main/java/org/openslx/libvirt/domain/Domain.java @@ -767,6 +767,90 @@ public class Domain extends LibvirtXmlDocument this.getRootXmlNode().setXmlElementAttributeValue( "cpu", "check", check.toString() ); } + /** + * Returns the number of virtual machine CPU dies defined in the Libvirt domain XML document. + * + * @return number of virtual machine CPU dies. + */ + public int getCpuDies() + { + final String numDies = this.getRootXmlNode().getXmlElementAttributeValue( "cpu/topology", "dies" ); + return Integer.valueOf( numDies ); + } + + /** + * Set number of virtual machine CPU dies in the Libvirt domain XML document. + * + * @param number virtual machine CPU dies. + */ + public void setCpuDies( int number ) + { + this.getRootXmlNode().setXmlElementAttributeValue( "cpu/topology", "dies", Integer.toString( number ) ); + } + + /** + * Returns the number of virtual machine CPU sockets defined in the Libvirt domain XML document. + * + * @return number of virtual machine CPU sockets. + */ + public int getCpuSockets() + { + final String numSockets = this.getRootXmlNode().getXmlElementAttributeValue( "cpu/topology", "sockets" ); + return Integer.valueOf( numSockets ); + } + + /** + * Set number of virtual machine CPU dies in the Libvirt domain XML document. + * + * @param number virtual machine CPU dies. + */ + public void setCpuSockets( int number ) + { + this.getRootXmlNode().setXmlElementAttributeValue( "cpu/topology", "sockets", Integer.toString( number ) ); + } + + /** + * Returns the number of virtual machine CPU cores defined in the Libvirt domain XML document. + * + * @return number of virtual machine CPU cores. + */ + public int getCpuCores() + { + final String numCores = this.getRootXmlNode().getXmlElementAttributeValue( "cpu/topology", "cores" ); + return Integer.valueOf( numCores ); + } + + /** + * Set number of virtual machine CPU cores in the Libvirt domain XML document. + * + * @param number virtual machine CPU cores. + */ + public void setCpuCores( int number ) + { + this.getRootXmlNode().setXmlElementAttributeValue( "cpu/topology", "cores", Integer.toString( number ) ); + } + + /** + * Returns the number of virtual machine CPU threads defined in the Libvirt domain XML document. + * + * @return number of virtual machine CPU threads. + */ + public int getCpuThreads() + { + final String numThreads = this.getRootXmlNode().getXmlElementAttributeValue( "cpu/topology", "threads" ); + return Integer.valueOf( numThreads ); + } + + /** + * Set number of virtual machine CPU threads in the Libvirt domain XML document. + * + * @param number virtual machine CPU threads. + */ + public void setCpuThreads( int number ) + { + this.getRootXmlNode().setXmlElementAttributeValue( "cpu/topology", "threads", Integer.toString( number ) ); + } + /** * Returns the file name of the emulator binary defined in the Libvirt domain XML document. * diff --git a/src/test/java/org/openslx/libvirt/domain/DomainTest.java b/src/test/java/org/openslx/libvirt/domain/DomainTest.java index 6b53125..00bf83b 100644 --- a/src/test/java/org/openslx/libvirt/domain/DomainTest.java +++ b/src/test/java/org/openslx/libvirt/domain/DomainTest.java @@ -322,6 +322,74 @@ public class DomainTest assertEquals( CpuCheck.NONE.toString(), vm.getCpuCheck().toString() ); } + @Test + @DisplayName( "Get VM CPU dies from libvirt XML file" ) + public void testGetCpuDies() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml" ); + assertEquals( 2, vm.getCpuDies() ); + } + + @Test + @DisplayName( "Set VM CPU dies in libvirt XML file" ) + public void testSetCpuDies() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml" ); + vm.setCpuDies( 3 ); + assertEquals( 3, vm.getCpuDies() ); + } + + @Test + @DisplayName( "Get VM CPU sockets from libvirt XML file" ) + public void testGetCpuSockets() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml" ); + assertEquals( 3, vm.getCpuSockets() ); + } + + @Test + @DisplayName( "Set VM CPU sockets in libvirt XML file" ) + public void testSetCpuSockets() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml" ); + vm.setCpuSockets( 2 ); + assertEquals( 2, vm.getCpuSockets() ); + } + + @Test + @DisplayName( "Get VM CPU cores from libvirt XML file" ) + public void testGetCpuCores() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml" ); + assertEquals( 4, vm.getCpuCores() ); + } + + @Test + @DisplayName( "Set VM CPU cores in libvirt XML file" ) + public void testSetCpuCores() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml" ); + vm.setCpuCores( 8 ); + assertEquals( 8, vm.getCpuCores() ); + } + + @Test + @DisplayName( "Get VM CPU threads from libvirt XML file" ) + public void testGetCpuThreads() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml" ); + assertEquals( 1, vm.getCpuThreads() ); + } + + @Test + @DisplayName( "Set VM CPU threads in libvirt XML file" ) + public void testSetCpuThreads() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml" ); + vm.setCpuThreads( 2 ); + assertEquals( 2, vm.getCpuThreads() ); + } + @Test @DisplayName( "Get VM emulator binary from libvirt XML file" ) public void testGetDevicesEmulator() diff --git a/src/test/resources/libvirt/xml/qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml b/src/test/resources/libvirt/xml/qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml new file mode 100644 index 0000000..a3f7c24 --- /dev/null +++ b/src/test/resources/libvirt/xml/qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml @@ -0,0 +1,166 @@ + + ubuntu-20-04 + 8dc5433c-0228-49e4-b019-fa2b606aa544 + Ubuntu 20.04 + Ubuntu 20.04 desktop installation + + + + + + 4194304 + 4194304 + 2 + + hvm + + + + + + + + + + + + + + + + destroy + restart + destroy + + + + + + /usr/bin/qemu-system-x86_64 + + + + +
+ + + + + +
+ + + + +
+ + +
+ + + +
+ + + +
+ + + +
+ + +
+ + + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + +
+ + +
+ + + + + + +
+ + + + + + + + + + + +
+ + + +
+ + +
+ + + + + + + + +
+ +