    {-# LANGUAGE BangPatterns #-}
    {-# LANGUAGE TupleSections #-}
    {-# LANGUAGE CPP #-}

    -- | Calculate the size of GHC.Stats statically.

    module Weigh.GHCStats
      (getGhcStatsSizeInBytes
      ,getStats
      ,gcCount
      ,totalBytesAllocated
      ,liveBytes
      ,maxBytesInUse
      ,maxOSBytes
      )
      where

    import Data.Word
    import GHC.Stats
    import System.Mem

    -- | Get GHC's statistics.
    getStats :: IO RTSStats
    getStats :: IO RTSStats
    getStats = IO RTSStats
    getRTSStats

    gcCount :: RTSStats -> Word32
    gcCount :: RTSStats -> Word32
    gcCount = RTSStats -> Word32
    gcs

    totalBytesAllocated :: RTSStats -> Word64
    totalBytesAllocated :: RTSStats -> Word64
    totalBytesAllocated = RTSStats -> Word64
    allocated_bytes

    liveBytes :: RTSStats -> Word64
    liveBytes :: RTSStats -> Word64
    liveBytes = GCDetails -> Word64
    gcdetails_live_bytes (GCDetails -> Word64)
    -> (RTSStats -> GCDetails) -> RTSStats -> Word64
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . RTSStats -> GCDetails
    gc

    maxBytesInUse :: RTSStats -> Word64
    maxBytesInUse :: RTSStats -> Word64
    maxBytesInUse = RTSStats -> Word64
    max_live_bytes

    maxOSBytes :: RTSStats -> Word64
    maxOSBytes :: RTSStats -> Word64
    maxOSBytes = RTSStats -> Word64
    max_mem_in_use_bytes

    -- | Get the size of a 'RTSStats' object in bytes.
    getGhcStatsSizeInBytes :: IO Word64
    getGhcStatsSizeInBytes :: IO Word64
    getGhcStatsSizeInBytes = do
      RTSStats
    s1 <- IO RTSStats
    oneGetStats
      RTSStats
    s2 <- IO RTSStats
    twoGetStats
      Word64 -> IO Word64
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Word64 -> Word64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (RTSStats -> Word64
    totalBytesAllocated RTSStats
    s2 Word64 -> Word64 -> Word64
    forall a. Num a => a -> a -> a
    - RTSStats -> Word64
    totalBytesAllocated RTSStats
    s1))
      where
        oneGetStats :: IO RTSStats
    oneGetStats = do
          IO ()
    performGC
          !RTSStats
    s <- IO RTSStats
    getStats
          RTSStats -> IO RTSStats
    forall (m :: * -> *) a. Monad m => a -> m a
    return RTSStats
    s
        twoGetStats :: IO RTSStats
    twoGetStats = do
          IO ()
    performGC
          !RTSStats
    _ <- IO RTSStats
    getStats
          !RTSStats
    s <- IO RTSStats
    getStats
          RTSStats -> IO RTSStats
    forall (m :: * -> *) a. Monad m => a -> m a
    return RTSStats
    s