#!/usr/bin/python
-import dbus, sys
+import sys, os
+import argparse
+from gi.repository import Gio
+try:
+ import cairo
+except ImportError:
+ cairo = None
def acquire_time_data():
-
- manager = dbus.Interface(bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1'), 'org.freedesktop.systemd1.Manager')
+ manager = Gio.DBusProxy.new_for_bus_sync(bus, Gio.DBusProxyFlags.NONE,
+ None, 'org.freedesktop.systemd1', '/org/freedesktop/systemd1', 'org.freedesktop.systemd1.Manager', None)
units = manager.ListUnits()
l = []
if i[5] != "":
continue
- properties = dbus.Interface(bus.get_object('org.freedesktop.systemd1', i[6]), 'org.freedesktop.DBus.Properties')
+ properties = Gio.DBusProxy.new_for_bus_sync(bus, Gio.DBusProxyFlags.NONE,
+ None, 'org.freedesktop.systemd1', i[6], 'org.freedesktop.DBus.Properties', None)
- ixt = int(properties.Get('org.freedesktop.systemd1.Unit', 'InactiveExitTimestampMonotonic'))
- aet = int(properties.Get('org.freedesktop.systemd1.Unit', 'ActiveEnterTimestampMonotonic'))
- axt = int(properties.Get('org.freedesktop.systemd1.Unit', 'ActiveExitTimestampMonotonic'))
- iet = int(properties.Get('org.freedesktop.systemd1.Unit', 'InactiveEnterTimestampMonotonic'))
+ ixt = properties.Get('(ss)', 'org.freedesktop.systemd1.Unit', 'InactiveExitTimestampMonotonic')
+ aet = properties.Get('(ss)', 'org.freedesktop.systemd1.Unit', 'ActiveEnterTimestampMonotonic')
+ axt = properties.Get('(ss)', 'org.freedesktop.systemd1.Unit', 'ActiveExitTimestampMonotonic')
+ iet = properties.Get('(ss)', 'org.freedesktop.systemd1.Unit', 'InactiveEnterTimestampMonotonic')
l.append((str(i[0]), ixt, aet, axt, iet))
return l
def acquire_start_time():
- properties = dbus.Interface(bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1'), 'org.freedesktop.DBus.Properties')
-
- initrd_time = int(properties.Get('org.freedesktop.systemd1.Manager', 'InitRDTimestampMonotonic'))
- startup_time = int(properties.Get('org.freedesktop.systemd1.Manager', 'StartupTimestampMonotonic'))
- finish_time = int(properties.Get('org.freedesktop.systemd1.Manager', 'FinishTimestampMonotonic'))
+ properties = Gio.DBusProxy.new_for_bus_sync(bus, Gio.DBusProxyFlags.NONE,
+ None, 'org.freedesktop.systemd1', '/org/freedesktop/systemd1', 'org.freedesktop.DBus.Properties', None)
+
+ # Note that the firmware/loader times are returned as positive
+ # values but are atcually considered negative from the point
+ # in time of kernel initialization. Also, the monotonic kernel
+ # time will always be 0 since that's where the epoch of the
+ # monotonic clock ist. Since we want to know whether the
+ # kernel time stamp is set at all, we will thus ask for the
+ # realtime clock for this timestamp, instead.
+
+ firmware_time = properties.Get('(ss)', 'org.freedesktop.systemd1.Manager', 'FirmwareTimestampMonotonic')
+ loader_time = properties.Get('(ss)', 'org.freedesktop.systemd1.Manager', 'LoaderTimestampMonotonic')
+ kernel_time = properties.Get('(ss)', 'org.freedesktop.systemd1.Manager', 'KernelTimestamp')
+ initrd_time = properties.Get('(ss)', 'org.freedesktop.systemd1.Manager', 'InitRDTimestampMonotonic')
+ userspace_time = properties.Get('(ss)', 'org.freedesktop.systemd1.Manager', 'UserspaceTimestampMonotonic')
+ finish_time = properties.Get('(ss)', 'org.freedesktop.systemd1.Manager', 'FinishTimestampMonotonic')
if finish_time == 0:
sys.stderr.write("Bootup is not yet finished. Please try again later.\n")
sys.exit(1)
- assert initrd_time <= startup_time
- assert startup_time <= finish_time
+ assert firmware_time >= loader_time
+ assert initrd_time <= userspace_time
+ assert userspace_time <= finish_time
- return initrd_time, startup_time, finish_time
+ return firmware_time, loader_time, kernel_time, initrd_time, userspace_time, finish_time
def draw_box(context, j, k, l, m, r = 0, g = 0, b = 0):
context.save()
context.restore()
-def help():
- sys.stdout.write("""systemd-analyze [--user] time
-systemd-analyze [--user] blame
-systemd-analyze [--user] plot
-
-Process systemd profiling information
-
- -h --help Show this help
-""")
-
+def time():
-bus = dbus.SystemBus()
-command_index = 1
+ firmware_time, loader_time, kernel_time, initrd_time, userspace_time, finish_time = acquire_start_time()
-if len(sys.argv) > 1 and sys.argv[1] == '--user':
- bus = dbus.SessionBus()
- command_index = 2
+ sys.stdout.write("Startup finished in ")
+ if firmware_time > 0:
+ sys.stdout.write("%lums (firmware) + " % ((firmware_time - loader_time) / 1000))
+ if loader_time > 0:
+ sys.stdout.write("%lums (loader) + " % (loader_time / 1000))
+ if initrd_time > 0:
+ sys.stdout.write("%lums (kernel) + %lums (initrd) + " % (initrd_time / 1000, (userspace_time - initrd_time) / 1000))
+ elif kernel_time > 0:
+ sys.stdout.write("%lums (kernel) + " % (userspace_time / 1000))
-if len(sys.argv) <= command_index or sys.argv[command_index] == 'time':
-
- initrd_time, start_time, finish_time = acquire_start_time()
+ sys.stdout.write("%lums (userspace) " % ((finish_time - userspace_time) / 1000))
- if initrd_time > 0:
- print "Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums" % ( \
- initrd_time/1000, \
- (start_time - initrd_time)/1000, \
- (finish_time - start_time)/1000, \
- finish_time/1000)
+ if kernel_time > 0:
+ sys.stdout.write("= %lums\n" % ((firmware_time + finish_time) / 1000))
else:
- print "Startup finished in %lums (kernel) + %lums (userspace) = %lums" % ( \
- start_time/1000, \
- (finish_time - start_time)/1000, \
- finish_time/1000)
+ sys.stdout.write("= %lums\n" % ((finish_time - userspace_time) / 1000))
-
-elif sys.argv[command_index] == 'blame':
+def blame():
data = acquire_time_data()
s = sorted(data, key = lambda i: i[2] - i[1], reverse = True)
sys.stdout.write("%6lums %s\n" % ((aet - ixt) / 1000, name))
-elif sys.argv[command_index] == 'plot':
- import cairo, os
-
- initrd_time, start_time, finish_time = acquire_start_time()
+def plot():
+ if cairo is None:
+ sys.stderr.write("Failed to initilize python-cairo required for 'plot' verb.\n")
+ sys.exit(1)
+ firmware_time, loader_time, kernel_time, initrd_time, userspace_time, finish_time = acquire_start_time()
data = acquire_time_data()
s = sorted(data, key = lambda i: i[1])
for name, ixt, aet, axt, iet in s:
- if (ixt >= start_time and ixt <= finish_time) or \
- (aet >= start_time and aet <= finish_time) or \
- (axt >= start_time and axt <= finish_time):
+ if (ixt >= userspace_time and ixt <= finish_time) or \
+ (aet >= userspace_time and aet <= finish_time) or \
+ (axt >= userspace_time and axt <= finish_time):
count += 1
border = 100
context.set_line_width(1)
context.set_source_rgb(0.7, 0.7, 0.7)
- for x in range(0, finish_time/10000 + 100, 100):
+ for x in range(0, int(finish_time/10000) + 100, 100):
context.move_to(x, 0)
context.line_to(x, height-border*2)
banner = "{} {} ({} {}) {}".format(osrel, *(os.uname()[1:5]))
draw_text(context, 0, -15, banner, hcenter = 0, vcenter = 1)
- for x in range(0, finish_time/10000 + 100, 100):
+ for x in range(0, int(finish_time/10000) + 100, 100):
draw_text(context, x, -5, "%lus" % (x/100), vcenter = 0, hcenter = 0)
y = 0
draw_text(context, 10, y + bar_height/2, "kernel", hcenter = 0)
y += bar_height + bar_space
- draw_box(context, initrd_time/10000, y, start_time/10000-initrd_time/10000, bar_height, 0.7, 0.7, 0.7)
+ draw_box(context, initrd_time/10000, y, userspace_time/10000-initrd_time/10000, bar_height, 0.7, 0.7, 0.7)
draw_text(context, initrd_time/10000 + 10, y + bar_height/2, "initramfs", hcenter = 0)
y += bar_height + bar_space
else:
- draw_box(context, 0, y, start_time/10000, bar_height, 0.6, 0.6, 0.6)
+ draw_box(context, 0, y, userspace_time/10000, bar_height, 0.6, 0.6, 0.6)
draw_text(context, 10, y + bar_height/2, "kernel", hcenter = 0)
y += bar_height + bar_space
- draw_box(context, start_time/10000, y, finish_time/10000-start_time/10000, bar_height, 0.7, 0.7, 0.7)
- draw_text(context, start_time/10000 + 10, y + bar_height/2, "userspace", hcenter = 0)
+ draw_box(context, userspace_time/10000, y, finish_time/10000-userspace_time/10000, bar_height, 0.7, 0.7, 0.7)
+ draw_text(context, userspace_time/10000 + 10, y + bar_height/2, "userspace", hcenter = 0)
y += bar_height + bar_space
for name, ixt, aet, axt, iet in s:
drawn = False
left = -1
- if ixt >= start_time and ixt <= finish_time:
+ if ixt >= userspace_time and ixt <= finish_time:
# Activating
a = ixt
if left < 0:
left = a
- if aet >= start_time and aet <= finish_time:
+ if aet >= userspace_time and aet <= finish_time:
# Active
a = aet
if left < 0:
left = a
- if axt >= start_time and axt <= finish_time:
+ if axt >= userspace_time and axt <= finish_time:
# Deactivating
a = axt
if initrd_time > 0:
draw_text(context, 0, height-border*2 + bar_height, "Startup finished in %lums (kernel) + %lums (initramfs) + %lums (userspace) = %lums" % ( \
initrd_time/1000, \
- (start_time - initrd_time)/1000, \
- (finish_time - start_time)/1000, \
+ (userspace_time - initrd_time)/1000, \
+ (finish_time - userspace_time)/1000, \
finish_time/1000), hcenter = 0, vcenter = -1)
else:
draw_text(context, 0, height-border*2 + bar_height, "Startup finished in %lums (kernel) + %lums (userspace) = %lums" % ( \
- start_time/1000, \
- (finish_time - start_time)/1000, \
+ userspace_time/1000, \
+ (finish_time - userspace_time)/1000, \
finish_time/1000), hcenter = 0, vcenter = -1)
surface.finish()
-elif sys.argv[command_index] in ("help", "--help", "-h"):
- help()
+
+parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
+ description='Process systemd profiling information',
+ epilog='''\
+time - print time spent in the kernel before reaching userspace
+blame - print list of running units ordered by time to init
+plot - output SVG graphic showing service initialization
+''')
+
+parser.add_argument('action', choices=('time', 'blame', 'plot'),
+ default='time', nargs='?',
+ help='action to perform (default: time)')
+parser.add_argument('--user', action='store_true',
+ help='use the session bus')
+
+args = parser.parse_args()
+
+if args.user:
+ bus = Gio.BusType.SESSION
else:
- sys.stderr.write("Unknown verb '%s'.\n" % sys.argv[command_index])
- sys.exit(1)
+ bus = Gio.BusType.SYSTEM
+
+verb = {'time' : time,
+ 'blame': blame,
+ 'plot' : plot,
+ }
+verb.get(args.action)()