| 1 |
{-# LANGUAGE LambdaCase #-} |
| 2 |
{-# LANGUAGE RecordWildCards #-} |
| 3 |
|
| 4 |
module Clash.GHCi.Leak |
| 5 |
( LeakIndicators |
| 6 |
, getLeakIndicators |
| 7 |
, checkLeakIndicators |
| 8 |
) where |
| 9 |
|
| 10 |
import Control.Monad |
| 11 |
import GHC |
| 12 |
import Outputable |
| 13 |
import HscTypes |
| 14 |
import UniqDFM |
| 15 |
import System.Mem |
| 16 |
import System.Mem.Weak |
| 17 |
|
| 18 |
-- Checking for space leaks in GHCi. See #15111, and the |
| 19 |
-- -fghci-leak-check flag. |
| 20 |
|
| 21 |
data LeakIndicators = LeakIndicators [LeakModIndicators] |
| 22 |
|
| 23 |
data LeakModIndicators = LeakModIndicators |
| 24 |
{ leakMod :: Weak HomeModInfo |
| 25 |
, leakIface :: Weak ModIface |
| 26 |
, leakDetails :: Weak ModDetails |
| 27 |
, leakLinkable :: Maybe (Weak Linkable) |
| 28 |
} |
| 29 |
|
| 30 |
-- | Grab weak references to some of the data structures representing |
| 31 |
-- the currently loaded modules. |
| 32 |
getLeakIndicators :: HscEnv -> IO LeakIndicators |
| 33 |
getLeakIndicators HscEnv{..} = |
| 34 |
fmap LeakIndicators $ |
| 35 |
forM (eltsUDFM hsc_HPT) $ \hmi@HomeModInfo{..} -> do |
| 36 |
leakMod <- mkWeakPtr hmi Nothing |
| 37 |
leakIface <- mkWeakPtr hm_iface Nothing |
| 38 |
leakDetails <- mkWeakPtr hm_details Nothing |
| 39 |
leakLinkable <- mapM (`mkWeakPtr` Nothing) hm_linkable |
| 40 |
return $ LeakModIndicators{..} |
| 41 |
|
| 42 |
-- | Look at the LeakIndicators collected by an earlier call to |
| 43 |
-- `getLeakIndicators`, and print messasges if any of them are still |
| 44 |
-- alive. |
| 45 |
checkLeakIndicators :: DynFlags -> LeakIndicators -> IO () |
| 46 |
checkLeakIndicators dflags (LeakIndicators leakmods) = do |
| 47 |
performGC |
| 48 |
forM_ leakmods $ \LeakModIndicators{..} -> do |
| 49 |
deRefWeak leakMod >>= \case |
| 50 |
Nothing -> return () |
| 51 |
Just hmi -> |
| 52 |
report ("HomeModInfo for " ++ |
| 53 |
showSDoc dflags (ppr (mi_module (hm_iface hmi)))) (Just hmi) |
| 54 |
deRefWeak leakIface >>= report "ModIface" |
| 55 |
deRefWeak leakDetails >>= report "ModDetails" |
| 56 |
forM_ leakLinkable $ \l -> deRefWeak l >>= report "Linkable" |
| 57 |
where |
| 58 |
report :: String -> Maybe a -> IO () |
| 59 |
report _ Nothing = return () |
| 60 |
report msg (Just _) = |
| 61 |
putStrLn ("-fghci-leak-check: " ++ msg ++ " is still alive!") |