Skip to content

Commit 865279c

Browse files
Stefan Rasplbonzini
authored andcommitted
tools/kvm_stat: display guest list in pid/guest selection screens
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 <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 6667ae8 commit 865279c

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

tools/kvm/kvm_stat/kvm_stat

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -894,15 +894,9 @@ class Tui(object):
894894
curses.nocbreak()
895895
curses.endwin()
896896

897-
@staticmethod
898-
def get_pid_from_gname(gname):
899-
"""Fuzzy function to convert guest name to QEMU process pid.
900-
901-
Returns a list of potential pids, can be empty if no match found.
902-
Throws an exception on processing errors.
903-
904-
"""
905-
pids = []
897+
def get_all_gnames(self):
898+
"""Returns a list of (pid, gname) tuples of all running guests"""
899+
res = []
906900
try:
907901
child = subprocess.Popen(['ps', '-A', '--format', 'pid,args'],
908902
stdout=subprocess.PIPE)
@@ -912,11 +906,40 @@ class Tui(object):
912906
line = line.lstrip().split(' ', 1)
913907
# perform a sanity check before calling the more expensive
914908
# function to possibly extract the guest name
915-
if (' -name ' in line[1] and
916-
gname == self.get_gname_from_pid(line[0])):
917-
pids.append(int(line[0]))
909+
if ' -name ' in line[1]:
910+
res.append((line[0], self.get_gname_from_pid(line[0])))
918911
child.stdout.close()
919912

913+
return res
914+
915+
def print_all_gnames(self, row):
916+
"""Print a list of all running guests along with their pids."""
917+
self.screen.addstr(row, 2, '%8s %-60s' %
918+
('Pid', 'Guest Name (fuzzy list, might be '
919+
'inaccurate!)'),
920+
curses.A_UNDERLINE)
921+
row += 1
922+
try:
923+
for line in self.get_all_gnames():
924+
self.screen.addstr(row, 2, '%8s %-60s' % (line[0], line[1]))
925+
row += 1
926+
if row >= self.screen.getmaxyx()[0]:
927+
break
928+
except Exception:
929+
self.screen.addstr(row + 1, 2, 'Not available')
930+
931+
def get_pid_from_gname(self, gname):
932+
"""Fuzzy function to convert guest name to QEMU process pid.
933+
934+
Returns a list of potential pids, can be empty if no match found.
935+
Throws an exception on processing errors.
936+
937+
"""
938+
pids = []
939+
for line in self.get_all_gnames():
940+
if gname == line[1]:
941+
pids.append(int(line[0]))
942+
920943
return pids
921944

922945
@staticmethod
@@ -1102,6 +1125,7 @@ class Tui(object):
11021125
'This might limit the shown data to the trace '
11031126
'statistics.')
11041127
self.screen.addstr(5, 0, msg)
1128+
self.print_all_gnames(7)
11051129

11061130
curses.echo()
11071131
self.screen.addstr(3, 0, "Pid [0 or pid]: ")
@@ -1171,6 +1195,7 @@ class Tui(object):
11711195
'This might limit the shown data to the trace '
11721196
'statistics.')
11731197
self.screen.addstr(5, 0, msg)
1198+
self.print_all_gnames()
11741199
curses.echo()
11751200
self.screen.addstr(3, 0, "Guest [ENTER or guest]: ")
11761201
gname = self.screen.getstr()

0 commit comments

Comments
 (0)