chiark / gitweb /
localectl: fix dbus call arguments in set_x11_keymap
[elogind.git] / src / analyze / systemd-analyze
1 #!/usr/bin/python
2
3 import sys, os
4 import argparse
5 from gi.repository import Gio
6 try:
7         import cairo
8 except ImportError:
9         cairo = None
10
11 def acquire_time_data():
12         manager = Gio.DBusProxy.new_for_bus_sync(bus, Gio.DBusProxyFlags.NONE,
13                 None, 'org.freedesktop.systemd1', '/org/freedesktop/systemd1', 'org.freedesktop.systemd1.Manager', None)
14         units = manager.ListUnits()
15
16         l = []
17
18         for i in units:
19                 if i[5] != "":
20                         continue
21
22                 properties = Gio.DBusProxy.new_for_bus_sync(bus, Gio.DBusProxyFlags.NONE,
23                         None, 'org.freedesktop.systemd1', i[6], 'org.freedesktop.DBus.Properties', None)
24
25                 ixt = properties.Get('(ss)', 'org.freedesktop.systemd1.Unit', 'InactiveExitTimestampMonotonic')
26                 aet = properties.Get('(ss)', 'org.freedesktop.systemd1.Unit', 'ActiveEnterTimestampMonotonic')
27                 axt = properties.Get('(ss)', 'org.freedesktop.systemd1.Unit', 'ActiveExitTimestampMonotonic')
28                 iet = properties.Get('(ss)', 'org.freedesktop.systemd1.Unit', 'InactiveEnterTimestampMonotonic')
29
30                 l.append((str(i[0]), ixt, aet, axt, iet))
31
32         return l
33
34 def acquire_start_time():
35         properties = Gio.DBusProxy.new_for_bus_sync(bus, Gio.DBusProxyFlags.NONE,
36                 None, 'org.freedesktop.systemd1', '/org/freedesktop/systemd1', 'org.freedesktop.DBus.Properties', None)
37
38         initrd_time = properties.Get('(ss)', 'org.freedesktop.systemd1.Manager', 'InitRDTimestampMonotonic')
39         userspace_time = properties.Get('(ss)', 'org.freedesktop.systemd1.Manager', 'UserspaceTimestampMonotonic')
40         finish_time = properties.Get('(ss)', 'org.freedesktop.systemd1.Manager', 'FinishTimestampMonotonic')
41
42         if finish_time == 0:
43                 sys.stderr.write("Bootup is not yet finished. Please try again later.\n")
44                 sys.exit(1)
45
46         assert initrd_time <= userspace_time
47         assert userspace_time <= finish_time
48
49         return initrd_time, userspace_time, finish_time
50
51 def draw_box(context, j, k, l, m, r = 0, g = 0, b = 0):
52         context.save()
53         context.set_source_rgb(r, g, b)
54         context.rectangle(j, k, l, m)
55         context.fill()
56         context.restore()
57
58 def draw_text(context, x, y, text, size = 12, r = 0, g = 0, b = 0, vcenter = 0.5, hcenter = 0.5):
59         context.save()
60
61         context.set_source_rgb(r, g, b)
62         context.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
63         context.set_font_size(size)
64
65         if vcenter or hcenter:
66                 x_bearing, y_bearing, width, height = context.text_extents(text)[:4]
67
68                 if hcenter:
69                         x = x - width*hcenter - x_bearing
70
71                 if vcenter:
72                         y = y - height*vcenter - y_bearing
73
74         context.move_to(x, y)
75         context.show_text(text)
76
77         context.restore()
78
79 def time():
80
81         initrd_time, start_time, finish_time = acquire_start_time()
82
83         if initrd_time > 0:
84                 sys.stdout.write("Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums\n" % ( \
85                         initrd_time/1000, \
86                         (start_time - initrd_time)/1000, \
87                         (finish_time - start_time)/1000, \
88                         finish_time/1000))
89         else:
90                 sys.stdout.write("Startup finished in %lums (kernel) + %lums (userspace) = %lums\n" % ( \
91                         start_time/1000, \
92                         (finish_time - start_time)/1000, \
93                         finish_time/1000))
94
95
96 def blame():
97
98         data = acquire_time_data()
99         s = sorted(data, key = lambda i: i[2] - i[1], reverse = True)
100
101         for name, ixt, aet, axt, iet in s:
102
103                 if ixt <= 0 or aet <= 0:
104                         continue
105
106                 if aet <= ixt:
107                         continue
108
109                 sys.stdout.write("%6lums %s\n" % ((aet - ixt) / 1000, name))
110
111 def plot():
112         if cairo is None:
113                 sys.stderr.write("Failed to initilize python-cairo required for 'plot' verb.\n")
114                 sys.exit(1)
115         initrd_time, start_time, finish_time = acquire_start_time()
116         data = acquire_time_data()
117         s = sorted(data, key = lambda i: i[1])
118
119         # Account for kernel and initramfs bars if they exist
120         if initrd_time > 0:
121                 count = 3
122         else:
123                 count = 2
124
125         for name, ixt, aet, axt, iet in s:
126
127                 if (ixt >= start_time and ixt <= finish_time) or \
128                                 (aet >= start_time and aet <= finish_time) or \
129                                 (axt >= start_time and axt <= finish_time):
130                         count += 1
131
132         border = 100
133         bar_height = 20
134         bar_space = bar_height * 0.1
135
136         # 1000px = 10s, 1px = 10ms
137         width = finish_time/10000 + border*2
138         height = count * (bar_height + bar_space) + border * 2
139
140         if width < 1000:
141                 width = 1000
142
143         surface = cairo.SVGSurface(sys.stdout, width, height)
144         context = cairo.Context(surface)
145
146         draw_box(context, 0, 0, width, height, 1, 1, 1)
147
148         context.translate(border + 0.5, border + 0.5)
149
150         context.save()
151         context.set_line_width(1)
152         context.set_source_rgb(0.7, 0.7, 0.7)
153
154         for x in range(0, int(finish_time/10000) + 100, 100):
155                 context.move_to(x, 0)
156                 context.line_to(x, height-border*2)
157
158         context.move_to(0, 0)
159         context.line_to(width-border*2, 0)
160
161         context.move_to(0, height-border*2)
162         context.line_to(width-border*2, height-border*2)
163
164         context.stroke()
165         context.restore()
166
167         osrel = "Linux"
168         if os.path.exists("/etc/os-release"):
169                 for line in open("/etc/os-release"):
170                         if line.startswith('PRETTY_NAME='):
171                                 osrel = line[12:]
172                                 osrel = osrel.strip('\"\n')
173                                 break
174
175         banner = "{} {} ({} {}) {}".format(osrel, *(os.uname()[1:5]))
176         draw_text(context, 0, -15, banner, hcenter = 0, vcenter = 1)
177
178         for x in range(0, int(finish_time/10000) + 100, 100):
179                 draw_text(context, x, -5, "%lus" % (x/100), vcenter = 0, hcenter = 0)
180
181         y = 0
182
183         # draw boxes for kernel and initramfs boot time
184         if initrd_time > 0:
185                 draw_box(context, 0, y, initrd_time/10000, bar_height, 0.7, 0.7, 0.7)
186                 draw_text(context, 10, y + bar_height/2, "kernel", hcenter = 0)
187                 y += bar_height + bar_space
188
189                 draw_box(context, initrd_time/10000, y, start_time/10000-initrd_time/10000, bar_height, 0.7, 0.7, 0.7)
190                 draw_text(context, initrd_time/10000 + 10, y + bar_height/2, "initramfs", hcenter = 0)
191                 y += bar_height + bar_space
192
193         else:
194                 draw_box(context, 0, y, start_time/10000, bar_height, 0.6, 0.6, 0.6)
195                 draw_text(context, 10, y + bar_height/2, "kernel", hcenter = 0)
196                 y += bar_height + bar_space
197
198         draw_box(context, start_time/10000, y, finish_time/10000-start_time/10000, bar_height, 0.7, 0.7, 0.7)
199         draw_text(context, start_time/10000 + 10, y + bar_height/2, "userspace", hcenter = 0)
200         y += bar_height + bar_space
201
202         for name, ixt, aet, axt, iet in s:
203
204                 drawn = False
205                 left = -1
206
207                 if ixt >= start_time and ixt <= finish_time:
208
209                         # Activating
210                         a = ixt
211                         b = min(filter(lambda x: x >= ixt, (aet, axt, iet, finish_time))) - ixt
212
213                         draw_box(context, a/10000, y, b/10000, bar_height, 1, 0, 0)
214                         drawn = True
215
216                         if left < 0:
217                                 left = a
218
219                 if aet >= start_time and aet <= finish_time:
220
221                         # Active
222                         a = aet
223                         b = min(filter(lambda x: x >= aet, (axt, iet, finish_time))) - aet
224
225                         draw_box(context, a/10000, y, b/10000, bar_height, .8, .6, .6)
226                         drawn = True
227
228                         if left < 0:
229                                 left = a
230
231                 if axt >= start_time and axt <= finish_time:
232
233                         # Deactivating
234                         a = axt
235                         b = min(filter(lambda x: x >= axt, (iet, finish_time))) - axt
236
237                         draw_box(context, a/10000, y, b/10000, bar_height, .6, .4, .4)
238                         drawn = True
239
240                         if left < 0:
241                                 left = a
242
243                 if drawn:
244                         x = left/10000
245
246                         if x < width/2-border:
247                                 draw_text(context, x + 10, y + bar_height/2, name, hcenter = 0)
248                         else:
249                                 draw_text(context, x - 10, y + bar_height/2, name, hcenter = 1)
250
251                         y += bar_height + bar_space
252
253         draw_text(context, 0, height-border*2, "Legend: Red = Activating; Pink = Active; Dark Pink = Deactivating", hcenter = 0, vcenter = -1)
254
255         if initrd_time > 0:
256                 draw_text(context, 0, height-border*2 + bar_height, "Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums" % ( \
257                         initrd_time/1000, \
258                         (start_time - initrd_time)/1000, \
259                         (finish_time - start_time)/1000, \
260                         finish_time/1000), hcenter = 0, vcenter = -1)
261         else:
262                 draw_text(context, 0, height-border*2 + bar_height, "Startup finished in %lums (kernel) + %lums (userspace) = %lums" % ( \
263                         start_time/1000, \
264                         (finish_time - start_time)/1000, \
265                         finish_time/1000), hcenter = 0, vcenter = -1)
266
267         surface.finish()
268
269 parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
270                                  description='Process systemd profiling information',
271                                  epilog='''\
272 time - print time spent in the kernel before reaching userspace
273 blame - print list of running units ordered by time to init
274 plot - output SVG graphic showing service initialization
275 ''')
276
277 parser.add_argument('action', choices=('time', 'blame', 'plot'),
278                     default='time', nargs='?',
279                     help='action to perform (default: time)')
280 parser.add_argument('--user', action='store_true',
281                     help='use the session bus')
282
283 args = parser.parse_args()
284
285 if args.user:
286         bus = Gio.BusType.SESSION
287 else:
288         bus = Gio.BusType.SYSTEM
289
290 verb = {'time' : time,
291         'blame': blame,
292         'plot' : plot,
293         }
294 verb.get(args.action)()