Skip to content

Commit 516f119

Browse files
Stefan-Rasplbonzini
authored andcommitted
tools/kvm_stat: eliminate extra guest/pid selection dialog
We can do with a single dialog that takes both, pids and guest names. Note that we keep both interactive commands, 'p' and 'g' for now, to avoid confusion among users used to a specific key. While at it, we improve on some minor glitches regarding curses usage, e.g. cursor still visible when not supposed to be. Signed-off-by: Stefan Raspl <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent c0e8c21 commit 516f119

File tree

2 files changed

+39
-75
lines changed

2 files changed

+39
-75
lines changed

tools/kvm/kvm_stat/kvm_stat

+37-73
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,8 @@ class Tui(object):
10411041

10421042
def _update_pid(self, pid):
10431043
"""Propagates pid selection to stats object."""
1044+
self.screen.addstr(4, 1, 'Updating pid filter...')
1045+
self.screen.refresh()
10441046
self.stats.pid_filter = pid
10451047

10461048
def _refresh_header(self, pid=None):
@@ -1144,10 +1146,10 @@ class Tui(object):
11441146
' filters)',
11451147
' c clear filter',
11461148
' f filter by regular expression',
1147-
' g filter by guest name',
1149+
' g filter by guest name/PID',
11481150
' h display interactive commands reference',
11491151
' o toggle sorting order (Total vs CurAvg/s)',
1150-
' p filter by PID',
1152+
' p filter by guest name/PID',
11511153
' q quit',
11521154
' r reset stats',
11531155
' s set update interval',
@@ -1199,44 +1201,6 @@ class Tui(object):
11991201
msg = '"' + regex + '": Not a valid regular expression'
12001202
continue
12011203

1202-
def _show_vm_selection_by_pid(self):
1203-
"""Draws PID selection mask.
1204-
1205-
Asks for a pid until a valid pid or 0 has been entered.
1206-
1207-
"""
1208-
msg = ''
1209-
while True:
1210-
self.screen.erase()
1211-
self.screen.addstr(0, 0,
1212-
'Show statistics for specific pid.',
1213-
curses.A_BOLD)
1214-
self.screen.addstr(1, 0,
1215-
'This might limit the shown data to the trace '
1216-
'statistics.')
1217-
self.screen.addstr(5, 0, msg)
1218-
self._print_all_gnames(7)
1219-
1220-
curses.echo()
1221-
self.screen.addstr(3, 0, "Pid [0 or pid]: ")
1222-
pid = self.screen.getstr().decode(ENCODING)
1223-
curses.noecho()
1224-
1225-
try:
1226-
if len(pid) > 0:
1227-
pid = int(pid)
1228-
if pid != 0 and not os.path.isdir(os.path.join('/proc/',
1229-
str(pid))):
1230-
msg = '"' + str(pid) + '": Not a running process'
1231-
continue
1232-
else:
1233-
pid = 0
1234-
self._refresh_header(pid)
1235-
self._update_pid(pid)
1236-
break
1237-
except ValueError:
1238-
msg = '"' + str(pid) + '": Not a valid pid'
1239-
12401204
def _show_set_update_interval(self):
12411205
"""Draws update interval selection mask."""
12421206
msg = ''
@@ -1269,50 +1233,57 @@ class Tui(object):
12691233
msg = '"' + str(val) + '": Invalid value'
12701234
self._refresh_header()
12711235

1272-
def _show_vm_selection_by_guest_name(self):
1236+
def _show_vm_selection_by_guest(self):
12731237
"""Draws guest selection mask.
12741238
1275-
Asks for a guest name until a valid guest name or '' is entered.
1239+
Asks for a guest name or pid until a valid guest name or '' is entered.
12761240
12771241
"""
12781242
msg = ''
12791243
while True:
12801244
self.screen.erase()
12811245
self.screen.addstr(0, 0,
1282-
'Show statistics for specific guest.',
1246+
'Show statistics for specific guest or pid.',
12831247
curses.A_BOLD)
12841248
self.screen.addstr(1, 0,
12851249
'This might limit the shown data to the trace '
12861250
'statistics.')
12871251
self.screen.addstr(5, 0, msg)
12881252
self._print_all_gnames(7)
12891253
curses.echo()
1290-
self.screen.addstr(3, 0, "Guest [ENTER or guest]: ")
1291-
gname = self.screen.getstr().decode(ENCODING)
1254+
curses.curs_set(1)
1255+
self.screen.addstr(3, 0, "Guest or pid [ENTER exits]: ")
1256+
guest = self.screen.getstr().decode(ENCODING)
12921257
curses.noecho()
12931258

1294-
if not gname:
1295-
self._refresh_header(0)
1296-
self._update_pid(0)
1259+
pid = 0
1260+
if not guest or guest == '0':
12971261
break
1298-
else:
1299-
pids = []
1300-
try:
1301-
pids = self.get_pid_from_gname(gname)
1302-
except:
1303-
msg = '"' + gname + '": Internal error while searching, ' \
1304-
'use pid filter instead'
1305-
continue
1306-
if len(pids) == 0:
1307-
msg = '"' + gname + '": Not an active guest'
1262+
if guest.isdigit():
1263+
if not os.path.isdir(os.path.join('/proc/', guest)):
1264+
msg = '"' + guest + '": Not a running process'
13081265
continue
1309-
if len(pids) > 1:
1310-
msg = '"' + gname + '": Multiple matches found, use pid ' \
1311-
'filter instead'
1312-
continue
1313-
self._refresh_header(pids[0])
1314-
self._update_pid(pids[0])
1266+
pid = int(guest)
13151267
break
1268+
pids = []
1269+
try:
1270+
pids = self.get_pid_from_gname(guest)
1271+
except:
1272+
msg = '"' + guest + '": Internal error while searching, ' \
1273+
'use pid filter instead'
1274+
continue
1275+
if len(pids) == 0:
1276+
msg = '"' + guest + '": Not an active guest'
1277+
continue
1278+
if len(pids) > 1:
1279+
msg = '"' + guest + '": Multiple matches found, use pid ' \
1280+
'filter instead'
1281+
continue
1282+
pid = pids[0]
1283+
break
1284+
curses.curs_set(0)
1285+
self._refresh_header(pid)
1286+
self._update_pid(pid)
13161287

13171288
def show_stats(self):
13181289
"""Refreshes the screen and processes user input."""
@@ -1344,20 +1315,13 @@ class Tui(object):
13441315
self._show_filter_selection()
13451316
curses.curs_set(0)
13461317
sleeptime = self._delay_initial
1347-
if char == 'g':
1348-
curses.curs_set(1)
1349-
self._show_vm_selection_by_guest_name()
1350-
curses.curs_set(0)
1318+
if char == 'g' or char == 'p':
1319+
self._show_vm_selection_by_guest()
13511320
sleeptime = self._delay_initial
13521321
if char == 'h':
13531322
self._show_help_interactive()
13541323
if char == 'o':
13551324
self._sorting = not self._sorting
1356-
if char == 'p':
1357-
curses.curs_set(1)
1358-
self._show_vm_selection_by_pid()
1359-
curses.curs_set(0)
1360-
sleeptime = self._delay_initial
13611325
if char == 'q':
13621326
break
13631327
if char == 'r':

tools/kvm/kvm_stat/kvm_stat.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ INTERACTIVE COMMANDS
3535

3636
*f*:: filter by regular expression
3737

38-
*g*:: filter by guest name
38+
*g*:: filter by guest name/PID
3939

4040
*h*:: display interactive commands reference
4141

4242
*o*:: toggle sorting order (Total vs CurAvg/s)
4343

44-
*p*:: filter by PID
44+
*p*:: filter by guest name/PID
4545

4646
*q*:: quit
4747

0 commit comments

Comments
 (0)