From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stefan Raspl Subject: [PATCH v1 19/19] tools/kvm_stat: display guest list in pid/guest selection screens Date: Wed, 7 Jun 2017 21:08:43 +0200 Message-ID: <20170607190843.76869-20-raspl@linux.vnet.ibm.com> References: <20170607190843.76869-1-raspl@linux.vnet.ibm.com> Cc: pbonzini@redhat.com, rkrcmar@redhat.com, frankja@linux.vnet.ibm.com To: kvm@vger.kernel.org Return-path: Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:60973 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752020AbdFGTJl (ORCPT ); Wed, 7 Jun 2017 15:09:41 -0400 Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v57J94ZH057582 for ; Wed, 7 Jun 2017 15:09:41 -0400 Received: from e06smtp13.uk.ibm.com (e06smtp13.uk.ibm.com [195.75.94.109]) by mx0a-001b2d01.pphosted.com with ESMTP id 2axpcj2r35-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 07 Jun 2017 15:09:41 -0400 Received: from localhost by e06smtp13.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 7 Jun 2017 20:09:38 +0100 In-Reply-To: <20170607190843.76869-1-raspl@linux.vnet.ibm.com> Sender: kvm-owner@vger.kernel.org List-ID: Display a (possibly inaccurate) list of all running guests. Note that we leave a bit of extra room above the list for potential error messages. Furthermore, we deliberately do not reject pids or guest names that are not in our list, as we cannot rule out that our fuzzy approach might be in error somehow. Signed-off-by: Stefan Raspl --- tools/kvm/kvm_stat/kvm_stat | 49 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/tools/kvm/kvm_stat/kvm_stat b/tools/kvm/kvm_stat/kvm_stat index f1cec0103757..8237d9bb8547 100755 --- a/tools/kvm/kvm_stat/kvm_stat +++ b/tools/kvm/kvm_stat/kvm_stat @@ -954,15 +954,9 @@ class Tui(object): curses.nocbreak() curses.endwin() - @staticmethod - def get_pid_from_gname(gname): - """Fuzzy function to convert guest name to QEMU process pid. - - Returns a list of potential pids, can be empty if no match found. - Throws an exception on processing errors. - - """ - pids = [] + def get_all_gnames(self): + """Returns a list of (pid, gname) tuples of all running guests""" + res = [] try: child = subprocess.Popen(['ps', '-A', '--format', 'pid,args'], stdout=subprocess.PIPE) @@ -972,11 +966,40 @@ class Tui(object): line = line.lstrip().split(' ', 1) # perform a sanity check before calling the more expensive # function to possibly extract the guest name - if (' -name ' in line[1] and - gname == self.get_gname_from_pid(line[0])): - pids.append(int(line[0])) + if ' -name ' in line[1]: + res.append((line[0], self.get_gname_from_pid(line[0]))) child.stdout.close() + return res + + def print_all_gnames(self, row): + """Print a list of all running guests along with their pids.""" + self.screen.addstr(row, 2, '%8s %-60s' % + ('Pid', 'Guest Name (fuzzy list, might be ' + 'inaccurate!)'), + curses.A_UNDERLINE) + row += 1 + try: + for line in self.get_all_gnames(): + self.screen.addstr(row, 2, '%8s %-60s' % (line[0], line[1])) + row += 1 + if row >= self.screen.getmaxyx()[0]: + break + except Exception: + self.screen.addstr(row + 1, 2, 'Not available') + + def get_pid_from_gname(self, gname): + """Fuzzy function to convert guest name to QEMU process pid. + + Returns a list of potential pids, can be empty if no match found. + Throws an exception on processing errors. + + """ + pids = [] + for line in self.get_all_gnames(): + if gname == line[1]: + pids.append(int(line[0])) + return pids @staticmethod @@ -1167,6 +1190,7 @@ class Tui(object): 'This might limit the shown data to the trace ' 'statistics.') self.screen.addstr(5, 0, msg) + self.print_all_gnames(7) curses.echo() self.screen.addstr(3, 0, "Pid [0 or pid]: ") @@ -1236,6 +1260,7 @@ class Tui(object): 'This might limit the shown data to the trace ' 'statistics.') self.screen.addstr(5, 0, msg) + self.print_all_gnames() curses.echo() self.screen.addstr(3, 0, "Guest [ENTER or guest]: ") gname = self.screen.getstr() -- 2.11.2