Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view MasterLoop.c @ 70:f9b60012fd74
working ucontext version
| author | Merten Sach <msach@mailbox.tu-berlin.de> |
|---|---|
| date | Fri, 27 May 2011 12:35:40 +0200 |
| parents | 11bfe9d136ed |
| children | efb55f1b5fb9 |
line source
1 /*
2 * Copyright 2010 OpenSourceStewardshipFoundation
3 *
4 * Licensed under BSD
5 */
9 #include <stdio.h>
10 #include <stddef.h>
12 #include "VMS.h"
15 //===========================================================================
16 void inline
17 stealWorkInto( SchedSlot *currSlot, VMSQueueStruc *readyToAnimateQ,
18 VirtProcr *masterPr );
20 //===========================================================================
23 /*This code is animated by the virtual Master processor.
24 *
25 *Polls each sched slot exactly once, hands any requests made by a newly
26 * done slave to the "request handler" plug-in function
27 *
28 *Any slots that need a virt procr assigned are given to the "schedule"
29 * plug-in function, which tries to assign a virt procr (slave) to it.
30 *
31 *When all slots needing a processor have been given to the schedule plug-in,
32 * a fraction of the procrs successfully scheduled are put into the
33 * work queue, then a continuation of this function is put in, then the rest
34 * of the virt procrs that were successfully scheduled.
35 *
36 *The first thing the continuation does is busy-wait until the previous
37 * animation completes. This is because an (unlikely) continuation may
38 * sneak through queue before previous continuation is done putting second
39 * part of scheduled slaves in, which is the only race condition.
40 *
41 */
43 /*May 29, 2010 -- birth a Master during init so that first core loop to
44 * start running gets it and does all the stuff for a newly born --
45 * from then on, will be doing continuation, but do suspension self
46 * directly at end of master loop
47 *So VMS__init just births the master virtual processor same way it births
48 * all the others -- then does any extra setup needed and puts it into the
49 * work queue.
50 *However means have to make masterEnv a global static volatile the same way
51 * did with readyToAnimateQ in core loop. -- for performance, put the
52 * jump to the core loop directly in here, and have it directly jump back.
53 *
54 *
55 *Aug 18, 2010 -- Going to a separate MasterVP for each core, to see if this
56 * avoids the suspected bug in the system stack that causes bizarre faults
57 * at random places in the system code.
58 *
59 *So, this function is coupled to each of the MasterVPs, -- meaning this
60 * function can't rely on a particular stack and frame -- each MasterVP that
61 * animates this function has a different one.
62 *
63 *At this point, the masterLoop does not write itself into the queue anymore,
64 * instead, the coreLoop acquires the masterLock when it has nothing to
65 * animate, and then animates its own masterLoop. However, still try to put
66 * several AppVPs into the queue to amortize the startup cost of switching
67 * to the MasterVP. Note, don't have to worry about latency of requests much
68 * because most requests generate work for same core -- only latency issue
69 * is case when other cores starved and one core's requests generate work
70 * for them -- so keep max in queue to 3 or 4..
71 */
72 void masterLoop( void *initData, VirtProcr *animatingPr )
73 {
74 int32 slotIdx, numSlotsFilled;
75 VirtProcr *schedVirtPr;
76 SchedSlot *currSlot, **schedSlots;
77 MasterEnv *masterEnv;
78 VMSQueueStruc *readyToAnimateQ;
80 SlaveScheduler slaveScheduler;
81 RequestHandler requestHandler;
82 void *semanticEnv;
84 int32 thisCoresIdx;
85 VirtProcr *masterPr;
87 //First animation of each MasterVP will in turn animate this part
88 // of setup code.. (VP creator sets up the stack as if this function
89 // was called normally, but actually get here by jmp)
90 //So, setup values about stack ptr, jmp pt and all that
91 masterPr = animatingPr; //just to make sure after jmp
94 //Note, got rid of writing the stack and frame ptr up here, because
95 // only one
96 // core can ever animate a given MasterVP, so don't need to communicate
97 // new frame and stack ptr to the MasterVP storage before a second
98 // version of that MasterVP can get animated on a different core.
99 //Also got rid of the busy-wait.
101 masterEnv = (MasterEnv*)_VMSMasterEnv;
103 thisCoresIdx = masterPr->coreAnimatedBy;
104 readyToAnimateQ = masterEnv->readyToAnimateQs[thisCoresIdx];
105 schedSlots = masterEnv->allSchedSlots[thisCoresIdx];
107 requestHandler = masterEnv->requestHandler;
108 slaveScheduler = masterEnv->slaveScheduler;
109 semanticEnv = masterEnv->semanticEnv;
113 while(1){
115 //============================= MEASUREMENT STUFF ========================
116 #ifdef MEAS__TIME_MASTER
117 //Total Master time includes one coreloop time -- just assume the core
118 // loop time is same for Master as for AppVPs, even though it may be
119 // smaller due to higher predictability of the fixed jmp.
120 saveLowTimeStampCountInto( masterPr->startMasterTSCLow );
121 #endif
122 //========================================================================
124 //Poll each slot's Done flag
125 numSlotsFilled = 0;
126 for( slotIdx = 0; slotIdx < NUM_SCHED_SLOTS; slotIdx++)
127 {
128 currSlot = schedSlots[ slotIdx ];
130 if( currSlot->workIsDone )
131 {
132 currSlot->workIsDone = FALSE;
133 currSlot->needsProcrAssigned = TRUE;
135 //process requests from slave to master
136 //====================== MEASUREMENT STUFF ===================
137 #ifdef MEAS__TIME_PLUGIN
138 int32 startStamp1, endStamp1;
139 saveLowTimeStampCountInto( startStamp1 );
140 #endif
141 //============================================================
142 (*requestHandler)( currSlot->procrAssignedToSlot, semanticEnv );
143 //====================== MEASUREMENT STUFF ===================
144 #ifdef MEAS__TIME_PLUGIN
145 saveLowTimeStampCountInto( endStamp1 );
146 addIntervalToHist( startStamp1, endStamp1,
147 _VMSMasterEnv->reqHdlrLowTimeHist );
148 addIntervalToHist( startStamp1, endStamp1,
149 _VMSMasterEnv->reqHdlrHighTimeHist );
150 #endif
151 //============================================================
152 }
153 if( currSlot->needsProcrAssigned )
154 { //give slot a new virt procr
155 schedVirtPr =
156 (*slaveScheduler)( semanticEnv, thisCoresIdx );
158 if( schedVirtPr != NULL )
159 { currSlot->procrAssignedToSlot = schedVirtPr;
160 schedVirtPr->schedSlot = currSlot;
161 currSlot->needsProcrAssigned = FALSE;
162 numSlotsFilled += 1;
164 writeVMSQ( schedVirtPr, readyToAnimateQ );
165 }
166 }
167 }
170 #ifdef USE_WORK_STEALING
171 //If no slots filled, means no more work, look for work to steal.
172 if( numSlotsFilled == 0 )
173 { gateProtected_stealWorkInto( currSlot, readyToAnimateQ, masterPr );
174 }
175 #endif
178 #ifdef MEAS__TIME_MASTER
179 saveLowTimeStampCountInto( masterPr->endMasterTSCLow );
180 #endif
182 //Clear lock
183 _VMSMasterEnv->masterLock = 0;
185 //Swap to coreLoop
186 swapcontext(masterPr->savedVPStatus,masterPr->savedCoreLoopStatus);
187 }
189 }
192 /*This has a race condition -- the coreloops are accessing their own queues
193 * at the same time that this work-stealer on a different core is trying to
194 */
195 void inline
196 stealWorkInto( SchedSlot *currSlot, VMSQueueStruc *readyToAnimateQ,
197 VirtProcr *masterPr )
198 {
199 VirtProcr *stolenPr;
200 int32 coreIdx, i;
201 VMSQueueStruc *currQ;
203 stolenPr = NULL;
204 coreIdx = masterPr->coreAnimatedBy;
205 for( i = 0; i < NUM_CORES -1; i++ )
206 {
207 if( coreIdx >= NUM_CORES -1 )
208 { coreIdx = 0;
209 }
210 else
211 { coreIdx++;
212 }
213 currQ = _VMSMasterEnv->readyToAnimateQs[coreIdx];
214 if( numInVMSQ( currQ ) > 0 )
215 { stolenPr = readVMSQ (currQ );
216 break;
217 }
218 }
220 if( stolenPr != NULL )
221 { currSlot->procrAssignedToSlot = stolenPr;
222 stolenPr->schedSlot = currSlot;
223 currSlot->needsProcrAssigned = FALSE;
225 writeVMSQ( stolenPr, readyToAnimateQ );
226 }
227 }
229 /*This algorithm makes the common case fast. Make the coreloop passive,
230 * and show its progress. Make the stealer control a gate that coreloop
231 * has to pass.
232 *To avoid interference, only one stealer at a time. Use a global
233 * stealer-lock.
234 *
235 *The pattern is based on a gate -- stealer shuts the gate, then monitors
236 * to be sure any already past make it all the way out, before starting.
237 *So, have a "progress" measure just before the gate, then have two after it,
238 * one is in a "waiting room" outside the gate, the other is at the exit.
239 *Then, the stealer first shuts the gate, then checks the progress measure
240 * outside it, then looks to see if the progress measure at the exit is the
241 * same. If yes, it knows the protected area is empty 'cause no other way
242 * to get in and the last to get in also exited.
243 *If the progress measure at the exit is not the same, then the stealer goes
244 * into a loop checking both the waiting-area and the exit progress-measures
245 * until one of them shows the same as the measure outside the gate. Might
246 * as well re-read the measure outside the gate each go around, just to be
247 * sure. It is guaranteed that one of the two will eventually match the one
248 * outside the gate.
249 *
250 *Here's an informal proof of correctness:
251 *The gate can be closed at any point, and have only four cases:
252 * 1) coreloop made it past the gate-closing but not yet past the exit
253 * 2) coreloop made it past the pre-gate progress update but not yet past
254 * the gate,
255 * 3) coreloop is right before the pre-gate update
256 * 4) coreloop is past the exit and far from the pre-gate update.
257 *
258 * Covering the cases in reverse order,
259 * 4) is not a problem -- stealer will read pre-gate progress, see that it
260 * matches exit progress, and the gate is closed, so stealer can proceed.
261 * 3) stealer will read pre-gate progress just after coreloop updates it..
262 * so stealer goes into a loop until the coreloop causes wait-progress
263 * to match pre-gate progress, so then stealer can proceed
264 * 2) same as 3..
265 * 1) stealer reads pre-gate progress, sees that it's different than exit,
266 * so goes into loop until exit matches pre-gate, now it knows coreloop
267 * is not in protected and cannot get back in, so can proceed.
268 *
269 *Implementation for the stealer:
270 *
271 *First, acquire the stealer lock -- only cores with no work to do will
272 * compete to steal, so not a big performance penalty having only one --
273 * will rarely have multiple stealers in a system with plenty of work -- and
274 * in a system with little work, it doesn't matter.
275 *
276 *Note, have single-reader, single-writer pattern for all variables used to
277 * communicate between stealer and victims
278 *
279 *So, scan the queues of the core loops, until find non-empty. Each core
280 * has its own list that it scans. The list goes in order from closest to
281 * furthest core, so it steals first from close cores. Later can add
282 * taking info from the app about overlapping footprints, and scan all the
283 * others then choose work with the most footprint overlap with the contents
284 * of this core's cache.
285 *
286 *Now, have a victim want to take work from. So, shut the gate in that
287 * coreloop, by setting the "gate closed" var on its stack to TRUE.
288 *Then, read the core's pre-gate progress and compare to the core's exit
289 * progress.
290 *If same, can proceed to take work from the coreloop's queue. When done,
291 * write FALSE to gate closed var.
292 *If different, then enter a loop that reads the pre-gate progress, then
293 * compares to exit progress then to wait progress. When one of two
294 * matches, proceed. Take work from the coreloop's queue. When done,
295 * write FALSE to the gate closed var.
296 *
297 */
298 void inline
299 gateProtected_stealWorkInto( SchedSlot *currSlot,
300 VMSQueueStruc *myReadyToAnimateQ,
301 VirtProcr *masterPr )
302 {
303 VirtProcr *stolenPr;
304 int32 coreIdx, i, haveAVictim, gotLock;
305 VMSQueueStruc *victimsQ;
307 volatile GateStruc *vicGate;
308 int32 coreMightBeInProtected;
312 //see if any other cores have work available to steal
313 haveAVictim = FALSE;
314 coreIdx = masterPr->coreAnimatedBy;
315 for( i = 0; i < NUM_CORES -1; i++ )
316 {
317 if( coreIdx >= NUM_CORES -1 )
318 { coreIdx = 0;
319 }
320 else
321 { coreIdx++;
322 }
323 victimsQ = _VMSMasterEnv->readyToAnimateQs[coreIdx];
324 if( numInVMSQ( victimsQ ) > 0 )
325 { haveAVictim = TRUE;
326 vicGate = _VMSMasterEnv->workStealingGates[ coreIdx ];
327 break;
328 }
329 }
330 if( !haveAVictim ) return; //no work to steal, exit
332 //have a victim core, now get the stealer-lock
333 gotLock =__sync_bool_compare_and_swap( &(_VMSMasterEnv->workStealingLock),
334 UNLOCKED, LOCKED );
335 if( !gotLock ) return; //go back to core loop, which will re-start master
338 //====== Start Gate-protection =======
339 vicGate->gateClosed = TRUE;
340 coreMightBeInProtected= vicGate->preGateProgress != vicGate->exitProgress;
341 while( coreMightBeInProtected )
342 { //wait until sure
343 if( vicGate->preGateProgress == vicGate->waitProgress )
344 coreMightBeInProtected = FALSE;
345 if( vicGate->preGateProgress == vicGate->exitProgress )
346 coreMightBeInProtected = FALSE;
347 }
349 stolenPr = readVMSQ ( victimsQ );
351 vicGate->gateClosed = FALSE;
352 //======= End Gate-protection =======
355 if( stolenPr != NULL ) //victim could have been in protected and taken
356 { currSlot->procrAssignedToSlot = stolenPr;
357 stolenPr->schedSlot = currSlot;
358 currSlot->needsProcrAssigned = FALSE;
360 writeVMSQ( stolenPr, myReadyToAnimateQ );
361 }
363 //unlock the work stealing lock
364 _VMSMasterEnv->workStealingLock = UNLOCKED;
365 }
