如何在Mac OS X上从命令行进行配置?

我正在尝试使用命令行工具在Mac OS X上分析C / C ++代码,我在gcc上使用-pg选项在Linux上运行gprof ,但我似乎无法在Mac上找到gprof ,即使我有一个描述在此页面中: 其他命令行工具(iOS)或其他命令行工具(mac) 。

 gprof:Produces execution profiles based on an execution analysis of a program. 

我安装了命令行工具,因此可以使用其他命令行工具,如otoolotool 。 我搜索了这个页面( 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,因为我想自动化一些进程并尝试保持跨平台系统。

  • 如何在Mac OS X上使用gprof? 我使用OS X 10.10。
  • 如何使用或不使用gprof从命令行进行配置文件?

听说OSX没有gprof profiler很奇怪。 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的一部分?)的变体,不知道具体如何,但是知道乐器是现代的,function丰富的探查器。

还有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不是一个探测器; 它是一个(慢)动态检测平台,其上构建了许多工具。 它包括两个能够进行分析的工具: callgrindcachegrind 。 Valgrind和这两个工具都不是本机分析器,它们不会分析应用程序,因为它将在现实生活中的真实CPU上运行。 相反,valgrind在虚拟机上执行程序,callgrind / cachegrind仪器用计数器执行机器代码。

callgrind( http://valgrind.org/docs/manual/cl-manual.html )使用每个线性指令块的计数器来计算“每条指令将执行多少次”(“Ir”事件,用于获取配置文件 -按所用时间百分比排序函数); 它还记录调用/返回以构建调用图。 “Ir”事件计数对于获取指令执行计数是正确的(它也可以模拟分支预测); 但它不能用于估计实际运行时间。 真正的cpu(高性能cpu称为超标量;乱序cpus也是超标量)能够在每个CPU时钟周期内执行多条指令; 并且它通常也无法执行任何指令,因为它们可能需要启动一些数据(来自远程缓存或来自内存或来自系统调用或来自其他高延迟指令的数据;或cpu误预测分支导致指令地址尚未读取/解码)。 大多数渐进式cpus甚至可能不执行某些命令(有些命令可以在每个周期执行多达8个“ nop ”,几个英特尔的Sandy / Ivy Bridges和更新的不会花费任何时间在“ xor eax,eax ”上将零写入寄存器;它们只是将下一个寄存器使用重新映射到归零物理寄存器)。 与硬件CPU上的实际运行相比,callgrind在分析运行方面的典型减速为10-20。

Cachegrind实现与callgrind(“Ir”,branches)相同的检测,但也可以模拟缓存层次结构(缓存加载/存储/未命中事件)。 它比callgrind慢。

callgrind和cachegrind的输出可以使用GUI工具kcachegrind( http://kcachegrind.sourceforge.net/ ,它可以在OS中运行)或命令行工具callgrind_annotate

其他工具是gperftools(google-perftools, https://github.com/gperftools/gperftools ),它在真实CPU上运行程序。 要使用它,请使用自制软件安装它,然后使用libprofiler链接程序(添加-Lpath_to_installed_gperftools -lprofiler )并将CPUPROFILE环境变量设置为某个文件名( CPUPROFILE=profile01 ./the_program). It will profile the program using interval timer ( 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 ) and output profiling data to the filename, defined in env var. Then you can view profile data in command-line or with svg/web browser using env var. Then you can view profile data in command-line or with svg/web browser using perl script from gperftools ( pprof ./the_program profile01`)中的pprof perl script from gperftools ( env var. Then you can view profile data in command-line or with svg/web browser using

在我的一个特殊问题(CrazyPython)上,我能够在@osgx的帮助下使用gperftools( pprof )。 这是问题, 这里是脚本的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 

警告:我试着把它清理一下。 它可能包含很多不必要的选项。