CSIT-337 Improve Qemu affinity setting 79/2179/10
authorpmikus <pmikus@cisco.com>
Tue, 2 Aug 2016 10:14:12 +0000 (11:14 +0100)
committerPeter Mikus <pmikus@cisco.com>
Thu, 4 Aug 2016 06:01:22 +0000 (06:01 +0000)
Improve Qemu affinity setting by getting the list of Qemu thread IDs via
QMP and pin each thread PID to specific host core by taskset.

Change-Id: I1b0ee8d8425cf1f97b16d6761fff0be2fadc44a8
Signed-off-by: pmikus <pmikus@cisco.com>
resources/libraries/python/QemuUtils.py

index 3751cd6..16ade29 100644 (file)
@@ -53,8 +53,6 @@ class QemuUtils(object):
         self._qemu_opt['huge_allocate'] = False
         # Default image for CSIT virl setup
         self._qemu_opt['disk_image'] = '/var/lib/vm/vhost-nested.img'
-        # Affinity of qemu processes
-        self._qemu_opt['affinity'] = False
         # VM node info dict
         self._vm_info = {
             'type': NodeType.VM,
@@ -128,13 +126,28 @@ class QemuUtils(object):
         """
         self._qemu_opt['disk_image'] = disk_image
 
-    def qemu_set_affinity(self, mask):
-        """Set qemu affinity by taskset with cpu mask.
+    def qemu_set_affinity(self, *host_cpus):
+        """Set qemu affinity by getting thread PIDs via QMP and taskset to list
+        of CPU cores.
 
-       :param mask: Hex CPU mask.
-       :type mask: str
+        :param host_cpus: List of CPU cores.
+        :type host_cpus: list
         """
-        self._qemu_opt['affinity'] = mask
+        qemu_cpus = self._qemu_qmp_exec('query-cpus')['return']
+
+        if len(qemu_cpus) != len(host_cpus):
+            logger.debug('Host CPU count {0}, Qemu Thread count {1}'.format(
+                len(host_cpus), len(qemu_cpus)))
+            raise ValueError('Host CPU count must match Qemu Thread count')
+
+        for qemu_cpu, host_cpu in zip(qemu_cpus, host_cpus):
+            cmd = 'taskset -p {0} {1}'.format(hex(1 << int(host_cpu)),
+                                              qemu_cpu['thread_id'])
+            (ret_code, _, stderr) = self._ssh.exec_command_sudo(cmd)
+            if int(ret_code) != 0:
+                logger.debug('Set affinity failed {0}'.format(stderr))
+                raise RuntimeError('Set affinity failed on {0}'.format(
+                    self._node['host']))
 
     def qemu_set_node(self, node):
         """Set node to run QEMU on.
@@ -409,12 +422,9 @@ class QemuUtils(object):
             '-device isa-serial,chardev=qga0'
         # Graphic setup
         graphic = '-monitor none -display none -vga none'
-        qbin = 'taskset {0} {1}'.format(self._qemu_opt.get('affinity'),
-            self.__QEMU_BIN) if self._qemu_opt.get(
-            'affinity') else self.__QEMU_BIN
         # Run QEMU
         cmd = '{0} {1} {2} {3} {4} -hda {5} {6} {7} {8} {9}'.format(
-            qbin, self._qemu_opt.get('smp'), mem, ssh_fwd,
+            self.__QEMU_BIN, self._qemu_opt.get('smp'), mem, ssh_fwd,
             self._qemu_opt.get('options'),
             self._qemu_opt.get('disk_image'), qmp, serial, qga, graphic)
         (ret_code, _, stderr) = self._ssh.exec_command_sudo(cmd, timeout=300)

©2016 FD.io a Linux Foundation Collaborative Project. All Rights Reserved.
Linux Foundation is a registered trademark of The Linux Foundation. Linux is a registered trademark of Linus Torvalds.
Please see our privacy policy and terms of use.