4 from gi.repository import Gio
10 def acquire_time_data():
11 manager = Gio.DBusProxy.new_for_bus_sync(bus, Gio.DBusProxyFlags.NONE,
12 None, 'org.freedesktop.systemd1', '/org/freedesktop/systemd1', 'org.freedesktop.systemd1.Manager', None)
13 units = manager.ListUnits()
21 properties = Gio.DBusProxy.new_for_bus_sync(bus, Gio.DBusProxyFlags.NONE,
22 None, 'org.freedesktop.systemd1', i[6], 'org.freedesktop.DBus.Properties', None)
24 ixt = properties.Get('(ss)', 'org.freedesktop.systemd1.Unit', 'InactiveExitTimestampMonotonic')
25 aet = properties.Get('(ss)', 'org.freedesktop.systemd1.Unit', 'ActiveEnterTimestampMonotonic')
26 axt = properties.Get('(ss)', 'org.freedesktop.systemd1.Unit', 'ActiveExitTimestampMonotonic')
27 iet = properties.Get('(ss)', 'org.freedesktop.systemd1.Unit', 'InactiveEnterTimestampMonotonic')
29 l.append((str(i[0]), ixt, aet, axt, iet))
33 def acquire_start_time():
34 properties = Gio.DBusProxy.new_for_bus_sync(bus, Gio.DBusProxyFlags.NONE,
35 None, 'org.freedesktop.systemd1', '/org/freedesktop/systemd1', 'org.freedesktop.DBus.Properties', None)
37 initrd_time = properties.Get('(ss)', 'org.freedesktop.systemd1.Manager', 'InitRDTimestampMonotonic')
38 userspace_time = properties.Get('(ss)', 'org.freedesktop.systemd1.Manager', 'UserspaceTimestampMonotonic')
39 finish_time = properties.Get('(ss)', 'org.freedesktop.systemd1.Manager', 'FinishTimestampMonotonic')
42 sys.stderr.write("Bootup is not yet finished. Please try again later.\n")
45 assert initrd_time <= userspace_time
46 assert userspace_time <= finish_time
48 return initrd_time, userspace_time, finish_time
50 def draw_box(context, j, k, l, m, r = 0, g = 0, b = 0):
52 context.set_source_rgb(r, g, b)
53 context.rectangle(j, k, l, m)
57 def draw_text(context, x, y, text, size = 12, r = 0, g = 0, b = 0, vcenter = 0.5, hcenter = 0.5):
60 context.set_source_rgb(r, g, b)
61 context.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
62 context.set_font_size(size)
64 if vcenter or hcenter:
65 x_bearing, y_bearing, width, height = context.text_extents(text)[:4]
68 x = x - width*hcenter - x_bearing
71 y = y - height*vcenter - y_bearing
74 context.show_text(text)
79 sys.stdout.write("""systemd-analyze [--user] time
80 systemd-analyze [--user] blame
81 systemd-analyze [--user] plot
83 Process systemd profiling information
85 -h --help Show this help
94 initrd_time, start_time, finish_time = acquire_start_time()
97 sys.stdout.write("Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums\n" % ( \
99 (start_time - initrd_time)/1000, \
100 (finish_time - start_time)/1000, \
103 sys.stdout.write("Startup finished in %lums (kernel) + %lums (userspace) = %lums\n" % ( \
105 (finish_time - start_time)/1000, \
111 data = acquire_time_data()
112 s = sorted(data, key = lambda i: i[2] - i[1], reverse = True)
114 for name, ixt, aet, axt, iet in s:
116 if ixt <= 0 or aet <= 0:
122 sys.stdout.write("%6lums %s\n" % ((aet - ixt) / 1000, name))
126 sys.stderr.write("Failed to initilize python-cairo required for 'plot' verb.\n")
128 initrd_time, start_time, finish_time = acquire_start_time()
129 data = acquire_time_data()
130 s = sorted(data, key = lambda i: i[1])
132 # Account for kernel and initramfs bars if they exist
138 for name, ixt, aet, axt, iet in s:
140 if (ixt >= start_time and ixt <= finish_time) or \
141 (aet >= start_time and aet <= finish_time) or \
142 (axt >= start_time and axt <= finish_time):
147 bar_space = bar_height * 0.1
149 # 1000px = 10s, 1px = 10ms
150 width = finish_time/10000 + border*2
151 height = count * (bar_height + bar_space) + border * 2
156 surface = cairo.SVGSurface(sys.stdout, width, height)
157 context = cairo.Context(surface)
159 draw_box(context, 0, 0, width, height, 1, 1, 1)
161 context.translate(border + 0.5, border + 0.5)
164 context.set_line_width(1)
165 context.set_source_rgb(0.7, 0.7, 0.7)
167 for x in range(0, int(finish_time/10000) + 100, 100):
168 context.move_to(x, 0)
169 context.line_to(x, height-border*2)
171 context.move_to(0, 0)
172 context.line_to(width-border*2, 0)
174 context.move_to(0, height-border*2)
175 context.line_to(width-border*2, height-border*2)
181 if os.path.exists("/etc/os-release"):
182 for line in open("/etc/os-release"):
183 if line.startswith('PRETTY_NAME='):
185 osrel = osrel.strip('\"\n')
188 banner = "{} {} ({} {}) {}".format(osrel, *(os.uname()[1:5]))
189 draw_text(context, 0, -15, banner, hcenter = 0, vcenter = 1)
191 for x in range(0, int(finish_time/10000) + 100, 100):
192 draw_text(context, x, -5, "%lus" % (x/100), vcenter = 0, hcenter = 0)
196 # draw boxes for kernel and initramfs boot time
198 draw_box(context, 0, y, initrd_time/10000, bar_height, 0.7, 0.7, 0.7)
199 draw_text(context, 10, y + bar_height/2, "kernel", hcenter = 0)
200 y += bar_height + bar_space
202 draw_box(context, initrd_time/10000, y, start_time/10000-initrd_time/10000, bar_height, 0.7, 0.7, 0.7)
203 draw_text(context, initrd_time/10000 + 10, y + bar_height/2, "initramfs", hcenter = 0)
204 y += bar_height + bar_space
207 draw_box(context, 0, y, start_time/10000, bar_height, 0.6, 0.6, 0.6)
208 draw_text(context, 10, y + bar_height/2, "kernel", hcenter = 0)
209 y += bar_height + bar_space
211 draw_box(context, start_time/10000, y, finish_time/10000-start_time/10000, bar_height, 0.7, 0.7, 0.7)
212 draw_text(context, start_time/10000 + 10, y + bar_height/2, "userspace", hcenter = 0)
213 y += bar_height + bar_space
215 for name, ixt, aet, axt, iet in s:
220 if ixt >= start_time and ixt <= finish_time:
224 b = min(filter(lambda x: x >= ixt, (aet, axt, iet, finish_time))) - ixt
226 draw_box(context, a/10000, y, b/10000, bar_height, 1, 0, 0)
232 if aet >= start_time and aet <= finish_time:
236 b = min(filter(lambda x: x >= aet, (axt, iet, finish_time))) - aet
238 draw_box(context, a/10000, y, b/10000, bar_height, .8, .6, .6)
244 if axt >= start_time and axt <= finish_time:
248 b = min(filter(lambda x: x >= axt, (iet, finish_time))) - axt
250 draw_box(context, a/10000, y, b/10000, bar_height, .6, .4, .4)
259 if x < width/2-border:
260 draw_text(context, x + 10, y + bar_height/2, name, hcenter = 0)
262 draw_text(context, x - 10, y + bar_height/2, name, hcenter = 1)
264 y += bar_height + bar_space
266 draw_text(context, 0, height-border*2, "Legend: Red = Activating; Pink = Active; Dark Pink = Deactivating", hcenter = 0, vcenter = -1)
269 draw_text(context, 0, height-border*2 + bar_height, "Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums" % ( \
271 (start_time - initrd_time)/1000, \
272 (finish_time - start_time)/1000, \
273 finish_time/1000), hcenter = 0, vcenter = -1)
275 draw_text(context, 0, height-border*2 + bar_height, "Startup finished in %lums (kernel) + %lums (userspace) = %lums" % ( \
277 (finish_time - start_time)/1000, \
278 finish_time/1000), hcenter = 0, vcenter = -1)
283 sys.stderr.write("Unknown verb '%s'.\n" % args[0])
287 bus = Gio.BusType.SYSTEM
290 opts, args = getopt.gnu_getopt(sys.argv[1:], "h", ["help", "user"])
291 except getopt.GetoptError as err:
292 sys.stdout.write(str(err) + "\n")
296 if o in ("-h", "--help"):
299 bus = Gio.BusType.SESSION
301 assert False, "unhandled option"
303 verb = {'time' : time,
312 verb.get(args[0], unknown_verb)()