Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions io-sim/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,39 @@
* Fixed `timeout` in `IOSim` and `IOSimPOR`: if the action finished when the
timeout fired, the timeout exception could be raised after `timeout`
returned (#269).
* Masking state is correctly restored after an async exception (both IOSim and IOSimPOR).
* A thread blocked in `throwTo` which is interrupted, e.g. by `killThread`,
cancels its `throwTo`, as in GHC (both `IOSim` and `IOSimPOR`). Before,
the target still received the exception when it unmasked, and the
interrupted thread was woken up then, even out of an unrelated blocking
call.
* A `throwTo` which blocks is an interruptible point, as in GHC: if the
thread has pending async exceptions, and is not `MaskedUninterruptible`,
one of them is raised, and the target does not receive the exception (both
`IOSim` and `IOSimPOR`).
* A `throwTo` no longer replaces an async exception which was raised in its
target, but which the target has not run yet:
`throwTo tid e1 >> throwTo tid e2` raised only `e2`. The second `throwTo`
now blocks until the target can receive it, as in GHC (both `IOSim` and
`IOSimPOR`). In `IOSim`, when nested `timeout`s fire at the same time, the
outer one now wins.
* `IOSimPOR`: `cancelTimeout`, and the cancel action returned by
`registerDelayCancellable`, only wake up threads blocked in STM, as in
`IOSim`. Before, they also woke up a thread which had waited for the
timeout in an STM transaction, but was blocked in something else by then,
e.g. in a `throwTo`, which then returned before the exception was
delivered.
* `IOSimPOR`: the threads which `timeout` forks to throw its exception are
racy if the timed thread is, as other threads forked by a racy thread are:
races with their steps are explored, and traces show them as
`RacyThreadId`s. In a thread which called `exploreRaces`, e.g. the main
thread, they now have a lower priority than the timed thread, so the
default schedule can end differently when the action finishes at the same
time as the timeout fires.
* `IOSimPOR` explores the race between a `throwTo` and a step which wakes up
its target, e.g. by writing a `TVar` the target is blocked on. This can
multiply the number of explored schedules, e.g. when timeouts fire while
their actions wait in STM transactions which other threads wake up.

## 1.11.0.0

Expand Down
3 changes: 2 additions & 1 deletion io-sim/io-sim.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ library
psqueues >=0.2 && <0.3,
time >=1.9.1 && <1.16,
quiet,
QuickCheck,
QuickCheck >=2.15,
parallel


Expand All @@ -93,6 +93,7 @@ test-suite test
Test.Control.Monad.STM
Test.Control.Monad.Utils
Test.Control.Monad.IOSim
Test.Control.Monad.IOSim.Preemption
Test.Control.Monad.IOSimPOR
default-language: GHC2021
default-extensions: LambdaCase
Expand Down
67 changes: 63 additions & 4 deletions io-sim/src/Control/Monad/IOSim.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ module Control.Monad.IOSim
, Failure (..)
, runSimTrace
, runSimTraceST
-- *** Preemption
-- $preemption
, Preemption (..)
, noPreemption
, preemptRandom
, preemptFromMap
, preemptionsOf
, runSimWith
, runSimTraceWith
, runSimTraceSTWith
-- *** QuickCheck Monadic combinators
, monadicIOSim_
, monadicIOSim
Expand Down Expand Up @@ -97,6 +107,8 @@ import Prelude
import Data.Bifoldable
import Data.Dynamic (fromDynamic)
import Data.List (intercalate)
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
import Data.Set (Set)
import Data.Set qualified as Set
import Data.Typeable (Typeable)
Expand All @@ -109,7 +121,9 @@ import Control.Monad.ST.Lazy

import Control.Monad.Class.MonadThrow as MonadThrow

import Control.Monad.IOSim.Internal (runSimTraceST)
import Control.Monad.Class.MonadTime.SI (DiffTime)
import Control.Monad.IOSim.Internal (Preemption (..), noPreemption,
preemptRandom, runSimTraceST, runSimTraceSTWith)
import Control.Monad.IOSim.Types
import Control.Monad.IOSimPOR.Internal qualified as IOSimPOR (controlSimTraceST)
import Control.Monad.IOSimPOR.QuickCheckUtils
Expand Down Expand Up @@ -502,6 +516,49 @@ ppSayTrace tr = ppEvents
runSimTrace :: forall a. (forall s. IOSim s a) -> SimTrace a
runSimTrace mainAction = runST (runSimTraceST mainAction)

--
-- Preemption
--

-- $preemption
--
-- /IOSim/ advances time only when no thread can run, so a window between two
-- steps of one thread is never open while time passes. Real threads are
-- descheduled at any point. A 'Preemption' oracle restores some of that: at
-- a thread's yield points (after a committed STM transaction, `yield`, or
-- `timeout`'s setup) it may deschedule the thread for a while. Other threads
-- run and time advances; the preempted thread then carries on. Without an
-- oracle ('runSimTrace') nothing changes.
--
-- Search with 'preemptRandom', take the preemptions of a failing run with
-- 'preemptionsOf', and shrink them as a 'Map' replayed by 'preemptFromMap'.

-- | Preempt exactly the given (thread, yield count) points.
--
preemptFromMap :: Map (IOSimThreadId, Int) DiffTime -> Preemption
preemptFromMap m = Preemption $ \tid n -> Map.lookup (tid, n) m

-- | The preemptions a run made, for 'preemptFromMap'.
--
preemptionsOf :: SimTrace a -> Map (IOSimThreadId, Int) DiffTime
preemptionsOf = Map.fromList . go
where
go (SimTrace _ tid _ (EventPreempted _ n d _) t) = ((tid, n), d) : go t
go (Cons _ t) = go t
go (Nil _) = []

-- | 'runSimTrace' with a preemption oracle.
--
runSimTraceWith :: forall a. Preemption -> (forall s. IOSim s a) -> SimTrace a
runSimTraceWith preemption mainAction =
runST (runSimTraceSTWith preemption mainAction)

-- | 'runSim' with a preemption oracle.
--
runSimWith :: forall a. Preemption -> (forall s. IOSim s a) -> Either Failure a
runSimWith preemption mainAction =
traceResult False (runSimTraceWith preemption mainAction)

--
-- IOSimPOR
--
Expand Down Expand Up @@ -562,8 +619,8 @@ exploreSimTrace optsf mainAction k =
$ controlSimTrace
(explorationStepTimelimit opts) control mainAction
(sleeper,trace) = compareTraces passingTrace trace0
in ( counterexample ("Schedule control: " ++ show control)
$ counterexample
in counterexample ("Schedule control: " ++ show control)
( counterexample
(case sleeper of
Nothing -> "No thread delayed"
Just ((t,tid,lab),racing) ->
Expand All @@ -575,7 +632,9 @@ exploreSimTrace optsf mainAction k =
)
$ k passingTrace trace
)
.&&| let limit = (n+m-1) `div` m
-- `.&&.` must be sequential: the second property does `readRaces`,
-- which will only be populated when the first property is evaluated.
.&&. let limit = (n+m-1) `div` m
-- To ensure the set of schedules explored is deterministic, we
-- filter out cached ones *after* selecting the children of this
-- node.
Expand Down
15 changes: 7 additions & 8 deletions io-sim/src/Control/Monad/IOSim/CommonTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module Control.Monad.IOSim.CommonTypes
, StepId
, ppStepId
, childThreadId
, setRacyThread
, TVarId (..)
, VarId
, TimeoutId (..)
Expand Down Expand Up @@ -74,13 +73,10 @@ ppIOSimThreadId :: IOSimThreadId -> String
ppIOSimThreadId (RacyThreadId as) = "Thread {"++ intercalate "," (map show as) ++"}"
ppIOSimThreadId (ThreadId as) = "Thread " ++ show as

childThreadId :: IOSimThreadId -> Int -> IOSimThreadId
childThreadId (RacyThreadId is) i = RacyThreadId (is ++ [i])
childThreadId (ThreadId is) i = ThreadId (is ++ [i])

setRacyThread :: IOSimThreadId -> IOSimThreadId
setRacyThread (ThreadId is) = RacyThreadId is
setRacyThread tid@RacyThreadId{} = tid
childThreadId :: Bool -> IOSimThreadId -> Int -> IOSimThreadId
childThreadId _ (RacyThreadId is) i = RacyThreadId (is ++ [i])
childThreadId False (ThreadId is) i = ThreadId (is ++ [i])
childThreadId True (ThreadId is) i = RacyThreadId (is ++ [i])

-- | Execution step in `IOSimPOR` is identified by the thread id and
-- a monotonically increasing number (thread specific).
Expand Down Expand Up @@ -187,6 +183,9 @@ data ThreadStatus = ThreadRunning
data BlockedReason = BlockedOnSTM
| BlockedOnDelay
| BlockedOnThrowTo
| BlockedOnPreempt
-- ^ descheduled by the preemption oracle while runnable;
-- not an interruptible operation
deriving (Eq, Show)

-- | A labelled value.
Expand Down
Loading
Loading