Path: blob/master/tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/SchedGui.py
10832 views
# SchedGui.py - Python extension for perf script, basic GUI code for1# traces drawing and overview.2#3# Copyright (C) 2010 by Frederic Weisbecker <[email protected]>4#5# This software is distributed under the terms of the GNU General6# Public License ("GPL") version 2 as published by the Free Software7# Foundation.8910try:11import wx12except ImportError:13raise ImportError, "You need to install the wxpython lib for this script"141516class RootFrame(wx.Frame):17Y_OFFSET = 10018RECT_HEIGHT = 10019RECT_SPACE = 5020EVENT_MARKING_WIDTH = 52122def __init__(self, sched_tracer, title, parent = None, id = -1):23wx.Frame.__init__(self, parent, id, title)2425(self.screen_width, self.screen_height) = wx.GetDisplaySize()26self.screen_width -= 1027self.screen_height -= 1028self.zoom = 0.529self.scroll_scale = 2030self.sched_tracer = sched_tracer31self.sched_tracer.set_root_win(self)32(self.ts_start, self.ts_end) = sched_tracer.interval()33self.update_width_virtual()34self.nr_rects = sched_tracer.nr_rectangles() + 135self.height_virtual = RootFrame.Y_OFFSET + (self.nr_rects * (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE))3637# whole window panel38self.panel = wx.Panel(self, size=(self.screen_width, self.screen_height))3940# scrollable container41self.scroll = wx.ScrolledWindow(self.panel)42self.scroll.SetScrollbars(self.scroll_scale, self.scroll_scale, self.width_virtual / self.scroll_scale, self.height_virtual / self.scroll_scale)43self.scroll.EnableScrolling(True, True)44self.scroll.SetFocus()4546# scrollable drawing area47self.scroll_panel = wx.Panel(self.scroll, size=(self.screen_width - 15, self.screen_height / 2))48self.scroll_panel.Bind(wx.EVT_PAINT, self.on_paint)49self.scroll_panel.Bind(wx.EVT_KEY_DOWN, self.on_key_press)50self.scroll_panel.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down)51self.scroll.Bind(wx.EVT_PAINT, self.on_paint)52self.scroll.Bind(wx.EVT_KEY_DOWN, self.on_key_press)53self.scroll.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down)5455self.scroll.Fit()56self.Fit()5758self.scroll_panel.SetDimensions(-1, -1, self.width_virtual, self.height_virtual, wx.SIZE_USE_EXISTING)5960self.txt = None6162self.Show(True)6364def us_to_px(self, val):65return val / (10 ** 3) * self.zoom6667def px_to_us(self, val):68return (val / self.zoom) * (10 ** 3)6970def scroll_start(self):71(x, y) = self.scroll.GetViewStart()72return (x * self.scroll_scale, y * self.scroll_scale)7374def scroll_start_us(self):75(x, y) = self.scroll_start()76return self.px_to_us(x)7778def paint_rectangle_zone(self, nr, color, top_color, start, end):79offset_px = self.us_to_px(start - self.ts_start)80width_px = self.us_to_px(end - self.ts_start)8182offset_py = RootFrame.Y_OFFSET + (nr * (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE))83width_py = RootFrame.RECT_HEIGHT8485dc = self.dc8687if top_color is not None:88(r, g, b) = top_color89top_color = wx.Colour(r, g, b)90brush = wx.Brush(top_color, wx.SOLID)91dc.SetBrush(brush)92dc.DrawRectangle(offset_px, offset_py, width_px, RootFrame.EVENT_MARKING_WIDTH)93width_py -= RootFrame.EVENT_MARKING_WIDTH94offset_py += RootFrame.EVENT_MARKING_WIDTH9596(r ,g, b) = color97color = wx.Colour(r, g, b)98brush = wx.Brush(color, wx.SOLID)99dc.SetBrush(brush)100dc.DrawRectangle(offset_px, offset_py, width_px, width_py)101102def update_rectangles(self, dc, start, end):103start += self.ts_start104end += self.ts_start105self.sched_tracer.fill_zone(start, end)106107def on_paint(self, event):108dc = wx.PaintDC(self.scroll_panel)109self.dc = dc110111width = min(self.width_virtual, self.screen_width)112(x, y) = self.scroll_start()113start = self.px_to_us(x)114end = self.px_to_us(x + width)115self.update_rectangles(dc, start, end)116117def rect_from_ypixel(self, y):118y -= RootFrame.Y_OFFSET119rect = y / (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE)120height = y % (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE)121122if rect < 0 or rect > self.nr_rects - 1 or height > RootFrame.RECT_HEIGHT:123return -1124125return rect126127def update_summary(self, txt):128if self.txt:129self.txt.Destroy()130self.txt = wx.StaticText(self.panel, -1, txt, (0, (self.screen_height / 2) + 50))131132133def on_mouse_down(self, event):134(x, y) = event.GetPositionTuple()135rect = self.rect_from_ypixel(y)136if rect == -1:137return138139t = self.px_to_us(x) + self.ts_start140141self.sched_tracer.mouse_down(rect, t)142143144def update_width_virtual(self):145self.width_virtual = self.us_to_px(self.ts_end - self.ts_start)146147def __zoom(self, x):148self.update_width_virtual()149(xpos, ypos) = self.scroll.GetViewStart()150xpos = self.us_to_px(x) / self.scroll_scale151self.scroll.SetScrollbars(self.scroll_scale, self.scroll_scale, self.width_virtual / self.scroll_scale, self.height_virtual / self.scroll_scale, xpos, ypos)152self.Refresh()153154def zoom_in(self):155x = self.scroll_start_us()156self.zoom *= 2157self.__zoom(x)158159def zoom_out(self):160x = self.scroll_start_us()161self.zoom /= 2162self.__zoom(x)163164165def on_key_press(self, event):166key = event.GetRawKeyCode()167if key == ord("+"):168self.zoom_in()169return170if key == ord("-"):171self.zoom_out()172return173174key = event.GetKeyCode()175(x, y) = self.scroll.GetViewStart()176if key == wx.WXK_RIGHT:177self.scroll.Scroll(x + 1, y)178elif key == wx.WXK_LEFT:179self.scroll.Scroll(x - 1, y)180elif key == wx.WXK_DOWN:181self.scroll.Scroll(x, y + 1)182elif key == wx.WXK_UP:183self.scroll.Scroll(x, y - 1)184185186