{-# LANGUAGE RecordWildCards #-}
module GHC.Prof.Types where
import Data.Monoid
import Prelude

import Data.IntMap (IntMap)
import Data.Map (Map)
import Data.Scientific (Scientific)
import Data.Set (Set)
import Data.Text (Text)
import Data.Time (DiffTime, LocalTime)

-- | Top-level profiling report
data Profile = Profile
  { Profile -> LocalTime
profileTimestamp :: !LocalTime
  , Profile -> Text
profileCommandLine :: !Text
  , Profile -> TotalTime
profileTotalTime :: !TotalTime
  , Profile -> TotalAlloc
profileTotalAlloc :: !TotalAlloc
  , Profile -> [AggregatedCostCentre]
profileTopCostCentres :: [AggregatedCostCentre]
  , Profile -> CostCentreTree
profileCostCentreTree :: !CostCentreTree
  } deriving CostCentreNo -> Profile -> ShowS
[Profile] -> ShowS
Profile -> String
forall a.
(CostCentreNo -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Profile] -> ShowS
$cshowList :: [Profile] -> ShowS
show :: Profile -> String
$cshow :: Profile -> String
showsPrec :: CostCentreNo -> Profile -> ShowS
$cshowsPrec :: CostCentreNo -> Profile -> ShowS
Show

-- | @total time@ in the profiling reports
data TotalTime = TotalTime
  { TotalTime -> DiffTime
totalTimeElapsed :: !DiffTime
  -- ^ Total elapsed time in seconds
  , TotalTime -> Integer
totalTimeTicks :: !Integer
  -- ^ Total number of ticks
  , TotalTime -> DiffTime
totalTimeResolution :: !DiffTime
  -- ^ Duration of a tick
  , TotalTime -> Maybe CostCentreNo
totalTimeProcessors :: !(Maybe Int)
  -- ^ Number of processors
  } deriving CostCentreNo -> TotalTime -> ShowS
[TotalTime] -> ShowS
TotalTime -> String
forall a.
(CostCentreNo -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TotalTime] -> ShowS
$cshowList :: [TotalTime] -> ShowS
show :: TotalTime -> String
$cshow :: TotalTime -> String
showsPrec :: CostCentreNo -> TotalTime -> ShowS
$cshowsPrec :: CostCentreNo -> TotalTime -> ShowS
Show

-- | @total alloc@ in the profiling reports
newtype TotalAlloc = TotalAlloc
  { TotalAlloc -> Integer
totalAllocBytes :: Integer
  -- ^ Total memory allocation in bytes
  } deriving CostCentreNo -> TotalAlloc -> ShowS
[TotalAlloc] -> ShowS
TotalAlloc -> String
forall a.
(CostCentreNo -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TotalAlloc] -> ShowS
$cshowList :: [TotalAlloc] -> ShowS
show :: TotalAlloc -> String
$cshow :: TotalAlloc -> String
showsPrec :: CostCentreNo -> TotalAlloc -> ShowS
$cshowsPrec :: CostCentreNo -> TotalAlloc -> ShowS
Show

data AggregatedCostCentre = AggregatedCostCentre
  { AggregatedCostCentre -> Text
aggregatedCostCentreName :: !Text
  -- ^ Name of the cost-centre
  , AggregatedCostCentre -> Text
aggregatedCostCentreModule :: !Text
  -- ^ Module name of the cost-centre
  , AggregatedCostCentre -> Maybe Text
aggregatedCostCentreSrc :: !(Maybe Text)
  -- ^ Source location of the cost-centre
  , AggregatedCostCentre -> Maybe Integer
aggregatedCostCentreEntries :: !(Maybe Integer)
  -- ^ Number of entries to the cost-centre
  , AggregatedCostCentre -> Scientific
aggregatedCostCentreTime :: !Scientific
  -- ^ Total time spent in the cost-centre
  , AggregatedCostCentre -> Scientific
aggregatedCostCentreAlloc :: !Scientific
  -- ^ Total allocation in the cost-centre
  , AggregatedCostCentre -> Maybe Integer
aggregatedCostCentreTicks :: !(Maybe Integer)
  -- ^ Total ticks in the cost-centre. This number exists only if
  -- @-P@ or @-Pa@ option is given at run-time.
  , AggregatedCostCentre -> Maybe Integer
aggregatedCostCentreBytes :: !(Maybe Integer)
  -- ^ Total memory allocation in the cost-centre. This number
  -- exists only if @-P@ or @-Pa@ option is given at run-time.
  } deriving (CostCentreNo -> AggregatedCostCentre -> ShowS
[AggregatedCostCentre] -> ShowS
AggregatedCostCentre -> String
forall a.
(CostCentreNo -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AggregatedCostCentre] -> ShowS
$cshowList :: [AggregatedCostCentre] -> ShowS
show :: AggregatedCostCentre -> String
$cshow :: AggregatedCostCentre -> String
showsPrec :: CostCentreNo -> AggregatedCostCentre -> ShowS
$cshowsPrec :: CostCentreNo -> AggregatedCostCentre -> ShowS
Show, AggregatedCostCentre -> AggregatedCostCentre -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AggregatedCostCentre -> AggregatedCostCentre -> Bool
$c/= :: AggregatedCostCentre -> AggregatedCostCentre -> Bool
== :: AggregatedCostCentre -> AggregatedCostCentre -> Bool
$c== :: AggregatedCostCentre -> AggregatedCostCentre -> Bool
Eq, Eq AggregatedCostCentre
AggregatedCostCentre -> AggregatedCostCentre -> Bool
AggregatedCostCentre -> AggregatedCostCentre -> Ordering
AggregatedCostCentre
-> AggregatedCostCentre -> AggregatedCostCentre
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: AggregatedCostCentre
-> AggregatedCostCentre -> AggregatedCostCentre
$cmin :: AggregatedCostCentre
-> AggregatedCostCentre -> AggregatedCostCentre
max :: AggregatedCostCentre
-> AggregatedCostCentre -> AggregatedCostCentre
$cmax :: AggregatedCostCentre
-> AggregatedCostCentre -> AggregatedCostCentre
>= :: AggregatedCostCentre -> AggregatedCostCentre -> Bool
$c>= :: AggregatedCostCentre -> AggregatedCostCentre -> Bool
> :: AggregatedCostCentre -> AggregatedCostCentre -> Bool
$c> :: AggregatedCostCentre -> AggregatedCostCentre -> Bool
<= :: AggregatedCostCentre -> AggregatedCostCentre -> Bool
$c<= :: AggregatedCostCentre -> AggregatedCostCentre -> Bool
< :: AggregatedCostCentre -> AggregatedCostCentre -> Bool
$c< :: AggregatedCostCentre -> AggregatedCostCentre -> Bool
compare :: AggregatedCostCentre -> AggregatedCostCentre -> Ordering
$ccompare :: AggregatedCostCentre -> AggregatedCostCentre -> Ordering
Ord)

-- | Cost-centre node
data CostCentre = CostCentre
  { CostCentre -> CostCentreNo
costCentreNo :: !CostCentreNo
  -- ^ Identifier of the cost-centre
  , CostCentre -> Text
costCentreName :: !Text
  -- ^ Name of the cost-centre
  , CostCentre -> Text
costCentreModule :: !Text
  -- ^ Module name of the cost-centre
  , CostCentre -> Maybe Text
costCentreSrc :: !(Maybe Text)
  -- ^ Source location of the cost-centre
  , CostCentre -> Integer
costCentreEntries :: !Integer
  -- ^ Number of entries to the cost-centre
  , CostCentre -> Scientific
costCentreIndTime :: !Scientific
  -- ^ Time spent in the cost-centre itself
  , CostCentre -> Scientific
costCentreIndAlloc :: !Scientific
  -- ^ Allocation incurred by the cost-centre itself
  , CostCentre -> Scientific
costCentreInhTime :: !Scientific
  -- ^ Time spent in the cost-centre's children
  , CostCentre -> Scientific
costCentreInhAlloc :: !Scientific
  -- ^ Allocation incurred by the cost-centre's children
  , CostCentre -> Maybe Integer
costCentreTicks :: !(Maybe Integer)
  -- ^ Number of ticks in the cost-centre.
  , CostCentre -> Maybe Integer
costCentreBytes :: !(Maybe Integer)
  -- ^ Number of allocated bytes in the cost-centre.
  } deriving (CostCentreNo -> CostCentre -> ShowS
[CostCentre] -> ShowS
CostCentre -> String
forall a.
(CostCentreNo -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CostCentre] -> ShowS
$cshowList :: [CostCentre] -> ShowS
show :: CostCentre -> String
$cshow :: CostCentre -> String
showsPrec :: CostCentreNo -> CostCentre -> ShowS
$cshowsPrec :: CostCentreNo -> CostCentre -> ShowS
Show, CostCentre -> CostCentre -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CostCentre -> CostCentre -> Bool
$c/= :: CostCentre -> CostCentre -> Bool
== :: CostCentre -> CostCentre -> Bool
$c== :: CostCentre -> CostCentre -> Bool
Eq, Eq CostCentre
CostCentre -> CostCentre -> Bool
CostCentre -> CostCentre -> Ordering
CostCentre -> CostCentre -> CostCentre
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CostCentre -> CostCentre -> CostCentre
$cmin :: CostCentre -> CostCentre -> CostCentre
max :: CostCentre -> CostCentre -> CostCentre
$cmax :: CostCentre -> CostCentre -> CostCentre
>= :: CostCentre -> CostCentre -> Bool
$c>= :: CostCentre -> CostCentre -> Bool
> :: CostCentre -> CostCentre -> Bool
$c> :: CostCentre -> CostCentre -> Bool
<= :: CostCentre -> CostCentre -> Bool
$c<= :: CostCentre -> CostCentre -> Bool
< :: CostCentre -> CostCentre -> Bool
$c< :: CostCentre -> CostCentre -> Bool
compare :: CostCentre -> CostCentre -> Ordering
$ccompare :: CostCentre -> CostCentre -> Ordering
Ord)

type CostCentreNo = Int

data CostCentreTree = CostCentreTree
  { CostCentreTree -> IntMap CostCentre
costCentreNodes :: !(IntMap CostCentre)
  , CostCentreTree -> IntMap CostCentreNo
costCentreParents :: !(IntMap CostCentreNo)
  , CostCentreTree -> IntMap (Set CostCentre)
costCentreChildren :: !(IntMap (Set CostCentre))
  , CostCentreTree -> Map (Text, Text) (Set CostCentre)
costCentreCallSites :: !(Map (Text, Text) (Set CostCentre))
  , CostCentreTree -> Map Text (Map Text AggregatedCostCentre)
costCentreAggregate :: !(Map Text (Map Text AggregatedCostCentre))
  } deriving CostCentreNo -> CostCentreTree -> ShowS
[CostCentreTree] -> ShowS
CostCentreTree -> String
forall a.
(CostCentreNo -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CostCentreTree] -> ShowS
$cshowList :: [CostCentreTree] -> ShowS
show :: CostCentreTree -> String
$cshow :: CostCentreTree -> String
showsPrec :: CostCentreNo -> CostCentreTree -> ShowS
$cshowsPrec :: CostCentreNo -> CostCentreTree -> ShowS
Show

emptyCostCentreTree :: CostCentreTree
emptyCostCentreTree :: CostCentreTree
emptyCostCentreTree = CostCentreTree
  { costCentreNodes :: IntMap CostCentre
costCentreNodes = forall a. Monoid a => a
mempty
  , costCentreParents :: IntMap CostCentreNo
costCentreParents = forall a. Monoid a => a
mempty
  , costCentreChildren :: IntMap (Set CostCentre)
costCentreChildren = forall a. Monoid a => a
mempty
  , costCentreCallSites :: Map (Text, Text) (Set CostCentre)
costCentreCallSites = forall a. Monoid a => a
mempty
  , costCentreAggregate :: Map Text (Map Text AggregatedCostCentre)
costCentreAggregate = forall a. Monoid a => a
mempty
  }

data CallSite cc = CallSite
  { forall cc. CallSite cc -> cc
callSiteCostCentre :: cc
  -- ^ Metrics for the caller function
  , forall cc. CallSite cc -> Integer
callSiteContribEntries :: !Integer
  -- ^ Number of entries contriubted by the caller function
  , forall cc. CallSite cc -> Scientific
callSiteContribTime :: !Scientific
  -- ^ Time contributed by the caller function
  , forall cc. CallSite cc -> Scientific
callSiteContribAlloc :: !Scientific
  -- ^ Allocation contributed by the caller function
  , forall cc. CallSite cc -> Maybe Integer
callSiteContribTicks :: !(Maybe Integer)
  -- ^ Number of tikcs contributed by the caller function
  , forall cc. CallSite cc -> Maybe Integer
callSiteContribBytes :: !(Maybe Integer)
  -- ^ Number of allocated bytes contributed byt hte caller function
  } deriving CostCentreNo -> CallSite cc -> ShowS
forall cc. Show cc => CostCentreNo -> CallSite cc -> ShowS
forall cc. Show cc => [CallSite cc] -> ShowS
forall cc. Show cc => CallSite cc -> String
forall a.
(CostCentreNo -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CallSite cc] -> ShowS
$cshowList :: forall cc. Show cc => [CallSite cc] -> ShowS
show :: CallSite cc -> String
$cshow :: forall cc. Show cc => CallSite cc -> String
showsPrec :: CostCentreNo -> CallSite cc -> ShowS
$cshowsPrec :: forall cc. Show cc => CostCentreNo -> CallSite cc -> ShowS
Show

data AggregateModule = AggregateModule
  { AggregateModule -> Text
aggregateModuleName :: !Text
  -- ^ Name of the module
  , AggregateModule -> Maybe Integer
aggregateModuleEntries :: !(Maybe Integer)
  -- ^ Total number of entries to cost centres in the module
  , AggregateModule -> Scientific
aggregateModuleTime :: !Scientific
  -- ^ Total time spent on cost centres in the module
  , AggregateModule -> Scientific
aggregateModuleAlloc :: !Scientific
  -- ^ Total allocation on cost centres in the module
  , AggregateModule -> Maybe Integer
aggregateModuleTicks :: !(Maybe Integer)
  -- ^ Total ticks on cost centres in the module. This number exists only if
  -- @-P@ or @-Pa@ option is given at run-time.
  , AggregateModule -> Maybe Integer
aggregateModuleBytes :: !(Maybe Integer)
  -- ^ Total memory allocation on cost centres in the module. This number
  -- exists only if @-P@ or @-Pa@ option is given at run-time.
  } deriving (CostCentreNo -> AggregateModule -> ShowS
[AggregateModule] -> ShowS
AggregateModule -> String
forall a.
(CostCentreNo -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AggregateModule] -> ShowS
$cshowList :: [AggregateModule] -> ShowS
show :: AggregateModule -> String
$cshow :: AggregateModule -> String
showsPrec :: CostCentreNo -> AggregateModule -> ShowS
$cshowsPrec :: CostCentreNo -> AggregateModule -> ShowS
Show, AggregateModule -> AggregateModule -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AggregateModule -> AggregateModule -> Bool
$c/= :: AggregateModule -> AggregateModule -> Bool
== :: AggregateModule -> AggregateModule -> Bool
$c== :: AggregateModule -> AggregateModule -> Bool
Eq, Eq AggregateModule
AggregateModule -> AggregateModule -> Bool
AggregateModule -> AggregateModule -> Ordering
AggregateModule -> AggregateModule -> AggregateModule
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: AggregateModule -> AggregateModule -> AggregateModule
$cmin :: AggregateModule -> AggregateModule -> AggregateModule
max :: AggregateModule -> AggregateModule -> AggregateModule
$cmax :: AggregateModule -> AggregateModule -> AggregateModule
>= :: AggregateModule -> AggregateModule -> Bool
$c>= :: AggregateModule -> AggregateModule -> Bool
> :: AggregateModule -> AggregateModule -> Bool
$c> :: AggregateModule -> AggregateModule -> Bool
<= :: AggregateModule -> AggregateModule -> Bool
$c<= :: AggregateModule -> AggregateModule -> Bool
< :: AggregateModule -> AggregateModule -> Bool
$c< :: AggregateModule -> AggregateModule -> Bool
compare :: AggregateModule -> AggregateModule -> Ordering
$ccompare :: AggregateModule -> AggregateModule -> Ordering
Ord)

emptyAggregateModule :: Text -> AggregateModule
emptyAggregateModule :: Text -> AggregateModule
emptyAggregateModule Text
name = AggregateModule
  { aggregateModuleName :: Text
aggregateModuleName = Text
name
  , aggregateModuleEntries :: Maybe Integer
aggregateModuleEntries = forall a. a -> Maybe a
Just Integer
0
  , aggregateModuleTime :: Scientific
aggregateModuleTime = Scientific
0
  , aggregateModuleAlloc :: Scientific
aggregateModuleAlloc = Scientific
0
  , aggregateModuleTicks :: Maybe Integer
aggregateModuleTicks = forall a. a -> Maybe a
Just Integer
0
  , aggregateModuleBytes :: Maybe Integer
aggregateModuleBytes = forall a. a -> Maybe a
Just Integer
0
  }