]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/analyze/systemd-analyze
manager: extend performance measurement interface to include firmware/loader times
[thirdparty/systemd.git] / src / analyze / systemd-analyze
1 #!/usr/bin/python
2
3 import getopt, dbus, sys, os
4 try:
5 import cairo
6 except ImportError:
7 cairo = None
8
9 def acquire_time_data():
10
11 manager = dbus.Interface(bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1'), 'org.freedesktop.systemd1.Manager')
12 units = manager.ListUnits()
13
14 l = []
15
16 for i in units:
17 if i[5] != "":
18 continue
19
20 properties = dbus.Interface(bus.get_object('org.freedesktop.systemd1', i[6]), 'org.freedesktop.DBus.Properties')
21
22 ixt = int(properties.Get('org.freedesktop.systemd1.Unit', 'InactiveExitTimestampMonotonic'))
23 aet = int(properties.Get('org.freedesktop.systemd1.Unit', 'ActiveEnterTimestampMonotonic'))
24 axt = int(properties.Get('org.freedesktop.systemd1.Unit', 'ActiveExitTimestampMonotonic'))
25 iet = int(properties.Get('org.freedesktop.systemd1.Unit', 'InactiveEnterTimestampMonotonic'))
26
27 l.append((str(i[0]), ixt, aet, axt, iet))
28
29 return l
30
31 def acquire_start_time():
32 properties = dbus.Interface(bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1'), 'org.freedesktop.DBus.Properties')
33
34 initrd_time = int(properties.Get('org.freedesktop.systemd1.Manager', 'InitRDTimestampMonotonic'))
35 userspace_time = int(properties.Get('org.freedesktop.systemd1.Manager', 'UserspaceTimestampMonotonic'))
36 finish_time = int(properties.Get('org.freedesktop.systemd1.Manager', 'FinishTimestampMonotonic'))
37
38 if finish_time == 0:
39 sys.stderr.write("Bootup is not yet finished. Please try again later.\n")
40 sys.exit(1)
41
42 assert initrd_time <= userspace_time
43 assert userspace_time <= finish_time
44
45 return initrd_time, userspace_time, finish_time
46
47 def draw_box(context, j, k, l, m, r = 0, g = 0, b = 0):
48 context.save()
49 context.set_source_rgb(r, g, b)
50 context.rectangle(j, k, l, m)
51 context.fill()
52 context.restore()
53
54 def draw_text(context, x, y, text, size = 12, r = 0, g = 0, b = 0, vcenter = 0.5, hcenter = 0.5):
55 context.save()
56
57 context.set_source_rgb(r, g, b)
58 context.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
59 context.set_font_size(size)
60
61 if vcenter or hcenter:
62 x_bearing, y_bearing, width, height = context.text_extents(text)[:4]
63
64 if hcenter:
65 x = x - width*hcenter - x_bearing
66
67 if vcenter:
68 y = y - height*vcenter - y_bearing
69
70 context.move_to(x, y)
71 context.show_text(text)
72
73 context.restore()
74
75 def usage():
76 sys.stdout.write("""systemd-analyze [--user] time
77 systemd-analyze [--user] blame
78 systemd-analyze [--user] plot
79
80 Process systemd profiling information
81
82 -h --help Show this help
83 """)
84
85 def help():
86 usage()
87 sys.exit()
88
89 def time():
90
91 initrd_time, start_time, finish_time = acquire_start_time()
92
93 if initrd_time > 0:
94 sys.stdout.write("Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums\n" % ( \
95 initrd_time/1000, \
96 (start_time - initrd_time)/1000, \
97 (finish_time - start_time)/1000, \
98 finish_time/1000))
99 else:
100 sys.stdout.write("Startup finished in %lums (kernel) + %lums (userspace) = %lums\n" % ( \
101 start_time/1000, \
102 (finish_time - start_time)/1000, \
103 finish_time/1000))
104
105
106 def blame():
107
108 data = acquire_time_data()
109 s = sorted(data, key = lambda i: i[2] - i[1], reverse = True)
110
111 for name, ixt, aet, axt, iet in s:
112
113 if ixt <= 0 or aet <= 0:
114 continue
115
116 if aet <= ixt:
117 continue
118
119 sys.stdout.write("%6lums %s\n" % ((aet - ixt) / 1000, name))
120
121 def plot():
122 if cairo is None:
123 sys.stderr.write("Failed to initilize python-cairo required for 'plot' verb.\n")
124 sys.exit(1)
125 initrd_time, start_time, finish_time = acquire_start_time()
126 data = acquire_time_data()
127 s = sorted(data, key = lambda i: i[1])
128
129 # Account for kernel and initramfs bars if they exist
130 if initrd_time > 0:
131 count = 3
132 else:
133 count = 2
134
135 for name, ixt, aet, axt, iet in s:
136
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):
140 count += 1
141
142 border = 100
143 bar_height = 20
144 bar_space = bar_height * 0.1
145
146 # 1000px = 10s, 1px = 10ms
147 width = finish_time/10000 + border*2
148 height = count * (bar_height + bar_space) + border * 2
149
150 if width < 1000:
151 width = 1000
152
153 surface = cairo.SVGSurface(sys.stdout, width, height)
154 context = cairo.Context(surface)
155
156 draw_box(context, 0, 0, width, height, 1, 1, 1)
157
158 context.translate(border + 0.5, border + 0.5)
159
160 context.save()
161 context.set_line_width(1)
162 context.set_source_rgb(0.7, 0.7, 0.7)
163
164 for x in range(0, int(finish_time/10000) + 100, 100):
165 context.move_to(x, 0)
166 context.line_to(x, height-border*2)
167
168 context.move_to(0, 0)
169 context.line_to(width-border*2, 0)
170
171 context.move_to(0, height-border*2)
172 context.line_to(width-border*2, height-border*2)
173
174 context.stroke()
175 context.restore()
176
177 osrel = "Linux"
178 if os.path.exists("/etc/os-release"):
179 for line in open("/etc/os-release"):
180 if line.startswith('PRETTY_NAME='):
181 osrel = line[12:]
182 osrel = osrel.strip('\"\n')
183 break
184
185 banner = "{} {} ({} {}) {}".format(osrel, *(os.uname()[1:5]))
186 draw_text(context, 0, -15, banner, hcenter = 0, vcenter = 1)
187
188 for x in range(0, int(finish_time/10000) + 100, 100):
189 draw_text(context, x, -5, "%lus" % (x/100), vcenter = 0, hcenter = 0)
190
191 y = 0
192
193 # draw boxes for kernel and initramfs boot time
194 if initrd_time > 0:
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
198
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
202
203 else:
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
207
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
211
212 for name, ixt, aet, axt, iet in s:
213
214 drawn = False
215 left = -1
216
217 if ixt >= start_time and ixt <= finish_time:
218
219 # Activating
220 a = ixt
221 b = min(filter(lambda x: x >= ixt, (aet, axt, iet, finish_time))) - ixt
222
223 draw_box(context, a/10000, y, b/10000, bar_height, 1, 0, 0)
224 drawn = True
225
226 if left < 0:
227 left = a
228
229 if aet >= start_time and aet <= finish_time:
230
231 # Active
232 a = aet
233 b = min(filter(lambda x: x >= aet, (axt, iet, finish_time))) - aet
234
235 draw_box(context, a/10000, y, b/10000, bar_height, .8, .6, .6)
236 drawn = True
237
238 if left < 0:
239 left = a
240
241 if axt >= start_time and axt <= finish_time:
242
243 # Deactivating
244 a = axt
245 b = min(filter(lambda x: x >= axt, (iet, finish_time))) - axt
246
247 draw_box(context, a/10000, y, b/10000, bar_height, .6, .4, .4)
248 drawn = True
249
250 if left < 0:
251 left = a
252
253 if drawn:
254 x = left/10000
255
256 if x < width/2-border:
257 draw_text(context, x + 10, y + bar_height/2, name, hcenter = 0)
258 else:
259 draw_text(context, x - 10, y + bar_height/2, name, hcenter = 1)
260
261 y += bar_height + bar_space
262
263 draw_text(context, 0, height-border*2, "Legend: Red = Activating; Pink = Active; Dark Pink = Deactivating", hcenter = 0, vcenter = -1)
264
265 if initrd_time > 0:
266 draw_text(context, 0, height-border*2 + bar_height, "Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums" % ( \
267 initrd_time/1000, \
268 (start_time - initrd_time)/1000, \
269 (finish_time - start_time)/1000, \
270 finish_time/1000), hcenter = 0, vcenter = -1)
271 else:
272 draw_text(context, 0, height-border*2 + bar_height, "Startup finished in %lums (kernel) + %lums (userspace) = %lums" % ( \
273 start_time/1000, \
274 (finish_time - start_time)/1000, \
275 finish_time/1000), hcenter = 0, vcenter = -1)
276
277 surface.finish()
278
279 def unknown_verb():
280 sys.stderr.write("Unknown verb '%s'.\n" % args[0])
281 usage()
282 sys.exit(1)
283
284 bus = dbus.SystemBus()
285
286 try:
287 opts, args = getopt.gnu_getopt(sys.argv[1:], "h", ["help", "user"])
288 except getopt.GetoptError as err:
289 sys.stdout.write(str(err) + "\n")
290 usage()
291 sys.exit(2)
292 for o, a in opts:
293 if o in ("-h", "--help"):
294 help()
295 elif o == '--user':
296 bus = dbus.SessionBus()
297 else:
298 assert False, "unhandled option"
299
300 verb = {'time' : time,
301 'blame': blame,
302 'plot' : plot,
303 'help' : help,
304 }
305
306 if len(args) == 0:
307 time()
308 else:
309 verb.get(args[0], unknown_verb)()