Skip to content

Commit f3cdf09

Browse files
authored
Trace rebuilds (#2283)
* trace interface regeneration events * trace recordDirtyKeys calls * fix 8.6 build * remove userTracingEnabled check as pointed out by @michaelpj the check is already installed in withTracing the logger should not make any assumptions regarding user tracing being static anyway
1 parent 8a18255 commit f3cdf09

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ import Data.Functor
102102
import qualified Data.HashMap.Strict as HashMap
103103
import Data.Tuple.Extra (dupe)
104104
import Data.Unique as Unique
105+
import Development.IDE.Core.Tracing (withTrace)
105106
import qualified Language.LSP.Server as LSP
106107
import qualified Language.LSP.Types as LSP
107108

@@ -899,7 +900,8 @@ loadHieFile ncu f = do
899900
-- Assumes file exists.
900901
-- Requires the 'HscEnv' to be set up with dependencies
901902
loadInterface
902-
:: MonadIO m => HscEnv
903+
:: (MonadIO m, MonadMask m)
904+
=> HscEnv
903905
-> ModSummary
904906
-> SourceModified
905907
-> Maybe LinkableType
@@ -939,7 +941,15 @@ loadInterface session ms sourceMod linkableNeeded regen = do
939941
hmi <- liftIO $ mkDetailsFromIface sessionWithMsDynFlags iface linkable
940942
return ([], Just $ mkHiFileResult ms hmi)
941943
else regen linkableNeeded
942-
(_reason, _) -> regen linkableNeeded
944+
(_reason, _) -> withTrace "regenerate interface" $ \setTag -> do
945+
setTag "Module" $ moduleNameString $ moduleName $ ms_mod ms
946+
setTag "Reason" $ showReason _reason
947+
regen linkableNeeded
948+
949+
showReason :: RecompileRequired -> String
950+
showReason UpToDate = "UpToDate"
951+
showReason MustCompile = "MustCompile"
952+
showReason (RecompBecause s) = s
943953

944954
mkDetailsFromIface :: HscEnv -> ModIface -> Maybe Linkable -> IO HomeModInfo
945955
mkDetailsFromIface session iface linkable = do

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,10 @@ recordDirtyKeys
439439
-> k
440440
-> [NormalizedFilePath]
441441
-> IO ()
442-
recordDirtyKeys ShakeExtras{dirtyKeys} key file =
442+
recordDirtyKeys ShakeExtras{dirtyKeys} key file = withEventTrace "recordDirtyKeys" $ \addEvent -> do
443443
atomicModifyIORef_ dirtyKeys $ \x -> foldl' (flip HSet.insert) x (toKey key <$> file)
444+
addEvent (fromString $ "dirty " <> show key) (fromString $ unlines $ map fromNormalizedFilePath file)
445+
444446

445447
-- | We return Nothing if the rule has not run and Just Failed if it has failed to produce a value.
446448
getValues ::

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ module Development.IDE.Core.Tracing
88
, getInstrumentCached
99
, otTracedProvider
1010
, otSetUri
11-
)
11+
, withTrace
12+
,withEventTrace)
1213
where
1314

1415
import Control.Concurrent.Async (Async, async)
@@ -19,13 +20,11 @@ import Control.Exception.Safe (SomeException, catch,
1920
generalBracket)
2021
import Control.Monad (forM_, forever, void, when,
2122
(>=>))
22-
import Control.Monad.Catch (ExitCase (..))
23+
import Control.Monad.Catch (ExitCase (..), MonadMask)
2324
import Control.Monad.Extra (whenJust)
2425
import Control.Monad.IO.Unlift
2526
import Control.Seq (r0, seqList, seqTuple2, using)
26-
#if MIN_VERSION_ghc(8,8,0)
2727
import Data.ByteString (ByteString)
28-
#endif
2928
import Data.ByteString.Char8 (pack)
3029
import Data.Dynamic (Dynamic)
3130
import qualified Data.HashMap.Strict as HMap
@@ -57,6 +56,26 @@ import OpenTelemetry.Eventlog (Instrument, SpanInFlight (..),
5756
mkValueObserver, observe,
5857
setTag, withSpan, withSpan_)
5958

59+
withTrace :: (MonadMask m, MonadIO m) =>
60+
String -> ((String -> String -> m ()) -> m a) -> m a
61+
withTrace name act
62+
| userTracingEnabled
63+
= withSpan (fromString name) $ \sp -> do
64+
let setSpan' k v = setTag sp (fromString k) (fromString v)
65+
act setSpan'
66+
| otherwise = act (\_ _ -> pure ())
67+
68+
#if MIN_VERSION_ghc(8,8,0)
69+
withEventTrace :: (MonadMask m, MonadIO m) => String -> ((ByteString -> ByteString -> m ()) -> m a) -> m a
70+
#else
71+
withEventTrace :: (MonadMask m, MonadIO m) => String -> ((String -> ByteString -> m ()) -> m a) -> m a
72+
#endif
73+
withEventTrace name act
74+
| userTracingEnabled
75+
= withSpan (fromString name) $ \sp -> do
76+
act (addEvent sp)
77+
| otherwise = act (\_ _ -> pure ())
78+
6079
-- | Trace a handler using OpenTelemetry. Adds various useful info into tags in the OpenTelemetry span.
6180
otTracedHandler
6281
:: MonadUnliftIO m

ghcide/src/Development/IDE/Main.hs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import qualified Data.Text.IO as T
3030
import Data.Text.Lazy.Encoding (decodeUtf8)
3131
import qualified Data.Text.Lazy.IO as LT
3232
import Data.Word (Word16)
33-
import Debug.Trace.Flags (userTracingEnabled)
3433
import Development.IDE (Action, GhcVersion (..),
3534
Priority (Debug), Rules,
3635
ghcVersion,
@@ -55,7 +54,8 @@ import Development.IDE.Core.Service (initialise, runAction)
5554
import Development.IDE.Core.Shake (IdeState (shakeExtras),
5655
ShakeExtras (state),
5756
shakeSessionInit, uses)
58-
import Development.IDE.Core.Tracing (measureMemory)
57+
import Development.IDE.Core.Tracing (measureMemory,
58+
withEventTrace)
5959
import Development.IDE.Graph (action)
6060
import Development.IDE.LSP.LanguageServer (runLanguageServer)
6161
import Development.IDE.Plugin (Plugin (pluginHandlers, pluginModifyDynflags, pluginRules))
@@ -101,7 +101,6 @@ import Ide.Types (IdeCommand (IdeCommand),
101101
ipMap)
102102
import qualified Language.LSP.Server as LSP
103103
import Numeric.Natural (Natural)
104-
import OpenTelemetry.Eventlog (addEvent, withSpan)
105104
import Options.Applicative hiding (action)
106105
import qualified System.Directory.Extra as IO
107106
import System.Exit (ExitCode (ExitFailure),
@@ -189,7 +188,7 @@ defaultArguments :: Priority -> Arguments
189188
defaultArguments priority = Arguments
190189
{ argsOTMemoryProfiling = False
191190
, argCommand = LSP
192-
, argsLogger = stderrLogger priority <> telemetryLogger
191+
, argsLogger = stderrLogger priority <> pure telemetryLogger
193192
, argsRules = mainRule >> action kick
194193
, argsGhcidePlugin = mempty
195194
, argsHlsPlugins = pluginDescToIdePlugins Ghcide.descriptors
@@ -236,12 +235,10 @@ stderrLogger logLevel = do
236235
return $ Logger $ \p m -> when (p >= logLevel) $ withLock lock $
237236
T.hPutStrLn stderr $ "[" <> T.pack (show p) <> "] " <> m
238237

239-
telemetryLogger :: IO Logger
240-
telemetryLogger
241-
| userTracingEnabled = return $ Logger $ \p m ->
242-
withSpan "log" $ \sp ->
243-
addEvent sp (fromString $ "Log " <> show p) (encodeUtf8 $ trim m)
244-
| otherwise = mempty
238+
telemetryLogger :: Logger
239+
telemetryLogger = Logger $ \p m ->
240+
withEventTrace "Log" $ \addEvent ->
241+
addEvent (fromString $ "Log " <> show p) (encodeUtf8 $ trim m)
245242
where
246243
-- eventlog message size is limited by EVENT_PAYLOAD_SIZE_MAX = STG_WORD16_MAX
247244
trim = T.take (fromIntegral(maxBound :: Word16) - 10)

0 commit comments

Comments
 (0)