Skip to content

Commit 64dc2ee

Browse files
iohk-bors[bot]coot
andauthored
Merge #3371
3371: CAD-3444 io-sim changes from p2p-master r=coot a=coot - io-sim: selectTraceEvents' and friends - io-sim: Octopus - io-sim: added pretty printers - io-sim: log EventMask - io-sim: label Async's TVar Co-authored-by: Marcin Szamotulski <[email protected]>
2 parents e221354 + f31a400 commit 64dc2ee

File tree

5 files changed

+407
-82
lines changed

5 files changed

+407
-82
lines changed

io-sim/io-sim.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ source-repository head
2525

2626
library
2727
hs-source-dirs: src
28-
exposed-modules: Control.Monad.IOSim
28+
exposed-modules: Data.List.Trace
29+
, Control.Monad.IOSim
2930
other-modules: Control.Monad.IOSim.Internal
3031
default-language: Haskell2010
3132
other-extensions: BangPatterns,

io-sim/src/Control/Monad/IOSim.hs

Lines changed: 123 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE ExplicitNamespaces #-}
2+
{-# LANGUAGE NamedFieldPuns #-}
13
{-# LANGUAGE RankNTypes #-}
24
{-# LANGUAGE ScopedTypeVariables #-}
35

@@ -19,15 +21,27 @@ module Control.Monad.IOSim (
1921
setCurrentTime,
2022
unshareClock,
2123
-- * Simulation trace
22-
Trace(..),
23-
TraceEvent(..),
24+
type SimTrace,
25+
Trace (Cons, Nil, Trace, SimTrace, TraceMainReturn, TraceMainException, TraceDeadlock),
26+
ppTrace,
27+
ppTrace_,
28+
ppEvents,
29+
SimResult(..),
30+
SimEvent(..),
31+
SimEventType(..),
2432
ThreadLabel,
2533
Labelled (..),
2634
traceEvents,
2735
traceResult,
2836
selectTraceEvents,
37+
selectTraceEvents',
2938
selectTraceEventsDynamic,
39+
selectTraceEventsDynamic',
3040
selectTraceEventsSay,
41+
selectTraceEventsSay',
42+
traceSelectTraceEvents,
43+
traceSelectTraceEventsDynamic,
44+
traceSelectTraceEventsSay,
3145
printTraceEventsSay,
3246
-- * Eventlog
3347
EventlogEvent(..),
@@ -36,15 +50,19 @@ module Control.Monad.IOSim (
3650
execReadTVar,
3751
-- * Deprecated interfaces
3852
SimM,
39-
SimSTM
53+
SimSTM,
54+
TraceEvent
4055
) where
4156

4257
import Prelude
4358

4459
import Data.Dynamic (fromDynamic)
4560
import Data.List (intercalate)
61+
import Data.Bifoldable
4662
import Data.Typeable (Typeable)
4763

64+
import Data.List.Trace (Trace (..))
65+
4866
import Control.Exception (throw)
4967

5068
import Control.Monad.ST.Lazy
@@ -56,48 +74,115 @@ import Control.Monad.IOSim.Internal
5674

5775

5876
selectTraceEvents
59-
:: (TraceEvent -> Maybe b)
60-
-> Trace a
77+
:: (SimEventType -> Maybe b)
78+
-> SimTrace a
6179
-> [b]
62-
selectTraceEvents fn = go
63-
where
64-
go (Trace _ _ _ ev trace) = case fn ev of
65-
Just x -> x : go trace
66-
Nothing -> go trace
67-
go (TraceMainException _ e _) = throw (FailureException e)
68-
go (TraceDeadlock _ threads) = throw (FailureDeadlock threads)
69-
go (TraceMainReturn _ _ _) = []
80+
selectTraceEvents fn =
81+
bifoldr ( \ v _
82+
-> case v of
83+
MainException _ e _ -> throw (FailureException e)
84+
Deadlock _ threads -> throw (FailureDeadlock threads)
85+
MainReturn _ _ _ -> []
86+
)
87+
( \ b acc -> b : acc )
88+
[]
89+
. traceSelectTraceEvents fn
90+
91+
selectTraceEvents'
92+
:: (SimEventType -> Maybe b)
93+
-> SimTrace a
94+
-> [b]
95+
selectTraceEvents' fn =
96+
bifoldr ( \ _ _ -> [] )
97+
( \ b acc -> b : acc )
98+
[]
99+
. traceSelectTraceEvents fn
70100

71101
-- | Select all the traced values matching the expected type. This relies on
72102
-- the sim's dynamic trace facility.
73103
--
74104
-- For convenience, this throws exceptions for abnormal sim termination.
75105
--
76-
selectTraceEventsDynamic :: forall a b. Typeable b => Trace a -> [b]
106+
selectTraceEventsDynamic :: forall a b. Typeable b => SimTrace a -> [b]
77107
selectTraceEventsDynamic = selectTraceEvents fn
78108
where
79-
fn :: TraceEvent -> Maybe b
109+
fn :: SimEventType -> Maybe b
110+
fn (EventLog dyn) = fromDynamic dyn
111+
fn _ = Nothing
112+
113+
-- | Like 'selectTraceEventsDynamic' but returns partial trace if an exception
114+
-- is found in it.
115+
--
116+
selectTraceEventsDynamic' :: forall a b. Typeable b => SimTrace a -> [b]
117+
selectTraceEventsDynamic' = selectTraceEvents' fn
118+
where
119+
fn :: SimEventType -> Maybe b
80120
fn (EventLog dyn) = fromDynamic dyn
81121
fn _ = Nothing
82122

83123
-- | Get a trace of 'EventSay'.
84124
--
85125
-- For convenience, this throws exceptions for abnormal sim termination.
86126
--
87-
selectTraceEventsSay :: Trace a -> [String]
127+
selectTraceEventsSay :: SimTrace a -> [String]
88128
selectTraceEventsSay = selectTraceEvents fn
89129
where
90-
fn :: TraceEvent -> Maybe String
130+
fn :: SimEventType -> Maybe String
131+
fn (EventSay s) = Just s
132+
fn _ = Nothing
133+
134+
-- | Like 'selectTraceEventsSay' but return partial trace if an exception is
135+
-- found in it.
136+
--
137+
selectTraceEventsSay' :: SimTrace a -> [String]
138+
selectTraceEventsSay' = selectTraceEvents' fn
139+
where
140+
fn :: SimEventType -> Maybe String
91141
fn (EventSay s) = Just s
92142
fn _ = Nothing
93143

94144
-- | Print all 'EventSay' to the console.
95145
--
96146
-- For convenience, this throws exceptions for abnormal sim termination.
97147
--
98-
printTraceEventsSay :: Trace a -> IO ()
148+
printTraceEventsSay :: SimTrace a -> IO ()
99149
printTraceEventsSay = mapM_ print . selectTraceEventsSay
100150

151+
152+
-- | The most general select function. It is a _total_ function.
153+
--
154+
traceSelectTraceEvents
155+
:: (SimEventType -> Maybe b)
156+
-> SimTrace a
157+
-> Trace (SimResult a) b
158+
traceSelectTraceEvents fn = bifoldr ( \ v _acc -> Nil v )
159+
( \ eventCtx acc
160+
-> case fn (seType eventCtx) of
161+
Nothing -> acc
162+
Just b -> Cons b acc
163+
)
164+
undefined -- it is ignored
165+
166+
-- | Select dynamic events. It is a _total_ function.
167+
--
168+
traceSelectTraceEventsDynamic :: forall a b. Typeable b
169+
=> SimTrace a -> Trace (SimResult a) b
170+
traceSelectTraceEventsDynamic = traceSelectTraceEvents fn
171+
where
172+
fn :: SimEventType -> Maybe b
173+
fn (EventLog dyn) = fromDynamic dyn
174+
fn _ = Nothing
175+
176+
177+
-- | Select say events. It is a _total_ function.
178+
--
179+
traceSelectTraceEventsSay :: forall a. SimTrace a -> Trace (SimResult a) String
180+
traceSelectTraceEventsSay = traceSelectTraceEvents fn
181+
where
182+
fn :: SimEventType -> Maybe String
183+
fn (EventSay s) = Just s
184+
fn _ = Nothing
185+
101186
-- | Simulation termination with failure
102187
--
103188
data Failure =
@@ -147,24 +232,37 @@ runSimOrThrow mainAction =
147232
runSimStrictShutdown :: forall a. (forall s. IOSim s a) -> Either Failure a
148233
runSimStrictShutdown mainAction = traceResult True (runSimTrace mainAction)
149234

150-
traceResult :: Bool -> Trace a -> Either Failure a
235+
traceResult :: Bool -> SimTrace a -> Either Failure a
151236
traceResult strict = go
152237
where
153-
go (Trace _ _ _ _ t) = go t
238+
go (SimTrace _ _ _ _ t) = go t
154239
go (TraceMainReturn _ _ tids@(_:_))
155240
| strict = Left (FailureSloppyShutdown tids)
156241
go (TraceMainReturn _ x _) = Right x
157242
go (TraceMainException _ e _) = Left (FailureException e)
158243
go (TraceDeadlock _ threads) = Left (FailureDeadlock threads)
159244

160-
traceEvents :: Trace a -> [(Time, ThreadId, Maybe ThreadLabel, TraceEvent)]
161-
traceEvents (Trace time tid tlbl event t) = (time, tid, tlbl, event)
162-
: traceEvents t
163-
traceEvents _ = []
245+
traceEvents :: SimTrace a -> [(Time, ThreadId, Maybe ThreadLabel, SimEventType)]
246+
traceEvents (SimTrace time tid tlbl event t) = (time, tid, tlbl, event)
247+
: traceEvents t
248+
traceEvents _ = []
164249

250+
ppEvents :: [(Time, ThreadId, Maybe ThreadLabel, SimEventType)]
251+
-> String
252+
ppEvents events =
253+
intercalate "\n"
254+
[ ppSimEvent width
255+
SimEvent {seTime, seThreadId, seThreadLabel, seType }
256+
| (seTime, seThreadId, seThreadLabel, seType) <- events
257+
]
258+
where
259+
width = maximum
260+
[ maybe 0 length threadLabel
261+
| (_, _, threadLabel, _) <- events
262+
]
165263

166264

167265
-- | See 'runSimTraceST' below.
168266
--
169-
runSimTrace :: forall a. (forall s. IOSim s a) -> Trace a
267+
runSimTrace :: forall a. (forall s. IOSim s a) -> SimTrace a
170268
runSimTrace mainAction = runST (runSimTraceST mainAction)

0 commit comments

Comments
 (0)