perf/scripts: Add Python scripting engine
[linux-2.6.git] / tools / perf / scripts / python / Perf-Trace-Util / lib / Perf / Trace / Core.py
1 # Core.py - Python extension for perf trace, 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
9 from collections import defaultdict
10
11 def autodict():
12     return defaultdict(autodict)
13
14 flag_fields = autodict()
15 symbolic_fields = autodict()
16
17 def define_flag_field(event_name, field_name, delim):
18     flag_fields[event_name][field_name]['delim'] = delim
19
20 def define_flag_value(event_name, field_name, value, field_str):
21     flag_fields[event_name][field_name]['values'][value] = field_str
22
23 def define_symbolic_field(event_name, field_name):
24     # nothing to do, really
25     pass
26
27 def define_symbolic_value(event_name, field_name, value, field_str):
28     symbolic_fields[event_name][field_name]['values'][value] = field_str
29
30 def 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
50 def 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
66 trace_flags = { 0x00: "NONE", \
67                     0x01: "IRQS_OFF", \
68                     0x02: "IRQS_NOSUPPORT", \
69                     0x04: "NEED_RESCHED", \
70                     0x08: "HARDIRQ", \
71                     0x10: "SOFTIRQ" }
72
73 def 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