diff options
Diffstat (limited to 'tools/perf/scripts/python/Perf-Trace-Util')
4 files changed, 479 insertions, 0 deletions
diff --git a/tools/perf/scripts/python/Perf-Trace-Util/Context.c b/tools/perf/scripts/python/Perf-Trace-Util/Context.c new file mode 100644 index 000000000000..315067b8f552 --- /dev/null +++ b/tools/perf/scripts/python/Perf-Trace-Util/Context.c | |||
@@ -0,0 +1,88 @@ | |||
1 | /* | ||
2 | * Context.c. Python interfaces for perf script. | ||
3 | * | ||
4 | * Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com> | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
19 | * | ||
20 | */ | ||
21 | |||
22 | #include <Python.h> | ||
23 | #include "../../../perf.h" | ||
24 | #include "../../../util/trace-event.h" | ||
25 | |||
26 | PyMODINIT_FUNC initperf_trace_context(void); | ||
27 | |||
28 | static PyObject *perf_trace_context_common_pc(PyObject *self, PyObject *args) | ||
29 | { | ||
30 | static struct scripting_context *scripting_context; | ||
31 | PyObject *context; | ||
32 | int retval; | ||
33 | |||
34 | if (!PyArg_ParseTuple(args, "O", &context)) | ||
35 | return NULL; | ||
36 | |||
37 | scripting_context = PyCObject_AsVoidPtr(context); | ||
38 | retval = common_pc(scripting_context); | ||
39 | |||
40 | return Py_BuildValue("i", retval); | ||
41 | } | ||
42 | |||
43 | static PyObject *perf_trace_context_common_flags(PyObject *self, | ||
44 | PyObject *args) | ||
45 | { | ||
46 | static struct scripting_context *scripting_context; | ||
47 | PyObject *context; | ||
48 | int retval; | ||
49 | |||
50 | if (!PyArg_ParseTuple(args, "O", &context)) | ||
51 | return NULL; | ||
52 | |||
53 | scripting_context = PyCObject_AsVoidPtr(context); | ||
54 | retval = common_flags(scripting_context); | ||
55 | |||
56 | return Py_BuildValue("i", retval); | ||
57 | } | ||
58 | |||
59 | static PyObject *perf_trace_context_common_lock_depth(PyObject *self, | ||
60 | PyObject *args) | ||
61 | { | ||
62 | static struct scripting_context *scripting_context; | ||
63 | PyObject *context; | ||
64 | int retval; | ||
65 | |||
66 | if (!PyArg_ParseTuple(args, "O", &context)) | ||
67 | return NULL; | ||
68 | |||
69 | scripting_context = PyCObject_AsVoidPtr(context); | ||
70 | retval = common_lock_depth(scripting_context); | ||
71 | |||
72 | return Py_BuildValue("i", retval); | ||
73 | } | ||
74 | |||
75 | static PyMethodDef ContextMethods[] = { | ||
76 | { "common_pc", perf_trace_context_common_pc, METH_VARARGS, | ||
77 | "Get the common preempt count event field value."}, | ||
78 | { "common_flags", perf_trace_context_common_flags, METH_VARARGS, | ||
79 | "Get the common flags event field value."}, | ||
80 | { "common_lock_depth", perf_trace_context_common_lock_depth, | ||
81 | METH_VARARGS, "Get the common lock depth event field value."}, | ||
82 | { NULL, NULL, 0, NULL} | ||
83 | }; | ||
84 | |||
85 | PyMODINIT_FUNC initperf_trace_context(void) | ||
86 | { | ||
87 | (void) Py_InitModule("perf_trace_context", ContextMethods); | ||
88 | } | ||
diff --git a/tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Core.py b/tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Core.py new file mode 100644 index 000000000000..de7211e4fa47 --- /dev/null +++ b/tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Core.py | |||
@@ -0,0 +1,121 @@ | |||
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 | |||
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 | ||
92 | |||
93 | |||
94 | def 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 | |||
108 | class EventHeaders: | ||
109 | def __init__(self, common_cpu, common_secs, common_nsecs, | ||
110 | common_pid, common_comm): | ||
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 | |||
117 | def ts(self): | ||
118 | return (self.secs * (10 ** 9)) + self.nsecs | ||
119 | |||
120 | def ts_format(self): | ||
121 | return "%d.%d" % (self.secs, int(self.nsecs / 1000)) | ||
diff --git a/tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/SchedGui.py b/tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/SchedGui.py new file mode 100644 index 000000000000..fdd92f699055 --- /dev/null +++ b/tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/SchedGui.py | |||
@@ -0,0 +1,184 @@ | |||
1 | # SchedGui.py - Python extension for perf script, basic GUI code for | ||
2 | # traces drawing and overview. | ||
3 | # | ||
4 | # Copyright (C) 2010 by Frederic Weisbecker <fweisbec@gmail.com> | ||
5 | # | ||
6 | # This software is distributed under the terms of the GNU General | ||
7 | # Public License ("GPL") version 2 as published by the Free Software | ||
8 | # Foundation. | ||
9 | |||
10 | |||
11 | try: | ||
12 | import wx | ||
13 | except ImportError: | ||
14 | raise ImportError, "You need to install the wxpython lib for this script" | ||
15 | |||
16 | |||
17 | class RootFrame(wx.Frame): | ||
18 | Y_OFFSET = 100 | ||
19 | RECT_HEIGHT = 100 | ||
20 | RECT_SPACE = 50 | ||
21 | EVENT_MARKING_WIDTH = 5 | ||
22 | |||
23 | def __init__(self, sched_tracer, title, parent = None, id = -1): | ||
24 | wx.Frame.__init__(self, parent, id, title) | ||
25 | |||
26 | (self.screen_width, self.screen_height) = wx.GetDisplaySize() | ||
27 | self.screen_width -= 10 | ||
28 | self.screen_height -= 10 | ||
29 | self.zoom = 0.5 | ||
30 | self.scroll_scale = 20 | ||
31 | self.sched_tracer = sched_tracer | ||
32 | self.sched_tracer.set_root_win(self) | ||
33 | (self.ts_start, self.ts_end) = sched_tracer.interval() | ||
34 | self.update_width_virtual() | ||
35 | self.nr_rects = sched_tracer.nr_rectangles() + 1 | ||
36 | self.height_virtual = RootFrame.Y_OFFSET + (self.nr_rects * (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE)) | ||
37 | |||
38 | # whole window panel | ||
39 | self.panel = wx.Panel(self, size=(self.screen_width, self.screen_height)) | ||
40 | |||
41 | # scrollable container | ||
42 | self.scroll = wx.ScrolledWindow(self.panel) | ||
43 | self.scroll.SetScrollbars(self.scroll_scale, self.scroll_scale, self.width_virtual / self.scroll_scale, self.height_virtual / self.scroll_scale) | ||
44 | self.scroll.EnableScrolling(True, True) | ||
45 | self.scroll.SetFocus() | ||
46 | |||
47 | # scrollable drawing area | ||
48 | self.scroll_panel = wx.Panel(self.scroll, size=(self.screen_width - 15, self.screen_height / 2)) | ||
49 | self.scroll_panel.Bind(wx.EVT_PAINT, self.on_paint) | ||
50 | self.scroll_panel.Bind(wx.EVT_KEY_DOWN, self.on_key_press) | ||
51 | self.scroll_panel.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down) | ||
52 | self.scroll.Bind(wx.EVT_PAINT, self.on_paint) | ||
53 | self.scroll.Bind(wx.EVT_KEY_DOWN, self.on_key_press) | ||
54 | self.scroll.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down) | ||
55 | |||
56 | self.scroll.Fit() | ||
57 | self.Fit() | ||
58 | |||
59 | self.scroll_panel.SetDimensions(-1, -1, self.width_virtual, self.height_virtual, wx.SIZE_USE_EXISTING) | ||
60 | |||
61 | self.txt = None | ||
62 | |||
63 | self.Show(True) | ||
64 | |||
65 | def us_to_px(self, val): | ||
66 | return val / (10 ** 3) * self.zoom | ||
67 | |||
68 | def px_to_us(self, val): | ||
69 | return (val / self.zoom) * (10 ** 3) | ||
70 | |||
71 | def scroll_start(self): | ||
72 | (x, y) = self.scroll.GetViewStart() | ||
73 | return (x * self.scroll_scale, y * self.scroll_scale) | ||
74 | |||
75 | def scroll_start_us(self): | ||
76 | (x, y) = self.scroll_start() | ||
77 | return self.px_to_us(x) | ||
78 | |||
79 | def paint_rectangle_zone(self, nr, color, top_color, start, end): | ||
80 | offset_px = self.us_to_px(start - self.ts_start) | ||
81 | width_px = self.us_to_px(end - self.ts_start) | ||
82 | |||
83 | offset_py = RootFrame.Y_OFFSET + (nr * (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE)) | ||
84 | width_py = RootFrame.RECT_HEIGHT | ||
85 | |||
86 | dc = self.dc | ||
87 | |||
88 | if top_color is not None: | ||
89 | (r, g, b) = top_color | ||
90 | top_color = wx.Colour(r, g, b) | ||
91 | brush = wx.Brush(top_color, wx.SOLID) | ||
92 | dc.SetBrush(brush) | ||
93 | dc.DrawRectangle(offset_px, offset_py, width_px, RootFrame.EVENT_MARKING_WIDTH) | ||
94 | width_py -= RootFrame.EVENT_MARKING_WIDTH | ||
95 | offset_py += RootFrame.EVENT_MARKING_WIDTH | ||
96 | |||
97 | (r ,g, b) = color | ||
98 | color = wx.Colour(r, g, b) | ||
99 | brush = wx.Brush(color, wx.SOLID) | ||
100 | dc.SetBrush(brush) | ||
101 | dc.DrawRectangle(offset_px, offset_py, width_px, width_py) | ||
102 | |||
103 | def update_rectangles(self, dc, start, end): | ||
104 | start += self.ts_start | ||
105 | end += self.ts_start | ||
106 | self.sched_tracer.fill_zone(start, end) | ||
107 | |||
108 | def on_paint(self, event): | ||
109 | dc = wx.PaintDC(self.scroll_panel) | ||
110 | self.dc = dc | ||
111 | |||
112 | width = min(self.width_virtual, self.screen_width) | ||
113 | (x, y) = self.scroll_start() | ||
114 | start = self.px_to_us(x) | ||
115 | end = self.px_to_us(x + width) | ||
116 | self.update_rectangles(dc, start, end) | ||
117 | |||
118 | def rect_from_ypixel(self, y): | ||
119 | y -= RootFrame.Y_OFFSET | ||
120 | rect = y / (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE) | ||
121 | height = y % (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE) | ||
122 | |||
123 | if rect < 0 or rect > self.nr_rects - 1 or height > RootFrame.RECT_HEIGHT: | ||
124 | return -1 | ||
125 | |||
126 | return rect | ||
127 | |||
128 | def update_summary(self, txt): | ||
129 | if self.txt: | ||
130 | self.txt.Destroy() | ||
131 | self.txt = wx.StaticText(self.panel, -1, txt, (0, (self.screen_height / 2) + 50)) | ||
132 | |||
133 | |||
134 | def on_mouse_down(self, event): | ||
135 | (x, y) = event.GetPositionTuple() | ||
136 | rect = self.rect_from_ypixel(y) | ||
137 | if rect == -1: | ||
138 | return | ||
139 | |||
140 | t = self.px_to_us(x) + self.ts_start | ||
141 | |||
142 | self.sched_tracer.mouse_down(rect, t) | ||
143 | |||
144 | |||
145 | def update_width_virtual(self): | ||
146 | self.width_virtual = self.us_to_px(self.ts_end - self.ts_start) | ||
147 | |||
148 | def __zoom(self, x): | ||
149 | self.update_width_virtual() | ||
150 | (xpos, ypos) = self.scroll.GetViewStart() | ||
151 | xpos = self.us_to_px(x) / self.scroll_scale | ||
152 | self.scroll.SetScrollbars(self.scroll_scale, self.scroll_scale, self.width_virtual / self.scroll_scale, self.height_virtual / self.scroll_scale, xpos, ypos) | ||
153 | self.Refresh() | ||
154 | |||
155 | def zoom_in(self): | ||
156 | x = self.scroll_start_us() | ||
157 | self.zoom *= 2 | ||
158 | self.__zoom(x) | ||
159 | |||
160 | def zoom_out(self): | ||
161 | x = self.scroll_start_us() | ||
162 | self.zoom /= 2 | ||
163 | self.__zoom(x) | ||
164 | |||
165 | |||
166 | def on_key_press(self, event): | ||
167 | key = event.GetRawKeyCode() | ||
168 | if key == ord("+"): | ||
169 | self.zoom_in() | ||
170 | return | ||
171 | if key == ord("-"): | ||
172 | self.zoom_out() | ||
173 | return | ||
174 | |||
175 | key = event.GetKeyCode() | ||
176 | (x, y) = self.scroll.GetViewStart() | ||
177 | if key == wx.WXK_RIGHT: | ||
178 | self.scroll.Scroll(x + 1, y) | ||
179 | elif key == wx.WXK_LEFT: | ||
180 | self.scroll.Scroll(x - 1, y) | ||
181 | elif key == wx.WXK_DOWN: | ||
182 | self.scroll.Scroll(x, y + 1) | ||
183 | elif key == wx.WXK_UP: | ||
184 | self.scroll.Scroll(x, y - 1) | ||
diff --git a/tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Util.py b/tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Util.py new file mode 100644 index 000000000000..15c8400240fd --- /dev/null +++ b/tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Util.py | |||
@@ -0,0 +1,86 @@ | |||
1 | # Util.py - Python extension for perf script, miscellaneous utility code | ||
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 | import errno, os | ||
10 | |||
11 | FUTEX_WAIT = 0 | ||
12 | FUTEX_WAKE = 1 | ||
13 | FUTEX_PRIVATE_FLAG = 128 | ||
14 | FUTEX_CLOCK_REALTIME = 256 | ||
15 | FUTEX_CMD_MASK = ~(FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME) | ||
16 | |||
17 | NSECS_PER_SEC = 1000000000 | ||
18 | |||
19 | def avg(total, n): | ||
20 | return total / n | ||
21 | |||
22 | def nsecs(secs, nsecs): | ||
23 | return secs * NSECS_PER_SEC + nsecs | ||
24 | |||
25 | def nsecs_secs(nsecs): | ||
26 | return nsecs / NSECS_PER_SEC | ||
27 | |||
28 | def nsecs_nsecs(nsecs): | ||
29 | return nsecs % NSECS_PER_SEC | ||
30 | |||
31 | def nsecs_str(nsecs): | ||
32 | str = "%5u.%09u" % (nsecs_secs(nsecs), nsecs_nsecs(nsecs)), | ||
33 | return str | ||
34 | |||
35 | def add_stats(dict, key, value): | ||
36 | if not dict.has_key(key): | ||
37 | dict[key] = (value, value, value, 1) | ||
38 | else: | ||
39 | min, max, avg, count = dict[key] | ||
40 | if value < min: | ||
41 | min = value | ||
42 | if value > max: | ||
43 | max = value | ||
44 | avg = (avg + value) / 2 | ||
45 | dict[key] = (min, max, avg, count + 1) | ||
46 | |||
47 | def clear_term(): | ||
48 | print("\x1b[H\x1b[2J") | ||
49 | |||
50 | audit_package_warned = False | ||
51 | |||
52 | try: | ||
53 | import audit | ||
54 | machine_to_id = { | ||
55 | 'x86_64': audit.MACH_86_64, | ||
56 | 'alpha' : audit.MACH_ALPHA, | ||
57 | 'ia64' : audit.MACH_IA64, | ||
58 | 'ppc' : audit.MACH_PPC, | ||
59 | 'ppc64' : audit.MACH_PPC64, | ||
60 | 's390' : audit.MACH_S390, | ||
61 | 's390x' : audit.MACH_S390X, | ||
62 | 'i386' : audit.MACH_X86, | ||
63 | 'i586' : audit.MACH_X86, | ||
64 | 'i686' : audit.MACH_X86, | ||
65 | } | ||
66 | try: | ||
67 | machine_to_id['armeb'] = audit.MACH_ARMEB | ||
68 | except: | ||
69 | pass | ||
70 | machine_id = machine_to_id[os.uname()[4]] | ||
71 | except: | ||
72 | if not audit_package_warned: | ||
73 | audit_package_warned = True | ||
74 | print "Install the audit-libs-python package to get syscall names" | ||
75 | |||
76 | def syscall_name(id): | ||
77 | try: | ||
78 | return audit.audit_syscall_to_name(id, machine_id) | ||
79 | except: | ||
80 | return str(id) | ||
81 | |||
82 | def strerror(nr): | ||
83 | try: | ||
84 | return errno.errorcode[abs(nr)] | ||
85 | except: | ||
86 | return "Unknown %d errno" % nr | ||