Skip to content

Commit e5fa387

Browse files
committed
STM stats in ghcide
1 parent 0491461 commit e5fa387

File tree

6 files changed

+40
-38
lines changed

6 files changed

+40
-38
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/FileExists.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ module Development.IDE.Core.FileExists
1010
)
1111
where
1212

13-
import Control.Concurrent.STM (atomically)
13+
import Control.Concurrent.STM.Stats (atomically,
14+
atomicallyNamed)
1415
import Control.Exception
1516
import Control.Monad.Extra
1617
import Control.Monad.IO.Class
@@ -94,7 +95,7 @@ modifyFileExists state changes = do
9495
-- Masked to ensure that the previous values are flushed together with the map update
9596
mask $ \_ -> do
9697
-- update the map
97-
void $ atomically $ forM_ changes $ \(f,c) ->
98+
void $ atomicallyNamed "modifyFileExists" $ forM_ changes $ \(f,c) ->
9899
case fromChange c of
99100
Just c' -> STM.focus (Focus.insert c') f var
100101
Nothing -> pure ()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module Development.IDE.Core.FileStore(
2424
registerFileWatches
2525
) where
2626

27-
import Control.Concurrent.STM (atomically,
27+
import Control.Concurrent.STM.Stats (atomically,
2828
modifyTVar')
2929
import Control.Concurrent.STM.TQueue (writeTQueue)
3030
import Control.Concurrent.Strict

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

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

1616
import Control.Concurrent.Async
17-
import Control.Concurrent.STM (TVar, atomically, newTVarIO,
18-
readTVar, readTVarIO,
19-
writeTVar)
17+
import Control.Concurrent.STM.Stats (TVar, atomicallyNamed,
18+
newTVarIO, readTVar,
19+
readTVarIO, writeTVar)
2020
import Control.Concurrent.Strict
2121
import Control.Monad.Extra
2222
import Control.Monad.IO.Class
@@ -83,7 +83,7 @@ newInProgress :: IO InProgressState
8383
newInProgress = InProgressState <$> newTVarIO 0 <*> newTVarIO 0 <*> STM.newIO
8484

8585
recordProgress :: InProgressState -> NormalizedFilePath -> (Int -> Int) -> IO ()
86-
recordProgress InProgressState{..} file shift = atomically $ do
86+
recordProgress InProgressState{..} file shift = atomicallyNamed "recordProgress" $ do
8787
done <- readTVar doneVar
8888
todo <- readTVar todoVar
8989
(prev, new) <- STM.focus alterPrevAndNew file currentVar

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

Lines changed: 30 additions & 29 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
@@ -342,11 +343,11 @@ lastValueIO s@ShakeExtras{positionMapping,persistentKeys,state} k file = do
342343
MaybeT $ pure $ (,del,ver) <$> fromDynamic dv
343344
case mv of
344345
Nothing -> do
345-
void $ atomically $ STM.focus (Focus.alter (alterValue $ Failed True)) (toKey k file) state
346+
void $ atomicallyNamed "lastValueIO 1" $ STM.focus (Focus.alter (alterValue $ Failed True)) (toKey k file) state
346347
return Nothing
347348
Just (v,del,ver) -> do
348-
void $ atomically $ STM.focus (Focus.alter (alterValue $ Stale (Just del) ver (toDyn v))) (toKey k file) state
349-
atomically $ Just . (v,) . addDelta del <$> mappingForVersion positionMapping file ver
349+
void $ atomicallyNamed "lastValueIO 2" $ STM.focus (Focus.alter (alterValue $ Stale (Just del) ver (toDyn v))) (toKey k file) state
350+
atomicallyNamed "lastValueIO 3" $ Just . (v,) . addDelta del <$> mappingForVersion positionMapping file ver
350351

351352
-- We got a new stale value from the persistent rule, insert it in the map without affecting diagnostics
352353
alterValue new Nothing = Just (ValueWithDiagnostics new mempty) -- If it wasn't in the map, give it empty diagnostics
@@ -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
-> IO ()
442-
deleteValue ShakeExtras{dirtyKeys, state} key file = atomically $ 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

@@ -450,7 +451,7 @@ recordDirtyKeys
450451
-> [NormalizedFilePath]
451452
-> IO ()
452453
recordDirtyKeys ShakeExtras{dirtyKeys} key file = withEventTrace "recordDirtyKeys" $ \addEvent -> do
453-
atomically $ modifyTVar' dirtyKeys $ \x -> foldl' (flip HSet.insert) x (toKey key <$> file)
454+
atomicallyNamed "recordDirtyKeys" $ modifyTVar' dirtyKeys $ \x -> foldl' (flip HSet.insert) x (toKey key <$> file)
454455
addEvent (fromString $ "dirty " <> show key) (fromString $ unlines $ map fromNormalizedFilePath file)
455456

456457

@@ -626,8 +627,8 @@ shakeRestart IdeState{..} reason acts =
626627
(\runner -> do
627628
(stopTime,()) <- duration (cancelShakeSession runner)
628629
res <- shakeDatabaseProfile shakeDb
629-
backlog <- readTVarIO (dirtyKeys shakeExtras)
630-
queue <- atomically $ peekInProgress $ actionQueue shakeExtras
630+
backlog <- readTVarIO $ dirtyKeys shakeExtras
631+
queue <- atomicallyNamed "actionQueue - peek" $ peekInProgress $ actionQueue shakeExtras
631632
let profile = case res of
632633
Just fp -> ", profile saved at " <> fp
633634
_ -> ""
@@ -660,7 +661,7 @@ notifyTestingLogMessage extras msg = do
660661
shakeEnqueue :: ShakeExtras -> DelayedAction a -> IO (IO a)
661662
shakeEnqueue ShakeExtras{actionQueue, logger} act = do
662663
(b, dai) <- instantiateDelayedAction act
663-
atomically $ pushQueue dai actionQueue
664+
atomicallyNamed "actionQueue - push" $ pushQueue dai actionQueue
664665
let wait' b =
665666
waitBarrier b `catches`
666667
[ Handler(\BlockedIndefinitelyOnMVar ->
@@ -669,7 +670,7 @@ shakeEnqueue ShakeExtras{actionQueue, logger} act = do
669670
, Handler (\e@AsyncCancelled -> do
670671
logPriority logger Debug $ T.pack $ actionName act <> " was cancelled"
671672

672-
atomically $ abortQueue dai actionQueue
673+
atomicallyNamed "actionQueue - abort" $ abortQueue dai actionQueue
673674
throw e)
674675
]
675676
return (wait' b >>= either throwIO return)
@@ -684,7 +685,7 @@ newSession
684685
-> IO ShakeSession
685686
newSession extras@ShakeExtras{..} shakeDb acts reason = do
686687
IdeOptions{optRunSubset} <- getIdeOptionsIO extras
687-
reenqueued <- atomically $ peekInProgress actionQueue
688+
reenqueued <- atomicallyNamed "actionQueue - peek" $ peekInProgress actionQueue
688689
allPendingKeys <-
689690
if optRunSubset
690691
then Just <$> readTVarIO dirtyKeys
@@ -693,14 +694,14 @@ newSession extras@ShakeExtras{..} shakeDb acts reason = do
693694
-- A daemon-like action used to inject additional work
694695
-- Runs actions from the work queue sequentially
695696
pumpActionThread otSpan = do
696-
d <- liftIO $ atomically $ popQueue actionQueue
697+
d <- liftIO $ atomicallyNamed "action queue - pop" $ popQueue actionQueue
697698
actionFork (run otSpan d) $ \_ -> pumpActionThread otSpan
698699

699700
-- TODO figure out how to thread the otSpan into defineEarlyCutoff
700701
run _otSpan d = do
701702
start <- liftIO offsetTime
702703
getAction d
703-
liftIO $ atomically $ doneQueue d actionQueue
704+
liftIO $ atomicallyNamed "actionQueue - done" $ doneQueue d actionQueue
704705
runTime <- liftIO start
705706
let msg = T.pack $ "finish: " ++ actionName d
706707
++ " (took " ++ showDuration runTime ++ ")"
@@ -759,11 +760,11 @@ instantiateDelayedAction (DelayedAction _ s p a) = do
759760

760761
getDiagnostics :: IdeState -> IO [FileDiagnostic]
761762
getDiagnostics IdeState{shakeExtras = ShakeExtras{diagnostics}} = do
762-
atomically $ getAllDiagnostics diagnostics
763+
atomicallyNamed "getAllDiagnostics" $ getAllDiagnostics diagnostics
763764

764765
getHiddenDiagnostics :: IdeState -> IO [FileDiagnostic]
765766
getHiddenDiagnostics IdeState{shakeExtras = ShakeExtras{hiddenDiagnostics}} = do
766-
atomically $ getAllDiagnostics hiddenDiagnostics
767+
atomicallyNamed "getAllDiagnostics - hidden" $ getAllDiagnostics hiddenDiagnostics
767768

768769
-- | Find and release old keys from the state Hashmap
769770
-- For the record, there are other state sources that this process does not release:
@@ -803,7 +804,7 @@ garbageCollectKeys label maxAge checkParents agedKeys = do
803804
| age > maxAge
804805
, Just (kt,_) <- fromKeyType k
805806
, not(kt `HSet.member` preservedKeys checkParents)
806-
= atomically $ do
807+
= atomicallyNamed "GC" $ do
807808
gotIt <- STM.focus (Focus.member <* Focus.delete) k values
808809
when gotIt $
809810
modifyTVar' dk (HSet.insert k)
@@ -907,7 +908,7 @@ useWithStaleFast' key file = do
907908
wait <- delayedAction $ mkDelayedAction ("C:" ++ show key ++ ":" ++ fromNormalizedFilePath file) Debug $ use key file
908909

909910
s@ShakeExtras{state} <- askShake
910-
r <- liftIO $ atomically $ getValues state key file
911+
r <- liftIO $ atomicallyNamed "useStateFast" $ getValues state key file
911912
liftIO $ case r of
912913
-- block for the result if we haven't computed before
913914
Nothing -> do
@@ -1016,7 +1017,7 @@ defineEarlyCutoff' doDiagnostics cmp key file old mode action = do
10161017
(if optSkipProgress options key then id else inProgress progress file) $ do
10171018
val <- case old of
10181019
Just old | mode == RunDependenciesSame -> do
1019-
v <- liftIO $ atomically $ getValues state key file
1020+
v <- liftIO $ atomicallyNamed "defineEarlyCutoff - read 1" $ getValues state key file
10201021
case v of
10211022
-- No changes in the dependencies and we have
10221023
-- an existing successful result.
@@ -1035,10 +1036,10 @@ defineEarlyCutoff' doDiagnostics cmp key file old mode action = do
10351036
(do v <- action; liftIO $ evaluate $ force v) $
10361037
\(e :: SomeException) -> do
10371038
pure (Nothing, ([ideErrorText file $ T.pack $ show e | not $ isBadDependency e],Nothing))
1038-
modTime <- liftIO $ (currentValue . fst =<<) <$> atomically (getValues state GetModificationTime file)
1039+
modTime <- liftIO $ (currentValue . fst =<<) <$> atomicallyNamed "defineEarlyCutoff - read 2" (getValues state GetModificationTime file)
10391040
(bs, res) <- case res of
10401041
Nothing -> do
1041-
staleV <- liftIO $ atomically $ getValues state key file
1042+
staleV <- liftIO $ atomicallyNamed "defineEarlyCutoff -read 3" $ getValues state key file
10421043
pure $ case staleV of
10431044
Nothing -> (toShakeValue ShakeResult bs, Failed False)
10441045
Just v -> case v of
@@ -1049,7 +1050,7 @@ defineEarlyCutoff' doDiagnostics cmp key file old mode action = do
10491050
(Failed b, _) ->
10501051
(toShakeValue ShakeResult bs, Failed b)
10511052
Just v -> pure (maybe ShakeNoCutoff ShakeResult bs, Succeeded (vfsVersion =<< modTime) v)
1052-
liftIO $ atomically $ setValues state key file res (Vector.fromList diags)
1053+
liftIO $ atomicallyNamed "defineEarlyCutoff - write" $ setValues state key file res (Vector.fromList diags)
10531054
doDiagnostics diags
10541055
let eq = case (bs, fmap decodeShakeValue old) of
10551056
(ShakeResult a, Just (ShakeResult b)) -> cmp a b
@@ -1061,7 +1062,7 @@ defineEarlyCutoff' doDiagnostics cmp key file old mode action = do
10611062
(if eq then ChangedRecomputeSame else ChangedRecomputeDiff)
10621063
(encodeShakeValue bs) $
10631064
A res
1064-
liftIO $ atomically $ modifyTVar' dirtyKeys (HSet.delete $ toKey key file)
1065+
liftIO $ atomicallyNamed "defineEarlyCutoff - dirtyKeys" $ modifyTVar' dirtyKeys (HSet.delete $ toKey key file)
10651066
return res
10661067

10671068
traceA :: A v -> String
@@ -1149,7 +1150,7 @@ updateFileDiagnostics :: MonadIO m
11491150
-> [(ShowDiagnostic,Diagnostic)] -- ^ current results
11501151
-> m ()
11511152
updateFileDiagnostics fp k ShakeExtras{logger, diagnostics, hiddenDiagnostics, publishedDiagnostics, state, debouncer, lspEnv} current = liftIO $ do
1152-
modTime <- (currentValue . fst =<<) <$> atomically (getValues state GetModificationTime fp)
1153+
modTime <- (currentValue . fst =<<) <$> atomicallyNamed "diagnostics - read" (getValues state GetModificationTime fp)
11531154
let (currentShown, currentHidden) = partition ((== ShowDiag) . fst) current
11541155
uri = filePathToUri' fp
11551156
ver = vfsVersion =<< modTime
@@ -1159,13 +1160,13 @@ updateFileDiagnostics fp k ShakeExtras{logger, diagnostics, hiddenDiagnostics, p
11591160
-- published. Otherwise, we might never publish certain diagnostics if
11601161
-- an exception strikes between modifyVar but before
11611162
-- publishDiagnosticsNotification.
1162-
newDiags <- liftIO $ atomically $ update (map snd currentShown) diagnostics
1163-
_ <- 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
11641165
let uri = filePathToUri' fp
11651166
let delay = if null newDiags then 0.1 else 0
11661167
registerEvent debouncer delay uri $ do
11671168
join $ mask_ $ do
1168-
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
11691170
let action = when (lastPublish /= newDiags) $ case lspEnv of
11701171
Nothing -> -- Print an LSP event.
11711172
logInfo logger $ showDiagnosticsColored $ map (fp,ShowDiag,) newDiags
@@ -1224,7 +1225,7 @@ getAllDiagnostics =
12241225

12251226
updatePositionMapping :: IdeState -> VersionedTextDocumentIdentifier -> List TextDocumentContentChangeEvent -> IO ()
12261227
updatePositionMapping IdeState{shakeExtras = ShakeExtras{positionMapping}} VersionedTextDocumentIdentifier{..} (List changes) =
1227-
atomically $ STM.focus (Focus.alter f) uri positionMapping
1228+
atomicallyNamed "updatePositionMapping" $ STM.focus (Focus.alter f) uri positionMapping
12281229
where
12291230
uri = toNormalizedUri _uri
12301231
f = Just . f' . fromMaybe mempty

0 commit comments

Comments
 (0)