3 # top-like utility for displaying kvm statistics
5 # Copyright 2006-2008 Qumranet Technologies
6 # Copyright 2008-2011 Red Hat, Inc.
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
18 class DebugfsProvider(object):
20 self.base = '/sys/kernel/debug/kvm'
21 self._fields = os.listdir(self.base)
24 def select(self, fields):
28 return int(file(self.base + '/' + key).read())
29 return dict([(key, val(key)) for key in self._fields])
33 1: 'EXTERNAL_INTERRUPT',
35 7: 'PENDING_INTERRUPT',
59 36: 'MWAIT_INSTRUCTION',
60 39: 'MONITOR_INSTRUCTION',
61 40: 'PAUSE_INSTRUCTION',
62 41: 'MCE_DURING_VMENTRY',
63 43: 'TPR_BELOW_THRESHOLD',
104 0x065: 'CR0_SEL_WRITE',
128 0x07d: 'TASK_SWITCH',
129 0x07e: 'FERR_FREEZE',
148 # From include/uapi/linux/kvm.h, KVM_EXIT_xxx
149 userspace_exit_reasons = {
157 7: 'IRQ_WINDOW_OPEN',
167 17: 'INTERNAL_ERROR',
178 'vmx': vmx_exit_reasons,
179 'svm': svm_exit_reasons,
182 sc_perf_evt_open = None
186 'SET_FILTER' : 0x40082406,
187 'ENABLE' : 0x00002400,
188 'DISABLE' : 0x00002401,
189 'RESET' : 0x00002403,
194 'sc_perf_evt_open' : 298,
195 'exit_reasons' : x86_exit_reasons[flag],
200 'sc_perf_evt_open' : 331
205 'sc_perf_evt_open' : 319,
207 'SET_FILTER' : 0x80002406 | (ctypes.sizeof(ctypes.c_char_p) << 16),
208 'ENABLE' : 0x20002400,
209 'DISABLE' : 0x20002401,
215 'sc_perf_evt_open' : 241
218 def detect_platform():
219 if os.uname()[4].startswith('ppc'):
222 elif os.uname()[4].startswith('aarch64'):
226 for line in file('/proc/cpuinfo').readlines():
227 if line.startswith('flags'):
228 for flag in line.split():
229 if flag in x86_exit_reasons:
232 elif line.startswith('vendor_id'):
233 for flag in line.split():
234 if flag == 'IBM/S390':
241 return dict((x[1], x[0]) for x in d.iteritems())
244 filters['kvm_userspace_exit'] = ('reason', invert(userspace_exit_reasons))
246 filters['kvm_exit'] = ('exit_reason', invert(exit_reasons))
250 libc = ctypes.CDLL('libc.so.6')
251 syscall = libc.syscall
252 get_errno = libc.__errno_location
253 get_errno.restype = POINTER(c_int)
255 class perf_event_attr(ctypes.Structure):
256 _fields_ = [('type', ctypes.c_uint32),
257 ('size', ctypes.c_uint32),
258 ('config', ctypes.c_uint64),
259 ('sample_freq', ctypes.c_uint64),
260 ('sample_type', ctypes.c_uint64),
261 ('read_format', ctypes.c_uint64),
262 ('flags', ctypes.c_uint64),
263 ('wakeup_events', ctypes.c_uint32),
264 ('bp_type', ctypes.c_uint32),
265 ('bp_addr', ctypes.c_uint64),
266 ('bp_len', ctypes.c_uint64),
268 def _perf_event_open(attr, pid, cpu, group_fd, flags):
269 return syscall(sc_perf_evt_open, ctypes.pointer(attr), ctypes.c_int(pid),
270 ctypes.c_int(cpu), ctypes.c_int(group_fd),
271 ctypes.c_long(flags))
273 PERF_TYPE_HARDWARE = 0
274 PERF_TYPE_SOFTWARE = 1
275 PERF_TYPE_TRACEPOINT = 2
276 PERF_TYPE_HW_CACHE = 3
278 PERF_TYPE_BREAKPOINT = 5
280 PERF_SAMPLE_IP = 1 << 0
281 PERF_SAMPLE_TID = 1 << 1
282 PERF_SAMPLE_TIME = 1 << 2
283 PERF_SAMPLE_ADDR = 1 << 3
284 PERF_SAMPLE_READ = 1 << 4
285 PERF_SAMPLE_CALLCHAIN = 1 << 5
286 PERF_SAMPLE_ID = 1 << 6
287 PERF_SAMPLE_CPU = 1 << 7
288 PERF_SAMPLE_PERIOD = 1 << 8
289 PERF_SAMPLE_STREAM_ID = 1 << 9
290 PERF_SAMPLE_RAW = 1 << 10
292 PERF_FORMAT_TOTAL_TIME_ENABLED = 1 << 0
293 PERF_FORMAT_TOTAL_TIME_RUNNING = 1 << 1
294 PERF_FORMAT_ID = 1 << 2
295 PERF_FORMAT_GROUP = 1 << 3
299 sys_tracing = '/sys/kernel/debug/tracing'
302 def __init__(self, cpu):
304 self.group_leader = None
306 def add_event(self, name, event_set, tracepoint, filter = None):
307 self.events.append(Event(group = self,
308 name = name, event_set = event_set,
309 tracepoint = tracepoint, filter = filter))
310 if len(self.events) == 1:
311 self.file = os.fdopen(self.events[0].fd)
313 bytes = 8 * (1 + len(self.events))
314 fmt = 'xxxxxxxx' + 'q' * len(self.events)
315 return dict(zip([event.name for event in self.events],
316 struct.unpack(fmt, self.file.read(bytes))))
319 def __init__(self, group, name, event_set, tracepoint, filter = None):
321 attr = perf_event_attr()
322 attr.type = PERF_TYPE_TRACEPOINT
323 attr.size = ctypes.sizeof(attr)
324 id_path = os.path.join(sys_tracing, 'events', event_set,
326 id = int(file(id_path).read())
328 attr.sample_type = (PERF_SAMPLE_RAW
331 attr.sample_period = 1
332 attr.read_format = PERF_FORMAT_GROUP
335 group_leader = group.events[0].fd
336 fd = _perf_event_open(attr, -1, group.cpu, group_leader, 0)
339 raise Exception('perf_event_open failed, errno = ' + err.__str__())
342 fcntl.ioctl(fd, ioctl_numbers['SET_FILTER'], filter)
346 fcntl.ioctl(self.fd, ioctl_numbers['ENABLE'], 0)
349 fcntl.ioctl(self.fd, ioctl_numbers['DISABLE'], 0)
352 fcntl.ioctl(self.fd, ioctl_numbers['RESET'], 0)
354 class TracepointProvider(object):
356 path = os.path.join(sys_tracing, 'events', 'kvm')
358 for f in os.listdir(path)
359 if os.path.isdir(os.path.join(path, f))]
363 subfield, values = filters[f]
364 for name, number in values.iteritems():
365 extra.append(f + '(' + name + ')')
372 def _online_cpus(self):
374 pattern = r'cpu([0-9]+)'
375 basedir = '/sys/devices/system/cpu'
376 for entry in os.listdir(basedir):
377 match = re.match(pattern, entry)
380 path = os.path.join(basedir, entry, 'online')
381 if os.path.exists(path) and open(path).read().strip() != '1':
383 l.append(int(match.group(1)))
386 def _setup(self, _fields):
387 self._fields = _fields
388 cpus = self._online_cpus()
390 nfiles = len(cpus) * 1000
391 resource.setrlimit(resource.RLIMIT_NOFILE, (nfiles, nfiles))
393 self.group_leaders = []
399 m = re.match(r'(.*)\((.*)\)', name)
401 tracepoint, sub = m.groups()
402 filter = '%s==%d\0' % (filters[tracepoint][0],
403 filters[tracepoint][1][sub])
404 event = group.add_event(name, event_set = 'kvm',
405 tracepoint = tracepoint,
407 self.group_leaders.append(group)
408 def select(self, fields):
409 for group in self.group_leaders:
410 for event in group.events:
411 if event.name in fields:
417 from collections import defaultdict
418 ret = defaultdict(int)
419 for group in self.group_leaders:
420 for name, val in group.read().iteritems():
425 def __init__(self, providers, fields = None):
426 self.providers = providers
427 self.fields_filter = fields
432 if not self.fields_filter:
434 return re.match(self.fields_filter, key) is not None
437 provider_fields = [key for key in d.fields() if wanted(key)]
438 for key in provider_fields:
439 self.values[key] = None
440 d.select(provider_fields)
441 def set_fields_filter(self, fields_filter):
442 self.fields_filter = fields_filter
447 for key in d.fields():
448 oldval = self.values.get(key, (0, 0))
451 if oldval is not None:
452 newdelta = newval - oldval[0]
453 self.values[key] = (newval, newdelta)
456 if not os.access('/sys/kernel/debug', os.F_OK):
457 print 'Please enable CONFIG_DEBUG_FS in your kernel'
459 if not os.access('/sys/kernel/debug/kvm', os.F_OK):
460 print "Please mount debugfs ('mount -t debugfs debugfs /sys/kernel/debug')"
461 print "and ensure the kvm modules are loaded"
467 def tui(screen, stats):
468 curses.use_default_colors()
471 fields_filter = stats.fields_filter
472 def update_drilldown():
473 if not fields_filter:
475 stats.set_fields_filter(None)
477 stats.set_fields_filter(r'^[^\(]*$')
479 def refresh(sleeptime):
481 screen.addstr(0, 0, 'kvm statistics')
486 return (-s[x][1], -s[x][0])
489 for key in sorted(s.keys(), key = sortkey):
490 if row >= screen.getmaxyx()[0]:
493 if not values[0] and not values[1]:
496 screen.addstr(row, col, key)
498 screen.addstr(row, col, '%10d' % (values[0],))
500 if values[1] is not None:
501 screen.addstr(row, col, '%8d' % (values[1] / sleeptime,))
508 curses.halfdelay(int(sleeptime * 10))
513 drilldown = not drilldown
517 except KeyboardInterrupt:
526 for key in sorted(s.keys()):
528 print '%-22s%10d%10d' % (key, values[0], values[1])
531 keys = sorted(stats.get().iterkeys())
534 print '%10s' % k[0:9],
539 print ' %9d' % s[k][1],
545 if line % banner_repeat == 0:
550 options = optparse.OptionParser()
551 options.add_option('-1', '--once', '--batch',
552 action = 'store_true',
555 help = 'run in batch mode for one second',
557 options.add_option('-l', '--log',
558 action = 'store_true',
561 help = 'run in logging mode (like vmstat)',
563 options.add_option('-t', '--tracepoints',
564 action = 'store_true',
566 dest = 'tracepoints',
567 help = 'retrieve statistics from tracepoints',
569 options.add_option('-d', '--debugfs',
570 action = 'store_true',
573 help = 'retrieve statistics from debugfs',
575 options.add_option('-f', '--fields',
579 help = 'fields to display (regex)',
581 (options, args) = options.parse_args(sys.argv)
584 if options.tracepoints:
585 providers.append(TracepointProvider())
587 providers.append(DebugfsProvider())
589 if len(providers) == 0:
591 providers = [TracepointProvider()]
593 providers = [DebugfsProvider()]
595 stats = Stats(providers, fields = options.fields)
599 elif not options.once:
600 import curses.wrapper
601 curses.wrapper(tui, stats)