Skip to content

Commit 11b06aa

Browse files
authored
Merge pull request #23 from RyanGlScott/master
Update to the new GHC.Stats API
2 parents 41dc023 + a5cd48c commit 11b06aa

File tree

1 file changed

+100
-36
lines changed

1 file changed

+100
-36
lines changed

System/Metrics.hs

Lines changed: 100 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,15 @@ createDistribution name store = do
331331
-- easily be added to a metrics store by calling their register
332332
-- function.
333333

334+
#if MIN_VERSION_base(4,10,0)
335+
-- | Convert nanoseconds to milliseconds.
336+
nsToMs :: Int64 -> Int64
337+
nsToMs s = round (realToFrac s / (1000000.0 :: Double))
338+
#else
334339
-- | Convert seconds to milliseconds.
335-
toMs :: Double -> Int64
336-
toMs s = round (s * 1000.0)
340+
sToMs :: Double -> Int64
341+
sToMs s = round (s * 1000.0)
342+
#endif
337343

338344
-- | Register a number of metrics related to garbage collector
339345
-- behavior.
@@ -410,18 +416,42 @@ toMs s = round (s * 1000.0)
410416
registerGcMetrics :: Store -> IO ()
411417
registerGcMetrics store =
412418
registerGroup
419+
#if MIN_VERSION_base(4,10,0)
420+
(M.fromList
421+
[ ("rts.gc.bytes_allocated" , Counter . fromIntegral . Stats.allocated_bytes)
422+
, ("rts.gc.num_gcs" , Counter . fromIntegral . Stats.gcs)
423+
, ("rts.gc.num_bytes_usage_samples" , Counter . fromIntegral . Stats.major_gcs)
424+
, ("rts.gc.cumulative_bytes_used" , Counter . fromIntegral . Stats.cumulative_live_bytes)
425+
, ("rts.gc.bytes_copied" , Counter . fromIntegral . Stats.copied_bytes)
426+
, ("rts.gc.mutator_cpu_ms" , Counter . nsToMs . Stats.mutator_cpu_ns)
427+
, ("rts.gc.mutator_wall_ms" , Counter . nsToMs . Stats.mutator_elapsed_ns)
428+
, ("rts.gc.gc_cpu_ms" , Counter . nsToMs . Stats.gc_cpu_ns)
429+
, ("rts.gc.gc_wall_ms" , Counter . nsToMs . Stats.gc_elapsed_ns)
430+
, ("rts.gc.cpu_ms" , Counter . nsToMs . Stats.cpu_ns)
431+
, ("rts.gc.wall_ms" , Counter . nsToMs . Stats.elapsed_ns)
432+
, ("rts.gc.max_bytes_used" , Gauge . fromIntegral . Stats.max_live_bytes)
433+
, ("rts.gc.current_bytes_used" , Gauge . fromIntegral . Stats.gcdetails_live_bytes . Stats.gc)
434+
, ("rts.gc.current_bytes_slop" , Gauge . fromIntegral . Stats.gcdetails_slop_bytes . Stats.gc)
435+
, ("rts.gc.max_bytes_slop" , Gauge . fromIntegral . Stats.max_slop_bytes)
436+
, ("rts.gc.peak_megabytes_allocated" , Gauge . fromIntegral . (`quot` (1024*1024)) . Stats.max_mem_in_use_bytes)
437+
, ("rts.gc.par_tot_bytes_copied" , Gauge . fromIntegral . Stats.par_copied_bytes)
438+
, ("rts.gc.par_avg_bytes_copied" , Gauge . fromIntegral . Stats.par_copied_bytes)
439+
, ("rts.gc.par_max_bytes_copied" , Gauge . fromIntegral . Stats.cumulative_par_max_copied_bytes)
440+
])
441+
getRTSStats
442+
#else
413443
(M.fromList
414444
[ ("rts.gc.bytes_allocated" , Counter . Stats.bytesAllocated)
415445
, ("rts.gc.num_gcs" , Counter . Stats.numGcs)
416446
, ("rts.gc.num_bytes_usage_samples" , Counter . Stats.numByteUsageSamples)
417447
, ("rts.gc.cumulative_bytes_used" , Counter . Stats.cumulativeBytesUsed)
418448
, ("rts.gc.bytes_copied" , Counter . Stats.bytesCopied)
419-
, ("rts.gc.mutator_cpu_ms" , Counter . toMs . Stats.mutatorCpuSeconds)
420-
, ("rts.gc.mutator_wall_ms" , Counter . toMs . Stats.mutatorWallSeconds)
421-
, ("rts.gc.gc_cpu_ms" , Counter . toMs . Stats.gcCpuSeconds)
422-
, ("rts.gc.gc_wall_ms" , Counter . toMs . Stats.gcWallSeconds)
423-
, ("rts.gc.cpu_ms" , Counter . toMs . Stats.cpuSeconds)
424-
, ("rts.gc.wall_ms" , Counter . toMs . Stats.wallSeconds)
449+
, ("rts.gc.mutator_cpu_ms" , Counter . sToMs . Stats.mutatorCpuSeconds)
450+
, ("rts.gc.mutator_wall_ms" , Counter . sToMs . Stats.mutatorWallSeconds)
451+
, ("rts.gc.gc_cpu_ms" , Counter . sToMs . Stats.gcCpuSeconds)
452+
, ("rts.gc.gc_wall_ms" , Counter . sToMs . Stats.gcWallSeconds)
453+
, ("rts.gc.cpu_ms" , Counter . sToMs . Stats.cpuSeconds)
454+
, ("rts.gc.wall_ms" , Counter . sToMs . Stats.wallSeconds)
425455
, ("rts.gc.max_bytes_used" , Gauge . Stats.maxBytesUsed)
426456
, ("rts.gc.current_bytes_used" , Gauge . Stats.currentBytesUsed)
427457
, ("rts.gc.current_bytes_slop" , Gauge . Stats.currentBytesSlop)
@@ -432,11 +462,68 @@ registerGcMetrics store =
432462
, ("rts.gc.par_max_bytes_copied" , Gauge . Stats.parMaxBytesCopied)
433463
])
434464
getGcStats
465+
#endif
435466
store
436467

468+
#if MIN_VERSION_base(4,10,0)
469+
-- | Get RTS statistics.
470+
getRTSStats :: IO Stats.RTSStats
471+
getRTSStats = do
472+
enabled <- Stats.getRTSStatsEnabled
473+
if enabled
474+
then Stats.getRTSStats
475+
else return emptyRTSStats
476+
477+
-- | Empty RTS statistics, as if the application hasn't started yet.
478+
emptyRTSStats :: Stats.RTSStats
479+
emptyRTSStats = Stats.RTSStats
480+
{ gcs = 0
481+
, major_gcs = 0
482+
, allocated_bytes = 0
483+
, max_live_bytes = 0
484+
, max_large_objects_bytes = 0
485+
, max_compact_bytes = 0
486+
, max_slop_bytes = 0
487+
, max_mem_in_use_bytes = 0
488+
, cumulative_live_bytes = 0
489+
, copied_bytes = 0
490+
, par_copied_bytes = 0
491+
, cumulative_par_max_copied_bytes = 0
492+
# if MIN_VERSION_base(4,11,0)
493+
, cumulative_par_balanced_copied_bytes = 0
494+
# endif
495+
, mutator_cpu_ns = 0
496+
, mutator_elapsed_ns = 0
497+
, gc_cpu_ns = 0
498+
, gc_elapsed_ns = 0
499+
, cpu_ns = 0
500+
, elapsed_ns = 0
501+
, gc = emptyGCDetails
502+
}
503+
504+
emptyGCDetails :: Stats.GCDetails
505+
emptyGCDetails = Stats.GCDetails
506+
{ gcdetails_gen = 0
507+
, gcdetails_threads = 0
508+
, gcdetails_allocated_bytes = 0
509+
, gcdetails_live_bytes = 0
510+
, gcdetails_large_objects_bytes = 0
511+
, gcdetails_compact_bytes = 0
512+
, gcdetails_slop_bytes = 0
513+
, gcdetails_mem_in_use_bytes = 0
514+
, gcdetails_copied_bytes = 0
515+
, gcdetails_par_max_copied_bytes = 0
516+
# if MIN_VERSION_base(4,11,0)
517+
, gcdetails_par_balanced_copied_bytes = 0
518+
# endif
519+
, gcdetails_sync_elapsed_ns = 0
520+
, gcdetails_cpu_ns = 0
521+
, gcdetails_elapsed_ns = 0
522+
}
523+
#else
437524
-- | Get GC statistics.
438525
getGcStats :: IO Stats.GCStats
439-
#if MIN_VERSION_base(4,6,0)
526+
# if MIN_VERSION_base(4,6,0)
440527
getGcStats = do
441528
enabled <- Stats.getGCStatsEnabled
442529
if enabled
@@ -445,7 +532,6 @@ getGcStats = do
445532

446533
-- | Empty GC statistics, as if the application hasn't started yet.
447534
emptyGCStats :: Stats.GCStats
448-
# if MIN_VERSION_base(4,10,0)
449535
emptyGCStats = Stats.GCStats
450536
{ bytesAllocated = 0
451537
, numGcs = 0
@@ -465,40 +551,18 @@ emptyGCStats = Stats.GCStats
465551
, wallSeconds = 0
466552
, parTotBytesCopied = 0
467553
, parMaxBytesCopied = 0
468-
, mblocksAllocated = 0
469554
}
470555
# else
471-
emptyGCStats = Stats.GCStats
472-
{ bytesAllocated = 0
473-
, numGcs = 0
474-
, maxBytesUsed = 0
475-
, numByteUsageSamples = 0
476-
, cumulativeBytesUsed = 0
477-
, bytesCopied = 0
478-
, currentBytesUsed = 0
479-
, currentBytesSlop = 0
480-
, maxBytesSlop = 0
481-
, peakMegabytesAllocated = 0
482-
, mutatorCpuSeconds = 0
483-
, mutatorWallSeconds = 0
484-
, gcCpuSeconds = 0
485-
, gcWallSeconds = 0
486-
, cpuSeconds = 0
487-
, wallSeconds = 0
488-
, parTotBytesCopied = 0
489-
, parMaxBytesCopied = 0
490-
}
491-
# endif
492-
#else
493556
getGcStats = Stats.getGCStats
494-
#endif
557+
# endif
495558

496559
-- | Helper to work around rename in GHC.Stats in base-4.6.
497560
gcParTotBytesCopied :: Stats.GCStats -> Int64
498-
#if MIN_VERSION_base(4,6,0)
561+
# if MIN_VERSION_base(4,6,0)
499562
gcParTotBytesCopied = Stats.parTotBytesCopied
500-
#else
563+
# else
501564
gcParTotBytesCopied = Stats.parAvgBytesCopied
565+
# endif
502566
#endif
503567

504568
------------------------------------------------------------------------

0 commit comments

Comments
 (0)