pro*_*eek 16 c c++ macos profiling gprof
我试图使用命令行工具在Mac OS X配置文件的C/C++代码,我使用-pg
与选项gcc
来运行gprof
在Linux上,但我似乎无法找到gprof
在Mac上,即使我有这个页面的说明:其他命令行工具(iOS)或其他命令行工具(mac).
gprof:Produces execution profiles based on an execution analysis of a program.
Run Code Online (Sandbox Code Playgroud)
我安装的命令行工具,所以其他的命令行工具,如otool
和atos
可用.我用谷歌搜索这个页面(https://apple.stackexchange.com/questions/154289/installing-gprof-on-mac),说不gprof
支持,但我不确定我什么时候有一个描述该工具的Apple doc ; 无论如何,我试图用来brew
下载gprof
,但它没有用.
我发现尝试在Mac上使用带有C++代码的gprof,但我没有输出instruments -t
.我还在mac os x上发现了profiling c ++,但我不想打开Instruments,因为我想自动化一些进程并尝试保持跨平台系统.
gprof
?听到gprof
OSX 没有分析器很奇怪.OSX是经过认证的unix和unix的分析器gprof
(基于profil
系统调用/库函数):https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man2/profil.2.html).
根据https://apple.stackexchange.com/questions/154289/installing-gprof-on-mac(2014 ;感谢Sreekanth Nagareddy用户删除的答案),GNU gprof(binutils的一部分)存在问题,brew install -v binutils
"***以下子目录不支持此配置:.. ld gas gprof"; OSX未在GNU gprof自述文件中列出:http://code.metager.de/source/xref/gnu/src/gprof/README (2012)在"支持的平台"中(仅限OSF/1,SunOS,Solaris,HP- UX列出;我认为它应该适用于Hurd并适用于Linux).
但是也有gprof的BSD实现(请查看https://en.wikipedia.org/wiki/gprof了解历史和参考资料).没有尝试让它在OSX上运行(没有OSX,也没有咬过比1995桌面和笔记本更新的苹果).
有不同的BSD gprof来源,例如,FreeBSD的版本(https://github.com/freebsd/freebsd/tree/af3e10e5a78d3af8cef6088748978c6c612757f0/usr.bin/gprof)或古老的4.3BSD原版http://www.retro11.de/ouxr/43bsd/usr/src/ucb/gprof /.两种变体都不支持OSX中使用的Mach-O格式.
在Darwin的cctools中甚至还有Apple自己的gprof(基于来自NetBSD/OpenBSD的BSD gprof)(Darwin是UNIX部分OSX的内核和用户空间;它是/是/将是开源的):https:// github .com/LeoTestard/Darwin/tree/master/cctools/gprof/https://github.com/darwin-on-arm/darwin-sdk/tree/master/cctools/gprof/http://src.gnu-darwin .org/src/usr.bin/gprof/gprof.c.html(一些较旧的FreeBSD代码组合和GNU疯狂的自由思想).
gprof的可用性可能取决于确切的OSX版本或Xcode版本/包; 根据http://louise.hu/poet/gprof-on-osx/或2012年的某些版本,有10.6.1的gprof - https://rachelbythebay.com/w/2012/09/14/mac /甚至在2001年:http://lists.apple.com/archives/darwin-development/2001/Apr/msg00617.html
instruments
在命令行中有使用(Xcode Tools的一部分?)的变体,不知道具体如何,但是知道这些乐器是现代的,功能丰富的探查器.
还有一个iprofiler
命令行界面来收集配置文件Instruments.app
,只需注明它的手册页https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/iprofiler.1.html(部分Xcode Tools 5.0版;来自网站遗留部分的手册页).
有第三方分析器,声明支持OSX.我知道其中两个:valgrind和gperftools(google-perftools).
valgrind不是探查者; 它是一个(慢速)动态检测平台,其上构建了许多工具.它包括两个工具,能够分析:callgrind
和cachegrind
.Valgrind和这两个工具都不是本机分析器,它们不会分析应用程序,因为它将在现实生活中的真实CPU上运行.相反,valgrind在虚拟机上执行程序,callgrind/cachegrind仪器用计数器执行机器代码.
callgrind(http://valgrind.org/docs/manual/cl-manual.html)使用每个线性指令块的计数器来计算"每条指令将执行多少次"("Ir"事件,用于获取配置文件 -按所用时间百分比排序函数); 它还记录调用/返回以构建调用图."Ir"事件计数对于获得指令执行计数是正确的(它也可以模拟分支预测); 但它不能用于估计实际运行时间.真正的cpu(高性能cpu称为超标量;乱序cpus也是超标量)能够在每个CPU时钟周期内执行多条指令; 并且它通常也无法执行任何指令,因为它们可能需要启动一些数据(来自远程缓存或来自内存或来自系统调用或来自其他高延迟指令的数据;或cpu误预测分支导致指令地址尚未读取/解码).大多数渐进式cpus甚至可能不会执行某些命令(有些命令可以执行nop
每个周期最多8" ,几个英特尔的Sandy/Ivy Bridges和更新版本不会花费任何时间在" xor eax,eax
" 上将0写入寄存器;它们只是重新映射下一个寄存器使用归零物理寄存器).与硬件CPU上的实际运行相比,callgrind对分析运行的典型减速为10-20.
Cachegrind实现与callgrind("Ir",分支)相同的检测,但也可以模拟缓存层次结构(缓存加载/存储/未命中事件).它比callgrind慢.
callgrind和cachegrind的输出可以使用GUI工具kcachegrind(http://kcachegrind.sourceforge.net/,它可以在OS中工作)或命令行工具查看callgrind_annotate
.
其他工具是gperftools(google-perftools,https://github.com/gperftools/gperftools),它在真实的CPU上运行程序.要使用它,请使用自制软件安装它,然后使用libprofiler(add -Lpath_to_installed_gperftools -lprofiler
)链接程序,并使用CPUPROFILE
设置为某个文件名的环境变量运行(CPUPROFILE=profile01 ./the_program). It will profile the program using interval timer (
setitimer ) and output profiling data to the filename, defined in
CPUPROFILE env var. Then you can view profile data in command-line or with svg/web browser using
pprof perl script from gperftools (
pprof ./the_program profile01`).
如何使用或不使用 gprof 从命令行进行分析?
这似乎适用于 MacOS Monterey 12.5
xctrace record --output <path/to/folder> --template "Time Profiler" --time-limit 10s --attach <pid>
Run Code Online (Sandbox Code Playgroud)
结果将是一个 .trace 文件,您可以使用它打开
open path/to/name-of-file.trace
Run Code Online (Sandbox Code Playgroud)
如果您想启动应用程序而不是附加到现有应用程序,请使用--launch -- command arugments
. --launch
必须是最后一个参数
了解更多信息
man xctrace
Run Code Online (Sandbox Code Playgroud)
noɥ*_*ɐɹƆ -1
在我的一个单独问题(CrazyPython)上,我能够pprof
在@osgx的帮助下使用gperftools()。这是问题,这里是脚本的 GitHub 要点。为了您的方便,这里是内联脚本:
#!/usr/bin/env bash
# Licensed under the Unlicense. Full text at (http://unlicense.org/) - CrazyPython
g++ -std=c++11 $1 -o ./.executables/profiler/$(basename $1 .cpp) -g -O -lprofiler
echo "Finished compiling + linking"
CPUPROFILE=$1.out ./.executables/profiler/$(basename $1 .cpp)
./.executables/profiler/$(basename $1 .cpp)
pprof ./.executables/profiler/$(basename $1 .cpp) $1.out
Run Code Online (Sandbox Code Playgroud)
警告:我试着把它清理一下。它可能包含很多不必要的选项。
归档时间: |
|
查看次数: |
6833 次 |
最近记录: |