/
Resources.hs
126 lines (115 loc) · 5.28 KB
/
Resources.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Cardano.Tracer.Handlers.RTView.Update.Resources
( updateResourcesHistory
) where
import Control.Concurrent.STM.TVar (readTVarIO)
import qualified Data.Map.Strict as M
import Data.Text.Read (decimal)
import Data.Time.Clock (UTCTime)
import Data.Word (Word64)
import Cardano.Tracer.Handlers.Metrics.Utils
import Cardano.Tracer.Handlers.RTView.State.Historical
import Cardano.Tracer.Handlers.RTView.State.Last
import Cardano.Tracer.Handlers.RTView.Update.Utils
import Cardano.Tracer.Types
updateResourcesHistory
:: NodeId
-> ResourcesHistory
-> LastResources
-> MetricName
-> MetricValue
-> UTCTime
-> IO ()
updateResourcesHistory nodeId (ResHistory rHistory) lastResources metricName metricValue now =
case metricName of
"Resources.Stat.Cputicks" -> updateCPUUsage
"Resources.Mem.Resident" -> updateRSSMemory
"Resources.RTS.GcLiveBytes" -> updateGCLiveMemory
"Resources.RTS.GcMajorNum" -> updateGCMajorNum
"Resources.RTS.GcMinorNum" -> updateGCMinorNum
"Resources.RTS.Gcticks" -> updateCPUTimeGC
"Resources.RTS.Mutticks" -> updateCPUTimeApp
"Resources.RTS.Stat.Threads" -> updateThreadsNum
_ -> return ()
where
updateCPUUsage =
case decimal metricValue of
Left _ -> return ()
Right (cpuTicks :: Int, _) -> do
lastOnes <- readTVarIO lastResources
case M.lookup nodeId lastOnes of
Nothing ->
-- There is no last resources for this node yet.
addNullResources lastResources nodeId
Just resourcesForNode -> do
let tns = utc2ns now
tDiffInSec = max 0.1 $ fromIntegral (tns - cpuLastNS resourcesForNode) / 1000_000_000 :: Double
ticksDiff = cpuTicks - cpuLastTicks resourcesForNode
cpuV = fromIntegral ticksDiff / fromIntegral (100 :: Int) / tDiffInSec
!newCPUPct = if cpuV < 0 then 0.0 else cpuV * 100.0
addHistoricalData rHistory nodeId now CPUData $ ValueD newCPUPct
updateLastResources lastResources nodeId $ \current ->
current { cpuLastTicks = cpuTicks
, cpuLastNS = tns
}
updateRSSMemory =
case decimal metricValue of
Left _ -> return ()
Right (bytes :: Word64, _) -> do
let !memoryInMB = fromIntegral bytes / 1024 / 1024 :: Double
addHistoricalData rHistory nodeId now MemoryData $ ValueD memoryInMB
updateGCLiveMemory =
case decimal metricValue of
Left _ -> return ()
Right (bytes :: Word64, _) -> do
let !memoryInMB = fromIntegral bytes / 1024 / 1024 :: Double
addHistoricalData rHistory nodeId now GCLiveMemoryData $ ValueD memoryInMB
updateGCMajorNum =
readValueI metricValue $ addHistoricalData rHistory nodeId now GCMajorNumData
updateGCMinorNum =
readValueI metricValue $ addHistoricalData rHistory nodeId now GCMinorNumData
updateCPUTimeGC =
case decimal metricValue of
Left _ -> return ()
Right (cpuTimeGCInCentiS :: Int, _) -> do
lastOnes <- readTVarIO lastResources
case M.lookup nodeId lastOnes of
Nothing ->
-- There is no last resources for this node yet.
addNullResources lastResources nodeId
Just resourcesForNode -> do
let tns = utc2ns now
tDiffInSec = max 0.1 $ fromIntegral (tns - cpuGCLastNS resourcesForNode) / 1000_000_000 :: Double
ticksDiff = cpuTimeGCInCentiS - cpuGCLastTicks resourcesForNode
cpuV = fromIntegral ticksDiff / fromIntegral (100 :: Int) / tDiffInSec
!newCPUPct = if cpuV < 0 then 0.0 else cpuV * 100.0
addHistoricalData rHistory nodeId now CPUTimeGCData $ ValueD newCPUPct
updateLastResources lastResources nodeId $ \current ->
current { cpuGCLastTicks = cpuTimeGCInCentiS
, cpuGCLastNS = tns
}
updateCPUTimeApp =
case decimal metricValue of
Left _ -> return ()
Right (cpuTimeAppInCentiS :: Int, _) -> do
lastOnes <- readTVarIO lastResources
case M.lookup nodeId lastOnes of
Nothing ->
-- There is no last resources for this node yet.
addNullResources lastResources nodeId
Just resourcesForNode -> do
let tns = utc2ns now
tDiffInSec = max 0.1 $ fromIntegral (tns - cpuAppLastNS resourcesForNode) / 1000_000_000 :: Double
ticksDiff = cpuTimeAppInCentiS - cpuAppLastTicks resourcesForNode
cpuV = fromIntegral ticksDiff / fromIntegral (100 :: Int) / tDiffInSec
!newCPUPct = if cpuV < 0 then 0.0 else cpuV * 100.0
addHistoricalData rHistory nodeId now CPUTimeAppData $ ValueD newCPUPct
updateLastResources lastResources nodeId $ \current ->
current { cpuAppLastTicks = cpuTimeAppInCentiS
, cpuAppLastNS = tns
}
updateThreadsNum =
readValueI metricValue $ addHistoricalData rHistory nodeId now ThreadsNumData