aboutsummaryrefslogtreecommitdiff
path: root/kvm/kvm_stat
blob: 92179398a7f963552db181c5f1aeeb6f6065374b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python

import curses
import sys, os, time, optparse

class Stats:
    def __init__(self):
        self.base = '/sys/kernel/debug/kvm'
        self.values = {}
        for key in os.listdir(self.base):
            self.values[key] = None
    def get(self):
        for key, oldval in self.values.iteritems():
            newval = int(file(self.base + '/' + key).read())
            newdelta = None
            if oldval is not None:
                newdelta = newval - oldval[0]
            self.values[key] = (newval, newdelta)
        return self.values

if not os.access('/sys/kernel/debug', os.F_OK):
    print 'Please enable CONFIG_DEBUG_FS in your kernel'
    sys.exit(1)
if not os.access('/sys/kernel/debug/kvm', os.F_OK):
    print "Please mount debugfs ('mount -t debugfs debugfs /sys/kernel/debug')"
    print "and ensure the kvm modules are loaded"
    sys.exit(1)

stats = Stats()

label_width = 20
number_width = 10

def tui(screen, stats):
    curses.use_default_colors()
    curses.noecho()
    def refresh():
        screen.erase()
        screen.addstr(0, 0, 'kvm statistics')
        row = 2
        s = stats.get()
        for key in sorted(s.keys()):
            values = s[key]
            col = 1
            screen.addstr(row, col, key)
            col += label_width
            screen.addstr(row, col, '%10d' % (values[0],))
            col += number_width
            if values[1] is not None:
                screen.addstr(row, col, '%8d' % (values[1],))
            row += 1
        screen.refresh()

    while True:
        refresh()
        curses.halfdelay(10)
        try:
            c = screen.getkey()
            if c == 'q':
                break
        except KeyboardInterrupt:
            break
        except curses.error:
            continue

def batch(stats):
    s = stats.get()
    time.sleep(1)
    s = stats.get()
    for key in sorted(s.keys()):
        values = s[key]
        print '%-22s%10d%10d' % (key, values[0], values[1])

options = optparse.OptionParser()
options.add_option('-1', '--once', '--batch',
                   action = 'store_true',
                   default = False,
                   dest = 'once',
                   help = 'run in batch mode for one second',
                   )
(options, args) = options.parse_args(sys.argv)

if not options.once:
    import curses.wrapper
    curses.wrapper(tui, stats)
else:
    batch(stats)