繁体   English   中英

如何从 Mac OS X 上的命令行进行分析?

[英]How to profile from the command line on Mac OS X?

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

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

我安装了命令行工具,所以其他命令行工具如otoolatos都可以使用。 我用谷歌搜索找到这个页面( https://apple.stackexchange.com/questions/154289/installing-gprof-on-mac )说gprof不受支持,但我不确定我什么时候有一个 Apple 文档描述工具; 无论如何,我尝试使用brew下载gprof ,但没有成功。

我发现Attempting to use gprof with C++ code on a Mac ,但我没有 output 和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的一部分?)的变体,不知道具体如何,但是知道乐器是现代的,功能丰富的探测器。

还有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”,分支)相同的检测,但也可以模拟缓存层次结构(缓存加载/存储/未命中事件)。 它比callgrind慢。

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

其他工具是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

无论有没有 gprof,我如何从命令行进行概要分析?

这似乎适用于 MacOS Monterey 12.5

xctrace record --output <path/to/folder> --template "Time Profiler" --time-limit 10s --attach <pid>

结果将是一个 .trace 文件,您可以使用它打开

open path/to/name-of-file.trace

如果您想启动应用程序而不是附加到现有应用程序,请使用--launch -- command arugments --launch必须是最后一个参数

了解更多信息

man xctrace

关于我的一个问题(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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM