Commit da1fda28 authored by Stefan Raspl's avatar Stefan Raspl Committed by Paolo Bonzini

tools/kvm_stat: add command line switch '-z' to skip zero records

When running in logging mode, skip records with all zeros (=empty records)
to preserve space when logging to files.
Signed-off-by: default avatarStefan Raspl <raspl@de.ibm.com>
Message-Id: <20200402085705.61155-2-raspl@linux.ibm.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 1b94f6f8
...@@ -1500,8 +1500,7 @@ class StdFormat(object): ...@@ -1500,8 +1500,7 @@ class StdFormat(object):
def get_banner(self): def get_banner(self):
return self._banner return self._banner
@staticmethod def get_statline(self, keys, s):
def get_statline(keys, s):
res = '' res = ''
for key in keys: for key in keys:
res += ' %9d' % s[key].delta res += ' %9d' % s[key].delta
...@@ -1517,8 +1516,7 @@ class CSVFormat(object): ...@@ -1517,8 +1516,7 @@ class CSVFormat(object):
def get_banner(self): def get_banner(self):
return self._banner return self._banner
@staticmethod def get_statline(self, keys, s):
def get_statline(keys, s):
return reduce(lambda res, key: "{},{!s}".format(res, s[key].delta), return reduce(lambda res, key: "{},{!s}".format(res, s[key].delta),
keys, '') keys, '')
...@@ -1527,14 +1525,21 @@ def log(stats, opts, frmt, keys): ...@@ -1527,14 +1525,21 @@ def log(stats, opts, frmt, keys):
"""Prints statistics as reiterating key block, multiple value blocks.""" """Prints statistics as reiterating key block, multiple value blocks."""
line = 0 line = 0
banner_repeat = 20 banner_repeat = 20
banner_printed = False
while True: while True:
try: try:
time.sleep(opts.set_delay) time.sleep(opts.set_delay)
if line % banner_repeat == 0: if line % banner_repeat == 0 and not banner_printed:
print(frmt.get_banner()) print(frmt.get_banner())
banner_printed = True
values = stats.get()
if (not opts.skip_zero_records or
any(values[k].delta != 0 for k in keys)):
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") +
frmt.get_statline(keys, stats.get())) frmt.get_statline(keys, values))
line += 1 line += 1
banner_printed = False
except KeyboardInterrupt: except KeyboardInterrupt:
break break
...@@ -1655,9 +1660,16 @@ Press any other key to refresh statistics immediately. ...@@ -1655,9 +1660,16 @@ Press any other key to refresh statistics immediately.
default=False, default=False,
help='retrieve statistics from tracepoints', help='retrieve statistics from tracepoints',
) )
argparser.add_argument('-z', '--skip-zero-records',
action='store_true',
default=False,
help='omit records with all zeros in logging mode',
)
options = argparser.parse_args() options = argparser.parse_args()
if options.csv and not options.log: if options.csv and not options.log:
sys.exit('Error: Option -c/--csv requires -l/--log') sys.exit('Error: Option -c/--csv requires -l/--log')
if options.skip_zero_records and not options.log:
sys.exit('Error: Option -z/--skip-zero-records requires -l/--log')
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)
......
...@@ -104,6 +104,10 @@ OPTIONS ...@@ -104,6 +104,10 @@ OPTIONS
--tracepoints:: --tracepoints::
retrieve statistics from tracepoints retrieve statistics from tracepoints
*z*::
--skip-zero-records::
omit records with all zeros in logging mode
SEE ALSO SEE ALSO
-------- --------
'perf'(1), 'trace-cmd'(1) 'perf'(1), 'trace-cmd'(1)
......
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