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 time
76 Process systemd profiling information
78 -h --help Show this help
82 bus = dbus.SystemBus()
84 if len(sys.argv) <= 1 or sys.argv[1] == 'time':
86 initrd_time, start_time, finish_time = acquire_start_time()
89 print "Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums" % ( \
91 (start_time - initrd_time)/1000, \
92 (finish_time - start_time)/1000, \
95 print "Startup finished in %lums (kernel) + %lums (userspace) = %lums" % ( \
97 (finish_time - start_time)/1000, \
101 elif sys.argv[1] == 'blame':
103 data = acquire_time_data()
104 s = sorted(data, key = lambda i: i[2] - i[1], reverse = True)
106 for name, ixt, aet, axt, iet in s:
108 if ixt <= 0 or aet <= 0:
114 sys.stdout.write("%6lums %s\n" % ((aet - ixt) / 1000, name))
116 elif sys.argv[1] == 'plot':
119 initrd_time, start_time, finish_time = acquire_start_time()
120 data = acquire_time_data()
121 s = sorted(data, key = lambda i: i[1])
123 # Account for kernel and initramfs bars if they exist
129 for name, ixt, aet, axt, iet in s:
131 if (ixt >= start_time and ixt <= finish_time) or \
132 (aet >= start_time and aet <= finish_time) or \
133 (axt >= start_time and axt <= finish_time):
138 bar_space = bar_height * 0.1
140 # 1000px = 10s, 1px = 10ms
141 width = finish_time/10000 + border*2
142 height = count * (bar_height + bar_space) + border * 2
147 surface = cairo.SVGSurface(sys.stdout, width, height)
148 context = cairo.Context(surface)
150 draw_box(context, 0, 0, width, height, 1, 1, 1)
152 context.translate(border + 0.5, border + 0.5)
155 context.set_line_width(1)
156 context.set_source_rgb(0.7, 0.7, 0.7)
158 for x in range(0, finish_time/10000 + 100, 100):
159 context.move_to(x, 0)
160 context.line_to(x, height-border*2)
162 context.move_to(0, 0)
163 context.line_to(width-border*2, 0)
165 context.move_to(0, height-border*2)
166 context.line_to(width-border*2, height-border*2)
172 if os.path.exists("/etc/os-release"):
173 for line in open("/etc/os-release"):
174 if line.startswith('PRETTY_NAME='):
176 osrel = osrel.strip('\"\n')
179 banner = "{} {} ({} {}) {}".format(osrel, *(os.uname()[1:5]))
180 draw_text(context, 0, -15, banner, hcenter = 0, vcenter = 1)
182 for x in range(0, finish_time/10000 + 100, 100):
183 draw_text(context, x, -5, "%lus" % (x/100), vcenter = 0, hcenter = 0)
187 # draw boxes for kernel and initramfs boot time
189 draw_box(context, 0, y, initrd_time/10000, bar_height, 0.7, 0.7, 0.7)
190 draw_text(context, 10, y + bar_height/2, "kernel", hcenter = 0)
191 y += bar_height + bar_space
193 draw_box(context, initrd_time/10000, y, start_time/10000-initrd_time/10000, bar_height, 0.7, 0.7, 0.7)
194 draw_text(context, initrd_time/10000 + 10, y + bar_height/2, "initramfs", hcenter = 0)
195 y += bar_height + bar_space
198 draw_box(context, 0, y, start_time/10000, bar_height, 0.6, 0.6, 0.6)
199 draw_text(context, 10, y + bar_height/2, "kernel", hcenter = 0)
200 y += bar_height + bar_space
202 draw_box(context, start_time/10000, y, finish_time/10000-start_time/10000, bar_height, 0.7, 0.7, 0.7)
203 draw_text(context, start_time/10000 + 10, y + bar_height/2, "userspace", hcenter = 0)
204 y += bar_height + bar_space
206 for name, ixt, aet, axt, iet in s:
211 if ixt >= start_time and ixt <= finish_time:
215 b = min(filter(lambda x: x >= ixt, (aet, axt, iet, finish_time))) - ixt
217 draw_box(context, a/10000, y, b/10000, bar_height, 1, 0, 0)
223 if aet >= start_time and aet <= finish_time:
227 b = min(filter(lambda x: x >= aet, (axt, iet, finish_time))) - aet
229 draw_box(context, a/10000, y, b/10000, bar_height, .8, .6, .6)
235 if axt >= start_time and axt <= finish_time:
239 b = min(filter(lambda x: x >= axt, (iet, finish_time))) - axt
241 draw_box(context, a/10000, y, b/10000, bar_height, .6, .4, .4)
250 if x < width/2-border:
251 draw_text(context, x + 10, y + bar_height/2, name, hcenter = 0)
253 draw_text(context, x - 10, y + bar_height/2, name, hcenter = 1)
255 y += bar_height + bar_space
257 draw_text(context, 0, height-border*2, "Legend: Red = Activating; Pink = Active; Dark Pink = Deactivating", hcenter = 0, vcenter = -1)
260 draw_text(context, 0, height-border*2 + bar_height, "Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums" % ( \
262 (start_time - initrd_time)/1000, \
263 (finish_time - start_time)/1000, \
264 finish_time/1000), hcenter = 0, vcenter = -1)
266 draw_text(context, 0, height-border*2 + bar_height, "Startup finished in %lums (kernel) + %lums (userspace) = %lums" % ( \
268 (finish_time - start_time)/1000, \
269 finish_time/1000), hcenter = 0, vcenter = -1)
272 elif sys.argv[1] in ("help", "--help", "-h"):
275 sys.stderr.write("Unknown verb '%s'.\n" % sys.argv[1])