kvm: stats: batch mode
[qemu-kvm/fedora.git] / kvm / kvm_stat
blob92179398a7f963552db181c5f1aeeb6f6065374b
1 #!/usr/bin/python
3 import curses
4 import sys, os, time, optparse
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 tui(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
66 def batch(stats):
67 s = stats.get()
68 time.sleep(1)
69 s = stats.get()
70 for key in sorted(s.keys()):
71 values = s[key]
72 print '%-22s%10d%10d' % (key, values[0], values[1])
74 options = optparse.OptionParser()
75 options.add_option('-1', '--once', '--batch',
76 action = 'store_true',
77 default = False,
78 dest = 'once',
79 help = 'run in batch mode for one second',
81 (options, args) = options.parse_args(sys.argv)
83 if not options.once:
84 import curses.wrapper
85 curses.wrapper(tui, stats)
86 else:
87 batch(stats)