Skip to content

Commit

Permalink
Add threadCPUTime based on threadCPUTime#
Browse files Browse the repository at this point in the history
  • Loading branch information
harendra-kumar committed Feb 28, 2022
1 parent d0c3682 commit 778ab89
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/Streamly/Metrics/Perf.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE UnboxedTuples #-}
module Streamly.Metrics.Perf
(
PerfMetrics(..)
Expand All @@ -8,25 +9,49 @@ module Streamly.Metrics.Perf
)
where

import Control.Monad (unless)
import Control.Monad (when, unless)
import Data.Maybe (catMaybes)
import GHC.Exts (threadCPUTime#)
import GHC.Int(Int64(..))
import GHC.IO(IO(..))
import GHC.Stats (getRTSStats, getRTSStatsEnabled, RTSStats(..))
import Streamly.Internal.Data.Time.Units (NanoSecond64, fromAbsTime)
import Streamly.Internal.Data.Time.TimeSpec (TimeSpec(..))
import Streamly.Internal.Data.Time.Units
(NanoSecond64, fromAbsTime, toRelTime, addToAbsTime)
import Streamly.Metrics.Measure (measureWith)
import Streamly.Metrics.Perf.Type (PerfMetrics(..), checkMonotony)
import Streamly.Metrics.Perf.RUsage (getRuMetrics, pattern RUsageSelf)
import Streamly.Metrics.Type (Seconds)
import Text.Show.Pretty (ppShow)

import qualified Streamly.Internal.Data.Time.Clock as Clock

threadCPUTime :: IO (Int64, Int64)
threadCPUTime = IO $ \s ->
case threadCPUTime# s of
(# s', sec, nsec, _ #) ->
(# s', (I64# sec, I64# nsec) #)

getThreadCPUTime :: IO (Seconds Double)
getThreadCPUTime = do
(sec, nsec) <- threadCPUTime
cur <- Clock.getTime Clock.ThreadCPUTime
let start = toRelTime $ TimeSpec sec nsec
delta = addToAbsTime cur start
t :: Double = fromIntegral (fromAbsTime delta :: NanoSecond64) * 1e-9
when (t <= 0)
$ error
$ "Thread CPU time is negative: delta = " ++ show delta
++ " cur = " ++ show cur ++ " start " ++ show start
return $ fromIntegral (fromAbsTime delta :: NanoSecond64) * 1e-9

{-# INLINE getProcMetrics #-}
getProcMetrics :: IO [PerfMetrics]
getProcMetrics = do
time <- Clock.getTime Clock.Monotonic
tcpu <- Clock.getTime Clock.ThreadCPUTime
tcpuSec <- getThreadCPUTime
pcpu <- Clock.getTime Clock.ProcessCPUTime

let tcpuSec = fromIntegral (fromAbsTime tcpu :: NanoSecond64) * 1e-9
let pcpuSec = fromIntegral (fromAbsTime pcpu :: NanoSecond64) * 1e-9
let timeSec = fromIntegral (fromAbsTime time :: NanoSecond64) * 1e-9
return
Expand Down

0 comments on commit 778ab89

Please sign in to comment.