{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RecordWildCards #-}

module Hermod.Tracing.Resources.Types
    ( Resources(..)
    , ResourceStats
    ) where


import           Cardano.Logging

import           Data.Aeson
import           Data.Text (pack)
import           Data.Word
import           GHC.Generics (Generic)

-- | A snapshot of resource usage for the current process, parameterised over
-- the numeric type @a@. The concrete alias 'ResourceStats' fixes @a ~ Word64@.
--
-- Fields that cannot be measured on the current platform are set to @0@.
data Resources a
  = Resources
      { forall a. Resources a -> a
rCentiCpu   :: !a
        -- ^ CPU time in centiseconds (1\/100 s) since process start,
        --   from @\/proc\/self\/stat@ on Linux or equivalent OS API.
      , forall a. Resources a -> a
rCentiGC    :: !a
        -- ^ CPU centiseconds spent in the GHC garbage collector (RTS stats).
      , forall a. Resources a -> a
rCentiMut   :: !a
        -- ^ CPU centiseconds spent in the mutator, i.e. application code (RTS stats).
      , forall a. Resources a -> a
rGcsMajor   :: !a
        -- ^ Number of major (full-heap) GC runs since process start (RTS stats).
      , forall a. Resources a -> a
rGcsMinor   :: !a
        -- ^ Number of minor GC runs since process start (RTS stats).
      , forall a. Resources a -> a
rAlloc      :: !a
        -- ^ Cumulative bytes allocated in the heap since process start (RTS stats).
      , forall a. Resources a -> a
rLive       :: !a
        -- ^ Live heap bytes immediately after the last GC run (RTS stats).
      , forall a. Resources a -> a
rHeap       :: !a
        -- ^ Committed heap bytes (total heap size reserved from the OS) (RTS stats).
      , forall a. Resources a -> a
rRSS        :: !a
        -- ^ Resident set size in bytes: physical memory currently mapped to
        --   the process (from the OS kernel).
      , forall a. Resources a -> a
rCentiBlkIO :: !a
        -- ^ Centiseconds spent waiting for block I/O (Linux @\/proc\/self\/stat@ only;
        --   @0@ on other platforms).
      , forall a. Resources a -> a
rNetRd      :: !a
        -- ^ IP packet bytes received since boot (Linux @\/proc\/self\/net\/netstat@,
        --   only when the @with-netstat@ flag is enabled; @0@ otherwise).
      , forall a. Resources a -> a
rNetWr      :: !a
        -- ^ IP packet bytes transmitted since boot (Linux @\/proc\/self\/net\/netstat@,
        --   only when the @with-netstat@ flag is enabled; @0@ otherwise).
      , forall a. Resources a -> a
rFsRd       :: !a
        -- ^ Filesystem bytes read by the process (from @\/proc\/self\/io@ on Linux
        --   or equivalent OS API).
      , forall a. Resources a -> a
rFsWr       :: !a
        -- ^ Filesystem bytes written by the process (from @\/proc\/self\/io@ on Linux
        --   or equivalent OS API).
      , forall a. Resources a -> a
rThreads    :: !a
        -- ^ Number of live GHC green threads (RTS stats).
      }
  deriving ((forall a b. (a -> b) -> Resources a -> Resources b)
-> (forall a b. a -> Resources b -> Resources a)
-> Functor Resources
forall a b. a -> Resources b -> Resources a
forall a b. (a -> b) -> Resources a -> Resources b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> Resources a -> Resources b
fmap :: forall a b. (a -> b) -> Resources a -> Resources b
$c<$ :: forall a b. a -> Resources b -> Resources a
<$ :: forall a b. a -> Resources b -> Resources a
Functor, (forall x. Resources a -> Rep (Resources a) x)
-> (forall x. Rep (Resources a) x -> Resources a)
-> Generic (Resources a)
forall x. Rep (Resources a) x -> Resources a
forall x. Resources a -> Rep (Resources a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Resources a) x -> Resources a
forall a x. Resources a -> Rep (Resources a) x
$cfrom :: forall a x. Resources a -> Rep (Resources a) x
from :: forall x. Resources a -> Rep (Resources a) x
$cto :: forall a x. Rep (Resources a) x -> Resources a
to :: forall x. Rep (Resources a) x -> Resources a
Generic, Int -> Resources a -> ShowS
[Resources a] -> ShowS
Resources a -> String
(Int -> Resources a -> ShowS)
-> (Resources a -> String)
-> ([Resources a] -> ShowS)
-> Show (Resources a)
forall a. Show a => Int -> Resources a -> ShowS
forall a. Show a => [Resources a] -> ShowS
forall a. Show a => Resources a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Resources a -> ShowS
showsPrec :: Int -> Resources a -> ShowS
$cshow :: forall a. Show a => Resources a -> String
show :: Resources a -> String
$cshowList :: forall a. Show a => [Resources a] -> ShowS
showList :: [Resources a] -> ShowS
Show)

-- | Concrete snapshot of resource usage with all fields as 'Word64' counts.
type ResourceStats = Resources Word64

instance Applicative Resources where
  pure :: forall a. a -> Resources a
pure a
a = a
-> a
-> a
-> a
-> a
-> a
-> a
-> a
-> a
-> a
-> a
-> a
-> a
-> a
-> a
-> Resources a
forall a.
a
-> a
-> a
-> a
-> a
-> a
-> a
-> a
-> a
-> a
-> a
-> a
-> a
-> a
-> a
-> Resources a
Resources a
a a
a a
a a
a a
a a
a a
a a
a a
a a
a a
a a
a a
a a
a a
a
  Resources (a -> b)
f <*> :: forall a b. Resources (a -> b) -> Resources a -> Resources b
<*> Resources a
x =
    Resources
    { rCentiCpu :: b
rCentiCpu   = Resources (a -> b) -> a -> b
forall a. Resources a -> a
rCentiCpu   Resources (a -> b)
f (Resources a -> a
forall a. Resources a -> a
rCentiCpu   Resources a
x)
    , rCentiGC :: b
rCentiGC    = Resources (a -> b) -> a -> b
forall a. Resources a -> a
rCentiGC    Resources (a -> b)
f (Resources a -> a
forall a. Resources a -> a
rCentiGC    Resources a
x)
    , rCentiMut :: b
rCentiMut   = Resources (a -> b) -> a -> b
forall a. Resources a -> a
rCentiMut   Resources (a -> b)
f (Resources a -> a
forall a. Resources a -> a
rCentiMut   Resources a
x)
    , rGcsMajor :: b
rGcsMajor   = Resources (a -> b) -> a -> b
forall a. Resources a -> a
rGcsMajor   Resources (a -> b)
f (Resources a -> a
forall a. Resources a -> a
rGcsMajor   Resources a
x)
    , rGcsMinor :: b
rGcsMinor   = Resources (a -> b) -> a -> b
forall a. Resources a -> a
rGcsMinor   Resources (a -> b)
f (Resources a -> a
forall a. Resources a -> a
rGcsMinor   Resources a
x)
    , rAlloc :: b
rAlloc      = Resources (a -> b) -> a -> b
forall a. Resources a -> a
rAlloc      Resources (a -> b)
f (Resources a -> a
forall a. Resources a -> a
rAlloc      Resources a
x)
    , rLive :: b
rLive       = Resources (a -> b) -> a -> b
forall a. Resources a -> a
rLive       Resources (a -> b)
f (Resources a -> a
forall a. Resources a -> a
rLive       Resources a
x)
    , rHeap :: b
rHeap       = Resources (a -> b) -> a -> b
forall a. Resources a -> a
rHeap       Resources (a -> b)
f (Resources a -> a
forall a. Resources a -> a
rHeap       Resources a
x)
    , rRSS :: b
rRSS        = Resources (a -> b) -> a -> b
forall a. Resources a -> a
rRSS        Resources (a -> b)
f (Resources a -> a
forall a. Resources a -> a
rRSS        Resources a
x)
    , rCentiBlkIO :: b
rCentiBlkIO = Resources (a -> b) -> a -> b
forall a. Resources a -> a
rCentiBlkIO Resources (a -> b)
f (Resources a -> a
forall a. Resources a -> a
rCentiBlkIO Resources a
x)
    , rNetRd :: b
rNetRd = Resources (a -> b) -> a -> b
forall a. Resources a -> a
rNetRd Resources (a -> b)
f (Resources a -> a
forall a. Resources a -> a
rNetRd Resources a
x)
    , rNetWr :: b
rNetWr = Resources (a -> b) -> a -> b
forall a. Resources a -> a
rNetWr Resources (a -> b)
f (Resources a -> a
forall a. Resources a -> a
rNetWr Resources a
x)
    , rFsRd :: b
rFsRd  = Resources (a -> b) -> a -> b
forall a. Resources a -> a
rFsRd  Resources (a -> b)
f (Resources a -> a
forall a. Resources a -> a
rFsRd  Resources a
x)
    , rFsWr :: b
rFsWr  = Resources (a -> b) -> a -> b
forall a. Resources a -> a
rFsWr  Resources (a -> b)
f (Resources a -> a
forall a. Resources a -> a
rFsWr  Resources a
x)
    , rThreads :: b
rThreads    = Resources (a -> b) -> a -> b
forall a. Resources a -> a
rThreads    Resources (a -> b)
f (Resources a -> a
forall a. Resources a -> a
rThreads    Resources a
x)
    }

instance FromJSON a => FromJSON (Resources a) where
  parseJSON :: Value -> Parser (Resources a)
parseJSON = Options -> Value -> Parser (Resources a)
forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON Options
jsonEncodingOptions

instance ToJSON a => ToJSON (Resources a) where
  toJSON :: Resources a -> Value
toJSON = Options -> Resources a -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON Options
jsonEncodingOptions
  toEncoding :: Resources a -> Encoding
toEncoding = Options -> Resources a -> Encoding
forall a.
(Generic a, GToJSON' Encoding Zero (Rep a)) =>
Options -> a -> Encoding
genericToEncoding Options
jsonEncodingOptions

jsonEncodingOptions :: Options
jsonEncodingOptions :: Options
jsonEncodingOptions = Options
defaultOptions
  { fieldLabelModifier     = drop 1
  , tagSingleConstructors  = True
  , sumEncoding =
    TaggedObject
    { tagFieldName = "kind"
    , contentsFieldName = "contents"
    }
  }

instance LogFormatting ResourceStats where
    forHuman :: ResourceStats -> Text
forHuman Resources{Word64
rCentiCpu :: forall a. Resources a -> a
rCentiGC :: forall a. Resources a -> a
rCentiMut :: forall a. Resources a -> a
rGcsMajor :: forall a. Resources a -> a
rGcsMinor :: forall a. Resources a -> a
rAlloc :: forall a. Resources a -> a
rLive :: forall a. Resources a -> a
rHeap :: forall a. Resources a -> a
rRSS :: forall a. Resources a -> a
rCentiBlkIO :: forall a. Resources a -> a
rNetRd :: forall a. Resources a -> a
rNetWr :: forall a. Resources a -> a
rFsRd :: forall a. Resources a -> a
rFsWr :: forall a. Resources a -> a
rThreads :: forall a. Resources a -> a
rCentiCpu :: Word64
rCentiGC :: Word64
rCentiMut :: Word64
rGcsMajor :: Word64
rGcsMinor :: Word64
rAlloc :: Word64
rLive :: Word64
rHeap :: Word64
rRSS :: Word64
rCentiBlkIO :: Word64
rNetRd :: Word64
rNetWr :: Word64
rFsRd :: Word64
rFsWr :: Word64
rThreads :: Word64
..} = Text
"Resources:"
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>  Text
" Cpu Ticks "            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (String -> Text
pack (String -> Text) -> (Word64 -> String) -> Word64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> String
forall a. Show a => a -> String
show) Word64
rCentiCpu
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", GC centiseconds "      Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (String -> Text
pack (String -> Text) -> (Word64 -> String) -> Word64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> String
forall a. Show a => a -> String
show) Word64
rCentiGC
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", Mutator centiseconds " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (String -> Text
pack (String -> Text) -> (Word64 -> String) -> Word64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> String
forall a. Show a => a -> String
show) Word64
rCentiMut
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", GCs major "            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (String -> Text
pack (String -> Text) -> (Word64 -> String) -> Word64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> String
forall a. Show a => a -> String
show) Word64
rGcsMajor
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", GCs minor "            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (String -> Text
pack (String -> Text) -> (Word64 -> String) -> Word64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> String
forall a. Show a => a -> String
show) Word64
rGcsMinor
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", Allocated bytes "      Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (String -> Text
pack (String -> Text) -> (Word64 -> String) -> Word64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> String
forall a. Show a => a -> String
show) Word64
rAlloc
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>Text
" , GC live bytes "        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (String -> Text
pack (String -> Text) -> (Word64 -> String) -> Word64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> String
forall a. Show a => a -> String
show) Word64
rLive
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", RTS heap "             Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (String -> Text
pack (String -> Text) -> (Word64 -> String) -> Word64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> String
forall a. Show a => a -> String
show) Word64
rHeap
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", RSS "                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (String -> Text
pack (String -> Text) -> (Word64 -> String) -> Word64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> String
forall a. Show a => a -> String
show) Word64
rRSS
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", Net bytes read "       Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (String -> Text
pack (String -> Text) -> (Word64 -> String) -> Word64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> String
forall a. Show a => a -> String
show) Word64
rNetRd
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" written "               Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (String -> Text
pack (String -> Text) -> (Word64 -> String) -> Word64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> String
forall a. Show a => a -> String
show) Word64
rNetWr
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", FS bytes read "        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (String -> Text
pack (String -> Text) -> (Word64 -> String) -> Word64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> String
forall a. Show a => a -> String
show) Word64
rFsRd
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" written "               Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (String -> Text
pack (String -> Text) -> (Word64 -> String) -> Word64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> String
forall a. Show a => a -> String
show) Word64
rFsWr
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", Threads "              Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (String -> Text
pack (String -> Text) -> (Word64 -> String) -> Word64 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> String
forall a. Show a => a -> String
show) Word64
rThreads
                  Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"."

    forMachine :: DetailLevel -> ResourceStats -> Object
forMachine DetailLevel
_dtal ResourceStats
rs = [Object] -> Object
forall a. Monoid a => [a] -> a
mconcat
      [ Key
"kind"          Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"ResourceStats"
      , Key
"CentiCpu"      Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number (Word64 -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Scientific) -> Word64 -> Scientific
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rCentiCpu ResourceStats
rs)
      , Key
"CentiGC"       Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number (Word64 -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Scientific) -> Word64 -> Scientific
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rCentiGC ResourceStats
rs)
      , Key
"CentiMut"      Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number (Word64 -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Scientific) -> Word64 -> Scientific
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rCentiMut ResourceStats
rs)
      , Key
"GcsMajor"      Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number (Word64 -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Scientific) -> Word64 -> Scientific
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rGcsMajor ResourceStats
rs)
      , Key
"GcsMinor"      Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number (Word64 -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Scientific) -> Word64 -> Scientific
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rGcsMinor ResourceStats
rs)
      , Key
"Alloc"         Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number (Word64 -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Scientific) -> Word64 -> Scientific
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rAlloc ResourceStats
rs)
      , Key
"Live"          Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number (Word64 -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Scientific) -> Word64 -> Scientific
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rLive ResourceStats
rs)
      , Key
"Heap"          Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number (Word64 -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Scientific) -> Word64 -> Scientific
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rHeap ResourceStats
rs)
      , Key
"RSS"           Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number (Word64 -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Scientific) -> Word64 -> Scientific
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rRSS ResourceStats
rs)
      , Key
"CentiBlkIO"    Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number (Word64 -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Scientific) -> Word64 -> Scientific
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rCentiBlkIO ResourceStats
rs)
      , Key
"NetRd"    Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number (Word64 -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Scientific) -> Word64 -> Scientific
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rNetRd ResourceStats
rs)
      , Key
"NetWr"    Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number (Word64 -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Scientific) -> Word64 -> Scientific
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rNetWr ResourceStats
rs)
      , Key
"FsRd"     Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number (Word64 -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Scientific) -> Word64 -> Scientific
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rFsRd ResourceStats
rs)
      , Key
"FsWr"     Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number (Word64 -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Scientific) -> Word64 -> Scientific
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rFsWr ResourceStats
rs)
      , Key
"Threads"       Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number (Word64 -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Scientific) -> Word64 -> Scientific
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rThreads ResourceStats
rs)
      ]

    asMetrics :: ResourceStats -> [Metric]
asMetrics ResourceStats
rs =
      [ Text -> Integer -> Metric
IntM Text
"Stat.cputicks"    (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rCentiCpu ResourceStats
rs)
      , Text -> Integer -> Metric
IntM Text
"RTS.gcticks"      (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rCentiGC ResourceStats
rs)
      , Text -> Integer -> Metric
IntM Text
"RTS.mutticks"     (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rCentiMut ResourceStats
rs)
      , Text -> Integer -> Metric
IntM Text
"RTS.gcMajorNum"   (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rGcsMajor ResourceStats
rs)
      , Text -> Integer -> Metric
IntM Text
"RTS.gcMinorNum"   (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rGcsMinor ResourceStats
rs)
      , Text -> Integer -> Metric
IntM Text
"RTS.alloc"        (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rAlloc ResourceStats
rs)
      , Text -> Integer -> Metric
IntM Text
"RTS.gcLiveBytes"            (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rLive ResourceStats
rs)
      , Text -> Integer -> Metric
IntM Text
"RTS.gcHeapBytes"         (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rHeap ResourceStats
rs)
      , Text -> Integer -> Metric
IntM Text
"Mem.resident"              (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rRSS ResourceStats
rs)
      , Text -> Integer -> Metric
IntM Text
"Stat.blkIOticks"  (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rCentiBlkIO ResourceStats
rs)
      , Text -> Integer -> Metric
IntM Text
"Stat.netRd"      (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rNetRd ResourceStats
rs)
      , Text -> Integer -> Metric
IntM Text
"Stat.netWr"      (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rNetWr ResourceStats
rs)
      , Text -> Integer -> Metric
IntM Text
"Stat.fsRd"       (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rFsRd ResourceStats
rs)
      , Text -> Integer -> Metric
IntM Text
"Stat.fsWr"       (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rFsWr ResourceStats
rs)
      , Text -> Integer -> Metric
IntM Text
"RTS.threads" (Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ ResourceStats -> Word64
forall a. Resources a -> a
rThreads ResourceStats
rs)
      ]

instance MetaTrace ResourceStats where
  namespaceFor :: ResourceStats -> Namespace ResourceStats
namespaceFor Resources {} =
    [Text] -> [Text] -> Namespace ResourceStats
forall a. [Text] -> [Text] -> Namespace a
Namespace [] [Text
"Resources"]
  severityFor :: Namespace ResourceStats -> Maybe ResourceStats -> Maybe SeverityS
severityFor  (Namespace [Text]
_ [Text
"Resources"]) Maybe ResourceStats
_ = SeverityS -> Maybe SeverityS
forall a. a -> Maybe a
Just SeverityS
Info
  severityFor Namespace ResourceStats
_ns Maybe ResourceStats
_ = Maybe SeverityS
forall a. Maybe a
Nothing
  documentFor :: Namespace ResourceStats -> Maybe Text
documentFor  (Namespace [Text]
_ [Text
"Resources"]) = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
""
  documentFor Namespace ResourceStats
_ns = Maybe Text
forall a. Maybe a
Nothing
  metricsDocFor :: Namespace ResourceStats -> [(Text, Text)]
metricsDocFor  (Namespace [Text]
_ [Text
"Resources"]) =
    [(Text
"Stat.cputicks", Text
"Kernel-reported CPU ticks (1/100th of a second), since process start")
    ,(Text
"RTS.gcticks", Text
"RTS-reported CPU ticks spent on GC")
    ,(Text
"RTS.mutticks", Text
"RTS-reported CPU ticks spent on mutator")
    ,(Text
"RTS.gcMajorNum", Text
"Major GCs")
    ,(Text
"RTS.gcMinorNum", Text
"Minor GCs")
    ,(Text
"RTS.alloc", Text
"RTS-reported bytes allocated")
    ,(Text
"RTS.gcLiveBytes", Text
"RTS-reported live bytes")
    ,(Text
"RTS.gcHeapBytes", Text
"RTS-reported heap bytes")
    ,(Text
"Mem.resident", Text
"Kernel-reported RSS (resident set size)")
    ,(Text
"Stat.netRd", Text
"IP packet bytes read")
    ,(Text
"Stat.netWr", Text
"IP packet bytes written")
    ,(Text
"Stat.fsRd", Text
"FS bytes read")
    ,(Text
"Stat.fsWr", Text
"FS bytes written")
    ,(Text
"RTS.threads",Text
"RTS green thread count")]
  metricsDocFor Namespace ResourceStats
_ns = []
  allNamespaces :: [Namespace ResourceStats]
allNamespaces = [ [Text] -> [Text] -> Namespace ResourceStats
forall a. [Text] -> [Text] -> Namespace a
Namespace [] [Text
"Resources"]]