From: Stefan Raspl <raspl@xxxxxxxxxx> When running in logging mode, skip records with all zeros (=empty records) to preserve space when logging to files. Signed-off-by: Stefan Raspl <raspl@xxxxxxxxxxxxx> --- tools/kvm/kvm_stat/kvm_stat | 47 +++++++++++++++++++++++++-------- tools/kvm/kvm_stat/kvm_stat.txt | 4 +++ 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/tools/kvm/kvm_stat/kvm_stat b/tools/kvm/kvm_stat/kvm_stat index 7fe767bd2625..54000ac508f9 100755 --- a/tools/kvm/kvm_stat/kvm_stat +++ b/tools/kvm/kvm_stat/kvm_stat @@ -1488,7 +1488,8 @@ def batch(stats): class StdFormat(object): - def __init__(self, keys): + def __init__(self, keys, skip_zero_records): + self._skip_zero_records = skip_zero_records self._banner = '' for key in keys: self._banner += key.split(' ')[0] + ' ' @@ -1496,16 +1497,21 @@ class StdFormat(object): def get_banner(self): return self._banner - @staticmethod - def get_statline(keys, s): + def get_statline(self, keys, s): res = '' + non_zero = False for key in keys: + if s[key].delta != 0: + non_zero = True res += ' %9d' % s[key].delta + if self._skip_zero_records and not non_zero: + return '' return res class CSVFormat(object): - def __init__(self, keys): + def __init__(self, keys, skip_zero_records): + self._skip_zero_records = skip_zero_records self._banner = 'timestamp' self._banner += reduce(lambda res, key: "{},{!s}".format(res, key.split(' ')[0]), keys, '') @@ -1513,8 +1519,14 @@ class CSVFormat(object): def get_banner(self): return self._banner - @staticmethod - def get_statline(keys, s): + def get_statline(self, keys, s): + if self._skip_zero_records: + non_zero = False + for key in keys: + if s[key].delta != 0: + non_zero = True + if self._skip_zero_records and not non_zero: + return '' return reduce(lambda res, key: "{},{!s}".format(res, s[key].delta), keys, '') @@ -1523,14 +1535,20 @@ def log(stats, opts, frmt, keys): """Prints statistics as reiterating key block, multiple value blocks.""" line = 0 banner_repeat = 20 + banner_printed = False + while True: try: time.sleep(opts.set_delay) - if line % banner_repeat == 0: + if line % banner_repeat == 0 and not banner_printed: print(frmt.get_banner()) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + - frmt.get_statline(keys, stats.get())) + banner_printed = True + statline = frmt.get_statline(keys, stats.get()) + if len(statline) == 0: + continue + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") + statline) line += 1 + banner_printed = False except KeyboardInterrupt: break @@ -1651,9 +1669,16 @@ Press any other key to refresh statistics immediately. default=False, 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() if options.csv and not options.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: # verify that we were passed a valid regex up front re.compile(options.fields) @@ -1736,9 +1761,9 @@ def main(): if options.log: keys = sorted(stats.get().keys()) if options.csv: - frmt = CSVFormat(keys) + frmt = CSVFormat(keys, options.skip_zero_records) else: - frmt = StdFormat(keys) + frmt = StdFormat(keys, options.skip_zero_records) log(stats, options, frmt, keys) elif not options.once: with Tui(stats, options) as tui: diff --git a/tools/kvm/kvm_stat/kvm_stat.txt b/tools/kvm/kvm_stat/kvm_stat.txt index a97ded2aedad..24296dccc00a 100644 --- a/tools/kvm/kvm_stat/kvm_stat.txt +++ b/tools/kvm/kvm_stat/kvm_stat.txt @@ -104,6 +104,10 @@ OPTIONS --tracepoints:: retrieve statistics from tracepoints +*z*:: +--skip-zero-records:: + omit records with all zeros in logging mode + SEE ALSO -------- 'perf'(1), 'trace-cmd'(1) -- 2.17.1