All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2] scripts: qmp: Introduce vcpu pinning helper script
@ 2016-10-04 11:22 Maxime Coquelin
  2016-11-03 15:01 ` Maxime Coquelin
  0 siblings, 1 reply; 3+ messages in thread
From: Maxime Coquelin @ 2016-10-04 11:22 UTC (permalink / raw)
  To: armbru, qemu-devel, peterx; +Cc: lcapitulino, 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.

If multiple vCPUs share a same thread ID (e.g. with TCG), the
thread ID is pinned a single time.

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
Changes since RFC:
==================
 - If vCPUs share a same TID, don't pin them multiple times (peterx)

 scripts/qmp/qmp-vcpu-pin | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
 create mode 100755 scripts/qmp/qmp-vcpu-pin

diff --git a/scripts/qmp/qmp-vcpu-pin b/scripts/qmp/qmp-vcpu-pin
new file mode 100755
index 0000000..9eefe6e
--- /dev/null
+++ b/scripts/qmp/qmp-vcpu-pin
@@ -0,0 +1,45 @@
+#!/usr/bin/python
+# QEMU vCPU pinning tool
+#
+# Copyright (C) 2016 Red Hat Inc.
+#
+# Authors:
+#  Maxime Coquelin <maxime.coquelin@redhat.com>
+#
+# 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
+
+pinned = []
+
+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']
+    if tid in pinned:
+        print 'vCPU{}\'s tid {} already pinned, skipping'.format(vcpuid, tid)
+        continue
+
+    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)
+        pinned.append(tid)
+    except OSError:
+        print 'Failed to pin vCPU{} to CPU{}'.format(vcpuid, cpuid)
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-11-07 16:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-10-04 11:22 [Qemu-devel] [PATCH v2] scripts: qmp: Introduce vcpu pinning helper script Maxime Coquelin
2016-11-03 15:01 ` Maxime Coquelin
2016-11-07 16:21   ` Peter Xu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.