You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从macOS进程采样生成FlameGraph火焰图?

macOS 进程采样转 FlameGraph 方案

一、修复原工具链的问题

你遇到的报错是因为新版 macOS 的sample命令输出格式有变化,旧版stackcollapse-sample.py无法正确解析。可以通过以下步骤解决:

  1. 重新生成采样文件,加上-noHeaders参数去掉冗余表头:
sudo sample PID -file ~/tmp/sample.txt -fullPaths 1 -noHeaders
  1. 使用最新版的stackcollapse-sample.py(从 FlameGraph 官方仓库获取的版本)处理采样文件:
python stackcollapse-sample.py ~/tmp/sample.txt > ~/tmp/sample_collapsed.txt
  1. 生成火焰图:
flamegraph.pl ~/tmp/sample_collapsed.txt > ~/tmp/sample_flamegraph.svg

如果仍有无效行报错,先过滤掉非调用栈内容再处理:

sed -n '/Call graph:/,$p' ~/tmp/sample.txt | grep -v '^\s*$' > ~/tmp/sample_cleaned.txt
python stackcollapse-sample.py ~/tmp/sample_cleaned.txt > ~/tmp/sample_collapsed.txt

二、更简便的替代工具(推荐)

用py-spy可以跳过手动处理采样文件的步骤,一键生成适配新版 macOS 的火焰图:

  1. 安装 py-spy:
brew install py-spy
  1. 对目标进程采样并生成火焰图:
sudo py-spy record -o ~/tmp/flamegraph.svg --pid PID

等待几秒后按Ctrl+C停止采样,直接得到可打开的 SVG 火焰图。

若需采样非 Python 进程,也可使用async-profiler(需配合 JDK,支持原生进程),但py-spy是最简洁的通用方案之一。

内容的提问来源于stack exchange,提问作者ylluminate

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 11:55:46