Skip to content

Commit 5130f53

Browse files
committed
STM stats in ghcide
1 parent 35ed108 commit 5130f53

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

ghcide/session-loader/Development/IDE/Session.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ import System.Info
7575
import Control.Applicative (Alternative ((<|>)))
7676
import Data.Void
7777

78-
import Control.Concurrent.STM (atomically)
78+
import Control.Concurrent.STM.Stats (atomically)
7979
import Control.Concurrent.STM.TQueue
8080
import Data.Foldable (for_)
8181
import qualified Data.HashSet as Set

ghcide/src/Development/IDE/Core/Compile.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ import GHC (GetDocsFailure (..),
9696
parsedSource)
9797

9898
import Control.Concurrent.Extra
99-
import Control.Concurrent.STM hiding (orElse)
99+
import Control.Concurrent.STM.Stats hiding (orElse)
100100
import Data.Aeson (toJSON)
101101
import Data.Binary
102102
import Data.Coerce

ghcide/src/Development/IDE/Core/ProgressReporting.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Development.IDE.Core.ProgressReporting
1414
where
1515

1616
import Control.Concurrent.Async
17-
import Control.Concurrent.STM.Stats (STM, TVar, atomically,
17+
import Control.Concurrent.STM.Stats (STM, TVar, atomicallyNamed,
1818
newTVarIO, readTVar,
1919
readTVarIO, writeTVar)
2020
import Control.Concurrent.Strict
@@ -186,7 +186,7 @@ delayedProgressReporting before after lspEnv optProgressStyle = do
186186
-- Do not remove the eta-expansion without profiling a session with at
187187
-- least 1000 modifications.
188188
where
189-
f shift = atomically $ recordProgress inProgress file shift
189+
f shift = atomicallyNamed "recordProgress" $ recordProgress inProgress file shift
190190

191191
mRunLspT :: Applicative m => Maybe (LSP.LanguageContextEnv c ) -> LSP.LspT c m () -> m ()
192192
mRunLspT (Just lspEnv) f = LSP.runLspT lspEnv f

ghcide/src/Development/IDE/Core/Shake.hs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ import GHC.Fingerprint
148148
import Language.LSP.Types.Capabilities
149149
import OpenTelemetry.Eventlog
150150

151+
import Control.Concurrent.STM.Stats (atomicallyNamed)
151152
import Control.Exception.Extra hiding (bracket_)
152153
import Data.Aeson (toJSON)
153154
import qualified Data.ByteString.Char8 as BS8
@@ -340,7 +341,7 @@ lastValueIO s@ShakeExtras{positionMapping,persistentKeys,state} k file = do
340341
f <- MaybeT $ pure $ HMap.lookup (Key k) pmap
341342
(dv,del,ver) <- MaybeT $ runIdeAction "lastValueIO" s $ f file
342343
MaybeT $ pure $ (,del,ver) <$> fromDynamic dv
343-
atomically $ case mv of
344+
atomicallyNamed "lastValueIO" $ case mv of
344345
Nothing -> do
345346
STM.focus (Focus.alter (alterValue $ Failed True)) (toKey k file) state
346347
return Nothing
@@ -356,13 +357,13 @@ lastValueIO s@ShakeExtras{positionMapping,persistentKeys,state} k file = do
356357
-- Something already succeeded before, leave it alone
357358
_ -> old
358359

359-
atomically (STM.lookup (toKey k file) state) >>= \case
360+
atomicallyNamed "lastValueIO 4" (STM.lookup (toKey k file) state) >>= \case
360361
Nothing -> readPersistent
361362
Just (ValueWithDiagnostics v _) -> case v of
362363
Succeeded ver (fromDynamic -> Just v) ->
363-
atomically $ Just . (v,) <$> mappingForVersion positionMapping file ver
364+
atomicallyNamed "lastValueIO 5" $ Just . (v,) <$> mappingForVersion positionMapping file ver
364365
Stale del ver (fromDynamic -> Just v) ->
365-
atomically $ Just . (v,) . maybe id addDelta del <$> mappingForVersion positionMapping file ver
366+
atomicallyNamed "lastValueIO 6" $ Just . (v,) . maybe id addDelta del <$> mappingForVersion positionMapping file ver
366367
Failed p | not p -> readPersistent
367368
_ -> pure Nothing
368369

@@ -439,7 +440,7 @@ deleteValue
439440
-> k
440441
-> NormalizedFilePath
441442
-> STM ()
442-
deleteValue ShakeExtras{dirtyKeys, state} key file = do
443+
deleteValue ShakeExtras{dirtyKeys, state} key file = atomicallyNamed "deleteValue" $ do
443444
STM.delete (toKey key file) state
444445
modifyTVar' dirtyKeys $ HSet.insert (toKey key file)
445446

@@ -454,7 +455,6 @@ recordDirtyKeys ShakeExtras{dirtyKeys} key file = do
454455
return $ withEventTrace "recordDirtyKeys" $ \addEvent -> do
455456
addEvent (fromString $ "dirty " <> show key) (fromString $ unlines $ map fromNormalizedFilePath file)
456457

457-
458458
-- | We return Nothing if the rule has not run and Just Failed if it has failed to produce a value.
459459
getValues ::
460460
forall k v.
@@ -627,8 +627,8 @@ shakeRestart IdeState{..} reason acts =
627627
(\runner -> do
628628
(stopTime,()) <- duration (cancelShakeSession runner)
629629
res <- shakeDatabaseProfile shakeDb
630-
backlog <- readTVarIO (dirtyKeys shakeExtras)
631-
queue <- atomically $ peekInProgress $ actionQueue shakeExtras
630+
backlog <- readTVarIO $ dirtyKeys shakeExtras
631+
queue <- atomicallyNamed "actionQueue - peek" $ peekInProgress $ actionQueue shakeExtras
632632
let profile = case res of
633633
Just fp -> ", profile saved at " <> fp
634634
_ -> ""
@@ -661,7 +661,7 @@ notifyTestingLogMessage extras msg = do
661661
shakeEnqueue :: ShakeExtras -> DelayedAction a -> IO (IO a)
662662
shakeEnqueue ShakeExtras{actionQueue, logger} act = do
663663
(b, dai) <- instantiateDelayedAction act
664-
atomically $ pushQueue dai actionQueue
664+
atomicallyNamed "actionQueue - push" $ pushQueue dai actionQueue
665665
let wait' b =
666666
waitBarrier b `catches`
667667
[ Handler(\BlockedIndefinitelyOnMVar ->
@@ -670,7 +670,7 @@ shakeEnqueue ShakeExtras{actionQueue, logger} act = do
670670
, Handler (\e@AsyncCancelled -> do
671671
logPriority logger Debug $ T.pack $ actionName act <> " was cancelled"
672672

673-
atomically $ abortQueue dai actionQueue
673+
atomicallyNamed "actionQueue - abort" $ abortQueue dai actionQueue
674674
throw e)
675675
]
676676
return (wait' b >>= either throwIO return)
@@ -685,7 +685,7 @@ newSession
685685
-> IO ShakeSession
686686
newSession extras@ShakeExtras{..} shakeDb acts reason = do
687687
IdeOptions{optRunSubset} <- getIdeOptionsIO extras
688-
reenqueued <- atomically $ peekInProgress actionQueue
688+
reenqueued <- atomicallyNamed "actionQueue - peek" $ peekInProgress actionQueue
689689
allPendingKeys <-
690690
if optRunSubset
691691
then Just <$> readTVarIO dirtyKeys
@@ -694,14 +694,14 @@ newSession extras@ShakeExtras{..} shakeDb acts reason = do
694694
-- A daemon-like action used to inject additional work
695695
-- Runs actions from the work queue sequentially
696696
pumpActionThread otSpan = do
697-
d <- liftIO $ atomically $ popQueue actionQueue
697+
d <- liftIO $ atomicallyNamed "action queue - pop" $ popQueue actionQueue
698698
actionFork (run otSpan d) $ \_ -> pumpActionThread otSpan
699699

700700
-- TODO figure out how to thread the otSpan into defineEarlyCutoff
701701
run _otSpan d = do
702702
start <- liftIO offsetTime
703703
getAction d
704-
liftIO $ atomically $ doneQueue d actionQueue
704+
liftIO $ atomicallyNamed "actionQueue - done" $ doneQueue d actionQueue
705705
runTime <- liftIO start
706706
let msg = T.pack $ "finish: " ++ actionName d
707707
++ " (took " ++ showDuration runTime ++ ")"
@@ -804,7 +804,7 @@ garbageCollectKeys label maxAge checkParents agedKeys = do
804804
| age > maxAge
805805
, Just (kt,_) <- fromKeyType k
806806
, not(kt `HSet.member` preservedKeys checkParents)
807-
= atomically $ do
807+
= atomicallyNamed "GC" $ do
808808
gotIt <- STM.focus (Focus.member <* Focus.delete) k values
809809
when gotIt $
810810
modifyTVar' dk (HSet.insert k)
@@ -908,7 +908,7 @@ useWithStaleFast' key file = do
908908
wait <- delayedAction $ mkDelayedAction ("C:" ++ show key ++ ":" ++ fromNormalizedFilePath file) Debug $ use key file
909909

910910
s@ShakeExtras{state} <- askShake
911-
r <- liftIO $ atomically $ getValues state key file
911+
r <- liftIO $ atomicallyNamed "useStateFast" $ getValues state key file
912912
liftIO $ case r of
913913
-- block for the result if we haven't computed before
914914
Nothing -> do
@@ -1017,7 +1017,7 @@ defineEarlyCutoff' doDiagnostics cmp key file old mode action = do
10171017
(if optSkipProgress options key then id else inProgress progress file) $ do
10181018
val <- case old of
10191019
Just old | mode == RunDependenciesSame -> do
1020-
v <- liftIO $ atomically $ getValues state key file
1020+
v <- liftIO $ atomicallyNamed "defineEarlyCutoff - read 1" $ getValues state key file
10211021
case v of
10221022
-- No changes in the dependencies and we have
10231023
-- an existing successful result.
@@ -1036,10 +1036,10 @@ defineEarlyCutoff' doDiagnostics cmp key file old mode action = do
10361036
(do v <- action; liftIO $ evaluate $ force v) $
10371037
\(e :: SomeException) -> do
10381038
pure (Nothing, ([ideErrorText file $ T.pack $ show e | not $ isBadDependency e],Nothing))
1039-
modTime <- liftIO $ (currentValue . fst =<<) <$> atomically (getValues state GetModificationTime file)
1039+
modTime <- liftIO $ (currentValue . fst =<<) <$> atomicallyNamed "defineEarlyCutoff - read 2" (getValues state GetModificationTime file)
10401040
(bs, res) <- case res of
10411041
Nothing -> do
1042-
staleV <- liftIO $ atomically $ getValues state key file
1042+
staleV <- liftIO $ atomicallyNamed "defineEarlyCutoff -read 3" $ getValues state key file
10431043
pure $ case staleV of
10441044
Nothing -> (toShakeValue ShakeResult bs, Failed False)
10451045
Just v -> case v of
@@ -1050,7 +1050,7 @@ defineEarlyCutoff' doDiagnostics cmp key file old mode action = do
10501050
(Failed b, _) ->
10511051
(toShakeValue ShakeResult bs, Failed b)
10521052
Just v -> pure (maybe ShakeNoCutoff ShakeResult bs, Succeeded (vfsVersion =<< modTime) v)
1053-
liftIO $ atomically $ setValues state key file res (Vector.fromList diags)
1053+
liftIO $ atomicallyNamed "defineEarlyCutoff - write" $ setValues state key file res (Vector.fromList diags)
10541054
doDiagnostics diags
10551055
let eq = case (bs, fmap decodeShakeValue old) of
10561056
(ShakeResult a, Just (ShakeResult b)) -> cmp a b
@@ -1062,7 +1062,7 @@ defineEarlyCutoff' doDiagnostics cmp key file old mode action = do
10621062
(if eq then ChangedRecomputeSame else ChangedRecomputeDiff)
10631063
(encodeShakeValue bs) $
10641064
A res
1065-
liftIO $ atomically $ modifyTVar' dirtyKeys (HSet.delete $ toKey key file)
1065+
liftIO $ atomicallyNamed "defineEarlyCutoff - dirtyKeys" $ modifyTVar' dirtyKeys (HSet.delete $ toKey key file)
10661066
return res
10671067

10681068
traceA :: A v -> String
@@ -1150,7 +1150,7 @@ updateFileDiagnostics :: MonadIO m
11501150
-> [(ShowDiagnostic,Diagnostic)] -- ^ current results
11511151
-> m ()
11521152
updateFileDiagnostics fp k ShakeExtras{logger, diagnostics, hiddenDiagnostics, publishedDiagnostics, state, debouncer, lspEnv} current = liftIO $ do
1153-
modTime <- (currentValue . fst =<<) <$> atomically (getValues state GetModificationTime fp)
1153+
modTime <- (currentValue . fst =<<) <$> atomicallyNamed "diagnostics - read" (getValues state GetModificationTime fp)
11541154
let (currentShown, currentHidden) = partition ((== ShowDiag) . fst) current
11551155
uri = filePathToUri' fp
11561156
ver = vfsVersion =<< modTime
@@ -1160,13 +1160,13 @@ updateFileDiagnostics fp k ShakeExtras{logger, diagnostics, hiddenDiagnostics, p
11601160
-- published. Otherwise, we might never publish certain diagnostics if
11611161
-- an exception strikes between modifyVar but before
11621162
-- publishDiagnosticsNotification.
1163-
newDiags <- liftIO $ atomically $ update (map snd currentShown) diagnostics
1164-
_ <- liftIO $ atomically $ update (map snd currentHidden) hiddenDiagnostics
1163+
newDiags <- liftIO $ atomicallyNamed "diagnostics - update" $ update (map snd currentShown) diagnostics
1164+
_ <- liftIO $ atomicallyNamed "diagnostics - hidden" $ update (map snd currentHidden) hiddenDiagnostics
11651165
let uri = filePathToUri' fp
11661166
let delay = if null newDiags then 0.1 else 0
11671167
registerEvent debouncer delay uri $ do
11681168
join $ mask_ $ do
1169-
lastPublish <- atomically $ STM.focus (Focus.lookupWithDefault [] <* Focus.insert newDiags) uri publishedDiagnostics
1169+
lastPublish <- atomicallyNamed "diagnostics - publish" $ STM.focus (Focus.lookupWithDefault [] <* Focus.insert newDiags) uri publishedDiagnostics
11701170
let action = when (lastPublish /= newDiags) $ case lspEnv of
11711171
Nothing -> -- Print an LSP event.
11721172
logInfo logger $ showDiagnosticsColored $ map (fp,ShowDiag,) newDiags

0 commit comments

Comments
 (0)