1

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

然后我运行 pprof 来生成输出:

[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

为什么它试图访问我机器上的互联网站点和他/她的本地文​​件?

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

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

4

2 回答 2

1

博客文章https://dudefrommangalore.wordpress.com/2012/02/09/profiling-c-code-using-google-performance-tools/ “Profiling C++ code using Google Performance Tools” 2012 年 dudefrommangalore 错过了关键步骤。

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

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

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

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

CPUPROFILE=name_of_profile ./program_to_be_profiled

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

pprof ./program_to_be_profiled name_of_profile
于 2016-06-29T02:20:53.277 回答
1

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

这通常是首先将您的程序与 libprofiler 链接,然后使用 CPUPROFILE=cpu.profile 运行它。

IE

$ CPUPROFILE=cpu.profile my_program

我认为后面的步骤是您所缺少的。

程序退出时会创建这个 cpu.profile 文件。然后您可以在其上使用 pprof(最好来自 github.com/google/pprof)进行可视化/分析。

于 2016-06-26T00:51:13.420 回答