]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - tools/kvm/kvm_stat/kvm_stat
tools/kvm_stat: simplify the sortkey function
[mirror_ubuntu-bionic-kernel.git] / tools / kvm / kvm_stat / kvm_stat
1 #!/usr/bin/python
2 #
3 # top-like utility for displaying kvm statistics
4 #
5 # Copyright 2006-2008 Qumranet Technologies
6 # Copyright 2008-2011 Red Hat, Inc.
7 #
8 # Authors:
9 # Avi Kivity <avi@redhat.com>
10 #
11 # This work is licensed under the terms of the GNU GPL, version 2. See
12 # the COPYING file in the top-level directory.
13 """The kvm_stat module outputs statistics about running KVM VMs
14
15 Three different ways of output formatting are available:
16 - as a top-like text ui
17 - in a key -> value format
18 - in an all keys, all values format
19
20 The data is sampled from the KVM's debugfs entries and its perf events.
21 """
22 from __future__ import print_function
23
24 import curses
25 import sys
26 import locale
27 import os
28 import time
29 import optparse
30 import ctypes
31 import fcntl
32 import resource
33 import struct
34 import re
35 import subprocess
36 from collections import defaultdict
37
38 VMX_EXIT_REASONS = {
39 'EXCEPTION_NMI': 0,
40 'EXTERNAL_INTERRUPT': 1,
41 'TRIPLE_FAULT': 2,
42 'PENDING_INTERRUPT': 7,
43 'NMI_WINDOW': 8,
44 'TASK_SWITCH': 9,
45 'CPUID': 10,
46 'HLT': 12,
47 'INVLPG': 14,
48 'RDPMC': 15,
49 'RDTSC': 16,
50 'VMCALL': 18,
51 'VMCLEAR': 19,
52 'VMLAUNCH': 20,
53 'VMPTRLD': 21,
54 'VMPTRST': 22,
55 'VMREAD': 23,
56 'VMRESUME': 24,
57 'VMWRITE': 25,
58 'VMOFF': 26,
59 'VMON': 27,
60 'CR_ACCESS': 28,
61 'DR_ACCESS': 29,
62 'IO_INSTRUCTION': 30,
63 'MSR_READ': 31,
64 'MSR_WRITE': 32,
65 'INVALID_STATE': 33,
66 'MWAIT_INSTRUCTION': 36,
67 'MONITOR_INSTRUCTION': 39,
68 'PAUSE_INSTRUCTION': 40,
69 'MCE_DURING_VMENTRY': 41,
70 'TPR_BELOW_THRESHOLD': 43,
71 'APIC_ACCESS': 44,
72 'EPT_VIOLATION': 48,
73 'EPT_MISCONFIG': 49,
74 'WBINVD': 54,
75 'XSETBV': 55,
76 'APIC_WRITE': 56,
77 'INVPCID': 58,
78 }
79
80 SVM_EXIT_REASONS = {
81 'READ_CR0': 0x000,
82 'READ_CR3': 0x003,
83 'READ_CR4': 0x004,
84 'READ_CR8': 0x008,
85 'WRITE_CR0': 0x010,
86 'WRITE_CR3': 0x013,
87 'WRITE_CR4': 0x014,
88 'WRITE_CR8': 0x018,
89 'READ_DR0': 0x020,
90 'READ_DR1': 0x021,
91 'READ_DR2': 0x022,
92 'READ_DR3': 0x023,
93 'READ_DR4': 0x024,
94 'READ_DR5': 0x025,
95 'READ_DR6': 0x026,
96 'READ_DR7': 0x027,
97 'WRITE_DR0': 0x030,
98 'WRITE_DR1': 0x031,
99 'WRITE_DR2': 0x032,
100 'WRITE_DR3': 0x033,
101 'WRITE_DR4': 0x034,
102 'WRITE_DR5': 0x035,
103 'WRITE_DR6': 0x036,
104 'WRITE_DR7': 0x037,
105 'EXCP_BASE': 0x040,
106 'INTR': 0x060,
107 'NMI': 0x061,
108 'SMI': 0x062,
109 'INIT': 0x063,
110 'VINTR': 0x064,
111 'CR0_SEL_WRITE': 0x065,
112 'IDTR_READ': 0x066,
113 'GDTR_READ': 0x067,
114 'LDTR_READ': 0x068,
115 'TR_READ': 0x069,
116 'IDTR_WRITE': 0x06a,
117 'GDTR_WRITE': 0x06b,
118 'LDTR_WRITE': 0x06c,
119 'TR_WRITE': 0x06d,
120 'RDTSC': 0x06e,
121 'RDPMC': 0x06f,
122 'PUSHF': 0x070,
123 'POPF': 0x071,
124 'CPUID': 0x072,
125 'RSM': 0x073,
126 'IRET': 0x074,
127 'SWINT': 0x075,
128 'INVD': 0x076,
129 'PAUSE': 0x077,
130 'HLT': 0x078,
131 'INVLPG': 0x079,
132 'INVLPGA': 0x07a,
133 'IOIO': 0x07b,
134 'MSR': 0x07c,
135 'TASK_SWITCH': 0x07d,
136 'FERR_FREEZE': 0x07e,
137 'SHUTDOWN': 0x07f,
138 'VMRUN': 0x080,
139 'VMMCALL': 0x081,
140 'VMLOAD': 0x082,
141 'VMSAVE': 0x083,
142 'STGI': 0x084,
143 'CLGI': 0x085,
144 'SKINIT': 0x086,
145 'RDTSCP': 0x087,
146 'ICEBP': 0x088,
147 'WBINVD': 0x089,
148 'MONITOR': 0x08a,
149 'MWAIT': 0x08b,
150 'MWAIT_COND': 0x08c,
151 'XSETBV': 0x08d,
152 'NPF': 0x400,
153 }
154
155 # EC definition of HSR (from arch/arm64/include/asm/kvm_arm.h)
156 AARCH64_EXIT_REASONS = {
157 'UNKNOWN': 0x00,
158 'WFI': 0x01,
159 'CP15_32': 0x03,
160 'CP15_64': 0x04,
161 'CP14_MR': 0x05,
162 'CP14_LS': 0x06,
163 'FP_ASIMD': 0x07,
164 'CP10_ID': 0x08,
165 'CP14_64': 0x0C,
166 'ILL_ISS': 0x0E,
167 'SVC32': 0x11,
168 'HVC32': 0x12,
169 'SMC32': 0x13,
170 'SVC64': 0x15,
171 'HVC64': 0x16,
172 'SMC64': 0x17,
173 'SYS64': 0x18,
174 'IABT': 0x20,
175 'IABT_HYP': 0x21,
176 'PC_ALIGN': 0x22,
177 'DABT': 0x24,
178 'DABT_HYP': 0x25,
179 'SP_ALIGN': 0x26,
180 'FP_EXC32': 0x28,
181 'FP_EXC64': 0x2C,
182 'SERROR': 0x2F,
183 'BREAKPT': 0x30,
184 'BREAKPT_HYP': 0x31,
185 'SOFTSTP': 0x32,
186 'SOFTSTP_HYP': 0x33,
187 'WATCHPT': 0x34,
188 'WATCHPT_HYP': 0x35,
189 'BKPT32': 0x38,
190 'VECTOR32': 0x3A,
191 'BRK64': 0x3C,
192 }
193
194 # From include/uapi/linux/kvm.h, KVM_EXIT_xxx
195 USERSPACE_EXIT_REASONS = {
196 'UNKNOWN': 0,
197 'EXCEPTION': 1,
198 'IO': 2,
199 'HYPERCALL': 3,
200 'DEBUG': 4,
201 'HLT': 5,
202 'MMIO': 6,
203 'IRQ_WINDOW_OPEN': 7,
204 'SHUTDOWN': 8,
205 'FAIL_ENTRY': 9,
206 'INTR': 10,
207 'SET_TPR': 11,
208 'TPR_ACCESS': 12,
209 'S390_SIEIC': 13,
210 'S390_RESET': 14,
211 'DCR': 15,
212 'NMI': 16,
213 'INTERNAL_ERROR': 17,
214 'OSI': 18,
215 'PAPR_HCALL': 19,
216 'S390_UCONTROL': 20,
217 'WATCHDOG': 21,
218 'S390_TSCH': 22,
219 'EPR': 23,
220 'SYSTEM_EVENT': 24,
221 }
222
223 IOCTL_NUMBERS = {
224 'SET_FILTER': 0x40082406,
225 'ENABLE': 0x00002400,
226 'DISABLE': 0x00002401,
227 'RESET': 0x00002403,
228 }
229
230 ENCODING = locale.getpreferredencoding(False)
231
232
233 class Arch(object):
234 """Encapsulates global architecture specific data.
235
236 Contains the performance event open syscall and ioctl numbers, as
237 well as the VM exit reasons for the architecture it runs on.
238
239 """
240 @staticmethod
241 def get_arch():
242 machine = os.uname()[4]
243
244 if machine.startswith('ppc'):
245 return ArchPPC()
246 elif machine.startswith('aarch64'):
247 return ArchA64()
248 elif machine.startswith('s390'):
249 return ArchS390()
250 else:
251 # X86_64
252 for line in open('/proc/cpuinfo'):
253 if not line.startswith('flags'):
254 continue
255
256 flags = line.split()
257 if 'vmx' in flags:
258 return ArchX86(VMX_EXIT_REASONS)
259 if 'svm' in flags:
260 return ArchX86(SVM_EXIT_REASONS)
261 return
262
263
264 class ArchX86(Arch):
265 def __init__(self, exit_reasons):
266 self.sc_perf_evt_open = 298
267 self.ioctl_numbers = IOCTL_NUMBERS
268 self.exit_reasons = exit_reasons
269
270
271 class ArchPPC(Arch):
272 def __init__(self):
273 self.sc_perf_evt_open = 319
274 self.ioctl_numbers = IOCTL_NUMBERS
275 self.ioctl_numbers['ENABLE'] = 0x20002400
276 self.ioctl_numbers['DISABLE'] = 0x20002401
277 self.ioctl_numbers['RESET'] = 0x20002403
278
279 # PPC comes in 32 and 64 bit and some generated ioctl
280 # numbers depend on the wordsize.
281 char_ptr_size = ctypes.sizeof(ctypes.c_char_p)
282 self.ioctl_numbers['SET_FILTER'] = 0x80002406 | char_ptr_size << 16
283 self.exit_reasons = {}
284
285
286 class ArchA64(Arch):
287 def __init__(self):
288 self.sc_perf_evt_open = 241
289 self.ioctl_numbers = IOCTL_NUMBERS
290 self.exit_reasons = AARCH64_EXIT_REASONS
291
292
293 class ArchS390(Arch):
294 def __init__(self):
295 self.sc_perf_evt_open = 331
296 self.ioctl_numbers = IOCTL_NUMBERS
297 self.exit_reasons = None
298
299 ARCH = Arch.get_arch()
300
301
302 class perf_event_attr(ctypes.Structure):
303 """Struct that holds the necessary data to set up a trace event.
304
305 For an extensive explanation see perf_event_open(2) and
306 include/uapi/linux/perf_event.h, struct perf_event_attr
307
308 All fields that are not initialized in the constructor are 0.
309
310 """
311 _fields_ = [('type', ctypes.c_uint32),
312 ('size', ctypes.c_uint32),
313 ('config', ctypes.c_uint64),
314 ('sample_freq', ctypes.c_uint64),
315 ('sample_type', ctypes.c_uint64),
316 ('read_format', ctypes.c_uint64),
317 ('flags', ctypes.c_uint64),
318 ('wakeup_events', ctypes.c_uint32),
319 ('bp_type', ctypes.c_uint32),
320 ('bp_addr', ctypes.c_uint64),
321 ('bp_len', ctypes.c_uint64),
322 ]
323
324 def __init__(self):
325 super(self.__class__, self).__init__()
326 self.type = PERF_TYPE_TRACEPOINT
327 self.size = ctypes.sizeof(self)
328 self.read_format = PERF_FORMAT_GROUP
329
330
331 PERF_TYPE_TRACEPOINT = 2
332 PERF_FORMAT_GROUP = 1 << 3
333
334 PATH_DEBUGFS_TRACING = '/sys/kernel/debug/tracing'
335 PATH_DEBUGFS_KVM = '/sys/kernel/debug/kvm'
336
337
338 class Group(object):
339 """Represents a perf event group."""
340
341 def __init__(self):
342 self.events = []
343
344 def add_event(self, event):
345 self.events.append(event)
346
347 def read(self):
348 """Returns a dict with 'event name: value' for all events in the
349 group.
350
351 Values are read by reading from the file descriptor of the
352 event that is the group leader. See perf_event_open(2) for
353 details.
354
355 Read format for the used event configuration is:
356 struct read_format {
357 u64 nr; /* The number of events */
358 struct {
359 u64 value; /* The value of the event */
360 } values[nr];
361 };
362
363 """
364 length = 8 * (1 + len(self.events))
365 read_format = 'xxxxxxxx' + 'Q' * len(self.events)
366 return dict(zip([event.name for event in self.events],
367 struct.unpack(read_format,
368 os.read(self.events[0].fd, length))))
369
370
371 class Event(object):
372 """Represents a performance event and manages its life cycle."""
373 def __init__(self, name, group, trace_cpu, trace_pid, trace_point,
374 trace_filter, trace_set='kvm'):
375 self.libc = ctypes.CDLL('libc.so.6', use_errno=True)
376 self.syscall = self.libc.syscall
377 self.name = name
378 self.fd = None
379 self.setup_event(group, trace_cpu, trace_pid, trace_point,
380 trace_filter, trace_set)
381
382 def __del__(self):
383 """Closes the event's file descriptor.
384
385 As no python file object was created for the file descriptor,
386 python will not reference count the descriptor and will not
387 close it itself automatically, so we do it.
388
389 """
390 if self.fd:
391 os.close(self.fd)
392
393 def perf_event_open(self, attr, pid, cpu, group_fd, flags):
394 """Wrapper for the sys_perf_evt_open() syscall.
395
396 Used to set up performance events, returns a file descriptor or -1
397 on error.
398
399 Attributes are:
400 - syscall number
401 - struct perf_event_attr *
402 - pid or -1 to monitor all pids
403 - cpu number or -1 to monitor all cpus
404 - The file descriptor of the group leader or -1 to create a group.
405 - flags
406
407 """
408 return self.syscall(ARCH.sc_perf_evt_open, ctypes.pointer(attr),
409 ctypes.c_int(pid), ctypes.c_int(cpu),
410 ctypes.c_int(group_fd), ctypes.c_long(flags))
411
412 def setup_event_attribute(self, trace_set, trace_point):
413 """Returns an initialized ctype perf_event_attr struct."""
414
415 id_path = os.path.join(PATH_DEBUGFS_TRACING, 'events', trace_set,
416 trace_point, 'id')
417
418 event_attr = perf_event_attr()
419 event_attr.config = int(open(id_path).read())
420 return event_attr
421
422 def setup_event(self, group, trace_cpu, trace_pid, trace_point,
423 trace_filter, trace_set):
424 """Sets up the perf event in Linux.
425
426 Issues the syscall to register the event in the kernel and
427 then sets the optional filter.
428
429 """
430
431 event_attr = self.setup_event_attribute(trace_set, trace_point)
432
433 # First event will be group leader.
434 group_leader = -1
435
436 # All others have to pass the leader's descriptor instead.
437 if group.events:
438 group_leader = group.events[0].fd
439
440 fd = self.perf_event_open(event_attr, trace_pid,
441 trace_cpu, group_leader, 0)
442 if fd == -1:
443 err = ctypes.get_errno()
444 raise OSError(err, os.strerror(err),
445 'while calling sys_perf_event_open().')
446
447 if trace_filter:
448 fcntl.ioctl(fd, ARCH.ioctl_numbers['SET_FILTER'],
449 trace_filter)
450
451 self.fd = fd
452
453 def enable(self):
454 """Enables the trace event in the kernel.
455
456 Enabling the group leader makes reading counters from it and the
457 events under it possible.
458
459 """
460 fcntl.ioctl(self.fd, ARCH.ioctl_numbers['ENABLE'], 0)
461
462 def disable(self):
463 """Disables the trace event in the kernel.
464
465 Disabling the group leader makes reading all counters under it
466 impossible.
467
468 """
469 fcntl.ioctl(self.fd, ARCH.ioctl_numbers['DISABLE'], 0)
470
471 def reset(self):
472 """Resets the count of the trace event in the kernel."""
473 fcntl.ioctl(self.fd, ARCH.ioctl_numbers['RESET'], 0)
474
475
476 class Provider(object):
477 """Encapsulates functionalities used by all providers."""
478 @staticmethod
479 def is_field_wanted(fields_filter, field):
480 """Indicate whether field is valid according to fields_filter."""
481 if not fields_filter:
482 return True
483 return re.match(fields_filter, field) is not None
484
485 @staticmethod
486 def walkdir(path):
487 """Returns os.walk() data for specified directory.
488
489 As it is only a wrapper it returns the same 3-tuple of (dirpath,
490 dirnames, filenames).
491 """
492 return next(os.walk(path))
493
494
495 class TracepointProvider(Provider):
496 """Data provider for the stats class.
497
498 Manages the events/groups from which it acquires its data.
499
500 """
501 def __init__(self, pid, fields_filter):
502 self.group_leaders = []
503 self.filters = self.get_filters()
504 self.update_fields(fields_filter)
505 self.pid = pid
506
507 @staticmethod
508 def get_filters():
509 """Returns a dict of trace events, their filter ids and
510 the values that can be filtered.
511
512 Trace events can be filtered for special values by setting a
513 filter string via an ioctl. The string normally has the format
514 identifier==value. For each filter a new event will be created, to
515 be able to distinguish the events.
516
517 """
518 filters = {}
519 filters['kvm_userspace_exit'] = ('reason', USERSPACE_EXIT_REASONS)
520 if ARCH.exit_reasons:
521 filters['kvm_exit'] = ('exit_reason', ARCH.exit_reasons)
522 return filters
523
524 def get_available_fields(self):
525 """Returns a list of available event's of format 'event name(filter
526 name)'.
527
528 All available events have directories under
529 /sys/kernel/debug/tracing/events/ which export information
530 about the specific event. Therefore, listing the dirs gives us
531 a list of all available events.
532
533 Some events like the vm exit reasons can be filtered for
534 specific values. To take account for that, the routine below
535 creates special fields with the following format:
536 event name(filter name)
537
538 """
539 path = os.path.join(PATH_DEBUGFS_TRACING, 'events', 'kvm')
540 fields = self.walkdir(path)[1]
541 extra = []
542 for field in fields:
543 if field in self.filters:
544 filter_name_, filter_dicts = self.filters[field]
545 for name in filter_dicts:
546 extra.append(field + '(' + name + ')')
547 fields += extra
548 return fields
549
550 def update_fields(self, fields_filter):
551 """Refresh fields, applying fields_filter"""
552 self.fields = [field for field in self.get_available_fields()
553 if self.is_field_wanted(fields_filter, field)]
554
555 @staticmethod
556 def get_online_cpus():
557 """Returns a list of cpu id integers."""
558 def parse_int_list(list_string):
559 """Returns an int list from a string of comma separated integers and
560 integer ranges."""
561 integers = []
562 members = list_string.split(',')
563
564 for member in members:
565 if '-' not in member:
566 integers.append(int(member))
567 else:
568 int_range = member.split('-')
569 integers.extend(range(int(int_range[0]),
570 int(int_range[1]) + 1))
571
572 return integers
573
574 with open('/sys/devices/system/cpu/online') as cpu_list:
575 cpu_string = cpu_list.readline()
576 return parse_int_list(cpu_string)
577
578 def setup_traces(self):
579 """Creates all event and group objects needed to be able to retrieve
580 data."""
581 fields = self.get_available_fields()
582 if self._pid > 0:
583 # Fetch list of all threads of the monitored pid, as qemu
584 # starts a thread for each vcpu.
585 path = os.path.join('/proc', str(self._pid), 'task')
586 groupids = self.walkdir(path)[1]
587 else:
588 groupids = self.get_online_cpus()
589
590 # The constant is needed as a buffer for python libs, std
591 # streams and other files that the script opens.
592 newlim = len(groupids) * len(fields) + 50
593 try:
594 softlim_, hardlim = resource.getrlimit(resource.RLIMIT_NOFILE)
595
596 if hardlim < newlim:
597 # Now we need CAP_SYS_RESOURCE, to increase the hard limit.
598 resource.setrlimit(resource.RLIMIT_NOFILE, (newlim, newlim))
599 else:
600 # Raising the soft limit is sufficient.
601 resource.setrlimit(resource.RLIMIT_NOFILE, (newlim, hardlim))
602
603 except ValueError:
604 sys.exit("NOFILE rlimit could not be raised to {0}".format(newlim))
605
606 for groupid in groupids:
607 group = Group()
608 for name in fields:
609 tracepoint = name
610 tracefilter = None
611 match = re.match(r'(.*)\((.*)\)', name)
612 if match:
613 tracepoint, sub = match.groups()
614 tracefilter = ('%s==%d\0' %
615 (self.filters[tracepoint][0],
616 self.filters[tracepoint][1][sub]))
617
618 # From perf_event_open(2):
619 # pid > 0 and cpu == -1
620 # This measures the specified process/thread on any CPU.
621 #
622 # pid == -1 and cpu >= 0
623 # This measures all processes/threads on the specified CPU.
624 trace_cpu = groupid if self._pid == 0 else -1
625 trace_pid = int(groupid) if self._pid != 0 else -1
626
627 group.add_event(Event(name=name,
628 group=group,
629 trace_cpu=trace_cpu,
630 trace_pid=trace_pid,
631 trace_point=tracepoint,
632 trace_filter=tracefilter))
633
634 self.group_leaders.append(group)
635
636 @property
637 def fields(self):
638 return self._fields
639
640 @fields.setter
641 def fields(self, fields):
642 """Enables/disables the (un)wanted events"""
643 self._fields = fields
644 for group in self.group_leaders:
645 for index, event in enumerate(group.events):
646 if event.name in fields:
647 event.reset()
648 event.enable()
649 else:
650 # Do not disable the group leader.
651 # It would disable all of its events.
652 if index != 0:
653 event.disable()
654
655 @property
656 def pid(self):
657 return self._pid
658
659 @pid.setter
660 def pid(self, pid):
661 """Changes the monitored pid by setting new traces."""
662 self._pid = pid
663 # The garbage collector will get rid of all Event/Group
664 # objects and open files after removing the references.
665 self.group_leaders = []
666 self.setup_traces()
667 self.fields = self._fields
668
669 def read(self, by_guest=0):
670 """Returns 'event name: current value' for all enabled events."""
671 ret = defaultdict(int)
672 for group in self.group_leaders:
673 for name, val in group.read().items():
674 if name in self._fields:
675 ret[name] += val
676 return ret
677
678 def reset(self):
679 """Reset all field counters"""
680 for group in self.group_leaders:
681 for event in group.events:
682 event.reset()
683
684
685 class DebugfsProvider(Provider):
686 """Provides data from the files that KVM creates in the kvm debugfs
687 folder."""
688 def __init__(self, pid, fields_filter, include_past):
689 self.update_fields(fields_filter)
690 self._baseline = {}
691 self.do_read = True
692 self.paths = []
693 self.pid = pid
694 if include_past:
695 self.restore()
696
697 def get_available_fields(self):
698 """"Returns a list of available fields.
699
700 The fields are all available KVM debugfs files
701
702 """
703 return self.walkdir(PATH_DEBUGFS_KVM)[2]
704
705 def update_fields(self, fields_filter):
706 """Refresh fields, applying fields_filter"""
707 self._fields = [field for field in self.get_available_fields()
708 if self.is_field_wanted(fields_filter, field)]
709
710 @property
711 def fields(self):
712 return self._fields
713
714 @fields.setter
715 def fields(self, fields):
716 self._fields = fields
717 self.reset()
718
719 @property
720 def pid(self):
721 return self._pid
722
723 @pid.setter
724 def pid(self, pid):
725 self._pid = pid
726 if pid != 0:
727 vms = self.walkdir(PATH_DEBUGFS_KVM)[1]
728 if len(vms) == 0:
729 self.do_read = False
730
731 self.paths = filter(lambda x: "{}-".format(pid) in x, vms)
732
733 else:
734 self.paths = []
735 self.do_read = True
736 self.reset()
737
738 def read(self, reset=0, by_guest=0):
739 """Returns a dict with format:'file name / field -> current value'.
740
741 Parameter 'reset':
742 0 plain read
743 1 reset field counts to 0
744 2 restore the original field counts
745
746 """
747 results = {}
748
749 # If no debugfs filtering support is available, then don't read.
750 if not self.do_read:
751 return results
752
753 paths = self.paths
754 if self._pid == 0:
755 paths = []
756 for entry in os.walk(PATH_DEBUGFS_KVM):
757 for dir in entry[1]:
758 paths.append(dir)
759 for path in paths:
760 for field in self._fields:
761 value = self.read_field(field, path)
762 key = path + field
763 if reset == 1:
764 self._baseline[key] = value
765 if reset == 2:
766 self._baseline[key] = 0
767 if self._baseline.get(key, -1) == -1:
768 self._baseline[key] = value
769 increment = (results.get(field, 0) + value -
770 self._baseline.get(key, 0))
771 if by_guest:
772 pid = key.split('-')[0]
773 if pid in results:
774 results[pid] += increment
775 else:
776 results[pid] = increment
777 else:
778 results[field] = increment
779
780 return results
781
782 def read_field(self, field, path):
783 """Returns the value of a single field from a specific VM."""
784 try:
785 return int(open(os.path.join(PATH_DEBUGFS_KVM,
786 path,
787 field))
788 .read())
789 except IOError:
790 return 0
791
792 def reset(self):
793 """Reset field counters"""
794 self._baseline = {}
795 self.read(1)
796
797 def restore(self):
798 """Reset field counters"""
799 self._baseline = {}
800 self.read(2)
801
802
803 class Stats(object):
804 """Manages the data providers and the data they provide.
805
806 It is used to set filters on the provider's data and collect all
807 provider data.
808
809 """
810 def __init__(self, options):
811 self.providers = self.get_providers(options)
812 self._pid_filter = options.pid
813 self._fields_filter = options.fields
814 self.values = {}
815
816 @staticmethod
817 def get_providers(options):
818 """Returns a list of data providers depending on the passed options."""
819 providers = []
820
821 if options.debugfs:
822 providers.append(DebugfsProvider(options.pid, options.fields,
823 options.dbgfs_include_past))
824 if options.tracepoints or not providers:
825 providers.append(TracepointProvider(options.pid, options.fields))
826
827 return providers
828
829 def update_provider_filters(self):
830 """Propagates fields filters to providers."""
831 # As we reset the counters when updating the fields we can
832 # also clear the cache of old values.
833 self.values = {}
834 for provider in self.providers:
835 provider.update_fields(self._fields_filter)
836
837 def reset(self):
838 self.values = {}
839 for provider in self.providers:
840 provider.reset()
841
842 @property
843 def fields_filter(self):
844 return self._fields_filter
845
846 @fields_filter.setter
847 def fields_filter(self, fields_filter):
848 if fields_filter != self._fields_filter:
849 self._fields_filter = fields_filter
850 self.update_provider_filters()
851
852 @property
853 def pid_filter(self):
854 return self._pid_filter
855
856 @pid_filter.setter
857 def pid_filter(self, pid):
858 if pid != self._pid_filter:
859 self._pid_filter = pid
860 self.values = {}
861 for provider in self.providers:
862 provider.pid = self._pid_filter
863
864 def get(self, by_guest=0):
865 """Returns a dict with field -> (value, delta to last value) of all
866 provider data."""
867 for provider in self.providers:
868 new = provider.read(by_guest=by_guest)
869 for key in new if by_guest else provider.fields:
870 oldval = self.values.get(key, (0, 0))[0]
871 newval = new.get(key, 0)
872 newdelta = newval - oldval
873 self.values[key] = (newval, newdelta)
874 return self.values
875
876 def toggle_display_guests(self, to_pid):
877 """Toggle between collection of stats by individual event and by
878 guest pid
879
880 Events reported by DebugfsProvider change when switching to/from
881 reading by guest values. Hence we have to remove the excess event
882 names from self.values.
883
884 """
885 if any(isinstance(ins, TracepointProvider) for ins in self.providers):
886 return 1
887 if to_pid:
888 for provider in self.providers:
889 if isinstance(provider, DebugfsProvider):
890 for key in provider.fields:
891 if key in self.values.keys():
892 del self.values[key]
893 else:
894 oldvals = self.values.copy()
895 for key in oldvals:
896 if key.isdigit():
897 del self.values[key]
898 # Update oldval (see get())
899 self.get(to_pid)
900 return 0
901
902 DELAY_DEFAULT = 3.0
903 MAX_GUEST_NAME_LEN = 48
904 MAX_REGEX_LEN = 44
905 DEFAULT_REGEX = r'^[^\(]*$'
906 SORT_DEFAULT = 0
907
908
909 class Tui(object):
910 """Instruments curses to draw a nice text ui."""
911 def __init__(self, stats):
912 self.stats = stats
913 self.screen = None
914 self._delay_initial = 0.25
915 self._delay_regular = DELAY_DEFAULT
916 self._sorting = SORT_DEFAULT
917 self._display_guests = 0
918
919 def __enter__(self):
920 """Initialises curses for later use. Based on curses.wrapper
921 implementation from the Python standard library."""
922 self.screen = curses.initscr()
923 curses.noecho()
924 curses.cbreak()
925
926 # The try/catch works around a minor bit of
927 # over-conscientiousness in the curses module, the error
928 # return from C start_color() is ignorable.
929 try:
930 curses.start_color()
931 except curses.error:
932 pass
933
934 # Hide cursor in extra statement as some monochrome terminals
935 # might support hiding but not colors.
936 try:
937 curses.curs_set(0)
938 except curses.error:
939 pass
940
941 curses.use_default_colors()
942 return self
943
944 def __exit__(self, *exception):
945 """Resets the terminal to its normal state. Based on curses.wrapper
946 implementation from the Python standard library."""
947 if self.screen:
948 self.screen.keypad(0)
949 curses.echo()
950 curses.nocbreak()
951 curses.endwin()
952
953 @staticmethod
954 def get_all_gnames():
955 """Returns a list of (pid, gname) tuples of all running guests"""
956 res = []
957 try:
958 child = subprocess.Popen(['ps', '-A', '--format', 'pid,args'],
959 stdout=subprocess.PIPE)
960 except:
961 raise Exception
962 for line in child.stdout:
963 line = line.decode(ENCODING).lstrip().split(' ', 1)
964 # perform a sanity check before calling the more expensive
965 # function to possibly extract the guest name
966 if ' -name ' in line[1]:
967 res.append((line[0], Tui.get_gname_from_pid(line[0])))
968 child.stdout.close()
969
970 return res
971
972 def print_all_gnames(self, row):
973 """Print a list of all running guests along with their pids."""
974 self.screen.addstr(row, 2, '%8s %-60s' %
975 ('Pid', 'Guest Name (fuzzy list, might be '
976 'inaccurate!)'),
977 curses.A_UNDERLINE)
978 row += 1
979 try:
980 for line in self.get_all_gnames():
981 self.screen.addstr(row, 2, '%8s %-60s' % (line[0], line[1]))
982 row += 1
983 if row >= self.screen.getmaxyx()[0]:
984 break
985 except Exception:
986 self.screen.addstr(row + 1, 2, 'Not available')
987
988 @staticmethod
989 def get_pid_from_gname(gname):
990 """Fuzzy function to convert guest name to QEMU process pid.
991
992 Returns a list of potential pids, can be empty if no match found.
993 Throws an exception on processing errors.
994
995 """
996 pids = []
997 for line in Tui.get_all_gnames():
998 if gname == line[1]:
999 pids.append(int(line[0]))
1000
1001 return pids
1002
1003 @staticmethod
1004 def get_gname_from_pid(pid):
1005 """Returns the guest name for a QEMU process pid.
1006
1007 Extracts the guest name from the QEMU comma line by processing the
1008 '-name' option. Will also handle names specified out of sequence.
1009
1010 """
1011 name = ''
1012 try:
1013 line = open('/proc/{}/cmdline'
1014 .format(pid), 'r').read().split('\0')
1015 parms = line[line.index('-name') + 1].split(',')
1016 while '' in parms:
1017 # commas are escaped (i.e. ',,'), hence e.g. 'foo,bar' results
1018 # in # ['foo', '', 'bar'], which we revert here
1019 idx = parms.index('')
1020 parms[idx - 1] += ',' + parms[idx + 1]
1021 del parms[idx:idx+2]
1022 # the '-name' switch allows for two ways to specify the guest name,
1023 # where the plain name overrides the name specified via 'guest='
1024 for arg in parms:
1025 if '=' not in arg:
1026 name = arg
1027 break
1028 if arg[:6] == 'guest=':
1029 name = arg[6:]
1030 except (ValueError, IOError, IndexError):
1031 pass
1032
1033 return name
1034
1035 def update_drilldown(self):
1036 """Sets or removes a filter that only allows fields without braces."""
1037 if not self.stats.fields_filter:
1038 self.stats.fields_filter = DEFAULT_REGEX
1039
1040 elif self.stats.fields_filter == DEFAULT_REGEX:
1041 self.stats.fields_filter = None
1042
1043 def update_pid(self, pid):
1044 """Propagates pid selection to stats object."""
1045 self.stats.pid_filter = pid
1046
1047 def refresh_header(self, pid=None):
1048 """Refreshes the header."""
1049 if pid is None:
1050 pid = self.stats.pid_filter
1051 self.screen.erase()
1052 gname = self.get_gname_from_pid(pid)
1053 if gname:
1054 gname = ('({})'.format(gname[:MAX_GUEST_NAME_LEN] + '...'
1055 if len(gname) > MAX_GUEST_NAME_LEN
1056 else gname))
1057 if pid > 0:
1058 self.screen.addstr(0, 0, 'kvm statistics - pid {0} {1}'
1059 .format(pid, gname), curses.A_BOLD)
1060 else:
1061 self.screen.addstr(0, 0, 'kvm statistics - summary', curses.A_BOLD)
1062 if self.stats.fields_filter and self.stats.fields_filter \
1063 != DEFAULT_REGEX:
1064 regex = self.stats.fields_filter
1065 if len(regex) > MAX_REGEX_LEN:
1066 regex = regex[:MAX_REGEX_LEN] + '...'
1067 self.screen.addstr(1, 17, 'regex filter: {0}'.format(regex))
1068 if self._display_guests:
1069 col_name = 'Guest Name'
1070 else:
1071 col_name = 'Event'
1072 self.screen.addstr(2, 1, '%-40s %10s%7s %8s' %
1073 (col_name, 'Total', '%Total', 'CurAvg/s'),
1074 curses.A_STANDOUT)
1075 self.screen.addstr(4, 1, 'Collecting data...')
1076 self.screen.refresh()
1077
1078 def refresh_body(self, sleeptime):
1079 row = 3
1080 self.screen.move(row, 0)
1081 self.screen.clrtobot()
1082 stats = self.stats.get(self._display_guests)
1083 total = 0.
1084 for key in stats.keys():
1085 if key.find('(') is -1:
1086 total += stats[key][0]
1087 if self._sorting == SORT_DEFAULT:
1088 def sortkey((_k, v)):
1089 # sort by (delta value, overall value)
1090 return (v[1], v[0])
1091 else:
1092 def sortkey((_k, v)):
1093 # sort by overall value
1094 return v[0]
1095
1096 tavg = 0
1097 for key, values in sorted(stats.items(), key=sortkey, reverse=True):
1098 if row >= self.screen.getmaxyx()[0] - 1:
1099 break
1100 if not values[0] and not values[1]:
1101 break
1102 if values[0] is not None:
1103 cur = int(round(values[1] / sleeptime)) if values[1] else ''
1104 if self._display_guests:
1105 key = self.get_gname_from_pid(key)
1106 self.screen.addstr(row, 1, '%-40s %10d%7.1f %8s' %
1107 (key, values[0], values[0] * 100 / total,
1108 cur))
1109 if cur is not '' and key.find('(') is -1:
1110 tavg += cur
1111 row += 1
1112 if row == 3:
1113 self.screen.addstr(4, 1, 'No matching events reported yet')
1114 else:
1115 self.screen.addstr(row, 1, '%-40s %10d %8s' %
1116 ('Total', total, tavg if tavg else ''),
1117 curses.A_BOLD)
1118 self.screen.refresh()
1119
1120 def show_msg(self, text):
1121 """Display message centered text and exit on key press"""
1122 hint = 'Press any key to continue'
1123 curses.cbreak()
1124 self.screen.erase()
1125 (x, term_width) = self.screen.getmaxyx()
1126 row = 2
1127 for line in text:
1128 start = (term_width - len(line)) / 2
1129 self.screen.addstr(row, start, line)
1130 row += 1
1131 self.screen.addstr(row + 1, (term_width - len(hint)) / 2, hint,
1132 curses.A_STANDOUT)
1133 self.screen.getkey()
1134
1135 def show_help_interactive(self):
1136 """Display help with list of interactive commands"""
1137 msg = (' b toggle events by guests (debugfs only, honors'
1138 ' filters)',
1139 ' c clear filter',
1140 ' f filter by regular expression',
1141 ' g filter by guest name',
1142 ' h display interactive commands reference',
1143 ' o toggle sorting order (Total vs CurAvg/s)',
1144 ' p filter by PID',
1145 ' q quit',
1146 ' r reset stats',
1147 ' s set update interval',
1148 ' x toggle reporting of stats for individual child trace'
1149 ' events',
1150 'Any other key refreshes statistics immediately')
1151 curses.cbreak()
1152 self.screen.erase()
1153 self.screen.addstr(0, 0, "Interactive commands reference",
1154 curses.A_BOLD)
1155 self.screen.addstr(2, 0, "Press any key to exit", curses.A_STANDOUT)
1156 row = 4
1157 for line in msg:
1158 self.screen.addstr(row, 0, line)
1159 row += 1
1160 self.screen.getkey()
1161 self.refresh_header()
1162
1163 def show_filter_selection(self):
1164 """Draws filter selection mask.
1165
1166 Asks for a valid regex and sets the fields filter accordingly.
1167
1168 """
1169 while True:
1170 self.screen.erase()
1171 self.screen.addstr(0, 0,
1172 "Show statistics for events matching a regex.",
1173 curses.A_BOLD)
1174 self.screen.addstr(2, 0,
1175 "Current regex: {0}"
1176 .format(self.stats.fields_filter))
1177 self.screen.addstr(3, 0, "New regex: ")
1178 curses.echo()
1179 regex = self.screen.getstr().decode(ENCODING)
1180 curses.noecho()
1181 if len(regex) == 0:
1182 self.stats.fields_filter = DEFAULT_REGEX
1183 self.refresh_header()
1184 return
1185 try:
1186 re.compile(regex)
1187 self.stats.fields_filter = regex
1188 self.refresh_header()
1189 return
1190 except re.error:
1191 continue
1192
1193 def show_vm_selection_by_pid(self):
1194 """Draws PID selection mask.
1195
1196 Asks for a pid until a valid pid or 0 has been entered.
1197
1198 """
1199 msg = ''
1200 while True:
1201 self.screen.erase()
1202 self.screen.addstr(0, 0,
1203 'Show statistics for specific pid.',
1204 curses.A_BOLD)
1205 self.screen.addstr(1, 0,
1206 'This might limit the shown data to the trace '
1207 'statistics.')
1208 self.screen.addstr(5, 0, msg)
1209 self.print_all_gnames(7)
1210
1211 curses.echo()
1212 self.screen.addstr(3, 0, "Pid [0 or pid]: ")
1213 pid = self.screen.getstr().decode(ENCODING)
1214 curses.noecho()
1215
1216 try:
1217 if len(pid) > 0:
1218 pid = int(pid)
1219 if pid != 0 and not os.path.isdir(os.path.join('/proc/',
1220 str(pid))):
1221 msg = '"' + str(pid) + '": Not a running process'
1222 continue
1223 else:
1224 pid = 0
1225 self.refresh_header(pid)
1226 self.update_pid(pid)
1227 break
1228 except ValueError:
1229 msg = '"' + str(pid) + '": Not a valid pid'
1230
1231 def show_set_update_interval(self):
1232 """Draws update interval selection mask."""
1233 msg = ''
1234 while True:
1235 self.screen.erase()
1236 self.screen.addstr(0, 0, 'Set update interval (defaults to %fs).' %
1237 DELAY_DEFAULT, curses.A_BOLD)
1238 self.screen.addstr(4, 0, msg)
1239 self.screen.addstr(2, 0, 'Change delay from %.1fs to ' %
1240 self._delay_regular)
1241 curses.echo()
1242 val = self.screen.getstr().decode(ENCODING)
1243 curses.noecho()
1244
1245 try:
1246 if len(val) > 0:
1247 delay = float(val)
1248 if delay < 0.1:
1249 msg = '"' + str(val) + '": Value must be >=0.1'
1250 continue
1251 if delay > 25.5:
1252 msg = '"' + str(val) + '": Value must be <=25.5'
1253 continue
1254 else:
1255 delay = DELAY_DEFAULT
1256 self._delay_regular = delay
1257 break
1258
1259 except ValueError:
1260 msg = '"' + str(val) + '": Invalid value'
1261 self.refresh_header()
1262
1263 def show_vm_selection_by_guest_name(self):
1264 """Draws guest selection mask.
1265
1266 Asks for a guest name until a valid guest name or '' is entered.
1267
1268 """
1269 msg = ''
1270 while True:
1271 self.screen.erase()
1272 self.screen.addstr(0, 0,
1273 'Show statistics for specific guest.',
1274 curses.A_BOLD)
1275 self.screen.addstr(1, 0,
1276 'This might limit the shown data to the trace '
1277 'statistics.')
1278 self.screen.addstr(5, 0, msg)
1279 self.print_all_gnames(7)
1280 curses.echo()
1281 self.screen.addstr(3, 0, "Guest [ENTER or guest]: ")
1282 gname = self.screen.getstr().decode(ENCODING)
1283 curses.noecho()
1284
1285 if not gname:
1286 self.refresh_header(0)
1287 self.update_pid(0)
1288 break
1289 else:
1290 pids = []
1291 try:
1292 pids = self.get_pid_from_gname(gname)
1293 except:
1294 msg = '"' + gname + '": Internal error while searching, ' \
1295 'use pid filter instead'
1296 continue
1297 if len(pids) == 0:
1298 msg = '"' + gname + '": Not an active guest'
1299 continue
1300 if len(pids) > 1:
1301 msg = '"' + gname + '": Multiple matches found, use pid ' \
1302 'filter instead'
1303 continue
1304 self.refresh_header(pids[0])
1305 self.update_pid(pids[0])
1306 break
1307
1308 def show_stats(self):
1309 """Refreshes the screen and processes user input."""
1310 sleeptime = self._delay_initial
1311 self.refresh_header()
1312 start = 0.0 # result based on init value never appears on screen
1313 while True:
1314 self.refresh_body(time.time() - start)
1315 curses.halfdelay(int(sleeptime * 10))
1316 start = time.time()
1317 sleeptime = self._delay_regular
1318 try:
1319 char = self.screen.getkey()
1320 if char == 'b':
1321 self._display_guests = not self._display_guests
1322 if self.stats.toggle_display_guests(self._display_guests):
1323 self.show_msg(['Command not available with tracepoints'
1324 ' enabled', 'Restart with debugfs only '
1325 '(see option \'-d\') and try again!'])
1326 self._display_guests = not self._display_guests
1327 self.refresh_header()
1328 if char == 'c':
1329 self.stats.fields_filter = DEFAULT_REGEX
1330 self.refresh_header(0)
1331 self.update_pid(0)
1332 if char == 'f':
1333 curses.curs_set(1)
1334 self.show_filter_selection()
1335 curses.curs_set(0)
1336 sleeptime = self._delay_initial
1337 if char == 'g':
1338 curses.curs_set(1)
1339 self.show_vm_selection_by_guest_name()
1340 curses.curs_set(0)
1341 sleeptime = self._delay_initial
1342 if char == 'h':
1343 self.show_help_interactive()
1344 if char == 'o':
1345 self._sorting = not self._sorting
1346 if char == 'p':
1347 curses.curs_set(1)
1348 self.show_vm_selection_by_pid()
1349 curses.curs_set(0)
1350 sleeptime = self._delay_initial
1351 if char == 'q':
1352 break
1353 if char == 'r':
1354 self.stats.reset()
1355 if char == 's':
1356 curses.curs_set(1)
1357 self.show_set_update_interval()
1358 curses.curs_set(0)
1359 sleeptime = self._delay_initial
1360 if char == 'x':
1361 self.update_drilldown()
1362 # prevents display of current values on next refresh
1363 self.stats.get(self._display_guests)
1364 except KeyboardInterrupt:
1365 break
1366 except curses.error:
1367 continue
1368
1369
1370 def batch(stats):
1371 """Prints statistics in a key, value format."""
1372 try:
1373 s = stats.get()
1374 time.sleep(1)
1375 s = stats.get()
1376 for key in sorted(s.keys()):
1377 values = s[key]
1378 print('%-42s%10d%10d' % (key, values[0], values[1]))
1379 except KeyboardInterrupt:
1380 pass
1381
1382
1383 def log(stats):
1384 """Prints statistics as reiterating key block, multiple value blocks."""
1385 keys = sorted(stats.get().keys())
1386
1387 def banner():
1388 for k in keys:
1389 print(k, end=' ')
1390 print()
1391
1392 def statline():
1393 s = stats.get()
1394 for k in keys:
1395 print(' %9d' % s[k][1], end=' ')
1396 print()
1397 line = 0
1398 banner_repeat = 20
1399 while True:
1400 try:
1401 time.sleep(1)
1402 if line % banner_repeat == 0:
1403 banner()
1404 statline()
1405 line += 1
1406 except KeyboardInterrupt:
1407 break
1408
1409
1410 def get_options():
1411 """Returns processed program arguments."""
1412 description_text = """
1413 This script displays various statistics about VMs running under KVM.
1414 The statistics are gathered from the KVM debugfs entries and / or the
1415 currently available perf traces.
1416
1417 The monitoring takes additional cpu cycles and might affect the VM's
1418 performance.
1419
1420 Requirements:
1421 - Access to:
1422 %s
1423 %s/events/*
1424 /proc/pid/task
1425 - /proc/sys/kernel/perf_event_paranoid < 1 if user has no
1426 CAP_SYS_ADMIN and perf events are used.
1427 - CAP_SYS_RESOURCE if the hard limit is not high enough to allow
1428 the large number of files that are possibly opened.
1429
1430 Interactive Commands:
1431 b toggle events by guests (debugfs only, honors filters)
1432 c clear filter
1433 f filter by regular expression
1434 g filter by guest name
1435 h display interactive commands reference
1436 o toggle sorting order (Total vs CurAvg/s)
1437 p filter by PID
1438 q quit
1439 r reset stats
1440 s set update interval
1441 x toggle reporting of stats for individual child trace events
1442 Press any other key to refresh statistics immediately.
1443 """ % (PATH_DEBUGFS_KVM, PATH_DEBUGFS_TRACING)
1444
1445 class PlainHelpFormatter(optparse.IndentedHelpFormatter):
1446 def format_description(self, description):
1447 if description:
1448 return description + "\n"
1449 else:
1450 return ""
1451
1452 def cb_guest_to_pid(option, opt, val, parser):
1453 try:
1454 pids = Tui.get_pid_from_gname(val)
1455 except:
1456 sys.exit('Error while searching for guest "{}". Use "-p" to '
1457 'specify a pid instead?'.format(val))
1458 if len(pids) == 0:
1459 sys.exit('Error: No guest by the name "{}" found'.format(val))
1460 if len(pids) > 1:
1461 sys.exit('Error: Multiple processes found (pids: {}). Use "-p" '
1462 'to specify the desired pid'.format(" ".join(pids)))
1463 parser.values.pid = pids[0]
1464
1465 optparser = optparse.OptionParser(description=description_text,
1466 formatter=PlainHelpFormatter())
1467 optparser.add_option('-1', '--once', '--batch',
1468 action='store_true',
1469 default=False,
1470 dest='once',
1471 help='run in batch mode for one second',
1472 )
1473 optparser.add_option('-i', '--debugfs-include-past',
1474 action='store_true',
1475 default=False,
1476 dest='dbgfs_include_past',
1477 help='include all available data on past events for '
1478 'debugfs',
1479 )
1480 optparser.add_option('-l', '--log',
1481 action='store_true',
1482 default=False,
1483 dest='log',
1484 help='run in logging mode (like vmstat)',
1485 )
1486 optparser.add_option('-t', '--tracepoints',
1487 action='store_true',
1488 default=False,
1489 dest='tracepoints',
1490 help='retrieve statistics from tracepoints',
1491 )
1492 optparser.add_option('-d', '--debugfs',
1493 action='store_true',
1494 default=False,
1495 dest='debugfs',
1496 help='retrieve statistics from debugfs',
1497 )
1498 optparser.add_option('-f', '--fields',
1499 action='store',
1500 default=DEFAULT_REGEX,
1501 dest='fields',
1502 help='''fields to display (regex)
1503 "-f help" for a list of available events''',
1504 )
1505 optparser.add_option('-p', '--pid',
1506 action='store',
1507 default=0,
1508 type='int',
1509 dest='pid',
1510 help='restrict statistics to pid',
1511 )
1512 optparser.add_option('-g', '--guest',
1513 action='callback',
1514 type='string',
1515 dest='pid',
1516 metavar='GUEST',
1517 help='restrict statistics to guest by name',
1518 callback=cb_guest_to_pid,
1519 )
1520 options, unkn = optparser.parse_args(sys.argv)
1521 if len(unkn) != 1:
1522 sys.exit('Error: Extra argument(s): ' + ' '.join(unkn[1:]))
1523 try:
1524 # verify that we were passed a valid regex up front
1525 re.compile(options.fields)
1526 except re.error:
1527 sys.exit('Error: "' + options.fields + '" is not a valid regular '
1528 'expression')
1529
1530 return options
1531
1532
1533 def check_access(options):
1534 """Exits if the current user can't access all needed directories."""
1535 if not os.path.exists('/sys/kernel/debug'):
1536 sys.stderr.write('Please enable CONFIG_DEBUG_FS in your kernel.')
1537 sys.exit(1)
1538
1539 if not os.path.exists(PATH_DEBUGFS_KVM):
1540 sys.stderr.write("Please make sure, that debugfs is mounted and "
1541 "readable by the current user:\n"
1542 "('mount -t debugfs debugfs /sys/kernel/debug')\n"
1543 "Also ensure, that the kvm modules are loaded.\n")
1544 sys.exit(1)
1545
1546 if not os.path.exists(PATH_DEBUGFS_TRACING) and (options.tracepoints or
1547 not options.debugfs):
1548 sys.stderr.write("Please enable CONFIG_TRACING in your kernel "
1549 "when using the option -t (default).\n"
1550 "If it is enabled, make {0} readable by the "
1551 "current user.\n"
1552 .format(PATH_DEBUGFS_TRACING))
1553 if options.tracepoints:
1554 sys.exit(1)
1555
1556 sys.stderr.write("Falling back to debugfs statistics!\n")
1557 options.debugfs = True
1558 time.sleep(5)
1559
1560 return options
1561
1562
1563 def main():
1564 options = get_options()
1565 options = check_access(options)
1566
1567 if (options.pid > 0 and
1568 not os.path.isdir(os.path.join('/proc/',
1569 str(options.pid)))):
1570 sys.stderr.write('Did you use a (unsupported) tid instead of a pid?\n')
1571 sys.exit('Specified pid does not exist.')
1572
1573 stats = Stats(options)
1574
1575 if options.fields == 'help':
1576 stats.fields_filter = None
1577 event_list = []
1578 for key in stats.get().keys():
1579 event_list.append(key.split('(', 1)[0])
1580 sys.stdout.write(' ' + '\n '.join(sorted(set(event_list))) + '\n')
1581 sys.exit(0)
1582
1583 if options.log:
1584 log(stats)
1585 elif not options.once:
1586 with Tui(stats) as tui:
1587 tui.show_stats()
1588 else:
1589 batch(stats)
1590
1591 if __name__ == "__main__":
1592 main()