From patchwork Tue Oct 4 11:22:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxime Coquelin X-Patchwork-Id: 9361617 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1585A600C8 for ; Tue, 4 Oct 2016 11:22:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0700F28811 for ; Tue, 4 Oct 2016 11:22:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EF41528989; Tue, 4 Oct 2016 11:22:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 81E8328811 for ; Tue, 4 Oct 2016 11:22:56 +0000 (UTC) Received: from localhost ([::1]:41575 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1brNoF-0006Ed-81 for patchwork-qemu-devel@patchwork.kernel.org; Tue, 04 Oct 2016 07:22:55 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41893) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1brNo0-0006EY-3r for qemu-devel@nongnu.org; Tue, 04 Oct 2016 07:22:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1brNnw-00035G-9H for qemu-devel@nongnu.org; Tue, 04 Oct 2016 07:22:39 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45652) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1brNnw-000351-1r for qemu-devel@nongnu.org; Tue, 04 Oct 2016 07:22:36 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1AEAA1E2A for ; Tue, 4 Oct 2016 11:22:35 +0000 (UTC) Received: from max-t460s.redhat.com (vpn1-5-203.ams2.redhat.com [10.36.5.203]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u94BMXNE000967; Tue, 4 Oct 2016 07:22:33 -0400 From: Maxime Coquelin To: armbru@redhat.com, qemu-devel@nongnu.org, peterx@redhat.com Date: Tue, 4 Oct 2016 13:22:31 +0200 Message-Id: <1475580151-3856-1-git-send-email-maxime.coquelin@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 04 Oct 2016 11:22:35 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2] scripts: qmp: Introduce vcpu pinning helper script X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Maxime Coquelin , lcapitulino@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 Reviewed-by: Peter Xu --- 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 +# +# 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)