Opened 4 years ago

Closed 4 years 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 Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


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 (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) (1.4 KB) - added by akio 4 years ago.
0001-rts_apply-uses-CCS_MAIN-rather-than-CCS_SYSTEM-7753.patch (1011 bytes) - added by akio 4 years ago.
use CCS_MAIN in rts_apply

Download all attachments as: .zip

Change History (8)

Changed 4 years ago by akio

Attachment: added

comment:1 Changed 4 years ago by igloo

difficulty: Unknown
Milestone: 7.8.1

thanks for the report

comment:2 Changed 4 years 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 4 years ago by akio

use CCS_MAIN in rts_apply

comment:3 Changed 4 years ago by akio

Status: newpatch

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 4 years ago by Austin Seipp <austin@…>

In 773365f91ff141ad38e929844c901ecd5465a000/ghc:

rts_apply uses CCS_MAIN rather than CCS_SYSTEM (#7753)

Signed-off-by: Austin Seipp <>

comment:5 Changed 4 years ago by thoughtpolice

Merged, thank you Takano!

comment:6 Changed 4 years ago by thoughtpolice

Resolution: fixed
Status: patchclosed

Merged, thanks!

Note: See TracTickets for help on using tickets.