Opened 14 months ago

Closed 6 months ago

#7753 closed bug (fixed)

Profiling report broken with foreign exported functions

Reported by: akio Owned by:
Priority: normal Milestone: 7.8.1
Component: Profiling Version: 7.6.2
Keywords: Cc:
Operating System: Linux Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

Save the following Haskell source as wrapper.hs:

import Foreign.Ptr
import Control.Monad

main = do
  fptr <- wrap wrapped
  replicateM 100 $ (return$!) =<< dyn fptr 4 

wrapped :: Double -> IO Double
wrapped x = return $ f 10000 x

f :: Int -> Double -> Double
f 0 u = u
f n u = (u / fromIntegral n) * f (n-1) u

foreign import ccall "wrapper" wrap :: (Double -> IO Double) -> IO (FunPtr (Double -> IO Double))
foreign import ccall "dynamic" dyn :: FunPtr (Double -> IO Double) -> Double -> IO Double

Then compile and run it with:

$ ghc -O2 wrapper.hs -fprof-auto -prof
$ ./wrapper +RTS -p

It generates wrapper.prof (attached). The file contains the following lines:

 CAF        GHC.IO.Encoding.Iconv    58           0    0.0    0.2     0.0    0.2
  wrapped   Main                     80         100    0.0    1.1     0.0    0.0
   f        Main                     81     1000100  100.0    0.0     0.0    0.0

I see two problems here, (1) the inherited column is 0 for 'wrapped' and 'f' but this is incorrect, and (2) 'wrapped' and 'f' belongs to the wrong cost center, 'GHC.IO.Encoding.Iconv'.

Attachments (2)

wrapper.prof (1.4 KB) - added by akio 14 months ago.
0001-rts_apply-uses-CCS_MAIN-rather-than-CCS_SYSTEM-7753.patch (1011 bytes) - added by akio 7 months ago.
use CCS_MAIN in rts_apply

Download all attachments as: .zip

Change History (8)

Changed 14 months ago by akio

comment:1 Changed 13 months ago by igloo

  • Difficulty set to Unknown
  • Milestone set to 7.8.1

thanks for the report

comment:2 Changed 7 months ago by akio

I think I know what is happening. The wrapper and f cost centres are actually placed under SYSTEM. I can see it by using +RTS -pa instead of +RTS -p. However SYSTEM is a hidden cost centre, so by default it's not included in the report, resulting in a confusing call tree.

Changed 7 months ago by akio

use CCS_MAIN in rts_apply

comment:3 Changed 7 months ago by akio

  • Status changed from new to patch

I'm not sure if this is the best way to fix it, but the attached patch improves the output; now wrapper and f are placed directly under MAIN, and they get inherited costs correctly.

comment:4 Changed 6 months ago by Austin Seipp <austin@…>

In 773365f91ff141ad38e929844c901ecd5465a000/ghc:

rts_apply uses CCS_MAIN rather than CCS_SYSTEM (#7753)

Signed-off-by: Austin Seipp <austin@well-typed.com>

comment:5 Changed 6 months ago by thoughtpolice

Merged, thank you Takano!

comment:6 Changed 6 months ago by thoughtpolice

  • Resolution set to fixed
  • Status changed from patch to closed

Merged, thanks!

Note: See TracTickets for help on using tickets.