5 def acquire_time_data():
7 manager = dbus.Interface(bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1'), 'org.freedesktop.systemd1.Manager')
8 units = manager.ListUnits()
16 properties = dbus.Interface(bus.get_object('org.freedesktop.systemd1', i[6]), 'org.freedesktop.DBus.Properties')
18 ixt = int(properties.Get('org.freedesktop.systemd1.Unit', 'InactiveExitTimestamp'))
19 aet = int(properties.Get('org.freedesktop.systemd1.Unit', 'ActiveEnterTimestamp'))
20 axt = int(properties.Get('org.freedesktop.systemd1.Unit', 'ActiveExitTimestamp'))
21 iet = int(properties.Get('org.freedesktop.systemd1.Unit', 'InactiveEnterTimestamp'))
23 l.append((str(i[0]), ixt, aet, axt, iet))
27 def acquire_start_time():
28 properties = dbus.Interface(bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1'), 'org.freedesktop.DBus.Properties')
30 startup_time = int(properties.Get('org.freedesktop.systemd1.Manager', 'StartupTimestamp'))
31 finish_time = int(properties.Get('org.freedesktop.systemd1.Manager', 'FinishTimestamp'))
33 assert startup_time <= finish_time
35 return startup_time, finish_time
37 def draw_box(context, j, k, l, m, r = 0, g = 0, b = 0):
39 context.set_source_rgb(r, g, b)
40 context.rectangle(j, k, l, m)
44 def draw_text(context, x, y, text, size = 12, r = 0, g = 0, b = 0, vcenter = 0.5, hcenter = 0.5):
47 context.set_source_rgb(r, g, b)
48 context.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
49 context.set_font_size(size)
51 if vcenter or hcenter:
52 x_bearing, y_bearing, width, height = context.text_extents(text)[:4]
55 x = x - width*hcenter - x_bearing
58 y = y - height*vcenter - y_bearing
61 context.show_text(text)
66 sys.stdout.write("""systemd-analyze blame
69 Process systemd profiling information
71 -h --help Show this help
75 bus = dbus.SystemBus()
77 if len(sys.argv) <= 1 or sys.argv[1] == 'blame':
79 data = acquire_time_data()
80 s = sorted(data, key = lambda i: i[2] - i[1], reverse = True)
82 for name, ixt, aet, axt, iet in s:
84 if ixt <= 0 or aet <= 0:
90 sys.stdout.write("%6lums %s\n" % ((aet - ixt) / 1000, name))
92 elif sys.argv[1] == 'plot':
95 start_time, finish_time = acquire_start_time()
96 data = acquire_time_data()
97 s = sorted(data, key = lambda i: i[1])
101 for name, ixt, aet, axt, iet in s:
103 if (ixt >= start_time and ixt <= finish_time) or \
104 (aet >= start_time and aet <= finish_time) or \
105 (axt >= start_time and axt <= finish_time):
110 bar_space = bar_height * 0.1
112 # 1000px = 10s, 1px = 10ms
113 width = (finish_time - start_time)/10000 + border*2
114 height = count * (bar_height + bar_space) + border * 2
119 surface = cairo.SVGSurface(sys.stdout, width, height)
120 context = cairo.Context(surface)
122 draw_box(context, 0, 0, width, height, 1, 1, 1)
124 context.translate(border + 0.5, border + 0.5)
127 context.set_line_width(1)
128 context.set_source_rgb(0.7, 0.7, 0.7)
130 for x in range(0, (finish_time - start_time)/10000, 100):
131 context.move_to(x, 0)
132 context.line_to(x, height-border*2)
134 context.move_to(0, 0)
135 context.line_to(width-border*2, 0)
137 context.move_to(0, height-border*2)
138 context.line_to(width-border*2, height-border*2)
143 for x in range(0, (finish_time - start_time)/10000, 100):
144 draw_text(context, x, -5, "%lus" % (x/100), vcenter = 0, hcenter = 0)
148 for name, ixt, aet, axt, iet in s:
153 if ixt >= start_time and ixt <= finish_time:
157 b = min(filter(lambda x: x >= ixt, (aet, axt, iet, finish_time))) - ixt
159 draw_box(context, a/10000, y, b/10000, bar_height, 1, 0, 0)
165 if aet >= start_time and aet <= finish_time:
169 b = min(filter(lambda x: x >= aet, (axt, iet, finish_time))) - aet
171 draw_box(context, a/10000, y, b/10000, bar_height, .8, .6, .6)
177 if axt >= start_time and axt <= finish_time:
181 b = min(filter(lambda x: x >= axt, (iet, finish_time))) - axt
183 draw_box(context, a/10000, y, b/10000, bar_height, .6, .4, .4)
192 if x < width/2-border:
193 draw_text(context, x + 10, y + bar_height/2, name, hcenter = 0)
195 draw_text(context, x - 10, y + bar_height/2, name, hcenter = 1)
197 y += bar_height + bar_space
199 draw_text(context, 0, height-border*2, "Legend: Red = Activating; Pink = Active; Dark Pink = Deactivating", hcenter = 0, vcenter = -1)
202 elif sys.argv[1] in ("help", "--help", "-h"):
205 sys.stderr.write("Unknown verb '%s'.\n" % sys.argv[1])