[virt-tools-list] [PATCH] virtinst: add vcpupin support

Menno Lageman menno.lageman at oracle.com
Tue Feb 6 11:56:41 UTC 2018


From: Wim ten Have <wim.ten.have at oracle.com>

Add vcpupin support to virt-install so that it can create guest
domains with statically allocated vcpu pinning towards a given cpuset.

Syntax: to pin vcpu=0 to cpuset="1,3" and vcpu=1 to cpuset=2

  --cputune vcpupin0.vcpu=0,vcpupin0.cpuset=1,3,vcpupin1.vcpu=1,vcpupin1.cpuset=2

generates below XML description for the guest domain.

  <cputune>
    <vcpupin vcpu="0" cpuset="1,3"/>
    <vcpupin vcpu="1" cpuset="2"/>
  </cputune>

Signed-off-by: Wim ten Have <wim.ten.have at oracle.com>
Signed-off-by: Menno Lageman <menno.lageman at oracle.com>
---
 man/virt-install.pod                               | 10 ++++++
 .../compare/virt-install-singleton-config-2.xml    |  6 ++++
 tests/clitest.py                                   |  1 +
 virtinst/__init__.py                               |  1 +
 virtinst/cli.py                                    | 28 +++++++++++++++
 virtinst/cputune.py                                | 40 ++++++++++++++++++++++
 virtinst/guest.py                                  |  2 ++
 7 files changed, 88 insertions(+)
 create mode 100644 virtinst/cputune.py

diff --git a/man/virt-install.pod b/man/virt-install.pod
index 349e4e6c..e75f517f 100644
--- a/man/virt-install.pod
+++ b/man/virt-install.pod
@@ -255,6 +255,16 @@ Example of passing through the host cpu's cache information.
 
 Use --cpu=? to see a list of all available sub options. Complete details at L<http://libvirt.org/formatdomain.html#elementsCPU>
 
+=item B<--cputune> OPTIONS
+
+Tune CPU parameters for the guest.
+
+Configure which of the host's physical CPUs the domain VCPU will be pinned to. Example invocation
+
+    --cputune vpcupin0.vcpu=0,vpcupin0.cpuset=0-3,vpcupin1.vcpu=1,vpcupin1.cpuset=4-7
+
+Use --cputune=? to see a list of all available sub options. Complete details at L<http://libvirt.org/formatdomain.html#elementsCPUTuning>
+
 =item B<--security> type=TYPE[,label=LABEL][,relabel=yes|no]
 
 Configure domain security driver settings. Type can be either 'static' or
diff --git a/tests/cli-test-xml/compare/virt-install-singleton-config-2.xml b/tests/cli-test-xml/compare/virt-install-singleton-config-2.xml
index 3b9210f5..b25d8c60 100644
--- a/tests/cli-test-xml/compare/virt-install-singleton-config-2.xml
+++ b/tests/cli-test-xml/compare/virt-install-singleton-config-2.xml
@@ -159,6 +159,9 @@
     <label>system_u:object_r:svirt_image_t:s0:c100,c200</label>
   </seclabel>
   <on_lockfailure>ignore</on_lockfailure>
+  <cputune>
+    <vcpupin vcpu="0" cpuset="0-3"/>
+  </cputune>
 </domain>
 <domain type="kvm">
   <name>foobar</name>
@@ -324,4 +327,7 @@
     <label>system_u:object_r:svirt_image_t:s0:c100,c200</label>
   </seclabel>
   <on_lockfailure>ignore</on_lockfailure>
+  <cputune>
+    <vcpupin vcpu="0" cpuset="0-3"/>
+  </cputune>
 </domain>
diff --git a/tests/clitest.py b/tests/clitest.py
index a7d38b6d..18513597 100644
--- a/tests/clitest.py
+++ b/tests/clitest.py
@@ -430,6 +430,7 @@ cell0.distances.sibling1.id=1,cell0.distances.sibling1.value=21,\
 cell1.distances.sibling0.id=0,cell1.distances.sibling0.value=21,\
 cell1.distances.sibling1.id=1,cell1.distances.sibling1.value=10,\
 cache.mode=emulate,cache.level=3 \
+--cputune vcpupin0.vcpu=0,vcpupin0.cpuset=0-3 \
 --metadata title=my-title,description=my-description,uuid=00000000-1111-2222-3333-444444444444 \
 --boot cdrom,fd,hd,network,menu=off,loader=/foo/bar \
 --idmap uid_start=0,uid_target=1000,uid_count=10,gid_start=0,gid_target=1000,gid_count=10 \
diff --git a/virtinst/__init__.py b/virtinst/__init__.py
index 175303a5..eb3ec9b3 100644
--- a/virtinst/__init__.py
+++ b/virtinst/__init__.py
@@ -50,6 +50,7 @@ from virtinst.domainmemorybacking import DomainMemorybacking
 from virtinst.domainresource import DomainResource
 from virtinst.clock import Clock
 from virtinst.cpu import CPU, CPUFeature
+from virtinst.cputune import CPUTune
 from virtinst.seclabel import Seclabel
 from virtinst.pm import PM
 from virtinst.idmap import IdMap
diff --git a/virtinst/cli.py b/virtinst/cli.py
index 50b366e6..3d3ac0ac 100644
--- a/virtinst/cli.py
+++ b/virtinst/cli.py
@@ -39,6 +39,7 @@ from virtcli import CLIConfig
 from . import util
 from .clock import Clock
 from .cpu import CPU
+from .cputune import CPUTune
 from .deviceaudio import VirtualAudio
 from .devicechar import (VirtualChannelDevice, VirtualConsoleDevice,
                          VirtualSerialDevice, VirtualParallelDevice)
@@ -717,6 +718,8 @@ def add_device_options(devg, sound_back_compat=False):
 def add_guest_xml_options(geng):
     geng.add_argument("--security", action="append",
         help=_("Set domain security driver configuration."))
+    geng.add_argument("--cputune",
+        help=_("Tune CPU parameters for the domain process."))
     geng.add_argument("--numatune",
         help=_("Tune NUMA policy for the domain process."))
     geng.add_argument("--memtune", action="append",
@@ -1562,6 +1565,31 @@ ParserCPU.add_arg("mode", "cache.mode", find_inst_cb=ParserCPU.set_l3_cache_cb)
 ParserCPU.add_arg("level", "cache.level", find_inst_cb=ParserCPU.set_l3_cache_cb)
 
 
+#####################
+# --cputune parsing #
+#####################
+
+class ParserCPUTune(VirtCLIParser):
+    cli_arg_name = "cputune"
+    objclass = CPUTune
+    remove_first = "model"
+    stub_none = False
+
+    def vcpu_find_inst_cb(self, *args, **kwargs):
+        cliarg = "vcpupin"  # vcpupin[0-9]*
+        objpropname = "vcpus"
+        objaddfn = "add_vcpu"
+        cb = self._make_find_inst_cb(cliarg, objpropname, objaddfn)
+        return cb(*args, **kwargs)
+
+_register_virt_parser(ParserCPUTune)
+# Options for CPU.vcpus config
+ParserCPUTune.add_arg("vcpu", "vcpupin[0-9]*.vcpu",
+                  find_inst_cb=ParserCPUTune.vcpu_find_inst_cb)
+ParserCPUTune.add_arg("cpuset", "vcpupin[0-9]*.cpuset", can_comma=True,
+                  find_inst_cb=ParserCPUTune.vcpu_find_inst_cb)
+
+
 ###################
 # --vcpus parsing #
 ###################
diff --git a/virtinst/cputune.py b/virtinst/cputune.py
new file mode 100644
index 00000000..df0c3889
--- /dev/null
+++ b/virtinst/cputune.py
@@ -0,0 +1,40 @@
+#
+# Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; If not, see <http://www.gnu.org/licenses/>.
+
+from .xmlbuilder import XMLBuilder, XMLProperty, XMLChildProperty
+
+
+class _VCPUPin(XMLBuilder):
+    """
+    Class for generating <cputune> child <vcpupin> XML
+    """
+    _XML_ROOT_NAME = "vcpupin"
+    _XML_PROP_ORDER = ["vcpu", "cpuset"]
+
+    vcpu = XMLProperty("./@vcpu", is_int=True)
+    cpuset = XMLProperty("./@cpuset")
+
+
+class CPUTune(XMLBuilder):
+    """
+    Class for generating <cpu> XML
+    """
+    _XML_ROOT_NAME = "cputune"
+    vcpus = XMLChildProperty(_VCPUPin)
+    def add_vcpu(self):
+        obj = _VCPUPin(self.conn)
+        self.add_child(obj)
+        return obj
diff --git a/virtinst/guest.py b/virtinst/guest.py
index 903ee188..32acd521 100644
--- a/virtinst/guest.py
+++ b/virtinst/guest.py
@@ -31,6 +31,7 @@ from . import support
 from .osdict import OSDB
 from .clock import Clock
 from .cpu import CPU
+from .cputune import CPUTune
 from .device import VirtualDevice
 from .deviceaudio import VirtualAudio
 from .devicechar import VirtualChannelDevice, VirtualConsoleDevice
@@ -210,6 +211,7 @@ class Guest(XMLBuilder):
     features = XMLChildProperty(DomainFeatures, is_single=True)
     clock = XMLChildProperty(Clock, is_single=True)
     cpu = XMLChildProperty(CPU, is_single=True)
+    cputune = XMLChildProperty(CPUTune, is_single=True)
     numatune = XMLChildProperty(DomainNumatune, is_single=True)
     pm = XMLChildProperty(PM, is_single=True)
     blkiotune = XMLChildProperty(DomainBlkiotune, is_single=True)
-- 
2.14.3




More information about the virt-tools-list mailing list