view MasterLoop.c @ 169:d1dd9e6ee72c

VMS preprocessor definitions moved to VMS_defs.h and changes to measurement
author Merten Sach <msach@mailbox.tu-berlin.de>
date Fri, 16 Dec 2011 20:00:21 +0100
parents fe5ec83f1baf
children c3f458403cd6
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 *masterVP );
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 *masterVP;
88 volatile VirtProcr *volatilemasterVP;
90 volatilemasterVP = animatingPr;
91 masterVP = (VirtProcr*)volatilemasterVP; //used to force re-define after jmp
93 //====================== Measurement =====================
94 TSCountLowHigh endMaster;
95 uint64 numCycles;
96 //==========================================================
98 //masterLoopStartPt:
99 while(1){ //switch to core_loop and back to here is at end of loop
101 //============================= MEASUREMENT STUFF =======================
102 #ifdef MEAS__TIME_MASTER
103 //Total Master time includes one coreloop time -- just assume the core
104 // loop time is same for Master as for AppVPs, even though it may be
105 // smaller due to higher predictability of the fixed jmp.
106 saveLowTimeStampCountInto( masterVP->startMasterTSCLow );
107 #endif
108 //=======================================================================
110 masterEnv = (MasterEnv*)_VMSMasterEnv;
112 //GCC may optimize so doesn't always re-define from frame-storage
113 masterVP = (VirtProcr*)volatilemasterVP; //just to make sure after jmp
114 thisCoresIdx = masterVP->coreAnimatedBy;
115 readyToAnimateQ = masterEnv->readyToAnimateQs[thisCoresIdx];
116 schedSlots = masterEnv->allSchedSlots[thisCoresIdx];
118 requestHandler = masterEnv->requestHandler;
119 slaveScheduler = masterEnv->slaveScheduler;
120 semanticEnv = masterEnv->semanticEnv;
123 //Poll each slot's Done flag
124 numSlotsFilled = 0;
125 Meas_startMasterLoop
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 Meas_startReqHdlr
137 (*requestHandler)( currSlot->procrAssignedToSlot, semanticEnv );
138 Meas_endReqHdlr
139 }
140 if( currSlot->needsProcrAssigned )
141 { //give slot a new virt procr
142 schedVirtPr =
143 (*slaveScheduler)( semanticEnv, thisCoresIdx );
145 if( schedVirtPr != NULL )
146 { currSlot->procrAssignedToSlot = schedVirtPr;
147 schedVirtPr->schedSlot = currSlot;
148 currSlot->needsProcrAssigned = FALSE;
149 numSlotsFilled += 1;
151 writeVMSQ( schedVirtPr, readyToAnimateQ );
152 }
153 }
154 }
155 Meas_endMasterLoop
157 #ifdef USE_WORK_STEALING
158 //If no slots filled, means no more work, look for work to steal.
159 if( numSlotsFilled == 0 )
160 { gateProtected_stealWorkInto( currSlot, readyToAnimateQ, masterVP );
161 }
162 #endif
166 //=================== Meas =======================
167 #ifdef MEAS__TIME_MASTER
168 saveLowTimeStampCountInto( masterVP->endMasterTSCLow );
169 #endif
170 #ifdef MEAS__TIME_2011_SYS
171 //Take meas here, to get cycles since entered Master
172 saveTSCLowHigh(endMaster);
173 numCycles = endMaster.longVal - _VMSMasterEnv->startMaster.longVal;
175 if( numCycles < 200000 ) //sanity check against swap thd out)
176 { masterEnv->totalMasterCycles += numCycles;
177 masterEnv->numMasterAnimations++;
178 }
179 #endif
180 //==================================================
181 masterSwitchToCoreLoop(animatingPr); //"finishes" when switch back to Master
182 flushRegisters();
183 }//MasterLoop
185 }
189 /*This has a race condition -- the coreloops are accessing their own queues
190 * at the same time that this work-stealer on a different core is trying to
191 */
192 void inline
193 stealWorkInto( SchedSlot *currSlot, VMSQueueStruc *readyToAnimateQ,
194 VirtProcr *masterVP )
195 {
196 VirtProcr *stolenPr;
197 int32 coreIdx, i;
198 VMSQueueStruc *currQ;
200 stolenPr = NULL;
201 coreIdx = masterVP->coreAnimatedBy;
202 for( i = 0; i < NUM_CORES -1; i++ )
203 {
204 if( coreIdx >= NUM_CORES -1 )
205 { coreIdx = 0;
206 }
207 else
208 { coreIdx++;
209 }
210 currQ = _VMSMasterEnv->readyToAnimateQs[coreIdx];
211 if( numInVMSQ( currQ ) > 0 )
212 { stolenPr = readVMSQ (currQ );
213 break;
214 }
215 }
217 if( stolenPr != NULL )
218 { currSlot->procrAssignedToSlot = stolenPr;
219 stolenPr->schedSlot = currSlot;
220 currSlot->needsProcrAssigned = FALSE;
222 writeVMSQ( stolenPr, readyToAnimateQ );
223 }
224 }
226 /*This algorithm makes the common case fast. Make the coreloop passive,
227 * and show its progress. Make the stealer control a gate that coreloop
228 * has to pass.
229 *To avoid interference, only one stealer at a time. Use a global
230 * stealer-lock.
231 *
232 *The pattern is based on a gate -- stealer shuts the gate, then monitors
233 * to be sure any already past make it all the way out, before starting.
234 *So, have a "progress" measure just before the gate, then have two after it,
235 * one is in a "waiting room" outside the gate, the other is at the exit.
236 *Then, the stealer first shuts the gate, then checks the progress measure
237 * outside it, then looks to see if the progress measure at the exit is the
238 * same. If yes, it knows the protected area is empty 'cause no other way
239 * to get in and the last to get in also exited.
240 *If the progress measure at the exit is not the same, then the stealer goes
241 * into a loop checking both the waiting-area and the exit progress-measures
242 * until one of them shows the same as the measure outside the gate. Might
243 * as well re-read the measure outside the gate each go around, just to be
244 * sure. It is guaranteed that one of the two will eventually match the one
245 * outside the gate.
246 *
247 *Here's an informal proof of correctness:
248 *The gate can be closed at any point, and have only four cases:
249 * 1) coreloop made it past the gate-closing but not yet past the exit
250 * 2) coreloop made it past the pre-gate progress update but not yet past
251 * the gate,
252 * 3) coreloop is right before the pre-gate update
253 * 4) coreloop is past the exit and far from the pre-gate update.
254 *
255 * Covering the cases in reverse order,
256 * 4) is not a problem -- stealer will read pre-gate progress, see that it
257 * matches exit progress, and the gate is closed, so stealer can proceed.
258 * 3) stealer will read pre-gate progress just after coreloop updates it..
259 * so stealer goes into a loop until the coreloop causes wait-progress
260 * to match pre-gate progress, so then stealer can proceed
261 * 2) same as 3..
262 * 1) stealer reads pre-gate progress, sees that it's different than exit,
263 * so goes into loop until exit matches pre-gate, now it knows coreloop
264 * is not in protected and cannot get back in, so can proceed.
265 *
266 *Implementation for the stealer:
267 *
268 *First, acquire the stealer lock -- only cores with no work to do will
269 * compete to steal, so not a big performance penalty having only one --
270 * will rarely have multiple stealers in a system with plenty of work -- and
271 * in a system with little work, it doesn't matter.
272 *
273 *Note, have single-reader, single-writer pattern for all variables used to
274 * communicate between stealer and victims
275 *
276 *So, scan the queues of the core loops, until find non-empty. Each core
277 * has its own list that it scans. The list goes in order from closest to
278 * furthest core, so it steals first from close cores. Later can add
279 * taking info from the app about overlapping footprints, and scan all the
280 * others then choose work with the most footprint overlap with the contents
281 * of this core's cache.
282 *
283 *Now, have a victim want to take work from. So, shut the gate in that
284 * coreloop, by setting the "gate closed" var on its stack to TRUE.
285 *Then, read the core's pre-gate progress and compare to the core's exit
286 * progress.
287 *If same, can proceed to take work from the coreloop's queue. When done,
288 * write FALSE to gate closed var.
289 *If different, then enter a loop that reads the pre-gate progress, then
290 * compares to exit progress then to wait progress. When one of two
291 * matches, proceed. Take work from the coreloop's queue. When done,
292 * write FALSE to the gate closed var.
293 *
294 */
295 void inline
296 gateProtected_stealWorkInto( SchedSlot *currSlot,
297 VMSQueueStruc *myReadyToAnimateQ,
298 VirtProcr *masterVP )
299 {
300 VirtProcr *stolenPr;
301 int32 coreIdx, i, haveAVictim, gotLock;
302 VMSQueueStruc *victimsQ;
304 volatile GateStruc *vicGate;
305 int32 coreMightBeInProtected;
309 //see if any other cores have work available to steal
310 haveAVictim = FALSE;
311 coreIdx = masterVP->coreAnimatedBy;
312 for( i = 0; i < NUM_CORES -1; i++ )
313 {
314 if( coreIdx >= NUM_CORES -1 )
315 { coreIdx = 0;
316 }
317 else
318 { coreIdx++;
319 }
320 victimsQ = _VMSMasterEnv->readyToAnimateQs[coreIdx];
321 if( numInVMSQ( victimsQ ) > 0 )
322 { haveAVictim = TRUE;
323 vicGate = _VMSMasterEnv->workStealingGates[ coreIdx ];
324 break;
325 }
326 }
327 if( !haveAVictim ) return; //no work to steal, exit
329 //have a victim core, now get the stealer-lock
330 gotLock =__sync_bool_compare_and_swap( &(_VMSMasterEnv->workStealingLock),
331 UNLOCKED, LOCKED );
332 if( !gotLock ) return; //go back to core loop, which will re-start master
335 //====== Start Gate-protection =======
336 vicGate->gateClosed = TRUE;
337 coreMightBeInProtected= vicGate->preGateProgress != vicGate->exitProgress;
338 while( coreMightBeInProtected )
339 { //wait until sure
340 if( vicGate->preGateProgress == vicGate->waitProgress )
341 coreMightBeInProtected = FALSE;
342 if( vicGate->preGateProgress == vicGate->exitProgress )
343 coreMightBeInProtected = FALSE;
344 }
346 stolenPr = readVMSQ ( victimsQ );
348 vicGate->gateClosed = FALSE;
349 //======= End Gate-protection =======
352 if( stolenPr != NULL ) //victim could have been in protected and taken
353 { currSlot->procrAssignedToSlot = stolenPr;
354 stolenPr->schedSlot = currSlot;
355 currSlot->needsProcrAssigned = FALSE;
357 writeVMSQ( stolenPr, myReadyToAnimateQ );
358 }
360 //unlock the work stealing lock
361 _VMSMasterEnv->workStealingLock = UNLOCKED;
362 }