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', 'InactiveExitTimestampMonotonic'))
19 aet = int(properties.Get('org.freedesktop.systemd1.Unit', 'ActiveEnterTimestampMonotonic'))
20 axt = int(properties.Get('org.freedesktop.systemd1.Unit', 'ActiveExitTimestampMonotonic'))
21 iet = int(properties.Get('org.freedesktop.systemd1.Unit', 'InactiveEnterTimestampMonotonic'))
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 initrd_time = int(properties.Get('org.freedesktop.systemd1.Manager', 'InitRDTimestampMonotonic'))
31 startup_time = int(properties.Get('org.freedesktop.systemd1.Manager', 'StartupTimestampMonotonic'))
32 finish_time = int(properties.Get('org.freedesktop.systemd1.Manager', 'FinishTimestampMonotonic'))
35 sys.stderr.write("Bootup is not yet finished. Please try again later.\n")
38 assert initrd_time <= startup_time
39 assert startup_time <= finish_time
41 return initrd_time, startup_time, finish_time
43 def draw_box(context, j, k, l, m, r = 0, g = 0, b = 0):
45 context.set_source_rgb(r, g, b)
46 context.rectangle(j, k, l, m)
50 def draw_text(context, x, y, text, size = 12, r = 0, g = 0, b = 0, vcenter = 0.5, hcenter = 0.5):
53 context.set_source_rgb(r, g, b)
54 context.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
55 context.set_font_size(size)
57 if vcenter or hcenter:
58 x_bearing, y_bearing, width, height = context.text_extents(text)[:4]
61 x = x - width*hcenter - x_bearing
64 y = y - height*vcenter - y_bearing
67 context.show_text(text)
72 sys.stdout.write("""systemd-analyze [--user] time
73 systemd-analyze [--user] blame
74 systemd-analyze [--user] plot
76 Process systemd profiling information
78 -h --help Show this help
82 bus = dbus.SystemBus()
85 if len(sys.argv) > 1 and sys.argv[1] == '--user':
86 bus = dbus.SessionBus()
90 if len(sys.argv) <= command_index or sys.argv[command_index] == 'time':
92 initrd_time, start_time, finish_time = acquire_start_time()
95 print "Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums" % ( \
97 (start_time - initrd_time)/1000, \
98 (finish_time - start_time)/1000, \
101 print "Startup finished in %lums (kernel) + %lums (userspace) = %lums" % ( \
103 (finish_time - start_time)/1000, \
107 elif sys.argv[command_index] == 'blame':
109 data = acquire_time_data()
110 s = sorted(data, key = lambda i: i[2] - i[1], reverse = True)
112 for name, ixt, aet, axt, iet in s:
114 if ixt <= 0 or aet <= 0:
120 sys.stdout.write("%6lums %s\n" % ((aet - ixt) / 1000, name))
122 elif sys.argv[command_index] == 'plot':
125 initrd_time, start_time, finish_time = acquire_start_time()
126 data = acquire_time_data()
127 s = sorted(data, key = lambda i: i[1])
129 # Account for kernel and initramfs bars if they exist
135 for name, ixt, aet, axt, iet in s:
137 if (ixt >= start_time and ixt <= finish_time) or \
138 (aet >= start_time and aet <= finish_time) or \
139 (axt >= start_time and axt <= finish_time):
144 bar_space = bar_height * 0.1
146 # 1000px = 10s, 1px = 10ms
147 width = finish_time/10000 + border*2
148 height = count * (bar_height + bar_space) + border * 2
153 surface = cairo.SVGSurface(sys.stdout, width, height)
154 context = cairo.Context(surface)
156 draw_box(context, 0, 0, width, height, 1, 1, 1)
158 context.translate(border + 0.5, border + 0.5)
161 context.set_line_width(1)
162 context.set_source_rgb(0.7, 0.7, 0.7)
164 for x in range(0, finish_time/10000 + 100, 100):
165 context.move_to(x, 0)
166 context.line_to(x, height-border*2)
168 context.move_to(0, 0)
169 context.line_to(width-border*2, 0)
171 context.move_to(0, height-border*2)
172 context.line_to(width-border*2, height-border*2)
178 if os.path.exists("/etc/os-release"):
179 for line in open("/etc/os-release"):
180 if line.startswith('PRETTY_NAME='):
182 osrel = osrel.strip('\"\n')
185 banner = "{} {} ({} {}) {}".format(osrel, *(os.uname()[1:5]))
186 draw_text(context, 0, -15, banner, hcenter = 0, vcenter = 1)
188 for x in range(0, finish_time/10000 + 100, 100):
189 draw_text(context, x, -5, "%lus" % (x/100), vcenter = 0, hcenter = 0)
193 # draw boxes for kernel and initramfs boot time
195 draw_box(context, 0, y, initrd_time/10000, bar_height, 0.7, 0.7, 0.7)
196 draw_text(context, 10, y + bar_height/2, "kernel", hcenter = 0)
197 y += bar_height + bar_space
199 draw_box(context, initrd_time/10000, y, start_time/10000-initrd_time/10000, bar_height, 0.7, 0.7, 0.7)
200 draw_text(context, initrd_time/10000 + 10, y + bar_height/2, "initramfs", hcenter = 0)
201 y += bar_height + bar_space
204 draw_box(context, 0, y, start_time/10000, bar_height, 0.6, 0.6, 0.6)
205 draw_text(context, 10, y + bar_height/2, "kernel", hcenter = 0)
206 y += bar_height + bar_space
208 draw_box(context, start_time/10000, y, finish_time/10000-start_time/10000, bar_height, 0.7, 0.7, 0.7)
209 draw_text(context, start_time/10000 + 10, y + bar_height/2, "userspace", hcenter = 0)
210 y += bar_height + bar_space
212 for name, ixt, aet, axt, iet in s:
217 if ixt >= start_time and ixt <= finish_time:
221 b = min(filter(lambda x: x >= ixt, (aet, axt, iet, finish_time))) - ixt
223 draw_box(context, a/10000, y, b/10000, bar_height, 1, 0, 0)
229 if aet >= start_time and aet <= finish_time:
233 b = min(filter(lambda x: x >= aet, (axt, iet, finish_time))) - aet
235 draw_box(context, a/10000, y, b/10000, bar_height, .8, .6, .6)
241 if axt >= start_time and axt <= finish_time:
245 b = min(filter(lambda x: x >= axt, (iet, finish_time))) - axt
247 draw_box(context, a/10000, y, b/10000, bar_height, .6, .4, .4)
256 if x < width/2-border:
257 draw_text(context, x + 10, y + bar_height/2, name, hcenter = 0)
259 draw_text(context, x - 10, y + bar_height/2, name, hcenter = 1)
261 y += bar_height + bar_space
263 draw_text(context, 0, height-border*2, "Legend: Red = Activating; Pink = Active; Dark Pink = Deactivating", hcenter = 0, vcenter = -1)
266 draw_text(context, 0, height-border*2 + bar_height, "Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums" % ( \
268 (start_time - initrd_time)/1000, \
269 (finish_time - start_time)/1000, \
270 finish_time/1000), hcenter = 0, vcenter = -1)
272 draw_text(context, 0, height-border*2 + bar_height, "Startup finished in %lums (kernel) + %lums (userspace) = %lums" % ( \
274 (finish_time - start_time)/1000, \
275 finish_time/1000), hcenter = 0, vcenter = -1)
278 elif sys.argv[command_index] in ("help", "--help", "-h"):
281 sys.stderr.write("Unknown verb '%s'.\n" % sys.argv[command_index])