Commit 0e6618fb authored by Stefan Raspl's avatar Stefan Raspl Committed by Paolo Bonzini

tools/kvm_stat: switch to argparse

optparse is deprecated for a while, hence switching over to argparse
(which also works with python2).
As a consequence, help output has some subtle changes, the most
significant one being that the options are all listed explicitly
instead of a universal '[options]' indicator. Also, some of the error
messages are phrased slightly different.
While at it, squashed a number of minor PEP8 issues.
Signed-off-by: default avatarStefan Raspl <raspl@linux.ibm.com>
Message-Id: <20200306114250.57585-3-raspl@linux.ibm.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent eecda7a9
...@@ -25,7 +25,7 @@ import sys ...@@ -25,7 +25,7 @@ import sys
import locale import locale
import os import os
import time import time
import optparse import argparse
import ctypes import ctypes
import fcntl import fcntl
import resource import resource
...@@ -873,7 +873,7 @@ class Stats(object): ...@@ -873,7 +873,7 @@ class Stats(object):
if options.debugfs: if options.debugfs:
providers.append(DebugfsProvider(options.pid, options.fields, providers.append(DebugfsProvider(options.pid, options.fields,
options.dbgfs_include_past)) options.debugfs_include_past))
if options.tracepoints or not providers: if options.tracepoints or not providers:
providers.append(TracepointProvider(options.pid, options.fields)) providers.append(TracepointProvider(options.pid, options.fields))
...@@ -1550,84 +1550,66 @@ Interactive Commands: ...@@ -1550,84 +1550,66 @@ Interactive Commands:
Press any other key to refresh statistics immediately. Press any other key to refresh statistics immediately.
""" % (PATH_DEBUGFS_KVM, PATH_DEBUGFS_TRACING) """ % (PATH_DEBUGFS_KVM, PATH_DEBUGFS_TRACING)
class PlainHelpFormatter(optparse.IndentedHelpFormatter): class Guest_to_pid(argparse.Action):
def format_description(self, description): def __call__(self, parser, namespace, values, option_string=None):
if description: try:
return description + "\n" pids = Tui.get_pid_from_gname(values)
else: except:
return "" sys.exit('Error while searching for guest "{}". Use "-p" to '
'specify a pid instead?'.format(values))
def cb_guest_to_pid(option, opt, val, parser): if len(pids) == 0:
try: sys.exit('Error: No guest by the name "{}" found'
pids = Tui.get_pid_from_gname(val) .format(values))
except: if len(pids) > 1:
sys.exit('Error while searching for guest "{}". Use "-p" to ' sys.exit('Error: Multiple processes found (pids: {}). Use "-p"'
'specify a pid instead?'.format(val)) ' to specify the desired pid'.format(" ".join(pids)))
if len(pids) == 0: namespace.pid = pids[0]
sys.exit('Error: No guest by the name "{}" found'.format(val))
if len(pids) > 1: argparser = argparse.ArgumentParser(description=description_text,
sys.exit('Error: Multiple processes found (pids: {}). Use "-p" ' formatter_class=argparse
'to specify the desired pid'.format(" ".join(pids))) .RawTextHelpFormatter)
parser.values.pid = pids[0] argparser.add_argument('-1', '--once', '--batch',
action='store_true',
optparser = optparse.OptionParser(description=description_text, default=False,
formatter=PlainHelpFormatter()) help='run in batch mode for one second',
optparser.add_option('-1', '--once', '--batch', )
action='store_true', argparser.add_argument('-d', '--debugfs',
default=False, action='store_true',
dest='once', default=False,
help='run in batch mode for one second', help='retrieve statistics from debugfs',
) )
optparser.add_option('-i', '--debugfs-include-past', argparser.add_argument('-f', '--fields',
action='store_true', default='',
default=False, help='''fields to display (regex)
dest='dbgfs_include_past', "-f help" for a list of available events''',
help='include all available data on past events for ' )
'debugfs', argparser.add_argument('-g', '--guest',
) type=str,
optparser.add_option('-l', '--log', help='restrict statistics to guest by name',
action='store_true', action=Guest_to_pid,
default=False, )
dest='log', argparser.add_argument('-i', '--debugfs-include-past',
help='run in logging mode (like vmstat)', action='store_true',
) default=False,
optparser.add_option('-t', '--tracepoints', help='include all available data on past events for'
action='store_true', ' debugfs',
default=False, )
dest='tracepoints', argparser.add_argument('-l', '--log',
help='retrieve statistics from tracepoints', action='store_true',
) default=False,
optparser.add_option('-d', '--debugfs', help='run in logging mode (like vmstat)',
action='store_true', )
default=False, argparser.add_argument('-p', '--pid',
dest='debugfs', type=int,
help='retrieve statistics from debugfs', default=0,
) help='restrict statistics to pid',
optparser.add_option('-f', '--fields', )
action='store', argparser.add_argument('-t', '--tracepoints',
default='', action='store_true',
dest='fields', default=False,
help='''fields to display (regex) help='retrieve statistics from tracepoints',
"-f help" for a list of available events''', )
) options = argparser.parse_args()
optparser.add_option('-p', '--pid',
action='store',
default=0,
type='int',
dest='pid',
help='restrict statistics to pid',
)
optparser.add_option('-g', '--guest',
action='callback',
type='string',
dest='pid',
metavar='GUEST',
help='restrict statistics to guest by name',
callback=cb_guest_to_pid,
)
options, unkn = optparser.parse_args(sys.argv)
if len(unkn) != 1:
sys.exit('Error: Extra argument(s): ' + ' '.join(unkn[1:]))
try: try:
# verify that we were passed a valid regex up front # verify that we were passed a valid regex up front
re.compile(options.fields) re.compile(options.fields)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment