-- | RTS stats type.
module ParkBench.RtsStats
  ( RtsStats (RtsStats),
    allocated_bytes,
    allocated_bytes_per_second,
    average_live_data,
    elapsed_ns,
    cumulative_live_bytes,
    cumulative_par_balanced_copied_bytes,
    copied_bytes,
    max_mem_in_use_bytes,
    major_gcs,
    gcs,
    par_copied_bytes,
    cpu_ns,
    gc_average_ns,
    gc_cpu_ns,
    gc_cpu_percent,
    gc_elapsed_ns,
    max_slop_bytes,
    gc_wall_percent,
    max_compact_bytes,
    max_large_objects_bytes,
    max_live_bytes,
    max_normal_objects_bytes,
    minor_gcs,
    mut_cpu_percent,
    mut_wall_percent,
    mutator_cpu_ns,
    mutator_elapsed_ns,
    work_balance,
  )
where

import ParkBench.Prelude
import ParkBench.Statistics

-- | RTS stats type.
--
-- This type is intentionally not a record, because it's kind of large, and generated record accessors cause quadratic
-- time to compile.
--
-- The hand-written accessors do, too, but we don't need the generated setters at all, so there are some savings by
-- writing it all out by hand.
--
-- TODO nonmoving_gc_*
data RtsStats
  = RtsStats
      {-# UNPACK #-} !Rational -- allocated_bytes
      {-# UNPACK #-} !Rational -- copied_bytes
      {-# UNPACK #-} !Rational -- cpu_ns
      {-# UNPACK #-} !Rational -- cumulative_live_bytes, requires major gc
      {-# UNPACK #-} !Rational -- cumulative_par_balanced_copied_bytes
      {-# UNPACK #-} !Rational -- elapsed_ns
      {-# UNPACK #-} !Rational -- gc_cpu_ns
      {-# UNPACK #-} !Rational -- gc_elapsed_ns
      {-# UNPACK #-} !Rational -- gcs
      {-# UNPACK #-} !Rational -- major_gcs
      {-# UNPACK #-} !Rational -- max_compact_bytes, requires major gc
      {-# UNPACK #-} !Rational -- max_large_objects_bytes, requires major gc
      {-# UNPACK #-} !Rational -- max_live_bytes, requires major gc
      {-# UNPACK #-} !Rational -- max_mem_in_use_bytes
      {-# UNPACK #-} !Rational -- max_slop_bytes, requires major gc
      {-# UNPACK #-} !Rational -- mutator_cpu_ns
      {-# UNPACK #-} !Rational -- mutator_elapsed_ns
      {-# UNPACK #-} !Rational -- par_copied_bytes

instance Roll RtsStats where
  roll :: (Rational -> Rational -> Rational)
-> RtsStats -> RtsStats -> RtsStats
roll
    Rational -> Rational -> Rational
step
    ( RtsStats
        Rational
allocated_bytes0
        Rational
copied_bytes0
        Rational
cpu_ns0
        Rational
cumulative_live_bytes0
        Rational
cumulative_par_balanced_copied_bytes0
        Rational
elapsed_ns0
        Rational
gc_cpu_ns0
        Rational
gc_elapsed_ns0
        Rational
gcs0
        Rational
major_gcs0
        Rational
max_compact_bytes0
        Rational
max_large_objects_bytes0
        Rational
max_live_bytes0
        Rational
max_mem_in_use_bytes0
        Rational
max_slop_bytes0
        Rational
mutator_cpu_ns0
        Rational
mutator_elapsed_ns0
        Rational
par_copied_bytes0
      )
    ( RtsStats
        Rational
allocated_bytes1
        Rational
copied_bytes1
        Rational
cpu_ns1
        Rational
cumulative_live_bytes1
        Rational
cumulative_par_balanced_copied_bytes1
        Rational
elapsed_ns1
        Rational
gc_cpu_ns1
        Rational
gc_elapsed_ns1
        Rational
gcs1
        Rational
major_gcs1
        Rational
max_compact_bytes1
        Rational
max_large_objects_bytes1
        Rational
max_live_bytes1
        Rational
max_mem_in_use_bytes1
        Rational
max_slop_bytes1
        Rational
mutator_cpu_ns1
        Rational
mutator_elapsed_ns1
        Rational
par_copied_bytes1
      ) =
      Rational
-> Rational
-> Rational
-> Rational
-> Rational
-> Rational
-> Rational
-> Rational
-> Rational
-> Rational
-> Rational
-> Rational
-> Rational
-> Rational
-> Rational
-> Rational
-> Rational
-> Rational
-> RtsStats
RtsStats
        (Rational -> Rational -> Rational
step Rational
allocated_bytes0 Rational
allocated_bytes1)
        (Rational -> Rational -> Rational
step Rational
copied_bytes0 Rational
copied_bytes1)
        (Rational -> Rational -> Rational
step Rational
cpu_ns0 Rational
cpu_ns1)
        (Rational -> Rational -> Rational
step Rational
cumulative_live_bytes0 Rational
cumulative_live_bytes1)
        (Rational -> Rational -> Rational
step Rational
cumulative_par_balanced_copied_bytes0 Rational
cumulative_par_balanced_copied_bytes1)
        (Rational -> Rational -> Rational
step Rational
elapsed_ns0 Rational
elapsed_ns1)
        (Rational -> Rational -> Rational
step Rational
gc_cpu_ns0 Rational
gc_cpu_ns1)
        (Rational -> Rational -> Rational
step Rational
gc_elapsed_ns0 Rational
gc_elapsed_ns1)
        (Rational -> Rational -> Rational
step Rational
gcs0 Rational
gcs1)
        (Rational -> Rational -> Rational
step Rational
major_gcs0 Rational
major_gcs1)
        (Rational -> Rational -> Rational
forall a. Ord a => a -> a -> a
max Rational
max_compact_bytes0 Rational
max_compact_bytes1)
        (Rational -> Rational -> Rational
forall a. Ord a => a -> a -> a
max Rational
max_large_objects_bytes0 Rational
max_large_objects_bytes1)
        (Rational -> Rational -> Rational
forall a. Ord a => a -> a -> a
max Rational
max_live_bytes0 Rational
max_live_bytes1)
        (Rational -> Rational -> Rational
forall a. Ord a => a -> a -> a
max Rational
max_mem_in_use_bytes0 Rational
max_mem_in_use_bytes1)
        (Rational -> Rational -> Rational
forall a. Ord a => a -> a -> a
max Rational
max_slop_bytes0 Rational
max_slop_bytes1)
        (Rational -> Rational -> Rational
step Rational
mutator_cpu_ns0 Rational
mutator_cpu_ns1)
        (Rational -> Rational -> Rational
step Rational
mutator_elapsed_ns0 Rational
mutator_elapsed_ns1)
        (Rational -> Rational -> Rational
step Rational
par_copied_bytes0 Rational
par_copied_bytes1)

allocated_bytes :: RtsStats -> Rational
allocated_bytes :: RtsStats -> Rational
allocated_bytes (RtsStats Rational
n Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_) = Rational
n

allocated_bytes_per_second :: RtsStats -> Rational
allocated_bytes_per_second :: RtsStats -> Rational
allocated_bytes_per_second RtsStats
s =
  RtsStats -> Rational
allocated_bytes RtsStats
s Rational -> Rational -> Rational
`divide` (RtsStats -> Rational
elapsed_ns RtsStats
s Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ Rational
1_000_000_000)

average_live_data :: RtsStats -> Rational
average_live_data :: RtsStats -> Rational
average_live_data RtsStats
s =
  -- If the thing didn't even perform 1 major GC, we still performed one ourselves to get accurate an accurate
  -- cumulative_live_bytes count. So, use 1 as the denominator in this case, too.
  RtsStats -> Rational
cumulative_live_bytes RtsStats
s Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ Rational -> Rational -> Rational
forall a. Ord a => a -> a -> a
max Rational
1 (RtsStats -> Rational
major_gcs RtsStats
s)

copied_bytes :: RtsStats -> Rational
copied_bytes :: RtsStats -> Rational
copied_bytes (RtsStats Rational
_ Rational
n Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_) = Rational
n

cpu_ns :: RtsStats -> Rational
cpu_ns :: RtsStats -> Rational
cpu_ns (RtsStats Rational
_ Rational
_ Rational
n Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_) = Rational
n

cumulative_live_bytes :: RtsStats -> Rational
cumulative_live_bytes :: RtsStats -> Rational
cumulative_live_bytes (RtsStats Rational
_ Rational
_ Rational
_ Rational
n Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_) = Rational
n

cumulative_par_balanced_copied_bytes :: RtsStats -> Rational
cumulative_par_balanced_copied_bytes :: RtsStats -> Rational
cumulative_par_balanced_copied_bytes (RtsStats Rational
_ Rational
_ Rational
_ Rational
_ Rational
n Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_) = Rational
n

elapsed_ns :: RtsStats -> Rational
elapsed_ns :: RtsStats -> Rational
elapsed_ns (RtsStats Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
n Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_) = Rational
n

gc_average_ns :: RtsStats -> Rational
gc_average_ns :: RtsStats -> Rational
gc_average_ns RtsStats
s =
  RtsStats -> Rational
gc_elapsed_ns RtsStats
s Rational -> Rational -> Rational
`divide` RtsStats -> Rational
gcs RtsStats
s

gc_cpu_ns :: RtsStats -> Rational
gc_cpu_ns :: RtsStats -> Rational
gc_cpu_ns (RtsStats Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
n Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_) = Rational
n

gc_cpu_percent :: RtsStats -> Rational
gc_cpu_percent :: RtsStats -> Rational
gc_cpu_percent RtsStats
s =
  RtsStats -> Rational
gc_cpu_ns RtsStats
s Rational -> Rational -> Rational
`divide` RtsStats -> Rational
cpu_ns RtsStats
s

gc_elapsed_ns :: RtsStats -> Rational
gc_elapsed_ns :: RtsStats -> Rational
gc_elapsed_ns (RtsStats Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
n Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_) =
  Rational
n

gc_wall_percent :: RtsStats -> Rational
gc_wall_percent :: RtsStats -> Rational
gc_wall_percent RtsStats
s =
  RtsStats -> Rational
gc_elapsed_ns RtsStats
s Rational -> Rational -> Rational
`divide` RtsStats -> Rational
elapsed_ns RtsStats
s

gcs :: RtsStats -> Rational
gcs :: RtsStats -> Rational
gcs (RtsStats Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
n Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_) =
  Rational
n

major_gcs :: RtsStats -> Rational
major_gcs :: RtsStats -> Rational
major_gcs (RtsStats Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
n Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_) =
  Rational
n

max_compact_bytes :: RtsStats -> Rational
max_compact_bytes :: RtsStats -> Rational
max_compact_bytes (RtsStats Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
n Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_) =
  Rational
n

max_large_objects_bytes :: RtsStats -> Rational
max_large_objects_bytes :: RtsStats -> Rational
max_large_objects_bytes (RtsStats Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
n Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_) =
  Rational
n

max_live_bytes :: RtsStats -> Rational
max_live_bytes :: RtsStats -> Rational
max_live_bytes (RtsStats Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
n Rational
_ Rational
_ Rational
_ Rational
_ Rational
_) =
  Rational
n

max_mem_in_use_bytes :: RtsStats -> Rational
max_mem_in_use_bytes :: RtsStats -> Rational
max_mem_in_use_bytes (RtsStats Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
n Rational
_ Rational
_ Rational
_ Rational
_) =
  Rational
n

max_normal_objects_bytes :: RtsStats -> Rational
max_normal_objects_bytes :: RtsStats -> Rational
max_normal_objects_bytes RtsStats
s =
  RtsStats -> Rational
max_live_bytes RtsStats
s Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
- RtsStats -> Rational
max_compact_bytes RtsStats
s Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
- RtsStats -> Rational
max_large_objects_bytes RtsStats
s

max_slop_bytes :: RtsStats -> Rational
max_slop_bytes :: RtsStats -> Rational
max_slop_bytes (RtsStats Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
n Rational
_ Rational
_ Rational
_) =
  Rational
n

minor_gcs :: RtsStats -> Rational
minor_gcs :: RtsStats -> Rational
minor_gcs RtsStats
s =
  RtsStats -> Rational
gcs RtsStats
s Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
- RtsStats -> Rational
major_gcs RtsStats
s

mut_cpu_percent :: RtsStats -> Rational
mut_cpu_percent :: RtsStats -> Rational
mut_cpu_percent RtsStats
s =
  RtsStats -> Rational
mutator_cpu_ns RtsStats
s Rational -> Rational -> Rational
`divide` RtsStats -> Rational
cpu_ns RtsStats
s

mut_wall_percent :: RtsStats -> Rational
mut_wall_percent :: RtsStats -> Rational
mut_wall_percent RtsStats
s =
  RtsStats -> Rational
mutator_elapsed_ns RtsStats
s Rational -> Rational -> Rational
`divide` RtsStats -> Rational
elapsed_ns RtsStats
s

mutator_cpu_ns :: RtsStats -> Rational
mutator_cpu_ns :: RtsStats -> Rational
mutator_cpu_ns (RtsStats Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
n Rational
_ Rational
_) =
  Rational
n

mutator_elapsed_ns :: RtsStats -> Rational
mutator_elapsed_ns :: RtsStats -> Rational
mutator_elapsed_ns (RtsStats Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
n Rational
_) =
  Rational
n

par_copied_bytes :: RtsStats -> Rational
par_copied_bytes :: RtsStats -> Rational
par_copied_bytes (RtsStats Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
_ Rational
n) =
  Rational
n

work_balance :: RtsStats -> Maybe Rational
work_balance :: RtsStats -> Maybe Rational
work_balance RtsStats
s =
  RtsStats -> Rational
cumulative_par_balanced_copied_bytes RtsStats
s Rational -> Rational -> Maybe Rational
`divide'` RtsStats -> Rational
par_copied_bytes RtsStats
s