2014-12-20 14:01:51 +03:00
|
|
|
{-# LANGUAGE RankNTypes #-}
|
|
|
|
|
|
|
|
-- | A runner for concurrent monads to systematically detect
|
|
|
|
-- concurrency errors such as data races and deadlocks.
|
|
|
|
--
|
|
|
|
-- As an example, consider this program, which has two locks and a
|
|
|
|
-- shared variable. Two threads are spawned, which claim the locks,
|
|
|
|
-- update the shared variable, and release the locks. The main thread
|
|
|
|
-- waits for them both to terminate, and returns the final result.
|
|
|
|
--
|
2014-12-21 15:59:57 +03:00
|
|
|
-- > bad :: ConcCVar cvar m => m Int
|
2014-12-20 14:01:51 +03:00
|
|
|
-- > bad = do
|
2014-12-21 10:47:45 +03:00
|
|
|
-- > a <- newEmptyCVar
|
|
|
|
-- > b <- newEmptyCVar
|
2014-12-20 14:01:51 +03:00
|
|
|
-- >
|
2014-12-21 12:38:25 +03:00
|
|
|
-- > c <- newCVar 0
|
2014-12-20 14:01:51 +03:00
|
|
|
-- >
|
2014-12-21 12:38:25 +03:00
|
|
|
-- > j1 <- spawn $ lock a >> lock b >> modifyCVar_ c (return . succ) >> unlock b >> unlock a
|
|
|
|
-- > j2 <- spawn $ lock b >> lock a >> modifyCVar_ c (return . pred) >> unlock a >> unlock b
|
2014-12-20 14:01:51 +03:00
|
|
|
-- >
|
2014-12-21 10:47:45 +03:00
|
|
|
-- > takeCVar j1
|
|
|
|
-- > takeCVar j2
|
2014-12-20 14:01:51 +03:00
|
|
|
-- >
|
2014-12-21 10:47:45 +03:00
|
|
|
-- > takeCVar c
|
2014-12-20 14:01:51 +03:00
|
|
|
--
|
|
|
|
-- The correct result is 0, as it starts out as 0 and is incremented
|
|
|
|
-- and decremented by threads 1 and 2, respectively. However, note the
|
|
|
|
-- order of acquisition of the locks in the two threads. If thread 2
|
|
|
|
-- pre-empts thread 1 between the acquisition of the locks (or if
|
|
|
|
-- thread 1 pre-empts thread 2), a deadlock situation will arise, as
|
|
|
|
-- thread 1 will have lock `a` and be waiting on `b`, and thread 2
|
|
|
|
-- will have `b` and be waiting on `a`.
|
|
|
|
|
|
|
|
module Control.Monad.Conc.SCT
|
2015-01-05 00:48:00 +03:00
|
|
|
( -- * Types
|
2014-12-20 14:01:51 +03:00
|
|
|
SCTScheduler
|
2014-12-25 00:41:32 +03:00
|
|
|
, SchedTrace
|
|
|
|
, SCTTrace
|
2014-12-21 15:42:43 +03:00
|
|
|
, Decision(..)
|
2015-01-05 00:48:00 +03:00
|
|
|
|
|
|
|
-- * SCT Runners
|
2014-12-20 14:01:51 +03:00
|
|
|
, runSCT
|
2014-12-28 15:12:57 +03:00
|
|
|
, runSCTIO
|
2015-01-04 18:06:53 +03:00
|
|
|
, runSCT'
|
|
|
|
, runSCTIO'
|
2014-12-21 16:50:52 +03:00
|
|
|
|
2015-01-05 00:48:00 +03:00
|
|
|
-- * Random Schedulers
|
2014-12-20 14:01:51 +03:00
|
|
|
, sctRandom
|
2014-12-21 16:50:52 +03:00
|
|
|
, sctRandomNP
|
|
|
|
|
2015-01-05 00:48:00 +03:00
|
|
|
-- * Pre-emption Bounding
|
|
|
|
, sctPreBound
|
|
|
|
, sctPreBoundIO
|
|
|
|
, preEmpCount
|
|
|
|
|
2014-12-21 16:50:52 +03:00
|
|
|
-- * Utilities
|
|
|
|
, toSCT
|
2014-12-21 15:42:43 +03:00
|
|
|
, showTrace
|
2015-01-05 00:48:00 +03:00
|
|
|
, ordNub
|
2014-12-20 14:01:51 +03:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Control.Monad.Conc.Fixed
|
2014-12-21 19:34:55 +03:00
|
|
|
import System.Random (RandomGen)
|
2014-12-20 14:01:51 +03:00
|
|
|
|
2014-12-28 15:12:57 +03:00
|
|
|
import qualified Control.Monad.Conc.Fixed.IO as CIO
|
2015-01-05 00:48:00 +03:00
|
|
|
import qualified Data.Set as Set
|
|
|
|
|
|
|
|
-- * Types
|
2014-12-28 15:12:57 +03:00
|
|
|
|
2014-12-20 14:01:51 +03:00
|
|
|
-- | An @SCTScheduler@ is like a regular 'Scheduler', except it builds
|
2014-12-21 16:25:48 +03:00
|
|
|
-- a trace of scheduling decisions made.
|
2015-01-05 18:05:29 +03:00
|
|
|
--
|
|
|
|
-- Note that the 'SchedTrace' is built in *reverse*, this is more
|
|
|
|
-- efficient than appending to the list every time.
|
2014-12-25 00:41:32 +03:00
|
|
|
type SCTScheduler s = Scheduler (s, SchedTrace)
|
2014-12-21 16:25:48 +03:00
|
|
|
|
2014-12-25 00:41:32 +03:00
|
|
|
-- | A @SchedTrace@ is just a list of all the decisions that were made,
|
2014-12-21 16:25:48 +03:00
|
|
|
-- with the alternative decisions that could have been made at each
|
2015-01-05 18:05:29 +03:00
|
|
|
-- step.
|
2014-12-25 00:41:32 +03:00
|
|
|
type SchedTrace = [(Decision, [Decision])]
|
|
|
|
|
|
|
|
-- | A @SCTTrace@ is a combined 'SchedTrace' and 'Trace'.
|
|
|
|
type SCTTrace = [(Decision, [Decision], ThreadAction)]
|
2014-12-21 15:42:43 +03:00
|
|
|
|
|
|
|
-- | Scheduling decisions are based on the state of the running
|
|
|
|
-- program, and so we can capture some of that state in recording what
|
|
|
|
-- specific decision we made.
|
|
|
|
data Decision =
|
|
|
|
Start ThreadId
|
|
|
|
-- ^ Start a new thread, because the last was blocked (or it's the
|
|
|
|
-- initial thread).
|
|
|
|
| Continue
|
|
|
|
-- ^ Continue running the last thread for another step.
|
|
|
|
| SwitchTo ThreadId
|
|
|
|
-- ^ Pre-empt the running thread, and switch to another.
|
2015-01-05 00:48:00 +03:00
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
|
|
|
-- * SCT Runners
|
2014-12-20 14:01:51 +03:00
|
|
|
|
|
|
|
-- | Run a concurrent program under a given scheduler a number of
|
|
|
|
-- times, collecting the results and the scheduling that gave rise to
|
|
|
|
-- them.
|
|
|
|
--
|
|
|
|
-- The initial state for each run is the final state of the last run,
|
|
|
|
-- so it is important that the scheduler actually maintain some
|
|
|
|
-- internal state, or all the results will be identical.
|
2014-12-27 15:26:40 +03:00
|
|
|
runSCT :: SCTScheduler s -> s -> Int -> (forall t. Conc t a) -> [(Maybe a, SCTTrace)]
|
2015-01-04 18:06:53 +03:00
|
|
|
runSCT sched s n = runSCT' sched s n term step where
|
2015-01-05 00:48:00 +03:00
|
|
|
term _ g = g == 0
|
|
|
|
step s' g _ = (s', g - 1)
|
2015-01-04 18:06:53 +03:00
|
|
|
|
|
|
|
-- | A varant of 'runSCT' for concurrent programs that do 'IO'.
|
|
|
|
--
|
|
|
|
-- Warning! The IO will be executed lots of times, in lots of
|
|
|
|
-- interleavings! Be very confident that nothing in a 'liftIO' can
|
|
|
|
-- block on the action of another thread, or you risk deadlocking this
|
|
|
|
-- function!
|
|
|
|
runSCTIO :: SCTScheduler s -> s -> Int -> (forall t. CIO.Conc t a) -> IO [(Maybe a, SCTTrace)]
|
|
|
|
runSCTIO sched s n = runSCTIO' sched s n term step where
|
2015-01-05 00:48:00 +03:00
|
|
|
term _ g = g == 0
|
|
|
|
step s' g _ = (s', g - 1)
|
2015-01-04 18:06:53 +03:00
|
|
|
|
|
|
|
-- | Run a concurrent program under a given scheduler, where the SCT
|
|
|
|
-- runner itself maintains some internal state, and has a function to
|
|
|
|
-- produce a new scheduler state for each run, and decide termination
|
|
|
|
-- based on the internal state.
|
|
|
|
--
|
|
|
|
-- Note: the state step function takes the state returned by the
|
|
|
|
-- scheduler, not the initial state!
|
|
|
|
runSCT' :: SCTScheduler s -- ^ The scheduler
|
|
|
|
-> s -- ^ The scheduler's initial satte
|
|
|
|
-> g -- ^ The runner's initial state
|
2015-01-05 00:48:00 +03:00
|
|
|
-> (s -> g -> Bool) -- ^ Termination decider
|
|
|
|
-> (s -> g -> SCTTrace -> (s, g)) -- ^ State step function
|
2015-01-04 18:06:53 +03:00
|
|
|
-> (forall t. Conc t a) -- ^ Conc program
|
|
|
|
-> [(Maybe a, SCTTrace)]
|
|
|
|
runSCT' sched s g term step c
|
2015-01-05 00:48:00 +03:00
|
|
|
| term s g = []
|
|
|
|
| otherwise = (res, trace) : rest where
|
2014-12-27 15:26:40 +03:00
|
|
|
|
|
|
|
(res, (s', strace), ttrace) = runConc' sched (s, [(Start 0, [])]) c
|
|
|
|
|
2015-01-05 18:05:29 +03:00
|
|
|
trace = reverse $ scttrace strace ttrace
|
2015-01-05 00:48:00 +03:00
|
|
|
|
|
|
|
(s'', g') = step s' g trace
|
2014-12-27 15:26:40 +03:00
|
|
|
|
2015-01-04 18:06:53 +03:00
|
|
|
rest = runSCT' sched s'' g' term step c
|
|
|
|
|
|
|
|
-- | A variant of runSCT' for concurrent programs that do IO.
|
2014-12-28 15:12:57 +03:00
|
|
|
--
|
|
|
|
-- Warning! The IO will be executed lots of times, in lots of
|
|
|
|
-- interleavings! Be very confident that nothing in a 'liftIO' can
|
|
|
|
-- block on the action of another thread, or you risk deadlocking this
|
2015-01-04 18:06:53 +03:00
|
|
|
-- function!
|
2015-01-05 00:48:00 +03:00
|
|
|
runSCTIO' :: SCTScheduler s -> s -> g -> (s -> g -> Bool) -> (s -> g -> SCTTrace -> (s, g)) -> (forall t. CIO.Conc t a) -> IO [(Maybe a, SCTTrace)]
|
2015-01-04 18:06:53 +03:00
|
|
|
runSCTIO' sched s g term step c
|
2015-01-05 00:48:00 +03:00
|
|
|
| term s g = return []
|
2015-01-04 18:06:53 +03:00
|
|
|
| otherwise = do
|
|
|
|
(res, (s', strace), ttrace) <- CIO.runConc' sched (s, [(Start 0, [])]) c
|
|
|
|
|
2015-01-05 18:05:29 +03:00
|
|
|
let trace = reverse $ scttrace strace ttrace
|
2015-01-05 00:48:00 +03:00
|
|
|
let (s'', g') = step s' g trace
|
2014-12-28 15:12:57 +03:00
|
|
|
|
2015-01-04 18:06:53 +03:00
|
|
|
rest <- runSCTIO' sched s'' g' term step c
|
2014-12-28 15:12:57 +03:00
|
|
|
|
2015-01-05 00:48:00 +03:00
|
|
|
return $ (res, trace) : rest
|
2014-12-28 15:12:57 +03:00
|
|
|
|
2015-01-05 00:48:00 +03:00
|
|
|
-- * Random Schedulers
|
2014-12-20 14:01:51 +03:00
|
|
|
|
|
|
|
-- | A simple pre-emptive random scheduler.
|
|
|
|
sctRandom :: RandomGen g => SCTScheduler g
|
2014-12-21 16:50:52 +03:00
|
|
|
sctRandom = toSCT randomSched
|
|
|
|
|
|
|
|
-- | A random scheduler with no pre-emption.
|
|
|
|
sctRandomNP :: RandomGen g => SCTScheduler g
|
|
|
|
sctRandomNP = toSCT randomSchedNP
|
|
|
|
|
2015-01-05 00:48:00 +03:00
|
|
|
-- * Pre-emption bounding
|
|
|
|
|
|
|
|
data PreBoundState = P
|
|
|
|
{ _pc :: Int
|
|
|
|
-- ^ Current pre-emption count.
|
|
|
|
, _next :: [[Decision]]
|
|
|
|
-- ^ Schedules to try in this pc.
|
|
|
|
, _done :: [SCTTrace]
|
|
|
|
-- ^ Schedules completed in this pc.
|
|
|
|
, _halt :: Bool
|
|
|
|
-- ^ Indicates more schedules couldn't be found, and to halt
|
|
|
|
-- immediately.
|
|
|
|
}
|
|
|
|
|
|
|
|
-- | An SCT runner using a pre-emption bounding scheduler. Schedules
|
|
|
|
-- will be explored systematically, starting with all
|
|
|
|
-- pre-emption-count zero schedules, and gradually adding more
|
|
|
|
-- pre-emptions.
|
2015-01-05 18:05:29 +03:00
|
|
|
sctPreBound :: Int
|
|
|
|
-- ^ The pre-emption bound. Anything < 0 will be
|
|
|
|
-- interpreted as 0.
|
2015-01-05 00:48:00 +03:00
|
|
|
-> (forall t. Conc t a) -> [(Maybe a, SCTTrace)]
|
|
|
|
sctPreBound pb = runSCT' pbSched s g (pbTerm pb') (pbStep pb') where
|
2015-01-05 09:42:41 +03:00
|
|
|
s = ([], [], [])
|
2015-01-05 00:48:00 +03:00
|
|
|
g = P { _pc = 0, _next = [], _done = [], _halt = False }
|
|
|
|
pb' = if pb < 0 then 0 else pb
|
|
|
|
|
|
|
|
-- | Variant of 'sctPreBound' using 'IO'. See usual caveats about IO.
|
|
|
|
sctPreBoundIO :: Int -> (forall t. CIO.Conc t a) -> IO [(Maybe a, SCTTrace)]
|
|
|
|
sctPreBoundIO pb = runSCTIO' pbSched s g (pbTerm pb') (pbStep pb') where
|
2015-01-05 09:42:41 +03:00
|
|
|
s = ([], [], [])
|
2015-01-05 00:48:00 +03:00
|
|
|
g = P { _pc = 0, _next = [], _done = [], _halt = False }
|
|
|
|
pb' = if pb < 0 then 0 else pb
|
|
|
|
|
|
|
|
-- | Pre-emption bounding scheduler, which uses a queue of scheduling
|
2015-01-05 09:42:41 +03:00
|
|
|
-- decisions to drive the initial trace, returning the generated
|
|
|
|
-- suffix.
|
|
|
|
pbSched :: SCTScheduler ([Decision], SchedTrace, SchedTrace)
|
|
|
|
pbSched ((d, pref, suff), trc) prior threads@(next:_) = case d of
|
2015-01-05 00:48:00 +03:00
|
|
|
-- If we have a decision queued, make it.
|
2015-01-05 18:05:29 +03:00
|
|
|
(Start t:ds) -> let trc' = (Start t, alters t) in (t, ((ds, trc':pref, suff), trc':trc))
|
|
|
|
(Continue:ds) -> let trc' = (Continue, alters prior) in (prior, ((ds, trc':pref, suff), trc':trc))
|
|
|
|
(SwitchTo t:ds) -> let trc' = (SwitchTo t, alters t) in (t, ((ds, trc':pref, suff), trc':trc))
|
2015-01-05 00:48:00 +03:00
|
|
|
|
|
|
|
-- Otherwise just use a non-pre-emptive scheduler.
|
2015-01-05 18:05:29 +03:00
|
|
|
[] | prior `elem` threads -> let trc' = (Continue, alters prior) in (prior, (([], pref, trc':suff), trc':trc))
|
|
|
|
| otherwise -> let trc' = (Start next, alters next) in (next, (([], pref, trc':suff), trc':trc))
|
2015-01-05 00:48:00 +03:00
|
|
|
|
2015-01-05 09:42:41 +03:00
|
|
|
where
|
|
|
|
alters tid
|
|
|
|
| tid == prior = map SwitchTo $ filter (/=prior) threads
|
|
|
|
| prior `elem` threads = Continue : map SwitchTo (filter (\t -> t /= prior && t /= tid) threads)
|
|
|
|
| otherwise = map Start $ filter (/=tid) threads
|
2015-01-05 00:48:00 +03:00
|
|
|
|
|
|
|
-- | Pre-emption bounding termination function: terminates on attempt
|
|
|
|
-- to start a PB above the limit.
|
|
|
|
pbTerm :: Int -> a -> PreBoundState -> Bool
|
|
|
|
pbTerm pb _ g = (_pc g == pb + 1) || _halt g
|
|
|
|
|
|
|
|
-- | Pre-emption bounding state step function: computes remaining
|
|
|
|
-- schedules to try and chooses one.
|
2015-01-05 09:42:41 +03:00
|
|
|
pbStep :: Int -> (a, SchedTrace, SchedTrace) -> PreBoundState -> SCTTrace -> (([Decision], SchedTrace, SchedTrace), PreBoundState)
|
2015-01-05 18:05:29 +03:00
|
|
|
pbStep pb (_, rPref, rSuff) g t = case _next g of
|
2015-01-05 00:48:00 +03:00
|
|
|
-- We have schedules remaining in this PB, so run the next
|
2015-01-05 09:42:41 +03:00
|
|
|
(x:xs) -> (s' x, g { _next = xs ++ thisPB, _done = done' })
|
2015-01-05 00:48:00 +03:00
|
|
|
|
|
|
|
-- We have no schedules remaining, try to generate some more.
|
|
|
|
--
|
|
|
|
-- If there are no more schedules in this PB, and this isn't the
|
|
|
|
-- last PB, advance to the next.
|
|
|
|
--
|
|
|
|
-- If there are no schedules in the next PB, halt.
|
|
|
|
[] ->
|
2015-01-05 09:42:41 +03:00
|
|
|
case thisPB of
|
|
|
|
(x:xs) -> (s' x, g { _next = xs, _done = done' })
|
|
|
|
[] -> if _pc g == pb
|
|
|
|
then halt
|
|
|
|
else case nextPB of
|
|
|
|
(x:xs) -> (s' x, g { _pc = pc', _next = xs, _done = [] })
|
|
|
|
[] -> halt
|
2015-01-05 00:48:00 +03:00
|
|
|
|
|
|
|
where
|
2015-01-05 18:05:29 +03:00
|
|
|
pref = reverse rPref
|
|
|
|
suff = reverse rSuff
|
|
|
|
|
2015-01-05 09:42:41 +03:00
|
|
|
halt = (([], [], []), g { _halt = True })
|
2015-01-05 18:05:29 +03:00
|
|
|
done' = if couldPre t then t : _done g else _done g
|
2015-01-05 00:48:00 +03:00
|
|
|
pc' = _pc g + 1
|
|
|
|
|
2015-01-05 09:42:41 +03:00
|
|
|
s' ds = (tail ds, [], [])
|
|
|
|
|
2015-01-05 18:05:29 +03:00
|
|
|
pref' rest = if null pref then (\((d,_,_):_) -> d:rest) t else map fst pref ++ rest
|
|
|
|
thisPB = [ pref' y | y <- others suff ]
|
2015-01-05 09:42:41 +03:00
|
|
|
nextPB = [ y | y <- ordNub $ concatMap next done', preEmpCount y == pc' ]
|
|
|
|
|
2015-01-05 00:48:00 +03:00
|
|
|
-- | Return all modifications to this schedule which do not
|
|
|
|
-- introduce extra pre-emptions.
|
2015-01-05 18:05:29 +03:00
|
|
|
others ((Start i, alts):ds) = [Start i : o | o <- others ds, not $ null o] ++ [[a] | a <- alts]
|
|
|
|
others ((SwitchTo i, alts):ds) = [SwitchTo i : o | o <- others ds, not $ null o] ++ [[a] | a <- alts]
|
|
|
|
others ((d, _):ds) = [d : o | o <- others ds, not $ null o]
|
2015-01-05 00:48:00 +03:00
|
|
|
others [] = []
|
|
|
|
|
|
|
|
-- | Return all modifications to this schedule which do introduce
|
|
|
|
-- an extra pre-emption. Only introduce pre-emptions around CVar
|
|
|
|
-- actions.
|
2015-01-05 18:05:29 +03:00
|
|
|
next ((Continue, alts, ta):ds) = [Continue : n | n <- next ds] ++ if preCand ta then [[n] | n <- alts] else []
|
|
|
|
next ((Start t, _, _):ds) = [Start t : n | n <- next ds]
|
|
|
|
next ((SwitchTo t, _, _):ds) = [SwitchTo t : n | n <- next ds]
|
2015-01-05 00:48:00 +03:00
|
|
|
next [] = []
|
|
|
|
|
2015-01-05 18:05:29 +03:00
|
|
|
-- | Check if a 'ThreadAction' is a candidate for pre-emption.
|
|
|
|
preCand (Put _) = True
|
|
|
|
preCand (TryPut _ _) = True
|
|
|
|
preCand (Take _) = True
|
|
|
|
preCand (TryTake _ _) = True
|
|
|
|
preCand BlockedPut = True
|
|
|
|
preCand Read = True
|
|
|
|
preCand BlockedRead = True
|
|
|
|
preCand BlockedTake = True
|
|
|
|
preCand _ = False
|
|
|
|
|
|
|
|
-- | Check if a trace could be modified to have additional pre-emptions
|
|
|
|
couldPre ((Continue, [], _):ds) = couldPre ds
|
|
|
|
couldPre ((Continue, _, ta):ds) = preCand ta || couldPre ds
|
|
|
|
couldPre (_:ds) = couldPre ds
|
|
|
|
couldPre [] = False
|
|
|
|
|
2015-01-05 00:48:00 +03:00
|
|
|
-- | Check the pre-emption count of some scheduling decisions.
|
|
|
|
preEmpCount :: [Decision] -> Int
|
|
|
|
preEmpCount (SwitchTo _:ss) = 1 + preEmpCount ss
|
|
|
|
preEmpCount (_:ss) = preEmpCount ss
|
|
|
|
preEmpCount [] = 0
|
|
|
|
|
|
|
|
-- * Utils
|
|
|
|
|
2014-12-21 16:50:52 +03:00
|
|
|
-- | Convert a 'Scheduler' to an 'SCTScheduler' by recording the
|
|
|
|
-- trace.
|
|
|
|
toSCT :: Scheduler s -> SCTScheduler s
|
2015-01-05 18:05:29 +03:00
|
|
|
toSCT sched (s, trace) prior threads = (tid, (s', (decision, alters) : trace)) where
|
2014-12-21 19:34:55 +03:00
|
|
|
(tid, s') = sched s prior threads
|
2014-12-21 16:50:52 +03:00
|
|
|
|
2014-12-21 19:34:55 +03:00
|
|
|
decision | tid == prior = Continue
|
|
|
|
| prior `elem` threads = SwitchTo tid
|
|
|
|
| otherwise = Start tid
|
2014-12-21 15:42:43 +03:00
|
|
|
|
2014-12-21 19:34:55 +03:00
|
|
|
alters | tid == prior = map SwitchTo $ filter (/=prior) threads
|
|
|
|
| prior `elem` threads = Continue : map SwitchTo (filter (\t -> t /= prior && t /= tid) threads)
|
|
|
|
| otherwise = map Start $ filter (/=tid) threads
|
2014-12-21 16:25:48 +03:00
|
|
|
|
2014-12-21 15:42:43 +03:00
|
|
|
-- | Pretty-print a scheduler trace.
|
2014-12-25 00:41:32 +03:00
|
|
|
showTrace :: SchedTrace -> String
|
2014-12-21 16:25:48 +03:00
|
|
|
showTrace = trace "" 0 . map fst where
|
2014-12-21 19:34:55 +03:00
|
|
|
trace prefix num (Start tid:ds) = thread prefix num ++ trace ("S" ++ show tid) 1 ds
|
|
|
|
trace prefix num (SwitchTo tid:ds) = thread prefix num ++ trace ("P" ++ show tid) 1 ds
|
|
|
|
trace prefix num (Continue:ds) = trace prefix (num + 1) ds
|
|
|
|
trace prefix num [] = thread prefix num
|
2014-12-21 15:42:43 +03:00
|
|
|
|
2014-12-21 19:34:55 +03:00
|
|
|
thread prefix num = prefix ++ replicate num '-'
|
2015-01-05 00:48:00 +03:00
|
|
|
|
|
|
|
-- | Zip a list of 'SchedTrace's and a 'Trace' together into an
|
|
|
|
-- 'SCTTrace'.
|
|
|
|
scttrace :: SchedTrace -> Trace -> SCTTrace
|
|
|
|
scttrace = zipWith $ \(d, alts) (_, act) -> (d, alts, act)
|
|
|
|
|
|
|
|
-- | O(nlogn) nub, <https://github.com/nh2/haskell-ordnub>
|
|
|
|
ordNub :: Ord a => [a] -> [a]
|
|
|
|
ordNub = go Set.empty where
|
|
|
|
go _ [] = []
|
|
|
|
go s (x:xs)
|
|
|
|
| x `Set.member` s = go s xs
|
|
|
|
| otherwise = x : go (Set.insert x s) xs
|