Skip to content

Commit 0c794dc

Browse files
Stefan Rasplbonzini
authored andcommitted
tools/kvm_stat: add command line switch '-c' to log in csv format
Add an alternative format that can be more easily used for further processing later on. Note that we add a timestamp in the first column for both, the regular and the new csv format. Signed-off-by: Stefan Raspl <raspl@linux.ibm.com> Message-Id: <20200306114250.57585-5-raspl@linux.ibm.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 3cbb394 commit 0c794dc

2 files changed

Lines changed: 53 additions & 14 deletions

File tree

‎tools/kvm/kvm_stat/kvm_stat‎

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import struct
3333
import re
3434
import subprocess
3535
from collections import defaultdict, namedtuple
36+
from functools import reduce
37+
from datetime import datetime
3638

3739
VMX_EXIT_REASONS = {
3840
'EXCEPTION_NMI': 0,
@@ -1489,28 +1491,49 @@ def batch(stats):
14891491
pass
14901492

14911493

1492-
def log(stats, opts):
1493-
"""Prints statistics as reiterating key block, multiple value blocks."""
1494-
keys = sorted(stats.get().keys())
1495-
1496-
def banner():
1494+
class StdFormat(object):
1495+
def __init__(self, keys):
1496+
self._banner = ''
14971497
for key in keys:
1498-
print(key.split(' ')[0], end=' ')
1499-
print()
1498+
self._banner += key.split(' ')[0] + ' '
15001499

1501-
def statline():
1502-
s = stats.get()
1500+
def get_banner(self):
1501+
return self._banner
1502+
1503+
@staticmethod
1504+
def get_statline(keys, s):
1505+
res = ''
15031506
for key in keys:
1504-
print(' %9d' % s[key].delta, end=' ')
1505-
print()
1507+
res += ' %9d' % s[key].delta
1508+
return res
1509+
1510+
1511+
class CSVFormat(object):
1512+
def __init__(self, keys):
1513+
self._banner = 'timestamp'
1514+
self._banner += reduce(lambda res, key: "{},{!s}".format(res,
1515+
key.split(' ')[0]), keys, '')
1516+
1517+
def get_banner(self):
1518+
return self._banner
1519+
1520+
@staticmethod
1521+
def get_statline(keys, s):
1522+
return reduce(lambda res, key: "{},{!s}".format(res, s[key].delta),
1523+
keys, '')
1524+
1525+
1526+
def log(stats, opts, frmt, keys):
1527+
"""Prints statistics as reiterating key block, multiple value blocks."""
15061528
line = 0
15071529
banner_repeat = 20
15081530
while True:
15091531
try:
15101532
time.sleep(opts.set_delay)
15111533
if line % banner_repeat == 0:
1512-
banner()
1513-
statline()
1534+
print(frmt.get_banner())
1535+
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S") +
1536+
frmt.get_statline(keys, stats.get()))
15141537
line += 1
15151538
except KeyboardInterrupt:
15161539
break
@@ -1584,6 +1607,11 @@ Press any other key to refresh statistics immediately.
15841607
default=False,
15851608
help='run in batch mode for one second',
15861609
)
1610+
argparser.add_argument('-c', '--csv',
1611+
action='store_true',
1612+
default=False,
1613+
help='log in csv format - requires option -l/--log',
1614+
)
15871615
argparser.add_argument('-d', '--debugfs',
15881616
action='store_true',
15891617
default=False,
@@ -1628,6 +1656,8 @@ Press any other key to refresh statistics immediately.
16281656
help='retrieve statistics from tracepoints',
16291657
)
16301658
options = argparser.parse_args()
1659+
if options.csv and not options.log:
1660+
sys.exit('Error: Option -c/--csv requires -l/--log')
16311661
try:
16321662
# verify that we were passed a valid regex up front
16331663
re.compile(options.fields)
@@ -1708,7 +1738,12 @@ def main():
17081738
sys.exit(0)
17091739

17101740
if options.log:
1711-
log(stats, options)
1741+
keys = sorted(stats.get().keys())
1742+
if options.csv:
1743+
frmt = CSVFormat(keys)
1744+
else:
1745+
frmt = StdFormat(keys)
1746+
log(stats, options, frmt, keys)
17121747
elif not options.once:
17131748
with Tui(stats, options) as tui:
17141749
tui.show_stats()

‎tools/kvm/kvm_stat/kvm_stat.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ OPTIONS
6464
--batch::
6565
run in batch mode for one second
6666

67+
-c::
68+
--csv=<file>::
69+
log in csv format - requires option -l/--log
70+
6771
-d::
6872
--debugfs::
6973
retrieve statistics from debugfs

0 commit comments

Comments
 (0)