macos 在 mac os x 上分析 c++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11445619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Profiling c++ on mac os x
提问by foges
I'm attempting to profile some c++ code on my mac (os x Lion) and I haven't been able to find anything useful. I'm looking for a profiler that will tell me what functions are taking up my cpu time (similar to the matlab profiler).
我试图在我的 mac (os x Lion) 上分析一些 c++ 代码,但我找不到任何有用的东西。我正在寻找一个分析器,它会告诉我哪些函数占用了我的 CPU 时间(类似于 matlab 分析器)。
Here is what I have tried
这是我尝试过的
- gprof. This is what I use on my linux machine, but it just gives me empty output on my mac (apparently a known problem)
- Instruments. I can't for the life of me figure out how to profile anything within my compiled binary. Nor can I find any sort of useful tutorial.
- (other searching revealed Shark, which is no longer available and Valgrind which is for memory).
- 教授。这是我在我的 linux 机器上使用的,但它只会在我的 mac 上给我空输出(显然是一个已知问题)
- 仪器。我一生都无法弄清楚如何在我编译的二进制文件中分析任何内容。我也找不到任何有用的教程。
- (其他搜索显示 Shark 不再可用,而 Valgrind 用于内存)。
Really appreciate the help!
真的很感谢帮助!
回答by duskwuff -inactive-
Instruments is the tool to use. A full explanation of Instruments is outside the scope of this answer, but here's a quick start guide:
仪器是使用的工具。仪器的完整解释超出了本答案的范围,但这里有一个快速入门指南:
- Open Instruments.1
- Select the "Time Profiler" template.
- Select your application in the "Target" dropdown menu.2
- Hit the red circle ("record") button to start your application running.
- If applicable, do some stuff in your application that you need to profile.
- Hit the record button again to stop recording.
- Use the tools in Instruments to analyze your results.
- 打开仪器。1
- 选择“时间探查器”模板。
- 在“目标”下拉菜单中选择您的应用程序。2
- 点击红色圆圈(“记录”)按钮开始您的应用程序运行。
- 如果适用,请在您的应用程序中执行一些您需要分析的内容。
- 再次点击录制按钮停止录制。
- 使用 Instruments 中的工具来分析您的结果。
Of the tools available, the ones that will be most frequently useful are:
在可用的工具中,最常用的是:
- Expanding the call tree using the disclosure arrows
- Clicking the circled arrow on a function name to focus it
- Double-clicking a function to view the associated source
- The "Invert Call Tree" checkbox on the left-hand side
- 使用披露箭头扩展调用树
- 单击函数名称上的带圆圈的箭头以使其聚焦
- 双击函数以查看关联的源
- 左侧的“反转调用树”复选框
You can also start an invocation of instruments on the command line, ex
instruments -l 30000 -t Time\ Profiler -p 5773` see instructions.
您还可以开始调用instruments on the command line, ex
仪器 -l 30000 -t Time\ Profiler -p 5773` 参见说明。
1One easy way to open Instruments is to use Spotlight: Just click on the magnifying glass in the upper right corner of the taskbar (next to the clock) and type "Instruments".
1打开仪器的一种简单方法是使用 Spotlight:只需单击任务栏右上角的放大镜(时钟旁边)并键入“仪器”。
2Click "Choose Target..." and navigate to the path of your executable.
2单击“选择目标...”并导航到可执行文件的路径。
回答by bames53
Instruments really is the right answer, but if you can't figure out how to use it then another option is the profiler in the built-in Activity Monitor application. In Activity Monitor you can get info on any running process and there's a button to sample its execution for a while. You'll have to start your program, switch to Activity Monitor, find the process, and then sample it.
Instruments 确实是正确的答案,但如果您不知道如何使用它,那么另一个选择是内置 Activity Monitor 应用程序中的分析器。在活动监视器中,您可以获得有关任何正在运行的进程的信息,并且有一个按钮可以对其执行进行一段时间的采样。您必须启动程序,切换到活动监视器,找到进程,然后对其进行采样。
Additionally you can do 'poor man's profiling' simply by running the program in a debugger and pausing it manually half a dozen times or so and noting the call stack at those times. It's very simple but it works surprisingly well as a first pass for a significant fraction of programs.
此外,您可以简单地通过在调试器中运行程序并手动暂停它六次左右并在这些时间注意调用堆栈来进行“穷人的分析”。它非常简单,但作为大部分程序的第一遍,它的效果出奇地好。
There is also a command line sample
program which samples the callstack like the others do.
还有一个命令行sample
程序可以像其他人一样对调用堆栈进行采样。
回答by chutney
Instruments is the tool to use. To overcome the issue of the blank traces, make sure that you open Instruments from within XCode:
仪器是使用的工具。要解决空白跟踪的问题,请确保从 XCode 中打开 Instruments:
Xcode > Open Developer Tool > Instruments
If you open Instruments from an old Instruments icon that you pinned to your dock before the last update to XCode, it will give you blank traces.
如果您从上次更新 XCode 之前固定到 Dock 的旧 Instruments 图标打开 Instruments,它将为您提供空白跟踪。