DragonOS/docs/kernel/debug/profiling-kernel-with-dadk.md
Samuel Dai 153b7a6cb8
feat(CI): Build both platform image without edit configs (#3)
* feat(ci): additional dadk manifest for CI, add container capable rv64 run arg and gendisk cmd

* feat(build): kernel compiling(linking) from diff-arch nolonger needs to make clean

* breaking: use ci-command to run targets, enable both arch to build together

* fix: specify toolchains and the dadk menifest for user program, and add nessesary toolchain. Now riscv64 ver of DragonOS can run into user mode.

* fix(env): cleanup dirty configs, add make clean back

* fix(build): update permission with whoami, and nolonger compile grub in rv64 building.

* feat(ide): support for vscode debuging, using lldb plugin

* feat(ci): automate u-boot download and installation for riscv64
2025-03-18 15:20:54 +08:00

3.3 KiB
Raw Blame History

使用DADK对内核进行性能分析

1. 概述

本文将教你使用DADK对DragonOS内核进行性能分析以识别和解决潜在的性能瓶颈。

1.1 准备工作

::: {note} 在开始之前请确保你已经安装了DADK并且已经配置好了DragonOS内核的编译环境。 :::

1.2 什么是火焰图?

如果你没有听说过火焰图,可以先阅读这篇文章:《如何读懂火焰图?- 阮一峰》

简单的说,火焰图是基于性能采样结果产生的 SVG 图片,用来展示 CPU 的调用栈。

x 轴表示抽样数,如果一个函数在 x 轴占据的宽度越宽就表示它被抽到的次数多即执行的时间长。注意x 轴不代表时间,而是所有的调用栈合并后,按字母顺序排列的。

火焰图就是看顶层的哪个函数占据的宽度最大。只要有"平顶"plateaus就表示该函数可能存在性能问题。

颜色没有特殊含义,因为火焰图表示的是 CPU 的繁忙程度,所以一般选择暖色调。

2. 配置DragonOS内核

由于性能分析需要详尽的符号表数据,因此我们需要在编译内核时,需要进行以下配置:

kernel/Cargo.toml中的[profile.release]部分,设置以下两项:

[profile.release]
debug = true
opt-level = 1

这样,编译出来的内核就会包含符号表数据,方便我们进行性能分析。

3. 使用DADK进行性能分析

3.1 启动内核

首先我们需要启动DragonOS内核。

# 使用你喜欢的方式启动内核,例如:
make run
# 或者
make build && make qemu-nographic

3.2 运行你的工作负载

在启动内核后,我们需要运行一些工作负载,以便进行性能分析。

这可以是一个应用程序也可以是别的东西。甚至你可以什么都不运行只是单纯看看DragonOS内核在空闲时的调用栈情况。

3.3 启动DADK进行性能分析

在DragonOS项目目录下运行以下命令

dadk profile sample --format flamegraph  --output flame.svg --interval 200ms --duration 20s  --cpu-mask 0x1 --kernel bin/x86_64/kernel/kernel.elf

上面的命令将会对DragonOS内核进行性能分析并生成一个火焰图。

详细解释:

  • --format flamegraph:指定输出格式为火焰图。
  • --output flame.svg:指定输出文件名为flame.svg
  • --interval 200ms指定采样间隔为200ms。
  • --duration 20s指定采样时间为20s。
  • --cpu-mask 0x1指定采样的CPU为0号CPU。这是个按位掩码也就是说如果要采样0和1号CPU那么cpu-mask为0x3

更多参数请参考dadk profile sample --help.

::: {note} 由于采样时会暂停vCPU因此采样时间不宜过短否则会影响系统的正常运行。 :::

经过一段时间的等待,你将会得到一个flame.svg文件。

3.4 分析火焰图

使用浏览器打开flame.svg文件,你将会看到一个火焰图。

你可以通过点击火焰图中的某个函数,来查看它的调用栈。

你可以右键下面的图片,在新的标签页打开,体验交互效果。