packages feed

eventlog-live-0.11.0.0: src-lib/GHC/Eventlog/Live/Processor/Threads.hs

{-# OPTIONS_GHC -Wno-orphans #-}

{- |
Module      : GHC.Eventlog.Live.Processor.Threads
Description : Thread Event Processors for OTLP.
Stability   : experimental
Portability : portable
-}
module GHC.Eventlog.Live.Processor.Threads (
  processThreadEvents,
)
where

import Control.Monad.IO.Class (MonadIO (..))
import Data.DList (DList)
import Data.DList qualified as D
import Data.Machine (ProcessT, asParts, mapping, (~>))
import Data.Machine.Fanout (fanout)
import Data.Proxy (Proxy (..))
import GHC.Eventlog.Live.Config (FullConfig (..))
import GHC.Eventlog.Live.Config qualified as C
import GHC.Eventlog.Live.Logger (Logger)
import GHC.Eventlog.Live.Machine.Analysis.Capability qualified as M
import GHC.Eventlog.Live.Machine.Analysis.Thread qualified as M
import GHC.Eventlog.Live.Machine.Core (Tick)
import GHC.Eventlog.Live.Machine.Core qualified as M
import GHC.Eventlog.Live.Machine.Validate (validateOrder)
import GHC.Eventlog.Live.Machine.WithStartTime (WithStartTime (..))
import GHC.Eventlog.Live.Machine.WithStartTime qualified as M
import GHC.Eventlog.Live.Processor.Core (runIf)
import GHC.Eventlog.Live.Processor.Core.Metrics qualified as CM
import GHC.Eventlog.Live.Processor.Core.Traces qualified as CT
import GHC.Eventlog.Live.Types.Metrics (SomeMetrics, toMetric)
import GHC.Eventlog.Live.Types.Traces (SomeSpans (..))
import GHC.RTS.Events (Event (..))

data ABC a b c = A !a | B !b | C !c

processThreadEvents ::
  (MonadIO m) =>
  Logger m ->
  FullConfig ->
  ProcessT m (Tick (WithStartTime Event)) (Tick (DList (Either SomeMetrics SomeSpans)))
processThreadEvents verbosity fullConfig =
  runIf (shouldProcessThreadEvents fullConfig) $
    M.sortByTicks (.value.evTime) fullConfig.eventlogFlushIntervalX
      ~> M.liftTick
        ( fanout
            [ validateOrder verbosity (.value.evTime)
            , runIf (shouldComputeCapabilityUsageSpan fullConfig) $
                M.processGCSpans verbosity
                  ~> mapping (D.singleton . A)
            , runIf (shouldComputeThreadStateSpan fullConfig) $
                M.processThreadStateSpans' M.tryGetTimeUnixNano (.value) M.setWithStartTime'value verbosity
                  ~> fanout
                    [ M.asMutatorSpans' (.value) M.setWithStartTime'value
                        ~> mapping (D.singleton . B)
                    , mapping (D.singleton . C)
                    ]
            ]
        )
      ~> M.liftTick (asParts ~> mapping repackCapabilityUsageSpanOrThreadStateSpan)
      ~> fanout
        [ M.liftTick (mapping leftToMaybe ~> asParts)
            ~> M.fanoutTick
              [ M.liftTick M.processCapabilityUsageDuration
                  ~> M.fanoutTick
                    [ CM.process (Proxy @"capabilityUsage") (mapping toMetric) fullConfig
                        ~> M.liftTick (mapping D.singleton)
                    , CM.process (Proxy @"productivity") (M.processProductivity ~> mapping (fmap (* 100.0) . toMetric)) fullConfig
                        ~> M.liftTick (mapping D.singleton)
                    ]
                  ~> mapping (fmap (fmap Left))
              , M.liftTick M.dropStartTime
                  ~> CT.process (Proxy @"capabilityUsage") fullConfig
                  ~> M.liftTick (mapping (D.singleton . Right))
              ]
        , M.liftTick (mapping rightToMaybe ~> asParts)
            ~> M.liftTick M.dropStartTime
            ~> CT.process (Proxy @"threadState") fullConfig
            ~> M.liftTick (mapping (D.singleton . Right))
        ]
 where
  repackCapabilityUsageSpanOrThreadStateSpan = \case
    A i -> Left $ fmap Left i
    B i -> Left $ fmap Right i
    C i -> Right i

{- |
Internal helper.
Get the `Left` value, if any.
-}
leftToMaybe :: Either a b -> Maybe a
leftToMaybe = either Just (const Nothing)

{- |
Internal helper.
Get the `Right` value, if any.
-}
rightToMaybe :: Either a b -> Maybe b
rightToMaybe = either (const Nothing) Just

{- |
Internal helper.
Determine whether or not any thread events should be processed at all.
-}
shouldProcessThreadEvents :: FullConfig -> Bool
shouldProcessThreadEvents = shouldComputeThreadStateSpan

{- |
Internal helper.
Determine whether or not the capability usage spans should be computed.
-}
shouldComputeCapabilityUsageSpan :: FullConfig -> Bool
shouldComputeCapabilityUsageSpan fullConfig =
  C.processorEnabled (.metrics) (.capabilityUsage) fullConfig
    || C.processorEnabled (.metrics) (.productivity) fullConfig
    || C.processorEnabled (.traces) (.capabilityUsage) fullConfig

{- |
Internal helper.
Determine whether or not the thread state spans should be computed.
-}
shouldComputeThreadStateSpan :: FullConfig -> Bool
shouldComputeThreadStateSpan fullConfig =
  C.processorEnabled (.traces) (.threadState) fullConfig
    || shouldComputeCapabilityUsageSpan fullConfig