Problems with using gperftools on Mac OS X

喜你入骨 提交于 2019-12-06 13:56:44

问题


I have found several conflicting answers over this topic. This blog post requires libuwind, but that doesn't work on Mac OS X. I included #include <google/profiler.h> in my code, however my compiler (g++) could not find the library. I installed gperftools via homebrew. In addition, I found this stackoverflow question showing 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
...

Running that command (without any of the prior steps) gets me this:

[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

Why does it try to access an internet site on my machine and a local file on his/hers?

Attempting to link lib profiler as a dry run with g++ gets me:

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

I have looked at the man pages, the help option text, the official online guide, blog posts, and many other sources.

I am so confused right now. Can someone help me use gperftools?

The result of my conversation with @osgx was this script. I tried to clean it up a bit. It likely contains quite a few unnecessary options too.


回答1:


The blog post https://dudefrommangalore.wordpress.com/2012/02/09/profiling-c-code-using-google-performance-tools/ "Profiling C++ code using Google Performance Tools" 2012 by dudefrommangalore missed the essential step.

You should link your program (which you want to be profiled) with cpu profiler library of gperftools library.

Check official manual: http://goog-perftools.sourceforge.net/doc/cpu_profiler.html, part "Linking in the Library"

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.)

Second step is to collect the profile, run the code with profiling enabled. In linux world it was done by setting controlling environment variable CPUPROFILE before running:

CPUPROFILE=name_of_profile ./program_to_be_profiled

Third step is to use pprof (google-pprof in ubuntu world). Check that there is not-empty name_of_profile profile file generated; it there is no such file, pprof will try to do remote profile fetch (you see output of such try).

pprof ./program_to_be_profiled name_of_profile



回答2:


First you need to run your program with profiling enabled.

This is usually first linking your program with libprofiler and then running it with CPUPROFILE=cpu.profile.

I.e.

$ CPUPROFILE=cpu.profile my_program

I think that later step is what you have been missing.

The program will create this cpu.profile file when it exits. And then you can use pprof (preferably from github.com/google/pprof) on it to visualize/analyze.



来源:https://stackoverflow.com/questions/37685434/problems-with-using-gperftools-on-mac-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!