chiark / gitweb /
rename machine-id-main.c tomacht the binary and move main.c to core/
[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         if finish_time == 0:
35                 sys.stderr.write("Bootup is not yet finished. Please try again later.\n")
36                 sys.exit(1)
37
38         assert initrd_time <= startup_time
39         assert startup_time <= finish_time
40
41         return initrd_time, startup_time, finish_time
42
43 def draw_box(context, j, k, l, m, r = 0, g = 0, b = 0):
44         context.save()
45         context.set_source_rgb(r, g, b)
46         context.rectangle(j, k, l, m)
47         context.fill()
48         context.restore()
49
50 def draw_text(context, x, y, text, size = 12, r = 0, g = 0, b = 0, vcenter = 0.5, hcenter = 0.5):
51         context.save()
52
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)
56
57         if vcenter or hcenter:
58                 x_bearing, y_bearing, width, height = context.text_extents(text)[:4]
59
60                 if hcenter:
61                         x = x - width*hcenter - x_bearing
62
63                 if vcenter:
64                         y = y - height*vcenter - y_bearing
65
66         context.move_to(x, y)
67         context.show_text(text)
68
69         context.restore()
70
71 def help():
72         sys.stdout.write("""systemd-analyze time
73 systemd-analyze blame
74 systemd-analyze plot
75
76 Process systemd profiling information
77
78   -h --help         Show this help
79 """)
80
81
82 bus = dbus.SystemBus()
83
84 if len(sys.argv) <= 1 or sys.argv[1] == 'time':
85
86         initrd_time, start_time, finish_time = acquire_start_time()
87
88         if initrd_time > 0:
89                 print "Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums" % ( \
90                         initrd_time/1000, \
91                         (start_time - initrd_time)/1000, \
92                         (finish_time - start_time)/1000, \
93                         finish_time/1000)
94         else:
95                 print "Startup finished in %lums (kernel) + %lums (userspace) = %lums" % ( \
96                         start_time/1000, \
97                         (finish_time - start_time)/1000, \
98                         finish_time/1000)
99
100
101 elif sys.argv[1] == 'blame':
102
103         data = acquire_time_data()
104         s = sorted(data, key = lambda i: i[2] - i[1], reverse = True)
105
106         for name, ixt, aet, axt, iet in s:
107
108                 if ixt <= 0 or aet <= 0:
109                         continue
110
111                 if aet <= ixt:
112                         continue
113
114                 sys.stdout.write("%6lums %s\n" % ((aet - ixt) / 1000, name))
115
116 elif sys.argv[1] == 'plot':
117         import cairo, os
118
119         initrd_time, start_time, finish_time = acquire_start_time()
120         data = acquire_time_data()
121         s = sorted(data, key = lambda i: i[1])
122
123         # Account for kernel and initramfs bars if they exist
124         if initrd_time > 0:
125                 count = 3
126         else:
127                 count = 2
128
129         for name, ixt, aet, axt, iet in s:
130
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):
134                         count += 1
135
136         border = 100
137         bar_height = 20
138         bar_space = bar_height * 0.1
139
140         # 1000px = 10s, 1px = 10ms
141         width = finish_time/10000 + border*2
142         height = count * (bar_height + bar_space) + border * 2
143
144         if width < 1000:
145                 width = 1000
146
147         surface = cairo.SVGSurface(sys.stdout, width, height)
148         context = cairo.Context(surface)
149
150         draw_box(context, 0, 0, width, height, 1, 1, 1)
151
152         context.translate(border + 0.5, border + 0.5)
153
154         context.save()
155         context.set_line_width(1)
156         context.set_source_rgb(0.7, 0.7, 0.7)
157
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)
161
162         context.move_to(0, 0)
163         context.line_to(width-border*2, 0)
164
165         context.move_to(0, height-border*2)
166         context.line_to(width-border*2, height-border*2)
167
168         context.stroke()
169         context.restore()
170
171         osrel = "Linux"
172         if os.path.exists("/etc/os-release"):
173                 for line in open("/etc/os-release"):
174                         if line.startswith('PRETTY_NAME='):
175                                 osrel = line[12:]
176                                 osrel = osrel.strip('\"\n')
177                                 break
178
179         banner = "{} {} ({} {}) {}".format(osrel, *(os.uname()[1:5]))
180         draw_text(context, 0, -15, banner, hcenter = 0, vcenter = 1)
181
182         for x in range(0, finish_time/10000 + 100, 100):
183                 draw_text(context, x, -5, "%lus" % (x/100), vcenter = 0, hcenter = 0)
184
185         y = 0
186
187         # draw boxes for kernel and initramfs boot time
188         if initrd_time > 0:
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
192
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
196
197         else:
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
201
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
205
206         for name, ixt, aet, axt, iet in s:
207
208                 drawn = False
209                 left = -1
210
211                 if ixt >= start_time and ixt <= finish_time:
212
213                         # Activating
214                         a = ixt
215                         b = min(filter(lambda x: x >= ixt, (aet, axt, iet, finish_time))) - ixt
216
217                         draw_box(context, a/10000, y, b/10000, bar_height, 1, 0, 0)
218                         drawn = True
219
220                         if left < 0:
221                                 left = a
222
223                 if aet >= start_time and aet <= finish_time:
224
225                         # Active
226                         a = aet
227                         b = min(filter(lambda x: x >= aet, (axt, iet, finish_time))) - aet
228
229                         draw_box(context, a/10000, y, b/10000, bar_height, .8, .6, .6)
230                         drawn = True
231
232                         if left < 0:
233                                 left = a
234
235                 if axt >= start_time and axt <= finish_time:
236
237                         # Deactivating
238                         a = axt
239                         b = min(filter(lambda x: x >= axt, (iet, finish_time))) - axt
240
241                         draw_box(context, a/10000, y, b/10000, bar_height, .6, .4, .4)
242                         drawn = True
243
244                         if left < 0:
245                                 left = a
246
247                 if drawn:
248                         x = left/10000
249
250                         if x < width/2-border:
251                                 draw_text(context, x + 10, y + bar_height/2, name, hcenter = 0)
252                         else:
253                                 draw_text(context, x - 10, y + bar_height/2, name, hcenter = 1)
254
255                         y += bar_height + bar_space
256
257         draw_text(context, 0, height-border*2, "Legend: Red = Activating; Pink = Active; Dark Pink = Deactivating", hcenter = 0, vcenter = -1)
258
259         if initrd_time > 0:
260                 draw_text(context, 0, height-border*2 + bar_height, "Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums" % ( \
261                         initrd_time/1000, \
262                         (start_time - initrd_time)/1000, \
263                         (finish_time - start_time)/1000, \
264                         finish_time/1000), hcenter = 0, vcenter = -1)
265         else:
266                 draw_text(context, 0, height-border*2 + bar_height, "Startup finished in %lums (kernel) + %lums (userspace) = %lums" % ( \
267                         start_time/1000, \
268                         (finish_time - start_time)/1000, \
269                         finish_time/1000), hcenter = 0, vcenter = -1)
270
271         surface.finish()
272 elif sys.argv[1] in ("help", "--help", "-h"):
273         help()
274 else:
275         sys.stderr.write("Unknown verb '%s'.\n" % sys.argv[1])
276         sys.exit(1)