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

如何在MacOS系统上为C程序生成火焰图?

在MacOS上生成代码火焰图的可行方法

方法一:使用py-spy(跨平台采样工具)

py-spy是无侵入式的采样工具,支持MacOS,无需修改目标代码:

  • 安装:brew install py-spy
  • 针对运行中的进程生成火焰图:
    py-spy record --rate 1000 --output flamegraph.svg --pid <你的进程ID>
  • 直接启动应用并采样:
    py-spy record --rate 1000 --output flamegraph.svg ./your-application
  • 生成的flamegraph.svg可直接用浏览器打开查看。

方法二:Instruments + 火焰图转换脚本

解决你之前用Instruments未成功的问题,需配合火焰图工具集的脚本:

  1. 打开Instruments,选择Time Profiler模板,启动你的应用并采集性能数据
  2. 采集完成后,点击右上角「Export」,选择导出为CSV格式
  3. 获取stackcollapse-instruments.pl和flamegraph.pl脚本,放到本地目录
  4. 转换CSV为折叠栈格式:
    ./stackcollapse-instruments.pl instruments_export.csv > folded.stacks
  5. 生成火焰图:
    ./flamegraph.pl folded.stacks > flamegraph.svg

方法三:dtrace + 火焰图工具

利用Mac原生的dtrace工具采集栈信息,再转成火焰图:

  1. 编写dtrace采样脚本(命名为profile.d):
    #!/usr/sbin/dtrace -s
    
    profile-997
    /pid == $target/
    {
        @[ustack()] = count();
    }
    
    END
    {
        trunc(@, 200);
        printa("%@d %S\n", @);
    }
    
  2. 赋予脚本执行权限:chmod +x profile.d
  3. 采集目标进程数据:
    sudo ./profile.d -p <你的进程ID> > dtrace_output.out
  4. 用stackcollapse-dtrace.pl转换栈信息:
    ./stackcollapse-dtrace.pl dtrace_output.out > folded.stacks
  5. 生成火焰图:
    ./flamegraph.pl folded.stacks > flamegraph.svg

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 16:57:10