Skip to content

Commit da1fda2

Browse files
Stefan Rasplbonzini
authored andcommitted
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: Stefan Raspl <raspl@de.ibm.com> Message-Id: <20200402085705.61155-2-raspl@linux.ibm.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 1b94f6f commit da1fda2

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

‎tools/kvm/kvm_stat/kvm_stat‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,8 +1500,7 @@ class StdFormat(object):
15001500
def get_banner(self):
15011501
return self._banner
15021502

1503-
@staticmethod
1504-
def get_statline(keys, s):
1503+
def get_statline(self, keys, s):
15051504
res = ''
15061505
for key in keys:
15071506
res += ' %9d' % s[key].delta
@@ -1517,8 +1516,7 @@ class CSVFormat(object):
15171516
def get_banner(self):
15181517
return self._banner
15191518

1520-
@staticmethod
1521-
def get_statline(keys, s):
1519+
def get_statline(self, keys, s):
15221520
return reduce(lambda res, key: "{},{!s}".format(res, s[key].delta),
15231521
keys, '')
15241522

@@ -1527,14 +1525,21 @@ def log(stats, opts, frmt, keys):
15271525
"""Prints statistics as reiterating key block, multiple value blocks."""
15281526
line = 0
15291527
banner_repeat = 20
1528+
banner_printed = False
1529+
15301530
while True:
15311531
try:
15321532
time.sleep(opts.set_delay)
1533-
if line % banner_repeat == 0:
1533+
if line % banner_repeat == 0 and not banner_printed:
15341534
print(frmt.get_banner())
1535-
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") +
1536-
frmt.get_statline(keys, stats.get()))
1537-
line += 1
1535+
banner_printed = True
1536+
values = stats.get()
1537+
if (not opts.skip_zero_records or
1538+
any(values[k].delta != 0 for k in keys)):
1539+
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") +
1540+
frmt.get_statline(keys, values))
1541+
line += 1
1542+
banner_printed = False
15381543
except KeyboardInterrupt:
15391544
break
15401545

@@ -1655,9 +1660,16 @@ Press any other key to refresh statistics immediately.
16551660
default=False,
16561661
help='retrieve statistics from tracepoints',
16571662
)
1663+
argparser.add_argument('-z', '--skip-zero-records',
1664+
action='store_true',
1665+
default=False,
1666+
help='omit records with all zeros in logging mode',
1667+
)
16581668
options = argparser.parse_args()
16591669
if options.csv and not options.log:
16601670
sys.exit('Error: Option -c/--csv requires -l/--log')
1671+
if options.skip_zero_records and not options.log:
1672+
sys.exit('Error: Option -z/--skip-zero-records requires -l/--log')
16611673
try:
16621674
# verify that we were passed a valid regex up front
16631675
re.compile(options.fields)

‎tools/kvm/kvm_stat/kvm_stat.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ OPTIONS
104104
--tracepoints::
105105
retrieve statistics from tracepoints
106106

107+
*z*::
108+
--skip-zero-records::
109+
omit records with all zeros in logging mode
110+
107111
SEE ALSO
108112
--------
109113
'perf'(1), 'trace-cmd'(1)

0 commit comments

Comments
 (0)