Skip to content

Commit

Permalink
Add GHC.Stats.getGCStatsEnabled function (#5846)
Browse files Browse the repository at this point in the history
Add getGCStatsEnabled function which checks whether GC stats have been
enabled (with `-T`, for example).

Make getGCStats throw an exception if called with GC stats disabled.
  • Loading branch information
pcapriotti committed Jun 19, 2012
1 parent aedfd7e commit 05cf001
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions GHC/Stats.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@
module GHC.Stats
( GCStats(..)
, getGCStats
, getGCStatsEnabled
) where

import Control.Monad
import Data.Int
import GHC.IO.Exception
import Foreign.Marshal.Alloc
import Foreign.Storable
import Foreign.Ptr
import Data.Int

#include "Rts.h"

foreign import ccall "getGCStats" getGCStats_ :: Ptr () -> IO ()
foreign import ccall "getGCStats" getGCStats_ :: Ptr () -> IO ()
foreign import ccall "getGCStatsEnabled" getGCStatsEnabled :: IO Bool

-- I'm probably violating a bucket of constraints here... oops.

Expand Down Expand Up @@ -76,7 +80,16 @@ data GCStats = GCStats
-- garbage collection. If you would like your statistics as recent as
-- possible, first run a 'System.Mem.performGC'.
getGCStats :: IO GCStats
getGCStats = allocaBytes (#size GCStats) $ \p -> do
getGCStats = do
statsEnabled <- getGCStatsEnabled
unless statsEnabled . ioError $ IOError
Nothing
UnsupportedOperation
""
"getGCStats: GC stats not enabled. Use `+RTS -T -RTS' to enable them."
Nothing
Nothing
allocaBytes (#size GCStats) $ \p -> do
getGCStats_ p
bytesAllocated <- (# peek GCStats, bytes_allocated) p
numGcs <- (# peek GCStats, num_gcs ) p
Expand Down

0 comments on commit 05cf001

Please sign in to comment.