Skip to content

Commit d267539

Browse files
committed
STM stats in ghcide
1 parent 3045558 commit d267539

File tree

6 files changed

+33
-31
lines changed

6 files changed

+33
-31
lines changed

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

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

78-
import Control.Concurrent.STM (atomically)
7978
import Control.Concurrent.STM.TQueue
79+
import Control.Concurrent.STM.Timed (atomically)
8080
import Data.Foldable (for_)
8181
import qualified Data.HashSet as Set
8282
import Data.IORef.Extra (atomicModifyIORef'_)

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.Timed 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.Timed (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,8 +24,8 @@ module Development.IDE.Core.FileStore(
2424
registerFileWatches
2525
) where
2626

27-
import Control.Concurrent.STM (atomically)
2827
import Control.Concurrent.STM.TQueue (writeTQueue)
28+
import Control.Concurrent.STM.Timed (atomically)
2929
import Control.Concurrent.Strict
3030
import Control.Exception
3131
import Control.Monad.Extra

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.Timed (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: 23 additions & 22 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.Timed (atomicallyNamed)
151152
import Control.Exception.Extra hiding (bracket_)
152153
import Data.Aeson (toJSON)
153154
import qualified Data.ByteString.Char8 as BS8
@@ -336,11 +337,11 @@ lastValueIO s@ShakeExtras{positionMapping,persistentKeys,state} k file = do
336337
MaybeT $ pure $ (,del,ver) <$> fromDynamic dv
337338
case mv of
338339
Nothing -> do
339-
void $ atomically $ STM.focus (Focus.alter (alterValue $ Failed True)) (toKey k file) state
340+
void $ atomicallyNamed "lastValueIO 1" $ STM.focus (Focus.alter (alterValue $ Failed True)) (toKey k file) state
340341
return Nothing
341342
Just (v,del,ver) -> do
342-
void $ atomically $ STM.focus (Focus.alter (alterValue $ Stale (Just del) ver (toDyn v))) (toKey k file) state
343-
atomically $ Just . (v,) . addDelta del <$> mappingForVersion positionMapping file ver
343+
void $ atomicallyNamed "lastValueIO 2" $ STM.focus (Focus.alter (alterValue $ Stale (Just del) ver (toDyn v))) (toKey k file) state
344+
atomicallyNamed "lastValueIO 3" $ Just . (v,) . addDelta del <$> mappingForVersion positionMapping file ver
344345

345346
-- We got a new stale value from the persistent rule, insert it in the map without affecting diagnostics
346347
alterValue new Nothing = Just (ValueWithDiagnostics new mempty) -- If it wasn't in the map, give it empty diagnostics
@@ -350,13 +351,13 @@ lastValueIO s@ShakeExtras{positionMapping,persistentKeys,state} k file = do
350351
-- Something already succeeded before, leave it alone
351352
_ -> old
352353

353-
atomically (STM.lookup (toKey k file) state) >>= \case
354+
atomicallyNamed "lastValueIO 4" (STM.lookup (toKey k file) state) >>= \case
354355
Nothing -> readPersistent
355356
Just (ValueWithDiagnostics v _) -> case v of
356357
Succeeded ver (fromDynamic -> Just v) ->
357-
atomically $ Just . (v,) <$> mappingForVersion positionMapping file ver
358+
atomicallyNamed "lastValueIO 5" $ Just . (v,) <$> mappingForVersion positionMapping file ver
358359
Stale del ver (fromDynamic -> Just v) ->
359-
atomically $ Just . (v,) . maybe id addDelta del <$> mappingForVersion positionMapping file ver
360+
atomicallyNamed "lastValueIO 6" $ Just . (v,) . maybe id addDelta del <$> mappingForVersion positionMapping file ver
360361
Failed p | not p -> readPersistent
361362
_ -> pure Nothing
362363

@@ -423,7 +424,7 @@ setValues :: IdeRule k v
423424
-> Vector FileDiagnostic
424425
-> IO ()
425426
setValues state key file val diags =
426-
atomically $ STM.insert (ValueWithDiagnostics (fmap toDyn val) diags) (toKey key file) state
427+
atomicallyNamed "setValues" $ STM.insert (ValueWithDiagnostics (fmap toDyn val) diags) (toKey key file) state
427428

428429

429430
-- | Delete the value stored for a given ide build key
@@ -434,7 +435,7 @@ deleteValue
434435
-> NormalizedFilePath
435436
-> IO ()
436437
deleteValue ShakeExtras{dirtyKeys, state} key file = do
437-
atomically $ STM.delete (toKey key file) state
438+
atomicallyNamed "deleteValue" $ STM.delete (toKey key file) state
438439
atomicModifyIORef_ dirtyKeys $ HSet.insert (toKey key file)
439440

440441
recordDirtyKeys
@@ -457,7 +458,7 @@ getValues ::
457458
NormalizedFilePath ->
458459
IO (Maybe (Value v, Vector FileDiagnostic))
459460
getValues state key file = do
460-
atomically (STM.lookup (toKey key file) state) >>= \case
461+
atomicallyNamed "getValues" (STM.lookup (toKey key file) state) >>= \case
461462
Nothing -> pure Nothing
462463
Just (ValueWithDiagnostics v diagsV) -> do
463464
let r = fmap (fromJust . fromDynamic @v) v
@@ -620,7 +621,7 @@ shakeRestart IdeState{..} reason acts =
620621
(stopTime,()) <- duration (cancelShakeSession runner)
621622
res <- shakeDatabaseProfile shakeDb
622623
backlog <- readIORef $ dirtyKeys shakeExtras
623-
queue <- atomically $ peekInProgress $ actionQueue shakeExtras
624+
queue <- atomicallyNamed "actionQueue - peek" $ peekInProgress $ actionQueue shakeExtras
624625
let profile = case res of
625626
Just fp -> ", profile saved at " <> fp
626627
_ -> ""
@@ -653,7 +654,7 @@ notifyTestingLogMessage extras msg = do
653654
shakeEnqueue :: ShakeExtras -> DelayedAction a -> IO (IO a)
654655
shakeEnqueue ShakeExtras{actionQueue, logger} act = do
655656
(b, dai) <- instantiateDelayedAction act
656-
atomically $ pushQueue dai actionQueue
657+
atomicallyNamed "actionQueue - push" $ pushQueue dai actionQueue
657658
let wait' b =
658659
waitBarrier b `catches`
659660
[ Handler(\BlockedIndefinitelyOnMVar ->
@@ -662,7 +663,7 @@ shakeEnqueue ShakeExtras{actionQueue, logger} act = do
662663
, Handler (\e@AsyncCancelled -> do
663664
logPriority logger Debug $ T.pack $ actionName act <> " was cancelled"
664665

665-
atomically $ abortQueue dai actionQueue
666+
atomicallyNamed "actionQueue - abort" $ abortQueue dai actionQueue
666667
throw e)
667668
]
668669
return (wait' b >>= either throwIO return)
@@ -677,7 +678,7 @@ newSession
677678
-> IO ShakeSession
678679
newSession extras@ShakeExtras{..} shakeDb acts reason = do
679680
IdeOptions{optRunSubset} <- getIdeOptionsIO extras
680-
reenqueued <- atomically $ peekInProgress actionQueue
681+
reenqueued <- atomicallyNamed "actionQueue - peek" $ peekInProgress actionQueue
681682
allPendingKeys <-
682683
if optRunSubset
683684
then Just <$> readIORef dirtyKeys
@@ -686,14 +687,14 @@ newSession extras@ShakeExtras{..} shakeDb acts reason = do
686687
-- A daemon-like action used to inject additional work
687688
-- Runs actions from the work queue sequentially
688689
pumpActionThread otSpan = do
689-
d <- liftIO $ atomically $ popQueue actionQueue
690+
d <- liftIO $ atomicallyNamed "action queue - pop" $ popQueue actionQueue
690691
actionFork (run otSpan d) $ \_ -> pumpActionThread otSpan
691692

692693
-- TODO figure out how to thread the otSpan into defineEarlyCutoff
693694
run _otSpan d = do
694695
start <- liftIO offsetTime
695696
getAction d
696-
liftIO $ atomically $ doneQueue d actionQueue
697+
liftIO $ atomicallyNamed "actionQueue - done" $ doneQueue d actionQueue
697698
runTime <- liftIO start
698699
let msg = T.pack $ "finish: " ++ actionName d
699700
++ " (took " ++ showDuration runTime ++ ")"
@@ -752,11 +753,11 @@ instantiateDelayedAction (DelayedAction _ s p a) = do
752753

753754
getDiagnostics :: IdeState -> IO [FileDiagnostic]
754755
getDiagnostics IdeState{shakeExtras = ShakeExtras{diagnostics}} = do
755-
atomically $ getAllDiagnostics diagnostics
756+
atomicallyNamed "getAllDiagnostics" $ getAllDiagnostics diagnostics
756757

757758
getHiddenDiagnostics :: IdeState -> IO [FileDiagnostic]
758759
getHiddenDiagnostics IdeState{shakeExtras = ShakeExtras{hiddenDiagnostics}} = do
759-
atomically $ getAllDiagnostics hiddenDiagnostics
760+
atomicallyNamed "getAllDiagnostics - hidden" $ getAllDiagnostics hiddenDiagnostics
760761

761762
-- | Find and release old keys from the state Hashmap
762763
-- For the record, there are other state sources that this process does not release:
@@ -780,7 +781,7 @@ garbageCollectKeys label maxAge checkParents agedKeys = do
780781
start <- liftIO offsetTime
781782
extras <- getShakeExtras
782783
let values = state extras
783-
(n::Int, garbage) <- liftIO $ atomically $
784+
(n::Int, garbage) <- liftIO $ atomicallyNamed "garbage collect" $
784785
foldM (removeDirtyKey values) (0,[]) agedKeys
785786
liftIO $ atomicModifyIORef_ (dirtyKeys extras) $ \x ->
786787
foldl' (flip HSet.insert) x garbage
@@ -1152,13 +1153,13 @@ updateFileDiagnostics fp k ShakeExtras{logger, diagnostics, hiddenDiagnostics, p
11521153
-- published. Otherwise, we might never publish certain diagnostics if
11531154
-- an exception strikes between modifyVar but before
11541155
-- publishDiagnosticsNotification.
1155-
newDiags <- liftIO $ atomically $ update (map snd currentShown) diagnostics
1156-
_ <- liftIO $ atomically $ update (map snd currentHidden) hiddenDiagnostics
1156+
newDiags <- liftIO $ atomicallyNamed "diagnostics - update" $ update (map snd currentShown) diagnostics
1157+
_ <- liftIO $ atomicallyNamed "diagnostics - hidden" $ update (map snd currentHidden) hiddenDiagnostics
11571158
let uri = filePathToUri' fp
11581159
let delay = if null newDiags then 0.1 else 0
11591160
registerEvent debouncer delay uri $ do
11601161
join $ mask_ $ do
1161-
lastPublish <- atomically $ STM.focus (Focus.lookupWithDefault [] <* Focus.insert newDiags) uri publishedDiagnostics
1162+
lastPublish <- atomicallyNamed "diagnostics - publish" $ STM.focus (Focus.lookupWithDefault [] <* Focus.insert newDiags) uri publishedDiagnostics
11621163
let action = when (lastPublish /= newDiags) $ case lspEnv of
11631164
Nothing -> -- Print an LSP event.
11641165
logInfo logger $ showDiagnosticsColored $ map (fp,ShowDiag,) newDiags
@@ -1217,7 +1218,7 @@ getAllDiagnostics =
12171218

12181219
updatePositionMapping :: IdeState -> VersionedTextDocumentIdentifier -> List TextDocumentContentChangeEvent -> IO ()
12191220
updatePositionMapping IdeState{shakeExtras = ShakeExtras{positionMapping}} VersionedTextDocumentIdentifier{..} (List changes) =
1220-
atomically $ STM.focus (Focus.alter f) uri positionMapping
1221+
atomicallyNamed "updatePositionMapping" $ STM.focus (Focus.alter f) uri positionMapping
12211222
where
12221223
uri = toNormalizedUri _uri
12231224
f = Just . f' . fromMaybe mempty

0 commit comments

Comments
 (0)