kvm: 'make clean' is eager to delete config.mak files
[kvm-userspace.git] / kvm_stat
blob07773b02c4d8ef4302a53f49705d61fe085f437b
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 if row >= screen.getmaxyx()[0]:
44 break
45 values = s[key]
46 col = 1
47 screen.addstr(row, col, key)
48 col += label_width
49 screen.addstr(row, col, '%10d' % (values[0],))
50 col += number_width
51 if values[1] is not None:
52 screen.addstr(row, col, '%8d' % (values[1],))
53 row += 1
54 screen.refresh()
56 while True:
57 refresh()
58 curses.halfdelay(10)
59 try:
60 c = screen.getkey()
61 if c == 'q':
62 break
63 except KeyboardInterrupt:
64 break
65 except curses.error:
66 continue
68 def batch(stats):
69 s = stats.get()
70 time.sleep(1)
71 s = stats.get()
72 for key in sorted(s.keys()):
73 values = s[key]
74 print '%-22s%10d%10d' % (key, values[0], values[1])
76 options = optparse.OptionParser()
77 options.add_option('-1', '--once', '--batch',
78 action = 'store_true',
79 default = False,
80 dest = 'once',
81 help = 'run in batch mode for one second',
83 (options, args) = options.parse_args(sys.argv)
85 if not options.once:
86 import curses.wrapper
87 curses.wrapper(tui, stats)
88 else:
89 batch(stats)