AIFeed

在 Brendan Gregg 的祭坛前朝拜

Translation(model translation, not the source's own text)

文章标题:在 Brendan Gregg 的祭坛前朝拜

正文: 好吧,我第一个承认我对 AI 有点狂热。当你正在处理一个具有无限可能性的问题时,很难将自己限制在一组有限的功能上。

我能再加一个吗?

Brendan Gregg 双眼呈黑白螺旋状,站在一个巨大的紫色催眠螺旋前
Brendan Gregg 双眼呈黑白螺旋状,站在一个巨大的紫色催眠螺旋前

我一直对像 Rewind 这样的确定性虚拟机所能实现的种种有趣且有用的功能感到惊叹。我一直在用它帮助寻找海森堡 bug,使配置文件引导优化可重现,以及让 Nix 构建实现位级可重现。但最有趣的是用它来理解性能。

事实证明,性能是 Rewind 大放异彩的又一个领域。我一直在用它来分析构建的关键路径。

什么是关键路径?

关键路径是程序中依赖关系最长的链条。它是程序能够完成所需的最短时间,无论你投入多少资源。让程序变快的唯一方法就是缩短关键路径。

如果你在 Brendan Gregg 的祭坛前朝拜,你就会知道他用多年时间将性能分析从一门黑暗艺术变成了一门科学。这是献给他的一点小小心意。🤲

我在 Rewind 中添加了一个新的 rewind time 命令,它分析一次录制,告诉你时间花在了哪里,以及什么能让运行更早完成。你可以深入查看答案的任何部分,并看到对应的源代码行。这就像 Brendan Gregg 的火焰图,但针对关键路径进行了强化,并且附带了调试器。11这个命令的所有想法要么受到 Brendan Gregg 工作的启发,要么直接受其影响。在 Zod 面前跪下!

旁注:刚接触 Rewind?从一次你只抓到过一次的不稳定构建开始,以及 dafuq 什么是确定性虚拟机?

让我们通过分析一次 libuv 构建和测试运行的关键路径来看看它是如何工作的。构建和测试套件在 Rewind 中耗时 78 秒,即使使用 64 核仍然需要 47 秒。为什么?

通常的答案是查看 CPU 性能分析,但在这种情况下,CPU 性能分析几乎毫无用处。在那 47 秒中有三分之二的时间,构建所等待的工作根本不在计算:它在休眠。💤💤💤

§时间花在了哪里

每次 rewind time 都以两个数字开始:


        $ rewind time 7a29e716
libuv-1.51.0
    78.0 s  elapsed on the VM's one CPU    47.4 s  critical path: the least it can take on any number of CPUs            67% asleep (epoll_pwait 23.1 s, clock_nanosleep 8.53 s, futex 100 ms), 33% computing
      

第一个数字是运行在虚拟机的一个虚拟 CPU 上花费的时间。第二个是关键路径:最长的工作链,其中每一步都必须等待前一步完成。

再多的 CPU 也无法让运行时间短于其关键路径,因此两个数字之间的差距就是并行化能为你带来的全部收益。

Drake 表情包。Drake 转过头不看“买一台 64 核构建机器”,并赞许地指向“先读关键路径”。
Drake 表情包。Drake 转过头不看“买一台 64 核构建机器”,并赞许地指向“先读关键路径”。

两个数字下面那行应用了 Gregg 的线程状态分析。这里最大的状态是休眠:23 秒在 epoll_pwait 中,8.5 秒在 clock_nanosleep 中。

休眠的线程对 CPU 性能分析器是不可见的。Gregg 的答案是 CPU 外分析:查看线程阻塞的位置,而不仅仅是它们计算的位置。

在实时机器上,CPU 外分析很困难。Linux 可以追踪哪个线程唤醒了哪个线程,Gregg 在此基础上构建了唤醒分析和唤醒后分析,但你必须要在慢速运行发生之前就开始追踪,追踪有成本,而且你捕获的大多数休眠都是噪声。Rewind 已经记录了每次上下文切换,因为它需要这些来回放,现在它还记录了每次唤醒以及是谁触发的。所有这些 juicy 的信息都在每次录制中。🤤

以下是 libuv 的完整输出:


        $ rewind time 7a29e716
libuv-1.51.0
    78.0 s  elapsed on the VM's one CPU
    47.4 s  critical path: the least it can take on any number of CPUs
            67% asleep (epoll_pwait 23.1 s, clock_nanosleep 8.53 s, futex 100 ms), 33% computing

The critical path by phase
    3.54 s  configurePhase  ██
    1.67 s  buildPhase      █
    42.0 s  checkPhase      █████████████████████
     99 ms  5 other phases

The critical path by process
   on path      pid  process, with what it started
    47.4 s       34  init › bash -e source-stdenv.sh default-builder.sh
    41.9 s     4076  ├─ make -j8 SHELL=bash-5.3p3/bin/bash VERBOSE=y check
    36.4 s     4910  │  ├─ make › bash › lt-run-tests
    3.38 s     5412  │  │  ├─ lt-run-tests hrtime hrtime  asleep in clock_nanosleep 3.38 s
    2.00 s     5013  │  │  ├─ lt-run-tests delayed_accept delayed_accept  asleep in epoll_pwait 2.00 s
    1.62 s     5126  │  │  ├─ lt-run-tests fs_event_watch_dir fs_event_watch_dir  asleep in epoll_pwait 1.62 s
    1.60 s     5103  │  │  ├─ lt-run-tests fs_event_close_in_callback fs_event_close_in…  asleep in epoll_pwait 1.60 s
    1.60 s     5508  │  │  ├─ lt-run-tests metrics_idle_time metrics_idle_time  asleep in epoll_pwait 1.00 s, computing 600 ms
    1.24 s     6095  │  │  ├─ lt-run-tests tcp_write_ready tcp_write_ready
    1.14 s     5510  │  │  ├─ lt-run-tests metrics_idle_time_thread metrics_idle_time_t…  asleep in epoll_pwait 1.00 s
    1.10 s     5132  │  │  ├─ lt-run-tests fs_event_watch_file_current_dir fs_event_wat…  asleep in epoll_pwait 1.10 s
    22.6 s           │  │  └─ 279 more
    5.59 s     4077  │  └─ make test/run-tests
    2.22 s     4183  │     ├─ gcc -fPIC -fstack-clash-protection -O2 -U_FORTIFY_SOURCE …
    2.10 s     4189  │     │  └─ cc1 /build/source/test/test-fs.c
    3.35 s           │     └─ 20 more
    1.99 s       68  ├─ sh ./autogen.sh
    1.99 s           │  └─ 9 more
    1.67 s     2642  ├─ make -j8 SHELL=bash-5.3p3/bin/bash
    1.67 s           │  └─ 5 more
    1.53 s      403  └─ bash ./configure --disable-static --disable-dependency-tracking…
    1.47 s              └─ 581 more

What would shorten it
    sooner      pid  if this took no time
    36.1 s           every lt-run-tests, 600 processes
    6.66 s           every cc1, 283 processes
    3.38 s     5412  75 sleeps in clock_nanosleep: lt-run-tests hrtime hrtime
    2.00 s     5013  2 sleeps in epoll_pwait: lt-run-tests delayed_accept delayed_accept
    1.76 s     4189  cc1 /build/source/test/test-fs.c
    1.62 s     5126  30 sleeps in epoll_pwait: lt-run-tests fs_event_watch_dir fs_event_watc…
    1.60 s     5103  16 sleeps in epoll_pwait: lt-run-tests fs_event_close_in_callback fs_ev…
    1.60 s     5508  lt-run-tests metrics_idle_time metrics_idle_time
    1.22 s           every perl, 9 processes
    1.14 s     5510  lt-run-tests metrics_idle_time_thread metrics_idle_time_thread

Look closer
  rewind time 7a29e716 --in PID             one process: its threads, sleeps and waits
  rewind time 7a29e716 --folded             the path for flamegraph.pl or speedscope.app
  rewind time 7a29e716 --perfetto F         every process on a timeline, for ui.perfetto.dev
      

第二张表把这条路径表示为一棵进程树,基于 Gregg 的 colony graph 想法。每一行是一个进程及其启动的所有东西,以及它在关键路径上的时间。22A ps 快照可能会漏掉短命的进程调用;记录中包含每一次 fork,无论多么短暂。

在这次构建中,make check 需要总 47.4 秒中的 41.9 秒,其中 36.4 秒是 lt-run-tests,也就是 libuv 的测试运行器。这个测试运行器调用了 287 个测试,每个花费 1 到 3 秒。

所有这些测试都在路径上,而路径是一条单链,所以运行器一个接一个地运行它们。更糟的是,每个测试的大部分时间都在休眠,等待定时器。

Drag 可以小于路径上的时间。

最后一张表回答的是另一个问题:如果这部分不耗时间,这次运行会提前多少完成?关键路径方法把这一项称为 drag。

真正神奇的部分在于,我们可以跳到路径上的任意位置,看看当时在发生什么,调用 gdb 并在那一刻把运行分叉出去。profiler 交给你的只是一个栈,采样自某个你再也回不去的时刻。而在 rewind time 里,每一帧都是录制中的一个时刻,Rewind 可以把运行重放到那里。

--in 深入单个进程内部,33它可以接受 pid、程序名或命令行中的某个词。那么我们就来看看那个在 clock_nanosleep 中睡了 3.38 秒的 hrtime 测试:


        $ rewind time 7a29e716 --in hrtime
lt-run-tests hrtime hrtime (pid 5412)
    3.38 s  on the critical path with what it started, 7% of the 47.4 s path
            100% asleep (clock_nanosleep 3.38 s), <1% computing

What would shorten it
    sooner      pid  if this took no time
    3.38 s           75 sleeps in clock_nanosleep

Look closer
  rewind where 7a29e716 115894 --pid 5412   the source line the first row's time starts at  rewind time 7a29e716 --perfetto F         every process on a timeline
      

由于这个测试 100% 的时间都在睡眠,最后一行给出的那个 step 就是它开始的地方。rewind where 会把运行重放到那个 step,并向 gdb 询问线程当时在哪里:


        $ rewind where 7a29e716 115894 --pid 5412
process 5412 (lt-run-tests), thread 5412, at step 115894
#2 uv_sleep (src/unix/core.c:1869)
    1867
    1868    do
>   1869      rc = nanosleep(&timeout, &timeout);    1870    while (rc == -1 && errno == EINTR);
    1871
arguments:
  msec = 45
locals:
  timeout = {tv_sec = 0, tv_nsec = 45000000}
  rc = <optimized out>
  __PRETTY_FUNCTION__ = <error: Cannot access memory at address 0x7fb172519ae8>
called from #3 run_test_hrtime (test/test-hrtime.c:39)
called from #4 __libc_start_call_main (../sysdeps/nptl/libc_start_call_main.h:58)
      

🎯 可以看到,该测试在 test/test-hrtime.c 的第 39 行调用 uv_sleep(45),并且它睡 45 毫秒。它这样做了 75 次,总计 3.38 秒。44原来这个测试是有意睡眠的:它在检查 uv_hrtime 在一次睡眠中前进了多少。这种情况下没有什么可优化的。

这是「Is this a pigeon」meme:一个标注为「我」的人指着一只标注为「75 次 uv_sleep(45) 调用」的蝴蝶,问道「这是 CPU 瓶颈吗?」
这是「Is this a pigeon」meme:一个标注为「我」的人指着一只标注为「75 次 uv_sleep(45) 调用」的蝴蝶,问道「这是 CPU 瓶颈吗?」

锦上添花的是:这次运行是确定性的,所以无论你重放多少次,step 115,894 都是同一条指令。你可以在这个 step 打开 rewind gdb,尽情地调查。

Rewind 应用的 Time 标签页给出的也是同样的答案。路径变成了一张火焰图,配色沿用 Gregg 给他的 off-CPU 火焰图上色的方式:计算是暖色,睡眠是蓝色,而一个进程会按它睡眠时间所占的比例在两者之间渐变。

Rewind 应用针对 libuv 的 Time 标签页:在虚拟机的单个 CPU 上已过去 78.0 s,关键路径为 47.4 s,其中 67% 处于睡眠。其下方依次是:按顺序排列成条状的路径、check 阶段、以火焰图呈现且 lt-run-tests 的各个测试排成一行的路径,以及「什么能缩短它」,其中每个 lt-run-tests 以 36.1 s 排在最前,每一行都按其帧的颜色标记
Rewind 应用针对 libuv 的 Time 标签页:在虚拟机的单个 CPU 上已过去 78.0 s,关键路径为 47.4 s,其中 67% 处于睡眠。其下方依次是:按顺序排列成条状的路径、check 阶段、以火焰图呈现且 lt-run-tests 的各个测试排成一行的路径,以及「什么能缩短它」,其中每个 lt-run-tests 以 36.1 s 排在最前,每一行都按其帧的颜色标记

点击一帧即可选中它并把播放头移到那里,于是构建日志、进程树和文件都会显示那一刻的状态;再次点击某个进程则会放大到该进程。当你选中一个蓝色的帧时,应用会把运行重放到该部分最长的一次睡眠,并显示它睡眠时所处的栈:

Time 标签页放大到 lt-run-tests,选中了 hrtime 测试的 sleep。卡片上写着在 clock_nanosleep 中睡眠 3.38 秒,共 75 次睡眠,最长的一次为 45 毫秒,来自线程 5412 上的步骤 115,923,以及其最长睡眠发生的位置:libc.so.6 中的两个栈帧,然后是 src/unix/core.c:1869 处的 uv_sleep,nanosleep(&timeout, &timeout),由 test/test-hrtime.c:39 处的 run_test_hrtime 调用,uv_sleep(45)
Time 标签页放大到 lt-run-tests,选中了 hrtime 测试的 sleep。卡片上写着在 clock_nanosleep 中睡眠 3.38 秒,共 75 次睡眠,最长的一次为 45 毫秒,来自线程 5412 上的步骤 115,923,以及其最长睡眠发生的位置:libc.so.6 中的两个栈帧,然后是 src/unix/core.c:1869 处的 uv_sleep,nanosleep(&timeout, &timeout),由 test/test-hrtime.c:39 处的 run_test_hrtime 调用,uv_sleep(45)

在堆栈下方,Show source 会在该线程的那个时刻打开源码面板,而 Open gdb here 会在该次运行的一个 fork 中、于该 sleep 处打开 gdb:

应用终端窗格中的 gdb,位于步骤 115,923、线程 5412:第 2 帧是 src/unix/core.c:1869 处 msec=45 的 uv_sleep,print msec 输出 45,而 up 显示 test/test-hrtime.c:39 处的 run_test_hrtime,uv_sleep(45)
应用终端窗格中的 gdb,位于步骤 115,923、线程 5412:第 2 帧是 src/unix/core.c:1869 处 msec=45 的 uv_sleep,print msec 输出 45,而 up 显示 test/test-hrtime.c:39 处的 run_test_hrtime,uv_sleep(45)

如果你更愿意使用已经熟悉的工具,rewind time 可以按这些工具的格式输出数据:用于 flamegraph.pl 和 speedscope 的 folded stacks,或用于 ui.perfetto.dev 的 Perfetto trace。

下面的火焰图是真正的 flamegraph.pl 输出:点击一个帧即可放大,或单独打开它。

§跪在祭坛前

这里的每一个想法都早于 Rewind。Gregg 绘制 off-CPU 和 wakeup 火焰图已经十多年了,而构建系统报告关键路径信息的时间甚至可能更久。55Bazel 的关键路径分析就是一个很好的例子,而且它已经存在多年。自从我第一次接触 eBPF 以来,我就一直受到他工作的启发,那时火焰图还很新鲜。🙇

Rewind 增加的是数据。大量的数据。

Rewind 拥有这些数据,因为没有它就无法重放。在单个 CPU 上,每次运行之间唯一变化的是调度器,所以每一份录制都已经记录了每一次上下文切换。

这改变了性能分析的经济学。在真实机器上,你必须在运行前进行追踪,而追踪是有成本的。你只能追踪少数几次运行,而且必须挑选哪几次。Rewind 追踪每一次运行,因此你之后可以分析其中任意一次,而且能精确地分析。

把多年来我欣赏的想法,指向一台确定性 VM,并看它们以在普通机器上难以或不可能实现的方式运作,这真是一种狂热高烧般的快乐。

请接受 rewind time 作为我在 Brendan Gregg 祭坛前的献礼。

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by Lobsters
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗