DTrace Profile Program

1
2
dtrace -l -n profile-*
dtrace -x ustackframes=100 -n 'profile-997 /execname == "test"/ { @[ustack()] = count(); }' -o user_stacks.txt -c './test'

gprof2dot

1
2
3
brew install gprof2dot
gprof2dot -f dtrace user_stacks.txt | dot -Tpng -o output.png
gprof2dot -f dtrace user_stacks.txt | dot -Tpdf -o output.pdf

FlameGraph

1
2
brew install flamegraph
stackcollapse.pl user_stacks.txt | flamegraph.pl > pretty-graph.svg

Instruments

Ref: https://help.apple.com/instruments/mac/current/#/devb14ffaa5

1
2
instruments -t "Time Profiler" ./a.out foo.txt 20
instruments -l 30000 -t Time\ Profiler -p 5773

FlameGraphs-Instruments

https://github.com/Kelvenbit/FlameGraphs-Instruments
菜单 Edit > Deep copy ⇧⌘C 复制完整堆栈到剪切板
swift run FlameGraph output.svg

分析系统自带CPU采样格式

macOS sample/活动监视器
sudo sample [pid] -file /dev/stdout | stackcollapse-sample.awk | flamegraph.pl > test.svg
Windows dump/任务管理器
Linux perf?

gperftools

Ref:https://www.cnblogs.com/yangzhouyyz/p/5433757.html
方法有三种:
1、直接调用提供的api:调用函数:ProfilerStart() and ProfilerStop()
2、链接静态库:这种方式是最为常用的方式,后面会有详细的介绍。
方式:在代码link过程中添加参数 –lprofiler
运行程序:env CPUPROFILE=./helloworld.prof ./helloworld
3、链接动态库:env LD_PRELOAD=”/usr/lib/libprofiler.so” (不推荐这种方式)。