kvm: stats: more width for labels
[kvm-userspace.git] / kvm_stat
blob5b27a75cbd93d9d8bb7896bd9d83f93b0885b8ab
1 #!/usr/bin/python
3 import curses
4 import sys, os, time
6 class Stats:
7 def __init__(self):
8 self.base = '/sys/kernel/debug/kvm'
9 self.values = {}
10 for key in os.listdir(self.base):
11 self.values[key] = None
12 def get(self):
13 for key, oldval in self.values.iteritems():
14 newval = int(file(self.base + '/' + key).read())
15 newdelta = None
16 if oldval is not None:
17 newdelta = newval - oldval[0]
18 self.values[key] = (newval, newdelta)
19 return self.values
21 if not os.access('/sys/kernel/debug', os.F_OK):
22 print 'Please enable CONFIG_DEBUG_FS in your kernel'
23 sys.exit(1)
24 if not os.access('/sys/kernel/debug/kvm', os.F_OK):
25 print "Please mount debugfs ('mount -t debugfs debugfs /sys/kernel/debug')"
26 print "and ensure the kvm modules are loaded"
27 sys.exit(1)
29 stats = Stats()
31 label_width = 20
32 number_width = 10
34 def main(screen, stats):
35 curses.use_default_colors()
36 curses.noecho()
37 def refresh():
38 screen.erase()
39 screen.addstr(0, 0, 'kvm statistics')
40 row = 2
41 s = stats.get()
42 for key in sorted(s.keys()):
43 values = s[key]
44 col = 1
45 screen.addstr(row, col, key)
46 col += label_width
47 screen.addstr(row, col, '%10d' % (values[0],))
48 col += number_width
49 if values[1] is not None:
50 screen.addstr(row, col, '%8d' % (values[1],))
51 row += 1
52 screen.refresh()
54 while True:
55 refresh()
56 curses.halfdelay(10)
57 try:
58 c = screen.getkey()
59 if c == 'q':
60 break
61 except KeyboardInterrupt:
62 break
63 except curses.error:
64 continue
67 import curses.wrapper
68 curses.wrapper(main, stats)