Improving performance/Boot process (简体中文)
本文将为读者提供数种加速系统启动的方法。通过学习实践这些方法,读者不仅能改善系统性能,还能学习系统启动脚本的知识。
Contents
启动过程分析
使用 systemd-analyze
systemd 提供了一个分析启动过程的工具 —— systemd-analyze
。可以用它看看哪些单元拖慢了开机过程,并据此进行优化。
查看开机过程在内核/用户空间消耗的时间:
$ systemd-analyze
按照耗费时间顺序,输出启动每个单元耗费的时间:
$ systemd-analyze blame
在开机过程的一些时刻, 需要特定的单元成功启动了才能继续. 查看在启动链中哪些单元处于这种危急时刻, 可以:
$ systemd-analyze critical-chain
生成类似于 bootchart 的开机过程图表:
$ systemd-analyze plot > plot.svg
使用 systemd-bootchart
自2012年10月17日,bootchart 工具已经合并进 systemd 中,使用方法和原来的 bootchart 大同小异,添加下列内容到内核参数即可:
initcall_debug printk.time=y init=/usr/lib/systemd/systemd-bootchart
更多信息请查看 manpage
使用 bootchart2
由于没有办法在内核参数设置两个 init,所以不能使用源里的 bootchart。不过,AUR 软件包 bootchart2-gitAUR 提供了一个的 systemd 服务,这样就可以和 systemd 一起使用。安装后启用即可:
# systemctl enable bootchart.service
详情参阅 bootchart 文档。
自己编译内核
自己编译内核、关闭不需要的功能,恐怕是加速系统启动的最有效方法了。 更多信息参见:Kernel Compilation From Source
Early start for services
One central feature of systemd is D-Bus and socket activation. This causes services to be started when they are first accessed and is generally a good thing. However, if you know that a service (like UPower) will always be started during boot, then the overall boot time might be reduced by starting it as early as possible. This can be achieved (if the service file is set up for it, which in most cases it is) by issuing:
# systemctl enable upower
This will cause systemd to start UPower as soon as possible, without causing races with the socket or D-Bus activation.
Staggered spin-up
有些硬件使用staggered spin-up,操作系统一个一个访问硬盘,以减少耗电。这会降低启动速度,大部分用户都不需要开启。检查是否开启:
$ dmesg | grep SSS
如果没有查到,表示未启动。如果有显示,可以将libahci.ignore_sss=1
加入 kernel line 进行禁用。
避免重复挂载
mkinitcpio提供了 fsck
钩子,将启动加载配置中的 root 从 ro
修改为 rw
并删除 /etc/fstab
中的 root 挂载,可以避免重复挂载。挂载参数可以通过rootflags=[mount options...]
设置。
删除 /etc/fstab
中的 API 文件系统,systemd 会自动挂载它们。下面命令可以获得这些 API 文件系统的列表:
$ pacman -Ql systemd | grep '\.mount$'
/home
等其他文件系统可以通过自定义挂载单元进行挂载。
精简输出信息
修改启动加载器内核参数中的 verbose
为 quiet
即可。对于某些用户,特别是 SSD 用户,TTY 的龟速实际上成为了性能瓶颈,精简输出信息实际上有利于提高性能。
参阅
- e4rat
- udev
- Daemon
- mkinitcpio
- Improving performance
- Systemd - An alternative init process
- Bootchart - A tool to assist in profiling the boot process
- Kexec A tool to reboot very quickly without waiting for the whole BIOS boot process to finish.