简体   繁体   English

gcc -Ofast -pg错误的gprof结果

[英]wrong gprof results with gcc -Ofast -pg

I'm seeing strange profiling results with gcc -pg and gprof . 我看到了gcc -pggprof奇怪分析结果。

I can't find a better/smaller way to reproduce so I'm linking the actual code where I'm seeing the issue. 我找不到更好/更小的重现方法,所以我将实际问题链接到实际的代码上。

I'm using the code here , building gprof profiling results with make prof 我在这里使用代码,用make prof构建gprof分析结果

I notice several errors, mainly: 我注意到几个错误,主要是:

  1. A function shape_stream_test that should never even be called is shown as being called heavily by ai_best_move_rec and taking up 15% of total run time in self time 显示一个甚至不应该调用的shape_stream_test函数被ai_best_move_rec大量调用,并占用了自身运行时间的15%
  2. Other functions legitimately called by ai_best_move_rec like grid_block_remove , grid_cpy , grid_new don't even show up as children ai_best_move_rec合法调用的其他函数,例如grid_block_removegrid_cpygrid_new甚至都不会显示为子级

Actual prof stats: 实际教授数据:

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
 53.92      4.00     4.00  4231936     0.00     0.00  grid_eval
 13.62      5.01     1.01  4467368     0.00     0.00  shape_stream_test
  6.88      5.52     0.51  4690742     0.00     0.00  grid_block_center_top
  6.07      5.97     0.45  4521016     0.00     0.00  grid_block_valid
  5.80      6.40     0.43  4467368     0.00     0.00  grid_block_add
  3.37      6.65     0.25  4467368     0.00     0.00  grid_block_drop
  2.02      6.80     0.15       35     4.29   209.20  ai_best_move_rec
  1.95      6.95     0.15   235469     0.00     0.00  grid_init
  1.75      7.08     0.13  9212461     0.00     0.00  block_extreme
  1.48      7.19     0.11  4467402     0.00     0.00  block_move
  0.94      7.26     0.07  1654899     0.00     0.00  block_get
  0.54      7.30     0.04                             block_crust_get
  0.40      7.33     0.03                             grid_block_set_color
  0.27      7.35     0.02  4467368     0.00     0.00  grid_block_remove
  0.27      7.37     0.02   235501     0.00     0.00  block_new
  0.27      7.39     0.02                             grid_block_intersects
  0.20      7.40     0.02   235469     0.00     0.00  grid_new
  0.13      7.41     0.01   235467     0.00     0.00  shape_stream_peek
  0.13      7.42     0.01                             drop_amount
  0.00      7.42     0.00      621     0.00     0.00  grid_clear_lines
  0.00      7.42     0.00      587     0.00     0.00  grid_cpy
  0.00      7.42     0.00       35     0.00     0.00  grid_print
  0.00      7.42     0.00       34     0.00     0.00  block_init
  0.00      7.42     0.00       34     0.00     0.00  block_print
  0.00      7.42     0.00       34     0.00     0.00  game_move_print
  0.00      7.42     0.00       34     0.00     0.00  grid_apply_moves
  0.00      7.42     0.00       34     0.00     0.00  shape_stream_pop
  0.00      7.42     0.00        7     0.00     0.00  shape_new
  0.00      7.42     0.00        1     0.00     0.00  shape_stream_new

 %         the percentage of the total running time of the
time       program used by this function.

cumulative a running sum of the number of seconds accounted
 seconds   for by this function and those listed above it.

 self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for this
           listing.

calls      the number of times this function was invoked, if
           this function is profiled, else blank.

 self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,
       else blank.

 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this 
       function is profiled, else blank.

name       the name of the function.  This is the minor sort
           for this listing. The index shows the location of
       the function in the gprof listing. If the index is
       in parenthesis it shows where it would appear in
       the gprof listing if it were to be printed.

Copyright (C) 2012-2014 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.

             Call graph (explanation follows)


granularity: each sample hit covers 2 byte(s) for 0.13% of 7.42 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]     98.7    0.00    7.32                 ai_test [1]
                0.15    7.17      35/35          ai_best_move_rec [2]
                0.00    0.00      34/34          grid_apply_moves [21]
                0.00    0.00       2/235469      grid_new [9]
                0.00    0.00      35/35          grid_print [24]
                0.00    0.00      34/34          game_move_print [27]
                0.00    0.00      34/34          shape_stream_pop [28]
                0.00    0.00       1/1           shape_stream_new [30]
-----------------------------------------------
                              235432             ai_best_move_rec [2]
                0.15    7.17      35/35          ai_test [1]
[2]     98.7    0.15    7.17      35+235432  ai_best_move_rec [2]
                4.00    0.00 4231936/4231936     grid_eval [3]
                1.01    0.00 4467368/4467368     shape_stream_test [4]
                0.51    0.11 4690742/4690742     grid_block_center_top [5]
                0.45    0.09 4521016/4521016     grid_block_valid [6]
                0.43    0.00 4467368/4467368     grid_block_add [7]
                0.25    0.00 4467368/4467368     grid_block_drop [8]
                0.02    0.15  235467/235469      grid_new [9]
                0.11    0.00 4467368/4467402     block_move [12]
                0.02    0.00 4467368/4467368     grid_block_remove [16]
                0.02    0.00  235467/235501      block_new [17]
                0.01    0.00  235467/235467      shape_stream_peek [19]
                0.00    0.00     587/587         grid_cpy [23]
                0.00    0.00     587/621         grid_clear_lines [22]
                              235432             ai_best_move_rec [2]
-----------------------------------------------
                4.00    0.00 4231936/4231936     ai_best_move_rec [2]
[3]     53.9    4.00    0.00 4231936         grid_eval [3]
-----------------------------------------------
                1.01    0.00 4467368/4467368     ai_best_move_rec [2]
[4]     13.6    1.01    0.00 4467368         shape_stream_test [4]
-----------------------------------------------
                0.51    0.11 4690742/4690742     ai_best_move_rec [2]
[5]      8.4    0.51    0.11 4690742         grid_block_center_top [5]
                0.07    0.00 4690742/9212461     block_extreme [11]
                0.04    0.00 1056160/1654899     block_get [13]
-----------------------------------------------
                0.45    0.09 4521016/4521016     ai_best_move_rec [2]
[6]      7.3    0.45    0.09 4521016         grid_block_valid [6]
                0.06    0.00 4521016/9212461     block_extreme [11]
                0.03    0.00  598739/1654899     block_get [13]
-----------------------------------------------
                0.43    0.00 4467368/4467368     ai_best_move_rec [2]
[7]      5.8    0.43    0.00 4467368         grid_block_add [7]
-----------------------------------------------
                0.25    0.00 4467368/4467368     ai_best_move_rec [2]
[8]      3.4    0.25    0.00 4467368         grid_block_drop [8]
                0.00    0.00     635/9212461     block_extreme [11]
-----------------------------------------------
                0.00    0.00       2/235469      ai_test [1]
                0.02    0.15  235467/235469      ai_best_move_rec [2]
[9]      2.2    0.02    0.15  235469         grid_new [9]
                0.15    0.00  235469/235469      grid_init [10]
-----------------------------------------------
                0.15    0.00  235469/235469      grid_new [9]
[10]     2.0    0.15    0.00  235469         grid_init [10]
-----------------------------------------------
                0.00    0.00      68/9212461     grid_apply_moves [21]
                0.00    0.00     635/9212461     grid_block_drop [8]
                0.06    0.00 4521016/9212461     grid_block_valid [6]
                0.07    0.00 4690742/9212461     grid_block_center_top [5]
[11]     1.8    0.13    0.00 9212461         block_extreme [11]
-----------------------------------------------
                0.00    0.00      34/4467402     grid_apply_moves [21]
                0.11    0.00 4467368/4467402     ai_best_move_rec [2]
[12]     1.5    0.11    0.00 4467402         block_move [12]
-----------------------------------------------
                0.03    0.00  598739/1654899     grid_block_valid [6]
                0.04    0.00 1056160/1654899     grid_block_center_top [5]
[13]     0.9    0.07    0.00 1654899         block_get [13]
-----------------------------------------------
                                                 <spontaneous>
[14]     0.5    0.04    0.00                 block_crust_get [14]
-----------------------------------------------
                                                 <spontaneous>
[15]     0.4    0.03    0.00                 grid_block_set_color [15]
-----------------------------------------------
                0.02    0.00 4467368/4467368     ai_best_move_rec [2]
[16]     0.3    0.02    0.00 4467368         grid_block_remove [16]
-----------------------------------------------
                0.00    0.00      34/235501      grid_apply_moves [21]
                0.02    0.00  235467/235501      ai_best_move_rec [2]
[17]     0.3    0.02    0.00  235501         block_new [17]
-----------------------------------------------
                                                 <spontaneous>
[18]     0.3    0.02    0.00                 grid_block_intersects [18]
-----------------------------------------------
                0.01    0.00  235467/235467      ai_best_move_rec [2]
[19]     0.1    0.01    0.00  235467         shape_stream_peek [19]
-----------------------------------------------
                                                 <spontaneous>
[20]     0.1    0.01    0.00                 drop_amount [20]
-----------------------------------------------
                0.00    0.00      34/34          ai_test [1]
[21]     0.0    0.00    0.00      34         grid_apply_moves [21]
                0.00    0.00      34/235501      block_new [17]
                0.00    0.00      68/9212461     block_extreme [11]
                0.00    0.00      34/4467402     block_move [12]
                0.00    0.00      34/34          block_init [25]
                0.00    0.00      34/621         grid_clear_lines [22]
-----------------------------------------------
                0.00    0.00      34/621         grid_apply_moves [21]
                0.00    0.00     587/621         ai_best_move_rec [2]
[22]     0.0    0.00    0.00     621         grid_clear_lines [22]
-----------------------------------------------
                0.00    0.00     587/587         ai_best_move_rec [2]
[23]     0.0    0.00    0.00     587         grid_cpy [23]
-----------------------------------------------
                0.00    0.00      35/35          ai_test [1]
[24]     0.0    0.00    0.00      35         grid_print [24]
-----------------------------------------------
                0.00    0.00      34/34          grid_apply_moves [21]
[25]     0.0    0.00    0.00      34         block_init [25]
-----------------------------------------------
                0.00    0.00      34/34          game_move_print [27]
[26]     0.0    0.00    0.00      34         block_print [26]
-----------------------------------------------
                0.00    0.00      34/34          ai_test [1]
[27]     0.0    0.00    0.00      34         game_move_print [27]
                0.00    0.00      34/34          block_print [26]
-----------------------------------------------
                0.00    0.00      34/34          ai_test [1]
[28]     0.0    0.00    0.00      34         shape_stream_pop [28]
-----------------------------------------------
                0.00    0.00       7/7           shapes_read [68]
[29]     0.0    0.00    0.00       7         shape_new [29]
-----------------------------------------------
                0.00    0.00       1/1           ai_test [1]
[30]     0.0    0.00    0.00       1         shape_stream_new [30]
-----------------------------------------------

 This table describes the call tree of the program, and was sorted by
 the total amount of time spent in each function and its children.

 Each entry in this table consists of several lines.  The line with the
 index number at the left hand margin lists the current function.
 The lines above it list the functions that called this function,
 and the lines below it list the functions this one called.
 This line lists:
     index  A unique number given to each element of the table.
        Index numbers are sorted numerically.
        The index number is printed next to every function name so
        it is easier to look up where the function is in the table.

     % time This is the percentage of the `total' time that was spent
        in this function and its children.  Note that due to
        different viewpoints, functions excluded by options, etc,
        these numbers will NOT add up to 100%.

     self   This is the total amount of time spent in this function.

     children   This is the total amount of time propagated into this
        function by its children.

     called This is the number of times the function was called.
        If the function called itself recursively, the number
        only includes non-recursive calls, and is followed by
        a `+' and the number of recursive calls.

     name   The name of the current function.  The index number is
        printed after it.  If the function is a member of a
        cycle, the cycle number is printed between the
        function's name and the index number.


 For the function's parents, the fields have the following meanings:

     self   This is the amount of time that was propagated directly
        from the function into this parent.

     children   This is the amount of time that was propagated from
        the function's children into this parent.

     called This is the number of times this parent called the
        function `/' the total number of times the function
        was called.  Recursive calls to the function are not
        included in the number after the `/'.

     name   This is the name of the parent.  The parent's index
        number is printed after it.  If the parent is a
        member of a cycle, the cycle number is printed between
        the name and the index number.

 If the parents of the function cannot be determined, the word
 `<spontaneous>' is printed in the `name' field, and all the other
 fields are blank.

 For the function's children, the fields have the following meanings:

     self   This is the amount of time that was propagated directly
        from the child into the function.

     children   This is the amount of time that was propagated from the
        child's children to the function.

     called This is the number of times the function called
        this child `/' the total number of times the child
        was called.  Recursive calls by the child are not
        listed in the number after the `/'.

     name   This is the name of the child.  The child's index
        number is printed after it.  If the child is a
        member of a cycle, the cycle number is printed
        between the name and the index number.

 If there are any cycles (circles) in the call graph, there is an
 entry for the cycle-as-a-whole.  This entry shows who called the
 cycle (as parents) and the members of the cycle (as children.)
 The `+' recursive calls entry shows the number of function calls that
 were internal to the cycle, and the calls entry for each member shows,
 for that member, how many times it was called from other members of
 the cycle.

Copyright (C) 2012-2014 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.

Index by function name

   [2] ai_best_move_rec       [21] grid_apply_moves        [3] grid_eval
  [14] block_crust_get         [7] grid_block_add         [10] grid_init
  [11] block_extreme           [5] grid_block_center_top   [9] grid_new
  [13] block_get               [8] grid_block_drop        [24] grid_print
  [25] block_init             [18] grid_block_intersects  [29] shape_new
  [12] block_move             [16] grid_block_remove      [30] shape_stream_new
  [17] block_new              [15] grid_block_set_color   [19] shape_stream_peek
  [26] block_print             [6] grid_block_valid       [28] shape_stream_pop
  [20] drop_amount            [22] grid_clear_lines        [4] shape_stream_test
  [27] game_move_print        [23] grid_cpy

-Ofast错误的调用图 Later I discovered I was combining the -Ofast and -pg flags, and removing -Ofast fixes these issues above. 后来我发现我正在组合-Ofast和-pg标志,并且删除-Ofast可以解决上述问题。

在此处输入图片说明

My questions are: 我的问题是:

  1. Are these two flags incompatible? 这两个标志不兼容吗? I couldn't find any such warning in the docs 我在文档中找不到任何此类警告

  2. If they are not incompatible, is this a gcc bug? 如果它们不兼容,这是gcc错误吗?

  3. How can I work around this to see profiling results that match the most aggressive optimizations so that I don't waste time optimizing the wrong code? 如何解决此问题,以查看与最积极的优化相匹配的性能分析结果,以免浪费时间优化错误的代码?

You want speed, right? 你要速度吧? -Ofast only matters at the bottom of the call stack. -Ofast仅在调用堆栈的底部-Ofast It can't fix things that only you can fix. 它无法解决仅您可以解决的问题。 It can only make them harder to find. 这只会使他们更难找到。 First turn off the optimizer and fix the things you can fix. 首先关闭优化器并修复您可以修复的问题。 Here's an example of how some people do it. 这是一些人如何做的例子。 When you've gone as far as possible with that, turn on the optimizer and let it do its magic. 当您尽可能地做到这一点时,请打开优化器并使其发挥作用。

Small Flame :) Plenty of people say it's a waste of time to profile unoptimized code, but they never say why . 小火焰:)很多人说配置未优化的代码是浪费时间,但是他们从不说原因 They heard it in a lecture hall, read it on a blog, or from stackoverflow, from someone whose clear authority on the subject obviates the need to give a reason. 他们在演讲厅里听到了这个声音,在博客上看了它,或者从stackoverflow上听到了这个声音,而这个人的明确权限使他不必给出理由。

I think it's a case of wishful thinking: My code is basically perfect, so the only ways it could be speeded up are to run the compiler's optimizer. 我认为这是一厢情愿的情况:我的代码基本上是完美的,因此可以加快其速度的唯一方法是运行编译器的优化器。 Then when the profiler shows no way to speed it up - it's obviously right and hurray for me! 然后,当探查器显示无法加快速度时,这显然是正确的,为我加油!

Added in response to comments: Let me digress a little bit (sorry). 为了回应评论而添加:让我有点离题(对不起)。 I understand that one can go over the code carefully, invest effort in data representations, inline some functions, do other high-level optimizations, and measure speedups from doing so. 我了解人们可以仔细检查代码,花精力在数据表示上,内联一些函数,进行其他高级优化,并以此来衡量加速。 Great. 大。 But if your reason for each code change is looking at and just thinking about the code, then as educated as your thinking is, it is still a guess. 但是,如果您每次更改代码的原因只是查看并思考代码,那么按照您的想法进行培训,这仍然是一种猜测。 A guess may be right, but the question you should be asking is "What have I missed?" 猜测可能是正确的,但是您应该问的问题是“我错过了什么?” and "How do I find it?" 和“我如何找到它?”

The method many people and I use is random pausing . 我和很多人使用的方法是随机暂停 The only tool it requires is a debugger, like GDB or any IDE. 它唯一需要的工具是调试器,例如GDB或任何IDE。 It differs from profiling in that it locates unnecessary computation directly , rather than taking measurements and counting on you to decipher them, filter out the noise, look at particular routines, and hunt inside them. 它与概要分析的不同之处在于,它直接定位不必要的计算,而不是进行测量并依靠您来解密,过滤噪声,查看特定例程并在其中进行搜寻。 It tells you down to the particular lines of code, and particular data, what the program is doing and why it's doing it. 它告诉您特定的代码行和特定的数据,程序在做什么以及为什么这样做。 Then you can ask if there's a less wasteful way to do that, and chances are there is. 然后,您可以询问是否有一种不那么浪费的方法来进行操作,并且有机会存在。 What it does not do is tell you the precise fraction of time - you only get a very coarse measurement. 什么它不会做的就是告诉你时间的精确分数-你只得到一个非常粗略测量。 What it does do is identify the problem. 所做的确定问题。 You might think you certainly can't depend on such an inaccurate method, and that's true if you only see the problem on one sample. 您可能会认为您当然不能依靠这种不准确的方法,并且如果您仅在一个样本上看到问题,那就是正确的。 But if you see it on more than one sample, you know it's big, and the fewer overall samples you took to see it more than once, the bigger it is. 但是,如果您在一个以上的样本上看到它,那您就会知道它很大,而一次以上看到的整体样本越少,它就越大。 Here are the statistics behind it. 以下是其背后的统计信息。 And here's the kicker: this will find any problem any profiler will find, and more. 关键在于:这将发现任何探查器将发现的任何问题,以及更多。

Then when you find and fix one problem, you can do it all again, because removal of one problem magnifies the remaining problems. 然后,当您发现并解决一个问题时,您可以再次执行所有操作,因为删除一个问题会放大其余的问题。 In this way you can "ladder" the speedups until you can't any more. 这样,您就可以“加速”加速,直到无法再加速为止。 That's how you get maximal speed as in this example . 如本例所示,这就是获得最大速度的方法 Then, by all means, use -Ofast . 然后,请-Ofast使用-Ofast

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM