mirrored from https://gitlab.haskell.org/ghc/ghc.git
-
Notifications
You must be signed in to change notification settings - Fork 703
/
FairShare.hs
285 lines (242 loc) · 7.63 KB
/
FairShare.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE DeriveDataTypeable #-}
-----------------------------------------------------------------------------
-- |
-- Module : FairShare
-- Copyright : (c) The University of Glasgow 2001
-- License : BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer : libraries@haskell.org
-- Stability : experimental
-- Portability : non-portable (concurrency)
--
-- A Fair-share scheduler.
--
-----------------------------------------------------------------------------
module FairShare
(
Sched
, SCont
, newSched -- IO (Sched)
, newCapability -- IO ()
, forkIO -- IO () -> IO SCont
, forkOS -- IO () -> IO SCont
, forkOn -- Int -> IO () -> IO SCont
, yield -- IO ()
, throwTo -- Exception e => SCont -> e -> IO ()
, BlockedIndefinitelyOnConcDS(..)
, blockedIndefinitelyOnConcDS
) where
import System.Time
import LwConc.Substrate
import Data.Array.IArray
import Data.Dynamic
import Control.Monad
import Data.Maybe
import qualified Data.PQueue.Min as PQ
#include "profile.h"
newtype State = State (PVar Int, PVar ClockTime, PVar Int)
deriving (Typeable)
-------------------------------------------------------------------------------
-- SCont Accounting
-------------------------------------------------------------------------------
-- |Returns the time difference in microseconds (potentially returning maxBound
-- <= the real difference)
timeDiffToMicroSec :: TimeDiff -> Int
timeDiffToMicroSec (TimeDiff _ _ _ _ _ sec picosec) =
if realTime > fromIntegral (maxBound :: Int)
then maxBound
else fromIntegral realTime
where
realTime :: Integer
realTime = (fromIntegral sec) * (10^6) + fromIntegral (picosec `div` (10^6))
_INL_(startClock)
startClock :: SCont -> PTM ()
startClock sc = do
sls <- getSLS sc
let State (_,st,_) = fromJust $ fromDynamic sls
time <- unsafeIOToPTM $ getClockTime
writePVar st $ time
_INL_(stopClock)
stopClock :: SCont -> PTM ()
stopClock sc = do
sls <- getSLS sc
let State (_,st,acc) = fromJust $ fromDynamic sls
startTime <- readPVar st
endTime <- unsafeIOToPTM $ getClockTime
sum <- readPVar acc
let newSum = sum + timeDiffToMicroSec (diffClockTimes endTime startTime)
writePVar acc newSum
where
-------------------------------------------------------------------------------
-- Scheduler
-------------------------------------------------------------------------------
data Elem = Elem SCont Int deriving Eq
instance Ord Elem where
compare (Elem _ a) (Elem _ b) = compare a b
newtype Sched = Sched (Array Int (PVar (PQ.MinQueue Elem)))
emptyScheduler :: Int -> IO Sched
emptyScheduler numCaps = do
l <- replicateM numCaps $ newPVarIO PQ.empty
return $ Sched $ listArray (0, numCaps-1) l
_INL_(deque)
deque :: Sched -> SCont -> PTM (Maybe SCont)
deque !(Sched pa) !sc = do
cc <- getSContCapability sc
pq <- readPVar $! pa ! cc
case PQ.getMin pq of
Nothing -> return $ Nothing
Just (Elem x _) -> do
writePVar (pa ! cc) (PQ.deleteMin pq)
return $ Just x
_INL_(enque)
enque :: Sched -> SCont -> PTM ()
enque !(Sched pa) !sc = do
cc <- getSContCapability sc
pq <- readPVar $! pa ! cc
acc <- readAcc sc
let newPq = PQ.insert (Elem sc acc) pq
writePVar (pa ! cc) newPq
where
readAcc sc = do
sls <- getSLS sc
let State (_,_,acc) = fromJust $ fromDynamic sls
readPVar acc
-------------------------------------------------------------------------------
-- Scheduler Activations
-------------------------------------------------------------------------------
_INL_(yieldControlAction)
yieldControlAction :: Sched -> SCont -> PTM ()
yieldControlAction !sched !sc = do
-- Accounting
stopClock sc
-- Switch to next thread
maybeSC <- deque sched sc
case maybeSC of
Nothing -> sleepCapability
Just x -> startClock x >> switchTo x
_INL_(scheduleSContAction)
scheduleSContAction :: Sched -> SCont -> PTM ()
scheduleSContAction !sched !sc = do
-- Since we are making the given scont runnable, update its status to
-- Yielded.
setSContSwitchReason sc Yielded
enque sched sc
-------------------------------------------------------------------------------
-- External scheduler interface
-------------------------------------------------------------------------------
_INL_(newSched)
newSched :: IO (Sched)
newSched = do
-- This token will be used to spawn in a round-robin fashion on different
-- capabilities.
token <- newPVarIO (0::Int)
-- Start time
startTime <- getClockTime >>= newPVarIO
-- Time diff
timeDiff <- newPVarIO (0::Int)
-- Save the token in the Thread-local State (TLS)
s <- getSContIO
setSLS s $ toDyn $ State (token, startTime, timeDiff)
-- Create the scheduler data structure
nc <- getNumCapabilities
sched <- emptyScheduler nc
-- Initialize scheduler actions
atomically $ do {
setYieldControlAction s $ yieldControlAction sched;
setScheduleSContAction s $ scheduleSContAction sched
}
-- return scheduler
return sched
_INL_(newCapability)
newCapability :: IO ()
newCapability = do
-- Initial task body
let initTask = atomically $ do {
s <- getSCont;
yca <- getYieldControlAction;
setSContSwitchReason s Completed;
yca
}
-- Create and initialize new task
s <- newSCont initTask
-- SLS
token <- newPVarIO (0::Int)
startTime <- getClockTime >>= newPVarIO
timeDiff <- newPVarIO (0::Int)
let state = State (token, startTime, timeDiff)
setSLS s $ toDyn state
atomically $ do
mySC <- getSCont
yca <- getYieldControlActionSCont mySC
setYieldControlAction s yca
ssa <- getScheduleSContActionSCont mySC
setScheduleSContAction s ssa
scheduleSContOnFreeCap s
data SContKind = Bound | Unbound
_INL_(fork)
fork :: IO () -> Maybe Int -> SContKind -> IO SCont
fork task on kind = do
currentSC <- getSContIO
nc <- getNumCapabilities
-- epilogue: Switch to next thread after completion
let epilogue = atomically $ do {
sc <- getSCont;
setSContSwitchReason sc Completed;
switchToNext <- getYieldControlAction;
switchToNext;
return ()
}
let makeSCont = case kind of
Bound -> newBoundSCont
Unbound -> newSCont
newSC <- makeSCont (task >> epilogue)
-- Initialize TLS
tls <- atomically $ getSLS currentSC
let State (token, _, _) = fromJust $ fromDynamic tls
-- Start time
startTime <- getClockTime >>= newPVarIO
-- Time diff
timeDiff <- newPVarIO (0::Int)
setSLS newSC $ toDyn $ State (token, startTime, timeDiff)
t <- atomically $ do
mySC <- getSCont
-- Initialize scheduler actions
yca <- getYieldControlActionSCont mySC
setYieldControlAction newSC yca
ssa <- getScheduleSContActionSCont mySC
setScheduleSContAction newSC ssa
t <- readPVar token
writePVar token $ (t+1) `mod` nc
return t
-- Set SCont Affinity
case on of
Nothing -> setSContCapability newSC t
Just t' -> setSContCapability newSC $ t' `mod` nc
-- Schedule new Scont
atomically $ do
ssa <- getScheduleSContActionSCont newSC
ssa newSC
return newSC
_INL_(forkIO)
forkIO :: IO () -> IO SCont
forkIO task = fork task Nothing Unbound
_INL_(forkOS)
forkOS :: IO () -> IO SCont
forkOS task = fork task Nothing Bound
_INL_(forkOn)
forkOn :: Int -> IO () -> IO SCont
forkOn on task = fork task (Just on) Unbound
_INL_(yield)
yield :: IO ()
yield = atomically $ do
-- Update SCont status to Yielded
s <- getSCont
setSContSwitchReason s Yielded
-- Append current SCont to scheduler
append <- getScheduleSContAction
append
-- Switch to next SCont from Scheduler
switchToNext <- getYieldControlAction
switchToNext