1# Core.py - Python extension for perf script, core functions 2# 3# Copyright (C) 2010 by Tom Zanussi <tzanussi@gmail.com> 4# 5# This software may be distributed under the terms of the GNU General 6# Public License ("GPL") version 2 as published by the Free Software 7# Foundation. 8 9from collections import defaultdict 10 11def autodict(): 12 return defaultdict(autodict) 13 14flag_fields = autodict() 15symbolic_fields = autodict() 16 17def define_flag_field(event_name, field_name, delim): 18 flag_fields[event_name][field_name]['delim'] = delim 19 20def define_flag_value(event_name, field_name, value, field_str): 21 flag_fields[event_name][field_name]['values'][value] = field_str 22 23def define_symbolic_field(event_name, field_name): 24 # nothing to do, really 25 pass 26 27def define_symbolic_value(event_name, field_name, value, field_str): 28 symbolic_fields[event_name][field_name]['values'][value] = field_str 29 30def flag_str(event_name, field_name, value): 31 string = "" 32 33 if flag_fields[event_name][field_name]: 34 print_delim = 0 35 keys = flag_fields[event_name][field_name]['values'].keys() 36 keys.sort() 37 for idx in keys: 38 if not value and not idx: 39 string += flag_fields[event_name][field_name]['values'][idx] 40 break 41 if idx and (value & idx) == idx: 42 if print_delim and flag_fields[event_name][field_name]['delim']: 43 string += " " + flag_fields[event_name][field_name]['delim'] + " " 44 string += flag_fields[event_name][field_name]['values'][idx] 45 print_delim = 1 46 value &= ~idx 47 48 return string 49 50def symbol_str(event_name, field_name, value): 51 string = "" 52 53 if symbolic_fields[event_name][field_name]: 54 keys = symbolic_fields[event_name][field_name]['values'].keys() 55 keys.sort() 56 for idx in keys: 57 if not value and not idx: 58 string = symbolic_fields[event_name][field_name]['values'][idx] 59 break 60 if (value == idx): 61 string = symbolic_fields[event_name][field_name]['values'][idx] 62 break 63 64 return string 65 66trace_flags = { 0x00: "NONE", \ 67 0x01: "IRQS_OFF", \ 68 0x02: "IRQS_NOSUPPORT", \ 69 0x04: "NEED_RESCHED", \ 70 0x08: "HARDIRQ", \ 71 0x10: "SOFTIRQ" } 72 73def trace_flag_str(value): 74 string = "" 75 print_delim = 0 76 77 keys = trace_flags.keys() 78 79 for idx in keys: 80 if not value and not idx: 81 string += "NONE" 82 break 83 84 if idx and (value & idx) == idx: 85 if print_delim: 86 string += " | "; 87 string += trace_flags[idx] 88 print_delim = 1 89 value &= ~idx 90 91 return string 92 93 94def taskState(state): 95 states = { 96 0 : "R", 97 1 : "S", 98 2 : "D", 99 64: "DEAD" 100 } 101 102 if state not in states: 103 return "Unknown" 104 105 return states[state] 106 107 108class EventHeaders: 109 def __init__(self, common_cpu, common_secs, common_nsecs, 110 common_pid, common_comm, common_callchain): 111 self.cpu = common_cpu 112 self.secs = common_secs 113 self.nsecs = common_nsecs 114 self.pid = common_pid 115 self.comm = common_comm 116 self.callchain = common_callchain 117 118 def ts(self): 119 return (self.secs * (10 ** 9)) + self.nsecs 120 121 def ts_format(self): 122 return "%d.%d" % (self.secs, int(self.nsecs / 1000)) 123