Skip to content

Commit bd3e56d

Browse files
committed
add descriptive field names to BlackHole heap object ; also add owner thread id field to BlackHole
1 parent 5ceaa45 commit bd3e56d

File tree

7 files changed

+39
-21
lines changed

7 files changed

+39
-21
lines changed

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,20 +184,25 @@ builtinStgEval so a@HeapPtr{} = do
184184
Debugger.checkBreakpoint $ BkpCustom "eval"
185185
case o of
186186
ApStack{..} -> do
187+
tid <- gets ssCurrentThreadId
187188
let HeapPtr l = a
188-
store l (BlackHole o []) -- HINT: prevent duplicate computation
189+
-- HINT: prevent duplicate computation
190+
store l BlackHole
191+
{ hoBHOwnerThreadId = tid
192+
, hoBHOriginalThunk = o
193+
, hoBHWaitQueue = []
194+
}
189195
stackPush (Update l) -- HINT: ensure sharing, ApStack is always created from Update frame
190196
mapM_ stackPush (reverse hoStack)
191197
pure hoResult
192198
RaiseException ex -> PrimExceptions.raiseEx ex
193199
Con{} -> pure [a]
194-
195-
BlackHole ho waitingThreads -> do
200+
BlackHole{..} -> do
196201
let HeapPtr addr = a
197202
tid <- gets ssCurrentThreadId
198203
ts <- getThreadState tid
199204
updateThreadState tid (ts {tsStatus = ThreadBlocked (BlockedOnBlackHole addr)})
200-
store addr (BlackHole ho $ tid : waitingThreads)
205+
store addr o {hoBHWaitQueue = tid : hoBHWaitQueue}
201206
stackPush (Apply []) -- retry evaluation next time also
202207
stackPush $ RunScheduler SR_ThreadBlocked
203208
pure [a]
@@ -252,7 +257,12 @@ builtinStgEval so a@HeapPtr{} = do
252257
-- read: http://mainisusuallyafunction.blogspot.com/2011/10/thunks-and-lazy-blackholes-introduction.html
253258
-- read: https://www.microsoft.com/en-us/research/wp-content/uploads/2005/09/2005-haskell.pdf
254259
stackPush (Update l)
255-
store l (BlackHole o [])
260+
tid <- gets ssCurrentThreadId
261+
store l BlackHole
262+
{ hoBHOwnerThreadId = tid
263+
, hoBHOriginalThunk = o
264+
, hoBHWaitQueue = []
265+
}
256266
evalExpr extendedEnv e
257267
SingleEntry -> do
258268
tid <- gets ssCurrentThreadId
@@ -274,19 +284,24 @@ builtinStgApply so a@HeapPtr{} args = do
274284
case o of
275285
ApStack{..} -> do
276286
let HeapPtr l = a
277-
store l (BlackHole o []) -- HINT: prevent duplicate computation
287+
tid <- gets ssCurrentThreadId
288+
-- HINT: prevent duplicate computation
289+
store l BlackHole
290+
{ hoBHOwnerThreadId = tid
291+
, hoBHOriginalThunk = o
292+
, hoBHWaitQueue = []
293+
}
278294
stackPush (Apply args)
279295
stackPush (Update l) -- HINT: ensure sharing, ApStack is always created from Update frame
280296
mapM_ stackPush (reverse hoStack)
281297
pure hoResult
282298
RaiseException ex -> PrimExceptions.raiseEx ex
283299
Con{} -> stgErrorM $ "unexpected con at apply: " ++ show o ++ ", args: " ++ show args ++ ", static-origin: " ++ show so
284-
285-
BlackHole ho waitingThreads -> do
300+
BlackHole{..} -> do
286301
tid <- gets ssCurrentThreadId
287302
ts <- getThreadState tid
288303
updateThreadState tid (ts {tsStatus = ThreadBlocked (BlockedOnBlackHole addr)})
289-
store addr (BlackHole ho $ tid : waitingThreads)
304+
store addr o {hoBHWaitQueue = tid : hoBHWaitQueue}
290305
stackPush (Apply args) -- retry evaluation next time also
291306
stackPush $ RunScheduler SR_ThreadBlocked
292307
pure [a]

external-stg-interpreter/lib/Stg/Interpreter/Base.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,11 @@ data HeapObject
162162
, hoCloArgs :: [Atom]
163163
, hoCloMissing :: Int -- HINT: this is a Thunk if 0 arg is missing ; if all is missing then Fun ; Pap is some arg is provided
164164
}
165-
| BlackHole HeapObject [Int] -- original heap object, blocking queue of thread ids
166-
-- NOTE: each blackhole has exactly one corresponding thread and one update frame
165+
| BlackHole -- NOTE: each blackhole has exactly one corresponding thread and one update frame
166+
{ hoBHOwnerThreadId :: Int -- owner thread id
167+
, hoBHOriginalThunk :: HeapObject -- original heap object
168+
, hoBHWaitQueue :: [Int] -- blocking queue of thread ids
169+
}
167170
| ApStack -- HINT: needed for the async exceptions
168171
{ hoResult :: [Atom]
169172
, hoStack :: [StackContinuation]
@@ -1447,7 +1450,7 @@ debugPrintHeapObject :: HeapObject -> String
14471450
debugPrintHeapObject = \case
14481451
Con{..} -> "Con: " ++ show (dcUniqueName $ unDC hoCon) ++ " " ++ show hoConArgs
14491452
Closure{..} -> "Clo: " ++ show hoName ++ " args: " ++ show hoCloArgs ++ " env: " ++ show (Map.size hoEnv) ++ " missing: " ++ show hoCloMissing
1450-
BlackHole o _q -> "BlackHole: " ++ debugPrintHeapObject o
1453+
BlackHole{..} -> "BlackHole: " ++ debugPrintHeapObject hoBHOriginalThunk
14511454
ApStack{} -> "ApStack"
14521455
RaiseException ex -> "RaiseException: " ++ show ex
14531456

@@ -1484,9 +1487,9 @@ mylog msg = do
14841487

14851488
wakeupBlackHoleQueueThreads :: Int -> M ()
14861489
wakeupBlackHoleQueueThreads addr = readHeap (HeapPtr addr) >>= \case
1487-
(BlackHole _ waitingThreads) -> do
1490+
BlackHole{..} -> do
14881491
-- wake up blocked threads
1489-
forM_ waitingThreads $ \waitingTid -> do
1492+
forM_ hoBHWaitQueue $ \waitingTid -> do
14901493
waitingTS <- getThreadState waitingTid
14911494
case tsStatus waitingTS of
14921495
ThreadBlocked (BlockedOnBlackHole dstAddr) -> do

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ exportStgStateM stgState@StgState{..} = do
203203
forM_ (zip [0..] (Map.toList hoEnv)) $ \(idx, (n, a)) -> do
204204
addFact "Heap_ClosureEnv" [I i, I idx, ID n, A $ snd a]
205205

206-
BlackHole o _ -> do
206+
BlackHole _ o _ -> do
207207
addFact "Heap_BlackHole" [I i, S (debugPrintHeapObject o)]
208208

209209
ApStack{..} -> do

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ printHeapObject = \case
129129
printEnv hoEnv
130130
putStrLn $ "source location: " ++ (ppSrcSpan . binderDefLoc . unId $ hoName)
131131

132-
BlackHole ho _ -> do
132+
BlackHole{..} -> do
133133
putStrLn "BlackHole:"
134-
printHeapObject ho
134+
printHeapObject hoBHOriginalThunk
135135
putStrLn ""
136136

137137
ApStack{} -> do

external-stg-interpreter/lib/Stg/Interpreter/GC/GCRef.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ instance VisitGCRef HeapObject where
2626
visitGCRef action = \case
2727
Con{..} -> visitGCRef action hoConArgs
2828
Closure{..} -> visitGCRef action hoCloArgs >> visitGCRef action hoEnv
29-
BlackHole _o _q -> pure () -- HINT: the blackhole wait queue is handled separately
29+
BlackHole _ _ _ -> pure () -- HINT: the blackhole wait queue is handled separately
3030
ApStack{..} -> visitGCRef action hoResult >> visitGCRef action hoStack
3131
RaiseException ex -> visitGCRef action ex
3232

external-stg-interpreter/lib/Stg/Interpreter/GC/LiveDataAnalysis.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ addReferenceFacts prog StgState{..} = do
169169
, Update addr <- tsStack ts
170170
]
171171
forM_ blackholes $ \(tid, addr) -> case IntMap.lookup addr ssHeap of
172-
Just (BlackHole _ waitingThreads) -> do
172+
Just (BlackHole _ _ waitingThreads) -> do
173173
forM_ waitingThreads $ \waitingTid -> do
174174
addReference (encodeRef tid NS_Thread) (encodeRef waitingTid NS_Thread)
175175
ho -> error $ "internal error - expected Blackhole, got: " ++ show ho

external-stg-interpreter/lib/Stg/Interpreter/PrimOp/Concurrency.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,5 +312,5 @@ removeFromMVarQueue tid m = do
312312
removeFromBlackHoleQueue :: Int -> Int -> M ()
313313
removeFromBlackHoleQueue tid addr = do
314314
readHeap (HeapPtr addr) >>= \case
315-
(BlackHole o queue) -> modify' $ \s@StgState{..} -> s { ssHeap = IntMap.insert addr (BlackHole o $ filter (tid /=) queue) ssHeap }
316-
x -> error $ "internal error - expected BlackHole, got: " ++ show x
315+
bh@BlackHole{..} -> modify' $ \s@StgState{..} -> s { ssHeap = IntMap.insert addr (bh {hoBHWaitQueue = filter (tid /=) hoBHWaitQueue}) ssHeap }
316+
x -> error $ "internal error - expected BlackHole, got: " ++ show x

0 commit comments

Comments
 (0)