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        for idx in sorted(flag_fields[event_name][field_name]['values']):
36            if not value and not idx:
37                string += flag_fields[event_name][field_name]['values'][idx]
38                break
39            if idx and (value & idx) == idx:
40                if print_delim and flag_fields[event_name][field_name]['delim']:
41                    string += " " + flag_fields[event_name][field_name]['delim'] + " "
42                string += flag_fields[event_name][field_name]['values'][idx]
43                print_delim = 1
44                value &= ~idx
45
46    return string
47
48def symbol_str(event_name, field_name, value):
49    string = ""
50
51    if symbolic_fields[event_name][field_name]:
52        for idx in sorted(symbolic_fields[event_name][field_name]['values']):
53            if not value and not idx:
54                string = symbolic_fields[event_name][field_name]['values'][idx]
55                break
56            if (value == idx):
57                string = symbolic_fields[event_name][field_name]['values'][idx]
58                break
59
60    return string
61
62trace_flags = { 0x00: "NONE", \
63                    0x01: "IRQS_OFF", \
64                    0x02: "IRQS_NOSUPPORT", \
65                    0x04: "NEED_RESCHED", \
66                    0x08: "HARDIRQ", \
67                    0x10: "SOFTIRQ" }
68
69def trace_flag_str(value):
70    string = ""
71    print_delim = 0
72
73    for idx in trace_flags:
74        if not value and not idx:
75            string += "NONE"
76            break
77
78        if idx and (value & idx) == idx:
79            if print_delim:
80                string += " | ";
81            string += trace_flags[idx]
82            print_delim = 1
83            value &= ~idx
84
85    return string
86
87
88def taskState(state):
89	states = {
90		0 : "R",
91		1 : "S",
92		2 : "D",
93		64: "DEAD"
94	}
95
96	if state not in states:
97		return "Unknown"
98
99	return states[state]
100
101
102class EventHeaders:
103	def __init__(self, common_cpu, common_secs, common_nsecs,
104		     common_pid, common_comm, common_callchain):
105		self.cpu = common_cpu
106		self.secs = common_secs
107		self.nsecs = common_nsecs
108		self.pid = common_pid
109		self.comm = common_comm
110		self.callchain = common_callchain
111
112	def ts(self):
113		return (self.secs * (10 ** 9)) + self.nsecs
114
115	def ts_format(self):
116		return "%d.%d" % (self.secs, int(self.nsecs / 1000))
117