Skip to content

Commit 0724fbd

Browse files
committed
export heap graph facts
1 parent 20eae33 commit 0724fbd

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

external-stg-interpreter/lib/Stg/Interpreter/Debugger/TraverseState.hs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{-# LANGUAGE RecordWildCards, LambdaCase, OverloadedStrings #-}
22
module Stg.Interpreter.Debugger.TraverseState
33
( exportReachableGraph
4+
, exportHeapGraph
5+
, getHeapObjectSummary
6+
, getHeapObjectCategory
47
) where
58

69
import Control.Monad.State
@@ -121,3 +124,51 @@ getHeapObjectCategory = \case
121124
BlackHole{} -> "BlackHole"
122125
ApStack{} -> "ApStack"
123126
RaiseException{} -> "Exception"
127+
128+
exportHeapGraph :: FilePath -> FilePath -> Heap -> IO ()
129+
exportHeapGraph nodesFname edgesFname heap = do
130+
withFile edgesFname WriteMode $ \hEdge -> do
131+
withFile nodesFname WriteMode $ \hNode -> do
132+
BS8.hPutStrLn hNode $ BS8.intercalate "\t"
133+
[ "Id"
134+
, "Label"
135+
, "partition2"
136+
]
137+
BS8.hPutStrLn hEdge $ BS8.intercalate "\t"
138+
[ "Source"
139+
, "Target"
140+
, "partition2"
141+
]
142+
flip evalStateT Set.empty $ do
143+
forM_ (IntMap.toList heap) $ \(addr, obj) -> do
144+
let source = encodeRef addr NS_HeapPtr
145+
146+
genNode node = do
147+
firstTimeVisit <- mark node
148+
when firstTimeVisit $ do
149+
let (ns, idx) = decodeRef node
150+
(nodeLabel, nodeCategory) = case ns of
151+
NS_HeapPtr
152+
| Just ho <- IntMap.lookup idx heap
153+
-> (getHeapObjectSummary ho, getHeapObjectCategory ho)
154+
_ -> (drop 3 $ show ns, drop 3 $ show ns)
155+
-- HINT: write line to node .tsv
156+
liftIO $ do
157+
BS8.hPut hNode $ unGCSymbol node
158+
BS8.hPut hNode "\t"
159+
hPutStr hNode nodeLabel
160+
BS8.hPut hNode "\t"
161+
hPutStr hNode nodeCategory
162+
BS8.hPut hNode "\n"
163+
164+
genNode source
165+
flip visitGCRef obj $ \target -> do
166+
genNode target
167+
-- HINT: write line to edge .tsv
168+
liftIO $ do
169+
BS8.hPut hEdge $ unGCSymbol source
170+
BS8.hPut hEdge "\t"
171+
BS8.hPut hEdge $ unGCSymbol target
172+
BS8.hPut hEdge "\t"
173+
BS8.hPut hEdge "green"
174+
BS8.hPut hEdge "\n"

0 commit comments

Comments
 (0)