3 # top-like utility for displaying kvm statistics
5 # Copyright 2006-2008 Qumranet Technologies
6 # Copyright 2008-2011 Red Hat, Inc.
9 # Avi Kivity <avi@redhat.com>
11 # This work is licensed under the terms of the GNU GPL, version 2. See
12 # the COPYING file in the top-level directory.
15 import sys
, os
, time
, optparse
17 class DebugfsProvider(object):
19 self
.base
= '/sys/kernel/debug/kvm'
20 self
._fields
= os
.listdir(self
.base
)
23 def select(self
, fields
):
27 return int(file(self
.base
+ '/' + key
).read())
28 return dict([(key
, val(key
)) for key
in self
._fields
])
32 1: 'EXTERNAL_INTERRUPT',
34 7: 'PENDING_INTERRUPT',
58 36: 'MWAIT_INSTRUCTION',
59 39: 'MONITOR_INSTRUCTION',
60 40: 'PAUSE_INSTRUCTION',
61 41: 'MCE_DURING_VMENTRY',
62 43: 'TPR_BELOW_THRESHOLD',
101 0x065: 'CR0_SEL_WRITE',
125 0x07d: 'TASK_SWITCH',
126 0x07e: 'FERR_FREEZE',
144 s390_exit_reasons
= {
152 0x007: 'IRQ_WINDOW_OPEN',
162 0x017: 'INTERNAL_ERROR',
167 vendor_exit_reasons
= {
168 'vmx': vmx_exit_reasons
,
169 'svm': svm_exit_reasons
,
170 'IBM/S390': s390_exit_reasons
,
177 sc_perf_evt_open
= 298
181 for line
in file('/proc/cpuinfo').readlines():
182 if line
.startswith('flags') or line
.startswith('vendor_id'):
183 for flag
in line
.split():
184 if flag
in vendor_exit_reasons
:
185 exit_reasons
= vendor_exit_reasons
[flag
]
186 if flag
in syscall_numbers
:
187 sc_perf_evt_open
= syscall_numbers
[flag
]
189 'kvm_exit': ('exit_reason', exit_reasons
)
193 return dict((x
[1], x
[0]) for x
in d
.iteritems())
196 filters
[f
] = (filters
[f
][0], invert(filters
[f
][1]))
198 import ctypes
, struct
, array
200 libc
= ctypes
.CDLL('libc.so.6')
201 syscall
= libc
.syscall
202 class perf_event_attr(ctypes
.Structure
):
203 _fields_
= [('type', ctypes
.c_uint32
),
204 ('size', ctypes
.c_uint32
),
205 ('config', ctypes
.c_uint64
),
206 ('sample_freq', ctypes
.c_uint64
),
207 ('sample_type', ctypes
.c_uint64
),
208 ('read_format', ctypes
.c_uint64
),
209 ('flags', ctypes
.c_uint64
),
210 ('wakeup_events', ctypes
.c_uint32
),
211 ('bp_type', ctypes
.c_uint32
),
212 ('bp_addr', ctypes
.c_uint64
),
213 ('bp_len', ctypes
.c_uint64
),
215 def _perf_event_open(attr
, pid
, cpu
, group_fd
, flags
):
216 return syscall(sc_perf_evt_open
, ctypes
.pointer(attr
), ctypes
.c_int(pid
),
217 ctypes
.c_int(cpu
), ctypes
.c_int(group_fd
),
218 ctypes
.c_long(flags
))
220 PERF_TYPE_HARDWARE
= 0
221 PERF_TYPE_SOFTWARE
= 1
222 PERF_TYPE_TRACEPOINT
= 2
223 PERF_TYPE_HW_CACHE
= 3
225 PERF_TYPE_BREAKPOINT
= 5
227 PERF_SAMPLE_IP
= 1 << 0
228 PERF_SAMPLE_TID
= 1 << 1
229 PERF_SAMPLE_TIME
= 1 << 2
230 PERF_SAMPLE_ADDR
= 1 << 3
231 PERF_SAMPLE_READ
= 1 << 4
232 PERF_SAMPLE_CALLCHAIN
= 1 << 5
233 PERF_SAMPLE_ID
= 1 << 6
234 PERF_SAMPLE_CPU
= 1 << 7
235 PERF_SAMPLE_PERIOD
= 1 << 8
236 PERF_SAMPLE_STREAM_ID
= 1 << 9
237 PERF_SAMPLE_RAW
= 1 << 10
239 PERF_FORMAT_TOTAL_TIME_ENABLED
= 1 << 0
240 PERF_FORMAT_TOTAL_TIME_RUNNING
= 1 << 1
241 PERF_FORMAT_ID
= 1 << 2
242 PERF_FORMAT_GROUP
= 1 << 3
246 sys_tracing
= '/sys/kernel/debug/tracing'
249 def __init__(self
, cpu
):
251 self
.group_leader
= None
253 def add_event(self
, name
, event_set
, tracepoint
, filter = None):
254 self
.events
.append(Event(group
= self
,
255 name
= name
, event_set
= event_set
,
256 tracepoint
= tracepoint
, filter = filter))
257 if len(self
.events
) == 1:
258 self
.file = os
.fdopen(self
.events
[0].fd
)
260 bytes
= 8 * (1 + len(self
.events
))
261 fmt
= 'xxxxxxxx' + 'q' * len(self
.events
)
262 return dict(zip([event
.name
for event
in self
.events
],
263 struct
.unpack(fmt
, self
.file.read(bytes
))))
266 def __init__(self
, group
, name
, event_set
, tracepoint
, filter = None):
268 attr
= perf_event_attr()
269 attr
.type = PERF_TYPE_TRACEPOINT
270 attr
.size
= ctypes
.sizeof(attr
)
271 id_path
= os
.path
.join(sys_tracing
, 'events', event_set
,
273 id = int(file(id_path
).read())
275 attr
.sample_type
= (PERF_SAMPLE_RAW
278 attr
.sample_period
= 1
279 attr
.read_format
= PERF_FORMAT_GROUP
282 group_leader
= group
.events
[0].fd
283 fd
= _perf_event_open(attr
, -1, group
.cpu
, group_leader
, 0)
285 raise Exception('perf_event_open failed')
288 fcntl
.ioctl(fd
, 0x40082406, filter)
292 fcntl
.ioctl(self
.fd
, 0x00002400, 0)
295 fcntl
.ioctl(self
.fd
, 0x00002401, 0)
297 class TracepointProvider(object):
299 path
= os
.path
.join(sys_tracing
, 'events', 'kvm')
301 for f
in os
.listdir(path
)
302 if os
.path
.isdir(os
.path
.join(path
, f
))]
306 subfield
, values
= filters
[f
]
307 for name
, number
in values
.iteritems():
308 extra
.append(f
+ '(' + name
+ ')')
315 def _online_cpus(self
):
317 pattern
= r
'cpu([0-9]+)'
318 basedir
= '/sys/devices/system/cpu'
319 for entry
in os
.listdir(basedir
):
320 match
= re
.match(pattern
, entry
)
323 path
= os
.path
.join(basedir
, entry
, 'online')
324 if os
.path
.exists(path
) and open(path
).read().strip() != '1':
326 l
.append(int(match
.group(1)))
329 def _setup(self
, _fields
):
330 self
._fields
= _fields
331 cpus
= self
._online
_cpus
()
333 nfiles
= len(cpus
) * 1000
334 resource
.setrlimit(resource
.RLIMIT_NOFILE
, (nfiles
, nfiles
))
336 self
.group_leaders
= []
342 m
= re
.match(r
'(.*)\((.*)\)', name
)
344 tracepoint
, sub
= m
.groups()
345 filter = '%s==%d\0' % (filters
[tracepoint
][0],
346 filters
[tracepoint
][1][sub
])
347 event
= group
.add_event(name
, event_set
= 'kvm',
348 tracepoint
= tracepoint
,
350 self
.group_leaders
.append(group
)
351 def select(self
, fields
):
352 for group
in self
.group_leaders
:
353 for event
in group
.events
:
354 if event
.name
in fields
:
359 from collections
import defaultdict
360 ret
= defaultdict(int)
361 for group
in self
.group_leaders
:
362 for name
, val
in group
.read().iteritems():
367 def __init__(self
, providers
, fields
= None):
368 self
.providers
= providers
369 self
.fields_filter
= fields
374 if not self
.fields_filter
:
376 return re
.match(self
.fields_filter
, key
) is not None
379 provider_fields
= [key
for key
in d
.fields() if wanted(key
)]
380 for key
in provider_fields
:
381 self
.values
[key
] = None
382 d
.select(provider_fields
)
383 def set_fields_filter(self
, fields_filter
):
384 self
.fields_filter
= fields_filter
389 for key
in d
.fields():
390 oldval
= self
.values
.get(key
, (0, 0))
393 if oldval
is not None:
394 newdelta
= newval
- oldval
[0]
395 self
.values
[key
] = (newval
, newdelta
)
398 if not os
.access('/sys/kernel/debug', os
.F_OK
):
399 print 'Please enable CONFIG_DEBUG_FS in your kernel'
401 if not os
.access('/sys/kernel/debug/kvm', os
.F_OK
):
402 print "Please mount debugfs ('mount -t debugfs debugfs /sys/kernel/debug')"
403 print "and ensure the kvm modules are loaded"
409 def tui(screen
, stats
):
410 curses
.use_default_colors()
413 fields_filter
= stats
.fields_filter
414 def update_drilldown():
415 if not fields_filter
:
417 stats
.set_fields_filter(None)
419 stats
.set_fields_filter(r
'^[^\(]*$')
421 def refresh(sleeptime
):
423 screen
.addstr(0, 0, 'kvm statistics')
428 return (-s
[x
][1], -s
[x
][0])
431 for key
in sorted(s
.keys(), key
= sortkey
):
432 if row
>= screen
.getmaxyx()[0]:
435 if not values
[0] and not values
[1]:
438 screen
.addstr(row
, col
, key
)
440 screen
.addstr(row
, col
, '%10d' % (values
[0],))
442 if values
[1] is not None:
443 screen
.addstr(row
, col
, '%8d' % (values
[1] / sleeptime
,))
450 curses
.halfdelay(int(sleeptime
* 10))
455 drilldown
= not drilldown
459 except KeyboardInterrupt:
468 for key
in sorted(s
.keys()):
470 print '%-22s%10d%10d' % (key
, values
[0], values
[1])
473 keys
= sorted(stats
.get().iterkeys())
476 print '%10s' % k
[0:9],
481 print ' %9d' % s
[k
][1],
487 if line
% banner_repeat
== 0:
492 options
= optparse
.OptionParser()
493 options
.add_option('-1', '--once', '--batch',
494 action
= 'store_true',
497 help = 'run in batch mode for one second',
499 options
.add_option('-l', '--log',
500 action
= 'store_true',
503 help = 'run in logging mode (like vmstat)',
505 options
.add_option('-t', '--tracepoints',
506 action
= 'store_true',
508 dest
= 'tracepoints',
509 help = 'retrieve statistics from tracepoints',
511 options
.add_option('-d', '--debugfs',
512 action
= 'store_true',
515 help = 'retrieve statistics from debugfs',
517 options
.add_option('-f', '--fields',
521 help = 'fields to display (regex)',
523 (options
, args
) = options
.parse_args(sys
.argv
)
526 if options
.tracepoints
:
527 providers
.append(TracepointProvider())
529 providers
.append(DebugfsProvider())
531 if len(providers
) == 0:
533 providers
= [TracepointProvider()]
535 providers
= [DebugfsProvider()]
537 stats
= Stats(providers
, fields
= options
.fields
)
541 elif not options
.once
:
542 import curses
.wrapper
543 curses
.wrapper(tui
, stats
)