Skip to contentSkip to conversation

scripts: qmp: Introduce vcpu pinning helper script

RFC Superseded Maxime Coquelin 2 participants 4 messages Sep 20, 2016 – Sep 29, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC 1/1 scripts: qmp: Introduce vcpu pinning helper script

This python script calls 'query-cpus' QMP command to retrieve vCPUs thread IDs.
Thread IDs are then used by taskset to pin vCPUs to physical CPUs passed in command line.

In case more vCPUs are present than the number of CPUs assigned in command line, multiple vCPUs get pinned to physical CPUs.

---

This script is not for production, where libvirt should be used. However, I find it useful during development, so I propose to share it as it might interest other people.

Note: it seems that tcp socket support is broken with QMP class. The bug has already been reported and patch proposed[0]. So it has only been tested with UNIX socket.

[0]: <b9d0e4270773564788dc15a2db8e407119648956.1471108930.git.carl@resonantcircuitlabs.com>

 scripts/qmp/qmp-vcpu-pin | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100755 scripts/qmp/qmp-vcpu-pin
scripts/qmp/qmp-vcpu-pin new file +39 -0
new file mode 100755
index 0000000..7bf96f7
@@ -0,0 +1,39 @@
+#!/usr/bin/python
+# QEMU vCPU pinning tool
+#
+# Copyright (C) 2016 Red Hat Inc.
+#
+# Authors:
+#  Maxime Coquelin <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL, version 2.  See
+# the COPYING file in the top-level directory
+import argparse
+import json
+import os
+
+from subprocess import call
+from qmp import QEMUMonitorProtocol
+
+parser = argparse.ArgumentParser(description='Pin QEMU vCPUs to physical CPUs')
+parser.add_argument('-s', '--server', type=str, required=True,
+                    help='QMP server path or address:port')
+parser.add_argument('cpu', type=int, nargs='+',
+                    help='Physical CPUs IDs')
+args = parser.parse_args()
+
+devnull = open(os.devnull, 'w')
+
+srv = QEMUMonitorProtocol(args.server)
+srv.connect()
+
+for vcpu in srv.command('query-cpus'):
+    vcpuid = vcpu['CPU']
+    tid = vcpu['thread_id']
+    cpuid = args.cpu[vcpuid % len(args.cpu)]
+    print 'Pin vCPU {} (tid {}) to physical CPU {}'.format(vcpuid, tid, cpuid)
+    try:
+        call(['taskset', '-pc', str(cpuid), str(tid)], stdout=devnull)
+    except OSError:
+        print 'Failed to pin vCPU{} to CPU{}'.format(vcpuid, cpuid)
+
Signature · 1 line
2.7.4
Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 72 lines · Maxime Coquelin

This python script calls 'query-cpus' QMP command to retrieve vCPUs thread IDs.
Thread IDs are then used by taskset to pin vCPUs to physical CPUs passed in command line.

In case more vCPUs are present than the number of CPUs assigned in command line, multiple vCPUs get pinned to physical CPUs.

---

This script is not for production, where libvirt should be used. However, I find it useful during development, so I propose to share it as it might interest other people.

Note: it seems that tcp socket support is broken with QMP class. The bug has already been reported and patch proposed[0]. So it has only been tested with UNIX socket.

[0]: <b9d0e4270773564788dc15a2db8e407119648956.1471108930.git.carl@resonantcircuitlabs.com>

 scripts/qmp/qmp-vcpu-pin | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100755 scripts/qmp/qmp-vcpu-pin
scripts/qmp/qmp-vcpu-pin new file +39 -0
new file mode 100755
index 0000000..7bf96f7
@@ -0,0 +1,39 @@
+#!/usr/bin/python
+# QEMU vCPU pinning tool
+#
+# Copyright (C) 2016 Red Hat Inc.
+#
+# Authors:
+#  Maxime Coquelin <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL, version 2.  See
+# the COPYING file in the top-level directory
+import argparse
+import json
+import os
+
+from subprocess import call
+from qmp import QEMUMonitorProtocol
+
+parser = argparse.ArgumentParser(description='Pin QEMU vCPUs to physical CPUs')
+parser.add_argument('-s', '--server', type=str, required=True,
+                    help='QMP server path or address:port')
+parser.add_argument('cpu', type=int, nargs='+',
+                    help='Physical CPUs IDs')
+args = parser.parse_args()
+
+devnull = open(os.devnull, 'w')
+
+srv = QEMUMonitorProtocol(args.server)
+srv.connect()
+
+for vcpu in srv.command('query-cpus'):
+    vcpuid = vcpu['CPU']
+    tid = vcpu['thread_id']
+    cpuid = args.cpu[vcpuid % len(args.cpu)]
+    print 'Pin vCPU {} (tid {}) to physical CPU {}'.format(vcpuid, tid, cpuid)
+    try:
+        call(['taskset', '-pc', str(cpuid), str(tid)], stdout=devnull)
+    except OSError:
+        print 'Failed to pin vCPU{} to CPU{}'.format(vcpuid, cpuid)
+
Signature · 1 line
2.7.4

Nit: for TCG, looks like we are using a single thread for all vCPUs. In that case, I'd suggest we make sure KVM is enabled before running this script, otherwise raise error (It won't make sense we pin the same TID for lots of times). I believe that's a prerequisite for this one.

Thanks,

-- peterx

#2 lore raw
Quoted 68 lines · Peter Xu
Quoted 67 lines · Maxime Coquelin

This python script calls 'query-cpus' QMP command to retrieve vCPUs thread IDs.
Thread IDs are then used by taskset to pin vCPUs to physical CPUs passed in command line.

In case more vCPUs are present than the number of CPUs assigned in command line, multiple vCPUs get pinned to physical CPUs.

---

This script is not for production, where libvirt should be used. However, I find it useful during development, so I propose to share it as it might interest other people.

Note: it seems that tcp socket support is broken with QMP class. The bug has already been reported and patch proposed[0]. So it has only been tested with UNIX socket.

[0]: <b9d0e4270773564788dc15a2db8e407119648956.1471108930.git.carl@resonantcircuitlabs.com>

 scripts/qmp/qmp-vcpu-pin | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100755 scripts/qmp/qmp-vcpu-pin
scripts/qmp/qmp-vcpu-pin new file +36 -0
new file mode 100755
index 0000000..7bf96f7
@@ -0,0 +1,39 @@
+#!/usr/bin/python
+# QEMU vCPU pinning tool
+#
+# Copyright (C) 2016 Red Hat Inc.
+#
+# Authors:
+#  Maxime Coquelin <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL, version 2.  See
+# the COPYING file in the top-level directory
+import argparse
+import json
+import os
+
+from subprocess import call
+from qmp import QEMUMonitorProtocol
+
+parser = argparse.ArgumentParser(description='Pin QEMU vCPUs to physical CPUs')
+parser.add_argument('-s', '--server', type=str, required=True,
+                    help='QMP server path or address:port')
+parser.add_argument('cpu', type=int, nargs='+',
+                    help='Physical CPUs IDs')
+args = parser.parse_args()
+
+devnull = open(os.devnull, 'w')
+
+srv = QEMUMonitorProtocol(args.server)
+srv.connect()
+
+for vcpu in srv.command('query-cpus'):
+    vcpuid = vcpu['CPU']
+    tid = vcpu['thread_id']
+    cpuid = args.cpu[vcpuid % len(args.cpu)]
+    print 'Pin vCPU {} (tid {}) to physical CPU {}'.format(vcpuid, tid, cpuid)
+    try:
+        call(['taskset', '-pc', str(cpuid), str(tid)], stdout=devnull)
Quoted 8 lines
Quoted 5 lines
scripts/qmp/qmp-vcpu-pin new file +3 -1
+    except OSError:
+        print 'Failed to pin vCPU{} to CPU{}'.format(vcpuid, cpuid)
+
--

2.7.4

Thanks Peter.

Quoted 5 lines

Nit: for TCG, looks like we are using a single thread for all vCPUs. In that case, I'd suggest we make sure KVM is enabled before running this script, otherwise raise error (It won't make sense we pin the same TID for lots of times). I believe that's a prerequisite for this one.

In the script, we could check that vCPUs TIDs are different. Checking for TCG or KVM is not a good solution, as IIUC, multi-threaded TCG support is in the pipe?

Regards,
Maxime

Quoted 4 lines

Thanks,

-- peterx

#3 lore raw

On Thu, Sep 29, 2016 at 08:50:51AM +0200, Maxime Coquelin wrote:

[...]

Quoted 9 lines
Quoted 5 lines

Nit: for TCG, looks like we are using a single thread for all vCPUs. In that case, I'd suggest we make sure KVM is enabled before running this script, otherwise raise error (It won't make sense we pin the same TID for lots of times). I believe that's a prerequisite for this one.

In the script, we could check that vCPUs TIDs are different. Checking for TCG or KVM is not a good solution, as IIUC, multi-threaded TCG support is in the pipe?

Sounds good. :)

-- peterx