kvm: external module: pci_get_bus_and_slot() compatibility
[kvm-userspace.git] / kvm_stat
blob21aff5b688d7b4f159108c63e703f36400dd4561
1 #!/usr/bin/python
3 import curses
4 import sys, os, time, optparse
6 class Stats:
7 def __init__(self, fields = None):
8 def wanted(key):
9 import re
10 if not fields:
11 return True
12 return re.match(fields, key) != None
13 self.base = '/sys/kernel/debug/kvm'
14 self.values = {}
15 for key in os.listdir(self.base):
16 if wanted(key):
17 self.values[key] = None
18 def get(self):
19 for key, oldval in self.values.iteritems():
20 newval = int(file(self.base + '/' + key).read())
21 newdelta = None
22 if oldval is not None:
23 newdelta = newval - oldval[0]
24 self.values[key] = (newval, newdelta)
25 return self.values
27 if not os.access('/sys/kernel/debug', os.F_OK):
28 print 'Please enable CONFIG_DEBUG_FS in your kernel'
29 sys.exit(1)
30 if not os.access('/sys/kernel/debug/kvm', os.F_OK):
31 print "Please mount debugfs ('mount -t debugfs debugfs /sys/kernel/debug')"
32 print "and ensure the kvm modules are loaded"
33 sys.exit(1)
35 label_width = 20
36 number_width = 10
38 def tui(screen, stats):
39 curses.use_default_colors()
40 curses.noecho()
41 def refresh():
42 screen.erase()
43 screen.addstr(0, 0, 'kvm statistics')
44 row = 2
45 s = stats.get()
46 for key in sorted(s.keys()):
47 if row >= screen.getmaxyx()[0]:
48 break
49 values = s[key]
50 col = 1
51 screen.addstr(row, col, key)
52 col += label_width
53 screen.addstr(row, col, '%10d' % (values[0],))
54 col += number_width
55 if values[1] is not None:
56 screen.addstr(row, col, '%8d' % (values[1],))
57 row += 1
58 screen.refresh()
60 while True:
61 refresh()
62 curses.halfdelay(10)
63 try:
64 c = screen.getkey()
65 if c == 'q':
66 break
67 except KeyboardInterrupt:
68 break
69 except curses.error:
70 continue
72 def batch(stats):
73 s = stats.get()
74 time.sleep(1)
75 s = stats.get()
76 for key in sorted(s.keys()):
77 values = s[key]
78 print '%-22s%10d%10d' % (key, values[0], values[1])
80 def log(stats):
81 keys = sorted(stats.get().iterkeys())
82 def banner():
83 for k in keys:
84 print '%10s' % k[0:9],
85 print
86 def statline():
87 s = stats.get()
88 for k in keys:
89 print ' %9d' % s[k][1],
90 print
91 line = 0
92 banner_repeat = 20
93 while True:
94 time.sleep(1)
95 if line % banner_repeat == 0:
96 banner()
97 statline()
98 line += 1
100 options = optparse.OptionParser()
101 options.add_option('-1', '--once', '--batch',
102 action = 'store_true',
103 default = False,
104 dest = 'once',
105 help = 'run in batch mode for one second',
107 options.add_option('-l', '--log',
108 action = 'store_true',
109 default = False,
110 dest = 'log',
111 help = 'run in logging mode (like vmstat)',
113 options.add_option('-f', '--fields',
114 action = 'store',
115 default = None,
116 dest = 'fields',
117 help = 'fields to display (regex)',
119 (options, args) = options.parse_args(sys.argv)
121 stats = Stats(fields = options.fields)
123 if options.log:
124 log(stats)
125 elif not options.once:
126 import curses.wrapper
127 curses.wrapper(tui, stats)
128 else:
129 batch(stats)