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