ghc-heap-view icon indicating copy to clipboard operation
ghc-heap-view copied to clipboard

How can I use ghc-heap-view to observe the thunk evaluation process.

Open yihuang opened this issue 9 years ago • 3 comments

For example, I've written this code trying to observe evaluation process of foldr and foldl, but it end up to a dead loop. Maybe it's because buildHeapGraph a would trigger evaluation of a? Is what I want possible with ghc-heap-view ?

https://gist.github.com/yihuang/f24675aee03c99cd4a6c

yihuang avatar Nov 24 '15 02:11 yihuang

What version of GHC are you on?

nomeata avatar Nov 24 '15 09:11 nomeata

Ah, judging from your imports, 7.10 :-). The program is better tested on older GHCs, I’ll see if that works better.

nomeata avatar Nov 24 '15 09:11 nomeata

Your observe returns a, and then you seq it. That’s your loop. Try

observe :: a -> b -> b
observe a b = unsafePerformIO $ do
    performGC
    s <- ppHeapGraph <$> buildHeapGraph 1000 () (asBox a)
    putStrLn s
    return b
{-# NOINLINE observe #-}

and

foldr f z l = result
    where
    result = loop f z l
    loop f z []     = observe result z
    loop f z (x:xs) = observe result (x `f` loop f z xs)

Now it does not loop, but it’s not very helpful either:

_ind (_bh _unsupported)
_ind (_bh _unsupported)
_ind (_bh _unsupported)
_ind (_bh _unsupported)
_ind (_bh _unsupported)
_ind (_bh _unsupported)
_ind (_bh _unsupported)
_ind (_bh _unsupported)
_ind (_bh _unsupported)
_ind (_bh _unsupported)
_ind (_bh _unsupported)

Maybe the value is on the stack, which is not well-supported by ghc-heap-view?

nomeata avatar Nov 24 '15 09:11 nomeata