chiark / gitweb /
journal: automaticall reset location when the set of matches changes
[elogind.git] / src / systemd-analyze
1 #!/usr/bin/python
2
3 import dbus, sys
4
5 def acquire_time_data():
6
7         manager = dbus.Interface(bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1'), 'org.freedesktop.systemd1.Manager')
8         units = manager.ListUnits()
9
10         l = []
11
12         for i in units:
13                 if i[5] != "":
14                         continue
15
16                 properties = dbus.Interface(bus.get_object('org.freedesktop.systemd1', i[6]), 'org.freedesktop.DBus.Properties')
17
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'))
22
23                 l.append((str(i[0]), ixt, aet, axt, iet))
24
25         return l
26
27 def acquire_start_time():
28         properties = dbus.Interface(bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1'), 'org.freedesktop.DBus.Properties')
29
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'))
33
34         assert initrd_time <= startup_time
35         assert startup_time <= finish_time
36
37         return initrd_time, startup_time, finish_time
38
39 def draw_box(context, j, k, l, m, r = 0, g = 0, b = 0):
40         context.save()
41         context.set_source_rgb(r, g, b)
42         context.rectangle(j, k, l, m)
43         context.fill()
44         context.restore()
45
46 def draw_text(context, x, y, text, size = 12, r = 0, g = 0, b = 0, vcenter = 0.5, hcenter = 0.5):
47         context.save()
48
49         context.set_source_rgb(r, g, b)
50         context.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
51         context.set_font_size(size)
52
53         if vcenter or hcenter:
54                 x_bearing, y_bearing, width, height = context.text_extents(text)[:4]
55
56                 if hcenter:
57                         x = x - width*hcenter - x_bearing
58
59                 if vcenter:
60                         y = y - height*vcenter - y_bearing
61
62         context.move_to(x, y)
63         context.show_text(text)
64
65         context.restore()
66
67 def help():
68         sys.stdout.write("""systemd-analyze time
69 systemd-analyze blame
70 systemd-analyze plot
71
72 Process systemd profiling information
73
74   -h --help         Show this help
75 """)
76
77
78 bus = dbus.SystemBus()
79
80 if len(sys.argv) <= 1 or sys.argv[1] == 'time':
81
82         initrd_time, start_time, finish_time = acquire_start_time()
83
84         if initrd_time > 0:
85                 print "Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums" % ( \
86                         initrd_time/1000, \
87                         (start_time - initrd_time)/1000, \
88                         (finish_time - start_time)/1000, \
89                         finish_time/1000)
90         else:
91                 print "Startup finished in %lums (kernel) + %lums (userspace) = %lums" % ( \
92                         start_time/1000, \
93                         (finish_time - start_time)/1000, \
94                         finish_time/1000)
95
96
97 elif sys.argv[1] == 'blame':
98
99         data = acquire_time_data()
100         s = sorted(data, key = lambda i: i[2] - i[1], reverse = True)
101
102         for name, ixt, aet, axt, iet in s:
103
104                 if ixt <= 0 or aet <= 0:
105                         continue
106
107                 if aet <= ixt:
108                         continue
109
110                 sys.stdout.write("%6lums %s\n" % ((aet - ixt) / 1000, name))
111
112 elif sys.argv[1] == 'plot':
113         import cairo, os
114
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, 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         banner = "Running on %s (%s %s) %s" % (os.uname()[1], os.uname()[2], os.uname()[3], os.uname()[4])
168         draw_text(context, 0, -15, banner, hcenter = 0, vcenter = 1)
169
170         for x in range(0, finish_time/10000 + 100, 100):
171                 draw_text(context, x, -5, "%lus" % (x/100), vcenter = 0, hcenter = 0)
172
173         y = 0
174
175         # draw boxes for kernel and initramfs boot time
176         if initrd_time > 0:
177                 draw_box(context, 0, y, initrd_time/10000, bar_height, 0.7, 0.7, 0.7)
178                 draw_text(context, 10, y + bar_height/2, "kernel", hcenter = 0)
179                 y += bar_height + bar_space
180
181                 draw_box(context, initrd_time/10000, y, start_time/10000-initrd_time/10000, bar_height, 0.7, 0.7, 0.7)
182                 draw_text(context, initrd_time/10000 + 10, y + bar_height/2, "initramfs", hcenter = 0)
183                 y += bar_height + bar_space
184
185         else:
186                 draw_box(context, 0, y, start_time/10000, bar_height, 0.6, 0.6, 0.6)
187                 draw_text(context, 10, y + bar_height/2, "kernel", hcenter = 0)
188                 y += bar_height + bar_space
189
190         draw_box(context, start_time/10000, y, finish_time/10000-start_time/10000, bar_height, 0.7, 0.7, 0.7)
191         draw_text(context, start_time/10000 + 10, y + bar_height/2, "userspace", hcenter = 0)
192         y += bar_height + bar_space
193
194         for name, ixt, aet, axt, iet in s:
195
196                 drawn = False
197                 left = -1
198
199                 if ixt >= start_time and ixt <= finish_time:
200
201                         # Activating
202                         a = ixt
203                         b = min(filter(lambda x: x >= ixt, (aet, axt, iet, finish_time))) - ixt
204
205                         draw_box(context, a/10000, y, b/10000, bar_height, 1, 0, 0)
206                         drawn = True
207
208                         if left < 0:
209                                 left = a
210
211                 if aet >= start_time and aet <= finish_time:
212
213                         # Active
214                         a = aet
215                         b = min(filter(lambda x: x >= aet, (axt, iet, finish_time))) - aet
216
217                         draw_box(context, a/10000, y, b/10000, bar_height, .8, .6, .6)
218                         drawn = True
219
220                         if left < 0:
221                                 left = a
222
223                 if axt >= start_time and axt <= finish_time:
224
225                         # Deactivating
226                         a = axt
227                         b = min(filter(lambda x: x >= axt, (iet, finish_time))) - axt
228
229                         draw_box(context, a/10000, y, b/10000, bar_height, .6, .4, .4)
230                         drawn = True
231
232                         if left < 0:
233                                 left = a
234
235                 if drawn:
236                         x = left/10000
237
238                         if x < width/2-border:
239                                 draw_text(context, x + 10, y + bar_height/2, name, hcenter = 0)
240                         else:
241                                 draw_text(context, x - 10, y + bar_height/2, name, hcenter = 1)
242
243                         y += bar_height + bar_space
244
245         draw_text(context, 0, height-border*2, "Legend: Red = Activating; Pink = Active; Dark Pink = Deactivating", hcenter = 0, vcenter = -1)
246
247         if initrd_time > 0:
248                 draw_text(context, 0, height-border*2 + bar_height, "Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums" % ( \
249                         initrd_time/1000, \
250                         (start_time - initrd_time)/1000, \
251                         (finish_time - start_time)/1000, \
252                         finish_time/1000), hcenter = 0, vcenter = -1)
253         else:
254                 draw_text(context, 0, height-border*2 + bar_height, "Startup finished in %lums (kernel) + %lums (userspace) = %lums" % ( \
255                         start_time/1000, \
256                         (finish_time - start_time)/1000, \
257                         finish_time/1000), hcenter = 0, vcenter = -1)
258
259         surface.finish()
260 elif sys.argv[1] in ("help", "--help", "-h"):
261         help()
262 else:
263         sys.stderr.write("Unknown verb '%s'.\n" % sys.argv[1])
264         sys.exit(1)