c++ - 在 Mac OS X 上使用 gperftools 的问题

标签 c++ macos profiling profile gperftools

关于这个主题,我发现了几个相互矛盾的答案。 This博客文章需要 libuwind,但这在 Mac OS X 上不起作用。我包括了 #include <google/profiler.h>在我的代码中,但是我的编译器 (g++) 找不到该库。我安装了gperftools通过自制软件。另外,我发现this显示这个的计算器问题:

Then I ran pprof to generate the output:

[hidden ~]$ pprof --text ./a.out cpu.profile 
Using local file ./a.out.
Using local file cpu.profile.
Removing __sigtramp from all stack traces.
Total: 282 samples
     107  37.9%  37.9%      107  37.9% 0x000000010d72229e
      16   5.7%  43.6%       16   5.7% 0x000000010d721a5f
      12   4.3%  47.9%       12   4.3% 0x000000010d721de8
...

运行该命令(没有任何先前的步骤)得到这个:

[hidden]$ pprof --text ./a.out cpu.profile 
Using remote profile at ./a.out.
Failed to get the number of symbols from http://cpu.profile/pprof/symbol

为什么它会尝试访问我机器上的网站和他/她机器上的本地文件?

尝试将 lib profiler 链接为 g++ 试运行让我:

[hidden]$ g++ -l libprofiler
ld: library not found for -llibprofiler
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我查看了手册页、帮助选项文本、官方在线指南、博客文章和许多其他来源。

我现在很迷茫。有人可以帮我使用 gperftools 吗?

我与@osgx 对话的结果是 this script .我试着清理了一下。它也可能包含很多不必要的选项。

最佳答案

博文https://dudefrommangalore.wordpress.com/2012/02/09/profiling-c-code-using-google-performance-tools/ dudefrommangalore 2012 年的“使用 Google 性能工具分析 C++ 代码”错过了关键步骤。

您应该链接您的程序(您希望对其进行分析)与 gperftools 库的 cpu 分析器库。

查看官方手册:http://goog-perftools.sourceforge.net/doc/cpu_profiler.html , “库中的链接”部分

add -lprofiler to the link-time step for your executable. (It's also probably possible to add in the profiler at run-time using LD_PRELOAD, but this isn't necessarily recommended.)

第二步是收集配置文件,在启用配置文件的情况下运行代码。在 linux 世界中,它是通过在运行之前设置控制环境变量 CPUPROFILE 来完成的:

CPUPROFILE=name_of_profile ./program_to_be_profiled

第三步是使用pprof(google-pprof in ubuntu world)。检查是否生成了非空的name_of_profile 配置文件;如果没有这样的文件,pprof 将尝试进行远程配置文件获取(您会看到此类尝试的输出)。

pprof ./program_to_be_profiled name_of_profile

关于c++ - 在 Mac OS X 上使用 gperftools 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37685434/

相关文章:

c++ - 使用 pugixml 解释程序的内存消耗

c++ - 在 C++ 中调用初始化列表中的函数

c++如何在没有默认构造函数的情况下定义另一种类型的对象成员

c++ - 交换在 int 的情况下失败,在 string 的情况下有效

excel - 如何使用 VBA for Excel for MacOS 创建文本文件

silverlight - 如何分析 Silverlight 应用程序?

c++ - 如何通过 Win API 配置快捷方式标题

bash - 在 shell 脚本 OS X 中设置环境变量

macos - macOS 应用程序组名称应该以 `group.` 还是开发团队 ID 开头?

c++ - gprof 的时间采样问题