Rewind确定性VM解析libuv构建关键路径

Rewind确定性VM解析libuv构建关键路径。数据显示,即使使用64核,构建仍耗时47秒,其中67%时间处于休眠等待。该工具通过精确重现执行过程,帮助开发者定位难以复现的Bug并优化性能。

当面对无限可能性的问题时,将解决方案限制在一组有限的特征中往往令人感到束缚。然而,像 Rewind 这样的决定性虚拟机(Deterministic VM)正在打破这一局限,为开发者带来前所未有的调试与优化体验。

libuv构建中67%时间处于休眠

Rewind 的核心价值在于其能够精确重现执行过程,帮助开发者定位难以复现的“海森 bug”(Heisenbug),并使配置文件导向的优化具备可重复性。其中,最具突破性的功能是对程序表现的分析,特别是通过关键路径(Critical Path)来理解构建过程。

什么是关键路径?

关键路径是指程序中依赖关系最长的工作链。无论投入多少计算资源,这条链条决定了程序完成所需的最小时间。因此,加速程序的唯一有效途径就是缩短关键路径。

对于熟悉布伦丹·格雷格(Brendan Gregg)工作的开发者而言,他多年来致力于将性能分析从一种“黑暗艺术”转变为科学方法。Rewind 的功能可以被视为对格雷格理念的一种致敬与延伸——它类似于格雷格的火焰图(Flame Graph),但结合了关键路径分析和调试器附加功能,提供了更深层的洞察。

另一方面:重新翻页?你只能抓到一次,然后是决定性的 VM?

案例分析:libuv 构建中的时间流逝

为了展示 Rewind 的工作原理,我们分析了 libuv 的一次构建和测试运行。即使在使用 64 个核心的情况下,该过程仍耗时 47 秒。传统的 CPU 配置文件在此场景下几乎毫无用处,因为在 47 秒中有三分之二的时间,构建进程并非在进行计算,而是在休眠等待非计算任务。

时间在哪里流逝

每个 Rewind 记录都提供两个核心指标:

$ 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 都无法使执行时间短于关键路径,因此这两个数值之间的差距代表了并行处理带来的性能增益。

数据下方的行应用了格雷格的线程状态分析方法。数据显示,最大的状态开销来自睡眠:epoll_pwait 耗时 23.1 秒,clock_nanosleep 耗时 8.53 秒。

休眠线程在传统 CPU 配置文件中是不可见的。格雷格提出的解决方案是非 CPU 分析,即关注线程阻塞的位置,而不仅仅是计算位置。在实时机器上,这种分析极具挑战性,通常需要在慢速运行发生前进行跟踪,并记录每一次上下文切换以便重播。而在 Rewind 中,所有这些信息都已包含在录音中。

以下是 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

第二个表格展示了基于格雷格殖民地图概念的过程树路径。每一行代表一个进程及其启动命令,以及在关键路径上消耗的时间。与传统 ps 快照可能遗漏短暂进程调用不同,Rewind 记录了每一次分叉,无论其持续时间多短。

在本次运行中,检查阶段(check phase)占据了 47.4 秒中的 41.9 秒,其中 36.4 秒用于运行测试。测试运行器调用了 287 个测试,每个测试耗时 1-3 秒。所有这些测试都位于单一的关键路径链条上,且大部分时间都在睡眠等待计时器。

最后一个表格回答了另一个问题:如果某个部分不占用时间,整体运行速度会提升多少?这种方法被称为“拖动”(Drag)分析。

Rewind 的强大之处在于允许用户跳转到路径的任何部分查看具体发生了什么。传统分析工具提供的堆栈信息往往是静态的,而 Rewind 可以重播录音中的每一个时刻。

通过指定 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% 的时间处于睡眠状态,最后一行指示了起始位置。通过重播到该步骤并使用 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 毫秒,总共耗时 3.38 秒。这表明测试是有目的的,旨在检查 UV_hr 时间在睡眠期间的移动距离,在此情况下并无优化空间。

步骤 115,894 处的指令在每次重播中都是确定的。用户可以在该步骤打开 gdb 进行任意调查。

Rewind 应用程序的时间选项卡也呈现相同的数据。关键路径被可视化为火焰图,颜色编码遵循格雷格的非 CPU 火焰图标准:计算部分为暖色,睡眠部分为蓝色,进程根据睡眠时长在两者之间变色。

点击图表中的元素可选择该项并将播放头移动到对应时间点,从而同步显示构建日志、进程树和文件内容。再次点击进程可将其放大。当选择一个蓝色(睡眠)区域时,应用程序会重播到该部分最长的睡眠时段,并显示堆栈信息:

在堆栈下方,“Show source”按钮可在源面板中打开相关代码,“Open gdb”则在运行时分叉处打开 gdb:

对于偏好现有工具的用户,Rewind 支持以标准格式导出数据,包括适用于 flamegraph.pl 和 Speedscope 的折叠堆栈,以及适用于 ui.perfetto.dev 的 Perfetto 跟踪文件。

技术背景与影响

Rewind 中的许多核心理念早于该产品本身。布伦丹·格雷格已绘制 CPU 和唤醒火焰图十余年,构建系统也早已报告关键路径信息。例如,Basel 的关键路径分析已存在多年。自 eBPF 首次接触以来,这些工作一直启发着性能分析领域的发展。

关键在于数据的完整性。由于缺乏这些数据,传统方法无法实现完全的重播。在单 CPU 环境下,运行间唯一的变量是调度器,因此每条记录都已捕获了所有的上下文切换。

这改变了性能分析的经济学模型。在实时机器上,追踪必须在运行前开始,且伴随成本,用户只能选择追踪少数几条路径。而在 Rewind 中,每次运行都被完整记录,允许事后进行任意分析。

通过将多年的理论构想应用于决定性虚拟机,这些方法以一种在普通机器上难以或不可能实现的方式发挥作用。Rewind Time 的出现,标志着性能分析工具向更高精度和可重复性迈出的重要一步。

评论 0

0/500

评论需审核后展示,请文明发言

💬
还没有评论,来说两句

相关阅读