4. 安装Bootchart:基于Debian/Ubuntu的apt安装、基于Yocto/OpenEmbedded的集成
Bootchart 的安装方式取决于目标平台。在 x86 开发环境或快速原型验证阶段,使用 Debian/Ubuntu 的包管理器是最直接的方式;而在嵌入式 Linux 产品开发中,通过 Yocto/OpenEmbedded 构建系统将 Bootchart 集成到根文件系统才是标准做法。本章将分别介绍这两种安装路径,并对比其适用场景。
apt 安装;如果你在构建嵌入式系统镜像(如 Raspberry Pi、i.MX、STM32MP1 等),请跳至 Yocto 集成部分。
4.1 基于 Debian/Ubuntu 的 apt 安装
在 Debian/Ubuntu 及其衍生发行版中,Bootchart 及相关工具已进入官方软件源。安装过程分为两个步骤:安装用户空间工具和启用启动时采集服务。
4.1.1 安装 bootchart 包
# 更新软件包索引
sudo apt update
# 安装 bootchart 和 bootchart2(后者为社区维护版本,功能更丰富)
sudo apt install -y bootchart bootchart2
安装完成后,系统会同时提供 bootchart 和 bootchartd 两个命令。前者用于将采集到的原始日志渲染为 SVG 图表,后者是启动时采集守护进程。
4.1.2 启用 bootchart 服务
在基于 systemd 的系统中,Bootchart 通过一个 systemd service 单元在启动早期运行。你需要手动启用并配置它:
# 启用 bootchart 服务(在下次启动时自动采集)
sudo systemctl enable bootchart
# 查看服务状态
sudo systemctl status bootchart
默认情况下,采集数据会写入 /var/log/bootchart.tgz。你可以通过修改 /etc/default/bootchart 配置文件来调整采样参数:
# 编辑配置文件
sudo vim /etc/default/bootchart
# 常用配置项示例
# 采样间隔(毫秒),默认 200ms
BOOTCHART_INTERVAL=100
# 采集持续时间(秒),默认 30s
BOOTCHART_DURATION=60
# 输出文件路径
BOOTCHART_OUTPUT=/var/log/bootchart.tgz
4.1.3 重启并生成图表
# 重启系统
sudo reboot
# 重启后,生成 SVG 图表
bootchart /var/log/bootchart.tgz
# 生成的图表默认位于当前目录:bootchart.svg
/var/log/bootchart.tgz,请检查 systemd 服务是否成功启动:journalctl -u bootchart。常见原因是内核启动参数中未正确传递 init=/lib/systemd/systemd-bootchart,或 initramfs 阶段未加载 bootchart 模块。
4.2 基于 Yocto/OpenEmbedded 的集成
在嵌入式 Linux 开发中,Yocto 项目是构建自定义 Linux 发行版的事实标准。将 Bootchart 集成到 Yocto 镜像中,需要修改配方(recipe)和本地配置。
4.2.1 添加 bootchart 配方层
Yocto 的 meta-openembedded 层中包含了 bootchart 的配方。首先确保你的构建环境已包含该层:
# 克隆 meta-openembedded 层(如果尚未添加)
git clone git://git.openembedded.org/meta-openembedded
# 在 bblayers.conf 中添加该层
# 编辑 conf/bblayers.conf,加入:
BBLAYERS += " /path/to/meta-openembedded/meta-oe "
4.2.2 在镜像配方中集成 bootchart
在您的自定义镜像配方(例如 recipes-core/images/my-image.bb)中,将 bootchart 添加到 IMAGE_INSTALL 变量:
# 在 my-image.bb 中添加
IMAGE_INSTALL += " \
bootchart \
bootchart2 \
${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'systemd-bootchart', '', d)} \
"
如果您的系统使用 systemd,建议同时添加 systemd-bootchart 包,它提供了与 systemd 深度集成的启动采集功能。
4.2.3 配置内核启动参数
在 Yocto 中,通过 APPEND 变量或内核配置片段来添加启动参数。编辑您的 local.conf 或机器配置:
# 在 conf/local.conf 中添加
APPEND += " init=/lib/systemd/systemd-bootchart"
# 或者通过内核配置片段启用 bootchart 支持
# 创建文件 recipes-kernel/linux/linux-yocto/bootchart.cfg
# 内容如下:
CONFIG_BOOTCHART=y
CONFIG_BOOTCHART_DAEMON=y
4.2.4 构建并验证
# 重新构建镜像
bitbake my-image
# 烧录镜像到目标板并启动
# 启动后,采集数据位于 /var/log/bootchart.tgz
# 在宿主机上渲染图表(需要安装 bootchart 工具)
bootchart /path/to/bootchart.tgz
- 确保
meta-oe层已添加到bblayers.conf。 - 如果使用 systemd,优先选择
systemd-bootchart包。 - 内核配置中必须启用
CONFIG_BOOTCHART和CONFIG_BOOTCHART_DAEMON。 - 启动参数
init=/lib/systemd/systemd-bootchart是触发采集的关键。
4.3 两种安装方式对比
| 对比维度 | Debian/Ubuntu apt 安装 | Yocto/OpenEmbedded 集成 |
|---|---|---|
| 适用平台 | x86/amd64 桌面或服务器 | 嵌入式 ARM、RISC-V、MIPS 等 |
| 安装复杂度 | 低,一条命令即可 | 中高,需要配置层和配方 |
| 定制灵活性 | 低,依赖发行版打包 | 高,可自定义采样参数和内核配置 |
| 依赖管理 | 自动处理 | 手动管理层依赖 |
| 典型使用场景 | 快速原型验证、开发调试 | 产品级嵌入式系统开发 |
4.4 Bootchart 安装与数据流架构
下图展示了从安装到数据采集、再到图表生成的完整数据流,帮助理解 Bootchart 在系统中的位置和作用:
4.5 常见问题与排查
- apt 安装后未生成日志文件: 检查
systemctl status bootchart是否显示 active (running)。如果服务未启动,手动执行sudo systemctl start bootchart并重启。 - Yocto 构建失败: 确认
meta-oe层版本与你的 Yocto 发行版(如 kirkstone、dunfell)兼容。运行bitbake-layers show-recipes bootchart验证配方是否被解析。 - 嵌入式目标上 bootchart 数据为空: 检查内核是否启用了
CONFIG_BOOTCHART。在目标板上执行zcat /proc/config.gz | grep BOOTCHART确认。 - 图表渲染失败: 确保宿主机上安装了
bootchart工具,并且 Java 运行时环境(JRE)可用(部分版本依赖 Java 渲染)。