Skip to content

Instantly share code, notes, and snippets.

@osa1
Last active August 29, 2015 14:14
Show Gist options
  • Save osa1/3b100e11e464f3cd7142 to your computer and use it in GitHub Desktop.
Save osa1/3b100e11e464f3cd7142 to your computer and use it in GitHub Desktop.
-- | Spawn N threads, each one adding new elements to sets randomly
-- selected from a pool of sets.
module Main where
import Control.Concurrent
import Control.DeepSeq
import Control.Monad
import Data.IORef
import Data.Maybe
import qualified Data.Set as S
import Options.Applicative
import System.Random
main = do
threads <- execParser cmdOpts
setPool <- replicateM threads $ newIORef (S.empty, 0)
replicateM threads $ forkIO (buildSet setPool threads >> return ())
threadDelay 10000000 -- run for 10 seconds
return ()
cmdOpts :: ParserInfo Int
cmdOpts = info (helper <*> argParser) mempty
argParser :: Parser Int
argParser = argument auto (help "Amount of threads to spawn" <> metavar "THREADS")
buildSet :: [IORef (S.Set Int, Int)] -> Int -> IO (S.Set Int, Int)
buildSet setPool len = do
setRef <- (setPool !!) <$> randomRIO (0, len - 1)
atomicModifyIORef' setRef $ \(set, lastElem) -> ((S.insert (lastElem + 1) set, lastElem + 1), ())
buildSet setPool len
./dist/dist-sandbox-e53dad53/build/random-parallel-snowball/random-parallel-snowball 1 +RTS -N -l -qa -srandom-parallel-snowball_1_qa.gc
15,880,207,640 bytes allocated in the heap
4,084,408,656 bytes copied during GC
992,608,224 bytes maximum residency (13 sample(s))
15,831,072 bytes maximum slop
1958 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 30757 colls, 30757 par 19.118s 2.414s 0.0001s 0.0144s
Gen 1 13 colls, 12 par 29.684s 3.856s 0.2966s 1.8202s
Parallel GC work balance: 0.00% (serial 0%, perfect 100%)
TASKS: 18 (1 bound, 17 peak workers (17 total), using -N8)
SPARKS: 0 (0 converted, 0 overflowed, 0 dud, 0 GC'd, 0 fizzled)
INIT time 0.015s ( 0.014s elapsed)
MUT time 8.823s ( 4.534s elapsed)
GC time 48.803s ( 6.271s elapsed)
EXIT time 0.100s ( 0.099s elapsed)
Total time 57.742s ( 10.918s elapsed)
Alloc rate 1,799,875,628 bytes per MUT second
Productivity 15.5% of total user, 81.7% of total elapsed
gc_alloc_block_sync: 457978
whitehole_spin: 0
gen[0].sync: 22
gen[1].sync: 189
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment