在Mac OS X上使用gperftools的问题

Problems with using gperftools on Mac OS X

本文关键字:gperftools 问题 Mac OS      更新时间:2023-10-16

我在这个话题上找到了几个相互矛盾的答案。这篇博客文章需要libuwind,但这在Mac OS X上不起作用。我在代码中包含了#include <google/profiler.h>,但我的编译器(g++)找不到库。我通过自制软件安装了gperftools。此外,我发现这个stackoverflow问题表明:

然后我运行approf生成输出:

[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对话的结果是这个脚本。我试着把它清理一下。它可能也包含了不少不必要的选项

博客文章https://dudefrommangalore.wordpress.com/2012/02/09/profiling-c-code-using-google-performance-tools/dudefcommangalore 2012年的"使用谷歌性能工具评测C++代码"错过了关键一步。

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

查看官方手册:http://goog-perftools.sourceforge.net/doc/cpu_profiler.html,部分"图书馆链接">

-lprofiler添加到可执行文件的链接时间步长中。(也可能在运行时使用LD_PRELOAD添加探查器,但不一定推荐这样做。)

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

CPUPROFILE=name_of_profile ./program_to_be_profiled

第三步是使用pprof(ubuntu世界中的google-pprof)。检查是否没有生成空的name_of_profile配置文件;如果没有这样的文件,pprof将尝试进行远程概要文件提取(您可以看到这样的尝试的输出)。

pprof ./program_to_be_profiled name_of_profile

首先,您需要在启用评测的情况下运行程序。

这通常是首先用libprofiler链接程序,然后用CPUPROFILE=cpu.profile运行它

$CPUPROFILE=cpu.profile my_program

我认为后面的步骤是你一直错过的。

程序将在退出时创建此cpu.profile文件。然后你可以在上面使用pprof(最好来自github.com/google/prof)来可视化/分析。