gprof和(unix)时间之间的差异; gprof报告较低的运行时间

时间:2016-05-21 11:38:19

标签: c++ g++ profiling profiler gprof

我有简单的排序程序,我正在分析,以便有一个案例来研究gprof;我后来计划分析一个更大的算法。

我已使用-pg编译并运行./sort以生成gmon.out文件。 但是,当我运行gprof ./sort gmon.out时,累积秒数和自我秒数产生的值,我认为不准确。

首先,运行time(./sort)我得到:

real    0m14.352s
user    0m14.330s
sys     0m0.005s

我的秒表准确无误。

但是,平面轮廓的gprof结果是:

Each sample counts as 0.01 seconds.
%   cumulative   self              self     total           
time   seconds   seconds    calls   s/call   s/call  name    
56.18      2.76     2.76        1     2.76     4.71  sort(std::vector<int, std::allocator<int> >&)
35.01      4.49     1.72 1870365596     0.00     0.00  std::vector<int, std::allocator<int> >::operator[](unsigned long)
8.96      4.93     0.44   100071     0.00     0.00  std::vector<int, std::allocator<int> >::size() const
0.00      4.93     0.00    50001     0.00     0.00  __gnu_cxx::new_allocator<int>::construct(int*, int const&)
0.00      4.93     0.00    50001     0.00     0.00  void __gnu_cxx::__alloc_traits<std::allocator<int> >::construct<int>(std::allocator<int>&, int*, int const&)
0.00      4.93     0.00    50001     0.00     0.00  std::vector<int, std::allocator<int> >::push_back(int const&)
0.00      4.93     0.00    50001     0.00     0.00  operator new(unsigned long, void*)
0.00      4.93     0.00      170     0.00     0.00  std::_Iter_base<int*, false>::_S_base(int*)
0.00      4.93     0.00      102     0.00     0.00  std::_Niter_base<int*>::iterator_type std::__niter_base<int*>(int*)
0.00      4.93     0.00       68     0.00     0.00  __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >::base() const
0.00      4.93     0.00       68     0.00     0.00  std::_Miter_base<int*>::iterator_type std::__miter_base<int*>(int*)
0.00      4.93     0.00       52     0.00     0.00  std::_Vector_base<int, std::allocator<int> >::_M_get_Tp_allocator()
0.00      4.93     0.00       51     0.00     0.00  __gnu_cxx::new_allocator<int>::max_size() const
0.00      4.93     0.00       34     0.00     0.00  __gnu_cxx::__alloc_traits<std::allocator<int> >::max_size(std::allocator<int> const&)
0.00      4.93     0.00       34     0.00     0.00  __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >::__normal_iterator(int* const&)
0.00      4.93     0.00       34     0.00     0.00  std::_Vector_base<int, std::allocator<int> >::_M_get_Tp_allocator() const
0.00      4.93     0.00       34     0.00     0.00  std::vector<int, std::allocator<int> >::max_size() const
0.00      4.93     0.00       34     0.00     0.00  int* std::__copy_move<false, true, std::random_access_iterator_tag>::__copy_m<int>(int const*, int const*, int*)
0.00      4.93     0.00       34     0.00     0.00  int* std::__uninitialized_copy<true>::__uninit_copy<int*, int*>(int*, int*, int*)
0.00      4.93     0.00       34     0.00     0.00  int* std::__copy_move_a<false, int*, int*>(int*, int*, int*)
0.00      4.93     0.00       34     0.00     0.00  int* std::__copy_move_a2<false, int*, int*>(int*, int*, int*)
0.00      4.93     0.00       34     0.00     0.00  int* std::uninitialized_copy<int*, int*>(int*, int*, int*)
0.00      4.93     0.00       34     0.00     0.00  int* std::__uninitialized_copy_a<int*, int*, int>(int*, int*, int*, std::allocator<int>&)
0.00      4.93     0.00       34     0.00     0.00  int* std::__uninitialized_move_if_noexcept_a<int*, int*, std::allocator<int> >(int*, int*, int*, std::allocator<int>&)
0.00      4.93     0.00       34     0.00     0.00  int* std::copy<int*, int*>(int*, int*, int*)
0.00      4.93     0.00       18     0.00     0.00  void std::_Destroy_aux<true>::__destroy<int*>(int*, int*)
0.00      4.93     0.00       18     0.00     0.00  std::_Vector_base<int, std::allocator<int> >::_M_deallocate(int*, unsigned long)
0.00      4.93     0.00       18     0.00     0.00  void std::_Destroy<int*>(int*, int*)
0.00      4.93     0.00       18     0.00     0.00  void std::_Destroy<int*, int>(int*, int*, std::allocator<int>&)
0.00      4.93     0.00       17     0.00     0.00  __gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long)
0.00      4.93     0.00       17     0.00     0.00  __gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*)
0.00      4.93     0.00       17     0.00     0.00  __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >::difference_type __gnu_cxx::operator-<int*, std::vector<int, std::allocator<int> > >(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > > const&, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > > const&)
0.00      4.93     0.00       17     0.00     0.00  std::vector<int, std::allocator<int> >::_M_check_len(unsigned long, char const*) const
0.00      4.93     0.00       17     0.00     0.00  std::_Vector_base<int, std::allocator<int> >::_M_allocate(unsigned long)
0.00      4.93     0.00       17     0.00     0.00  std::vector<int, std::allocator<int> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&)
0.00      4.93     0.00       17     0.00     0.00  std::vector<int, std::allocator<int> >::end()
0.00      4.93     0.00       17     0.00     0.00  std::vector<int, std::allocator<int> >::begin()
0.00      4.93     0.00       17     0.00     0.00  unsigned long const& std::max<unsigned long>(unsigned long const&, unsigned long const&)
0.00      4.93     0.00        2     0.00     0.00  std::operator|(std::_Ios_Openmode, std::_Ios_Openmode)
0.00      4.93     0.00        1     0.00     0.00  _GLOBAL__sub_I_main
0.00      4.93     0.00        1     0.00     0.00  generateData(std::basic_ofstream<char, std::char_traits<char> >&)
0.00      4.93     0.00        1     0.00     0.22  writeSortedFile(std::vector<int, std::allocator<int> >&)
0.00      4.93     0.00        1     0.00     0.00  __static_initialization_and_destruction_0(int, int)
0.00      4.93     0.00        1     0.00     0.00  loadBuf(std::vector<int, std::allocator<int> >&, std::basic_ifstream<char, std::char_traits<char> >&)
0.00      4.93     0.00        1     0.00     0.00  __gnu_cxx::new_allocator<int>::new_allocator()
0.00      4.93     0.00        1     0.00     0.00  __gnu_cxx::new_allocator<int>::~new_allocator()
0.00      4.93     0.00        1     0.00     0.00  std::allocator<int>::allocator()
0.00      4.93     0.00        1     0.00     0.00  std::allocator<int>::~allocator()
0.00      4.93     0.00        1     0.00     0.00  std::_Vector_base<int, std::allocator<int> >::_Vector_impl::_Vector_impl()
0.00      4.93     0.00        1     0.00     0.00  std::_Vector_base<int, std::allocator<int> >::_Vector_impl::~_Vector_impl()
0.00      4.93     0.00        1     0.00     0.00  std::_Vector_base<int, std::allocator<int> >::_Vector_base()
0.00      4.93     0.00        1     0.00     0.00  std::_Vector_base<int, std::allocator<int> >::~_Vector_base()
0.00      4.93     0.00        1     0.00     0.00  std::vector<int, std::allocator<int> >::vector()
0.00      4.93     0.00        1     0.00     0.00  std::vector<int, std::allocator<int> >::~vector()

因此,累积秒数不会累积到真值(~14s),它会出现。结果确实显示sort()是时间成本最高的,但实际时间值不会累加。 -z并未改变这一点,但这是预期的。 调用图(不包括在内)似乎没有显示任何暗示缺少秒数的地方;额外的秒数不在孩子身上。

当我尝试分析上面提到的更大的算法时,我似乎得到了类似的结果(gprof的时间值比预期的要小得多) - gprof说运行时间是~450秒,实际上需要3小时。我虽然这是因为gprof无法处理MPI,而较大的算法广泛使用,但现在我认为我要么误解了gprof结果,要么我错过了一些标志。

我是否有可能实际上没有考虑我的gmon.out文件? 我认为这是因为,当我运行gprof ./sort时,我得到与gprof ./sort gmon.out完全相同的结果。因此,它似乎甚至没有使用gmon.out。我认为gmon.out与可执行文件一起需要将时间映射到函数。 gprof如何在没有gmon.out的情况下生成输出?

任何有启发性的信息都非常受欢迎,提前谢谢!

注意: 阅读例如{this post),我发现信息表明gprof在分析堆分配等方面存在问题(new)。我应该注意./sort使用std::vector来保存元素,这些元素将被分配给堆。如果这是一个可能的问题,请告诉我。

1 个答案:

答案 0 :(得分:5)

gprof不知道它没有调试信息访问的函数,即标准库。如果您想获得准确的经过时间并仍然获得一个callgraph,您可以使用perf

作为一个例子,我写了一个循环10000次的程序。在这个循环中,我用随机值填充向量,然后对其进行排序。对于gprof,我执行以下步骤:

g++ -std=c++11 -O2 -pg -g
./a.out
gprof -b ./a.out

gmon.out如果不存在则会被创建,如果存在则会被覆盖,如果您没有指定要使用的文件,则gprof会自动使用它。 -b会抑制文字模糊。

这是示例输出:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  Ts/call  Ts/call  name    
100.52      4.94     4.94                             frame_dummy
  0.00      4.94     0.00       26     0.00     0.00  void std::__adjust_heap<__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, long, double, __gnu_cxx::__ops::_Iter_less_iter>(__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, long, long, double, __gnu_cxx::__ops::_Iter_less_iter)
  0.00      4.94     0.00        1     0.00     0.00  _GLOBAL__sub_I_main

正如您所看到的,它只记录向量堆实现的时间,并且根本不知道排序(或其他任何东西)。现在让我们试试perf

perf record -g ./a.out
perf report --call-graph --stdio

# Total Lost Samples: 0
#
# Samples: 32K of event 'cycles'
# Event count (approx.): 31899806183
#
# Children      Self  Command  Shared Object        Symbol                                                                            
# ........  ........  .......  ...................  ..................................................................................
#
    99.98%    34.46%  a.out    a.out                [.] main                                                                          
              |          
              |--65.52%-- main
              |          |          
              |          |--65.29%-- std::__introsort_loop<__gnu_cxx::__normal_iterator<double*, std::vector<double

[其余文字省略]

如您所见,perf捕获了排序功能。如果我运行perf stat,我也会得到准确的运行时间。

如果您正在使用GCC,则可以传递-D_GLIBCXX_DEBUG以使其使用调试库实现。这将使您的代码运行速度慢很多,但必须使gprof能够查看这些函数。一个例子:

g++ -std=c++11 -O2 test.cpp -D_GLIBCXX_DEBUG -pg -g
./a.out
gprof -b ./a.out

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  us/call  us/call  name    
 88.26      0.15     0.15   102875     1.46     1.46  __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<double*, std::__cxx1998::vector<double, std::allocator<double> > >, std::__debug::vector<double, std::allocator<double> > > std::__unguarded_partition<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<double*, std::__cxx1998::vector<double, std::allocator<double> > >, std::__debug::vector<double, std::allocator<double> > >, __gnu_cxx::__ops::_Iter_less_iter>(__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<double*, std::__cxx1998::vector<double, std::allocator<double> > >, std::__debug::vector<double, std::allocator<double> > >, __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<double*, std::__cxx1998::vector<double, std::allocator<double> > >, std::__debug::vector<double, std::allocator<double> > >, __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<double*, std::__cxx1998::vector<double, std::allocator<double> > >, std::__debug::vector<double, std::allocator<double> > >, __gnu_cxx::__ops::_Iter_less_iter)
 11.77      0.17     0.02   996280     0.02     0.02  void std::__unguarded_linear_insert<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<double*, std::__cxx1998::vector<double, std::allocator<double> > >, std::__debug::vector<double, std::allocator<double> > >, __gnu_cxx::__ops::_Val_less_iter>(__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<double*, std::__cxx1998::vector<double, std::allocator<double> > >, std::__debug::vector<double, std::allocator<double> > >, __gnu_cxx::__ops::_Val_less_iter)
  0.00      0.17     0.00   417220     0.00     0.00  frame_dummy
  0.00      0.17     0.00   102875     0.00     0.00  void std::__move_median_to_first<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<double*, std::__cxx1998::vector<double, std::allocator<double> > >, std::__debug::vector<double, std::allocator<double> > >, __gnu_cxx::__ops::_Iter_less_iter>(__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<double*, std::__cxx1998::vector<double, std::allocator<double> > >, std::__debug::vector<double, std::allocator<double> > >, __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<double*, std::__cxx1998::vector<double, std::allocator<double> > >, std::__debug::vector<double, std::allocator<double> > >, __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<double*, std::__cxx1998::vector<double, std::allocator<double> > >, std::__debug::vector<double, std::allocator<double> > >, __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<double*, std::__cxx1998::vector<double, std::allocator<double> > >, std::__debug::vector<double, std::allocator<double> > >, __gnu_cxx::__ops::_Iter_less_iter)
  0.00      0.17     0.00     1000     0.00     0.25  void std::__insertion_sort<__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<double*, std::__cxx1998::vector<double, std::allocator<double> > >, std::__debug::vector<double, std::allocator<double> > >, __gnu_cxx::__ops::_Iter_less_iter>(__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<double*, std::__cxx1998::vector<double, std::allocator<double> > >, std::__debug::vector<double, std::allocator<double> > >, __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<double*, std::__cxx1998::vector<double, std::allocator<double> > >, std::__debug::vector<double, std::allocator<double> > >, __gnu_cxx::__ops::_Iter_less_iter)
  0.00      0.17     0.00        1     0.00     0.00  _GLOBAL__sub_I_main

我故意减少了迭代次数,以便在合理的时间内完成执行,但是你看到gprof现在显示了之前没有计算的功能。