scripts: qmp: Introduce vcpu pinning helper script
PATCH v2 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.
If multiple vCPUs share a same thread ID (e.g. with TCG), the thread ID is pinned a single time.
---
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
scripts/qmp/qmp-vcpu-pin new file +45 -0
@@ -0,0 +1,45 @@ +#!/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 + +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)
Signature · 1 line
2.7.4
Hi Peter,
Quoted 12 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.
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 <[email protected]>
Quoted 8 lines
---
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
I added your proposed change not to pin same TID multiple time, does it look good to you?
Thanks,
Maxime
Peter Xu
Reviewed-byQuoted 20 lines · Maxime Coquelin
Hi Peter,
Quoted 17 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.
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 <[email protected]>---
Changes since RFC:
==================
- If vCPUs share a same TID, don't pin them multiple times (peterx)
Quoted 6 lines
Quoted 3 lines
scripts/qmp/qmp-vcpu-pin| 45 +++++++++++++++++++++++++++++++++++++++++++++1 file changed, 45 insertions(+)create mode 100755
scripts/qmp/qmp-vcpu-pinI added your proposed change not to pin same TID multiple time, does it look good to you?
Though I am still not familiar with the python and QMP stuffs, but yes this looks good to me, so:
Sorry for the late response.
-- peterx