{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module GHC.RTS.Events.Binary
  ( -- * Readers
    getHeader
  , getEvent
  , standardParsers
  , ghc6Parsers
  , ghc7Parsers
  , mercuryParsers
  , perfParsers
  , heapProfParsers
  , timeProfParsers
  , pre77StopParsers
  , ghc782StopParser
  , post782StopParser
  , parRTSParsers
  , binaryEventParsers

  -- * Writers
  , putEventLog
  , putHeader
  , putEvent

  -- * Perf events
  , nEVENT_PERF_NAME
  , nEVENT_PERF_COUNTER
  , nEVENT_PERF_TRACEPOINT

  ) where
import Control.Exception (assert)
import Control.Monad
import Data.List (intersperse)
import Data.Maybe
import Prelude hiding (gcd, rem, id)

import Data.Array
import Data.Binary
import Data.Binary.Put
import qualified Data.Binary.Get as G
import qualified Data.ByteString as B
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import qualified Data.Vector.Unboxed as VU

import GHC.RTS.EventTypes
import GHC.RTS.EventParserUtils

#define EVENTLOG_CONSTANTS_ONLY
#include "EventLogFormat.h"

getEventType :: Get EventType
getEventType :: Get EventType
getEventType = do
           EventTypeNum
etNum <- Get EventTypeNum
forall t. Binary t => Get t
get
           EventTypeNum
size <- Get EventTypeNum
forall t. Binary t => Get t
get :: Get EventTypeSize
           let etSize :: Maybe EventTypeNum
etSize = if EventTypeNum
size EventTypeNum -> EventTypeNum -> Bool
forall a. Eq a => a -> a -> Bool
== 0xffff then Maybe EventTypeNum
forall a. Maybe a
Nothing else EventTypeNum -> Maybe EventTypeNum
forall a. a -> Maybe a
Just EventTypeNum
size
           -- 0xffff indicates variable-sized event
           EventTypeDescLen
etDescLen <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get EventTypeDescLen
           Text
etDesc <- EventTypeDescLen -> Get Text
forall a. Integral a => a -> Get Text
getText EventTypeDescLen
etDescLen
           EventTypeDescLen
etExtraLen <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get Word32
           Int -> Get ()
G.skip (EventTypeDescLen -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral EventTypeDescLen
etExtraLen)
           EventTypeDescLen
ete <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get Marker
           Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (EventTypeDescLen
ete EventTypeDescLen -> EventTypeDescLen -> Bool
forall a. Eq a => a -> a -> Bool
/= EVENT_ET_END(Get () -> Get ()) -> Get () -> Get ()
forall a b. (a -> b) -> a -> b
) $
              String -> Get ()
forall (m :: * -> *) a. MonadFail m => String -> m a
fail "Event Type end marker not found."
           EventType -> Get EventType
forall (m :: * -> *) a. Monad m => a -> m a
return (EventTypeNum -> Text -> Maybe EventTypeNum -> EventType
EventType EventTypeNum
etNum Text
etDesc Maybe EventTypeNum
etSize)

getHeader :: Get Header
getHeader :: Get Header
getHeader = do
            EventTypeDescLen
hdrb <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get Marker
            Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (EventTypeDescLen
hdrb EventTypeDescLen -> EventTypeDescLen -> Bool
forall a. Eq a => a -> a -> Bool
/= EVENT_HEADER_BEGIN) $
                 String -> Get ()
forall (m :: * -> *) a. MonadFail m => String -> m a
fail "Header begin marker not found"
            EventTypeDescLen
hetm <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get Marker
            Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (EventTypeDescLen
hetm EventTypeDescLen -> EventTypeDescLen -> Bool
forall a. Eq a => a -> a -> Bool
/= EVENT_HET_BEGIN) $
                 String -> Get ()
forall (m :: * -> *) a. MonadFail m => String -> m a
fail "Header Event Type begin marker not found"
            [EventType]
ets <- Get [EventType]
getEventTypes
            EventTypeDescLen
emark <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get Marker
            Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (EventTypeDescLen
emark EventTypeDescLen -> EventTypeDescLen -> Bool
forall a. Eq a => a -> a -> Bool
/= EVENT_HEADER_END) $
                 String -> Get ()
forall (m :: * -> *) a. MonadFail m => String -> m a
fail "Header end marker not found"
            EventTypeDescLen
db <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get Marker
            Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (EventTypeDescLen
db EventTypeDescLen -> EventTypeDescLen -> Bool
forall a. Eq a => a -> a -> Bool
/= EVENT_DATA_BEGIN) $
                  String -> Get ()
forall (m :: * -> *) a. MonadFail m => String -> m a
fail "My Data begin marker not found"
            Header -> Get Header
forall (m :: * -> *) a. Monad m => a -> m a
return (Header -> Get Header) -> Header -> Get Header
forall a b. (a -> b) -> a -> b
$ [EventType] -> Header
Header [EventType]
ets
     where
      getEventTypes :: Get [EventType]
      getEventTypes :: Get [EventType]
getEventTypes = do
          EventTypeDescLen
m <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get Marker
          case EventTypeDescLen
m of
             EVENT_ET_BEGIN -> do
                  et <- getEventType
                  nextET <- getEventTypes
                  return (et : nextET)
             EVENT_HET_END ->
                  return []
             _ ->
                  String -> Get [EventType]
forall (m :: * -> *) a. MonadFail m => String -> m a
fail "Malformed list of Event Types in header"

getEvent :: EventParsers -> Get (Maybe Event)
getEvent :: EventParsers -> Get (Maybe Event)
getEvent (EventParsers parsers :: Array Int (Get EventInfo)
parsers) = do
  EventTypeNum
etRef <- Get EventTypeNum
forall t. Binary t => Get t
get :: Get EventTypeNum
  if EventTypeNum
etRef EventTypeNum -> EventTypeNum -> Bool
forall a. Eq a => a -> a -> Bool
== EVENT_DATA_END
     then Maybe Event -> Get (Maybe Event)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Event
forall a. Maybe a
Nothing
     else do !Timestamp
evTime   <- Get Timestamp
forall t. Binary t => Get t
get
             EventInfo
evSpec <- Array Int (Get EventInfo)
parsers Array Int (Get EventInfo) -> Int -> Get EventInfo
forall i e. Ix i => Array i e -> i -> e
! EventTypeNum -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral EventTypeNum
etRef
             Maybe Event -> Get (Maybe Event)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Event -> Get (Maybe Event))
-> Maybe Event -> Get (Maybe Event)
forall a b. (a -> b) -> a -> b
$ Event -> Maybe Event
forall a. a -> Maybe a
Just $WEvent :: Timestamp -> EventInfo -> Maybe Int -> Event
Event { evCap :: Maybe Int
evCap = Maybe Int
forall a. HasCallStack => a
undefined, .. }

--
-- standardEventParsers.
--
standardParsers :: [EventParser EventInfo]
standardParsers :: [EventParser EventInfo]
standardParsers = [
 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_STARTUP sz_cap (do -- (n_caps)
      c <- get :: Get CapNo
      return Startup{ n_caps = fromIntegral c }
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_BLOCK_MARKER (sz_block_size + sz_time + sz_cap) (do -- (size, end_time, cap)
      block_size <- get :: Get BlockSize
      end_time <- get :: Get Timestamp
      c <- get :: Get CapNo
      return EventBlock { end_time   = end_time,
                          cap        = fromIntegral c,
                          block_size = ((fromIntegral block_size) -
                                        (fromIntegral sz_block_event))
                        }
   )),

 -- EVENT_SHUTDOWN is replaced by EVENT_CAP_DELETE and GHC 7.6+
 -- no longer generate the event; should be removed at some point
 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_SHUTDOWN Shutdown),

 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_REQUEST_SEQ_GC RequestSeqGC),

 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_REQUEST_PAR_GC RequestParGC),

 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_GC_START StartGC),

 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_GC_WORK GCWork),

 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_GC_IDLE GCIdle),

 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_GC_DONE GCDone),

 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_GC_END EndGC),

 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_GC_GLOBAL_SYNC GlobalSyncGC),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_GC_STATS_GHC (sz_capset + 2 + 5*8 + 4) (do  -- (heap_capset, generation, copied_bytes, slop_bytes, frag_bytes, par_n_threads, par_max_copied, par_tot_copied)
      EventTypeDescLen
heapCapset   <- Get EventTypeDescLen
forall t. Binary t => Get t
get
      EventTypeNum
gen          <- Get EventTypeNum
forall t. Binary t => Get t
get :: Get Word16
      Timestamp
copied       <- Get Timestamp
forall t. Binary t => Get t
get :: Get Word64
      Timestamp
slop         <- Get Timestamp
forall t. Binary t => Get t
get :: Get Word64
      Timestamp
frag         <- Get Timestamp
forall t. Binary t => Get t
get :: Get Word64
      EventTypeDescLen
parNThreads  <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get Word32
      Timestamp
parMaxCopied <- Get Timestamp
forall t. Binary t => Get t
get :: Get Word64
      Timestamp
parTotCopied <- Get Timestamp
forall t. Binary t => Get t
get :: Get Word64
      EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WGCStatsGHC :: EventTypeDescLen
-> Int
-> Timestamp
-> Timestamp
-> Timestamp
-> Int
-> Timestamp
-> Timestamp
-> Maybe Timestamp
-> EventInfo
GCStatsGHC{ gen :: Int
gen = EventTypeNum -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral EventTypeNum
gen
                       , parNThreads :: Int
parNThreads = EventTypeDescLen -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral EventTypeDescLen
parNThreads
                       , parBalancedCopied :: Maybe Timestamp
parBalancedCopied = Maybe Timestamp
forall a. Maybe a
Nothing
                       , ..}
 )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_GC_STATS_GHC (sz_capset + 2 + 5*8 + 4 + 8) (do  -- (heap_capset, generation, copied_bytes, slop_bytes, frag_bytes, par_n_threads, par_max_copied, par_tot_copied, par_balanced_copied)
      EventTypeDescLen
heapCapset   <- Get EventTypeDescLen
forall t. Binary t => Get t
get
      EventTypeNum
gen          <- Get EventTypeNum
forall t. Binary t => Get t
get :: Get Word16
      Timestamp
copied       <- Get Timestamp
forall t. Binary t => Get t
get :: Get Word64
      Timestamp
slop         <- Get Timestamp
forall t. Binary t => Get t
get :: Get Word64
      Timestamp
frag         <- Get Timestamp
forall t. Binary t => Get t
get :: Get Word64
      EventTypeDescLen
parNThreads  <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get Word32
      Timestamp
parMaxCopied <- Get Timestamp
forall t. Binary t => Get t
get :: Get Word64
      Timestamp
parTotCopied <- Get Timestamp
forall t. Binary t => Get t
get :: Get Word64
      Timestamp
parBalancedCopied <- Get Timestamp
forall t. Binary t => Get t
get :: Get Word64
      EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WGCStatsGHC :: EventTypeDescLen
-> Int
-> Timestamp
-> Timestamp
-> Timestamp
-> Int
-> Timestamp
-> Timestamp
-> Maybe Timestamp
-> EventInfo
GCStatsGHC{ gen :: Int
gen = EventTypeNum -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral EventTypeNum
gen
                       , parNThreads :: Int
parNThreads = EventTypeDescLen -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral EventTypeDescLen
parNThreads
                       , parBalancedCopied :: Maybe Timestamp
parBalancedCopied = Timestamp -> Maybe Timestamp
forall a. a -> Maybe a
Just Timestamp
parBalancedCopied
                       , ..}
 )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_HEAP_ALLOCATED (sz_capset + 8) (do  -- (heap_capset, alloc_bytes)
      heapCapset <- get
      allocBytes <- get
      return HeapAllocated{..}
 )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_HEAP_SIZE (sz_capset + 8) (do  -- (heap_capset, size_bytes)
      heapCapset <- get
      sizeBytes  <- get
      return HeapSize{..}
 )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_HEAP_LIVE (sz_capset + 8) (do  -- (heap_capset, live_bytes)
      heapCapset <- get
      liveBytes  <- get
      return HeapLive{..}
 )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_HEAP_INFO_GHC (sz_capset + 2 + 4*8) (do  -- (heap_capset, n_generations, max_heap_size, alloc_area_size, mblock_size, block_size)
      heapCapset    <- get
      gens          <- get :: Get Word16
      maxHeapSize   <- get :: Get Word64
      allocAreaSize <- get :: Get Word64
      mblockSize    <- get :: Get Word64
      blockSize     <- get :: Get Word64
      return HeapInfoGHC{gens = fromIntegral gens, ..}
 )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_CAP_CREATE (sz_cap) (do  -- (cap)
      cap <- get :: Get CapNo
      return CapCreate{cap = fromIntegral cap}
 )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_CAP_DELETE (sz_cap) (do  -- (cap)
      cap <- get :: Get CapNo
      return CapDelete{cap = fromIntegral cap}
 )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_CAP_DISABLE (sz_cap) (do  -- (cap)
      cap <- get :: Get CapNo
      return CapDisable{cap = fromIntegral cap}
 )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_CAP_ENABLE (sz_cap) (do  -- (cap)
      cap <- get :: Get CapNo
      return CapEnable{cap = fromIntegral cap}
 )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_CAPSET_CREATE (sz_capset + sz_capset_type) (do -- (capset, capset_type)
      EventTypeDescLen
cs <- Get EventTypeDescLen
forall t. Binary t => Get t
get
      CapsetType
ct <- (EventTypeNum -> CapsetType) -> Get EventTypeNum -> Get CapsetType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap EventTypeNum -> CapsetType
mkCapsetType Get EventTypeNum
forall t. Binary t => Get t
get
      EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WCapsetCreate :: EventTypeDescLen -> CapsetType -> EventInfo
CapsetCreate{capset :: EventTypeDescLen
capset=EventTypeDescLen
cs,capsetType :: CapsetType
capsetType=CapsetType
ct}
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_CAPSET_DELETE sz_capset (do -- (capset)
      cs <- get
      return CapsetDelete{capset=cs}
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_CAPSET_ASSIGN_CAP (sz_capset + sz_cap) (do -- (capset, cap)
      EventTypeDescLen
cs <- Get EventTypeDescLen
forall t. Binary t => Get t
get
      EventTypeNum
cp <- Get EventTypeNum
forall t. Binary t => Get t
get :: Get CapNo
      EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WCapsetAssignCap :: EventTypeDescLen -> Int -> EventInfo
CapsetAssignCap{capset :: EventTypeDescLen
capset=EventTypeDescLen
cs,cap :: Int
cap=EventTypeNum -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral EventTypeNum
cp}
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_CAPSET_REMOVE_CAP (sz_capset + sz_cap) (do -- (capset, cap)
      EventTypeDescLen
cs <- Get EventTypeDescLen
forall t. Binary t => Get t
get
      EventTypeNum
cp <- Get EventTypeNum
forall t. Binary t => Get t
get :: Get CapNo
      EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WCapsetRemoveCap :: EventTypeDescLen -> Int -> EventInfo
CapsetRemoveCap{capset :: EventTypeDescLen
capset=EventTypeDescLen
cs,cap :: Int
cap=EventTypeNum -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral EventTypeNum
cp}
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_OSPROCESS_PID (sz_capset + sz_pid) (do -- (capset, pid)
      cs <- get
      pd <- get
      return OsProcessPid{capset=cs,pid=pd}
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_OSPROCESS_PPID (sz_capset + sz_pid) (do -- (capset, ppid)
      cs <- get
      pd <- get
      return OsProcessParentPid{capset=cs,ppid=pd}
  )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_WALL_CLOCK_TIME (sz_capset + 8 + 4) (do -- (capset, unix_epoch_seconds, nanoseconds)
      cs <- get
      s  <- get
      ns <- get
      return WallClockTime{capset=cs,sec=s,nsec=ns}
  )),

 (Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_LOG_MSG (do -- (msg)
      num <- get :: Get Word16
      string <- getText num
      return Message{ msg = string }
   )),
 (Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_USER_MSG (do -- (msg)
      num <- get :: Get Word16
      string <- getText num
      return UserMessage{ msg = string }
   )),
    (Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_USER_MARKER (do -- (markername)
      num <- get :: Get Word16
      string <- getText num
      return UserMarker{ markername = string }
   )),
 (Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_PROGRAM_ARGS (do -- (capset, [arg])
      num <- get :: Get Word16
      cs <- get
      string <- getText (num - sz_capset)
      return ProgramArgs
        { capset = cs
        , args = T.splitOn "\0" $ T.dropWhileEnd (== '\0') string }
   )),
 (Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_PROGRAM_ENV (do -- (capset, [arg])
      num <- get :: Get Word16
      cs <- get
      string <- getText (num - sz_capset)
      return ProgramEnv
        { capset = cs
        , env = T.splitOn "\0" $ T.dropWhileEnd (== '\0') string }
   )),
 (Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_RTS_IDENTIFIER (do -- (capset, str)
      num <- get :: Get Word16
      cs <- get
      string <- getText (num - sz_capset)
      return RtsIdentifier{ capset = cs
                          , rtsident = string }
   )),

 (Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_INTERN_STRING (do -- (str, id)
      num <- get :: Get Word16
      string <- getString (num - sz_string_id)
      sId <- get :: Get StringId
      return (InternString string sId)
    )),

 (Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_THREAD_LABEL (do -- (thread, str)
      num <- get :: Get Word16
      tid <- get
      str <- getText (num - sz_tid)
      return ThreadLabel{ thread      = tid
                        , threadlabel = str }
    ))
 ]

-- Parsers valid for GHC7 but not GHC6.
ghc7Parsers :: [EventParser EventInfo]
ghc7Parsers :: [EventParser EventInfo]
ghc7Parsers = [
 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_CREATE_THREAD sz_tid (do  -- (thread)
      t <- get
      return CreateThread{thread=t}
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_RUN_THREAD sz_tid (do  --  (thread)
      t <- get
      return RunThread{thread=t}
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_THREAD_RUNNABLE sz_tid (do  -- (thread)
      t <- get
      return ThreadRunnable{thread=t}
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_MIGRATE_THREAD (sz_tid + sz_cap) (do  --  (thread, newCap)
      EventTypeDescLen
t  <- Get EventTypeDescLen
forall t. Binary t => Get t
get
      EventTypeNum
nc <- Get EventTypeNum
forall t. Binary t => Get t
get :: Get CapNo
      EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WMigrateThread :: EventTypeDescLen -> Int -> EventInfo
MigrateThread{thread :: EventTypeDescLen
thread=EventTypeDescLen
t,newCap :: Int
newCap=EventTypeNum -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral EventTypeNum
nc}
   )),

 -- Yes, EVENT_RUN/STEAL_SPARK are deprecated, but see the explanation in the
 -- 'ghc6Parsers' section below. Since we're parsing them anyway, we might
 -- as well convert them to the new SparkRun/SparkSteal events.
 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_RUN_SPARK sz_tid (do  -- (thread)
      _ <- get :: Get ThreadId
      return SparkRun
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_STEAL_SPARK (sz_tid + sz_cap) (do  -- (thread, victimCap)
      _  <- get :: Get ThreadId
      vc <- get :: Get CapNo
      return SparkSteal{victimCap=fromIntegral vc}
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_CREATE_SPARK_THREAD sz_tid (do  -- (sparkThread)
      st <- get :: Get ThreadId
      return CreateSparkThread{sparkThread=st}
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_SPARK_COUNTERS (7*8) (do -- (crt,dud,ovf,cnv,gcd,fiz,rem)
      crt <- get :: Get Word64
      dud <- get :: Get Word64
      ovf <- get :: Get Word64
      cnv <- get :: Get Word64
      gcd <- get :: Get Word64
      fiz <- get :: Get Word64
      rem <- get :: Get Word64
      return SparkCounters{sparksCreated    = crt, sparksDud       = dud,
                           sparksOverflowed = ovf, sparksConverted = cnv,
                           -- Warning: order of fiz and gcd reversed!
                           sparksFizzled    = fiz, sparksGCd       = gcd,
                           sparksRemaining  = rem}
   )),

 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_SPARK_CREATE   SparkCreate),
 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_SPARK_DUD      SparkDud),
 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_SPARK_OVERFLOW SparkOverflow),
 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_SPARK_RUN      SparkRun),
 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_SPARK_STEAL sz_cap (do  -- (victimCap)
      vc <- get :: Get CapNo
      return SparkSteal{victimCap=fromIntegral vc}
   )),
 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_SPARK_FIZZLE   SparkFizzle),
 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_SPARK_GC       SparkGC),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_TASK_CREATE (sz_taskid + sz_cap + sz_kernel_tid) (do  -- (taskID, cap, tid)
      taskId <- get :: Get TaskId
      cap    <- get :: Get CapNo
      tid    <- get :: Get KernelThreadId
      return TaskCreate{ taskId, cap = fromIntegral cap, tid }
   )),
 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_TASK_MIGRATE (sz_taskid + sz_cap*2) (do  -- (taskID, cap, new_cap)
      taskId  <- get :: Get TaskId
      cap     <- get :: Get CapNo
      new_cap <- get :: Get CapNo
      return TaskMigrate{ taskId, cap = fromIntegral cap
                                , new_cap = fromIntegral new_cap
                        }
   )),
 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_TASK_DELETE (sz_taskid) (do  -- (taskID)
      taskId <- get :: Get TaskId
      return TaskDelete{ taskId }
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_THREAD_WAKEUP (sz_tid + sz_cap) (do  -- (thread, other_cap)
      EventTypeDescLen
t <- Get EventTypeDescLen
forall t. Binary t => Get t
get
      EventTypeNum
oc <- Get EventTypeNum
forall t. Binary t => Get t
get :: Get CapNo
      EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WWakeupThread :: EventTypeDescLen -> Int -> EventInfo
WakeupThread{thread :: EventTypeDescLen
thread=EventTypeDescLen
t,otherCap :: Int
otherCap=EventTypeNum -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral EventTypeNum
oc}
   ))
 ]

-- special thread stop event parsers for GHC version 7.8.2
-- see [Stop status in GHC-7.8.2] in EventTypes.hs
ghc782StopParser :: EventParser EventInfo
ghc782StopParser :: EventParser EventInfo
ghc782StopParser =
 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_STOP_THREAD (sz_tid + sz_th_stop_status + sz_tid) (do
      -- (thread, status, info)
      t <- get
      s <- get :: Get RawThreadStopStatus
      i <- get :: Get ThreadId
      return StopThread{thread = t,
                        status = case () of
                                  _ | s > maxThreadStopStatus782
                                    -> NoStatus
                                    | s == 9 {- XXX yeuch -}
                                      -- GHC-7.8.2: 9 == BlockedOnBlackHole
                                    -> BlockedOnBlackHoleOwnedBy i
                                    | otherwise
                                    -> mkStopStatus782 s}
   ))

-- parsers for GHC < 7.8.2. Older versions do not use block info
-- (different length).  See [Stop status in GHC-7.8.2] in
-- EventTypes.hs
pre77StopParsers :: [EventParser EventInfo]
pre77StopParsers :: [EventParser EventInfo]
pre77StopParsers = [
 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_STOP_THREAD (sz_tid + sz_th_stop_status) (do
      -- (thread, status)
      t <- get
      s <- get :: Get RawThreadStopStatus
      return StopThread{thread=t, status = if s > maxThreadStopStatusPre77
                                              then NoStatus
                                              else mkStopStatus s}
                        -- older version of the event, no block info
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_STOP_THREAD (sz_tid + sz_th_stop_status + sz_tid)
    (do
      -- (thread, status, info)
      EventTypeDescLen
t <- Get EventTypeDescLen
forall t. Binary t => Get t
get
      EventTypeNum
s <- Get EventTypeNum
forall t. Binary t => Get t
get :: Get RawThreadStopStatus
      EventTypeDescLen
i <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get ThreadId
      EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WStopThread :: EventTypeDescLen -> ThreadStopStatus -> EventInfo
StopThread{thread :: EventTypeDescLen
thread = EventTypeDescLen
t,
                        status :: ThreadStopStatus
status = case () of
                                  _ | EventTypeNum
s EventTypeNum -> EventTypeNum -> Bool
forall a. Ord a => a -> a -> Bool
> EventTypeNum
maxThreadStopStatusPre77
                                    -> ThreadStopStatus
NoStatus
                                    | EventTypeNum
s EventTypeNum -> EventTypeNum -> Bool
forall a. Eq a => a -> a -> Bool
== 8 {- XXX yeuch -}
                                      -- pre-7.7: 8==BlockedOnBlackhole
                                    -> EventTypeDescLen -> ThreadStopStatus
BlockedOnBlackHoleOwnedBy EventTypeDescLen
i
                                    | Bool
otherwise
                                    -> EventTypeNum -> ThreadStopStatus
mkStopStatus EventTypeNum
s}
    ))
  ]

-- parsers for GHC >= 7.8.3, always using block info field parser.
-- See [Stop status in GHC-7.8.2] in EventTypes.hs
post782StopParser :: EventParser EventInfo
post782StopParser :: EventParser EventInfo
post782StopParser =
 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_STOP_THREAD (sz_tid + sz_th_stop_status + sz_tid)
    (do
      -- (thread, status, info)
      EventTypeDescLen
t <- Get EventTypeDescLen
forall t. Binary t => Get t
get
      EventTypeNum
s <- Get EventTypeNum
forall t. Binary t => Get t
get :: Get RawThreadStopStatus
      EventTypeDescLen
i <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get ThreadId
      EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WStopThread :: EventTypeDescLen -> ThreadStopStatus -> EventInfo
StopThread{thread :: EventTypeDescLen
thread = EventTypeDescLen
t,
                        status :: ThreadStopStatus
status = case () of
                                  _ | EventTypeNum
s EventTypeNum -> EventTypeNum -> Bool
forall a. Ord a => a -> a -> Bool
> EventTypeNum
maxThreadStopStatus
                                    -> ThreadStopStatus
NoStatus
                                    | EventTypeNum
s EventTypeNum -> EventTypeNum -> Bool
forall a. Eq a => a -> a -> Bool
== 8 {- XXX yeuch -}
                                      -- post-7.8.2: 8==BlockedOnBlackhole
                                    -> EventTypeDescLen -> ThreadStopStatus
BlockedOnBlackHoleOwnedBy EventTypeDescLen
i
                                    | Bool
otherwise
                                    -> EventTypeNum -> ThreadStopStatus
mkStopStatus EventTypeNum
s}
    ))

 -----------------------
 -- GHC 6.12 compat: GHC 6.12 reported the wrong sizes for some events,
 -- so we have to recognise those wrong sizes here for backwards
 -- compatibility.
ghc6Parsers :: [EventParser EventInfo]
ghc6Parsers :: [EventParser EventInfo]
ghc6Parsers = [
 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_STARTUP 0 (do
      -- BUG in GHC 6.12: the startup event was incorrectly
      -- declared as size 0, so we accept it here.
      c <- get :: Get CapNo
      return Startup{ n_caps = fromIntegral c }
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_CREATE_THREAD sz_old_tid (do  -- (thread)
      t <- get
      return CreateThread{thread=t}
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_RUN_THREAD sz_old_tid (do  --  (thread)
      t <- get
      return RunThread{thread=t}
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_STOP_THREAD (sz_old_tid + 2) (do  -- (thread, status)
      t <- get
      s <- get :: Get RawThreadStopStatus
      return StopThread{thread=t, status = if s > maxThreadStopStatusPre77
                                              then NoStatus
                                              else mkStopStatus s}
                        -- older version of the event uses pre-77 encoding
                        -- (actually, it only uses encodings 0 to 5)
                        -- see [Stop status in GHC-7.8.2] in EventTypes.hs
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_THREAD_RUNNABLE sz_old_tid (do  -- (thread)
      t <- get
      return ThreadRunnable{thread=t}
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_MIGRATE_THREAD (sz_old_tid + sz_cap) (do  --  (thread, newCap)
      t  <- get
      nc <- get :: Get CapNo
      return MigrateThread{thread=t,newCap=fromIntegral nc}
   )),

 -- Note: it is vital that these two (EVENT_RUN/STEAL_SPARK) remain here (at
 -- least in the ghc6Parsers section) even though both events are deprecated.
 -- The reason is that .eventlog files created by the buggy GHC-6.12
 -- mis-declare the size of these two events. So we have to handle them
 -- specially here otherwise we'll get the wrong size, leading to us getting
 -- out of sync and eventual parse failure. Since we're parsing them anyway,
 -- we might as well convert them to the new SparkRun/SparkSteal events.
 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_RUN_SPARK sz_old_tid (do  -- (thread)
      _ <- get :: Get ThreadId
      return SparkRun
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_STEAL_SPARK (sz_old_tid + sz_cap) (do  -- (thread, victimCap)
      _  <- get :: Get ThreadId
      vc <- get :: Get CapNo
      return SparkSteal{victimCap=fromIntegral vc}
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_CREATE_SPARK_THREAD sz_old_tid (do  -- (sparkThread)
      st <- get :: Get ThreadId
      return CreateSparkThread{sparkThread=st}
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_THREAD_WAKEUP (sz_old_tid + sz_cap) (do  -- (thread, other_cap)
      t <- get
      oc <- get :: Get CapNo
      return WakeupThread{thread=t,otherCap=fromIntegral oc}
   ))
 ]

-- Parsers for parallel events. Parameter is the thread_id size, to create
-- ghc6-parsers (using the wrong size) where necessary.
parRTSParsers :: EventTypeSize -> [EventParser EventInfo]
parRTSParsers :: EventTypeNum -> [EventParser EventInfo]
parRTSParsers sz_tid' :: EventTypeNum
sz_tid' = [
 (Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_VERSION (do -- (version)
      num <- get :: Get Word16
      string <- getString num
      return Version{ version = string }
   )),

 (Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_PROGRAM_INVOCATION (do -- (cmd. line)
      num <- get :: Get Word16
      string <- getString num
      return ProgramInvocation{ commandline = string }
   )),

 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_EDEN_START_RECEIVE EdenStartReceive),
 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_EDEN_END_RECEIVE   EdenEndReceive),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_CREATE_PROCESS sz_procid
    (do EventTypeDescLen
p <- Get EventTypeDescLen
forall t. Binary t => Get t
get
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WCreateProcess :: EventTypeDescLen -> EventInfo
CreateProcess{ process :: EventTypeDescLen
process = EventTypeDescLen
p })
 ),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_KILL_PROCESS sz_procid
    (do EventTypeDescLen
p <- Get EventTypeDescLen
forall t. Binary t => Get t
get
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WKillProcess :: EventTypeDescLen -> EventInfo
KillProcess{ process :: EventTypeDescLen
process = EventTypeDescLen
p })
 ),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_ASSIGN_THREAD_TO_PROCESS (sz_tid' + sz_procid)
    (do EventTypeDescLen
t <- Get EventTypeDescLen
forall t. Binary t => Get t
get
        EventTypeDescLen
p <- Get EventTypeDescLen
forall t. Binary t => Get t
get
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WAssignThreadToProcess :: EventTypeDescLen -> EventTypeDescLen -> EventInfo
AssignThreadToProcess { thread :: EventTypeDescLen
thread = EventTypeDescLen
t, process :: EventTypeDescLen
process = EventTypeDescLen
p })
 ),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_CREATE_MACHINE (sz_mid + sz_realtime)
    (do EventTypeNum
m <- Get EventTypeNum
forall t. Binary t => Get t
get
        Timestamp
t <- Get Timestamp
forall t. Binary t => Get t
get
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WCreateMachine :: EventTypeNum -> Timestamp -> EventInfo
CreateMachine { machine :: EventTypeNum
machine = EventTypeNum
m, realtime :: Timestamp
realtime = Timestamp
t })
 ),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_KILL_MACHINE sz_mid
    (do EventTypeNum
m <- Get EventTypeNum
forall t. Binary t => Get t
get :: Get MachineId
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WKillMachine :: EventTypeNum -> EventInfo
KillMachine { machine :: EventTypeNum
machine = EventTypeNum
m })
 ),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_SEND_MESSAGE
    (EventTypeNum
sz_msgtag EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
+ 2EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
*EventTypeNum
sz_procid EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
+ 2EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
*EventTypeNum
sz_tid' EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
+ EventTypeNum
sz_mid)
    (do RawMsgTag
tag <- Get RawMsgTag
forall t. Binary t => Get t
get :: Get RawMsgTag
        EventTypeDescLen
sP  <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get ProcessId
        EventTypeDescLen
sT  <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get ThreadId
        EventTypeNum
rM  <- Get EventTypeNum
forall t. Binary t => Get t
get :: Get MachineId
        EventTypeDescLen
rP  <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get ProcessId
        EventTypeDescLen
rIP <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get PortId
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WSendMessage :: MessageTag
-> EventTypeDescLen
-> EventTypeDescLen
-> EventTypeNum
-> EventTypeDescLen
-> EventTypeDescLen
-> EventInfo
SendMessage { mesTag :: MessageTag
mesTag = RawMsgTag -> MessageTag
toMsgTag RawMsgTag
tag,
                             senderProcess :: EventTypeDescLen
senderProcess = EventTypeDescLen
sP,
                             senderThread :: EventTypeDescLen
senderThread = EventTypeDescLen
sT,
                             receiverMachine :: EventTypeNum
receiverMachine = EventTypeNum
rM,
                             receiverProcess :: EventTypeDescLen
receiverProcess = EventTypeDescLen
rP,
                             receiverInport :: EventTypeDescLen
receiverInport = EventTypeDescLen
rIP
                           })
 ),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_RECEIVE_MESSAGE
    (EventTypeNum
sz_msgtag EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
+ 2EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
*EventTypeNum
sz_procid EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
+ 2EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
*EventTypeNum
sz_tid' EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
+ EventTypeNum
sz_mid EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
+ EventTypeNum
sz_mes)
    (do RawMsgTag
tag <- Get RawMsgTag
forall t. Binary t => Get t
get :: Get Word8
        EventTypeDescLen
rP  <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get ProcessId
        EventTypeDescLen
rIP <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get PortId
        EventTypeNum
sM  <- Get EventTypeNum
forall t. Binary t => Get t
get :: Get MachineId
        EventTypeDescLen
sP  <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get ProcessId
        EventTypeDescLen
sT  <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get ThreadId
        EventTypeDescLen
mS  <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get MessageSize
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return  $WReceiveMessage :: MessageTag
-> EventTypeDescLen
-> EventTypeDescLen
-> EventTypeNum
-> EventTypeDescLen
-> EventTypeDescLen
-> EventTypeDescLen
-> EventInfo
ReceiveMessage { mesTag :: MessageTag
mesTag = RawMsgTag -> MessageTag
toMsgTag RawMsgTag
tag,
                                 receiverProcess :: EventTypeDescLen
receiverProcess = EventTypeDescLen
rP,
                                 receiverInport :: EventTypeDescLen
receiverInport = EventTypeDescLen
rIP,
                                 senderMachine :: EventTypeNum
senderMachine = EventTypeNum
sM,
                                 senderProcess :: EventTypeDescLen
senderProcess = EventTypeDescLen
sP,
                                 senderThread :: EventTypeDescLen
senderThread= EventTypeDescLen
sT,
                                 messageSize :: EventTypeDescLen
messageSize = EventTypeDescLen
mS
                               })
 ),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_SEND_RECEIVE_LOCAL_MESSAGE
    (EventTypeNum
sz_msgtag EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
+ 2EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
*EventTypeNum
sz_procid EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
+ 2EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
*EventTypeNum
sz_tid')
    (do RawMsgTag
tag <- Get RawMsgTag
forall t. Binary t => Get t
get :: Get Word8
        EventTypeDescLen
sP  <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get ProcessId
        EventTypeDescLen
sT  <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get ThreadId
        EventTypeDescLen
rP  <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get ProcessId
        EventTypeDescLen
rIP <- Get EventTypeDescLen
forall t. Binary t => Get t
get :: Get PortId
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WSendReceiveLocalMessage :: MessageTag
-> EventTypeDescLen
-> EventTypeDescLen
-> EventTypeDescLen
-> EventTypeDescLen
-> EventInfo
SendReceiveLocalMessage { mesTag :: MessageTag
mesTag = RawMsgTag -> MessageTag
toMsgTag RawMsgTag
tag,
                                         senderProcess :: EventTypeDescLen
senderProcess = EventTypeDescLen
sP,
                                         senderThread :: EventTypeDescLen
senderThread = EventTypeDescLen
sT,
                                         receiverProcess :: EventTypeDescLen
receiverProcess = EventTypeDescLen
rP,
                                         receiverInport :: EventTypeDescLen
receiverInport = EventTypeDescLen
rIP
                                       })
 )]

mercuryParsers :: [EventParser EventInfo]
mercuryParsers :: [EventParser EventInfo]
mercuryParsers = [
 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_MER_START_PAR_CONJUNCTION
    (EventTypeNum
sz_par_conj_dyn_id EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
+ EventTypeNum
sz_par_conj_static_id)
    (do Timestamp
dyn_id <- Get Timestamp
forall t. Binary t => Get t
get
        EventTypeDescLen
static_id <- Get EventTypeDescLen
forall t. Binary t => Get t
get
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return (Timestamp -> EventTypeDescLen -> EventInfo
MerStartParConjunction Timestamp
dyn_id EventTypeDescLen
static_id))
 ),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_MER_STOP_PAR_CONJUNCTION sz_par_conj_dyn_id
    (do Timestamp
dyn_id <- Get Timestamp
forall t. Binary t => Get t
get
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return (Timestamp -> EventInfo
MerEndParConjunction Timestamp
dyn_id))
 ),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_MER_STOP_PAR_CONJUNCT sz_par_conj_dyn_id
    (do Timestamp
dyn_id <- Get Timestamp
forall t. Binary t => Get t
get
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return (Timestamp -> EventInfo
MerEndParConjunct Timestamp
dyn_id))
 ),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_MER_CREATE_SPARK (sz_par_conj_dyn_id + sz_spark_id)
    (do Timestamp
dyn_id <- Get Timestamp
forall t. Binary t => Get t
get
        EventTypeDescLen
spark_id <- Get EventTypeDescLen
forall t. Binary t => Get t
get
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return (Timestamp -> EventTypeDescLen -> EventInfo
MerCreateSpark Timestamp
dyn_id EventTypeDescLen
spark_id))
 ),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_MER_FUT_CREATE (sz_future_id + sz_string_id)
    (do Timestamp
future_id <- Get Timestamp
forall t. Binary t => Get t
get
        EventTypeDescLen
name_id <- Get EventTypeDescLen
forall t. Binary t => Get t
get
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return (Timestamp -> EventTypeDescLen -> EventInfo
MerFutureCreate Timestamp
future_id EventTypeDescLen
name_id))
 ),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_MER_FUT_WAIT_NOSUSPEND (sz_future_id)
    (do Timestamp
future_id <- Get Timestamp
forall t. Binary t => Get t
get
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return (Timestamp -> EventInfo
MerFutureWaitNosuspend Timestamp
future_id))
 ),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_MER_FUT_WAIT_SUSPENDED (sz_future_id)
    (do Timestamp
future_id <- Get Timestamp
forall t. Binary t => Get t
get
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return (Timestamp -> EventInfo
MerFutureWaitSuspended Timestamp
future_id))
 ),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_MER_FUT_SIGNAL (sz_future_id)
    (do Timestamp
future_id <- Get Timestamp
forall t. Binary t => Get t
get
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return (Timestamp -> EventInfo
MerFutureSignal Timestamp
future_id))
 ),

 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_MER_LOOKING_FOR_GLOBAL_CONTEXT MerLookingForGlobalThread),
 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_MER_WORK_STEALING MerWorkStealing),
 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_MER_LOOKING_FOR_LOCAL_SPARK MerLookingForLocalSpark),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_MER_RELEASE_CONTEXT sz_tid
    (do EventTypeDescLen
thread_id <- Get EventTypeDescLen
forall t. Binary t => Get t
get
        EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return (EventTypeDescLen -> EventInfo
MerReleaseThread EventTypeDescLen
thread_id))
 ),

 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_MER_ENGINE_SLEEPING MerCapSleeping),
 (Int -> EventInfo -> EventParser EventInfo
forall a. Int -> a -> EventParser a
simpleEvent EVENT_MER_CALLING_MAIN MerCallingMain)

 ]

perfParsers :: [EventParser EventInfo]
perfParsers :: [EventParser EventInfo]
perfParsers = [
 (Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_PERF_NAME (do -- (perf_num, name)
      num     <- get :: Get Word16
      perfNum <- get
      name    <- getText (num - sz_perf_num)
      return PerfName{perfNum, name}
   )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_PERF_COUNTER (sz_perf_num + sz_kernel_tid + 8) (do -- (perf_num, tid, period)
      perfNum <- get
      tid     <- get
      period  <- get
      return PerfCounter{perfNum, tid, period}
  )),

 (Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_PERF_TRACEPOINT (sz_perf_num + sz_kernel_tid) (do -- (perf_num, tid)
      EventTypeDescLen
perfNum <- Get EventTypeDescLen
forall t. Binary t => Get t
get
      KernelThreadId
tid     <- Get KernelThreadId
forall t. Binary t => Get t
get
      EventInfo -> Get EventInfo
forall (m :: * -> *) a. Monad m => a -> m a
return $WPerfTracepoint :: EventTypeDescLen -> KernelThreadId -> EventInfo
PerfTracepoint{EventTypeDescLen
perfNum :: EventTypeDescLen
perfNum :: EventTypeDescLen
perfNum, KernelThreadId
tid :: KernelThreadId
tid :: KernelThreadId
tid}
  ))
 ]

heapProfParsers :: [EventParser EventInfo]
heapProfParsers :: [EventParser EventInfo]
heapProfParsers =
  [ Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_HEAP_PROF_BEGIN $ do
    payloadLen <- get :: Get Word16
    heapProfId <- get
    heapProfSamplingPeriod <- get
    heapProfBreakdown <- get
    heapProfModuleFilter <- getTextNul
    heapProfClosureDescrFilter <- getTextNul
    heapProfTypeDescrFilter <- getTextNul
    heapProfCostCentreFilter <- getTextNul
    heapProfCostCentreStackFilter <- getTextNul
    heapProfRetainerFilter <- getTextNul
    heapProfBiographyFilter <- getTextNul
    assert
      (fromIntegral payloadLen == sum
        [ 1 -- heapProfId
        , 8 -- heapProfSamplingPeriod
        , 4 -- heapProfBreakdown
        , textByteLen heapProfModuleFilter
        , textByteLen heapProfClosureDescrFilter
        , textByteLen heapProfTypeDescrFilter
        , textByteLen heapProfCostCentreFilter
        , textByteLen heapProfCostCentreStackFilter
        , textByteLen heapProfRetainerFilter
        , textByteLen heapProfBiographyFilter
        ])
      (return ())
    return $! HeapProfBegin {..}
  , Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_HEAP_PROF_COST_CENTRE $ do
    payloadLen <- get :: Get Word16
    heapProfCostCentreId <- get
    heapProfLabel <- getTextNul
    heapProfModule <- getTextNul
    heapProfSrcLoc <- getTextNul
    heapProfFlags <- get
    assert
      (fromIntegral payloadLen == sum
        [ 4 -- heapProfCostCentreId
        , textByteLen heapProfLabel
        , textByteLen heapProfModule
        , textByteLen heapProfSrcLoc
        , 1 -- heapProfFlags
        ])
      (return ())
    return $! HeapProfCostCentre {..}
  , Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_HEAP_PROF_SAMPLE_BEGIN 8 $ do
    heapProfSampleEra <- get
    return $! HeapProfSampleBegin {..}
  , Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_HEAP_PROF_SAMPLE_END 8 $ do
    heapProfSampleEra <- get
    return $! HeapProfSampleEnd {..}
  , Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_HEAP_BIO_PROF_SAMPLE_BEGIN 16 $ do
    heapProfSampleEra <- get
    heapProfSampleTime <- get
    return $! HeapBioProfSampleBegin {..}
  , Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_HEAP_PROF_SAMPLE_COST_CENTRE $ do
    payloadLen <- get :: Get Word16
    heapProfId <- get
    heapProfResidency <- get
    heapProfStackDepth <- get
    heapProfStack <- VU.replicateM (fromIntegral heapProfStackDepth) get
    assert
      ((fromIntegral payloadLen :: Int) == sum
        [ 1 -- heapProfId
        , 8 -- heapProfResidency
        , 1 -- heapProfStackDepth
        , fromIntegral heapProfStackDepth * 4
        ])
      (return ())
    return $! HeapProfSampleCostCentre {..}
  , Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_HEAP_PROF_SAMPLE_STRING $ do
    payloadLen <- get :: Get Word16
    heapProfId <- get
    heapProfResidency <- get
    heapProfLabel <- getTextNul
    assert
      (fromIntegral payloadLen == sum
        [ 1 -- heapProfId
        , 8 -- heapProfResidency
        , textByteLen heapProfLabel
        ])
      (return ())
    return $! HeapProfSampleString {..}
  ]

timeProfParsers :: [EventParser EventInfo]
timeProfParsers :: [EventParser EventInfo]
timeProfParsers = [
  Int -> EventTypeNum -> Get EventInfo -> EventParser EventInfo
forall a. Int -> EventTypeNum -> Get a -> EventParser a
FixedSizeParser EVENT_PROF_BEGIN 8 $ do
    profTickInterval <- get
    return $! ProfBegin{..}
  , Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_PROF_SAMPLE_COST_CENTRE $ do
    payloadLen <- get :: Get Word16
    profCapset <- get
    profTicks <- get
    profStackDepth <- get
    profCcsStack <- VU.replicateM (fromIntegral profStackDepth) get
    assert
      ((fromIntegral payloadLen :: Int) == sum
        [ 4
        , 8 -- ticks
        , 1 -- stack depth
        , fromIntegral profStackDepth * 4
        ])
      (return ())
    return $! ProfSampleCostCentre {..} ]

binaryEventParsers :: [EventParser EventInfo]
binaryEventParsers :: [EventParser EventInfo]
binaryEventParsers =
  [ Int -> Get EventInfo -> EventParser EventInfo
forall a. Int -> Get a -> EventParser a
VariableSizeParser EVENT_USER_BINARY_MSG $ do
    payloadLen <- get :: Get Word16
    payload <- G.getByteString $ fromIntegral payloadLen
    return $! UserBinaryMessage { payload }
  ]

-- | String byte length in the eventlog format. It includes
-- 1 byte for NUL.
textByteLen :: T.Text -> Int
textByteLen :: Text -> Int
textByteLen = (Int -> Int -> Int
forall a. Num a => a -> a -> a
+1) (Int -> Int) -> (Text -> Int) -> Text -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int
B.length (ByteString -> Int) -> (Text -> ByteString) -> Text -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TE.encodeUtf8

-----------------------------------------------------------

putE :: Binary a => a -> PutM ()
putE :: a -> PutM ()
putE = a -> PutM ()
forall t. Binary t => t -> PutM ()
put

putType :: EventTypeNum -> PutM ()
putType :: EventTypeNum -> PutM ()
putType = EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE

putCap :: Int -> PutM ()
putCap :: Int -> PutM ()
putCap c :: Int
c = EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeNum
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
c :: CapNo)

putMarker :: Word32 -> PutM ()
putMarker :: EventTypeDescLen -> PutM ()
putMarker = EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE

putEventLog :: EventLog -> PutM ()
putEventLog :: EventLog -> PutM ()
putEventLog (EventLog hdr :: Header
hdr es :: Data
es) = do
    Header -> PutM ()
putHeader Header
hdr
    Data -> PutM ()
putData Data
es

putHeader :: Header -> PutM ()
putHeader :: Header -> PutM ()
putHeader (Header ets :: [EventType]
ets) = do
    EventTypeDescLen -> PutM ()
putMarker EVENT_HEADER_BEGIN
    EventTypeDescLen -> PutM ()
putMarker EVENT_HET_BEGIN
    (EventType -> PutM ()) -> [EventType] -> PutM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ EventType -> PutM ()
putEventType [EventType]
ets
    EventTypeDescLen -> PutM ()
putMarker EVENT_HET_END
    EventTypeDescLen -> PutM ()
putMarker EVENT_HEADER_END
 where
    putEventType :: EventType -> PutM ()
putEventType (EventType n :: EventTypeNum
n d :: Text
d msz :: Maybe EventTypeNum
msz) = do
        EventTypeDescLen -> PutM ()
putMarker EVENT_ET_BEGIN
        EventTypeNum -> PutM ()
putType EventTypeNum
n
        EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (EventTypeNum -> PutM ()) -> EventTypeNum -> PutM ()
forall a b. (a -> b) -> a -> b
$ EventTypeNum -> Maybe EventTypeNum -> EventTypeNum
forall a. a -> Maybe a -> a
fromMaybe 0xffff Maybe EventTypeNum
msz
        EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeDescLen
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> EventTypeDescLen) -> Int -> EventTypeDescLen
forall a b. (a -> b) -> a -> b
$ Text -> Int
T.length Text
d :: EventTypeDescLen)
        Text -> PutM ()
forall t. Binary t => t -> PutM ()
putE Text
d
        -- the event type header allows for extra data, which we don't use:
        EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE (0 :: Word32)
        EventTypeDescLen -> PutM ()
putMarker EVENT_ET_END

putData :: Data -> PutM ()
putData :: Data -> PutM ()
putData (Data es :: [Event]
es) = do
    EventTypeDescLen -> PutM ()
putMarker EVENT_DATA_BEGIN -- Word32
    (Event -> PutM ()) -> [Event] -> PutM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Event -> PutM ()
putEvent [Event]
es
    EventTypeNum -> PutM ()
putType EVENT_DATA_END -- Word16

eventTypeNum :: EventInfo -> EventTypeNum
eventTypeNum :: EventInfo -> EventTypeNum
eventTypeNum e :: EventInfo
e = case EventInfo
e of
    CreateThread {} -> EVENT_CREATE_THREAD
    RunThread {} -> EVENT_RUN_THREAD
    StopThread {} -> EVENT_STOP_THREAD
    ThreadRunnable {} -> EVENT_THREAD_RUNNABLE
    MigrateThread {} -> EVENT_MIGRATE_THREAD
    Shutdown {} -> EVENT_SHUTDOWN
    WakeupThread {} -> EVENT_THREAD_WAKEUP
    ThreadLabel {}  -> EVENT_THREAD_LABEL
    StartGC {} -> EVENT_GC_START
    EndGC {} -> EVENT_GC_END
    GlobalSyncGC {} -> EVENT_GC_GLOBAL_SYNC
    RequestSeqGC {} -> EVENT_REQUEST_SEQ_GC
    RequestParGC {} -> EVENT_REQUEST_PAR_GC
    CreateSparkThread {} -> EVENT_CREATE_SPARK_THREAD
    SparkCounters {} -> EVENT_SPARK_COUNTERS
    SparkCreate   {} -> EVENT_SPARK_CREATE
    SparkDud      {} -> EVENT_SPARK_DUD
    SparkOverflow {} -> EVENT_SPARK_OVERFLOW
    SparkRun      {} -> EVENT_SPARK_RUN
    SparkSteal    {} -> EVENT_SPARK_STEAL
    SparkFizzle   {} -> EVENT_SPARK_FIZZLE
    SparkGC       {} -> EVENT_SPARK_GC
    TaskCreate  {} -> EVENT_TASK_CREATE
    TaskMigrate {} -> EVENT_TASK_MIGRATE
    TaskDelete  {} -> EVENT_TASK_DELETE
    Message {} -> EVENT_LOG_MSG
    Startup {} -> EVENT_STARTUP
    EventBlock {} -> EVENT_BLOCK_MARKER
    UserMessage {} -> EVENT_USER_MSG
    UserMarker  {} -> EVENT_USER_MARKER
    GCIdle {} -> EVENT_GC_IDLE
    GCWork {} -> EVENT_GC_WORK
    GCDone {} -> EVENT_GC_DONE
    GCStatsGHC{} -> EVENT_GC_STATS_GHC
    HeapAllocated{} -> EVENT_HEAP_ALLOCATED
    HeapSize{} -> EVENT_HEAP_SIZE
    HeapLive{} -> EVENT_HEAP_LIVE
    HeapInfoGHC{} -> EVENT_HEAP_INFO_GHC
    CapCreate{} -> EVENT_CAP_CREATE
    CapDelete{} -> EVENT_CAP_DELETE
    CapDisable{} -> EVENT_CAP_DISABLE
    CapEnable{} -> EVENT_CAP_ENABLE
    CapsetCreate {} -> EVENT_CAPSET_CREATE
    CapsetDelete {} -> EVENT_CAPSET_DELETE
    CapsetAssignCap {} -> EVENT_CAPSET_ASSIGN_CAP
    CapsetRemoveCap {} -> EVENT_CAPSET_REMOVE_CAP
    RtsIdentifier {} -> EVENT_RTS_IDENTIFIER
    ProgramArgs {} -> EVENT_PROGRAM_ARGS
    ProgramEnv {} -> EVENT_PROGRAM_ENV
    OsProcessPid {} -> EVENT_OSPROCESS_PID
    OsProcessParentPid{} -> EVENT_OSPROCESS_PPID
    WallClockTime{} -> EVENT_WALL_CLOCK_TIME
    UnknownEvent {} -> String -> EventTypeNum
forall a. HasCallStack => String -> a
error "eventTypeNum UnknownEvent"
    InternString {} -> EVENT_INTERN_STRING
    Version {} -> EVENT_VERSION
    ProgramInvocation {} -> EVENT_PROGRAM_INVOCATION
    EdenStartReceive {} -> EVENT_EDEN_START_RECEIVE
    EdenEndReceive {} -> EVENT_EDEN_END_RECEIVE
    CreateProcess {} -> EVENT_CREATE_PROCESS
    KillProcess {} -> EVENT_KILL_PROCESS
    AssignThreadToProcess {} -> EVENT_ASSIGN_THREAD_TO_PROCESS
    CreateMachine {} -> EVENT_CREATE_MACHINE
    KillMachine {} -> EVENT_KILL_MACHINE
    SendMessage {} -> EVENT_SEND_MESSAGE
    ReceiveMessage {} -> EVENT_RECEIVE_MESSAGE
    SendReceiveLocalMessage {} -> EVENT_SEND_RECEIVE_LOCAL_MESSAGE
    MerStartParConjunction {} -> EVENT_MER_START_PAR_CONJUNCTION
    MerEndParConjunction _ -> EVENT_MER_STOP_PAR_CONJUNCTION
    MerEndParConjunct _ -> EVENT_MER_STOP_PAR_CONJUNCT
    MerCreateSpark {} -> EVENT_MER_CREATE_SPARK
    MerFutureCreate {} -> EVENT_MER_FUT_CREATE
    MerFutureWaitNosuspend _ -> EVENT_MER_FUT_WAIT_NOSUSPEND
    MerFutureWaitSuspended _ -> EVENT_MER_FUT_WAIT_SUSPENDED
    MerFutureSignal _ -> EVENT_MER_FUT_SIGNAL
    MerLookingForGlobalThread -> EVENT_MER_LOOKING_FOR_GLOBAL_CONTEXT
    MerWorkStealing -> EVENT_MER_WORK_STEALING
    MerLookingForLocalSpark -> EVENT_MER_LOOKING_FOR_LOCAL_SPARK
    MerReleaseThread _ -> EVENT_MER_RELEASE_CONTEXT
    MerCapSleeping -> EVENT_MER_ENGINE_SLEEPING
    MerCallingMain -> EVENT_MER_CALLING_MAIN
    PerfName       {} -> EventTypeNum
nEVENT_PERF_NAME
    PerfCounter    {} -> EventTypeNum
nEVENT_PERF_COUNTER
    PerfTracepoint {} -> EventTypeNum
nEVENT_PERF_TRACEPOINT
    HeapProfBegin {} -> EVENT_HEAP_PROF_BEGIN
    HeapProfCostCentre {} -> EVENT_HEAP_PROF_COST_CENTRE
    HeapProfSampleBegin {} -> EVENT_HEAP_PROF_SAMPLE_BEGIN
    HeapProfSampleEnd {} -> EVENT_HEAP_PROF_SAMPLE_END
    HeapBioProfSampleBegin {} -> EVENT_HEAP_BIO_PROF_SAMPLE_BEGIN
    HeapProfSampleCostCentre {} -> EVENT_HEAP_PROF_SAMPLE_COST_CENTRE
    HeapProfSampleString {} -> EVENT_HEAP_PROF_SAMPLE_STRING
    ProfSampleCostCentre {} -> EVENT_PROF_SAMPLE_COST_CENTRE
    ProfBegin {}            -> EVENT_PROF_BEGIN
    UserBinaryMessage {} -> EVENT_USER_BINARY_MSG

nEVENT_PERF_NAME, nEVENT_PERF_COUNTER, nEVENT_PERF_TRACEPOINT :: EventTypeNum
nEVENT_PERF_NAME :: EventTypeNum
nEVENT_PERF_NAME = EVENT_PERF_NAME
nEVENT_PERF_COUNTER :: EventTypeNum
nEVENT_PERF_COUNTER = EVENT_PERF_COUNTER
nEVENT_PERF_TRACEPOINT :: EventTypeNum
nEVENT_PERF_TRACEPOINT = EVENT_PERF_TRACEPOINT

putEvent :: Event -> PutM ()
putEvent :: Event -> PutM ()
putEvent Event {..} = do
    EventTypeNum -> PutM ()
putType (EventInfo -> EventTypeNum
eventTypeNum EventInfo
evSpec)
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
put Timestamp
evTime
    EventInfo -> PutM ()
putEventSpec EventInfo
evSpec

putEventSpec :: EventInfo -> PutM ()
putEventSpec :: EventInfo -> PutM ()
putEventSpec (Startup caps :: Int
caps) = do
    Int -> PutM ()
putCap (Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
caps)

putEventSpec (EventBlock end :: Timestamp
end cap :: Int
cap sz :: EventTypeDescLen
sz) = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE (EventTypeDescLen -> EventTypeDescLen
forall a b. (Integral a, Num b) => a -> b
fromIntegral (EventTypeDescLen
szEventTypeDescLen -> EventTypeDescLen -> EventTypeDescLen
forall a. Num a => a -> a -> a
+24) :: BlockSize)
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
end
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeNum
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
cap :: CapNo)

putEventSpec (CreateThread t :: EventTypeDescLen
t) =
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
t

putEventSpec (RunThread t :: EventTypeDescLen
t) =
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
t

-- here we assume that ThreadStopStatus fromEnum matches the definitions in
-- EventLogFormat.h
-- The standard encoding is used here, which is wrong for eventlogs
-- produced by GHC-7.8.2 ([Stop status in GHC-7.8.2] in EventTypes.hs
putEventSpec (StopThread t :: EventTypeDescLen
t s :: ThreadStopStatus
s) = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
t
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (EventTypeNum -> PutM ()) -> EventTypeNum -> PutM ()
forall a b. (a -> b) -> a -> b
$ case ThreadStopStatus
s of
            NoStatus -> 0 :: Word16
            HeapOverflow -> 1
            StackOverflow -> 2
            ThreadYielding -> 3
            ThreadBlocked -> 4
            ThreadFinished -> 5
            ForeignCall -> 6
            BlockedOnMVar -> 7
            BlockedOnMVarRead -> 20 -- since GHC-7.8.3
            BlockedOnBlackHole -> 8
            BlockedOnBlackHoleOwnedBy _ -> 8
            BlockedOnRead -> 9
            BlockedOnWrite -> 10
            BlockedOnDelay -> 11
            BlockedOnSTM -> 12
            BlockedOnDoProc -> 13
            BlockedOnCCall -> 14
            BlockedOnCCall_NoUnblockExc -> 15
            BlockedOnMsgThrowTo -> 16
            ThreadMigrating -> 17
            BlockedOnMsgGlobalise -> 18
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE (EventTypeDescLen -> PutM ()) -> EventTypeDescLen -> PutM ()
forall a b. (a -> b) -> a -> b
$ case ThreadStopStatus
s of
            BlockedOnBlackHoleOwnedBy i :: EventTypeDescLen
i -> EventTypeDescLen
i
            _                           -> 0

putEventSpec (ThreadRunnable t :: EventTypeDescLen
t) =
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
t

putEventSpec (MigrateThread t :: EventTypeDescLen
t c :: Int
c) = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
t
    Int -> PutM ()
putCap Int
c

putEventSpec (CreateSparkThread t :: EventTypeDescLen
t) =
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
t

putEventSpec (SparkCounters crt :: Timestamp
crt dud :: Timestamp
dud ovf :: Timestamp
ovf cnv :: Timestamp
cnv fiz :: Timestamp
fiz gcd :: Timestamp
gcd rem :: Timestamp
rem) = do
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
crt
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
dud
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
ovf
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
cnv
    -- Warning: order of fiz and gcd reversed!
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
gcd
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
fiz
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
rem

putEventSpec SparkCreate =
    () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec SparkDud =
    () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec SparkOverflow =
    () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec SparkRun =
    () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec (SparkSteal c :: Int
c) =
    Int -> PutM ()
putCap Int
c

putEventSpec SparkFizzle =
    () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec SparkGC =
    () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec (WakeupThread t :: EventTypeDescLen
t c :: Int
c) = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
t
    Int -> PutM ()
putCap Int
c

putEventSpec (ThreadLabel t :: EventTypeDescLen
t l :: Text
l) = do
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeNum
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Text -> Int
T.length Text
l) EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
+ EventTypeNum
sz_tid :: Word16)
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
t
    Text -> PutM ()
forall t. Binary t => t -> PutM ()
putE Text
l

putEventSpec Shutdown =
    () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec RequestSeqGC =
    () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec RequestParGC =
    () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec StartGC =
    () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec GCWork =
    () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec GCIdle =
    () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec GCDone =
    () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec EndGC =
    () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec GlobalSyncGC =
    () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec (TaskCreate taskId :: Timestamp
taskId cap :: Int
cap tid :: KernelThreadId
tid) = do
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
taskId
    Int -> PutM ()
putCap Int
cap
    KernelThreadId -> PutM ()
forall t. Binary t => t -> PutM ()
putE KernelThreadId
tid

putEventSpec (TaskMigrate taskId :: Timestamp
taskId cap :: Int
cap new_cap :: Int
new_cap) = do
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
taskId
    Int -> PutM ()
putCap Int
cap
    Int -> PutM ()
putCap Int
new_cap

putEventSpec (TaskDelete taskId :: Timestamp
taskId) =
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
taskId

putEventSpec GCStatsGHC{..} = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
heapCapset
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeNum
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
gen :: Word16)
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
copied
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
slop
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
frag
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeDescLen
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
parNThreads :: Word32)
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
parMaxCopied
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
parTotCopied
    case Maybe Timestamp
parBalancedCopied of
      Nothing -> () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      Just v :: Timestamp
v  -> Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
v

putEventSpec HeapAllocated{..} = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
heapCapset
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
allocBytes

putEventSpec HeapSize{..} = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
heapCapset
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
sizeBytes

putEventSpec HeapLive{..} = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
heapCapset
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
liveBytes

putEventSpec HeapInfoGHC{..} = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
heapCapset
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeNum
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
gens :: Word16)
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
maxHeapSize
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
allocAreaSize
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
mblockSize
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
blockSize

putEventSpec CapCreate{Int
cap :: Int
cap :: EventInfo -> Int
cap} =
    Int -> PutM ()
putCap Int
cap

putEventSpec CapDelete{Int
cap :: Int
cap :: EventInfo -> Int
cap} =
    Int -> PutM ()
putCap Int
cap

putEventSpec CapDisable{Int
cap :: Int
cap :: EventInfo -> Int
cap} =
    Int -> PutM ()
putCap Int
cap

putEventSpec CapEnable{Int
cap :: Int
cap :: EventInfo -> Int
cap} =
    Int -> PutM ()
putCap Int
cap

putEventSpec (CapsetCreate cs :: EventTypeDescLen
cs ct :: CapsetType
ct) = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
cs
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (EventTypeNum -> PutM ()) -> EventTypeNum -> PutM ()
forall a b. (a -> b) -> a -> b
$ case CapsetType
ct of
            CapsetCustom -> 1 :: Word16
            CapsetOsProcess -> 2
            CapsetClockDomain -> 3
            CapsetUnknown -> 0

putEventSpec (CapsetDelete cs :: EventTypeDescLen
cs) =
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
cs

putEventSpec (CapsetAssignCap cs :: EventTypeDescLen
cs cp :: Int
cp) = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
cs
    Int -> PutM ()
putCap Int
cp

putEventSpec (CapsetRemoveCap cs :: EventTypeDescLen
cs cp :: Int
cp) = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
cs
    Int -> PutM ()
putCap Int
cp

putEventSpec (RtsIdentifier cs :: EventTypeDescLen
cs rts :: Text
rts) = do
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeNum
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Text -> Int
T.length Text
rts) EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
+ EventTypeNum
sz_capset :: Word16)
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
cs
    Text -> PutM ()
forall t. Binary t => t -> PutM ()
putE Text
rts

putEventSpec (ProgramArgs cs :: EventTypeDescLen
cs as :: [Text]
as) = do
    let sz_args :: Int
sz_args = [Int] -> Int
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ (Text -> Int) -> [Text] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ((Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1) {- for \0 -} (Int -> Int) -> (Text -> Int) -> Text -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Int
T.length) [Text]
as
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeNum
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
sz_args EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
+ EventTypeNum
sz_capset :: Word16)
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
cs
    (Text -> PutM ()) -> [Text] -> PutM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Text -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
intersperse "\0" [Text]
as)

putEventSpec (ProgramEnv cs :: EventTypeDescLen
cs es :: [Text]
es) = do
    let sz_env :: Int
sz_env = [Int] -> Int
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ (Text -> Int) -> [Text] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ((Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1) {- for \0 -} (Int -> Int) -> (Text -> Int) -> Text -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Int
T.length) [Text]
es
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeNum
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
sz_env EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
+ EventTypeNum
sz_capset :: Word16)
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
cs
    (Text -> PutM ()) -> [Text] -> PutM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Text -> PutM ()
forall t. Binary t => t -> PutM ()
putE ([Text] -> PutM ()) -> [Text] -> PutM ()
forall a b. (a -> b) -> a -> b
$ Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
intersperse "\0" [Text]
es

putEventSpec (OsProcessPid cs :: EventTypeDescLen
cs pid :: EventTypeDescLen
pid) = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
cs
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
pid

putEventSpec (OsProcessParentPid cs :: EventTypeDescLen
cs ppid :: EventTypeDescLen
ppid) = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
cs
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
ppid

putEventSpec (WallClockTime cs :: EventTypeDescLen
cs sec :: Timestamp
sec nsec :: EventTypeDescLen
nsec) = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
cs
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
sec
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
nsec

putEventSpec (Message s :: Text
s) = do
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeNum
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Text -> Int
T.length Text
s) :: Word16)
    Text -> PutM ()
forall t. Binary t => t -> PutM ()
putE Text
s

putEventSpec (UserMessage s :: Text
s) = do
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeNum
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Text -> Int
T.length Text
s) :: Word16)
    Text -> PutM ()
forall t. Binary t => t -> PutM ()
putE Text
s

putEventSpec (UserMarker s :: Text
s) = do
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeNum
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Text -> Int
T.length Text
s) :: Word16)
    Text -> PutM ()
forall t. Binary t => t -> PutM ()
putE Text
s

putEventSpec (UnknownEvent {}) = String -> PutM ()
forall a. HasCallStack => String -> a
error "putEventSpec UnknownEvent"

putEventSpec (InternString str :: String
str id :: EventTypeDescLen
id) = do
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeNum
len
    (Char -> PutM ()) -> String -> PutM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Char -> PutM ()
forall t. Binary t => t -> PutM ()
putE String
str
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
id
  where len :: EventTypeNum
len = (Int -> EventTypeNum
forall a b. (Integral a, Num b) => a -> b
fromIntegral (String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
str) :: Word16) EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
+ EventTypeNum
sz_string_id

putEventSpec (Version s :: String
s) = do
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeNum
forall a b. (Integral a, Num b) => a -> b
fromIntegral (String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s) :: Word16)
    (Char -> PutM ()) -> String -> PutM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Char -> PutM ()
forall t. Binary t => t -> PutM ()
putE String
s

putEventSpec (ProgramInvocation s :: String
s) = do
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeNum
forall a b. (Integral a, Num b) => a -> b
fromIntegral (String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s) :: Word16)
    (Char -> PutM ()) -> String -> PutM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Char -> PutM ()
forall t. Binary t => t -> PutM ()
putE String
s

putEventSpec ( EventInfo
EdenStartReceive ) = () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec ( EventInfo
EdenEndReceive ) = () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec ( CreateProcess  process :: EventTypeDescLen
process ) = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
process

putEventSpec ( KillProcess process :: EventTypeDescLen
process ) = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
process

putEventSpec ( AssignThreadToProcess thread :: EventTypeDescLen
thread process :: EventTypeDescLen
process ) = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
thread
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
process

putEventSpec ( CreateMachine machine :: EventTypeNum
machine realtime :: Timestamp
realtime ) = do
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeNum
machine
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
realtime

putEventSpec ( KillMachine machine :: EventTypeNum
machine ) = do
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeNum
machine

putEventSpec ( SendMessage mesTag :: MessageTag
mesTag senderProcess :: EventTypeDescLen
senderProcess senderThread :: EventTypeDescLen
senderThread
                 receiverMachine :: EventTypeNum
receiverMachine receiverProcess :: EventTypeDescLen
receiverProcess receiverInport :: EventTypeDescLen
receiverInport ) = do
    RawMsgTag -> PutM ()
forall t. Binary t => t -> PutM ()
putE (MessageTag -> RawMsgTag
fromMsgTag MessageTag
mesTag)
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
senderProcess
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
senderThread
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeNum
receiverMachine
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
receiverProcess
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
receiverInport

putEventSpec ( ReceiveMessage mesTag :: MessageTag
mesTag receiverProcess :: EventTypeDescLen
receiverProcess receiverInport :: EventTypeDescLen
receiverInport
                 senderMachine :: EventTypeNum
senderMachine senderProcess :: EventTypeDescLen
senderProcess senderThread :: EventTypeDescLen
senderThread messageSize :: EventTypeDescLen
messageSize ) = do
    RawMsgTag -> PutM ()
forall t. Binary t => t -> PutM ()
putE (MessageTag -> RawMsgTag
fromMsgTag MessageTag
mesTag)
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
receiverProcess
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
receiverInport
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeNum
senderMachine
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
senderProcess
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
senderThread
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
messageSize

putEventSpec ( SendReceiveLocalMessage mesTag :: MessageTag
mesTag senderProcess :: EventTypeDescLen
senderProcess senderThread :: EventTypeDescLen
senderThread
                 receiverProcess :: EventTypeDescLen
receiverProcess receiverInport :: EventTypeDescLen
receiverInport ) = do
    RawMsgTag -> PutM ()
forall t. Binary t => t -> PutM ()
putE (MessageTag -> RawMsgTag
fromMsgTag MessageTag
mesTag)
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
senderProcess
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
senderThread
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
receiverProcess
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
receiverInport

putEventSpec (MerStartParConjunction dyn_id :: Timestamp
dyn_id static_id :: EventTypeDescLen
static_id) = do
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
dyn_id
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
static_id

putEventSpec (MerEndParConjunction dyn_id :: Timestamp
dyn_id) =
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
dyn_id

putEventSpec (MerEndParConjunct dyn_id :: Timestamp
dyn_id) =
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
dyn_id

putEventSpec (MerCreateSpark dyn_id :: Timestamp
dyn_id spark_id :: EventTypeDescLen
spark_id) = do
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
dyn_id
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
spark_id

putEventSpec (MerFutureCreate future_id :: Timestamp
future_id name_id :: EventTypeDescLen
name_id) = do
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
future_id
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
name_id

putEventSpec (MerFutureWaitNosuspend future_id :: Timestamp
future_id) =
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
future_id

putEventSpec (MerFutureWaitSuspended future_id :: Timestamp
future_id) =
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
future_id

putEventSpec (MerFutureSignal future_id :: Timestamp
future_id) =
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
future_id

putEventSpec MerLookingForGlobalThread = () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
putEventSpec MerWorkStealing = () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
putEventSpec MerLookingForLocalSpark = () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec (MerReleaseThread thread_id :: EventTypeDescLen
thread_id) =
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
thread_id

putEventSpec MerCapSleeping = () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
putEventSpec MerCallingMain = () -> PutM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

putEventSpec PerfName{..} = do
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeNum
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Text -> Int
T.length Text
name) EventTypeNum -> EventTypeNum -> EventTypeNum
forall a. Num a => a -> a -> a
+ EventTypeNum
sz_perf_num :: Word16)
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
perfNum
    Text -> PutM ()
forall t. Binary t => t -> PutM ()
putE Text
name

putEventSpec PerfCounter{..} = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
perfNum
    KernelThreadId -> PutM ()
forall t. Binary t => t -> PutM ()
putE KernelThreadId
tid
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
period

putEventSpec PerfTracepoint{..} = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
perfNum
    KernelThreadId -> PutM ()
forall t. Binary t => t -> PutM ()
putE KernelThreadId
tid

putEventSpec HeapProfBegin {..} = do
    RawMsgTag -> PutM ()
forall t. Binary t => t -> PutM ()
putE RawMsgTag
heapProfId
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
heapProfSamplingPeriod
    HeapProfBreakdown -> PutM ()
forall t. Binary t => t -> PutM ()
putE HeapProfBreakdown
heapProfBreakdown
    (Text -> PutM ()) -> [Text] -> PutM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (String -> PutM ()
forall t. Binary t => t -> PutM ()
putE (String -> PutM ()) -> (Text -> String) -> Text -> PutM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack)
      [ Text
heapProfModuleFilter
      , Text
heapProfClosureDescrFilter
      , Text
heapProfTypeDescrFilter
      , Text
heapProfCostCentreFilter
      , Text
heapProfCostCentreStackFilter
      , Text
heapProfRetainerFilter
      , Text
heapProfBiographyFilter
      ]

putEventSpec HeapProfCostCentre {..} = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
heapProfCostCentreId
    String -> PutM ()
forall t. Binary t => t -> PutM ()
putE (String -> PutM ()) -> String -> PutM ()
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
heapProfLabel
    String -> PutM ()
forall t. Binary t => t -> PutM ()
putE (String -> PutM ()) -> String -> PutM ()
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
heapProfModule
    String -> PutM ()
forall t. Binary t => t -> PutM ()
putE (String -> PutM ()) -> String -> PutM ()
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
heapProfSrcLoc
    HeapProfFlags -> PutM ()
forall t. Binary t => t -> PutM ()
putE HeapProfFlags
heapProfFlags

putEventSpec HeapProfSampleBegin {..} =
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
heapProfSampleEra

putEventSpec HeapProfSampleEnd {..} =
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
heapProfSampleEra

putEventSpec HeapBioProfSampleBegin {..} = do
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
heapProfSampleEra
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
heapProfSampleTime


putEventSpec HeapProfSampleCostCentre {..} = do
    RawMsgTag -> PutM ()
forall t. Binary t => t -> PutM ()
putE RawMsgTag
heapProfId
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
heapProfResidency
    RawMsgTag -> PutM ()
forall t. Binary t => t -> PutM ()
putE RawMsgTag
heapProfStackDepth
    (EventTypeDescLen -> PutM ()) -> Vector EventTypeDescLen -> PutM ()
forall (m :: * -> *) a b.
(Monad m, Unbox a) =>
(a -> m b) -> Vector a -> m ()
VU.mapM_ EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE Vector EventTypeDescLen
heapProfStack

putEventSpec HeapProfSampleString {..} = do
    RawMsgTag -> PutM ()
forall t. Binary t => t -> PutM ()
putE RawMsgTag
heapProfId
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
heapProfResidency
    String -> PutM ()
forall t. Binary t => t -> PutM ()
putE (String -> PutM ()) -> String -> PutM ()
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
heapProfLabel

putEventSpec ProfSampleCostCentre {..} = do
    EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE EventTypeDescLen
profCapset
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
profTicks
    RawMsgTag -> PutM ()
forall t. Binary t => t -> PutM ()
putE RawMsgTag
profStackDepth
    (EventTypeDescLen -> PutM ()) -> Vector EventTypeDescLen -> PutM ()
forall (m :: * -> *) a b.
(Monad m, Unbox a) =>
(a -> m b) -> Vector a -> m ()
VU.mapM_ EventTypeDescLen -> PutM ()
forall t. Binary t => t -> PutM ()
putE Vector EventTypeDescLen
profCcsStack

putEventSpec ProfBegin {..} = do
    Timestamp -> PutM ()
forall t. Binary t => t -> PutM ()
putE Timestamp
profTickInterval

putEventSpec UserBinaryMessage {..} = do
    EventTypeNum -> PutM ()
forall t. Binary t => t -> PutM ()
putE (Int -> EventTypeNum
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
payload) :: Word16)
    ByteString -> PutM ()
putByteString ByteString
payload