gprof没有输出

时间:2017-10-08 05:48:10

标签: g++ profiling gprof

我正在尝试使用gprof来分析我正在开发的一些数字代码,但是gprof似乎无法从我的程序中收集数据。这是我的命令行:

g++ -Wall -O3 -g -pg -o fftw_test fftw_test.cpp -lfftw3 -lfftw3_threads -lm && ./fftw_test

创建了gmon.out文件,但它似乎没有数据。我跑的时候

gprof -b fftw_test gmon.out > gprof.out

我得到的只是

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  Ts/call  Ts/call  name    


                        Call graph


granularity: each sample hit covers 2 byte(s) no time propagated

index % time    self  children    called     name


Index by function name

任何见解?

代码做了很多事情,它不是简单地调用FFTW例程。它具有计算某些复系数的函数,通过这些系数乘以输入数据的函数,等等。

编辑:包括示例代码和结果。

#include <cstdlib>
#include <ctime>

int main()
{
   std::srand( std::time( 0 ) );

   double sum = 0.0;

   for ( int i = 0; i < RAND_MAX; ++i )
      sum += std::rand() / ( double ) RAND_MAX;

   std::cout << sum << '\n';

   return 0;
}

命令行:

$ g++ -Wall -O3 -g -pg -o gprof_test gprof_test.cpp && ./gprof_test
1.07374e+09
$ gprof -b gprof_test gmon.out > gprof.out
$ cat gprof.out

结果:

Flat profile:

Each sample counts as 0.01 seconds.
 no time accumulated

  %   cumulative   self              self     total           
 time   seconds   seconds    calls  Ts/call  Ts/call  name    


                        Call graph


granularity: each sample hit covers 2 byte(s) no time propagated

index % time    self  children    called     name


Index by function name

就是这样。

2 个答案:

答案 0 :(得分:2)

如果您使用的是gcc 6,则很可能会遇到this错误(请注意,该错误并非特定于Debian,而是取决于gcc的构建方式)。一种解决方法是使用“ -no-pie”选项进行编译,该选项将禁用与位置无关的代码生成。

如果想进一步了解PIE,

This是一个很好的开始。

答案 1 :(得分:-1)

  

gprof似乎无法从我的程序中收集数据。这是我的命令行:

g++ -Wall -O3 -g -pg -o fftw_test fftw_test.cpp -lfftw3 -lfftw3_threads -lm && ./fftw_test

您的程序使用fftw库,可能几乎只包含fftw库调用。运行时间是多少?您的程序可能太快,无法使用gprof进行分析。 更新 gprof可能看不到库,因为它是在没有启用gprof性能分析的情况下编译的。

GNU gprof有两个部分。首先,它使用-pg选项编译的c / cpp文件中的函数调用(使用mcount函数调用 - https://en.wikipedia.org/wiki/Gprof) - 获取调用者/被调用者信息。其次,它将附加的分析库链接到您的可执行文件中,以添加定期采样以查找执行了更长时间的代码。采样是用profil(setitimer)完成的。 Setitimer分析的分辨率有限,无法解析小于10毫秒或1毫秒的间隔(每秒100或1000个样本)。

在您的示例中,fftw库可能在没有检测的情况下编译,因此没有mcount调用它。它仍然可以通过采样部分捕获,但仅适用于程序的主线程(https://en.wikipedia.org/wiki/Gprof - “通常它只描述应用程序的主线程”。

perf profiler没有mcount的检测(当使用-g选项记录时,它从堆栈展开得到被调用者/调用者),但是它有更好的统计/采样变体(它可以使用硬件PMU计数器),没有100或1000 Hz限制,并且它正确支持(配置文件)线程。尝试perf record -F1000 ./fftw_test(采样频率为1 kHz)和perf reportperf report > report.txt。还有一些GUI / HTML前端也是如此:https://github.com/KDAB/hotspot https://github.com/jrfonseca/gprof2dot

要获得更好的setitimer样式分析器,请检查google-perftools https://github.com/gperftools/gperftools以获取“CPU PROFILER”。

==

通过测试,我在Debian 8.6 Linux内核版本3.16.0-4-amd64 x86_64机器上获得了一些gprof结果,g ++(Debian 4.9.2-10),gprof是“GNU gprof(GNU Binutils for Debian)2.27”

$ cat gprof_test.cpp
#include <cstdlib>
#include <ctime>
#include <iostream>
int main()
{
   std::srand( std::time( 0 ) );
   double sum = 0.0;
   for ( int i = 0; i < 100000000; ++i )
      sum += std::rand() / ( double ) RAND_MAX;
   std::cout << sum << '\n';
   return 0;
}
$ g++ -Wall -O3 -g -pg -o gprof_test gprof_test.cpp && time ./gprof_test
5.00069e+06
real    0m0.992s
$ gprof -b gprof_test gmon.out
Flat profile:

Each sample counts as 0.01 seconds.
 no time accumulated

  %   cumulative   self              self     total
 time   seconds   seconds    calls  Ts/call  Ts/call  name
  0.00      0.00     0.00        1     0.00     0.00  _GLOBAL__sub_I_main

因此,gprof在这个1秒示例中没有捕获任何时间样本,并且没有关于调用库的信息(它们是在没有-pg 的情况下编译的)。添加一些包装器函数并禁止内联优化后,我从gprof获得了一些数据,但库时间没有计算(它看到了2秒运行时间的0.72秒):

$ cat *cpp
#include <cstdlib>
#include <ctime>
#include <iostream>

int rand_wrapper1()
{
  return std::rand();
}
int rand_scale1()
{
  return rand_wrapper1() / ( double ) RAND_MAX;
}
int main()
{
   std::srand( std::time( 0 ) );
   double sum = 0.0;
   for ( int i = 0; i < 100000000; ++i )
    sum+= rand_scale1();
//      sum += std::rand() / ( double ) RAND_MAX;
   std::cout << sum << '\n';
   return 0;
}
$ g++ -Wall -O3 -fno-inline -g -pg -o gprof_test gprof_test.cpp && time ./gprof_test
real    0m2.345s
$ gprof -b gprof_test gmon.out
Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls  ns/call  ns/call  name
 80.02      0.57     0.57                             rand_scale1()
 19.29      0.71     0.14 100000000     1.37     1.37  rand_wrapper1()
  2.14      0.72     0.02                             frame_dummy
  0.00      0.72     0.00        1     0.00     0.00  _GLOBAL__sub_I__Z13rand_wrapper1v
  0.00      0.72     0.00        1     0.00     0.00  __static_initialization_and_destruction_0(int, int) [clone .constprop.0]


                        Call graph


granularity: each sample hit covers 2 byte(s) for 1.39% of 0.72 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]     97.9    0.57    0.14                 rand_scale1() [1]
                0.14    0.00 100000000/100000000     rand_wrapper1() [2]
-----------------------------------------------
                0.14    0.00 100000000/100000000     rand_scale1() [1]
[2]     19.0    0.14    0.00 100000000         rand_wrapper1() [2]

perf看到所有部分:

$ perf record ./gprof_test
0
[ perf record: Woken up 2 times to write data ]
[ perf record: Captured and wrote 0.388 MB perf.data (~16954 samples) ]
$ perf report |more   
# Samples: 9K of event 'cycles'
# Event count (approx.): 7373484231
#
# Overhead     Command      Shared Object                     Symbol
# ........  ..........  .................  .........................
#
    25.91%  gprof_test  gprof_test         [.] rand_scale1()
    21.65%  gprof_test  libc-2.19.so       [.] __mcount_internal
    13.88%  gprof_test  libc-2.19.so       [.] _mcount
    12.54%  gprof_test  gprof_test         [.] main
     9.35%  gprof_test  libc-2.19.so       [.] __random_r
     8.40%  gprof_test  libc-2.19.so       [.] __random
     3.97%  gprof_test  gprof_test         [.] rand_wrapper1()
     2.79%  gprof_test  libc-2.19.so       [.] rand
     1.41%  gprof_test  gprof_test         [.] mcount@plt
     0.03%  gprof_test  [kernel.kallsyms]  [k] memset
相关问题