Skip to content

Commit 4497edc

Browse files
committed
io-sim(-por): added ppSimEventType
1 parent 2b35ab3 commit 4497edc

File tree

3 files changed

+160
-27
lines changed

3 files changed

+160
-27
lines changed

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

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE DeriveAnyClass #-}
22
{-# LANGUAGE DeriveGeneric #-}
33
{-# LANGUAGE DerivingStrategies #-}
4+
{-# LANGUAGE DerivingVia #-}
45
{-# LANGUAGE GADTs #-}
56
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
67
{-# LANGUAGE ScopedTypeVariables #-}
@@ -9,12 +10,16 @@
910
--
1011
module Control.Monad.IOSim.CommonTypes
1112
( IOSimThreadId (..)
13+
, ppIOSimThreadId
14+
, StepId
15+
, ppStepId
1216
, childThreadId
1317
, setRacyThread
1418
, TVarId (..)
1519
, TimeoutId (..)
1620
, ClockId (..)
1721
, VectorClock (..)
22+
, ppVectorClock
1823
, unTimeoutId
1924
, ThreadLabel
2025
, TVarLabel
@@ -23,6 +28,8 @@ module Control.Monad.IOSim.CommonTypes
2328
, Deschedule (..)
2429
, ThreadStatus (..)
2530
, BlockedReason (..)
31+
-- * Utils
32+
, ppList
2633
) where
2734

2835
import Control.DeepSeq (NFData (..))
@@ -31,10 +38,13 @@ import Control.Monad.ST.Lazy
3138

3239
import NoThunks.Class
3340

41+
import Data.List (intercalate, intersperse)
3442
import Data.Map (Map)
43+
import qualified Data.Map as Map
3544
import Data.STRef.Lazy
3645
import Data.Set (Set)
3746
import GHC.Generics
47+
import Quiet
3848

3949

4050
-- | A thread id.
@@ -44,12 +54,20 @@ import GHC.Generics
4454
-- `Control.Monad.Class.MonadTest.exploreRaces` was
4555
-- executed in it or it's a thread forked by a racy thread.
4656
--
47-
data IOSimThreadId = RacyThreadId [Int]
48-
| ThreadId [Int] -- non racy threads have higher priority
57+
data IOSimThreadId =
58+
-- | A racy thread (`IOSimPOR` only), shown in the trace with curly braces,
59+
-- e.g. `Thread {2,3}`.
60+
RacyThreadId [Int]
61+
-- | A non racy thread. They have higher priority than racy threads in
62+
-- `IOSimPOR` scheduler.
63+
| ThreadId [Int]
4964
deriving stock (Eq, Ord, Show, Generic)
5065
deriving anyclass NFData
5166
deriving anyclass NoThunks
5267

68+
ppIOSimThreadId :: IOSimThreadId -> String
69+
ppIOSimThreadId (RacyThreadId as) = "Thread {"++ intercalate "," (map show as) ++"}"
70+
ppIOSimThreadId (ThreadId as) = "Thread " ++ show as
5371

5472
childThreadId :: IOSimThreadId -> Int -> IOSimThreadId
5573
childThreadId (RacyThreadId is) i = RacyThreadId (is ++ [i])
@@ -59,12 +77,26 @@ setRacyThread :: IOSimThreadId -> IOSimThreadId
5977
setRacyThread (ThreadId is) = RacyThreadId is
6078
setRacyThread tid@RacyThreadId{} = tid
6179

80+
-- | Execution step in `IOSimPOR` is identified by the thread id and
81+
-- a monotonically increasing number (thread specific).
82+
--
83+
type StepId = (IOSimThreadId, Int)
84+
85+
ppStepId :: (IOSimThreadId, Int) -> String
86+
ppStepId (tid, step) | step < 0
87+
= concat [ppIOSimThreadId tid, ".-"]
88+
ppStepId (tid, step) = concat [ppIOSimThreadId tid, ".", show step]
89+
6290

6391
newtype TVarId = TVarId Int deriving (Eq, Ord, Enum, Show)
6492
newtype TimeoutId = TimeoutId Int deriving (Eq, Ord, Enum, Show)
6593
newtype ClockId = ClockId [Int] deriving (Eq, Ord, Show)
6694
newtype VectorClock = VectorClock { getVectorClock :: Map IOSimThreadId Int }
67-
deriving Show
95+
deriving Generic
96+
deriving Show via Quiet VectorClock
97+
98+
ppVectorClock :: VectorClock -> String
99+
ppVectorClock (VectorClock m) = "VectorClock " ++ "[" ++ concat (intersperse ", " (ppStepId <$> Map.toList m)) ++ "]"
68100

69101
unTimeoutId :: TimeoutId -> Int
70102
unTimeoutId (TimeoutId a) = a
@@ -129,3 +161,10 @@ data BlockedReason = BlockedOnSTM
129161
| BlockedOnDelay
130162
| BlockedOnThrowTo
131163
deriving (Eq, Show)
164+
165+
--
166+
-- Utils
167+
--
168+
169+
ppList :: (a -> String) -> [a] -> String
170+
ppList pp as = "[" ++ concat (intersperse ", " (map pp as)) ++ "]"

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

Lines changed: 106 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{-# LANGUAGE ExistentialQuantification #-}
66
{-# LANGUAGE FlexibleInstances #-}
77
{-# LANGUAGE GADTSyntax #-}
8+
{-# LANGUAGE LambdaCase #-}
89
{-# LANGUAGE MultiParamTypeClasses #-}
910
{-# LANGUAGE NamedFieldPuns #-}
1011
{-# LANGUAGE NumericUnderscores #-}
@@ -22,7 +23,6 @@ module Control.Monad.IOSim.Types
2223
, traceSTM
2324
, liftST
2425
, SimA (..)
25-
, StepId
2626
, STMSim
2727
, STM (..)
2828
, runSTM
@@ -46,6 +46,7 @@ module Control.Monad.IOSim.Types
4646
, EventlogEvent (..)
4747
, EventlogMarker (..)
4848
, SimEventType (..)
49+
, ppSimEventType
4950
, SimEvent (..)
5051
, SimResult (..)
5152
, SimTrace
@@ -767,31 +768,35 @@ ppSimEvent :: Int -- ^ width of the time
767768
-> Int -- ^ width of thread label
768769
-> SimEvent
769770
-> String
770-
ppSimEvent timeWidth tidWidth tLabelWidth SimEvent {seTime, seThreadId, seThreadLabel, seType} =
771+
772+
ppSimEvent timeWidth tidWidth tLabelWidth SimEvent {seTime = Time time, seThreadId, seThreadLabel, seType} =
771773
printf "%-*s - %-*s %-*s - %s"
772774
timeWidth
773-
(show seTime)
775+
(show time)
774776
tidWidth
775-
(show seThreadId)
777+
(ppIOSimThreadId seThreadId)
776778
tLabelWidth
777779
threadLabel
778-
(show seType)
780+
(ppSimEventType seType)
779781
where
780782
threadLabel = fromMaybe "" seThreadLabel
781-
ppSimEvent timeWidth tidWidth tLableWidth SimPOREvent {seTime, seThreadId, seStep, seThreadLabel, seType} =
783+
784+
ppSimEvent timeWidth tidWidth tLableWidth SimPOREvent {seTime = Time time, seThreadId, seStep, seThreadLabel, seType} =
782785
printf "%-*s - %-*s %-*s - %s"
783786
timeWidth
784-
(show seTime)
787+
(show time)
785788
tidWidth
786-
(show (seThreadId, seStep))
789+
(ppStepId (seThreadId, seStep))
787790
tLableWidth
788791
threadLabel
789-
(show seType)
792+
(ppSimEventType seType)
790793
where
791794
threadLabel = fromMaybe "" seThreadLabel
795+
792796
ppSimEvent _ _ _ (SimRacesFound controls) =
793797
"RacesFound "++show controls
794798

799+
795800
-- | A result type of a simulation.
796801
data SimResult a
797802
= MainReturn !Time a ![Labelled IOSimThreadId]
@@ -805,6 +810,7 @@ data SimResult a
805810
| Loop
806811
-- ^ Only returned by /IOSimPOR/ when a step execution took longer than
807812
-- 'explorationStepTimelimit` was exceeded.
813+
| InternalError String
808814
deriving (Show, Functor)
809815

810816
-- | A type alias for 'IOSim' simulation trace. It comes with useful pattern
@@ -867,6 +873,8 @@ ppTrace_ tr = Trace.ppTrace
867873
)
868874
$ tr
869875

876+
877+
870878
-- | Trace each event using 'Debug.trace'; this is useful when a trace ends with
871879
-- a pure error, e.g. an assertion.
872880
--
@@ -1029,6 +1037,91 @@ data SimEventType
10291037
-- a simulation. Useful for debugging IOSimPOR.
10301038
deriving Show
10311039

1040+
ppSimEventType :: SimEventType -> String
1041+
ppSimEventType = \case
1042+
EventSay a -> "Say " ++ a
1043+
EventLog a -> "Dynamic " ++ show a
1044+
EventMask a -> "Mask " ++ show a
1045+
EventThrow a -> "Throw " ++ show a
1046+
EventThrowTo err tid ->
1047+
concat [ "ThrowTo (",
1048+
show err, ") ",
1049+
ppIOSimThreadId tid ]
1050+
EventThrowToBlocked -> "ThrowToBlocked"
1051+
EventThrowToWakeup -> "ThrowToWakeup"
1052+
EventThrowToUnmasked a ->
1053+
"ThrowToUnmasked " ++ ppLabelled ppIOSimThreadId a
1054+
EventThreadForked a ->
1055+
"ThreadForked " ++ ppIOSimThreadId a
1056+
EventThreadFinished -> "ThreadFinished"
1057+
EventThreadUnhandled a ->
1058+
"ThreadUnhandled " ++ show a
1059+
EventTxCommitted written created mbEff ->
1060+
concat [ "TxCommitted ",
1061+
ppList (ppLabelled show) written, " ",
1062+
ppList (ppLabelled show) created,
1063+
maybe "" ((' ' :) . ppEffect) mbEff ]
1064+
1065+
EventTxAborted mbEff ->
1066+
concat [ "TxAborted",
1067+
maybe "" ((' ' :) . ppEffect) mbEff ]
1068+
EventTxBlocked blocked mbEff ->
1069+
concat [ "TxBlocked ",
1070+
ppList (ppLabelled show) blocked,
1071+
maybe "" ((' ' :) . ppEffect) mbEff ]
1072+
EventTxWakeup changed ->
1073+
"TxWakeup " ++ ppList (ppLabelled show) changed
1074+
EventUnblocked unblocked ->
1075+
"Unblocked " ++ ppList ppIOSimThreadId unblocked
1076+
EventThreadDelay tid t ->
1077+
concat [ "ThreadDelay ",
1078+
show tid, " ",
1079+
show t ]
1080+
EventThreadDelayFired tid -> "ThreadDelayFired " ++ show tid
1081+
EventTimeoutCreated timer tid t ->
1082+
concat [ "TimeoutCreated ",
1083+
show timer, " ",
1084+
ppIOSimThreadId tid, " ",
1085+
show t ]
1086+
EventTimeoutFired timer ->
1087+
"TimeoutFired " ++ show timer
1088+
EventRegisterDelayCreated timer tvarId t ->
1089+
concat [ "RegisterDelayCreated ",
1090+
show timer, " ",
1091+
show tvarId, " ",
1092+
show t ]
1093+
EventRegisterDelayFired timer -> "RegisterDelayFired " ++ show timer
1094+
EventTimerCreated timer tvarId t ->
1095+
concat [ "TimerCreated ",
1096+
show timer, " ",
1097+
show tvarId, " ",
1098+
show t ]
1099+
EventTimerUpdated timer t ->
1100+
concat [ "TimerUpdated ",
1101+
show timer, " ",
1102+
show t ]
1103+
EventTimerCancelled timer -> "TimerCancelled " ++ show timer
1104+
EventTimerFired timer -> "TimerFired " ++ show timer
1105+
EventThreadStatus tid tid' ->
1106+
concat [ "ThreadStatus ",
1107+
ppIOSimThreadId tid, " ",
1108+
ppIOSimThreadId tid' ]
1109+
EventSimStart a -> "SimStart " ++ show a
1110+
EventThreadSleep -> "ThreadSleep"
1111+
EventThreadWake -> "ThreadWake"
1112+
EventDeschedule a -> "Deschedule " ++ show a
1113+
EventFollowControl a -> "FollowControl " ++ show a
1114+
EventAwaitControl s a ->
1115+
concat [ "AwaitControl ",
1116+
ppStepId s, " ",
1117+
show a ]
1118+
EventPerformAction a -> "PerformAction " ++ ppStepId a
1119+
EventReschedule a -> "Reschedule " ++ show a
1120+
EventEffect clock eff ->
1121+
concat [ "Effect ",
1122+
ppVectorClock clock, " ",
1123+
ppEffect eff ]
1124+
EventRaces a -> "Races " ++ show a
10321125

10331126
-- | A labelled value.
10341127
--
@@ -1041,6 +1134,10 @@ data Labelled a = Labelled {
10411134
deriving (Eq, Ord, Generic)
10421135
deriving Show via Quiet (Labelled a)
10431136

1137+
ppLabelled :: (a -> String) -> Labelled a -> String
1138+
ppLabelled pp Labelled { l_labelled = a, l_label = Nothing } = pp a
1139+
ppLabelled pp Labelled { l_labelled = a, l_label = Just lbl } = concat ["Labelled ", pp a, " ", lbl]
1140+
10441141
--
10451142
-- Executing STM Transactions
10461143
--

io-sim/src/Control/Monad/IOSimPOR/Types.hs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ module Control.Monad.IOSimPOR.Types
99
, wakeupEffects
1010
, onlyReadEffect
1111
, racingEffects
12+
, ppEffect
1213
-- * Schedules
1314
, ScheduleControl (..)
1415
, isDefaultSchedule
1516
, ScheduleMod (..)
1617
-- * Steps
1718
, StepId
19+
, ppStepId
1820
, Step (..)
1921
, StepInfo (..)
2022
-- * Races
@@ -42,17 +44,17 @@ data Effect = Effect {
4244
effectThrows :: ![IOSimThreadId],
4345
effectWakeup :: !(Set IOSimThreadId)
4446
}
45-
deriving Eq
47+
deriving (Show, Eq)
4648

47-
instance Show Effect where
48-
show Effect { effectReads, effectWrites, effectForks, effectThrows, effectWakeup } =
49-
concat $ [ "Effect { " ]
50-
++ [ "reads = " ++ show effectReads ++ ", " | not (null effectReads) ]
51-
++ [ "writes = " ++ show effectWrites ++ ", " | not (null effectWrites) ]
52-
++ [ "forks = " ++ show effectForks ++ ", " | not (null effectForks)]
53-
++ [ "throws = " ++ show effectThrows ++ ", " | not (null effectThrows) ]
54-
++ [ "wakeup = " ++ show effectWakeup ++ ", " | not (null effectWakeup) ]
55-
++ [ "}" ]
49+
ppEffect :: Effect -> String
50+
ppEffect Effect { effectReads, effectWrites, effectForks, effectThrows, effectWakeup } =
51+
concat $ [ "Effect { " ]
52+
++ [ "reads = " ++ show effectReads ++ ", " | not (null effectReads) ]
53+
++ [ "writes = " ++ show effectWrites ++ ", " | not (null effectWrites) ]
54+
++ [ "forks = " ++ ppList ppIOSimThreadId (Set.toList effectForks) ++ ", " | not (null effectForks) ]
55+
++ [ "throws = " ++ ppList ppIOSimThreadId effectThrows ++ ", " | not (null effectThrows) ]
56+
++ [ "wakeup = " ++ ppList ppIOSimThreadId (Set.toList effectWakeup) ++ ", " | not (null effectWakeup) ]
57+
++ [ "}" ]
5658

5759

5860
instance Semigroup Effect where
@@ -164,11 +166,6 @@ data ScheduleMod = ScheduleMod{
164166
deriving (Eq, Ord)
165167

166168

167-
-- | Execution step is identified by the thread id and a monotonically
168-
-- increasing number (thread specific).
169-
--
170-
type StepId = (IOSimThreadId, Int)
171-
172169
instance Show ScheduleMod where
173170
showsPrec d (ScheduleMod tgt ctrl insertion) =
174171
showParen (d>10) $

0 commit comments

Comments
 (0)