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