summaryrefslogtreecommitdiff
path: root/testsuite/tests/rts/T17574.hs
blob: 2af8d156b125caac6a2a6d5f8013fc9bb0d2517f (plain)
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
-- | Check that large objects are properly accounted for by GHC.Stats
module Main (main) where

import Control.Monad
import Control.Exception
import Control.Concurrent
import System.Mem
import System.Exit
import GHC.Stats
import GHC.Compact
import Data.List (replicate)

import qualified Data.ByteString.Char8 as BS

doGC :: IO ()
doGC = do
  performMajorGC
  threadDelay 1000 -- small delay to allow GC to run when using concurrent gc

main :: IO ()
main = do
  let size = 4096*2
  largeString <- evaluate $ BS.replicate size 'A'
  compactString <- compact $ replicate size 'A'
  doGC
  doGC -- run GC twice to make sure the objects end up in the oldest gen
  stats <- getRTSStats
  let large_obj_bytes = gcdetails_large_objects_bytes $ gc stats
  let compact_obj_bytes = gcdetails_compact_bytes $ gc stats
  -- assert that large_obj_bytes is at least as big as size
  -- this indicates that `largeString` is being accounted for by the stats department
  when (large_obj_bytes < fromIntegral size) $ do
    putStrLn $ "large_obj_bytes is: " <> show large_obj_bytes <> " but expected at least: " <> show size
    exitFailure
  when (compact_obj_bytes < fromIntegral size) $ do
    putStrLn $ "compact_obj_bytes is: " <> show large_obj_bytes <> " but expected at least: " <> show size
    exitFailure
  -- keep them alive
  print $ BS.length largeString
  print $ length $ getCompact compactString