Skip to content

Commit db456b0

Browse files
pepeiborracocreature
authored andcommitted
Add a new flag --shake-profiling DIR (#307)
The flag provides a way to enable Shake profiling reports without recompiling. Debug output prints links to the Shake reports for convenience
1 parent fd163cd commit db456b0

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

exe/Arguments.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ data Arguments = Arguments
1111
,argsCwd :: Maybe FilePath
1212
,argFiles :: [FilePath]
1313
,argsVersion :: Bool
14+
,argsShakeProfiling :: Maybe FilePath
1415
}
1516

1617
getArguments :: IO Arguments
@@ -27,3 +28,4 @@ arguments = Arguments
2728
<*> optional (strOption $ long "cwd" <> metavar "DIR" <> help "Change to this directory")
2829
<*> many (argument str (metavar "FILES/DIRS..."))
2930
<*> switch (long "version" <> help "Show ghcide and GHC versions")
31+
<*> optional (strOption $ long "shake-profiling" <> metavar "DIR" <> help "Dump profiling reports to this directory")

exe/Main.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ main = do
9393
-- very important we only call loadSession once, and it's fast, so just do it before starting
9494
session <- loadSession dir
9595
let options = (defaultIdeOptions $ return session)
96-
{ optReportProgress = clientSupportsProgress caps }
96+
{ optReportProgress = clientSupportsProgress caps
97+
, optShakeProfiling = argsShakeProfiling
98+
}
9799
initialise (mainRule >> action kick) getLspId event (logger minBound) options vfs
98100
else do
99101
putStrLn $ "Ghcide setup tester in " ++ dir ++ "."

src/Development/IDE/Core/Shake.hs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ import qualified Data.ByteString.Internal as BS
5353
import Data.Dynamic
5454
import Data.Maybe
5555
import Data.Map.Strict (Map)
56-
import Data.List.Extra
56+
import Data.List.Extra (foldl', partition, takeEnd)
5757
import qualified Data.Set as Set
5858
import qualified Data.Text as T
59+
import Data.Traversable (for)
5960
import Data.Tuple.Extra
6061
import Data.Unique
6162
import Development.IDE.Core.Debouncer
@@ -227,14 +228,15 @@ data IdeState = IdeState
227228

228229

229230
-- This is debugging code that generates a series of profiles, if the Boolean is true
230-
shakeRunDatabaseProfile :: Maybe FilePath -> ShakeDatabase -> [Action a] -> IO [a]
231+
shakeRunDatabaseProfile :: Maybe FilePath -> ShakeDatabase -> [Action a] -> IO ([a], Maybe FilePath)
231232
shakeRunDatabaseProfile mbProfileDir shakeDb acts = do
232233
(time, (res,_)) <- duration $ shakeRunDatabase shakeDb acts
233-
whenJust mbProfileDir $ \dir -> do
234-
count <- modifyVar profileCounter $ \x -> let !y = x+1 in return (y,y)
235-
let file = "ide-" ++ profileStartTime ++ "-" ++ takeEnd 5 ("0000" ++ show count) ++ "-" ++ showDP 2 time <.> "html"
236-
shakeProfileDatabase shakeDb $ dir </> file
237-
return res
234+
proFile <- for mbProfileDir $ \dir -> do
235+
count <- modifyVar profileCounter $ \x -> let !y = x+1 in return (y,y)
236+
let file = "ide-" ++ profileStartTime ++ "-" ++ takeEnd 5 ("0000" ++ show count) ++ "-" ++ showDP 2 time <.> "html"
237+
shakeProfileDatabase shakeDb $ dir </> file
238+
return (dir </> file)
239+
return (res, proFile)
238240
where
239241

240242
{-# NOINLINE profileStartTime #-}
@@ -392,9 +394,15 @@ shakeRun IdeState{shakeExtras=ShakeExtras{..}, ..} acts =
392394
let res' = case res of
393395
Left e -> "exception: " <> displayException e
394396
Right _ -> "completed"
397+
profile = case res of
398+
Right (_, Just fp) ->
399+
let link = case filePathToUri' $ toNormalizedFilePath fp of
400+
NormalizedUri x -> x
401+
in ", profile saved at " <> T.unpack link
402+
_ -> ""
395403
logDebug logger $ T.pack $
396-
"Finishing shakeRun (took " ++ showDuration runTime ++ ", " ++ res' ++ ")"
397-
signalBarrier bar res
404+
"Finishing shakeRun (took " ++ showDuration runTime ++ ", " ++ res' ++ profile ++ ")"
405+
signalBarrier bar (fst <$> res)
398406
-- important: we send an async exception to the thread, then wait for it to die, before continuing
399407
pure (killThread thread >> void (waitBarrier bar), either throwIO return =<< waitBarrier bar))
400408

0 commit comments

Comments
 (0)