comparison MasterLoop.c @ 61:984f7d78bfdf

Merge See what happens -- merged test stuff into Nov 8 VMS version
author SeanHalle
date Thu, 11 Nov 2010 06:19:51 -0800
parents 054006c26b92 3bac84e4e56e
children
comparison
equal deleted inserted replaced
13:73dfae1847f5 17:329ee6a1efa6
5 */ 5 */
6 6
7 7
8 8
9 #include <stdio.h> 9 #include <stdio.h>
10 #include <malloc.h>
11 #include <stddef.h> 10 #include <stddef.h>
12 11
13 #include "VMS.h" 12 #include "VMS.h"
13
14
15 //===========================================================================
16 void inline
17 stealWorkInto( SchedSlot *currSlot, VMSQueueStruc *readyToAnimateQ,
18 VirtProcr *masterPr );
19
20 //===========================================================================
14 21
15 22
16 23
17 /*This code is animated by the virtual Master processor. 24 /*This code is animated by the virtual Master processor.
18 * 25 *
63 * is case when other cores starved and one core's requests generate work 70 * is case when other cores starved and one core's requests generate work
64 * for them -- so keep max in queue to 3 or 4.. 71 * for them -- so keep max in queue to 3 or 4..
65 */ 72 */
66 void masterLoop( void *initData, VirtProcr *animatingPr ) 73 void masterLoop( void *initData, VirtProcr *animatingPr )
67 { 74 {
68 int slotIdx; 75 int32 slotIdx, numSlotsFilled;
69 VirtProcr *schedVirtPr; 76 VirtProcr *schedVirtPr;
70 SchedSlot *currSlot, **schedSlots; 77 SchedSlot *currSlot, **schedSlots;
71 MasterEnv *masterEnv; 78 MasterEnv *masterEnv;
72 VMSQueueStruc *readyToAnimateQ; 79 VMSQueueStruc *readyToAnimateQ;
73 80
74 SlaveScheduler slaveScheduler; 81 SlaveScheduler slaveScheduler;
75 RequestHandler requestHandler; 82 RequestHandler requestHandler;
76 void *semanticEnv; 83 void *semanticEnv;
77 84
78 int thisCoresIdx; 85 int32 thisCoresIdx;
79 VirtProcr *masterPr; 86 VirtProcr *masterPr;
80 volatile VirtProcr *volatileMasterPr; 87 volatile VirtProcr *volatileMasterPr;
81 88
82 volatileMasterPr = animatingPr; 89 volatileMasterPr = animatingPr;
83 masterPr = volatileMasterPr; //used to force re-define after jmp 90 masterPr = volatileMasterPr; //used to force re-define after jmp
108 #endif 115 #endif
109 //======================================================================== 116 //========================================================================
110 117
111 masterEnv = _VMSMasterEnv; 118 masterEnv = _VMSMasterEnv;
112 119
113 //TODO: check that compiles so that always re-define from frame-storage 120 //GCC may optimize so doesn't always re-define from frame-storage
114 masterPr = volatileMasterPr; //just to make sure after jmp 121 masterPr = volatileMasterPr; //just to make sure after jmp
115 thisCoresIdx = masterPr->coreAnimatedBy; 122 thisCoresIdx = masterPr->coreAnimatedBy;
116 readyToAnimateQ = masterEnv->readyToAnimateQs[thisCoresIdx]; 123 readyToAnimateQ = masterEnv->readyToAnimateQs[thisCoresIdx];
117 schedSlots = masterEnv->allSchedSlots[thisCoresIdx]; 124 schedSlots = masterEnv->allSchedSlots[thisCoresIdx];
118 125
120 slaveScheduler = masterEnv->slaveScheduler; 127 slaveScheduler = masterEnv->slaveScheduler;
121 semanticEnv = masterEnv->semanticEnv; 128 semanticEnv = masterEnv->semanticEnv;
122 129
123 130
124 //Poll each slot's Done flag 131 //Poll each slot's Done flag
132 numSlotsFilled = 0;
125 for( slotIdx = 0; slotIdx < NUM_SCHED_SLOTS; slotIdx++) 133 for( slotIdx = 0; slotIdx < NUM_SCHED_SLOTS; slotIdx++)
126 { 134 {
127 currSlot = schedSlots[ slotIdx ]; 135 currSlot = schedSlots[ slotIdx ];
128 136
129 if( currSlot->workIsDone ) 137 if( currSlot->workIsDone )
141 149
142 if( schedVirtPr != NULL ) 150 if( schedVirtPr != NULL )
143 { currSlot->procrAssignedToSlot = schedVirtPr; 151 { currSlot->procrAssignedToSlot = schedVirtPr;
144 schedVirtPr->schedSlot = currSlot; 152 schedVirtPr->schedSlot = currSlot;
145 currSlot->needsProcrAssigned = FALSE; 153 currSlot->needsProcrAssigned = FALSE;
146 154 numSlotsFilled += 1;
147 writeSRSWQ( schedVirtPr, readyToAnimateQ ); 155
156 writeVMSQ( schedVirtPr, readyToAnimateQ );
148 } 157 }
149 } 158 }
150 } 159 }
151 160
152 161
153 //Save stack ptr and frame, restore CoreLoop's stack and frame, 162 #ifdef USE_WORK_STEALING
154 // and clear the MasterLock 163 //If no slots filled, means no more work, look for work to steal.
155 //TODO: cafefully verify don't need to force saving anything to stack 164 if( numSlotsFilled == 0 )
156 // before jumping back to core loop. 165 { gateProtected_stealWorkInto( currSlot, readyToAnimateQ, masterPr );
157 void *stackPtrAddr, *framePtrAddr, *masterLockAddr; 166 }
158 void *jmpPt, *coreLoopFramePtr, *coreLoopStackPtr; 167 #endif
159 168
160 stackPtrAddr = &(masterPr->stackPtr);
161 framePtrAddr = &(masterPr->framePtr);
162 masterLockAddr = &(_VMSMasterEnv->masterLock);
163
164 jmpPt = _VMSMasterEnv->coreLoopStartPt;
165 coreLoopFramePtr = masterPr->coreLoopFramePtr;//need this only
166 coreLoopStackPtr = masterPr->coreLoopStackPtr;//shouldn't need -- safety
167 169
168 //============================= MEASUREMENT STUFF ======================== 170 //============================= MEASUREMENT STUFF ========================
169 #ifdef MEAS__TIME_MASTER 171 #ifdef MEAS__TIME_MASTER
170 saveLowTimeStampCountInto( endStamp ); 172 saveLowTimeStampCountInto( endStamp );
171 addIntervalToHist(startStamp,endStamp,_VMSMasterEnv->stats->masterTimeHist); 173 addIntervalToHist(startStamp,endStamp,_VMSMasterEnv->stats->masterTimeHist);
172 #endif 174 #endif
173 //======================================================================== 175 //========================================================================
174 176
175 asm volatile("movl %0, %%eax; \ 177
176 movl %%esp, (%%eax); \ 178 masterSwitchToCoreLoop( masterPr )
177 movl %1, %%eax; \
178 movl %%ebp, (%%eax); \
179 movl %2, %%ebx; \
180 movl %3, %%eax; \
181 movl %4, %%esp; \
182 movl %5, %%ebp; \
183 movl $0x0, (%%ebx); \
184 jmp %%eax;" \
185 /* outputs */ : "=g" (stackPtrAddr), "=g" (framePtrAddr), \
186 "=g"(masterLockAddr) \
187 /* inputs */ : "g" (jmpPt), "g"(coreLoopStackPtr), "g"(coreLoopFramePtr)\
188 /* clobber */ : "memory", "%eax", "%ebx", "%ecx", "%edx", "%edi", "%esi" \
189 );//can probably make clobber list empty -- but safe for now
190 } 179 }
191 180
192 181
182
183 /*This has a race condition -- the coreloops are accessing their own queues
184 * at the same time that this work-stealer on a different core is trying to
185 */
186 void inline
187 stealWorkInto( SchedSlot *currSlot, VMSQueueStruc *readyToAnimateQ,
188 VirtProcr *masterPr )
189 {
190 VirtProcr *stolenPr;
191 int32 coreIdx, i;
192 VMSQueueStruc *currQ;
193
194 stolenPr = NULL;
195 coreIdx = masterPr->coreAnimatedBy;
196 for( i = 0; i < NUM_CORES -1; i++ )
197 {
198 if( coreIdx >= NUM_CORES -1 )
199 { coreIdx = 0;
200 }
201 else
202 { coreIdx++;
203 }
204 currQ = _VMSMasterEnv->readyToAnimateQs[coreIdx];
205 if( numInVMSQ( currQ ) > 0 )
206 { stolenPr = readVMSQ (currQ );
207 break;
208 }
209 }
210
211 if( stolenPr != NULL )
212 { currSlot->procrAssignedToSlot = stolenPr;
213 stolenPr->schedSlot = currSlot;
214 currSlot->needsProcrAssigned = FALSE;
215
216 writeVMSQ( stolenPr, readyToAnimateQ );
217 }
218 }
219
220 /*This algorithm makes the common case fast. Make the coreloop passive,
221 * and show its progress. Make the stealer control a gate that coreloop
222 * has to pass.
223 *To avoid interference, only one stealer at a time. Use a global
224 * stealer-lock.
225 *
226 *The pattern is based on a gate -- stealer shuts the gate, then monitors
227 * to be sure any already past make it all the way out, before starting.
228 *So, have a "progress" measure just before the gate, then have two after it,
229 * one is in a "waiting room" outside the gate, the other is at the exit.
230 *Then, the stealer first shuts the gate, then checks the progress measure
231 * outside it, then looks to see if the progress measure at the exit is the
232 * same. If yes, it knows the protected area is empty 'cause no other way
233 * to get in and the last to get in also exited.
234 *If the progress measure at the exit is not the same, then the stealer goes
235 * into a loop checking both the waiting-area and the exit progress-measures
236 * until one of them shows the same as the measure outside the gate. Might
237 * as well re-read the measure outside the gate each go around, just to be
238 * sure. It is guaranteed that one of the two will eventually match the one
239 * outside the gate.
240 *
241 *Here's an informal proof of correctness:
242 *The gate can be closed at any point, and have only four cases:
243 * 1) coreloop made it past the gate-closing but not yet past the exit
244 * 2) coreloop made it past the pre-gate progress update but not yet past
245 * the gate,
246 * 3) coreloop is right before the pre-gate update
247 * 4) coreloop is past the exit and far from the pre-gate update.
248 *
249 * Covering the cases in reverse order,
250 * 4) is not a problem -- stealer will read pre-gate progress, see that it
251 * matches exit progress, and the gate is closed, so stealer can proceed.
252 * 3) stealer will read pre-gate progress just after coreloop updates it..
253 * so stealer goes into a loop until the coreloop causes wait-progress
254 * to match pre-gate progress, so then stealer can proceed
255 * 2) same as 3..
256 * 1) stealer reads pre-gate progress, sees that it's different than exit,
257 * so goes into loop until exit matches pre-gate, now it knows coreloop
258 * is not in protected and cannot get back in, so can proceed.
259 *
260 *Implementation for the stealer:
261 *
262 *First, acquire the stealer lock -- only cores with no work to do will
263 * compete to steal, so not a big performance penalty having only one --
264 * will rarely have multiple stealers in a system with plenty of work -- and
265 * in a system with little work, it doesn't matter.
266 *
267 *Note, have single-reader, single-writer pattern for all variables used to
268 * communicate between stealer and victims
269 *
270 *So, scan the queues of the core loops, until find non-empty. Each core
271 * has its own list that it scans. The list goes in order from closest to
272 * furthest core, so it steals first from close cores. Later can add
273 * taking info from the app about overlapping footprints, and scan all the
274 * others then choose work with the most footprint overlap with the contents
275 * of this core's cache.
276 *
277 *Now, have a victim want to take work from. So, shut the gate in that
278 * coreloop, by setting the "gate closed" var on its stack to TRUE.
279 *Then, read the core's pre-gate progress and compare to the core's exit
280 * progress.
281 *If same, can proceed to take work from the coreloop's queue. When done,
282 * write FALSE to gate closed var.
283 *If different, then enter a loop that reads the pre-gate progress, then
284 * compares to exit progress then to wait progress. When one of two
285 * matches, proceed. Take work from the coreloop's queue. When done,
286 * write FALSE to the gate closed var.
287 *
288 */
289 void inline
290 gateProtected_stealWorkInto( SchedSlot *currSlot,
291 VMSQueueStruc *myReadyToAnimateQ,
292 VirtProcr *masterPr )
293 {
294 VirtProcr *stolenPr;
295 int32 coreIdx, i, haveAVictim, gotLock;
296 VMSQueueStruc *victimsQ;
297
298 volatile GateStruc *vicGate;
299 int32 coreMightBeInProtected;
300
301
302
303 //see if any other cores have work available to steal
304 haveAVictim = FALSE;
305 coreIdx = masterPr->coreAnimatedBy;
306 for( i = 0; i < NUM_CORES -1; i++ )
307 {
308 if( coreIdx >= NUM_CORES -1 )
309 { coreIdx = 0;
310 }
311 else
312 { coreIdx++;
313 }
314 victimsQ = _VMSMasterEnv->readyToAnimateQs[coreIdx];
315 if( numInVMSQ( victimsQ ) > 0 )
316 { haveAVictim = TRUE;
317 vicGate = _VMSMasterEnv->workStealingGates[ coreIdx ];
318 break;
319 }
320 }
321 if( !haveAVictim ) return; //no work to steal, exit
322
323 //have a victim core, now get the stealer-lock
324 gotLock =__sync_bool_compare_and_swap( &(_VMSMasterEnv->workStealingLock),
325 UNLOCKED, LOCKED );
326 if( !gotLock ) return; //go back to core loop, which will re-start master
327
328
329 //====== Start Gate-protection =======
330 vicGate->gateClosed = TRUE;
331 coreMightBeInProtected= vicGate->preGateProgress != vicGate->exitProgress;
332 while( coreMightBeInProtected )
333 { //wait until sure
334 if( vicGate->preGateProgress == vicGate->waitProgress )
335 coreMightBeInProtected = FALSE;
336 if( vicGate->preGateProgress == vicGate->exitProgress )
337 coreMightBeInProtected = FALSE;
338 }
339
340 stolenPr = readVMSQ ( victimsQ );
341
342 vicGate->gateClosed = FALSE;
343 //======= End Gate-protection =======
344
345
346 if( stolenPr != NULL ) //victim could have been in protected and taken
347 { currSlot->procrAssignedToSlot = stolenPr;
348 stolenPr->schedSlot = currSlot;
349 currSlot->needsProcrAssigned = FALSE;
350
351 writeVMSQ( stolenPr, myReadyToAnimateQ );
352 }
353
354 //unlock the work stealing lock
355 _VMSMasterEnv->workStealingLock = UNLOCKED;
356 }