如何使用谷歌perf工具

时间:2012-06-03 22:09:09

标签: profiling gperftools

我刚刚开始在ubuntu中使用谷歌性能工具(google-perftoolslibgoogle-perftools4包),我发誓我正在谷歌搜索大约一天,我没有找到答案! 问题是我没有通过CPU分析获得所有函数的结果。这是我的代码:

#include "gperftools/profiler.h"
#include <iostream>
#include <math.h>
using namespace std;
void bar()
{
        int a,b,c,d,j,k;
        a=0;
        int z=0;
        b = 1000;
        while(z < b)
        {
                while (a < b)
                {
                        d = sin(a);
                        c = cos(a);
                        j = tan(a);
                        k = tan(a);
                        k = d * c + j *k;
                        a++;
                }
                a = 0;
                z++;
        }
}
void foo()
{
        cout << "hey " << endl;
}

int main()
{
        ProfilerStart("/home/mohammad/gperf/dump.txt");

        int a = 1000;
        while(a--){foo();}
        bar();
        ProfilerFlush();
        ProfilerStop();
}

编译为g++ test.cc -lprofiler -o a.out

这就是我运行代码的方式:

CPUPROFILE=dump.txt ./a.out

我也试过这个:

CPUPROFILE_FREQUENCY=10000 LD_PRELOAD=/usr/local/lib/libprofiler.so.0.3.0 CPUPROFILE=dump.txt ./a.out

这是我从google-pprof --text a.out dump.txt得到的:

Using local file ./a.out.
Using local file ./dump.txt.
Total: 22 samples
8  36.4%  36.4%        8  36.4% 00d8cb04
6  27.3%  63.6%        6  27.3% bar
3  13.6%  77.3%        3  13.6% __cos (inline)
2   9.1%  86.4%        2   9.1% 00d8cab4
1   4.5%  90.9%        1   4.5% 00d8cab6
1   4.5%  95.5%        1   4.5% 00d8cb06
1   4.5% 100.0%        1   4.5% __write_nocancel
0   0.0% 100.0%        3  13.6% __cos

但是没有关于foo功能的信息!

我的系统信息: ubuntu 12.04 g ++ 4.6.3

多数民众赞成!

2 个答案:

答案 0 :(得分:10)

TL; DR:foo用于快速和小型以获取分析事件,再运行100次。频率设置是拼写错误,pprof的采样频率不会超过CONFIG_HZ(通常为250)。最好切换到更现代的Linux perf分析器(tutorial from its authorswikipedia)。

长版:

您的foo功能太短而且简单 - 只需调用两个函数即可。使用g++ test.cc -lprofiler -o test.s -S -g编译测试,并使用test.s程序过滤c++filt以使c ++名称可读:

foo():
.LFB972:
        .loc 1 27 0
        pushq   %rbp
        movq    %rsp, %rbp
        .loc 1 28 0
        movl    $.LC0, %esi
        movl    std::cout, %edi
        call    std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
        movl    std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&), %esi
        movq    %rax, %rdi
        call    std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))
        .loc 1 29 0
        popq    %rbp
        ret
.LFE972:
        .size   foo(), .-foo()

因此,要在配置文件中看到它,您应该运行foo更多次,将主要版本中的int a = 1000;更改为更大的内容,例如10000或更高的100000(与测试中的I一样)。

您也可以修正错误的“CPUPROFILE_FREQUENC=10000”以更正CPUPROFILE_FREQUENCY(请注意Y)。我应该说10000为CPUPROFILE_FREQUENCY的设置太高,因为它通常每秒只能生成1000或250个事件,具体取决于内核配置CONFIG_HZ(大多数3.x内核有250,检查grep CONFIG_HZ= /boot/config*)。 pprof中CPUPROFILE_FREQUENCY的默认设置为100。

我在Ubuntu 14.04上使用bash脚本测试了不同的CPUPROFILE_FREQUENCY值:100000,10000,1000,250

for a in 100000 100000 10000 10000 1000 1000 300 300 250 250; do 
   echo -n "$a "; 
   CPUPROFILE_FREQUENCY=$a CPUPROFILE=dump$a.txt ./test >/dev/null;
done

结果是每个./test的120-140事件和运行时间大约0.5秒,所以来自google-perftools的cpuprofiler不能为单线程每秒做更多事件,而不是内核中设置的CONFIG_HZ(我有250)。

100000 PROFILE: interrupts/evictions/bytes = 124/1/6584
100000 PROFILE: interrupts/evictions/bytes = 134/0/7864
10000 PROFILE: interrupts/evictions/bytes = 125/0/7488
10000 PROFILE: interrupts/evictions/bytes = 123/0/6960
1000 PROFILE: interrupts/evictions/bytes = 134/0/6264
1000 PROFILE: interrupts/evictions/bytes = 125/2/7272
300 PROFILE: interrupts/evictions/bytes = 137/2/7984
300 PROFILE: interrupts/evictions/bytes = 126/0/7216
250 PROFILE: interrupts/evictions/bytes = 123/3/6680
250 PROFILE: interrupts/evictions/bytes = 137/2/7352

原始a = 1000 foo并且cout的函数运行得太快,无法在每次运行中获得任何分析事件(即使在250个事件/秒),因此您没有foo,也没有输入/输出功能。在少量运行中,__write_nocancel可能会得到采样事件,然后将报告foo和I / O函数形式libstdc ++(某处不在最顶层,因此请使用--text选项具有零自我事件计数的pprofgoogle-pprof)以及非零子事件计数:

 ....
   1   0.9%  99.1%        1   0.9% __write_nocancel
 ....
   0   0.0% 100.0%        1   0.9% _IO_new_file_overflow
   0   0.0% 100.0%        1   0.9% _IO_new_file_write
   0   0.0% 100.0%        1   0.9% __GI__IO_putc
   0   0.0% 100.0%        1   0.9% foo
   0   0.0% 100.0%        1   0.9% new_do_write
   0   0.0% 100.0%        1   0.9% std::endl
   0   0.0% 100.0%        1   0.9% std::ostream::put

对于a=100000,foo仍然太短而且速度太快而无法获得自己的事件,但I / O函数有几个。这是我从长--text输出中获取的列表:

  34  24.6%  24.6%       34  24.6% __write_nocancel

   1   0.7%  95.7%       35  25.4% __GI__IO_fflush
   1   0.7%  96.4%        1   0.7% __GI__IO_putc
   1   0.7%  97.8%        2   1.4% std::operator<< 
   1   0.7%  98.6%       36  26.1% std::ostream::flush
   1   0.7%  99.3%        2   1.4% std::ostream::put
   0   0.0% 100.0%       34  24.6% _IO_new_file_sync
   0   0.0% 100.0%       34  24.6% _IO_new_file_write
   0   0.0% 100.0%       40  29.0% foo

   0   0.0% 100.0%       34  24.6% new_do_write

   0   0.0% 100.0%        2   1.4% std::endl

只有pprof能够读取调用链才能看到具有零自有计数器的函数(如果没有省略帧信息,它知道谁调用了获取样本的函数)。

我还可以推荐更现代,更强大的功能(软件和硬件事件,频率高达5 kHz或更高;用户空间和内核分析)和更好的支持分析器,Linux perf分析器({ {3}},tutorial from its authors)。

perf的结果为a=10000

$ perf record  ./test3  >/dev/null
 ... skip some perf's spam about inaccessibility of kernel symbols 
 ... note the 3 kHz frequency here VVVV
Lowering default frequency rate to 3250. 
Please consider tweaking /proc/sys/kernel/perf_event_max_sample_rate.
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.078 MB perf.data (~3386 samples) ]

要查看来自perf.data输出文件的文本报告,我将使用less(因为perf report默认启动交互式配置文件浏览器):

$ perf report |less
... skip some extra info about the machine, kernel, and perf starting command
# Samples: 1K of event 'cycles'
# Event count (approx.): 1155264208
# Overhead   Command   Shared Object          Symbol
    41.94%    test3  libm-2.19.so         [.] __tan_sse2                                                                                                                                                                    
    16.95%    test3  libm-2.19.so         [.] __sin_sse2    
    13.40%    test3  libm-2.19.so         [.] __cos_sse2                                                                                                                                                                    
     4.93%    test3  test3                [.] bar()                                                                                                                                                                         
     2.90%    test3  libc-2.19.so         [.] __GI___libc_write    
....
     0.20%    test3  test3                [.] foo()                                                                                                                                                                         

perf report -n | less查看原始事件(样本)计数器:

# Overhead       Samples  Command        Shared Object 
    41.94%           663    test3  libm-2.19.so         [.] __tan_sse2                                                                                                                                                                    
    16.95%           268    test3  libm-2.19.so         [.] __sin_sse2   
    13.40%           212    test3  libm-2.19.so         [.] __cos_sse2                                                                                                                                                                    
     4.93%            78    test3  test3                [.] bar()                                                                                                                                                                         
     2.90%            62    test3  libc-2.19.so         [.] __GI___libc_write     
 ....
     0.20%             4    test3  test3                [.] foo()                                                                                                                                                                         

答案 1 :(得分:0)

尝试将LD_PRELOAD设置为

  

LD_PRELOAD =的/ usr /本地/ LIB / libprofiler.so

当传递不以后缀.so。

结尾的共享库时,它looks like there are problems