chiark / gitweb /
unit: disallow configuration of more than one on_failure dependencies if OnFailureIso...
[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', 'InactiveExitTimestamp'))
19                 aet = int(properties.Get('org.freedesktop.systemd1.Unit', 'ActiveEnterTimestamp'))
20                 axt = int(properties.Get('org.freedesktop.systemd1.Unit', 'ActiveExitTimestamp'))
21                 iet = int(properties.Get('org.freedesktop.systemd1.Unit', 'InactiveEnterTimestamp'))
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         startup_time = int(properties.Get('org.freedesktop.systemd1.Manager', 'StartupTimestamp'))
31         finish_time = int(properties.Get('org.freedesktop.systemd1.Manager', 'FinishTimestamp'))
32
33         assert startup_time <= finish_time
34
35         return startup_time, finish_time
36
37 def draw_box(context, j, k, l, m, r = 0, g = 0, b = 0):
38         context.save()
39         context.set_source_rgb(r, g, b)
40         context.rectangle(j, k, l, m)
41         context.fill()
42         context.restore()
43
44 def draw_text(context, x, y, text, size = 12, r = 0, g = 0, b = 0, vcenter = 0.5, hcenter = 0.5):
45         context.save()
46
47         context.set_source_rgb(r, g, b)
48         context.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
49         context.set_font_size(size)
50
51         if vcenter or hcenter:
52                 x_bearing, y_bearing, width, height = context.text_extents(text)[:4]
53
54                 if hcenter:
55                         x = x - width*hcenter - x_bearing
56
57                 if vcenter:
58                         y = y - height*vcenter - y_bearing
59
60         context.move_to(x, y)
61         context.show_text(text)
62
63         context.restore()
64
65 def help():
66         sys.stdout.write("""systemd-analyze blame
67 systemd-analyze plot
68
69 Process systemd profiling information
70
71   -h --help         Show this help
72 """)
73
74
75 bus = dbus.SystemBus()
76
77 if len(sys.argv) <= 1 or sys.argv[1] == 'blame':
78
79         data = acquire_time_data()
80         s = sorted(data, key = lambda i: i[2] - i[1], reverse = True)
81
82         for name, ixt, aet, axt, iet in s:
83
84                 if ixt <= 0 or aet <= 0:
85                         continue
86
87                 if aet <= ixt:
88                         continue
89
90                 sys.stdout.write("%6lums %s\n" % ((aet - ixt) / 1000, name))
91
92 elif sys.argv[1] == 'plot':
93         import cairo
94
95         start_time, finish_time = acquire_start_time()
96         data = acquire_time_data()
97         s = sorted(data, key = lambda i: i[1])
98
99         count = 0
100
101         for name, ixt, aet, axt, iet in s:
102
103                 if (ixt >= start_time and ixt <= finish_time) or \
104                                 (aet >= start_time and aet <= finish_time) or \
105                                 (axt >= start_time and axt <= finish_time):
106                         count += 1
107
108         border = 100
109         bar_height = 20
110         bar_space = bar_height * 0.1
111
112         # 1000px = 10s, 1px = 10ms
113         width = (finish_time - start_time)/10000 + border*2
114         height = count * (bar_height + bar_space) + border * 2
115
116         if width < 1000:
117                 width = 1000
118
119         surface = cairo.SVGSurface(sys.stdout, width, height)
120         context = cairo.Context(surface)
121
122         draw_box(context, 0, 0, width, height, 1, 1, 1)
123
124         context.translate(border + 0.5, border + 0.5)
125
126         context.save()
127         context.set_line_width(1)
128         context.set_source_rgb(0.7, 0.7, 0.7)
129
130         for x in range(0, (finish_time - start_time)/10000, 100):
131                 context.move_to(x, 0)
132                 context.line_to(x, height-border*2)
133
134         context.move_to(0, 0)
135         context.line_to(width-border*2, 0)
136
137         context.move_to(0, height-border*2)
138         context.line_to(width-border*2, height-border*2)
139
140         context.stroke()
141         context.restore()
142
143         for x in range(0, (finish_time - start_time)/10000, 100):
144                 draw_text(context, x, -5, "%lus" % (x/100), vcenter = 0, hcenter = 0)
145
146         y = 0
147
148         for name, ixt, aet, axt, iet in s:
149
150                 drawn = False
151                 left = -1
152
153                 if ixt >= start_time and ixt <= finish_time:
154
155                         # Activating
156                         a = ixt - start_time
157                         b = min(filter(lambda x: x >= ixt, (aet, axt, iet, finish_time))) - ixt
158
159                         draw_box(context, a/10000, y, b/10000, bar_height, 1, 0, 0)
160                         drawn = True
161
162                         if left < 0:
163                                 left = a
164
165                 if aet >= start_time and aet <= finish_time:
166
167                         # Active
168                         a = aet - start_time
169                         b = min(filter(lambda x: x >= aet, (axt, iet, finish_time))) - aet
170
171                         draw_box(context, a/10000, y, b/10000, bar_height, .8, .6, .6)
172                         drawn = True
173
174                         if left < 0:
175                                 left = a
176
177                 if axt >= start_time and axt <= finish_time:
178
179                         # Deactivating
180                         a = axt - start_time
181                         b = min(filter(lambda x: x >= axt, (iet, finish_time))) - axt
182
183                         draw_box(context, a/10000, y, b/10000, bar_height, .6, .4, .4)
184                         drawn = True
185
186                         if left < 0:
187                                 left = a
188
189                 if drawn:
190                         x = left/10000
191
192                         if x < width/2-border:
193                                 draw_text(context, x + 10, y + bar_height/2, name, hcenter = 0)
194                         else:
195                                 draw_text(context, x - 10, y + bar_height/2, name, hcenter = 1)
196
197                         y += bar_height + bar_space
198
199         draw_text(context, 0, height-border*2, "Legend: Red = Activating; Pink = Active; Dark Pink = Deactivating", hcenter = 0, vcenter = -1)
200
201         surface.finish()
202 elif sys.argv[1] in ("help", "--help", "-h"):
203         help()
204 else:
205         sys.stderr.write("Unknown verb '%s'.\n" % sys.argv[1])
206         sys.exit(1)