4
4
{-# LANGUAGE TypeApplications #-}
5
5
6
6
module Cardano.BM.Counters.Windows
7
- (
8
- readCounters
7
+ ( readCounters
8
+ , readResourceStats
9
9
) where
10
10
11
11
#ifdef ENABLE_OBSERVABLES
@@ -16,12 +16,14 @@ import Foreign.C.Types
16
16
import Foreign.Marshal.Alloc
17
17
import Foreign.Ptr
18
18
import Foreign.Storable
19
+ import qualified GHC.Stats as GhcStats
19
20
import System.Win32.Process (ProcessId , getCurrentProcessId )
20
21
import System.Win32.Types
21
22
22
23
import Cardano.BM.Counters.Common (getMonoClock , readRTSStats )
23
24
import Cardano.BM.Data.Observable
24
25
import Cardano.BM.Data.Aggregated (Measurable (.. ))
26
+ import Cardano.BM.Stats.Resources
25
27
#endif
26
28
import Cardano.BM.Data.Counter
27
29
import Cardano.BM.Data.SubTrace
@@ -121,7 +123,7 @@ foreign import ccall unsafe c_get_io_counters :: Ptr IOCounters -> CInt -> IO CI
121
123
122
124
123
125
{- system information -}
124
- {-
126
+ {-
125
127
typedef struct _SYSTEM_INFO {
126
128
union {
127
129
DWORD dwOemId; // Obsolete field...do not use
@@ -231,7 +233,7 @@ readCounters (ObservableTrace _ _) = return []
231
233
#ifdef ENABLE_OBSERVABLES
232
234
readProcMem :: ProcessId -> IO [Counter ]
233
235
readProcMem pid = do
234
- meminfo <- getMemoryInfo
236
+ meminfo <- getMemoryInfo pid
235
237
return [ Counter MemoryCounter " Pid" (PureI $ fromIntegral pid)
236
238
, Counter MemoryCounter " WorkingSetSize" (PureI $ fromIntegral (_workingSetSize meminfo))
237
239
, Counter MemoryCounter " PeakWorkingSetSize" (PureI $ fromIntegral (_peakWorkingSetSize meminfo))
@@ -243,25 +245,25 @@ readProcMem pid = do
243
245
, Counter MemoryCounter " PeakPagefileUsage" (PureI $ fromIntegral (_peakPagefileUsage meminfo))
244
246
, Counter MemoryCounter " PageFaultCount" (PureI $ fromIntegral (_pageFaultCount meminfo))
245
247
]
246
- where
247
- getMemoryInfo :: IO ProcessMemoryCounters
248
- getMemoryInfo =
249
- allocaBytes 128 $ \ ptr -> do
250
- res <- c_get_process_memory_info ptr (fromIntegral pid)
251
- if res <= 0
252
- then do
253
- putStrLn $ " c_get_process_memory_info: failure returned: " ++ (show res)
254
- return $ ProcessMemoryCounters 0 0 0 0 0 0 0 0 0 0
255
- else
256
- peek ptr
248
+
249
+ getMemoryInfo :: ProcessId -> IO ProcessMemoryCounters
250
+ getMemoryInfo pid =
251
+ allocaBytes 128 $ \ ptr -> do
252
+ res <- c_get_process_memory_info ptr (fromIntegral pid)
253
+ if res <= 0
254
+ then do
255
+ putStrLn $ " c_get_process_memory_info: failure returned: " ++ (show res)
256
+ return $ ProcessMemoryCounters 0 0 0 0 0 0 0 0 0 0
257
+ else
258
+ peek ptr
257
259
#endif
258
260
259
261
260
262
#ifdef ENABLE_OBSERVABLES
261
263
readSysStats :: ProcessId -> IO [Counter ]
262
264
readSysStats pid = do
263
265
sysinfo <- getSysInfo
264
- cputimes <- getCpuTimes
266
+ cputimes <- getCpuTimes pid
265
267
winbits <- getWinBits
266
268
return [ Counter SysInfo " Pid" (PureI $ fromIntegral pid)
267
269
, Counter SysInfo " Platform" (PureI $ fromIntegral $ fromEnum Windows )
@@ -275,7 +277,7 @@ readSysStats pid = do
275
277
, Counter SysInfo " KernelTime" (Microseconds $ systime cputimes)
276
278
, Counter SysInfo " CPUTime" (Microseconds $ (systime cputimes + usertime cputimes))
277
279
, Counter SysInfo " IdleTime" (Microseconds $ idletime cputimes)
278
- , Counter SysInfo " WindowsPlatformBits" (PureI $ fromIntegral winbits)
280
+ , Counter SysInfo " WindowsPlatformBits" (PureI $ fromIntegral winbits)
279
281
]
280
282
where
281
283
getWinBits :: IO CInt
@@ -305,6 +307,30 @@ readSysStats pid = do
305
307
306
308
307
309
#ifdef ENABLE_OBSERVABLES
310
+ readResourceStats :: IO (Maybe ResourceStats )
311
+ readResourceStats = getCurrentProcessId >>= \ pid -> do
312
+ cpu <- getCpuTimes pid
313
+ mem <- getMemoryInfo pid
314
+ rts <- GhcStats. getRTSStats
315
+ pure . Just $
316
+ Resources
317
+ { rCentiCpu = usecsToCenti $ usertime cpu + systime cpu
318
+ , rCentiGC = nsToCenti $ GhcStats. gc_cpu_ns rts
319
+ , rCentiMut = nsToCenti $ GhcStats. mutator_cpu_ns rts
320
+ , rGcsMajor = fromIntegral $ GhcStats. major_gcs rts
321
+ , rGcsMinor = fromIntegral $ GhcStats. gcs rts - GhcStats. major_gcs rts
322
+ , rAlloc = GhcStats. allocated_bytes rts
323
+ , rLive = GhcStats. gcdetails_live_bytes $ GhcStats. gc rts
324
+ , rRSS = fromIntegral (_workingSetSize mem)
325
+ , rCentiBlkIO = 0
326
+ , rThreads = 0
327
+ }
328
+ where
329
+ usecsToCenti :: ULONGLONG -> Word64
330
+ usecsToCenti = ceiling . (/ 10000 )
331
+ nsToCenti :: GhcStats. RtsTime -> Word64
332
+ nsToCenti = fromIntegral . (/ 10000000 )
333
+
308
334
readProcStats :: ProcessId -> IO [Counter ]
309
335
readProcStats pid = do
310
336
cputimes <- getCpuTimes
@@ -314,17 +340,17 @@ readProcStats pid = do
314
340
, Counter StatInfo " StartTime" (Microseconds $ idletime cputimes)
315
341
, Counter StatInfo " CPUTime" (Microseconds $ (systime cputimes + usertime cputimes))
316
342
]
317
- where
318
- getCpuTimes :: IO CpuTimes
319
- getCpuTimes =
320
- allocaBytes 128 $ \ ptr -> do
321
- res <- c_get_proc_cpu_times ptr (fromIntegral pid)
322
- if res <= 0
323
- then do
324
- putStrLn $ " c_get_proc_cpu_times: failure returned: " ++ (show res)
325
- return $ CpuTimes 0 0 0
326
- else
327
- peek ptr
343
+
344
+ getCpuTimes :: ProcessId -> IO CpuTimes
345
+ getCpuTimes pid =
346
+ allocaBytes 128 $ \ ptr -> do
347
+ res <- c_get_proc_cpu_times ptr (fromIntegral pid)
348
+ if res <= 0
349
+ then do
350
+ putStrLn $ " c_get_proc_cpu_times: failure returned: " ++ (show res)
351
+ return $ CpuTimes 0 0 0
352
+ else
353
+ peek ptr
328
354
329
355
#endif
330
356
0 commit comments