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
, ctypes
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 # From include/uapi/linux/kvm.h, KVM_EXIT_xxx
145 userspace_exit_reasons
= {
153 7: 'IRQ_WINDOW_OPEN',
163 17: 'INTERNAL_ERROR',
173 'vmx': vmx_exit_reasons
,
174 'svm': svm_exit_reasons
,
177 sc_perf_evt_open
= None
181 'SET_FILTER' : 0x40082406,
182 'ENABLE' : 0x00002400,
183 'DISABLE' : 0x00002401,
188 'sc_perf_evt_open' : 298,
189 'exit_reasons' : x86_exit_reasons
[flag
],
194 'sc_perf_evt_open' : 331
199 'sc_perf_evt_open' : 319,
201 'SET_FILTER' : 0x80002406 |
(ctypes
.sizeof(ctypes
.c_char_p
) << 16),
202 'ENABLE' : 0x20002400,
203 'DISABLE' : 0x20002401,
207 def detect_platform():
208 if os
.uname()[4].startswith('ppc'):
212 for line
in file('/proc/cpuinfo').readlines():
213 if line
.startswith('flags'):
214 for flag
in line
.split():
215 if flag
in x86_exit_reasons
:
218 elif line
.startswith('vendor_id'):
219 for flag
in line
.split():
220 if flag
== 'IBM/S390':
227 return dict((x
[1], x
[0]) for x
in d
.iteritems())
230 filters
['kvm_userspace_exit'] = ('reason', invert(userspace_exit_reasons
))
232 filters
['kvm_exit'] = ('exit_reason', invert(exit_reasons
))
236 libc
= ctypes
.CDLL('libc.so.6')
237 syscall
= libc
.syscall
238 class perf_event_attr(ctypes
.Structure
):
239 _fields_
= [('type', ctypes
.c_uint32
),
240 ('size', ctypes
.c_uint32
),
241 ('config', ctypes
.c_uint64
),
242 ('sample_freq', ctypes
.c_uint64
),
243 ('sample_type', ctypes
.c_uint64
),
244 ('read_format', ctypes
.c_uint64
),
245 ('flags', ctypes
.c_uint64
),
246 ('wakeup_events', ctypes
.c_uint32
),
247 ('bp_type', ctypes
.c_uint32
),
248 ('bp_addr', ctypes
.c_uint64
),
249 ('bp_len', ctypes
.c_uint64
),
251 def _perf_event_open(attr
, pid
, cpu
, group_fd
, flags
):
252 return syscall(sc_perf_evt_open
, ctypes
.pointer(attr
), ctypes
.c_int(pid
),
253 ctypes
.c_int(cpu
), ctypes
.c_int(group_fd
),
254 ctypes
.c_long(flags
))
256 PERF_TYPE_HARDWARE
= 0
257 PERF_TYPE_SOFTWARE
= 1
258 PERF_TYPE_TRACEPOINT
= 2
259 PERF_TYPE_HW_CACHE
= 3
261 PERF_TYPE_BREAKPOINT
= 5
263 PERF_SAMPLE_IP
= 1 << 0
264 PERF_SAMPLE_TID
= 1 << 1
265 PERF_SAMPLE_TIME
= 1 << 2
266 PERF_SAMPLE_ADDR
= 1 << 3
267 PERF_SAMPLE_READ
= 1 << 4
268 PERF_SAMPLE_CALLCHAIN
= 1 << 5
269 PERF_SAMPLE_ID
= 1 << 6
270 PERF_SAMPLE_CPU
= 1 << 7
271 PERF_SAMPLE_PERIOD
= 1 << 8
272 PERF_SAMPLE_STREAM_ID
= 1 << 9
273 PERF_SAMPLE_RAW
= 1 << 10
275 PERF_FORMAT_TOTAL_TIME_ENABLED
= 1 << 0
276 PERF_FORMAT_TOTAL_TIME_RUNNING
= 1 << 1
277 PERF_FORMAT_ID
= 1 << 2
278 PERF_FORMAT_GROUP
= 1 << 3
282 sys_tracing
= '/sys/kernel/debug/tracing'
285 def __init__(self
, cpu
):
287 self
.group_leader
= None
289 def add_event(self
, name
, event_set
, tracepoint
, filter = None):
290 self
.events
.append(Event(group
= self
,
291 name
= name
, event_set
= event_set
,
292 tracepoint
= tracepoint
, filter = filter))
293 if len(self
.events
) == 1:
294 self
.file = os
.fdopen(self
.events
[0].fd
)
296 bytes
= 8 * (1 + len(self
.events
))
297 fmt
= 'xxxxxxxx' + 'q' * len(self
.events
)
298 return dict(zip([event
.name
for event
in self
.events
],
299 struct
.unpack(fmt
, self
.file.read(bytes
))))
302 def __init__(self
, group
, name
, event_set
, tracepoint
, filter = None):
304 attr
= perf_event_attr()
305 attr
.type = PERF_TYPE_TRACEPOINT
306 attr
.size
= ctypes
.sizeof(attr
)
307 id_path
= os
.path
.join(sys_tracing
, 'events', event_set
,
309 id = int(file(id_path
).read())
311 attr
.sample_type
= (PERF_SAMPLE_RAW
314 attr
.sample_period
= 1
315 attr
.read_format
= PERF_FORMAT_GROUP
318 group_leader
= group
.events
[0].fd
319 fd
= _perf_event_open(attr
, -1, group
.cpu
, group_leader
, 0)
321 raise Exception('perf_event_open failed')
324 fcntl
.ioctl(fd
, ioctl_numbers
['SET_FILTER'], filter)
328 fcntl
.ioctl(self
.fd
, ioctl_numbers
['ENABLE'], 0)
331 fcntl
.ioctl(self
.fd
, ioctl_numbers
['DISABLE'], 0)
333 class TracepointProvider(object):
335 path
= os
.path
.join(sys_tracing
, 'events', 'kvm')
337 for f
in os
.listdir(path
)
338 if os
.path
.isdir(os
.path
.join(path
, f
))]
342 subfield
, values
= filters
[f
]
343 for name
, number
in values
.iteritems():
344 extra
.append(f
+ '(' + name
+ ')')
351 def _online_cpus(self
):
353 pattern
= r
'cpu([0-9]+)'
354 basedir
= '/sys/devices/system/cpu'
355 for entry
in os
.listdir(basedir
):
356 match
= re
.match(pattern
, entry
)
359 path
= os
.path
.join(basedir
, entry
, 'online')
360 if os
.path
.exists(path
) and open(path
).read().strip() != '1':
362 l
.append(int(match
.group(1)))
365 def _setup(self
, _fields
):
366 self
._fields
= _fields
367 cpus
= self
._online
_cpus
()
369 nfiles
= len(cpus
) * 1000
370 resource
.setrlimit(resource
.RLIMIT_NOFILE
, (nfiles
, nfiles
))
372 self
.group_leaders
= []
378 m
= re
.match(r
'(.*)\((.*)\)', name
)
380 tracepoint
, sub
= m
.groups()
381 filter = '%s==%d\0' % (filters
[tracepoint
][0],
382 filters
[tracepoint
][1][sub
])
383 event
= group
.add_event(name
, event_set
= 'kvm',
384 tracepoint
= tracepoint
,
386 self
.group_leaders
.append(group
)
387 def select(self
, fields
):
388 for group
in self
.group_leaders
:
389 for event
in group
.events
:
390 if event
.name
in fields
:
395 from collections
import defaultdict
396 ret
= defaultdict(int)
397 for group
in self
.group_leaders
:
398 for name
, val
in group
.read().iteritems():
403 def __init__(self
, providers
, fields
= None):
404 self
.providers
= providers
405 self
.fields_filter
= fields
410 if not self
.fields_filter
:
412 return re
.match(self
.fields_filter
, key
) is not None
415 provider_fields
= [key
for key
in d
.fields() if wanted(key
)]
416 for key
in provider_fields
:
417 self
.values
[key
] = None
418 d
.select(provider_fields
)
419 def set_fields_filter(self
, fields_filter
):
420 self
.fields_filter
= fields_filter
425 for key
in d
.fields():
426 oldval
= self
.values
.get(key
, (0, 0))
429 if oldval
is not None:
430 newdelta
= newval
- oldval
[0]
431 self
.values
[key
] = (newval
, newdelta
)
434 if not os
.access('/sys/kernel/debug', os
.F_OK
):
435 print 'Please enable CONFIG_DEBUG_FS in your kernel'
437 if not os
.access('/sys/kernel/debug/kvm', os
.F_OK
):
438 print "Please mount debugfs ('mount -t debugfs debugfs /sys/kernel/debug')"
439 print "and ensure the kvm modules are loaded"
445 def tui(screen
, stats
):
446 curses
.use_default_colors()
449 fields_filter
= stats
.fields_filter
450 def update_drilldown():
451 if not fields_filter
:
453 stats
.set_fields_filter(None)
455 stats
.set_fields_filter(r
'^[^\(]*$')
457 def refresh(sleeptime
):
459 screen
.addstr(0, 0, 'kvm statistics')
464 return (-s
[x
][1], -s
[x
][0])
467 for key
in sorted(s
.keys(), key
= sortkey
):
468 if row
>= screen
.getmaxyx()[0]:
471 if not values
[0] and not values
[1]:
474 screen
.addstr(row
, col
, key
)
476 screen
.addstr(row
, col
, '%10d' % (values
[0],))
478 if values
[1] is not None:
479 screen
.addstr(row
, col
, '%8d' % (values
[1] / sleeptime
,))
486 curses
.halfdelay(int(sleeptime
* 10))
491 drilldown
= not drilldown
495 except KeyboardInterrupt:
504 for key
in sorted(s
.keys()):
506 print '%-22s%10d%10d' % (key
, values
[0], values
[1])
509 keys
= sorted(stats
.get().iterkeys())
512 print '%10s' % k
[0:9],
517 print ' %9d' % s
[k
][1],
523 if line
% banner_repeat
== 0:
528 options
= optparse
.OptionParser()
529 options
.add_option('-1', '--once', '--batch',
530 action
= 'store_true',
533 help = 'run in batch mode for one second',
535 options
.add_option('-l', '--log',
536 action
= 'store_true',
539 help = 'run in logging mode (like vmstat)',
541 options
.add_option('-t', '--tracepoints',
542 action
= 'store_true',
544 dest
= 'tracepoints',
545 help = 'retrieve statistics from tracepoints',
547 options
.add_option('-d', '--debugfs',
548 action
= 'store_true',
551 help = 'retrieve statistics from debugfs',
553 options
.add_option('-f', '--fields',
557 help = 'fields to display (regex)',
559 (options
, args
) = options
.parse_args(sys
.argv
)
562 if options
.tracepoints
:
563 providers
.append(TracepointProvider())
565 providers
.append(DebugfsProvider())
567 if len(providers
) == 0:
569 providers
= [TracepointProvider()]
571 providers
= [DebugfsProvider()]
573 stats
= Stats(providers
, fields
= options
.fields
)
577 elif not options
.once
:
578 import curses
.wrapper
579 curses
.wrapper(tui
, stats
)