-
-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log live_bytes and heap_size as reported by GHC.Stats (#1508)
* Log live_bytes and heap_size as reported by GHC.Stats A thread is spawned which at a prespecified interval will report the live bytes and heap size at the last major collection. Live bytes corresponds to how much live data a program has and should match closely the value reported during heap profiling. Heap size reports the total amount of memory the RTS is using, which corresponds more closely to OS memory usage. ``` [INFO] Live bytes: 367.45MB Heap size: 676.33MB ``` Closes #1493 * Update ghcide/ghcide.cabal Co-authored-by: Jan Hrcek <2716069+jhrcek@users.noreply.github.com> Co-authored-by: Pepe Iborra <pepeiborra@gmail.com> Co-authored-by: Jan Hrcek <2716069+jhrcek@users.noreply.github.com> Co-authored-by: Javier Neira <atreyu.bbb@gmail.com> Co-authored-by: wz1000 <zubin.duggal@gmail.com> Co-authored-by: Anton Latukha <anton.latukha@gmail.com>
- Loading branch information
1 parent
acdb82e
commit e1949dd
Showing
4 changed files
with
67 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{-# LANGUAGE NumericUnderscores #-} | ||
-- | Logging utilities for reporting heap statistics | ||
module Development.IDE.Main.HeapStats ( withHeapStats ) where | ||
|
||
import GHC.Stats | ||
import Development.IDE.Types.Logger (Logger, logInfo) | ||
import Control.Concurrent.Async | ||
import qualified Data.Text as T | ||
import Data.Word | ||
import Control.Monad | ||
import Control.Concurrent | ||
import Text.Printf (printf) | ||
|
||
-- | Interval at which to report the latest heap statistics. | ||
heapStatsInterval :: Int | ||
heapStatsInterval = 60_000_000 -- 60s | ||
|
||
-- | Report the live bytes and heap size at the last major collection. | ||
logHeapStats :: Logger -> IO () | ||
logHeapStats l = do | ||
stats <- getRTSStats | ||
-- live_bytes is the total amount of live memory in a program | ||
-- (corresponding to the amount on a heap profile) | ||
let live_bytes = gcdetails_live_bytes (gc stats) | ||
-- heap_size is the total amount of memory the RTS is using | ||
-- this corresponds closer to OS memory usage | ||
heap_size = gcdetails_mem_in_use_bytes (gc stats) | ||
format :: Word64 -> T.Text | ||
format m = T.pack (printf "%.2fMB" (fromIntegral @Word64 @Double m / 1e6)) | ||
message = "Live bytes: " <> format live_bytes <> " " <> | ||
"Heap size: " <> format heap_size | ||
logInfo l message | ||
|
||
-- | An action which logs heap statistics at the 'heapStatsInterval' | ||
heapStatsThread :: Logger -> IO r | ||
heapStatsThread l = forever $ do | ||
threadDelay heapStatsInterval | ||
logHeapStats l | ||
|
||
-- | A helper function which lauches the 'heapStatsThread' and kills it | ||
-- appropiately when the inner action finishes. It also checks to see | ||
-- if `-T` is enabled. | ||
withHeapStats :: Logger -> IO r -> IO r | ||
withHeapStats l k = do | ||
enabled <- getRTSStatsEnabled | ||
if enabled | ||
then do | ||
logInfo l ("Logging heap statistics every " | ||
<> T.pack (printf "%.2fs" (fromIntegral @Int @Double heapStatsInterval / 1e6))) | ||
withAsync (heapStatsThread l) (const k) | ||
else do | ||
logInfo l "Heap statistics are not enabled (RTS option -T is needed)" | ||
k |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters