4 import sys
, os
, time
, optparse
7 def __init__(self
, fields
= None):
12 return re
.match(fields
, key
) != None
13 self
.base
= '/sys/kernel/debug/kvm'
15 for key
in os
.listdir(self
.base
):
17 self
.values
[key
] = None
19 for key
, oldval
in self
.values
.iteritems():
20 newval
= int(file(self
.base
+ '/' + key
).read())
22 if oldval
is not None:
23 newdelta
= newval
- oldval
[0]
24 self
.values
[key
] = (newval
, newdelta
)
27 if not os
.access('/sys/kernel/debug', os
.F_OK
):
28 print 'Please enable CONFIG_DEBUG_FS in your kernel'
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"
38 def tui(screen
, stats
):
39 curses
.use_default_colors()
43 screen
.addstr(0, 0, 'kvm statistics')
46 for key
in sorted(s
.keys()):
47 if row
>= screen
.getmaxyx()[0]:
51 screen
.addstr(row
, col
, key
)
53 screen
.addstr(row
, col
, '%10d' % (values
[0],))
55 if values
[1] is not None:
56 screen
.addstr(row
, col
, '%8d' % (values
[1],))
67 except KeyboardInterrupt:
76 for key
in sorted(s
.keys()):
78 print '%-22s%10d%10d' % (key
, values
[0], values
[1])
81 keys
= sorted(stats
.get().iterkeys())
84 print '%10s' % k
[0:9],
89 print ' %9d' % s
[k
][1],
95 if line
% banner_repeat
== 0:
100 options
= optparse
.OptionParser()
101 options
.add_option('-1', '--once', '--batch',
102 action
= 'store_true',
105 help = 'run in batch mode for one second',
107 options
.add_option('-l', '--log',
108 action
= 'store_true',
111 help = 'run in logging mode (like vmstat)',
113 options
.add_option('-f', '--fields',
117 help = 'fields to display (regex)',
119 (options
, args
) = options
.parse_args(sys
.argv
)
121 stats
= Stats(fields
= options
.fields
)
125 elif not options
.once
:
126 import curses
.wrapper
127 curses
.wrapper(tui
, stats
)