对于系统服务启动问题,systemd自带了一个很好的优化工具,名叫systemd-analyze。它支持很多个子命令,用于满足各种优化需求。最常用的要算blame子命令。使用这个子命令,可以按耗时多少列出所有后台服务,耗时最长的列在最上面。
比如,在幽兰代码本上运行这条命令后,可以看到一张包含111个服务的耗时排行榜。

geduer@ulan:~$ systemd-analyze blame
2.911s NetworkManager-wait-online.service
2.297s logrotate.service
2.047s NetworkManager.service
1.520s snapd.service

超过1秒钟的有上面4个,是值得优化的目标。小于20毫秒的有下面8个,它们是速度标兵。

  19ms systemd-user-sessions.service
  17ms sys-fs-fuse-connections.mount
  16ms ifupdown-pre.service
  14ms sys-kernel-config.mount
  14ms modprobe@dm_mod.service
  11ms plymouth-quit-wait.service
  11ms rtkit-daemon.service
   6ms snapd.socket

当不带任何子命令运行时,可以看到一个启动耗时简报,比如在幽兰上运行的结果如下:
geduer@ulan:~$ systemd-analyze
Startup finished in 3.783s (kernel) + 8.258s (userspace) = 12.042s
graphical.target reached after 7.980s in userspace.
上面结果的含义是Linux系统启动总共用时12秒,其中内核阶段占用3.8秒,用户空间的初始化耗时8.2秒。

附录:完整的耗时排行表(2024-12-10)双剑2镜像

2.911s NetworkManager-wait-online.service
2.297s logrotate.service
2.047s NetworkManager.service
1.520s snapd.service
 906ms dev-nvme0n1p2.device
 756ms systemd-udev-trigger.service
 692ms udisks2.service
 623ms snapd.seeded.service
 617ms blueman-mechanism.service
 564ms dev-loop1.device
 557ms dev-loop5.device
 538ms apport.service
 529ms dev-loop6.device
 526ms dev-loop7.device
 523ms dev-loop2.device
 517ms dev-loop4.device
 515ms dev-loop9.device
 509ms power-profiles-daemon.service
 506ms upower.service
 506ms dev-loop8.device
 495ms dev-loop3.device
 486ms polkit.service
 484ms accounts-daemon.service
 484ms dev-loop0.device
 477ms e2scrub_reap.service
 452ms dev-loop11.device
 438ms dev-loop12.device
 432ms dev-loop10.device
 398ms user@1000.service
 394ms waydroid-container.service
 380ms user@123.service
 365ms systemd-rfkill.service
 356ms rc-local.service
 321ms systemd-logind.service
 314ms apport-autoreport.service
 296ms systemd-fsck@dev-disk-by\x2duuid-4800\x2d9122.service
 252ms systemd-resolved.service
 212ms systemd-journal-flush.service
 210ms systemd-oomd.service
 207ms avahi-daemon.service
 199ms systemd-journald.service
 195ms systemd-timesyncd.service
 190ms networking.service
 184ms ModemManager.service
 177ms rsyslog.service
 175ms snap-bare-5.mount
 171ms snap-core22-1666.mount
 167ms snap-core22-1720.mount
 164ms lm-sensors.service
 162ms snap-core24-493.mount
 157ms snap-core24-612.mount
 154ms snap-cups-1059.mount
 149ms snap-cups-1069.mount
 145ms dpkg-db-backup.service
 144ms snap-gnome\x2d46\x2d2404-41.mount
 142ms switcheroo-control.service
 141ms dbus.service
 140ms snap-gnome\x2d46\x2d2404-49.mount
 134ms snap-gtk\x2dcommon\x2dthemes-1535.mount
 133ms systemd-remount-fs.service
 133ms systemd-tmpfiles-setup.service
 126ms lxc-net.service
 125ms snap-mesa\x2d2404-142.mount
 121ms snap-snapd-21761.mount
 117ms snap-snapd-23259.mount
 115ms lightdm.service
 115ms tmp.mount
 111ms uefi_ext.mount
  95ms systemd-udevd.service
  92ms ssh.service
  89ms systemd-fsck@dev-disk-by\x2duuid-4801\x2d6134.service
  79ms kerneloops.service
  73ms cups.service
  70ms systemd-sysusers.service
  69ms dev-disk-by\x2duuid-5256e7ae\x2d1d0b\x2d4006\x2d8cc0\x2d2cea0900bd03.swap
  67ms wpa_supplicant.service
  62ms dev-hugepages.mount
  59ms dev-mqueue.mount
  57ms modprobe@loop.service
  56ms sys-kernel-debug.mount
  55ms netperf.service
  55ms boot.mount
  55ms modprobe@efi_pstore.service
  54ms openvpn.service
  53ms sys-kernel-tracing.mount
  51ms pangolin.service
  50ms systemd-tmpfiles-setup-dev.service
  45ms systemd-backlight@backlight:backlight.service
  44ms kmod-static-nodes.service
  44ms user-runtime-dir@123.service
  44ms alsa-restore.service
  42ms modprobe@configfs.service
  41ms plymouth-read-write.service
  38ms modprobe@drm.service
  38ms systemd-random-seed.service
  38ms systemd-sysctl.service
  34ms systemd-update-utmp.service
  33ms modprobe@fuse.service
  31ms plymouth-start.service
  31ms systemd-update-utmp-runlevel.service
  27ms systemd-modules-load.service
  26ms lxc.service
  24ms user-runtime-dir@1000.service
  19ms systemd-user-sessions.service
  17ms sys-fs-fuse-connections.mount
  16ms ifupdown-pre.service
  14ms sys-kernel-config.mount
  14ms modprobe@dm_mod.service
  11ms plymouth-quit-wait.service
  11ms rtkit-daemon.service
   6ms snapd.socket
作者:Zhang Yinkui  创建时间:2024-12-10 10:08
最后编辑:Zhang Yinkui  更新时间:2024-12-20 17:01